6.8. Utilities

6.8.1. Functions

UINT8_t *UTIL_htons(UINT8_t *buff_p, UINT16_t val)

Convert 16-bit value from host to network order. Note that MSP430 is Little Endian while networking is Big Endian.

Parameters:
  • buff_p – Pointers to buffer where value is to be stored. Function expects pre-allocated valid memory.
  • val – Value that needs to be converted.
Returns:

Points to next free location in buffer after conversion. In other words, this is a modification of input pointer buff_p.

UINT16_t UTIL_ntohs(UINT8_t *buff_p)

Convert 16-bit value from network order to host order. Note that MSP430 is Little Endian while networking is Big Endian.

Parameters:
  • buff_p – Points to buffer where input value is stored in network order.
Returns:

Converted 16-bit value in host order.

UINT32_t UTIL_ntohl(UINT8_t *buff_p)

Convert 32-bit value from network order to host order. Note that MSP430 is Little Endian while networking is Big Endian.

Parameters:
  • buff_p – Points to buffer where input value is stored in network order.
Returns:

Converted 32-bit value in host order.

UINT8_t *UTIL_htonl(UINT8_t *buff_p, UINT32_t val)

Convert 32-bit value from host to network order. Note that MSP430 is Little Endian while networking is Big Endian.

Parameters:
  • buff_p – Pointers to buffer where value is to be stored. Function expects pre-allocated valid memory.
  • val – Value that needs to be converted.
Returns:

Points to next free location in buffer after conversion. In other words, this is a modification of input pointer buff_p.

UINT8_t *UTIL_htole16(UINT8_t *buff_p, UINT16_t val)

Convert 16-bit value from host to Little Endian order. Note that MSP430 is Little Endian while networking is Big Endian. This function is useful in certain cases when a communication protocol operates in Little Endian and buffered data is stored in Big Endian.

Parameters:
  • buff_p – Pointers to buffer where value is to be stored. Function expects pre-allocated valid memory.
  • val – Value that needs to be coverted.
Returns:

Points to next free location in buffer after conversion. In other words, this is a modification of input pointer buff_p.

UINT8_t UTIL_getTlv(UINT8_t *buff_p, UINT8_t buffLen, UINT8_t tlvType, UINT8_t *tlvValLen_p, UINT8_t **tlvValBuff_pp)

TLV refers to Type-Length-Value format of communication data. This function is typically called for decoding a received packet. In particular, this function looks for the specified type and obtains the length and value. Both length and value are returned to caller via pointers passed as function arguments.

Parameters:
  • buff_p – Points to memory buffer where the data in TLV format is stored.
  • buffLen – Length of the buffer starting from buff_p. This is used to ensure that function does not read beyond the buffer boundary. If the desired type is not found in buffer, this helps the function to abort the process.
  • tlvType – The type to search.
  • tlvValLen_p – Function updates this pointer to a location where the length will be stored when the desired type is found.
  • tlvValBuff_pp – Function updates this pointer’s value to the start of payload (the value in TLV) when the desired type is found.
Returns:

Returns value 1 if type is found. If not found, value 0 is returned.

UINT8_t *UTIL_buildTlvHdr(UINT8_t *buff_p, UINT8_t tlvType, UINT8_t tlvPyldLen)

TLV refers to Type-Length-Value format of communication data. This function builds the TLV header by setting the type and length fields.

Parameters:
  • buff_p – Points to memory where the header is to be stored.
  • tlvType – Type of the message or field.
  • tlvPyldLen – Length of the message or field.
Returns:

Points to the next free memory location after type and length are encoded. In other words, this is a modification of input pointer buff_p.

6.8.2. Macros

#define UTIL_TLV_TYPE_FIELD_SZ   0x1
#define UTIL_TLV_LEN_FIELD_SZ    0x1

#define UTIL_TLV_HDR_SZ  \
        (UTIL_TLV_TYPE_FIELD_SZ + UTIL_TLV_LEN_FIELD_SZ)