USB笔记 字符串描述符(String Descriptor)
字符串描述符(String Descriptor)规定了所支持的语言或者存储文本字符串。部分描述符含有字符串索引,用于描述相关信息。字符串描述符是可选的。如果不支持字符串描述符,需要将相关的描述符中的字符串索引设置为0。
String Descriptor 0, 指定设备支持的语言(String Descriptor Zero, Specifying Languages Supported by the Device)
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | N+2 | 以字节为单位的描述符大小 |
1 | bDescriptorType | 1 | Constant | 字符串描述符类型 |
2 | wLANGID[0] | 2 | Number | LANGID 码 0 |
… | … | … | … | … |
N | wLANGID[x] | 2 | Number | LANGID 码 x |
bLength 以字节为单位的描述符大小。
bDescriptorType 字符串描述符类型,为String(0x03)。
wLANGID[0 - x] 语言ID。只对String Descriptor 0有效。字段含有USB-IF定义的16-bit语言ID,表明了字符串的可用语言。wLANGID必须有效,才能使其他的字符串有效。对于不支持字符串描述符的设备,也不能返回语言ID。USB-IF语言ID的定义可以查看文档Universal Serial Bus (USB) Language Identifiers (LANGIDs) Version 1.0。最常用的语言ID是0x0409(English (United States))。LANGID码数组不以NULL结束。数组的大小由描述符第一个字节的值减2计算出。
UNICODE String Descriptor
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | 以字节为单位的描述符大小 |
1 | bDescriptorType | 1 | Constant | 字符串描述符类型 |
2 | bString | N | Number | UNICODE 编码字符串 |
bLength 以字节为单位的描述符大小。
bDescriptorType 字符串描述符类型,为String(0x03)。
bString UNICODE 编码的字符串。对String Descriptor 0x01和更高的值有效。字段含有Unicode UTF-16LE 格式的字符串。编码定义于《The Unicode Standard, Worldwide Character Encoding》。UNICODE 字符串描述符不以NULL结束。字符串的大小由描述符第一个字节的值减2计算出。
获取字符串描述符
主机通过发送Get Descriptor
请求,来取得字符串描述符(String Descriptor)。wValue的低字节是索引值。取0的索引值表示请求语言ID(LANGID),其他索引值表示请求字符串。
Get Descriptor | Value |
---|---|
bmRequestType | 0x80 |
bmRequestType.Recipient | 0b00000(Device) |
bmRequestType.Type | 0b00(Standard) |
bmRequestType.Direction | 0b1(Device-to-Host) |
bRequest | 0x06(Get Descriptor) |
wValue | 0x03??(String #?) |
wIndex | 0x??? |
wLength | 0x??? |
下面举例说明一个获取字符串描述符String Descriptor的过程。使用USB分析仪抓包,软件Total Phase Data Center。
获取一个键盘的字符串描述符,该键盘的字符串描述符如下所示:
1.String Descriptor Zero, Specifying Languages Supported by the Device
2.UNICODE String Descriptor
使用USB分析仪抓包可以看到,获取字符串描述符String Descriptor:
字符串描述符String Descriptor的获取过程如下:
再展开看到详细的过程:
接下来详细看整个获取过程的各个字段含义:
一开始的setup包过程:
1.String Descriptor Zero, Specifying Languages Supported by the Device
2.UNICODE String Descriptor
setup data:
1.String Descriptor Zero, Specifying Languages Supported by the Device
2.UNICODE String Descriptor
IN包过程:
OUT包过程:
完成整个字符串描述符String Descriptor的获取。
描述符示例
这里举例一个带有该描述符的示例设备,这是一个键盘Keyboard的信息。
String Descriptor Zero, Specifying Languages Supported by the Device
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | 0x04 | |
1 | bDescriptorType | 1 | 0x03 | STRING |
2 | bString | 2 | 0x0409 | English (United States) |
UNICODE String Descriptor
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | 0x1A | |
1 | bDescriptorType | 1 | 0x03 | STRING |
2 | bString[0] | 1 | 0x55 | ‘U’ |
3 | bString[1] | 1 | 0x00 | |
4 | bString[2] | 1 | 0x53 | ‘S’ |
5 | bString[3] | 1 | 0x00 | |
6 | bString[4] | 1 | 0x42 | ‘B’ |
7 | bString[5] | 1 | 0x00 | |
8 | bString[6] | 1 | 0x20 | ’ ’ |
9 | bString[7] | 1 | 0x00 | |
10 | bString[8] | 1 | 0x4B | ‘K’ |
11 | bString[9] | 1 | 0x00 | |
12 | bString[10] | 1 | 0x65 | ‘e’ |
13 | bString[11] | 1 | 0x00 | |
14 | bString[12] | 1 | 0x79 | ‘y’ |
15 | bString[13] | 1 | 0x00 | |
16 | bString[14] | 1 | 0x62 | ‘b’ |
17 | bString[15] | 1 | 0x00 | |
18 | bString[16] | 1 | 0x6F | ‘o’ |
19 | bString[17] | 1 | 0x00 | |
20 | bString[18] | 1 | 0x61 | ‘a’ |
21 | bString[19] | 1 | 0x00 | |
22 | bString[20] | 1 | 0x72 | ‘r’ |
23 | bString[21] | 1 | 0x00 | |
24 | bString[22] | 1 | 0x64 | ‘d’ |
25 | bString[23] | 1 | 0x00 |
附录
USB 1.x/USB 2.0/USB 3.x String Descriptor Zero, Specifying Languages Supported by the Device
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | N+2 | Size of this descriptor in bytes |
1 | bDescriptorType | 1 | Constant | STRING Descriptor Type |
2 | wLANGID[0] | 2 | Number | LANGID code zero |
… | … | … | … | … |
N | wLANGID[x] | 2 | Number | LANGID code x |
USB 1.1/USB 2.0/USB 3.x UNICODE String Descriptor
Offset | Field | Size | Value | Description |
---|---|---|---|---|
0 | bLength | 1 | Number | Size of this descriptor in bytes |
1 | bDescriptorType | 1 | Constant | STRING Descriptor Type |
2 | bString | N | Number | UNICODE encoded string |
[参考资料]
Universal Serial Bus Specification Revision 1.0
Universal Serial Bus Specification Revision 1.1
Universal Serial Bus Specification Revision 2.0
Universal Serial Bus 3.0 Specification
Universal Serial Bus 3.1 Specification
Universal Serial Bus 3.2 Specification
Universal Serial Bus (USB) Language Identifiers (LANGIDs) Version 1.0
USB Complete, 5th Edition
USB开发大全
USB2.0与OTG规范及开发指南
本文链接:https://blog.csdn.net/u012028275/article/details/109698493