nrfxlib API 3.3.99
Loading...
Searching...
No Matches
nrf_802154_utils_byteorder.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35#include <stdint.h>
36#include <string.h>
37#include "nrf_802154_assert.h"
38
39#ifndef NRF_802154_UTILS_BYTEORDER_H
40#define NRF_802154_UTILS_BYTEORDER_H
41
49#if !defined(__BYTE_ORDER__)
50
51/* Couldn't determine endian-ness of the target machine. */
52#error "Please define __BYTE_ORDER__!"
53
54#elif defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
55
61static inline void host_64_to_little(uint64_t value, uint8_t * p_buffer)
62{
63 NRF_802154_ASSERT(p_buffer != NULL);
64 memcpy(p_buffer, &value, sizeof(uint64_t));
65}
66
72static inline void host_32_to_little(uint32_t value, uint8_t * p_buffer)
73{
74 NRF_802154_ASSERT(p_buffer != NULL);
75 memcpy(p_buffer, &value, sizeof(uint32_t));
76}
77
83static inline void host_24_to_little(uint32_t value, uint8_t * p_buffer)
84{
85 NRF_802154_ASSERT(p_buffer != NULL);
86 memcpy(p_buffer, &value, 3);
87}
88
94static inline void host_16_to_little(uint16_t value, uint8_t * p_buffer)
95{
96 NRF_802154_ASSERT(p_buffer != NULL);
97 memcpy(p_buffer, &value, sizeof(uint16_t));
98}
99
106static inline uint64_t little_64_to_host(uint8_t * p_buffer)
107{
108 NRF_802154_ASSERT(p_buffer != NULL);
109
110 uint64_t value = 0;
111
112 memcpy(&value, p_buffer, sizeof(uint64_t));
113
114 return value;
115}
116
123static inline uint32_t little_32_to_host(uint8_t * p_buffer)
124{
125 NRF_802154_ASSERT(p_buffer != NULL);
126
127 uint32_t value = 0;
128
129 memcpy(&value, p_buffer, sizeof(uint32_t));
130
131 return value;
132}
133
140static inline uint32_t little_24_to_host(uint8_t * p_buffer)
141{
142 NRF_802154_ASSERT(p_buffer != NULL);
143
144 uint32_t value = 0;
145
146 memcpy(&value, p_buffer, 3);
147
148 return value;
149}
150
157static inline uint16_t little_16_to_host(uint8_t * p_buffer)
158{
159 NRF_802154_ASSERT(p_buffer != NULL);
160
161 uint16_t value = 0;
162
163 memcpy(&value, p_buffer, sizeof(uint16_t));
164
165 return value;
166}
167
173static inline void host_64_to_big(uint64_t value, uint8_t * p_buffer)
174{
175 NRF_802154_ASSERT(p_buffer != NULL);
176
177 uint8_t shift = (sizeof(uint64_t) - 1) * 8;
178
179 for (uint8_t i = 0; i < sizeof(uint64_t); i++, shift -= 8)
180 {
181 p_buffer[i] = (value >> shift) & 0xff;
182 }
183}
184
190static inline void host_32_to_big(uint32_t value, uint8_t * p_buffer)
191{
192 NRF_802154_ASSERT(p_buffer != NULL);
193
194 uint8_t shift = (sizeof(uint32_t) - 1) * 8;
195
196 for (uint8_t i = 0; i < sizeof(uint32_t); i++, shift -= 8)
197 {
198 p_buffer[i] = (value >> shift) & 0xff;
199 }
200}
201
207static inline void host_24_to_big(uint32_t value, uint8_t * p_buffer)
208{
209 NRF_802154_ASSERT(p_buffer != NULL);
210
211 uint8_t shift = (sizeof(uint32_t) - 2) * 8;
212
213 for (uint8_t i = 0; i < sizeof(uint32_t) - 1; i++, shift -= 8)
214 {
215 p_buffer[i] = (value >> shift) & 0xff;
216 }
217}
218
224static inline void host_16_to_big(uint16_t value, uint8_t * p_buffer)
225{
226 NRF_802154_ASSERT(p_buffer != NULL);
227
228 uint8_t shift = (sizeof(uint16_t) - 1) * 8;
229
230 for (uint8_t i = 0; i < sizeof(uint16_t); i++, shift -= 8)
231 {
232 p_buffer[i] = (value >> shift) & 0xff;
233 }
234}
235
242static inline uint64_t big_64_to_host(uint8_t * p_buffer)
243{
244 NRF_802154_ASSERT(p_buffer != NULL);
245
246 uint64_t value = 0;
247
248 for (uint8_t i = 0; i < sizeof(uint64_t); i++)
249 {
250 value = (value << 8) | p_buffer[i];
251 }
252
253 return value;
254}
255
262static inline uint32_t big_32_to_host(uint8_t * p_buffer)
263{
264 NRF_802154_ASSERT(p_buffer != NULL);
265
266 uint32_t value = 0;
267
268 for (uint8_t i = 0; i < sizeof(uint32_t); i++)
269 {
270 value = (value << 8) | p_buffer[i];
271 }
272
273 return value;
274}
275
282static inline uint32_t big_24_to_host(uint8_t * p_buffer)
283{
284 NRF_802154_ASSERT(p_buffer != NULL);
285
286 uint32_t value = 0;
287
288 for (uint8_t i = 0; i < sizeof(uint32_t) - 1; i++)
289 {
290 value = (value << 8) | p_buffer[i];
291 }
292
293 return value;
294}
295
302static inline uint16_t big_16_to_host(uint8_t * p_buffer)
303{
304 NRF_802154_ASSERT(p_buffer != NULL);
305
306 uint16_t value = 0;
307
308 for (uint8_t i = 0; i < sizeof(uint16_t); i++)
309 {
310 value = (value << 8) | p_buffer[i];
311 }
312
313 return value;
314}
315
316#else
317
318/* Most likely the case of big-endian machine. */
319#error "Unsupported endian-ness of the target machine"
320
321#endif // __BYTE_ORDER__
322
327#endif // NRF_802154_UTILS_BYTEORDER_H
#define NRF_802154_ASSERT(condition)
Definition nrf_802154_assert.h:45
#define NULL
Definition osal_types.h:36