Skip to content

Commit 6a3f885

Browse files
joerchankartben
authored andcommitted
sys: byteorder: Add endian-specific buffer convert and copy functions
Add endian specific buffer convert and copy functions. This is helpful when you want to convert arbitrary data lengths but only swapping byte order when needed. Signed-off-by: Joakim Andersson <[email protected]>
1 parent 54ecd18 commit 6a3f885

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

include/zephyr/sys/byteorder.h

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
#include <zephyr/types.h>
1515
#include <stddef.h>
16+
#include <string.h>
1617
#include <zephyr/sys/__assert.h>
18+
#include <zephyr/sys/util_macro.h>
1719
#include <zephyr/toolchain.h>
1820

1921
#define BSWAP_16(x) ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
@@ -719,4 +721,132 @@ static inline void sys_mem_swap(void *buf, size_t length)
719721
}
720722
}
721723

724+
/**
725+
* @brief Convert buffer from little-endian to host endianness.
726+
*
727+
* @param buf A valid pointer on a memory area to convert from little-endian to host endianness.
728+
* @param length Size of buf memory area
729+
*/
730+
static inline void sys_le_to_cpu(void *buf, size_t length)
731+
{
732+
if (IS_ENABLED(CONFIG_BIG_ENDIAN)) {
733+
sys_mem_swap(buf, length);
734+
}
735+
}
736+
737+
/**
738+
* @brief Convert buffer from host endianness to little-endian.
739+
*
740+
* @param buf A valid pointer on a memory area to convert from host endianness to little-endian.
741+
* @param length Size of buf memory area
742+
*/
743+
static inline void sys_cpu_to_le(void *buf, size_t length)
744+
{
745+
if (IS_ENABLED(CONFIG_BIG_ENDIAN)) {
746+
sys_mem_swap(buf, length);
747+
}
748+
}
749+
750+
/**
751+
* @brief Convert buffer from big-endian to host endianness.
752+
*
753+
* @param buf A valid pointer on a memory area to convert from big-endian to host endianness.
754+
* @param length Size of buf memory area
755+
*/
756+
static inline void sys_be_to_cpu(void *buf, size_t length)
757+
{
758+
if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
759+
sys_mem_swap(buf, length);
760+
}
761+
}
762+
763+
/**
764+
* @brief Convert buffer from host endianness to big-endian.
765+
*
766+
* @param buf A valid pointer on a memory area to convert from host endianness to big-endian.
767+
* @param length Size of buf memory area
768+
*/
769+
static inline void sys_cpu_to_be(void *buf, size_t length)
770+
{
771+
if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
772+
sys_mem_swap(buf, length);
773+
}
774+
}
775+
776+
/**
777+
* @brief Put a buffer as little-endian to arbitrary location.
778+
*
779+
* Put a buffer originally in host endianness, to a
780+
* potentially unaligned memory location in little-endian format.
781+
*
782+
* @param dst A valid pointer on a memory area where to copy the data in
783+
* @param src A valid pointer on a memory area where to copy the data from
784+
* @param length Size of both dst and src memory areas
785+
*/
786+
static inline void sys_put_le(void *dst, const void *src, size_t length)
787+
{
788+
if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
789+
(void)memcpy(dst, src, length);
790+
} else {
791+
sys_memcpy_swap(dst, src, length);
792+
}
793+
}
794+
795+
/**
796+
* @brief Put a buffer as big-endian to arbitrary location.
797+
*
798+
* Put a buffer originally in host endianness, to a
799+
* potentially unaligned memory location in big-endian format.
800+
*
801+
* @param dst A valid pointer on a memory area where to copy the data in
802+
* @param src A valid pointer on a memory area where to copy the data from
803+
* @param length Size of both dst and src memory areas
804+
*/
805+
static inline void sys_put_be(void *dst, const void *src, size_t length)
806+
{
807+
if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
808+
sys_memcpy_swap(dst, src, length);
809+
} else {
810+
(void)memcpy(dst, src, length);
811+
}
812+
}
813+
814+
/**
815+
* @brief Get a buffer stored in little-endian format.
816+
*
817+
* Get a buffer, stored in little-endian format in a potentially
818+
* unaligned memory location, and convert it to the host endianness.
819+
*
820+
* @param dst A valid pointer on a memory area where to copy the data in
821+
* @param src A valid pointer on a memory area where to copy the data from
822+
* @param length Size of both dst and src memory areas
823+
*/
824+
static inline void sys_get_le(void *dst, const void *src, size_t length)
825+
{
826+
if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
827+
(void)memcpy(dst, src, length);
828+
} else {
829+
sys_memcpy_swap(dst, src, length);
830+
}
831+
}
832+
833+
/**
834+
* @brief Get a buffer stored in big-endian format.
835+
*
836+
* Get a buffer, stored in big-endian format in a potentially
837+
* unaligned memory location, and convert it to the host endianness.
838+
*
839+
* @param dst A valid pointer on a memory area where to copy the data in
840+
* @param src A valid pointer on a memory area where to copy the data from
841+
* @param length Size of both dst and src memory areas
842+
*/
843+
static inline void sys_get_be(void *dst, const void *src, size_t length)
844+
{
845+
if (IS_ENABLED(CONFIG_LITTLE_ENDIAN)) {
846+
sys_memcpy_swap(dst, src, length);
847+
} else {
848+
(void)memcpy(dst, src, length);
849+
}
850+
}
851+
722852
#endif /* ZEPHYR_INCLUDE_SYS_BYTEORDER_H_ */

0 commit comments

Comments
 (0)