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

core/byteorder: add uint32 from/to buffer funcs

This commit is contained in:
Jose Alamos 2020-06-26 11:43:34 +02:00
parent 972d9441ce
commit 244d5191c8

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