星期日, 12月 18, 2022

M.2 interfce

M.2 是電腦內部擴充卡規格,可提供 PCIe、SATA 和 USB 等介面的裝置,如 Wi-Fi、SSD 等。

M.2 模組長方形,一端有雙面金手指接腳,另一端有半圓形孔,允許寬度有 12、16、22、和 30 mm,長度有 16、26、30、38、42、60、80、和 110 mm,例如模組碼 2280 代表 22 mm 寬、80 mm 長。接腳雙面共有 75 個位置,最多使用 67 pins,0.5 mm pitch、背面插中間。

考量 PCIe/SATA/USB 等各自介面的訊號和電氣特性不同,定義了多組的插槽防呆鍵位 (目前實體產品有 A/B/E/M key) 來避免誤插而導致運作不正常或損壞。

Wi-Fi/Bluetooth、GPS 或 NFC 介面卡等 M.2 模組,原先多屬於 USB 介面,主要採用 A/E key。

目前 M.2 SSD 大多是 M key 2280 模組,採用 NVMe PCIe x4。而 B+M 模組有 2242 和 2280,主要是 SATA、少數 PCIe x2。此外類似的有 mSATA、Apple 用的。

支援 M.2 SSD 插槽有兩種類型:B key (另稱socket 2) 和 M key (另稱socket 3)。

設備插座KeySSD 介面
筆電2B可能僅支援 SATA PCIe ×2, SATA
3MPCIe or SATAPCIe ×4, SATA


插座Key大小裝置
?A1630, 2230, 3030WiFi, Bluetooth, NFC and/or WiGig2 of PCIe ×1, USB 2.0, I2C and DP ×4
Socket 1E1630, 2230, 3030WiFi, Bluetooth, NFC and/or GPS/GNSS2 of PCIe x1, USB 2.0, I2C, SDIO, UART, PCM and CNVi
Socket 2B3042, 2230, 2242, 2260, 2280, 221103G/LTE + GPS/GNSS or SSDPCIe ×2, SATA, USB 2.0 and 3.0, audio, UIM, HSIC, SSIC, I2C and SMBus
Socket 3M2242, 2260, 2280, 22110SSD with PCIe or SATA interfacePCIe ×4, SATA and SMBus

SATA SSD 是 B+M-keyed,可放在 B-keyed 或 M-keyed 插座

NVMe PCIe x4 SSD 是 M-keyed,不能放在 B-keyed 插座

PinAEBMB 說明
1GNDGNDCONFIG_3CONFIG_3
23.3V3.3V3.3V3.3 V
3USB D+USB D+GNDGND
43.3V3.3V3.3V3.3V
5USB D-USB D-GNDPERn3
6LED1#LED1#FULL CARD POWER OFF#N/A通知 Host
7GNDGNDUSB_D+PERp3
8APCM_CLK/I2S SCKW_DISABLE#N/A通知 Host
9SDIO CLK/SYSCLKUSB_D-GND
10PCM SYNC/I2S WSGPIO_9/ DAS/DSSDAS/DSS
11SDIO CMDGNDPETn3
12PCM_IN/I2S_INx3.3 V
13SDIO DATA0PETp3
14PCM_OUT/I2S SD_OUT3.3 V
15SDIO DATA1GND
16LED2#LED2#3.3V
17ASDIO DATA2PERn2
18GNDGND3.3 V
19ASDIO DATA3PERp2
20AUART WAKE#GPIO_5N/A
21ASDIO WAKE#CONFIG_0CONFIG_0
22AUART RxDGPIO_6N/A
23ASDIO_RESET#/ TX_BLANKINGGPIO_11PETn2
24AEGPIO_7N/A
25ADPRPETp2模組輸出
26AGPIO_10N/A
27AGNDGND
28AGPIO_8N/A
29APERn1 / USB RX- /SSIC-RxN
PERn1   or USB 3.0 RX-
30AUIM-RESETN/A
31APERp1 / USB RX+ / SSIC-RxP
PERp1   or USB 3.0 RX+  
32AUART TxDUIM-CLKN/A   Host 輸出
33GNDGNDGNDGND
34AUART CTSUIM-DATAN/A
35PETp0PETp0PETn1 / USB TX- / SSIC-TxN
PETn1   or USB 3.0 TX-
36AUART RTSUIM-PWRN/A   Host 輸出
37PETn0PETn0PETp1 / USB TX+ / SSIC-TxP
PETp1   or USB 3.0 TX+
38Vendor definedVendor definedDEVSLPDEVSLP
39GNDGNDGNDGND
40Vendor definedVendor definedGPIO_0/SMB_CLKN/A
41PERp0PERp0SATA-B+/PERn0SATA-B+/PERn0
42Vendor definedVendor definedGPIO_1/SMB_DATAN/A
43PERn0PERn0SATA-B-/PERp0SATA-B-/PERp0
44COEX3COEX3GPIO_2/ALERT#N/A
45GNDGNDGNDGND
46COEX2COEX_RxDGPIO_3N/A
47PEFCLKP0PEFCLKP0SATA-A-/PETn0SATA-A-/PETn0
48COEX1COEX_TxDGPIO_4N/A
49PEFCLKN0PEFCLKN0SATA-A+/PETp0SATA-A+/PETp0
50SUSCLKSUSCLKPERST#PERST#
51GNDGNDGNDGND
52PERST0#PERST0#CLKREQ#CLKREQ#   Reference clock request signal
53CLKREQ0#CLKREQ0#REFCLKNREFCLKN   PCIe Reference Clock signals (100 MHz)
54W_DISABLE2#W_DISABLE2#PEWAKE#PEWAKE#   PCIe WAKE# Open Drain with pull up on platform. Active Low.
55PEWake0#PEWake0#REFCLKPREFCLKP  PCIe Reference Clock signals (100 MHz)
56W_DISABLE1#W_DISABLE1#MFG1MFG1
57GNDGNDGNDGND
58SMB_DATAI2C_DATAMFG2MFG2
59ReservedReserved/PETp1ANTCTL0xHost 輸出
60SMB_CLKI2c_CLKCOEX3Host 輸出
61ReservedReserved/PETn1ANTCTL1Host 輸出
62ALERT#ALERT#COEX_TXDHost 輸出
63GNDGNDANTCTL2Host 輸出
64ReservedReservedCOEX_RXDHost 輸出
65ReservedReserved/PERp1ANTCTL3Host 輸出
66UIM_SWPUIM_SWP/PERST1#SIM DETECT模組輸出
67ReservedReserved/PERn1RESET#N/A模組輸出
68UIM_POWER_SNKUIM_POWER_SNK/ CLKREQ1#SUSCLKSUSCLK   Host 輸出 32.768 kHz
69GNDGNDCONFIG_1CONFIG_1
70UIM_POWER_SRCUIM_POWER_SRC/ PEWAKE1#3.3V/VBAT3.3 V
71ReservedReserved/REFCLKp1GNDGND
723.3V3.3V3.3V/VBAT3.3 V
73ReservedReserved/REFCLKn1GNDGND
743.3V3.3V3.3V/VBAT3.3 V
75GNDGNDCONFIG_2CONFIG_2

CONFIG_x:Defines module type

DAS/DSS:Device Activity Signal / Disable Staggered Spinup

PERST#:PCIe reset

 

PERp0、PERn0、PETp0、PETn0、PERp1、PERn1、PETp1、PETn1、PERp2、PERn2、PETp2、PETn2、PERp3、PERn3、PETp3、PETn3:PCIe Lane

DEVSLP:Device Sleep,high 是 host 通知 SSD 進入 low power state.

MFGx:Manufacturing pin. Use determined by vendor.

參考

  1. https://pinoutguide.com/HD/M.2_NGFF_connector_pinout.shtml
  2. https://www.autobuy.tw/article_f85d01

星期日, 12月 04, 2022

ASUS ExpertBook B1400CEAE

ASUS ExpertBook B1:1.45 公斤級 810H 軍規

B1400CEAE (i5-1135G7) $32880 (2022/?? 公司購買)

  • i5-1135G7 2.4~4.2 GHz,8M Cache,4 cores,Iris Xᵉ Graphics
  • 14吋 FHD (1920 x 1080) 16:9 IPS霧面/Anti-glare display/NTSC: 45%
  • 16G DDR4 on board
  • 512GB M.2 NVMe™ PCIe® 3.0 SSD (最高 1TG)
  • HDD 擴充最多 2TG
  • 32.34 x 21.56 x 1.92 cm, 1.45 kg
  • I/O連接埠
    • 1x USB 2.0 Type-A
    • 1x USB 3.2 Gen 1 Type-C support display / power delivery
    • 2x USB 3.2 Gen 2 Type-A
    • 1x HDMI 1.4
    • 1x VGA Port (D-Sub)
    • 1x 3.5mm Combo Audio Jack
    • 1x RJ45 Gigabit Ethernet
    • 1x DC-in 65W
  • 內建喇叭麥克風、WebCam
  • Wi-Fi 6(802.11ax), BT5.2 (Dual band) 2*2
  • Windows 11 專業版
  • BIOS 版本 B1400CEAEY.318, 2022/6/17
  • 長按電源鍵按鈕15秒強制關機。
  • 20230718 安裝 Lubuntu 23.04 (Lunar Lobster 月光龍蝦)
  • 20230719 由於編譯問題改安裝 Lubuntu 20.04.5
  • Hibernate 沒作用

B1400CEPE (i5-1135G7) $35880

  • 額外 MX330/2G 顯示
  • 記憶體多 1 DIMM
  • 多 2TB 5400RPM HDD
  • USB-C 換 1x Thunderbolt™ 4,多 SD Card Reader
  • 90W 電源

