From 95718d9ef9151b7a0421efab8b80beb17b556280 Mon Sep 17 00:00:00 2001 From: lly Date: Thu, 26 Mar 2020 09:59:43 +0800 Subject: [PATCH] ble_mesh: Add support for 24 bit data type [Zephyr] This enables pulling and pushing values in 24 bit format. --- .../mesh_common/include/mesh_buf.h | 100 ++++++++++++++++++ .../bt/esp_ble_mesh/mesh_common/mesh_buf.c | 38 +++++++ 2 files changed, 138 insertions(+) diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h index 9609698be..6a2698053 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h @@ -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. diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c index 2fe8da1da..4cbbab75a 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c @@ -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;