ble_mesh: Add support for 24 bit data type [Zephyr]

This enables pulling and pushing values in 24 bit format.
This commit is contained in:
lly 2020-03-26 09:59:43 +08:00
parent 33c0ddce40
commit 95718d9ef9
2 changed files with 138 additions and 0 deletions

View file

@ -241,6 +241,30 @@ void net_buf_simple_add_le16(struct net_buf_simple *buf, u16_t val);
*/
void net_buf_simple_add_be16(struct net_buf_simple *buf, u16_t val);
/**
* @brief Add 24-bit value at the end of the buffer
*
* Adds 24-bit value in little endian format at the end of buffer.
* Increments the data length of a buffer to account for more data
* at the end.
*
* @param buf Buffer to update.
* @param val 24-bit value to be added.
*/
void net_buf_simple_add_le24(struct net_buf_simple *buf, u32_t val);
/**
* @brief Add 24-bit value at the end of the buffer
*
* Adds 24-bit value in big endian format at the end of buffer.
* Increments the data length of a buffer to account for more data
* at the end.
*
* @param buf Buffer to update.
* @param val 24-bit value to be added.
*/
void net_buf_simple_add_be24(struct net_buf_simple *buf, u32_t val);
/**
* @brief Add 32-bit value at the end of the buffer
*
@ -372,6 +396,30 @@ u16_t net_buf_simple_pull_le16(struct net_buf_simple *buf);
*/
u16_t net_buf_simple_pull_be16(struct net_buf_simple *buf);
/**
* @brief Remove and convert 24 bits from the beginning of the buffer.
*
* Same idea as with net_buf_simple_pull(), but a helper for operating
* on 24-bit little endian data.
*
* @param buf A valid pointer on a buffer.
*
* @return 24-bit value converted from little endian to host endian.
*/
u32_t net_buf_simple_pull_le24(struct net_buf_simple *buf);
/**
* @brief Remove and convert 24 bits from the beginning of the buffer.
*
* Same idea as with net_buf_simple_pull(), but a helper for operating
* on 24-bit big endian data.
*
* @param buf A valid pointer on a buffer.
*
* @return 24-bit value converted from big endian to host endian.
*/
u32_t net_buf_simple_pull_be24(struct net_buf_simple *buf);
/**
* @brief Remove and convert 32 bits from the beginning of the buffer.
*
@ -882,6 +930,32 @@ static inline void *net_buf_user_data(struct net_buf *buf)
*/
#define net_buf_add_be16(buf, val) net_buf_simple_add_be16(&(buf)->b, val)
/**
* @def net_buf_add_le24
* @brief Add 24-bit value at the end of the buffer
*
* Adds 24-bit value in little endian format at the end of buffer.
* Increments the data length of a buffer to account for more data
* at the end.
*
* @param buf Buffer to update.
* @param val 24-bit value to be added.
*/
#define net_buf_add_le24(buf, val) net_buf_simple_add_le24(&(buf)->b, val)
/**
* @def net_buf_add_be24
* @brief Add 24-bit value at the end of the buffer
*
* Adds 24-bit value in big endian format at the end of buffer.
* Increments the data length of a buffer to account for more data
* at the end.
*
* @param buf Buffer to update.
* @param val 24-bit value to be added.
*/
#define net_buf_add_be24(buf, val) net_buf_simple_add_be24(&(buf)->b, val)
/**
* @def net_buf_add_le32
* @brief Add 32-bit value at the end of the buffer
@ -1024,6 +1098,32 @@ static inline void *net_buf_user_data(struct net_buf *buf)
*/
#define net_buf_pull_be16(buf) net_buf_simple_pull_be16(&(buf)->b)
/**
* @def net_buf_pull_le24
* @brief Remove and convert 24 bits from the beginning of the buffer.
*
* Same idea as with net_buf_pull(), but a helper for operating on
* 24-bit little endian data.
*
* @param buf A valid pointer on a buffer.
*
* @return 24-bit value converted from little endian to host endian.
*/
#define net_buf_pull_le24(buf) net_buf_simple_pull_le24(&(buf)->b)
/**
* @def net_buf_pull_be24
* @brief Remove and convert 24 bits from the beginning of the buffer.
*
* Same idea as with net_buf_pull(), but a helper for operating on
* 24-bit big endian data.
*
* @param buf A valid pointer on a buffer.
*
* @return 24-bit value converted from big endian to host endian.
*/
#define net_buf_pull_be24(buf) net_buf_simple_pull_be24(&(buf)->b)
/**
* @def net_buf_pull_le32
* @brief Remove and convert 32 bits from the beginning of the buffer.

View file

@ -84,6 +84,20 @@ void net_buf_simple_add_be16(struct net_buf_simple *buf, u16_t val)
memcpy(net_buf_simple_add(buf, sizeof(val)), &val, sizeof(val));
}
void net_buf_simple_add_le24(struct net_buf_simple *buf, u32_t val)
{
NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
sys_put_le24(val, net_buf_simple_add(buf, 3));
}
void net_buf_simple_add_be24(struct net_buf_simple *buf, u32_t val)
{
NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
sys_put_be24(val, net_buf_simple_add(buf, 3));
}
void net_buf_simple_add_le32(struct net_buf_simple *buf, u32_t val)
{
NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
@ -188,6 +202,30 @@ u16_t net_buf_simple_pull_be16(struct net_buf_simple *buf)
return sys_be16_to_cpu(val);
}
u32_t net_buf_simple_pull_le24(struct net_buf_simple *buf)
{
struct uint24 {
u32_t u24:24;
} __packed val;
val = UNALIGNED_GET((struct uint24 *)buf->data);
net_buf_simple_pull(buf, sizeof(val));
return sys_le24_to_cpu(val.u24);
}
u32_t net_buf_simple_pull_be24(struct net_buf_simple *buf)
{
struct uint24 {
u32_t u24:24;
} __packed val;
val = UNALIGNED_GET((struct uint24 *)buf->data);
net_buf_simple_pull(buf, sizeof(val));
return sys_be24_to_cpu(val.u24);
}
u32_t net_buf_simple_pull_le32(struct net_buf_simple *buf)
{
u32_t val = 0U;