B1400CBA-0031A1235U 2022/11/09 $32990

  • i5-1235U 1.3 GHz (12M Cache, up to 4.4 GHz, 10 cores)
  • 14.0 吋 FHD (1920 x 1080) 16:9/LED Backlit/250nits//Anti-glare display/NTSC: 45%
  • 16G DDR4 on board
  • 512GB M.2 NVMe™ PCIe® 4.0 SSD
  • HDD 擴充最大 2TG
  • IO連接埠
    • 1x USB 2.0 Type-A
    • 1x USB-C Thunderbolt™ 4 40Gbps/顯示/電源
    • 2x USB 3.2 Gen 2 Type-A
    • 1x HDMI 2.0 (1.4?)
    • 1x VGA Port (D-Sub)
    • 1x 3.5mm Combo Audio Jack
    • 1x RJ45 Gigabit Ethernet
    • microSD
    • 1x DC-in 65W
  • ? 內建喇叭麥克風
  • Wi-Fi 6(802.11ax) (Dual band) 2*2 + Bluetooth 5.2
  • WebCam
  • NumberPad、FingerPrint、Backlit Chiclet Keyboard
  • MyASUS in WinRE
  • Windows 11 Pro
  • 3年國際保固/3年國內到府維修

Linux 安裝

  • 備份原開機作業系統?https://lirobo.blogspot.com/2007/09/clonezilla-live.html
  • 換 SSD?加硬碟?SATA 速度?PCIe 3.0 速度?Gen 4 或 Gen 4
  • Crucial MX500 2TB SATAⅢ SSD 讀560M/寫510M/3D TLC/五年保 $4199, 1TB $2199
  • Crucial BX500 2TB SATAⅢ SSD 讀540M/寫500M/3D TLC/三年保 $4180, 1TB $1999
  • Kingston KC600 2TB SATA-3 2.5吋 SSD (SKC600/2048G) 讀550M/寫520M/3D TLC/五年保 $6888
  • Micron Crucial P2 1TB M.2 2280 PCIe SSD 讀2400MB/s;寫1800MB/s;五年保 $2088
  • Kingston NV1 1TB NVMe PCIe SSD 讀2100M/寫1700M/QLC/三年保 $1980
  • Kingston NV2 1TB Gen4 PCIe SSD 讀3500M/寫2100M/三年保 $1980
  • Kingston KC3000 1TB PCIe 4.0 NVMe M.2 SSD 讀7000M/寫6000M/3D TLC/五年保 $2988
  • Kingston FURY Renegade 1TB PCIe 4.0 NVMe M.2 SSD 讀7300M/寫6000M/3D TLC/五年保 $3699
  • 雙開機?https://lirobo.blogspot.com/2013/01/vaio-s13-ubuntu-1210.html

星期日, 11月 20, 2022

USB Hub

USB-C 充電,USB-C

HDMI

Ethernet

Thunderbolt

CX1 USB-C Gen2 10Gbps 十合一極速轉接器 (CX1G2) $2190

  • 輸入端:USB3.1 Type C Male,線長 17 cm(不含 USB-C 端)
  • USB-C PD3.0 Pass-through 充電,最高 100W。不支援資料傳輸,保留 15W 給外接裝置。保留足夠電力給硬碟。
  • USB-C 3.1(3.2):SuperSpeed+ Gen2 傳輸速度最高達 10Gbps
  • 2x USB-A 3.1(3.2):SuperSpeed+ Gen2 傳輸速度最高達 10Gbps
  • USB-A 3.0:傳輸速度最高達 5Gbps
  • HDMI 2.0:最高達 4K x 2K @ 60Hz,支援 HDCP2.3, HDCP2.2 和 HDCP1.4 加密協定,支援 HDR 高動態色深影像輸出。需要支持 DisplayPort 1.4 Alt Mode 的電腦才可傳輸 4K@60Hz 影像
  • SD 和 MicroSD SD4.0/UHS-II 讀卡機,傳輸速度最高達 312MB/s。兩張卡片可同時互相讀寫 (Dual-LUN)。
  • RJ45 網路口:10/100/1000Mbps
  • 3.5mm 耳機埠:支援聲音輸出與麥克風輸出
  • 127.5mm(長)x 56mm(寬)x 16mm(厚)
  • 鋁合金 108g
  • 保固:18 個月

CASA Hub X USB-C 十合一集線器 $2680

  • USB-C 3.1 Gen 2 編織線長
  • USB-C 3.1 Gen 1 5Gbps, PD 最高 100W。
  • USB-C 3.1 Gen 1 5Gbps, 充電 7.5W
  • 2x USB-A 3.1 Gen 1
  • HDMI 4K @ 60Hz,電腦 DisplayPort 1.4 以上
  • VGA 1080@60Hz
  • SD 和 MicroSD
  • RJ45 10/100/1000 Mbps
  • 3.5mm 音源孔 48kHz 16-bit 立體輸出
  • 258mm(長)x 50mm(寬)x 17mm(厚)
  • 鋁合金 90g
  • 保固:3 年

Thunderbolt 對 Gigabit 乙太網路轉換器 NT$990

Belkin Thunderbolt™ 4 多功能集線器 $14990

  • Thunderbolt 4 上行
  • Thunderbolt 4 下行
  • USB-C 3.1 Gen2: 10Gbps, 20W PD, 兼容 QC4.0
  • 2x USB-A 3.1 Gen2 10Gbps
  • 2x USB-A 2.0 480Mbps
  • 2x HDMI 2.0 4K@60Hz
  • SD4.0
  • 千兆乙太網路
  • 3.5mm 音訊輸入/出 
  • 直通電源 (不能 USB?)

星期六, 11月 05, 2022

Thunderbolt

Intel 的 Thunderbolt™ 在第一代和第二代結合 Apple 的 Mini DisplayPort,第三代開始改結合 USB-C,能以雙向各 40 Gbit/s傳輸資料。Thunderbolt 4 以不變的 40 Gbps 頻寬支援電源、資料和視訊。而 USB 纜線、裝置和連接埠的資料速度範圍可能有限,而且不保證顯示器品質。

\Thunderbolt 4Thunderbolt 3USB4USB3/DP
線長達 2 米yes---
總頻寬40Gb/s40Gb/s20Gb/s10Gb/s
顯示2x 4K @60Hz
1x 8K @30Hz
至少 1x 4K1x1x
資料速度PCIe 32Gb/s
USB 3.2 - 10Gb/s
PCIe 16Gb/s
USB 3.2 - 10Gb/s
USB 3.2 - 10Gb/sUSB 3.2 - 5Gb/s
充電yes---
喚醒yes---
供電15W15W7.5W4.5W
Thunderbolt networkingyesyes--
\Thunderbolt™ 4Thunderbolt™ 3USB4USB3/DP

Thunderbolt 3 涵蓋 USB 3.1、DisplayPort 1.2 和 PCIe。

PCIe 可供你外接顯卡擴充效能?

參考:https://www.kocpc.com.tw/archives/382652 

https://www.intel.com.tw/content/www/tw/zh/architecture-and-technology/thunderbolt/thunderbolt-4-vs-usb-c.html

星期六, 10月 22, 2022

四軸quadcopter

空拍機 穿越機

光流定位:一種影像定位方式

室內無人機:定位方式 (機器視覺和紅外線),螺旋槳保護。大疆 Avata。

空拍

工具

CoDrone

Cetus Pro FPV 無刷馬達 $6500

  • 多樣的模式。
  • 定位系統:光流/氣壓計/激光
  • 高度定位:激光/氣壓計
  • 定位:光流(有效高度0.3-3m)
  • 自動懸停精度:水平±0.2m,垂直±0.3m(無風環境下)
  • 接收器協議:Frsky D8
  • 飛控:Lite 1-2S Pro 無刷FC
  • VTX:25mW
  • 相機傾斜度:30°
  • 相機:C02 FPV微型攝像機
  • 發射機: Literadio2 SE 發射器
  • TX模式:模式1&模式2可選
  • 馬達: 1102-18000KV無刷電機
  • 螺旋槳:40mm 3-blade 1.5mm 軸孔
  • 起飛重量:33.19g(不含電池)
  • 動力系統:1S
  • 電池: BT2.0 450mah 1S 電池 (外部的)
  • 飛行時間:4-5分鐘
  • 飛行距離:80m(寬廣無干擾環境)
  • 風鏡:VR02 FPV Goggles:300g 內建 2hr 3.7V/2000mAh 電池 USB-C 充電,4.3 inch 螢幕 800*480 300cd/m2 55° 視角,內建 5.8G 14DBI 天線,頻率搜尋功能。

https://hom-wang.gitbooks.io/quadcopter/content/

星期六, 10月 15, 2022

MacBook Air (M1, 2020)

Apple M1 晶片:4 效能核心、4 節能核心、7 GPU 核心、16 核心神經網路引擎

16GB RAM

1TB SSD

兩個 Thunderbolt/USB 4,可支援:

  • 充電
  • DisplayPort
  • Thunderbolt 3 (速度可達 40Gb/s):為什麼不能稱為 Thunderbolt 4?
  • USB 4 (速度可達 40Gb/s)
  • USB 3.1 Gen 2 (速度最高可達 10Gb/s)

3.5 公釐耳麥插孔

截圖

  • 整個螢幕:Shift、Command 和 3
  • 框選部分螢幕:Shift、Command 和 4
  • 單一視窗:Shift、Command、4 和空白鍵,選擇視窗或選單。若要排除視窗的陰影,選擇時同時按住 Option 鍵。
  • 開啟「截圖」App:Shift、Command 和 5,可以設定計時器等。
  • 截圖拷貝至剪貼板,在拍攝截圖的同時按住 Control 鍵。

