|
OpenThread_app
|
TLV (Type-Length-Value) message serialization/deserialization API. More...
Macros | |
| #define | OT_APP_MSG_TLV_OK (-1) |
| Operation successful. | |
| #define | OT_APP_MSG_TLV_ERROR (-2) |
| Generic error (null ptr, invalid param). | |
| #define | OT_APP_MSG_TLV_ERROR_NO_SPACE (-3) |
| no space for new TLV | |
| #define | OT_APP_MSG_TLV_KEY_EXIST (-4) |
| Key already exists (duplicate add). | |
| #define | OT_APP_MSG_TLV_KEY_NO_EXIST (-5) |
| Key not found. | |
| #define | OT_APP_MSG_TLV_EMPTY_BUFFER (-5) |
| Buffer has no TLV data. | |
Functions | |
| int8_t | otapp_msg_tlv_keyAdd (uint8_t *buffer, const uint16_t bufferSize, const uint16_t key, const uint16_t valueLengthIn, uint8_t *valueIn) |
| Add new TLV block to buffer if key unique and space available. | |
| int8_t | otapp_msg_tlv_keyGet (uint8_t *buffer, const uint16_t bufferSize, const uint16_t key, uint16_t *valueLengthOut, uint8_t *valueOut) |
| Extract TLV value by key (optional copy to output). | |
| int8_t | otapp_msg_tlv_getBufferTotalFreeSpace (const uint8_t *buffer, const uint16_t bufferSize, uint16_t *freeBufSpaceOut) |
| Get remaining free space in TLV buffer. | |
| int8_t | otapp_msg_tlv_getBufferTotalUsedSpace (const uint8_t *buffer, const uint16_t bufferSize, uint16_t *writtenBufSpaceOut) |
| uint16_t | otapp_msg_tlv_calcualeBuffer (uint8_t keyDataLength, uint8_t cnt) |
TLV (Type-Length-Value) message serialization/deserialization API.
The TLV Message Buffer module provides serialization and deserialization of Type-Length-Value (TLV) data structures in a fixed-size user-provided byte buffer. Designed for constrained environments like OpenThread/CoAP payloads where dynamic allocation is avoided.
*Key features:**
*Buffer layout (exact byte-by-byte format):**
*Minimum size for 1 key: 2 + 4 + valueLength bytes**
*Example for valueLength=4 (TOTAL: 10 bytes minimum):**
| Bytes | Content | Size | Example | Description |
|---|---|---|---|---|
| [0:1] | writtenBytes | 2B | 0x000A (10) | TLV data counter |
| [2:3] | key | 2B | 0xAAA1 | TLV identifier |
| [4:5] | length | 2B | 0x0004 (4) | Value bytes count |
| [6:9] | value | 4B | [1,2,3,4] | Actual data |
| ↓ | TOTAL | 10B | Minimum buffer size |
*Error handling:**
*Typical lifecycle:**
*Integration:**
This example demonstrates how to gather device URI resources, serialize them into TLV format using a thread-safe system buffer, and transmit them as a CoAP response.
| #define OT_APP_MSG_TLV_EMPTY_BUFFER (-5) |
Buffer has no TLV data.
| #define OT_APP_MSG_TLV_ERROR (-2) |
Generic error (null ptr, invalid param).
| #define OT_APP_MSG_TLV_ERROR_NO_SPACE (-3) |
no space for new TLV
| #define OT_APP_MSG_TLV_KEY_EXIST (-4) |
Key already exists (duplicate add).
| #define OT_APP_MSG_TLV_KEY_NO_EXIST (-5) |
Key not found.
| #define OT_APP_MSG_TLV_OK (-1) |
Operation successful.
| uint16_t otapp_msg_tlv_calcualeBuffer | ( | uint8_t | keyDataLength, |
| uint8_t | cnt ) |
| int8_t otapp_msg_tlv_getBufferTotalFreeSpace | ( | const uint8_t * | buffer, |
| const uint16_t | bufferSize, | ||
| uint16_t * | freeBufSpaceOut ) |
Get remaining free space in TLV buffer.
Calculates: bufferSize - reserved(2B) - writtenBytes.
| buffer | Pointer to TLV buffer. |
| bufferSize | Total size. |
| freeBufSpaceOut | OUT: Free bytes available. |
| int8_t otapp_msg_tlv_getBufferTotalUsedSpace | ( | const uint8_t * | buffer, |
| const uint16_t | bufferSize, | ||
| uint16_t * | writtenBufSpaceOut ) |
| int8_t otapp_msg_tlv_keyAdd | ( | uint8_t * | buffer, |
| const uint16_t | bufferSize, | ||
| const uint16_t | key, | ||
| const uint16_t | valueLengthIn, | ||
| uint8_t * | valueIn ) |
Add new TLV block to buffer if key unique and space available.
Appends a TLV entry: key (u16) + length (u16) + value bytes. Updates internal writtenBytes counter. Validates space, duplicates, and parameters.
| buffer | Pointer to buffer (user-allocated). |
| bufferSize | Total buffer size in bytes. (must >= reserved 2B + TLV). |
| key | Unique 16-bit key identifier. |
| valueLengthIn | Value length in bytes (0 invalid). |
| valueIn | Pointer to value data to copy. |
| int8_t otapp_msg_tlv_keyGet | ( | uint8_t * | buffer, |
| const uint16_t | bufferSize, | ||
| const uint16_t | key, | ||
| uint16_t * | valueLengthOut, | ||
| uint8_t * | valueOut ) |
Extract TLV value by key (optional copy to output).
Linear scan from buffer start. Validates buffer integrity and key presence. Copies value if pointers provided.
| buffer | Pointer to TLV buffer. |
| bufferSize | Total buffer size. |
| key | 16-bit key to find. |
| valueLengthOut | OUT: Value length (NULL to skip). |
| valueOut | OUT: Value copy destination (NULL to skip). |