From 4963d4ddf8e828adb4820bfe035ae0a6cd493111 Mon Sep 17 00:00:00 2001 From: lly Date: Thu, 26 Mar 2020 09:50:07 +0800 Subject: [PATCH] ble_mesh: Add 24 bit variants [Zephyr] This adds sys_*_*24 functions that operation on a 24 bits values. --- .../mesh_common/include/mesh_util.h | 192 ++++++++++++++++-- 1 file changed, 179 insertions(+), 13 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h index 19c0a17ad2..d9d9021e79 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h @@ -72,32 +72,138 @@ extern "C" { #define __bswap_16(x) ((u16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))) #endif +#ifndef __bswap_24 +#define __bswap_24(x) ((u32_t) ((((x) >> 16) & 0xff) | \ + (((x)) & 0xff00) | \ + (((x) & 0xff) << 16))) +#endif + #ifndef __bswap_32 #define __bswap_32(x) ((u32_t) ((((x) >> 24) & 0xff) | \ - (((x) >> 8) & 0xff00) | \ - (((x) & 0xff00) << 8) | \ - (((x) & 0xff) << 24))) + (((x) >> 8) & 0xff00) | \ + (((x) & 0xff00) << 8) | \ + (((x) & 0xff) << 24))) #endif #ifndef __bswap_64 -#define __bswap_64(x) ((u64_t) ((((x) >> 56) & 0xff) | \ - (((x) >> 40) & 0xff00) | \ - (((x) >> 24) & 0xff0000) | \ - (((x) >> 8) & 0xff000000) | \ - (((x) & 0xff000000) << 8) | \ - (((x) & 0xff0000) << 24) | \ - (((x) & 0xff00) << 40) | \ - (((x) & 0xff) << 56))) +#define __bswap_64(x) ((u64_t) ((((x) >> 56) & 0xff) | \ + (((x) >> 40) & 0xff00) | \ + (((x) >> 24) & 0xff0000) | \ + (((x) >> 8) & 0xff000000) | \ + (((x) & 0xff000000) << 8) | \ + (((x) & 0xff0000) << 24) | \ + (((x) & 0xff00) << 40) | \ + (((x) & 0xff) << 56))) #endif +/** @def sys_le16_to_cpu + * @brief Convert 16-bit integer from little-endian to host endianness. + * + * @param val 16-bit integer in little-endian format. + * + * @return 16-bit integer in host endianness. + */ + +/** @def sys_cpu_to_le16 + * @brief Convert 16-bit integer from host endianness to little-endian. + * + * @param val 16-bit integer in host endianness. + * + * @return 16-bit integer in little-endian format. + */ + +/** @def sys_le24_to_cpu + * @brief Convert 24-bit integer from little-endian to host endianness. + * + * @param val 24-bit integer in little-endian format. + * + * @return 24-bit integer in host endianness. + */ + +/** @def sys_cpu_to_le24 + * @brief Convert 24-bit integer from host endianness to little-endian. + * + * @param val 24-bit integer in host endianness. + * + * @return 24-bit integer in little-endian format. + */ + +/** @def sys_le32_to_cpu + * @brief Convert 32-bit integer from little-endian to host endianness. + * + * @param val 32-bit integer in little-endian format. + * + * @return 32-bit integer in host endianness. + */ + +/** @def sys_cpu_to_le32 + * @brief Convert 32-bit integer from host endianness to little-endian. + * + * @param val 32-bit integer in host endianness. + * + * @return 32-bit integer in little-endian format. + */ + +/** @def sys_be16_to_cpu + * @brief Convert 16-bit integer from big-endian to host endianness. + * + * @param val 16-bit integer in big-endian format. + * + * @return 16-bit integer in host endianness. + */ + +/** @def sys_cpu_to_be16 + * @brief Convert 16-bit integer from host endianness to big-endian. + * + * @param val 16-bit integer in host endianness. + * + * @return 16-bit integer in big-endian format. + */ + +/** @def sys_be24_to_cpu + * @brief Convert 24-bit integer from big-endian to host endianness. + * + * @param val 24-bit integer in big-endian format. + * + * @return 24-bit integer in host endianness. + */ + +/** @def sys_cpu_to_be24 + * @brief Convert 24-bit integer from host endianness to big-endian. + * + * @param val 24-bit integer in host endianness. + * + * @return 24-bit integer in big-endian format. + */ + +/** @def sys_be32_to_cpu + * @brief Convert 32-bit integer from big-endian to host endianness. + * + * @param val 32-bit integer in big-endian format. + * + * @return 32-bit integer in host endianness. + */ + +/** @def sys_cpu_to_be32 + * @brief Convert 32-bit integer from host endianness to big-endian. + * + * @param val 32-bit integer in host endianness. + * + * @return 32-bit integer in big-endian format. + */ + #define sys_le16_to_cpu(val) (val) #define sys_cpu_to_le16(val) (val) -#define sys_be16_to_cpu(val) __bswap_16(val) -#define sys_cpu_to_be16(val) __bswap_16(val) +#define sys_le24_to_cpu(val) (val) +#define sys_cpu_to_le24(val) (val) #define sys_le32_to_cpu(val) (val) #define sys_cpu_to_le32(val) (val) #define sys_le64_to_cpu(val) (val) #define sys_cpu_to_le64(val) (val) +#define sys_be16_to_cpu(val) __bswap_16(val) +#define sys_cpu_to_be16(val) __bswap_16(val) +#define sys_be24_to_cpu(val) __bswap_24(val) +#define sys_cpu_to_be24(val) __bswap_24(val) #define sys_be32_to_cpu(val) __bswap_32(val) #define sys_cpu_to_be32(val) __bswap_32(val) #define sys_be64_to_cpu(val) __bswap_64(val) @@ -235,6 +341,21 @@ static inline void sys_put_be16(u16_t val, u8_t dst[2]) dst[1] = val; } +/** + * @brief Put a 24-bit integer as big-endian to arbitrary location. + * + * Put a 24-bit integer, originally in host endianness, to a + * potentially unaligned memory location in big-endian format. + * + * @param val 24-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_be24(u32_t val, u8_t dst[3]) +{ + dst[0] = val >> 16; + sys_put_be16(val, &dst[1]); +} + /** * @brief Put a 32-bit integer as big-endian to arbitrary location. * @@ -265,6 +386,21 @@ static inline void sys_put_le16(u16_t val, u8_t dst[2]) dst[1] = val >> 8; } +/** + * @brief Put a 24-bit integer as little-endian to arbitrary location. + * + * Put a 24-bit integer, originally in host endianness, to a + * potentially unaligned memory location in littel-endian format. + * + * @param val 24-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_le24(u32_t val, u8_t dst[3]) +{ + sys_put_le16(val, dst); + dst[2] = val >> 16; +} + /** * @brief Put a 32-bit integer as little-endian to arbitrary location. * @@ -310,6 +446,21 @@ static inline u16_t sys_get_be16(const u8_t src[2]) return ((u16_t)src[0] << 8) | src[1]; } +/** + * @brief Get a 24-bit integer stored in big-endian format. + * + * Get a 24-bit integer, stored in big-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the big-endian 24-bit integer to get. + * + * @return 24-bit integer in host endianness. + */ +static inline u32_t sys_get_be24(const u8_t src[3]) +{ + return ((u32_t)src[0] << 16) | sys_get_be16(&src[1]); +} + /** * @brief Get a 32-bit integer stored in big-endian format. * @@ -340,6 +491,21 @@ static inline u16_t sys_get_le16(const u8_t src[2]) return ((u16_t)src[1] << 8) | src[0]; } +/** + * @brief Get a 24-bit integer stored in big-endian format. + * + * Get a 24-bit integer, stored in big-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the big-endian 24-bit integer to get. + * + * @return 24-bit integer in host endianness. + */ +static inline u32_t sys_get_le24(const u8_t src[3]) +{ + return ((u32_t)src[2] << 16) | sys_get_le16(&src[0]); +} + /** * @brief Get a 32-bit integer stored in little-endian format. *