看照片

  • 一般的開啟方式是直接點選:開啟「預覽程式」一張圖一個個點每次看一張。
  • 框選要看的照片按下「空白鍵」開啟「快速預覽」,可使用左右鍵切換圖片,也可以使用左上角的圖片選單篩選要觀看的圖片。
  • 進入照片資料夾點選進入到「圖庫」模式。顯示方式可「隱藏測邊欄(左側)」及「隱藏預覽(右側)」。

參考

  1. https://www.apple.com/tw/macbook-air-m1/specs/

星期日, 10月 09, 2022

LED Strip

AC110V

DC12V

Q.931 Called party number

Q.931 Channel Identification (ITU Q.931 section 4.5.8)
87654321Octet
IE ID: 0x70 1
Length2
ext.
1
Type of numberNumber plan identification3
0Number digits (IA5 characters)4
etc.
  • Type of number
  • Number plan identification
  • Number digits:IA5 (International Alphabet No. 5) 字元也就是 ITU-T Recommendation T.50,也就是 ASCII。

參考

星期六, 10月 01, 2022

syscall prctl()

process control

#include <sys/prctl.h>

int prctl(int option, unsigned long arg2, unsigned long arg3,
          unsigned long arg4, unsigned long arg5);

第一個引數決定要做什麼和定義後續引數怎麼用,可以是:

  • PR_CAP_AMBIENT (Linux 4.3+)
  • PR_CAPBSET_READ (Linux 2.6.25+)
  • PR_CAPBSET_DROP (Linux 2.6.25+)
  • PR_SET_CHILD_SUBREAPER (Linux 3.4+)
  • PR_GET_CHILD_SUBREAPER (Linux 3.4+)
  • PR_SET_DUMPABLE
  • PR_GET_DUMPABLE
  • PR_SET_KEEPCAPS
  • PR_GET_KEEPCAPS
  • PR_MCE_KILL (Linux 2.6.32+)
  • PR_MCE_KILL_GET (Linux 2.6.32+)
  • PR_SET_MM (Linux 3.3+)
  • PR_MPX_ENABLE_MANAGEMENT, PR_MPX_DISABLE_MANAGEMENT (since Linux 3.19+)
  • PR_SET_NAME (Linux 2.6.9+):設定自己 thread 名稱為 (char *)arg2,和 pthread_setname_np() 設定的、pthread_getname_np() 取得的屬性一樣,也可以透過 /proc/self/task/[tid]/comm 存取 (Linux 2.6.33+)。
  • PR_GET_NAME (Linux 2.6.11+):在 (char *)arg2 給 buffer 供回傳 thread 名稱字串,buffer 應該要有 16-byte 空間。
  • PR_SET_NO_NEW_PRIVS (Linux 3.5+)
  • PR_GET_NO_NEW_PRIVS (Linux 3.5+)
  • PR_SET_PDEATHSIG
  • PR_GET_PDEATHSIG
  • PR_SET_PTRACER (Linux 3.4+)
  • PR_SET_SECCOMP (Linux 2.6.23+)
  • PR_GET_SECCOMP (Linux 2.6.23+)
  • PR_SET_SECUREBITS (Linux 2.6.26+)
  • PR_GET_SECUREBITS (Linux 2.6.26+)
  • PR_GET_SPECULATION_CTRL (Linux 4.17+)
  • PR_SET_SPECULATION_CTRL (Linux 4.17+)
  • PR_SET_THP_DISABLE (Linux 3.15+)
  • PR_TASK_PERF_EVENTS_DISABLE (Linux 2.6.31+)
  • PR_TASK_PERF_EVENTS_ENABLE (Linux 2.6.31+)
  • PR_GET_THP_DISABLE (Linux 3.15+)
  • PR_GET_TID_ADDRESS (Linux 3.5+)
  • PR_SET_TIMERSLACK (Linux 2.6.28+)
  • PR_GET_TIMERSLACK (Linux 2.6.28+)
  • PR_SET_TIMING
  • PR_GET_TIMING
  • PR_SET_TSC (Linux 2.6.26+, x86 only)
  • PR_GET_TSC (Linux 2.6.26+, x86 only)
  • PR_SET_UNALIGN
  • PR_GET_UNALIGN

參考:man prctl

星期六, 9月 24, 2022

IPv6

Fragment

IPv6 基本上不做 Fragment,先透過 Path MTU Discovery 讓傳送端依照 MTU 送,就不需要 Fragment,所以標準 IPv6 封頭沒有 Fragmentation 支援。

透過 extension header,仍可以包含 fragmentation 資訊。

https://lirobo.blogspot.com/2022/09/internet-protocol.html

IPv4

16-bit Identification (ID)

  • 規定在特定來源位址、目的位址、和協定下,每個 datagram 要有不同值,但實際只用在 Fragmentation 和 Reassembly,Fragment 後沿用同樣 ID。其它有些實作並不改變 ID。
  • 在 maximum datagram lifetime (MDL) 內會有唯一的值,和建議的 reassembly timeout 有關,典型 MDL 是 2 分鐘 [RFC791] [RFC1122]。這樣限制了兩點間單一協定在典型 MTU 1500 時的速度上限為 6.4 Mbps [RFC4963],在高速裝置常會違反而變得沒有意義。
  • RFC1122 提到的 datagram de-duplication 用途,仍可以如同 IPv6 unfragmented datagrams 用 hash-based duplicate detection 達成 [RFC6621]。
  • 不改變 ID 導致如 RObust Header Compression (ROHC) [RFC5225] 支援。
  • 參考 RFC6864

3-bit Flags (0DM)

  • 0-bit:保留,沒用。
  • D-bit 表示 Do not Fragment (DF),不進行 Fragment。可用做 path MTU discovery。
  • M-bit 表示 More Fragments (MF)。Fragment 後沿用同樣 ID,除了最後封包外都設 M-bit。

13-bit Fragment Offset

  • IP Payload 的 Offset,採 8 的倍數。

IPv4 位址

https://lirobo.blogspot.com/2022/09/internet-protocol.html

https://lirobo.blogspot.com/2018/08/ipv4-and-ipv6-identification-and.html

https://lirobo.blogspot.com/2010/03/ipv4-link-local-addressing.html

https://lirobo.blogspot.com/2014/09/ether-type.html

星期六, 9月 17, 2022

Internet Protocol

IPv4

IPv6

https://lirobo.blogspot.com/2018/08/simple-history-of-ip-protocol_23.html

https://lirobo.blogspot.com/2018/08/ipv4-and-ipv6-identification-and.html

https://lirobo.blogspot.com/2020/03/ip-multicast.html

https://lirobo.blogspot.com/2010/10/ipsec.html

星期日, 9月 11, 2022

參數 (Parameter) vs. 引數 (Argument)

參數 (Parameter) 是函數的變數,宣告在函數的方法宣告。

引數 (Argument) 用來呼叫函數。

參考:https://notfalse.net/6/arg-vs-param

星期六, 9月 10, 2022

PWM 馬達調速電路

555 控制 PWM 馬達控制

https://www.circuits-diy.com/dc-motor-speed-control-circuit-using-555-timer-ic/

工作電壓:DC4.5-35V

輸出電流:0-5A

輸出功率:90W max.

靜態電流:7uA

PWM 占空比:1-100%

PWM 頻率:20kHz

https://www.circuits-diy.com/pwm-dc-motor-controller-using-ne555-timer-ic-electronics-project/ 

https://jin-hua.com.tw/webc/html/product/show.aspx?num=29768 (5-35V 或 3-15V)

星期日, 7月 24, 2022

stty

類比 tcgetattr() 和 tcsetattr() 改變終端機屬性的指令。

顯示精簡屬性 (settings that deviate from sane values.):

$ stty
speed 38400 baud; line = 0;
-brkint -imaxbel iutf8

顯示所有屬性 (here carried out on a virtual console):

$ stty -a
speed 38400 baud; rows 25; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
  • 第 1 行:終端機 line speed、window size、和 line discipline (0 代表 N_TTY,new line discipline)。
  • 再來 3 行:終端機特殊字元設定。min 和 time 用在 noncanonical 模式輸入。
  • 剩下依序是 c_cflag, c_iflag, c_oflag, and c_lflag,前置 hyphen (-) 代表關閉,否則啟用。

將中斷字元改為 Ctrl-L:

$ stty intr Ctrl-L
$ stty intr 014
$ stty intr 0xC
$ stty intr <實際字元>

如果要設定的字元會被 shell 或終端機驅動程式特別解釋,需要前置 literal next character (通常是 ^V):

$ stty intr Ctrl-VCtrl-L

啟用或停用 TOSTOP

$ stty tostop
$ stty -tostop

有時程式改變終端機屬性而當掉卡住,可輸入:

Ctrl-J stty sane Ctrl-J

來回復 terminal flags 和特殊字元為 reasonable 狀態。Ctrl-J (ASCII 10) 是換行。在有些模式,終端機驅動程式不再對應 Enter 鍵 (ASCII 13) 到換行字元。第一個 Ctr-J 確保全新的指令行。

操作在指定終端機,不是 standard input,需要有權限:

# stty -a -F /dev/tty3
# stty -a < /dev/tty3

將中斷字元改為 Ctrl-L:

$ stty intr Ctrl-L
$ stty
speed 38400 baud; line = 0;
intr = ^L;

開始 sleep,發現 Ctrl-C 沒作用,只是 echoed:

$ sleep 10
^C

Ctrl-L 結束 sleep,顯示 termination status:

$ echo $?
130

130 表示程式由 signal number 130 – 128 = 2 (SIGINT) 所殺。

然後停用中斷字元:

$ stty intr undef
$ stty
speed 38400 baud; line = 0;
intr = <undef>;

現在 Ctrl-C 和 Ctrl-L 都不會產生 SIGINT signal,必須用 Ctrl-\ 代替來結束程式:

