ble_mesh: Add 48 bit variants [Zephyr]

This adds sys_*_*48 functions that operation on a 48 bits values.
This commit is contained in:
lly 2020-03-26 10:06:48 +08:00
parent e7d37f2d1a
commit 4536a485be

View file

@ -85,6 +85,15 @@ extern "C" {
(((x) & 0xff) << 24)))
#endif
#ifndef __bswap_48
#define __bswap_48(x) ((u64_t) ((((x) >> 40) & 0xff) | \
(((x) >> 24) & 0xff00) | \
(((x) >> 8) & 0xff0000) | \
(((x) & 0xff0000) << 8) | \
(((x) & 0xff00) << 24) | \
(((x) & 0xff) << 40)))
#endif
#ifndef __bswap_64
#define __bswap_64(x) ((u64_t) ((((x) >> 56) & 0xff) | \
(((x) >> 40) & 0xff00) | \
@ -144,6 +153,22 @@ extern "C" {
* @return 32-bit integer in little-endian format.
*/
/** @def sys_le48_to_cpu
* @brief Convert 48-bit integer from little-endian to host endianness.
*
* @param val 48-bit integer in little-endian format.
*
* @return 48-bit integer in host endianness.
*/
/** @def sys_cpu_to_le48
* @brief Convert 48-bit integer from host endianness to little-endian.
*
* @param val 48-bit integer in host endianness.
*
* @return 48-bit integer in little-endian format.
*/
/** @def sys_be16_to_cpu
* @brief Convert 16-bit integer from big-endian to host endianness.
*
@ -192,12 +217,30 @@ extern "C" {
* @return 32-bit integer in big-endian format.
*/
/** @def sys_be48_to_cpu
* @brief Convert 48-bit integer from big-endian to host endianness.
*
* @param val 48-bit integer in big-endian format.
*
* @return 48-bit integer in host endianness.
*/
/** @def sys_cpu_to_be48
* @brief Convert 48-bit integer from host endianness to big-endian.
*
* @param val 48-bit integer in host endianness.
*
* @return 48-bit integer in big-endian format.
*/
#define sys_le16_to_cpu(val) (val)
#define sys_cpu_to_le16(val) (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_le48_to_cpu(val) (val)
#define sys_cpu_to_le48(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)
@ -206,6 +249,8 @@ extern "C" {
#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_be48_to_cpu(val) __bswap_48(val)
#define sys_cpu_to_be48(val) __bswap_48(val)
#define sys_be64_to_cpu(val) __bswap_64(val)
#define sys_cpu_to_be64(val) __bswap_64(val)
@ -371,6 +416,21 @@ static inline void sys_put_be32(u32_t val, u8_t dst[4])
sys_put_be16(val, &dst[2]);
}
/**
* @brief Put a 48-bit integer as big-endian to arbitrary location.
*
* Put a 48-bit integer, originally in host endianness, to a
* potentially unaligned memory location in big-endian format.
*
* @param val 48-bit integer in host endianness.
* @param dst Destination memory address to store the result.
*/
static inline void sys_put_be48(u64_t val, u8_t dst[6])
{
sys_put_be16(val >> 32, dst);
sys_put_be32(val, &dst[2]);
}
/**
* @brief Put a 16-bit integer as little-endian to arbitrary location.
*
@ -416,6 +476,21 @@ static inline void sys_put_le32(u32_t val, u8_t dst[4])
sys_put_le16(val >> 16, &dst[2]);
}
/**
* @brief Put a 48-bit integer as little-endian to arbitrary location.
*
* Put a 48-bit integer, originally in host endianness, to a
* potentially unaligned memory location in little-endian format.
*
* @param val 48-bit integer in host endianness.
* @param dst Destination memory address to store the result.
*/
static inline void sys_put_le48(u64_t val, u8_t dst[6])
{
sys_put_le32(val, dst);
sys_put_le16(val >> 32, &dst[4]);
}
/**
* @brief Put a 64-bit integer as little-endian to arbitrary location.
*
@ -476,6 +551,21 @@ static inline u32_t sys_get_be32(const u8_t src[4])
return ((u32_t)sys_get_be16(&src[0]) << 16) | sys_get_be16(&src[2]);
}
/**
* @brief Get a 48-bit integer stored in big-endian format.
*
* Get a 48-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 48-bit integer to get.
*
* @return 48-bit integer in host endianness.
*/
static inline u64_t sys_get_be48(const u8_t src[6])
{
return ((u64_t)sys_get_be32(&src[0]) << 32) | sys_get_be16(&src[4]);
}
/**
* @brief Get a 16-bit integer stored in little-endian format.
*
@ -521,6 +611,21 @@ static inline u32_t sys_get_le32(const u8_t src[4])
return ((u32_t)sys_get_le16(&src[2]) << 16) | sys_get_le16(&src[0]);
}
/**
* @brief Get a 48-bit integer stored in little-endian format.
*
* Get a 48-bit integer, stored in little-endian format in a potentially
* unaligned memory location, and convert it to the host endianness.
*
* @param src Location of the little-endian 48-bit integer to get.
*
* @return 48-bit integer in host endianness.
*/
static inline u64_t sys_get_le48(const u8_t src[6])
{
return ((u64_t)sys_get_le32(&src[2]) << 32) | sys_get_le16(&src[0]);
}
/**
* @brief Get a 64-bit integer stored in little-endian format.
*