1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #14368 from jia200x/pr/byteorder_bebuftohs

core/byteorder: add uint32 from/to buffer funcs
This commit is contained in:
benpicco 2020-07-13 12:31:43 +02:00 committed by GitHub
commit e2e51ae7ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

View File

@ -237,6 +237,19 @@ static inline uint64_t byteorder_swapll(uint64_t v);
*/
static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
/**
* @brief Read a big endian encoded unsigned integer from a buffer
* into host byte order encoded variable, 32-bit
*
* @note This function is agnostic to the alignment of the target
* value in the given buffer
*
* @param[in] buf position in a buffer holding the target value
*
* @return 32-bit unsigned integer in host byte order
*/
static inline uint32_t byteorder_bebuftohl(const uint8_t *buf);
/**
* @brief Write a host byte order encoded unsigned integer as big
* endian encoded value into a buffer, 16-bit
@ -249,6 +262,18 @@ static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
*/
static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val);
/**
* @brief Write a host byte order encoded unsigned integer as big
* endian encoded value into a buffer, 32-bit
*
* @note This function is alignment agnostic and works with any given
* memory location of the buffer
*
* @param[out] buf target buffer, must be able to accept 4 bytes
* @param[in] val value written to the buffer, in host byte order
*/
static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val);
/**
* @brief Convert from host byte order to network byte order, 16 bit.
* @see byteorder_htons()
@ -460,12 +485,28 @@ static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
return (uint16_t)((buf[0] << 8) | (buf[1] << 0));
}
static inline uint32_t byteorder_bebuftohl(const uint8_t *buf)
{
return (((uint32_t) buf[0] << 24)
| ((uint32_t) buf[1] << 16)
| ((uint32_t) buf[2] << 8)
| ((uint32_t) buf[3] << 0));
}
static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
{
buf[0] = (uint8_t)(val >> 8);
buf[1] = (uint8_t)(val >> 0);
}
static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val)
{
buf[0] = (uint8_t)(val >> 24);
buf[1] = (uint8_t)(val >> 16);
buf[2] = (uint8_t)(val >> 8);
buf[3] = (uint8_t)(val >> 0);
}
#ifdef __cplusplus
}
#endif

View File

@ -88,6 +88,14 @@ static void test_byteorder_bebuftohs(void)
TEST_ASSERT_EQUAL_INT(host, byteorder_bebuftohs(bebuf));
}
static void test_byteorder_bebuftohl(void)
{
static const uint8_t bebuf[4] = { 0xAA, 0xBB, 0xCC, 0xDD};
static const uint32_t host = 0xAABBCCDD;
TEST_ASSERT_EQUAL_INT(host, byteorder_bebuftohl(bebuf));
}
static void test_byteorder_htobebufs(void)
{
static const uint8_t bebuf[2] = { 0xAA, 0xBB };
@ -100,6 +108,18 @@ static void test_byteorder_htobebufs(void)
TEST_ASSERT_EQUAL_INT(0, memcmp(bebuf, tmp, sizeof(tmp)));
}
static void test_byteorder_htobebufl(void)
{
static const uint8_t bebuf[4] = { 0xAA, 0xBB, 0xCC, 0xDD};
static const uint32_t host = 0xAABBCCDD;
uint8_t tmp[4] = {0};
byteorder_htobebufl(tmp, host);
TEST_ASSERT_EQUAL_INT(0, memcmp(bebuf, tmp, sizeof(tmp)));
}
Test *tests_core_byteorder_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
@ -114,6 +134,8 @@ Test *tests_core_byteorder_tests(void)
new_TestFixture(test_byteorder_host_to_network_64),
new_TestFixture(test_byteorder_bebuftohs),
new_TestFixture(test_byteorder_htobebufs),
new_TestFixture(test_byteorder_bebuftohl),
new_TestFixture(test_byteorder_htobebufl),
};
EMB_UNIT_TESTCALLER(core_byteorder_tests, NULL, NULL, fixtures);