$ sleep 10
^C^L Control-C and Control-L are simply echoed
Type Control-\ to generate SIGQUIT
Quit
$ stty sane Return terminal to a sane state

參考

  1. TLPI §62

星期日, 7月 10, 2022

kernel Link Layer Multicast

struct dev_mc_list *mc_list; // multicast 位址列表,透過 dev_mc_add() 和 dev_mc_delete() 新增和移除。

int mc_count; // multicast 位址數目

int allmult; // 接收所有 multicast 封包

星期日, 7月 03, 2022

Simulator and Emulator

Emulation 和另一個系統有完全相同的行為而能取代它,或許有速度上的差異。Simulation 是嘗試預測某些系統行為而建立的模型。

星期六, 7月 02, 2022

TERMINALS

TLPI §62 的筆記整理

終端機是顯示和輸入的裝置,早期 UNIX 系統是用 RS-232 序列埠連接,顯示器是 24x80 CRT 黑白螢幕。終端機的螢幕控制的字元順序,例如移動游標到行首或頁首,有 VT-100、ANSI 等等標準,所以發展了表格資料庫 termcap 和 terminfo 和函式庫 curses。更早,終端機是紙本電傳打字機 (teletype),所以用 /dev/ttyn 來代表終端機。在現代高效能圖形顯示器,變成 X 視窗系統下的終端機視窗 (terminal emulator,一個 xterm 或類似的 pseudoterminal [TLPI §64]?)。

在 Linux,/dev/ttyn 是 virtual console。virtual console 和 terminal 有什麼不同?

每個終端機有對應的終端機驅動程式來處理輸入和輸出。

當進行輸入,設定 ICANON 決定驅動程式運作在 Canonical 模式 (預設) 或 Noncanonical 模式:

  • Canonical 模式:read() 以「行」為單位,並且 Enter 換行前可以編輯。如果 read() 要求較少位元,需要下個 read() 讀取。
    • 「行」的結束字元有 NL、EOL、EOL2 (如果 IEXTEN)、和 CR (如果 ICRNL),這些結束字元也會傳給讀取程式。另外,在行首以外的 EOF 也會結束一行,但不會傳給讀取程式。A read() may also terminate if interrupted by a signal handler and restarting of system calls is not enabled for this signal (Section 21.5)。中斷會清掉輸入 queue,無論程式是否要 caught 中斷或忽略,啟用 NOFLSH 可避免。
    • 「行」編輯功能有 ERASE、KILL、和 WERASE (如果 IEXTEN) 可修改輸入。
    • 如果 IEXTEN,啟用 REPRINT 和 LNEXT。
  • Noncanonical 模式:read() 指定等多久或幾 byte 結束,分別由 c_cc[] 的 TIME [VTIME] 和 MIN [VMIN] 決定。 不一行一行收集,例如用在程式 vi、more、less 等,讀取每一單一字元,沒有特殊輸入處理,不用等換行。
    • MIN == 0, TIME == 0 (polling read):馬上結束,回讀取的字元數目。 類似設 O_NONBLOCK (Section 5.9),但讀沒資料時是回 -1 加 error EAGAIN。
    • MIN > 0, TIME == 0 (blocking read):等候直到滿足 MIN 或請求數。MIN 設為 1 讓程式等候任何按鍵,CPU 不用忙於 polling 迴圈,可使用 Chapter 63 的techniques。
    • MIN == 0, TIME > 0 (read with timeout):任何讀到或 TIME 到結束。適合用在 serial device (e.g., a modem),送資料後等候回應,使用 timeout 避免等不到回應卡住。
    • MIN > 0, TIME > 0 (read with interbyte timeout):至少會讀到 1 byte,每有 1 個 byte 後重新 TIME 計數,直到逾時或滿足 MIN 或請求數。適合處理產生 escape sequences 的終端機按鍵,例如左箭頭鍵會很快的產生 3-character sequence Escape followed by OD,程式需要識別是按左箭頭、還是分別按了這三鍵,可透過設定小的 TIME,如 0.2 秒。

終端機驅動程式同時解釋一些特殊字元,例如中斷字元 (一般 Ctrl-C)、EOF 字元 (一般 Ctrl-D)。在 noncanonical 模式通常關閉部份或全部這些特殊字元處理。

終端機驅動程式有一個輸入 queue (從終端機輸入到 process 讀取) 和一個輸出 queue (從 process 寫入到終端機顯示)。如果啟用 echoing,任何輸入字元自動 appends 一份到輸出 queue 的尾巴,輸入字元才會顯示在 terminal.

終端機
終端機驅動程式
程式
輸入 queue


輸出 queue



Linux kernel 限制輸入 queue 4096 bytes。輸出 queue 也是 4096 bytes,但應用程式不用考量這個限制,如果輸出超過驅動程式可以處理的速度,kernel suspends 寫入 process 的執行,直到輸出 queue 有空間。

在 Linux,呼叫 ioctl(fd, FIONREAD, &cnt) 取得輸入 queue 未讀的 byte 數。

取得和設定終端機屬性

#include <termios.h>
int tcgetattr(int fd, struct termios *termios_p);
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
  • 成功回 0,失敗回 –1。通常先 tcgetattr() 取得屬性後作必要的修改後 tcsetattr() 設定。tcsetattr() 只要有部份設定成功就回 0,當有多項改變時,可能需要再 tcgetattr() 來比較。在 Linux 和許多 UNIX,是使用 ioctl() 的程式庫函式。
  • fd 是參照到終端機的 file descriptor,不是的話回 error ENOTTY。 
  • struct termios 紀錄終端機屬性

struct termios 紀錄終端機屬性:

struct termios {
  tcflag_t c_iflag; // Terminal Input flags
  tcflag_t c_oflag; // Terminal Output flags
  tcflag_t c_cflag; // Hardware Control flags of the terminal line
  tcflag_t c_lflag; // Local modes,關於輸入 line edit 部份,例如 ECHO
  cc_t c_line;      // Line discipline (nonstandard)
  cc_t c_cc[NCCS];  // Terminal special characters,見 §62.4
  speed_t c_ispeed; // Input speed,Linux 沒用,Linux 方式見 §62.7
  speed_t c_ospeed; // Output speed,Linux 沒用,Linux 方式見 §62.7
};

前 4 個 flag 欄位是 bit masks 控制各種終端機驅動程式的操作。Default column shows the default settings for a virtual console login。

c_iflag

  • BRKINT:預設 on。IGNBRK 未設時,按 BREAK 時送 Signal interrupt (SIGINT)。在 virtual console,BREAK 是按 Ctrl-Break。大部分傳統 dumb 終端機有 BREAK 鍵,按此鍵實際上不產生字元,但一系列 0 bits is sent to the terminal driver for a specified length of time, typically 0.25 or 0.5 seconds (i.e., longer than the time required to transmit a single byte). 許多 UNIX 系統,BREAK 是要告訴遠端主機換 line speed (baud),直到登入提示出現。
  • ICRNL:預設 on。 Map CR to NL on input on
  • IGNBRK:預設 off。 Ignore BREAK condition off
  • IGNCR:預設 off。 Ignore CR on input off
  • IGNPAR:忽略 parity 錯誤字元,預設不忽略。
  • IMAXBEL:Ring bell 當輸入 queue 滿了,預設啟用,Linux 忽略。
  • INLCR:預設 off。 Map NL to CR on input off
  • INPCK:輸入 parity 檢查,預設停用。
  • ISTRIP:Strip high bit (bit 8) from input characters,預設不。
  • IUTF8:預設停用。 Input is UTF-8 (since Linux 2.6.4) Setting the IUTF8 flag enables cooked mode (Section 62.6.3) to correctly handle UTF-8 input when performing line editing.
  • IUCLC:預設 off。 Map uppercase to lowercase on input (if IEXTEN also set) off
  • IXANY:預設 off。 Allow any character to restart stopped output off
  • IXOFF:預設 off。 Enable start/stop input flow control off
  • IXON:預設 on。Enable start/stop output flow control on
  • PARMRK:送 parity 錯誤字元前置 0377 + 0 (ISTRIP 沒設時,0377 + 0377 + 0),預設 off。

c_oflag

  • BSDLY:預設 BS0。 Backspace delay mask (BS0, BS1) BS0
  • CRDLY:預設 CR0。 CR delay mask (CR0, CR1, CR2, CR3) CR0
  • FFDLY:預設 FF0。 Form-feed delay mask (FF0, FF1) FF0
  • NLDLY:預設 NL0。 Newline delay mask (NL0, NL1) NL0
  • OCRNL:預設 off。 Map CR to NL on output (see also ONOCR) off
  • OFDEL:預設 off。 Use DEL (0177) as fill character; otherwise NUL (0) off
  • OFILL:預設 off。 Use fill characters for delay (rather than timed delay) off
  • OLCUC:預設 off。 Map lowercase to uppercase on output off
  • ONLCR:預設 on。 Map NL to CR-NL on output on
  • ONLRET:預設 off。 Assume NL performs CR function (move to start of line) off
  • ONOCR:預設 off。 Don’t output CR if already at column 0 (start of line) off
  • OPOST:輸出 postprocessing,預設啟用,讓 c_oflag 其它 flag 有效。
  • TABDLY:預設 TAB0。 Horizontal-tab delay mask (TAB0, TAB1, TAB2, TAB3) TAB0
  • VTDLY:預設 VT0。 Vertical-tab delay mask (VT0, VT1) VT0

