From cdbcfc4ce7afdde2916edd252a258163e7ce3bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rymanowski?= Date: Wed, 30 Oct 2019 11:05:19 +0100 Subject: [PATCH] porting/endian: Add le24 and be24 helpers Sync with apache-mynewt-core Those are useful for some BLE and Mesh PDU handling (and probably more). Instead of user defining own function it should be beneficial to have common implementation. Not that this is useful only for flat buffers so only put and get helpers. --- porting/nimble/include/os/endian.h | 4 +++ porting/nimble/src/endian.c | 50 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/porting/nimble/include/os/endian.h b/porting/nimble/include/os/endian.h index ca8698d43..e02307569 100644 --- a/porting/nimble/include/os/endian.h +++ b/porting/nimble/include/os/endian.h @@ -204,15 +204,19 @@ extern "C" { #endif void put_le16(void *buf, uint16_t x); +void put_le24(void *buf, uint32_t x); void put_le32(void *buf, uint32_t x); void put_le64(void *buf, uint64_t x); uint16_t get_le16(const void *buf); +uint32_t get_le24(const void *buf); uint32_t get_le32(const void *buf); uint64_t get_le64(const void *buf); void put_be16(void *buf, uint16_t x); +void put_be24(void *buf, uint32_t x); void put_be32(void *buf, uint32_t x); void put_be64(void *buf, uint64_t x); uint16_t get_be16(const void *buf); +uint32_t get_be24(const void *buf); uint32_t get_be32(const void *buf); uint64_t get_be64(const void *buf); void swap_in_place(void *buf, int len); diff --git a/porting/nimble/src/endian.c b/porting/nimble/src/endian.c index 1311f43ef..2afd6a22e 100644 --- a/porting/nimble/src/endian.c +++ b/porting/nimble/src/endian.c @@ -29,6 +29,17 @@ put_le16(void *buf, uint16_t x) u8ptr[1] = (uint8_t)(x >> 8); } +void +put_le24(void *buf, uint32_t x) +{ + uint8_t *u8ptr; + + u8ptr = buf; + u8ptr[0] = (uint8_t)x; + u8ptr[1] = (uint8_t)(x >> 8); + u8ptr[2] = (uint8_t)(x >> 16); +} + void put_le32(void *buf, uint32_t x) { @@ -70,6 +81,20 @@ get_le16(const void *buf) return x; } +uint32_t +get_le24(const void *buf) +{ + const uint8_t *u8ptr; + uint32_t x; + + u8ptr = buf; + x = u8ptr[0]; + x |= (uint32_t)u8ptr[1] << 8; + x |= (uint32_t)u8ptr[2] << 16; + + return x; +} + uint32_t get_le32(const void *buf) { @@ -114,6 +139,17 @@ put_be16(void *buf, uint16_t x) u8ptr[1] = (uint8_t)x; } +void +put_be24(void *buf, uint32_t x) +{ + uint8_t *u8ptr; + + u8ptr = buf; + u8ptr[0] = (uint8_t)(x >> 24); + u8ptr[1] = (uint8_t)(x >> 16); + u8ptr[2] = (uint8_t)(x >> 8); +} + void put_be32(void *buf, uint32_t x) { @@ -155,6 +191,20 @@ get_be16(const void *buf) return x; } +uint32_t +get_be24(const void *buf) +{ + const uint8_t *u8ptr; + uint32_t x; + + u8ptr = buf; + x = (uint32_t)u8ptr[0] << 24; + x |= (uint32_t)u8ptr[1] << 16; + x |= (uint32_t)u8ptr[2] << 8; + + return x; +} + uint32_t get_be32(const void *buf) {