NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
endian.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef H_ENDIAN_
21#define H_ENDIAN_
22
23#include <inttypes.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/* Internal helpers */
30#ifndef os_bswap_64
31#define os_bswap_64(x) ((uint64_t) \
32 ((((x) & 0xff00000000000000ull) >> 56) | \
33 (((x) & 0x00ff000000000000ull) >> 40) | \
34 (((x) & 0x0000ff0000000000ull) >> 24) | \
35 (((x) & 0x000000ff00000000ull) >> 8) | \
36 (((x) & 0x00000000ff000000ull) << 8) | \
37 (((x) & 0x0000000000ff0000ull) << 24) | \
38 (((x) & 0x000000000000ff00ull) << 40) | \
39 (((x) & 0x00000000000000ffull) << 56)))
40#endif
41
42#ifndef os_bswap_32
43#define os_bswap_32(x) ((uint32_t) \
44 ((((x) & 0xff000000) >> 24) | \
45 (((x) & 0x00ff0000) >> 8) | \
46 (((x) & 0x0000ff00) << 8) | \
47 (((x) & 0x000000ff) << 24)))
48#endif
49
50#ifndef os_bswap_16
51#define os_bswap_16(x) ((uint16_t) \
52 ((((x) & 0xff00) >> 8) | \
53 (((x) & 0x00ff) << 8)))
54#endif
55
56#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
57
58#ifndef ntohll
59#define ntohll(x) ((uint64_t)(x))
60#endif
61
62#ifndef htonll
63#define htonll(x) ((uint64_t)(x))
64#endif
65
66#ifndef ntohl
67#define ntohl(x) ((uint32_t)(x))
68#endif
69
70#ifndef htonl
71#define htonl(x) ((uint32_t)(x))
72#endif
73
74#ifndef ntohs
75#define ntohs(x) ((uint16_t)(x))
76#endif
77
78#ifndef htons
79#define htons(x) ((uint16_t)(x))
80#endif
81
82#ifndef htobe16
83#define htobe16(x) ((uint16_t)(x))
84#endif
85
86#ifndef htole16
87#define htole16(x) os_bswap_16 (x)
88#endif
89
90#ifndef be16toh
91#define be16toh(x) ((uint16_t)(x))
92#endif
93
94#ifndef le16toh
95#define le16toh(x) os_bswap_16 (x)
96#endif
97
98#ifndef htobe32
99#define htobe32(x) ((uint32_t)(x))
100#endif
101
102#ifndef htole32
103#define htole32(x) os_bswap_32 (x)
104#endif
105
106#ifndef be32toh
107#define be32toh(x) ((uint32_t)(x))
108#endif
109
110#ifndef le32toh
111#define le32toh(x) os_bswap_32 (x)
112#endif
113
114#ifndef htobe64
115#define htobe64(x) ((uint64_t)(x))
116#endif
117
118#ifndef htole64
119#define htole64(x) os_bswap_64 (x)
120#endif
121
122#ifndef be64toh
123#define be64toh(x) ((uint64_t)(x))
124#endif
125
126#ifndef le64toh
127#define le64toh(x) os_bswap_64 (x)
128#endif
129
130#else
131
132#ifndef ntohll
133#define ntohll(x) os_bswap_64(x)
134#endif
135
136#ifndef htonll
137#define htonll ntohll
138#endif
139
140/* These are not used in NimBLE and ESP-IDF uses them from LwIP */
141#if 0
142#ifndef ntohl
143#define ntohl(x) os_bswap_32(x)
144#endif
145
146#ifndef htonl
147#define htonl ntohl
148#endif
149
150#ifndef htons
151#define htons(x) os_bswap_16(x)
152#endif
153
154#ifndef ntohs
155#define ntohs htons
156#endif
157#endif
158
159#ifndef htobe16
160#define htobe16(x) os_bswap_16(x)
161#endif
162
163#ifndef htole16
164#define htole16(x) ((uint16_t)(x))
165#endif
166
167#ifndef be16toh
168#define be16toh(x) os_bswap_16(x)
169#endif
170
171#ifndef le16toh
172#define le16toh(x) ((uint16_t)(x))
173#endif
174
175#ifndef htobe32
176#define htobe32(x) os_bswap_32(x)
177#endif
178
179#ifndef htole32
180#define htole32(x) ((uint32_t)(x))
181#endif
182
183#ifndef be32toh
184#define be32toh(x) os_bswap_32(x)
185#endif
186
187#ifndef le32toh
188#define le32toh(x) ((uint32_t)(x))
189#endif
190
191#ifndef htobe64
192#define htobe64(x) os_bswap_64(x)
193#endif
194
195#ifndef htole64
196#define htole64(x) ((uint64_t)(x))
197#endif
198
199#ifndef be64toh
200#define be64toh(x) os_bswap_64(x)
201#endif
202
203#ifndef le64toh
204#define le64toh(x) ((uint64_t)(x))
205#endif
206
207#endif
208
209#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
210void r_put_le16(void *buf, uint16_t x);
211#define put_le16 r_put_le16
212
213void r_put_le24(void *buf, uint32_t x);
214#define put_le24 r_put_le24
215
216void r_put_le32(void *buf, uint32_t x);
217#define put_le32 r_put_le32
218
219void r_put_le64(void *buf, uint64_t x);
220#define put_le64 r_put_le64
221
222uint16_t r_get_le16(const void *buf);
223#define get_le16 r_get_le16
224
225uint32_t r_get_le24(const void *buf);
226#define get_le24 r_get_le24
227
228uint32_t r_get_le32(const void *buf);
229#define get_le32 r_get_le32
230
231uint64_t r_get_le64(const void *buf);
232#define get_le64 r_get_le64
233
234void r_put_be16(void *buf, uint16_t x);
235#define put_be16 r_put_be16
236
237void r_put_be24(void *buf, uint32_t x);
238#define put_be24 r_put_be24
239
240void r_put_be32(void *buf, uint32_t x);
241#define put_be32 r_put_be32
242
243void r_put_be64(void *buf, uint64_t x);
244#define put_be64 r_put_be64
245
246uint16_t r_get_be16(const void *buf);
247#define get_be16 r_get_be16
248
249uint32_t r_get_be24(const void *buf);
250#define get_be24 r_get_be24
251
252uint32_t r_get_be32(const void *buf);
253#define get_be32 r_get_be32
254
255uint64_t r_get_be64(const void *buf);
256#define get_be64 r_get_be64
257
258void r_swap_in_place(void *buf, int len);
259#define swap_in_place r_swap_in_place
260
261void r_swap_buf(uint8_t *dst, const uint8_t *src, int len);
262#define swap_buf r_swap_buf
263
264
265#else
266void put_le16(void *buf, uint16_t x);
267void put_le24(void *buf, uint32_t x);
268void put_le32(void *buf, uint32_t x);
269void put_le64(void *buf, uint64_t x);
270uint16_t get_le16(const void *buf);
271uint32_t get_le24(const void *buf);
272uint32_t get_le32(const void *buf);
273uint64_t get_le64(const void *buf);
274void put_be16(void *buf, uint16_t x);
275void put_be24(void *buf, uint32_t x);
276void put_be32(void *buf, uint32_t x);
277void put_be64(void *buf, uint64_t x);
278uint16_t get_be16(const void *buf);
279uint32_t get_be24(const void *buf);
280uint32_t get_be32(const void *buf);
281uint64_t get_be64(const void *buf);
282void swap_in_place(void *buf, int len);
283void swap_buf(uint8_t *dst, const uint8_t *src, int len);
284#endif
285#ifdef __cplusplus
286}
287#endif
288
289#endif