TLV encoding

Base

The TLV protocol is a type of BER encoding, and its full name is Tag, length, and value. The protocol is simple and efficient, applicable to various communication scenarios, and has good scalability. The basic format of the TLV protocol is as follows:

insert image description here

Among them, Tag occupies 2 bytes, which is the unique identifier of the message; Length occupies 4 bytes, indicating the length of the Value field; the data in the Value field is the data to be transmitted, and the length is represented by the Length field.

Simple TLV

Simple TLV structure, the specific content of the Value field is represented by fixed-length fields, such as the number of bytes occupied by the first field and the number of bytes occupied by the second field, both of which are fixed-length and will not change. Therefore, the Length of the simple TLV structure is generally fixed, and different structures are distinguished by Tag, and there is no limit to the number of fields, which has strong scalability.

Hybrid TLV

Hybrid TLV structure, the specific content of the Value field is another TLV structure, so recursive, without any restrictions, as shown below:

insert image description here

It should be noted that the length field of the outer TLV structure must be the total length of the inner TLV structure, so that it conforms to the basic definition of TLV.

Cyclic TLV

In the cyclic TLV structure, the specific content of the Value field is composed of multiple data with the same structure. Therefore, in order to describe the number of cycles, it is necessary to define a field representing the number of cycles. The format is as follows:
insert image description here

Among them, the LoopNum field indicates the number of loops, followed by the sequential arrangement of data with the same structure. This structure is designed for messages with the same data format. It should be noted that the number of loops must match the number of subsequent data.

Pointer TLV
Pointer TLV structure, also known as array TLV, the length of a certain subfield in the Value field is uncertain and needs to be determined according to the value of the corresponding length field, so it cannot be defined in advance, so it is processed by a pointer. The processing of pointers can also solve the problem of variable-length arrays, so the processing methods of the two are the same.

Summarize

All the above TLV structures can be recursively nested without any restrictions, so they have very good scalability. The only regret is that there is no check field, and a check field can be added later to ensure the correctness and stability of the data transmission process.

This article is reproduced from: https://www.cnblogs.com/chusiyong/p/12191108.html

Guess you like

Origin blog.csdn.net/u010378559/article/details/131704993