openaliro
Aliro reader: UWB/CCC core and ESP32-S3/C5/C6 port
Loading...
Searching...
No Matches
woz_bytes.h
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2026 asxeem
7 * SPDX-License-Identifier: ISC
8 * woz_bytes.h - endian-neutral byte load/store helpers.
9 *
10 * Pure code: no OS, no allocation, no platform calls. It lives here rather than
11 * behind a <zephyr/sys/byteorder.h> compat header because nothing about it is
12 * platform-specific, so every port was re-supplying the same eight inlines.
13 *
14 * The Zephyr spellings (sys_get_le32 etc.) are kept deliberately: on Zephyr we
15 * defer to the real header, so the names have to match, and keeping them means
16 * the call sites need only swap an include.
17 */
18#ifndef WOZ_BYTES_H
19#define WOZ_BYTES_H
20
21#include <stdint.h>
22
23#if defined(__ZEPHYR__)
24
25/* Defer to Zephyr's own definitions. Redefining them here as static inlines
26 * would collide wherever <zephyr/sys/byteorder.h> is pulled in transitively. */
27#include <zephyr/sys/byteorder.h>
28
29#else
30
36static inline uint16_t sys_get_le16(const uint8_t *p)
37{
38 return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8));
39}
40
46static inline uint32_t sys_get_le32(const uint8_t *p)
47{
48 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
49 ((uint32_t)p[3] << 24);
50}
51
57static inline uint16_t sys_get_be16(const uint8_t *p)
58{
59 return (uint16_t)(((uint16_t)p[0] << 8) | (uint16_t)p[1]);
60}
61
67static inline uint32_t sys_get_be32(const uint8_t *p)
68{
69 return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) |
70 (uint32_t)p[3];
71}
72
78static inline void sys_put_le16(uint16_t v, uint8_t *p)
79{
80 p[0] = (uint8_t)v;
81 p[1] = (uint8_t)(v >> 8);
82}
83
89static inline void sys_put_le32(uint32_t v, uint8_t *p)
90{
91 p[0] = (uint8_t)v;
92 p[1] = (uint8_t)(v >> 8);
93 p[2] = (uint8_t)(v >> 16);
94 p[3] = (uint8_t)(v >> 24);
95}
96
102static inline void sys_put_be16(uint16_t v, uint8_t *p)
103{
104 p[0] = (uint8_t)(v >> 8);
105 p[1] = (uint8_t)v;
106}
107
113static inline void sys_put_be32(uint32_t v, uint8_t *p)
114{
115 p[0] = (uint8_t)(v >> 24);
116 p[1] = (uint8_t)(v >> 16);
117 p[2] = (uint8_t)(v >> 8);
118 p[3] = (uint8_t)v;
119}
120
121#endif /* __ZEPHYR__ */
122
123#endif /* WOZ_BYTES_H */
static void sys_put_le16(uint16_t v, uint8_t *p)
Write a 16-bit value to a byte buffer in little-endian order.
Definition woz_bytes.h:78
static void sys_put_be32(uint32_t v, uint8_t *p)
Write a 32-bit value to a byte buffer in big-endian order.
Definition woz_bytes.h:113
static uint16_t sys_get_be16(const uint8_t *p)
Read a 16-bit big-endian value from a byte buffer.
Definition woz_bytes.h:57
static void sys_put_le32(uint32_t v, uint8_t *p)
Write a 32-bit value to a byte buffer in little-endian order.
Definition woz_bytes.h:89
static void sys_put_be16(uint16_t v, uint8_t *p)
Write a 16-bit value to a byte buffer in big-endian order.
Definition woz_bytes.h:102
static uint16_t sys_get_le16(const uint8_t *p)
Read a 16-bit little-endian value from a byte buffer.
Definition woz_bytes.h:36
static uint32_t sys_get_le32(const uint8_t *p)
Read a 32-bit little-endian value from a byte buffer.
Definition woz_bytes.h:46
static uint32_t sys_get_be32(const uint8_t *p)
Read a 32-bit big-endian value from a byte buffer.
Definition woz_bytes.h:67