diff --git a/core/include/byteorder.h b/core/include/byteorder.h index d4d0edd613a58d4ae266447d72e28551bad38f1e..a666bd2a808c63f2311211c7f817f8b39fe1b926 100644 --- a/core/include/byteorder.h +++ b/core/include/byteorder.h @@ -445,22 +445,13 @@ static inline uint64_t ntohll(uint64_t v) static inline uint16_t byteorder_bebuftohs(const uint8_t *buf) { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return (uint16_t)((buf[0] << 8) | buf[1]); -#else - return (uint16_t)((buf[1] << 8) | buf[0]); -#endif + return (uint16_t)((buf[0] << 8) | (buf[1] << 0)); } static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val) { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ buf[0] = (uint8_t)(val >> 8); - buf[1] = (uint8_t)(val & 0xff); -#else - buf[0] = (uint8_t)(val & 0xff); - buf[1] = (uint8_t)(val >> 8); -#endif + buf[1] = (uint8_t)(val >> 0); } #ifdef __cplusplus diff --git a/tests/unittests/tests-core/tests-core-byteorder.c b/tests/unittests/tests-core/tests-core-byteorder.c index 9f5ecb6a954e4fd692d47a2c0b57bddad5bce8d1..2b51f0a51470c92452d24af234816957f21f75fc 100644 --- a/tests/unittests/tests-core/tests-core-byteorder.c +++ b/tests/unittests/tests-core/tests-core-byteorder.c @@ -6,6 +6,8 @@ * directory for more details. */ +#include <string.h> + #include "embUnit.h" #include "byteorder.h" @@ -78,6 +80,26 @@ static void test_byteorder_host_to_network_64(void) TEST_ASSERT_EQUAL_INT(host, byteorder_ntohll(network)); } +static void test_byteorder_bebuftohs(void) +{ + static const uint8_t bebuf[2] = { 0xAA, 0xBB }; + static const uint16_t host = 0xAABB; + + TEST_ASSERT_EQUAL_INT(host, byteorder_bebuftohs(bebuf)); +} + +static void test_byteorder_htobebufs(void) +{ + static const uint8_t bebuf[2] = { 0xAA, 0xBB }; + static const uint16_t host = 0xAABB; + + uint8_t tmp[2] = {0}; + + byteorder_htobebufs(tmp, host); + + TEST_ASSERT_EQUAL_INT(0, memcmp(bebuf, tmp, sizeof(tmp))); +} + Test *tests_core_byteorder_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures) { @@ -90,6 +112,8 @@ Test *tests_core_byteorder_tests(void) new_TestFixture(test_byteorder_host_to_network_16), new_TestFixture(test_byteorder_host_to_network_32), new_TestFixture(test_byteorder_host_to_network_64), + new_TestFixture(test_byteorder_bebuftohs), + new_TestFixture(test_byteorder_htobebufs), }; EMB_UNIT_TESTCALLER(core_byteorder_tests, NULL, NULL, fixtures);