2024年2月10日 星期六

SIP MWI

RFC3842 SIP event package for MWI

Alice 訂閱 message-summary 狀態 1 天,馬上同步目前狀態。refresh 或停止訂閱也會同步目前狀態。Via 省略 for clarity。

  • Event 帶 event package 名稱「message-summary」。
  • Accept 帶 NOTIFY 批體使用的 MIME 格式是 application/simple-message-summary
SUBSCRIBE sip:alice@vmail.example.com SIP/2.0
To: <sip:alice@example.com>
From: <sip:alice@example.com>;tag=78923
Date: Mon, 10 Jul 2000 03:55:06 GMT
Call-Id: 1349882@alice-phone.example.com
CSeq: 4 SUBSCRIBE
Contact: <sip:alice@alice-phone.example.com>
Event: message-summary
Expires: 86400
Accept: application/simple-message-summary
Content-Length: 0

回應

SIP/2.0 200 OK
To: <sip:alice@example.com>;tag=4442
From: <sip:alice@example.com>;tag=78923
Date: Mon, 10 Jul 2000 03:55:07 GMT
Call-Id: 1349882@alice-phone.example.com
CSeq: 4 SUBSCRIBE
Expires: 86400
Content-Length: 0

馬上同步目前狀態:2 new and 8 old [2 urgent] 語音留言

  • Event 帶 message-summary。
  • Subscription-State 是 active。
  • Content-Type: application/simple-message-summary 表示批體的格式。
NOTIFY sip:alice@alice-phone.example.com SIP/2.0
To: <sip:alice@example.com>;tag=78923
From: <sip:alice@example.com>;tag=4442
Date: Mon, 10 Jul 2000 03:55:07 GMT
Call-Id: 1349882@alice-phone.example.com
CSeq: 20 NOTIFY
Contact: <sip:alice@vmail.example.com>
Event: message-summary
Subscription-State: active
Content-Type: application/simple-message-summary
Content-Length: 99

Messages-Waiting: yes
Message-Account: sip:alice@vmail.example.com
Voice-Message: 2/8 (0/2)

批體的格式

message-summary = msg-status-line CRLF
                   [msg-account CRLF]
                   [*(msg-summary-line CRLF)]
                   [ *opt-msg-headers ]

msg-status-line  = "Messages-Waiting" HCOLON msg-status
msg-status = "yes" / "no"
msg-account = "Message-Account" HCOLON Account-URI
Account-URI = SIP-URI / SIPS-URI / absoluteURI

msg-summary-line = message-context-class HCOLON newmsgs SLASH oldmsgs
                [ LPAREN new-urgentmsgs SLASH old-urgentmsgs RPAREN ]

opt-msg-headers = CRLF 1*(extension-header CRLF) ;新訊息 heeaders

message-context-class 見 Section 6.2 of RFC 3458 [7].
newmsgs = msgcount
oldmsgs = msgcount
new-urgentmsgs = msgcount
old-urgentmsgs  = msgcount
msgcount = 1*DIGIT   ; MUST NOT exceed 2^32-1

新訊息通知。新訊息一些 headers appended。

NOTIFY sip:alice@alice-phone.example.com SIP/2.0
To: <sip:alice@example.com>;tag=78923
From: <sip:alice@example.com>;tag=4442
Date: Mon, 10 Jul 2000 04:28:53 GMT
Contact: <sip:alice@vmail.example.com>
Call-ID: 1349882@alice-phone.example.com
CSeq: 31 NOTIFY
Event: message-summary
Subscription-State: active
Content-Type: application/simple-message-summary
Content-Length: 503

Messages-Waiting: yes
Message-Account: sip:alice@vmail.example.com
Voice-Message: 4/8 (1/2)

To: <alice@atlanta.example.com>
From: <bob@biloxi.example.com>
Subject: carpool tomorrow?
Date: Sun, 09 Jul 2000 21:23:01 -0700
Priority: normal
Message-ID: 13784434989@vmail.example.com

To: <alice@example.com>
From: <cathy-the-bob@example.com>
Subject: HELP! at home ill, present for me please
Date: Sun, 09 Jul 2000 21:25:12 -0700
Priority: urgent
Message-ID: 13684434990@vmail.example.com

使用 Callee Capabilities 和 Caller Preferences 的範例。使用 callee capabilities,notifier 進行註冊,Contact 含有參數 methods 和 events。為了表明身份,可以加 actor="msg-taker"。

REGISTER sip:sip3-sj.example.com SIP/2.0
To: <sip:alice@example.com>
From: <sip:alice@example.com>;tag=4442
...
Contact: <sip:alice@vm13-sj.example.com>
 ;actor="msg-taker";methods="SUBSCRIBE"
 ;automata;events="message-summary"