c_cflag

  • CBAUD:預設 B38400。 Baud (bit rate) mask (B0, B2400, B9600, and so on) B38400
  • CBAUDEX:預設 off。 Extended baud (bit rate) mask (for rates > 38,400) off
  • CIBAUD:預設 (off)。 Input baud (bit rate), if different from output (unused) (off)
  • CLOCAL:預設 off。 Ignore modem status lines (don’t check carrier signal) off
  • CMSPAR:預設 off。 Use “stick” (mark/space) parity off
  • CREAD:預設 on。 Allow input to be received on
  • CRTSCTS:預設 off。 Enable RTS/CTS (hardware) flow control off
  • CSIZE:預設 CS8。 Character-size mask (5 to 8 bits: CS5, CS6, CS7, CS8) CS8
  • CSTOPB:預設 off。 Use 2 stop bits per character; otherwise 1 off
  • HUPCL:預設 on。 Hang up (drop modem connection) on last close on
  • PARENB:啟用 parity,預設停用。
  • PARODD:使用 odd parity,否則 even parity,預設 even。

c_lflag

  • ECHO:顯示輸入字元,預設啟用,不論 canonical 模式與否都有效用。在讀取密碼、或 vi command mode 會停用。
  • ECHOCTL:顯示 tab、換行、START 和 STOP 以外的控制字元 (32 以下和 127),預設 on,ECHO 時有效。
  • ECHOE:ERASE 原本字元改為顯示 backspace-space-backspace,預設啟用。canonical 模式時有效。
  • ECHOK:KILL 原本字元改為顯示一行刪除,預設啟用。canonical 模式且 ECHOKE 時有效。
  • ECHOKE:KILL 後不顯示換行,預設啟用。否則在 ECHOK 時會顯示換行。
  • ECHONL:預設 off。 Echo NL (in canonical mode) even if echoing is disabled off
  • ECHOPRT:預設 off。 Echo deleted characters backward (between \ and /) off
  • FLUSHO:預設 -。 Output is being flushed (unused) -
  • ICANON:canonical 模式 (一行一行處理) 輸入,預設啟用。Input is gathered into lines, and special interpretation of the EOF, EOL, EOL2, ERASE, LNEXT, KILL, REPRINT, and WERASE characters is enabled (but note the effect of the IEXTEN flag described below)
  • IEXTEN:啟用 extended processing of 輸入字元,預設啟用。This flag (as well as ICANON) must be set in order for the following characters to be interpreted: EOL2, LNEXT, REPRINT, and WERASE. The IEXTEN flag must also be set for the IUCLC flag to be effective.
  • ISIG:預設 on。 Enable signal-generating characters (INTR, QUIT, SUSP) on
  • NOFLSH:INTR、QUIT、和 SUSP 時不忽略輸入輸出 queue 內的資料,預設停用會忽略。
  • PENDIN:預設 (off)。 Redisplay pending input at next read (not implemented) (off)
  • TOSTOP:預設 off。 Generate SIGTTOU for background output (Section 34.7.1) off
  • XCASE:預設 (off)。 Canonical upper/lowercase presentation (unimplemented) (off)

有些 flags 是為了功能有限的古老終端機,沒什麼用處。例如 IUCLC、OLCUC、和 XCASE 只用在只能顯示大寫字母的終端機。ECHOPRT flag was also designed for limited-capability terminals.

The various delay masks are also historical, allowing for terminals and printers that took longer to echo characters such as carriage return and form feed. The related OFILL and OFDEL flags specified how such a delay was to be performed. 大部分在 Linux 沒用。One exception is the TAB3 setting for the TABDLY mask, which causes tab characters to be output as (up to eight) spaces.

c_line:終端機的 line discipline。For the purposes of programming terminal emulators, the line discipline will always be set to N_TTY, the so-called new discipline, a component of the kernel terminal-handling code that implements canonical mode I/O processing. Setting the line discipline can be relevant when programming serial lines.

c_cc[] 是終端機特殊字元 (interrupt, suspend, and so on) as well as fields controlling the operation of noncanonical mode input. We describe the terminal special characters in Section 62.4.

tcsetattr() 設定終端機屬性,optional_actions 決定改變何時生效:

  • TCSANOW:立即生效。
  • TCSADRAIN:目前輸出 queue 都送出後生效。通常改變輸出需要,避免影響已 queue 但未顯示的輸出。
  • TCSAFLUSH:如同 TCSADRAIN 外,生效時再 discard pending 輸入。例如用在讀取密碼時,要關閉 echoing 並避免使用者事先輸入。

TLPI §34.7.2,如果在背景呼叫 tcsetattr(),終端機驅動程式 suspends the process group by delivering a SIGTTOU signal。如果在 orphaned process group 呼叫,fails with the error EIO。對 tcflush()、tcflow()、tcsendbreak()、和 tcdrain() 也是一樣。

Terminal Special Characters 和 c_cc[]

特殊字元c_cc[]預設相關 termios flags說明
CR(不能變)^MICANON
IGNCR:忽略輸入
ICRNL:輸入轉成 NL
OPOST
OCRNL
ONOCR
Carriage return。輸出將終端機游標移到行首。
DISCARDVDISCARD^O
toggle 忽略輸出,在 Linux 沒作用。
EOFVEOF^DICANONEnd-of-file
EOLVEOL停用ICANONEnd-of-line
EOL2VEOL2停用ICANON 且 IEXTEN另一個 end-of-line
ERASEVERASE^?ICANON消除前一字元
INTRVINTR^CISIG送 SIGINT
KILLVKILL^UICANON消除行
LNEXTVLNEXT^VICANON 且 IEXTENLiteral next,下個字元視為一般字元。
NL(不能變)^JICANON, INLCR, ECHONL,
OPOST, ONLCR, ONLRET
Newline,輸出將游標往下移一行。
QUITVQUIT^\ISIG送 SIGQUIT signal
REPRINTVREPRINT^RICANON, IEXTEN, ECHO重印輸入行 (輸入可能混輸出)
STARTVSTART^QIXON, IXOFFStart output
STOPVSTOP^SIXON, IXOFFStop output
SUSPVSUSP^ZISIG送 SIGTSTP signal
WERASEVWERASE^WICANON 且 IEXTENErase word
-VTIME?非 ICANONTIME,單位十分之一秒。
-VMIN?非 ICANONMIN

停用終端機特殊字元透過設為 fpathconf(fd, _PC_VDISABLE) 回傳的值,大部分 UNIX 實作是 0。

終端機特殊字元,除了換行的 CR、EOL、EOL2、和 NL 外,不會傳給讀取的程式。

  • EOL 和 EOL2:額外的行結束字元。用在 telnet,設定為跳脫字元 (通常是 Ctrl-],或在 rlogin 模式時 ~),讓 telnet 在讀取時能夠立即處理。
  • NL:換行。In canonical mode, this character terminates an input line. If the OPOST and ONLCR (map NL to CR-NL) flags are set (the default), then, on output, a newline character is mapped to the 2-character sequence CR plus NL. (The combination of the ICRNL and ONLCR flags means that an input CR is converted to a NL, and then echoed as CR plus NL.)
  • START and STOP:the start output and stop output characters, which operate if the IXON (enable start/stop output control) flag is enabled (the default). (The START and STOP characters are not honored by some terminal emulators.) Typing the STOP character suspends terminal output. If the IXOFF flag is set and the terminal input queue is full, then the terminal driver automatically sends a STOP character to throttle the input. Typing the START character causes terminal output to resume after previously being stopped by the STOP character. If the IXOFF (enable start/stop input control) flag is set (this flag is disabled by default) and the terminal driver had previously sent a STOP character because the input queue was full, the terminal driver automatically generates a START character when space once more becomes available in the input queue. If the IXANY flag is set, then any character, not just START, may be typed in order to restart output (and that character is similarly not passed to the reading process). The START and STOP characters are used for software flow control in either direction between the computer and the terminal device. One function of these characters is to allow users to stop and start terminal output. This is output flow control, as enabled by IXON. However, flow control in the other direction (i.e., control of input flow from the device to the computer, as enabled by IXOFF) is also important when, for example, the device in question is a modem or another computer. Input flow control makes sure that no data is lost if the application is slow to handle input and the kernel buffers fill up. With the higher line speeds that are nowadays typical, software flow control has been superseded by hardware (RTS/CTS) flow control, whereby data flow is enabled and disabled using signals sent via separate wires on the serial port. (RTS stands for Request To Send, and CTS stands for Clear To Send.)

the magic SysRq key; see the kernel source file Documentation/sysrq.txt.

62.5 Terminal Flags

struct termios 的 4 個 flag 欄位。

許多 shell 有提供指令行編輯功能進行 flag manipulations,用 stty 改設定未必有效。為了 circumvent this behavior,必須關閉 shell 指令行編輯功能,例如 invoke bash 加 ––noediting 選項。

62.6 Terminal I/O Modes

terminal modes—cooked, cbreak, and raw are emulated on modern UNIX systems by setting appropriate values in the termios structure.

62.6.3 Cooked, Cbreak, and Raw Modes

Cooked mode was essentially canonical mode with all of the default special charac- ter processing enabled (i.e., interpretation of CR, NL, and EOF; enabling of line edit- ing; handling of signal-generating characters; ICRNL; OCRNL; and so on).

Raw mode was the converse: noncanonical mode, with all input and output processing, as well as echoing, switched off. (An application that needed to ensure that the terminal driver makes absolutely no changes to the data transferred across a serial line would use this mode.)

Cbreak mode was intermediate between cooked and raw modes. Input was noncanonical, but signal-generating characters were interpreted, and the various input and output transformations could still occur (depending on individual flag settings). Cbreak mode did not disable echoing, but applications employing this mode would usually disable echoing as well. Cbreak mode was useful in screen- handling applications (such as less) that permitted character-by-character input, but still needed to allow interpretation of characters such as INTR, QUIT, and SUSP.

62.7 Terminal Line Speed (Bit Rate)

62.8 Terminal Line Control

62.9 Terminal Window Size

