From face869e3e21b5edee91f7310cded2960e113728 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 23 Aug 2018 10:17:26 +0200 Subject: [PATCH 1/2] tests/unittests/core: add byteorder_bebuftohs(), byteorder_htobebufs() tests --- .../tests-core/tests-core-byteorder.c | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/unittests/tests-core/tests-core-byteorder.c b/tests/unittests/tests-core/tests-core-byteorder.c index 9f5ecb6a95..2b51f0a514 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 + #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); From accf50ce07a3d9b842d48a8b07eb827fb424adf9 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 23 Aug 2018 10:34:21 +0200 Subject: [PATCH 2/2] core/byteorder: fix byteorder_htobebufs, byteorder_bebuftohs Logic was swapping byte order on Big Endian platforms --- core/include/byteorder.h | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/core/include/byteorder.h b/core/include/byteorder.h index d4d0edd613..a666bd2a80 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