1. Protocol message body
1. Message body TLV encoding
The protocol message body is encoded in TLV format, all in little endian order. TLV is a variable length code, where T represents Tag code, L represents data length Length, V represents data value Value, TLV can be nested, and Value itself can also be TLV code.
-
The Tag field consists of fixed 2 bytes (Byte), and the specific meaning is as follows:
Tag type (Byte1 high 4 bits) |
TLV encoding (low 4 bits of Byte1) |
Tag encoding (Byte2) |
0: original type 1: Custom type |
0: basic encoding 1: Structure coding |
A total of 0 to 255 fields can be coded for each tag type. |
The upper 4 bits of Byte1 are used to represent the type of Tag in the TLV, and 0 represents the primitive type (such as bool, int, string, float, etc., used to describe the value type of a specific message). 1 means user-defined type (used to describe the message in the protocol).
The lower 4 bits of Byte1 indicate the encoding method of TLV, 0 indicates the basic encoding, and the value itself is the value. 1 represents the structure code, and the value itself is the code in the TLV format.
Byte2 is used to encode the specific meaning of the Tag field. The tags and their meanings as agreed in this agreement are as follows:
Basic type, Tag encoding when the upper 4 bits of Byte1 are 0 |
|||
Tag encoding (Byte2) |
Desc |
Length |
Value |
1 |
Bool |
1 |
1:true,2:false |
2 |
Tiny |
1 |
-127~127 |
3 |
Unsigned small integer utiny |
1 |
0~255 |
4 |
Short |
2 |
-32768~32767 |
5 |
Unsigned short integer ushort |
2 |
0~65535 |
6 |
Integer int |
4 |
-2147483648~2147483648 |
7 |
Unsigned integer uint |
4 |
0~4294967295 |
8 |
Long |
8 |
-2^64 ~ 2^64 |
9 |
Unsigned long ulong |
8 |
0 ~2^65-1 |
10 |
Single-precision floating-point number float |
4 |
3.4*10^-38~3.4*10^38 |
11 |
Double-precision floating-point number double |
8 |
1.7*10^-308~1.7*10^308 |
12 |
Single character char |
1 |
ASCII |
13 |
String |
variable |
One or more chars |
14 |
Complex |
variable |
Consists of 1-9 |
15 |
Null |
0 |
|
Custom type, Tag encoding when the upper 4 bits of Byte1 are 1 |
|||
Tag encoding (Byte2) |
Desc |
Value type |
Value |
1 |
Device serial number SN |
String |
Such as "SN180000001234" |
2 |
Equipment manufacturer |
tiny |
1= 2= |
3 |
Equipment type |
tiny |
1= 2= |
4 |
System type |
tiny |
1=linux 2=windows 3= |
5 |
system version |
string |
|
6 |
Hardware model |
string |
|
7 |
hardware version |
string |
|
8 |
Equipment factory time |
uint |
The number of seconds from 1970-01-01 00:00:00 to the current time |
9 |
Device MAC address |
string |
|
10 |
Device startup time |
uint |
|
11 |
Application software version |
string |
|
12 |
Software update time |
uint |
|
13 |
Software startup time |
uint |
|
14 |
Software running status |
tiny |
1=normal 2=Abnormal |
15 |
Network card description |
string |
|
16 |
Device IP address |
uint |
Integer representation of IP address |
17 |
Subnet mask |
uint |
|
18 |
Gateway |
uint |
|
19 |
DNS server |
uint |
|
20 |
The number of bytes received by the network card |
long |
|
21 |
The number of bytes received by the network card per second |
long |
|
22 |
The number of bytes sent by the network card |
long |
|
23 |
The number of bytes sent by the network card per second |
long |
|
24 |
CPU description |
string |
|
25 |
CPU usage |
float |
|
26 |
GPU description |
string |
|
27 |
GPU usage |
float |
|
28 |
Memory description |
string |
|
29 |
Total memory size |
long |
|
30 |
Memory used size |
long |
|
31 |
Free memory size |
long |
|
32 |
Memory usage |
float |
|
33 |
Hard drive description |
string |
|
34 |
Total hard disk size |
long |
|
35 |
Hard disk used size |
long |
|
36 |
Disk free size |
long |
|
37 |
Hard disk usage |
float |
|
-
The Length field is a fixed 2 bytes to indicate the length of Value, ranging from 0 to 65535.
-
The Value field indicates the value of the data. The TLV encoding method is divided into basic encoding and structural encoding. The value of the structural encoding is also in the TLV format.
Basic coding:
T |
L |
V |
T |
L |
V |
Structure coding:
T |
L |
|
T |
L |
|
2. Protocol example
For example, we want to send two fields, device manufacturer and system version, as a response to the client.
Then you can define the TLV encoding message body structure as follows:
0x1102 |
Custom type, structure code, Tag means equipment manufacturer(2) |
|
Length |
0x0005 |
The length of Value is 5 bytes |
T |
0x0002 |
Basic type, basic code, Tag code represents tiny type |
L |
0x0001 |
The length of Value is 1 byte |
V |
0x01 |
The value is 1=xxxx |
0x1105 |
Custom type, structure code, Tag means system version(5) |
|
Length |
0x0023 |
The length of Value is 35 bytes |
T |
0x0013 |
Basic type, basic code, Tag code represents string type |
L |
0x001F |
The length of Value is 31 bytes |
V |
The value is "version 2.6.32-573.3.1.el6.i686" |
如上可见,上述协议虽然可以详细的描述字段值的类型,但当TLV每嵌套一层,都会有4字节增加(Tag和Length),所以一般通信双方可以按照协议对数据类型进行推定,省略第二层的Tag和Length。如确实需要可通过配置文件了解字段的类型,从而降低数据包的大小,节省流量。
则修改后的协议如下:
T |
0x1002 |
自定义类型,基本编码,Tag表示设备厂家(2) |
L |
0x0001 |
Value的长度为1字节 |
V |
0x01 |
值为1=xxxx |
T |
0x1005 |
自定义类型,基本编码,Tag表示系统版本(5) |
L |
0x001F |
Value的长度为31字节 |
V |
值为”version 2.6.32-573.3.1.el6.i686” |