62.10 Terminal Identification

62.11 Summary

62.12 Exercises

參考

  1. TLPI §62
  • https://en.wikipedia.org/wiki/Computer_terminal
  • Terminal Recorders:紀錄和播放指令動作。

星期日, 6月 26, 2022

User space Atomic Operations

Atomic Operation (原子運算?) 的指令執行不會因其它指令插入而導致錯誤結果。

Linux Kernel 的 atomic.h 適用在 user space 嗎?

GCC Atomic builtins GCC v4.1 以上。

簡單型別,可以使用 atomic_xxx (如:atomic_int)

C++11、C11 將低階的硬體指令變成標準函數,如 atomic_fetch_add

Comparing the performance of atomic, spinlock and mutex》比較執行一千萬次的時間:直接存取 0.070,Atomic 0.457,LOCK 0.481,Spinlock 0.541,Mutex 22.667。

參考:

  1. https://the-linux-channel.the-toffee-project.org/index.php?page=6-tutorials-linux-user-space-atomic-operations&lang=en
  2. https://wirelessr.gitbooks.io/working-life/content/atomic_variable_in_user_space.html

Concurrency 和 Parallelism。Concurrency (並行) 是多個獨立的工作同時進行。Parallelism (平行) 將並行的工作分配給不同硬體單元進行,真的同時做。Concurrency 可將單一硬體分時共享達成,也可以用 Parallelism 實現。

星期六, 6月 25, 2022

Windows Command Line

序列埠

  • mode
  • [System.IO.Ports.SerialPort]::getportnames()
  • $port= new-Object System.IO.Ports.SerialPort COM#,Baudrate,None,8,one
  • cygwin minicom
  • plink -serial \\.\COM9 -sercfg 115200,8,1,N,N
  • !plink -load COM9
  • COM9 以上的特殊指定方式

參考

https://docs.microsoft.com/zh-tw/windows-server/administration/windows-commands/windows-commands

https://www.puttygen.com/windows-terminal-emulators

星期日, 6月 19, 2022

[Linux] expect

expect 指令依據腳本和其它程式互動,預期 (expect) 對方的輸出而給對應的回應。

expect 是擴充 tcl 的直譯式語言,提供 branching 和高階控制架構來引導對話。需要時,使用者可以取得控制直接互動,後來再將控制權交還給 script。expect 實際上可以同時連線多個程式。

構想來自普遍在 uucp、kermit 和其它數據機控制程式的 send/expect sequences.

指令格式

expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]

讀 cmdfile 取得要執行指令,或在可執行腳本首行放

#!/usr/bin/expect -f

來隱含 invoke。

  • cmdfile:
  • -f cmdfile:prefaces a file from which to read commands from. 只用在可執行腳本首行的 #!,so that other arguments may be supplied on the command line.
  • -b cmdfile:batch 模式,一行一行讀取 cmdfile,例如 stdin 就是用這種方式讀取。預設 cmdfile 是一次讀到記憶體來執行。Note that stdio-buffering may still take place however this shouldn't cause problems when reading from a fifo or stdin.
  • -b - 或 -:cmdfile 檔名用「-」使用 stdin。
  • -c cmds:預先執行的指令。The command should be quoted to prevent being broken up by the shell. 可有多個 -c 按照出現順序執行,或者多個指令用分號隔開使用單一 -c。
  • -d:啟用 some diagnostic 輸出,主要回報內部 activity of commands such as expect and interact。相當於「exp_internal 1」at  the  beginning  of  an  Expect script,  plus the version of Expect is printed.  (The strace command is useful for tracing statements, and the  trace  command  is  useful  for tracing  variable  assignments.)
  • -D:啟用 an interactive debugger.  An integer  value  should follow.   The  debugger will take control before the next Tcl procedure if the value is non-zero or if a ^C is pressed (or a breakpoint is hit, or  other appropriate debugger command appears in the script).  See the README file or SEE ALSO (below) for more information on  the  debugger.
  • -i:causes Expect to interactively prompt for commands instead of reading them from a file.  Prompting is terminated via the exit command or upon EOF.  See interpreter (below) for more information.  -i is assumed if neither a command file nor -c is used.
  • --:表示給 expect 的選項結束,之後的引數直接 pass 給腳本,包括「-」開頭的引數。適合用在腳本的 #! 行。For  example,  the following will leave the original arguments (including the script name) in the variable argv.

    #!/usr/bin/expect --

    Note that the usual getopt(3) and execve(2)  conventions  must  be  observed when adding arguments to the #! line.

  • -N:The  file  $exp_library/expect.rc  is sourced automatically if present, unless the -N flag is used.  (When using Expectk, this option is specified  as  -NORC.)   Immediately  after  this,  the file ~/.expect.rc is sourced automatically, unless the -n flag is used.  If the  environment variable DOTDIR is defined, it is treated as a directory and .expect.rc is read from there.  This sourcing occurs only after executing any -c flags.
  • -v:印版號後離開。

Optional args are constructed into a list and stored  in  the  variable
       named argv.  argc is initialized to the length of argv.

argv0  is  defined to be the name of the script (or binary if no script
       is used).  For example, the following prints out the name of the script
       and the first three arguments:

           send_user "$argv0 [lrange $argv 0 2]\n"

指令

expect 使用 Tcl (Tool Command Language) 提供流程控制 (例如 if, for, break)、expression evaluation、和幾個其它 features such as recursion, procedure definition 等。這裡沒定義的指令是 Tcl 指令 (如 set, if, exec),見 tcl(3)。expect 額外支援的指令如下,除非特別 specified,指令回 empty string。

指令按照字母排序,但新使用者可能要先看 spawn、send、expect、和 interact 較容易。

接下來,用「Expect」表示程式 (Linux 下的指令),「expect」表示 Expect 程式裡的 expect 指令。

close [-slave] [-onexec 0|1] [-i spawn_id]

停止連線到程式,大部分程式偵測到 stdin EOF 後也會自動結束。expectinteract 都會偵測連線的程式結束而隱含 close,但如果用「exec kill $pid」來停止 process,需要明確 close。

-i spawn_id:停止連線到指定的 spawn_id。

-onexec:determines whether the spawn id will  be  closed
             in  any new spawned processes or if the process is overlayed.  To
             leave a spawn id open, use the value 0.  A non-zero integer value
             will force the spawn closed (the default) in any new processes.

-slave:closes the slave associated with the spawn id.
             (See "spawn -pty".)  When the connection is closed, the slave  is
             automatically closed as well if still open.

No  matter whether the connection is closed implicitly or explicitly, you should call wait to clear up the  corresponding  kernel process slot.  close does not call wait since there is no guarantee that closing a process connection will cause it to exit.

debug [[-now] 0|1]

控制 Tcl debugger 來 step through statements、設 breakpoints 等。

no arguments:看 debugger 是否執行中,回 0 表示執行中,1 不在執行中。

1:啟動 debugger。(下個 Tcl statement 開始)

-now 1:馬上啟動 debugger。(在本身 debug 指令中間就啟動) 

0:停止 debugger。

The  debug  command  does  not change any traps. 比較 Expect 的 -D 參數。

disconnect

中斷和親 process 的連繫,繼續在背景執行,會有自己的 process group (可能的話),標準 I/O 導到 /dev/null。

下列 script 預先讀取 priv_prog 需要的 password,然後每小時背景執行 priv_prog。(見 stty 如何關閉 password echoing。)

send_user "password?\ "
expect_user -re "(.*)\n"
for {} 1 {} {
    if {[fork]!=0} {sleep 3600;continue}
    disconnect
    spawn priv_prog
    expect Password:
    send "$expect_out(1,string)\r"
    . . .
    exit
}

使用 disconnect 的好處是在和親 process 的斷線前可以儲存參數,然後用在新建立的 ptys。如果用 shell 的 asynchronous process  feature (&),Expect 沒有機會讀取 the terminal's parameters since the terminal is  already  disconnected  by the time Expect receives control.

exit [-opts] [status]

causes Expect to exit or otherwise prepare to do so.

-onexit:causes the next argument to be used as an exit handler.  Without an argument, the current exit  handler  is  returned.

-noexit:causes Expect to prepare to exit but stop short of actually returning control to the operating system.  The user-defined exit handler is run as well as Expect's own internal handlers.  No further Expect commands should be executed.   This  is useful  if you are running Expect with other Tcl extensions.  The current interpreter (and main window if in  the  Tk  environment) remain  so  that  other Tcl extensions can clean up.  If Expect's exit is called again (however this might occur), the handlers are not rerun.

Upon  exiting,  all  connections to spawned processes are closed. Closure will be detected as an EOF by  spawned  processes.   exit takes  no other actions beyond what the normal _exit(2) procedure does.  Thus, spawned processes that do not check for EOF may continue  to  run.  (A variety of conditions are important to determining, for example, what signals a spawned process will be sent, but   these  are  system-dependent,  typically  documented  under exit(3).)  Spawned processes that continue to run will be  inherited by init.

status  (or 0 if not specified) is returned as the exit status of Expect.  exit is implicitly executed if the end of the script  is reached.

exp_continue [-continue_timer]

The command exp_continue allows expect itself to continue execut‐
             ing rather than  returning  as  it  normally  would.  By  default
             exp_continue  resets  the timeout timer. The -continue_timer flag
             prevents timer from being restarted. (See expect for more  infor‐
             mation.)

exp_internal [-f file] value
             causes  further  commands to send diagnostic information internal
             to Expect to stderr if value is non-zero.  This  output  is  dis‐
             abled  if  value is 0.  The diagnostic information includes every
             character received, and every attempt made to match  the  current
             output against the patterns.

             If the optional file is supplied, all normal and debugging output
             is written to that file (regardless of the value of value).   Any
             previous diagnostic output file is closed.

             The -info flag causes exp_internal to return a description of the
             most recent non-info arguments given.