SUBSCRIBE 可看到上述的 Contact 參數。

SUBSCRIBE sip:alice@example.com SIP/2.0
...
Accept: application/simple-message-summary
Event: message-summary
Accept-Contact: *;automata;actor="msg-taker"

2024年2月7日 星期三

Q.931 CALL PROCEEDING

Q.931 訊息是被叫送給主叫表示已經開始建立請求的呼叫,不再接受其它呼叫建立資訊。

什麼是 call establishment information (呼叫建立資訊)?

可有下列選擇性 IE

  • Channel identification:如果是回應 SETUP 的第一個訊息,在 network-to-user 是必要的,在 user-to-network 不接受 SETUP 的 B-channel 時是必要的。
  • Progress indicator:interworking 提供 in-band information/patterns。在 user-to-network if Annex K is implemented or in accordance with the procedures of 5.11.3 and 5.12.3.
  • Display (限 n → u):最長 34 或 82 octets。
  • Bearer capability:included when the procedures of 5.11 for bearer capability selection apply. When present, progress indication No. 5, interworking has occurred and has resulted in a telecommunication service change, shall also be present.
  • High layer compatibility:included when the procedures of 5.12 for high layer compatibility selection apply. When present, progress indication No. 5, interworking has occurred and has resulted in a telecommunication service change, shall also be present.

SIP interworking

  • 收到 CALL PROCEEDING (撥入都用 En bloc,全部撥號都在 SETUP,沒有撥號結束的問題。)
    • 忽略?
    • 送 183?會被誤認為陣鈴?如果 183 /wo SDP 呢?
  • 送 CALL PROCEEDIN
    • ALERTING 前,也就是收到 180 或 183 /w SDP?
    • 收到 183 /wo SDP?
    • 送 SIP INVITE 前?
    • 認為撥號結束?

參考

  1. Q.931 3.1.2

2024年2月2日 星期五

UTC GMT

GMT (Greenwich Mean Time,格林威治標準時間):原理是平均太陽日。英國倫敦的格林威治皇家天文台觀察一整年「過中天」的時間,得到平均一天的長度,再細分成時、分、秒等。這個時間概念 1884 年確立,並往歐陸其他國家擴散。1924 開始,每小時就會向全世界播報時間。

後來有更穩定的觀測太陽日的方法,而不一定要在格林威治天文台,稱為世界時 (Universal Time,UT),有三種版本,UT0 是原始的觀測結果計算值,UT1 則是修正了地球在長時間尺度下會產生的自轉軸漂移的影響,UT2 則是為了研究需求更多修正了季節性的影響。基本上 UT1 和 GMT 是一致的。

由於地球的自轉正在緩慢減速,導致平均太陽日會逐漸變長,GMT 的 1 秒會愈來愈久,會變動對度量衡而言並不適合。1967 國際度量衡大會把秒的定義改成銫原子固定震盪次數的時間 (原子鐘),依此計算時間與日期的系統稱作國際原子時 (International Atomic Time, TAI),和地球自轉脫節了。。

國際原子時有個問題是長久下來會超前 GMT 愈來愈多。以國際原子時為基準,選擇適當的時間加入閏秒,來和 GMT 時間對齊,叫做協調世界時 (Universal Time Coordinated,UTC)。由於 UTC 相關國際度量衡標準,目前所有的國際通訊系統,像是衛星、航空、GPS 等,都採用 UTC 時間。

參考

  1. https://pansci.asia/archives/84978

2024年1月26日 星期五

disable anydesk autostart

systemctl disable anydesk.service

「systemctl list-unit-files」可列出系統服務。另外 LXQT 有另一套 autostart 在 /etc/xdg/autostart。

參考

2024年1月5日 星期五

copy_from_user() 和 copy_to_user()

unsigned long copy_from_user (void *to, const void *from, unsigned long count);
unsigned long copy_to_user (void *to, const void *from, unsigned long count);

在 read()、write()、或 ioctl() 等,可能需要在 user-space 和 kernel-space 間搬移資料,本質是 memcpy(),回傳未完成的 byte 數。此外也 access_ok() 檢查 user-space 指標是否 valid。另外有 __get_user() 和 __put_user()。

User-space 資料

  • user-space 指標在 kernel 未必是對的,和運行的 architecture 和 kernel 的設定有關。(有不對的例子嗎?)
  • user-space 資料可能 page-out 而不在記憶體中,直接存取會造成 page fault OOPS。
  • user program 提供的 pointer 可能是 buggy or malicious,而開了一個後門造成安全問題。例如指到 kernel-space 並寫入資料,變成 kernel 資料無故被改變。