exp_open [args] [-i spawn_id]
             returns a Tcl file identifier that corresponds  to  the  original
             spawn  id.   The  file  identifier can then be used as if it were
             opened by Tcl's open command.  (The spawn id should no longer  be
             used.  A wait should not be executed.

             The  -leaveopen  flag leaves the spawn id open for access through
             Expect commands.  A wait must be executed on the spawn id.

exp_pid [-i spawn_id]
             returns the process id corresponding  to  the  currently  spawned
             process.  If the -i flag is used, the pid returned corresponds to
             that of the given spawn id.

exp_send
             is an alias for send.

exp_send_error
             is an alias for send_error.

exp_send_log
             is an alias for send_log.

exp_send_tty
             is an alias for send_tty.

exp_send_user
             is an alias for send_user.

exp_version [[-exit] version]
             is useful for assuring that the script  is  compatible  with  the
             current version of Expect.

             With  no  arguments,  the  current version of Expect is returned.
             This version may then be encoded in your script.  If you actually
             know  that you are not using features of recent versions, you can
             specify an earlier version.

             Versions consist of three numbers separated by  dots.   First  is
             the  major number.  Scripts written for versions of Expect with a
             different major number will almost certainly not work.   exp_ver‐
             sion returns an error if the major numbers do not match.

             Second is the minor number.  Scripts written for a version with a
             greater minor number than the current  version  may  depend  upon
             some new feature and might not run.  exp_version returns an error
             if the major numbers  match,  but  the  script  minor  number  is
             greater than that of the running Expect.

             Third  is  a number that plays no part in the version comparison.
             However, it is incremented when the Expect software  distribution
             is changed in any way, such as by additional documentation or op‐
             timization.  It is reset to 0 upon each new minor version.

             With the -exit flag, Expect prints an error and exits if the ver‐
             sion is out of date.

expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]

等候 spawn 的 process 輸出直到符合其中一個 pattern、EOF、或 timeout。If the final body is empty, it may be omitted.

Patterns  from  the most recent expect_before command are implicitly used before any other patterns.  Patterns from the most  recent  expect_after  command  are  implicitly used after any other patterns.

If the arguments to the entire expect statement 需要跨多行,可以 "braced" into one 來避免每行以 backslash 結束。the usual Tcl substitutions will occur despite the braces。

如果 pattern 是 keyword eof,the corresponding body is executed upon end-of-file.  If a pattern is the keyword timeout, the corresponding  body is executed upon timeout.  If no timeout keyword is used, an implicit null action is executed  upon  timeout。預設 timeout 是 10 秒,但 may be set, for example to 30, by the command "set timeout 30".  An infinite  timeout  may be designated by the value -1.  If a pattern is the keyword default, the corresponding  body  is  executed  upon  either timeout or end-of-file.

If  a  pattern  matches, then the corresponding body is executed. expect returns the result of the body (or the empty string if  no pattern matched).

Each time new output arrives, it is compared to each  pattern  in
             the  order  they are listed.  Thus, you may test for absence of a
             match by making the last pattern something guaranteed to  appear,
             such  as  a  prompt.  In situations where there is no prompt, you
             must use timeout (just like you would  if  you  were  interacting
             manually).

Patterns  are  specified in three ways. 預設用 Tcl 的 string match 指令,(Such patterns are
             also  similar  to C-shell regular expressions usually referred to
             as "glob" patterns).  The -gl flag may may  be  used  to  protect
             patterns  that  might otherwise match expect flags from doing so.
             Any pattern beginning with a "-" should be  protected  this  way.
             (All strings starting with "-" are reserved for future options.)

For example, the following fragment looks for a successful login.
             (Note that abort is presumed to be a procedure defined  elsewhere
             in the script.)

                 expect {
                     busy               {puts busy\n ; exp_continue}
                     failed             abort
                     "invalid password" abort
                     timeout            abort
                     connected
                 }

Quotes  are  necessary  on the fourth pattern since it contains a space, which would otherwise separate the pattern  from  the  action. More information on forming glob-style patterns can be found in the Tcl manual.

Regexp-style patterns follow the syntax defined by  Tcl's  regexp command.  regexp patterns are introduced with the  flag  -re.   The  previous  example  can  be rewritten using a regexp as:

                 expect {
                     busy       {puts busy\n ; exp_continue}
                     -re "failed|invalid password" abort
                     timeout    abort
                     connected
                 }

Both  types  of  patterns are "unanchored".  This means that patterns do not have to match the entire string, but can  begin  and
             end  the match anywhere in the string (as long as everything else
             matches).  Use ^ to match the beginning of a  string,  and  $  to
             match  the  end.   Note  that if you do not wait for the end of a
             string, your responses can easily end up in  the  middle  of  the
             string  as they are echoed from the spawned process.  While still
             producing correct results, the output can look unnatural.   Thus,
             use of $ is encouraged if you can exactly describe the characters
             at the end of a string.

Note that in many editors, the ^ and $ match  the  beginning  and end  of  lines  respectively. However, because expect is not line oriented, these characters match the beginning  and  end  of  the data  (as opposed to lines) currently in the expect matching buffer.  (Also, see the note below on "system indigestion.")

-ex:the pattern  to  be  matched  as  an  "exact" string.   No  interpretation  of  *, ^, etc is made (although the usual Tcl conventions must still be  observed).   Exact  patterns are always unanchored.

-nocase:大寫輸出視為小寫。不影響 pattern。

While  reading  output,  more  than  2000 bytes can force earlier
             bytes to be "forgotten".  This may be changed with  the  function
             match_max.  (Note that excessively large values can slow down the
             pattern matcher.)  If patlist is full_buffer,  the  corresponding
             body  is  executed  if  match_max bytes have been received and no
             other patterns have matched.  Whether or not the full_buffer key‐
             word  is  used,  the  forgotten  characters  are  written  to ex‐
             pect_out(buffer).

If patlist is the keyword null, and nulls are  allowed  (via  the
             remove_nulls  command),  the  corresponding body is executed if a
             single ASCII 0 is matched.  It is not possible to match  0  bytes
             via glob or regexp patterns.

Upon matching a pattern (or eof or full_buffer), any matching and
             previously  unmatched  output  is  saved  in  the  variable   ex‐
             pect_out(buffer).   Up to 9 regexp substring matches are saved in
             the variables expect_out(1,string) through  expect_out(9,string).
             If  the  -indices flag is used before a pattern, the starting and
             ending indices (in a form suitable for lrange) of the 10  strings
             are   stored   in   the  variables  expect_out(X,start)  and  ex‐
             pect_out(X,end) where X is a digit, corresponds to the  substring
             position  in  the  buffer.  0 refers to strings which matched the
             entire pattern and is generated for glob patterns as well as reg‐
             exp  patterns.   For example, if a process has produced output of
             "abcdefgh\n", the result of:

                 expect "cd"

is as if the following statements had executed:

                 set expect_out(0,string) cd
                 set expect_out(buffer) abcd

and "efgh\n" is left in the output buffer.  If a process produced the output "abbbcabkkkka\n", the result of:

                 expect -indices -re "b(b*).*(k+)"

is as if the following statements had executed:

                 set expect_out(0,start) 1
                 set expect_out(0,end) 10
                 set expect_out(0,string) bbbcabkkkk
                 set expect_out(1,start) 2
                 set expect_out(1,end) 3
                 set expect_out(1,string) bb
                 set expect_out(2,start) 10
                 set expect_out(2,end) 10
                 set expect_out(2,string) k
                 set expect_out(buffer) abbbcabkkkk

and "a\n" is left in the output buffer.  The pattern "*" (and -re ".*") will flush the output buffer without reading any more  output from the process.

Normally,  the matched output is discarded from Expect's internal
             buffers.  This may be prevented by prefixing a pattern  with  the
             -notransfer  flag.  This flag is especially useful in experiment‐
             ing (and can be abbreviated to "-not" for convenience  while  ex‐
             perimenting).

The  spawn  id  associated  with  the  matching output (or eof or
             full_buffer) is stored in expect_out(spawn_id).

-timeout:the current expect command  to  use  the following  value  as  a timeout instead of using the value of the timeout variable.

By default, patterns are matched against output from the  current
             process,  however  the -i flag declares the output from the named
             spawn_id list be matched against any following  patterns  (up  to
             the  next  -i).   The spawn_id list should either be a whitespace
             separated list of spawn_ids or a variable  referring  to  such  a
             list of spawn_ids.

For example, the following example waits for "connected" from the current process, or "busy", "failed" or "invalid  password"  from the spawn_id named by $proc2.

                 expect {
                     -i $proc2 busy {puts busy\n ; exp_continue}
                     -re "failed|invalid password" abort
                     timeout abort
                     connected
                 }

The  value  of  the  global  variable any_spawn_id may be used to match patterns to any spawn_ids that are named with all other  -i flags in the current expect command.  The spawn_id from a -i flag with no associated pattern (i.e., followed immediately by another -i)  is  made  available to any other patterns in the same expect command associated with any_spawn_id.

The -i flag may also name a global variable  in  which  case  the
             variable is read for a list of spawn ids.  The variable is reread
             whenever it changes.  This provides a way  of  changing  the  I/O
             source  while  the  command  is in execution.  Spawn ids provided
             this way are called "indirect" spawn ids.

Actions such as  break  and  continue  cause  control  structures
             (i.e.,  for,  proc)  to  behave  in  the  usual way.  The command
             exp_continue allows expect itself to  continue  executing  rather
             than returning as it normally would.