如果不需要 access_ok() 檢查,例如已經檢查過,__copy_to_user()、__copy_from_user()、__get_user() 和 __put_user() 版本。

driver accesses user space must be reentrant, must be able to execute concurrently with other driver functions, and, in particular, must be in a position where it can legally sleep.

參考

  1. LDD chap 3
  • LDD chap 6

2023年12月16日 星期六

Fixed-point arithmetic 定點運算

fixed-point 是一種小數表示方式,儲存固定位數小數。某個固定單位的部份,皆可視為小數。

閱讀時,有小數點分隔整數和小數。但在內部處理程式,並沒有實際的分隔,而由程式隱含定義位置。

低成本嵌入式處理器,沒有浮點運算單元,有些計算需要定點運算適當的 scaling 來達到可接受的動態範圍。定點運算可以比浮點運算更快、更高精確度,但寫程式需要更精確預測數值範圍,調整 scaling facotr,避免 overflow。

表示法

  • Qm.n:通常是有號數,不含 sign-bit,整數部份 m-bit,小數部份 n-bit。在 ARM,m 包含 sign-bit。
  • UQm.n:無號數,整數部份 m-bit,小數部份 n-bit。
  • Qn:小數部份 n-bit。

加法和減法:相同 scaling factor,結果 scaling factor 不變。overflow。

乘法:scaling factor 相乘。在二進位,通常使用 scaling factor that is a power of two,乘完後 scaling factor 可透過 shifting right 調整回來。Rounding 可在 shift 前加上 scaling factor 的一半。證明:round(x/y) = floor(x/y + 0.5) = floor((x + y/2)/y) = shift-of-n(x + 2^(n-1))。

// saturate to range of int16_t
int16_t sat16(int32_t x)
{
	if (x > 0x7FFF) return 0x7FFF;
	else if (x < -0x8000) return -0x8000;
	else return (int16_t)x;
}

int16_t q_mul(int16_t a, int16_t b)
{
    int16_t result;
    int32_t temp;

    temp = (int32_t)a * (int32_t)b; // result type is operand's type
    // Rounding; mid values are rounded up
    temp += 1 << (Q - 1);
    // Correct by dividing by base and saturate result
    result = sat16(temp >> Q);

    return result;
}

除法 https://lirobo.blogspot.com/2021/08/rounding-integer-division.html

int16_t q_div(int16_t a, int16_t b)
{
    int32_t temp = (int32_t)a << Q;
    // Rounding: mid values are rounded up.
    if (((temp >> 31) & 1) == ((b >> 15) & 1)) {
        temp += b>>1; // round up
    } else {
        temp -= b>>1; // round down for negative value
    }
    return (int16_t)(temp / b);
}

參考

  • https://en.wikipedia.org/wiki/Fixed-point_arithmetic
  • libfixmath

2023年12月15日 星期五

Rounding

Rounding 是用較短的、簡單的、或 more explicit 的近似值表示數值,例如 $23.4476 用 $23.45, 表示、,the fraction 312/937 用 1/3 表示,或 √2 用 1.414 表示。

捨棄低位元時,增加尾數 0 或減少小數位數,決定是否進位。

round down:捨去,是指 round toward zero,但在 2’s complement 數值會是 round toward negative infinity,負值是有所不同。

round up:進位。

round toward zero:無條件捨去法。正數捨去低位元是變小,負數捨去低位元是捨去一些負值而變大,所以都是趨向 0。ROUNDDOWN() truncate()

round away from zero:無條件進位法。捨去低位元時一律進位,正數是變大,負數是變小,所以都是遠離 0。ROUNDUP()

round toward negative infinity:程式的無條件捨去法。一般程式數值用 2’s complement,正數捨去低位元是變小,負數捨去低位元也是變小。floor()

round half down

round half up

round half toward zero (五捨六入)

round half away from zero (四捨五入)

round half toward even (四捨六入五成雙)

round half toward odd

round half alternatively

round half randomly

stochastic round

參考

  1. https://en.wikipedia.org/wiki/Rounding

truncation:無條件捨去法
round (round-to-nearest)

  • 傳統是四捨五入,只看一位是五就進位,其實五居中 (biased rounding)
  • convergent rounding:不只看一位
  • unbiased rounding:如果剛好居中,只會變成是偶數。

SIP header Via

所有 SIP 訊息 都要有 Via,縮寫 v。一開始的 UAC 和後續途經的每個 proxy 都會疊加一個 Via 放傳送的位址,依序作為回應的路徑。 格式 sent-protocol sent-by [ ;branch= branch ][ ; 參數 ...] s...