This  is  useful  for  avoiding explicit loops or repeated expect
             statements.  The following example is part of a fragment to auto‐
             mate  rlogin.   The  exp_continue avoids having to write a second
             expect statement (to look for the prompt  again)  if  the  rlogin
             prompts for a password.

                 expect {
                     Password: {
                         stty -echo
                         send_user "password (for $user) on $host: "
                         expect_user -re "(.*)\n"
                         send_user "\n"
                         send "$expect_out(1,string)\r"
                         stty echo
                         exp_continue
                     } incorrect {
                         send_user "invalid password or account\n"
                         exit
                     } timeout {
                         send_user "connection to $host timed out\n"
                         exit
                     } eof {
                         send_user \
                             "connection to host failed: $expect_out(buffer)"
                         exit
                     } -re $prompt
                 }

For  example,  the  following fragment might help a user guide an
             interaction that is already totally automated.  In this case, the
             terminal  is put into raw mode.  If the user presses "+", a vari‐
             able is incremented.  If "p" is pressed, several returns are sent
             to  the process, perhaps to poke it in some way, and "i" lets the
             user interact with the process, effectively stealing away control
             from  the script.  In each case, the exp_continue allows the cur‐
             rent expect to continue pattern matching after executing the cur‐
             rent action.

                 stty raw -echo
                 expect_after {
                     -i $user_spawn_id
                     "p" {send "\r\r\r"; exp_continue}
                     "+" {incr foo; exp_continue}
                     "i" {interact; exp_continue}
                     "quit" exit
                 }

By  default, exp_continue resets the timeout timer.  The timer is
             not restarted, if exp_continue is called with the -continue_timer
             flag.

expect_after [expect_args]
works  identically  to  the expect_before except that if patterns
             from both expect and expect_after can match, the  expect  pattern
             is used.  See the expect_before command for more information.

expect_background [expect_args]

takes  the  same  arguments as expect, however it returns immedi‐
             ately.  Patterns are tested whenever new input arrives.  The pat‐
             tern timeout and default are meaningless to expect_background and
             are silently discarded.  Otherwise, the expect_background command
             uses  expect_before  and  expect_after  patterns just like expect
             does.

             When expect_background actions are  being  evaluated,  background
             processing for the same spawn id is blocked.  Background process‐
             ing is unblocked when the  action  completes.   While  background
             processing is blocked, it is possible to do a (foreground) expect
             on the same spawn id.

             It is not possible to execute an  expect  while  an  expect_back‐
             ground is unblocked.  expect_background for a particular spawn id
             is deleted by declaring a new  expect_background  with  the  same
             spawn  id.   Declaring  expect_background with no pattern removes
             the given spawn id from the ability  to  match  patterns  in  the
             background.

expect_before [expect_args]

takes  the  same  arguments as expect, however it returns immediately.  Pattern-action pairs from the most  recent  expect_before
             with  the same spawn id are implicitly added to any following ex‐
             pect commands.  If a pattern matches, it is treated as if it  had
             been  specified  in the expect command itself, and the associated
             body is executed in the context of the expect command.   If  pat‐
             terns  from  both  expect_before  and  expect  can match, the ex‐
             pect_before pattern is used.

             If no pattern is specified, the spawn id is not checked  for  any
             patterns.

             Unless  overridden  by  a  -i  flag, expect_before patterns match
             against the spawn id defined at the time that  the  expect_before
             command was executed (not when its pattern is matched).

             The  -info flag causes expect_before to return the current speci‐
             fications of what patterns it will match.  By default, it reports
             on  the current spawn id.  An optional spawn id specification may
             be given for information on that spawn id.  For example

                 expect_before -info -i $proc

             At most one spawn id specification may be given.  The flag -indi‐
             rect  suppresses  direct  spawn  ids that come only from indirect
             specifications.

             Instead of a spawn id specification, the flag "-all"  will  cause
             "-info" to report on all spawn ids.

             The output of the -info flag can be reused as the argument to ex‐
             pect_before.

expect_tty [expect_args]

類似 expect,但讀 /dev/tty。

expect_user [expect_args]

類似 expect,但讀 stdin。

fork

分叉出子 process,成功時在子 process 回 0,在親 process 回子 process ID。失敗時,回 -1,不會建立子 process。

子 processes exit via the exit command, just like the original process. 子 processe 仍會寫 log 檔,親子都紀錄可能會混淆。

有些 pty 實作在多個 reader 或 writer 時會困惑,即使只是短暫的,因此 fork 後 spawning processes 較安全。

[待續]

參考

  1. man expect
  • tcl 是一個高階的電腦語言,語法類似 shell script 與 C 語言之間。而 tk 是一個 GUI 發展工具,有 buttons、menus、listboxes、 scrollbars 等等。
  • 書《Exploring Expect》介紹 expect 和 tcl。
  • Expectk 是 Expect 和 Tk 的混合,使用長參數名稱。libexpect 可以直接和 C 或 C++ 使用,而不用 Tcl。
  • 待讀:Tcl/Tk 學習筆記

星期六, 6月 18, 2022

Innergie C6 (GaN)

交流轉 USB 電源

官網

輸入 100-240V AC/1.6A 50-60 Hz,單孔 USB-C 輸出 5V/3A, 9V/3A, 12V/3A, 15V/3A, 20V/3A (PPS?)

港都狼仔簡介測試 [面冊]

  • 空載功耗 0.046W
  • 充電定義支援 PD3.0/2.0、BC1.2、DCP 1.5A
  • 輸出轉換效率在 87.7% (5V/3A) 至 92.2% 之間

另購 C-T 1.5公尺 筆電充電線 僅適用於 60C Pro 與 C6 Duo 隨附6種筆電接頭

  • 港都狼仔簡介測試
  • 可搭配 18T:USB-C 充電連接器 輸入 19.5VDC/4.63A 輸出 5V/3A, 9V/2A, 20V/3A (沒接 Tip 時)
  • 可搭配 12T:USB-A 充電連接器 輸入 19.5VDC/0.74A 輸出 5V/2.4A

星期日, 6月 12, 2022

Registered jack

RJ11:6P2C,1 線電話界面

RJ14:6P4C,2 線電話界面

RJ25:6P6C,3 線電話界面

RJ9:4P4C,聽筒界面,非正式,沒直接接到公眾網路。黑紅綠黃,一般黑黃接 mic、紅綠 500ohm 接喇叭。

RJ21:25-pair (50-pin) miniature ribbon connector.

RJ48C:8P4C pin 1, 2, 4 and 5,T1

RJ48X:RJ48C 的回接。

https://en.wikipedia.org/wiki/Registered_jack

星期日, 6月 05, 2022

Linux 存取目錄

列出目錄的檔案。

opendir(目錄) 或 fdopendir(fd) 開啟一個 directory stream (DIR),包含此目錄的所有檔案 (含目錄)。readdir(DIR *) 自 directory stream 取得一個檔案,用 struct direct 表示,包含名稱等。再 readdir() 取得下一個,直到 NULL。

參考

  1. https://www.gnu.org/software/libc/manual/html_node/Accessing-Directories.html

星期日, 5月 29, 2022

USB Communication Device Class

Base Class 02h,通訊裝置包括電話、PSTN/DSL/Cable 數據機、ISDN、Ethernet/ATM/無線/行動網路。

Class CodeSubClass說明
02h 01hDirect Line Control Model[USBPSTN1.2]
02h 02h 00hAbstract Control Model (CDC-ACM)No Class Specified Protocol [USBPSTN1.2]
02h 02h 01hAT commands [V250] [USBPSTN1.2]
02h 03hTelephone Control Model[USBPSTN1.2]
02h 04hMulti-Channel Control Model[USBISDN1.2]
02h 05hCAPI Control Model[USBISDN1.2]
02h 06hEthernet Networking Control Model[USBECM1.2]
02h 07hATM Networking Control Model[USBATM1.2]
02h 08hWireless Handset Control Model[USBWMC1.1]
02h 09hDevice Management[USBWMC1.1]
02h 0AhMobile Direct Line Model[USBWMC1.1]
02h 0BhOBEX[USBWMC1.1]
02h 0ChEthernet Emulation Model[USBEEM1.0]
02h 0DhNetwork Control Model[USBNCM1.0]

一個 CDC 裝置必須支援兩個 Interface,一個描述所支援的功能,另一個描述資料傳輸的通道,透過 IAD (Interface Association Descriptor) 將兩者宣告屬於同一個 function。

RS232 over USB

Ethernet over USB:透過 USB 傳送 Ethernet frames,達到這的目的協定有 Microsoft 的 Remote Network Driver Interface Specification (RNDIS)、CDC Ethernet Control Model (ECM)、CDC Ethernet Emulation Model (EEM)、和 CDC Network Control Model (NCM)。

ECM 用在 USB full speed。

EEM is intended for use in communicating with devices, using Ethernet frames as the next layer of trans- port. It is not intended for use with routing or Internet connectivity devices, although this use is not pro- hibited.

NCM 改良 ECM 1.2 支援更高 data rates:單一 USB 傳送可送多個 Ethernet frames,並可指定偏好位置減少處理 overhead。NCM 定義兩種 encapsulating datagrams,一種長度可達 64KiB 可放 40 Ethernet frames,另一種達 4GiB 支援 High Speed 和 SuperSpeed data rates。一個 NCM function 包含一個 NCM Communications Interface 和一個 NCM Data Interface。NCM Communications Interface 用來設定和管理 the networking function。NCM Data Interface 使用定義的 endpoints 來傳送 data。

參考

  1. http://pollos-blog.blogspot.com/2014/10/rs232-over-usb.html
  2. https://en.wikipedia.org/wiki/Ethernet_over_USB
  3. https://www.usb.org/document-library/network-control-model-devices-specification-v10-and-errata-and-adopters-agreement
  • 比較:Video、MTP 傳送特定應用協定