1 /** @file
2  *  @brief Bluetooth byteorder API
3  */
4 
5 /*
6  * Copyright (c) 2023 Nordic Semiconductor ASA
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_BYTEORDER_H_
12 #define ZEPHYR_INCLUDE_BLUETOOTH_BYTEORDER_H_
13 
14 /**
15  * @brief Byteorder
16  * @defgroup bt_byteorder Byteorder
17  * @ingroup bluetooth
18  * @{
19  */
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 
26 /** @brief Encode 16-bit value into array values in little-endian format.
27  *
28  *  Helper macro to encode 16-bit values into comma separated values.
29  *
30  *  @note @p _v is evaluated 2 times.
31  *
32  *  @param _v 16-bit integer in host endianness.
33  *
34  *  @return The comma separated values for the 16-bit value.
35  */
36 #define BT_BYTES_LIST_LE16(_v)         \
37 	(((_v) >>  0) & 0xFFU),     \
38 	(((_v) >>  8) & 0xFFU)      \
39 
40 /** @brief Encode 24-bit value into array values in little-endian format.
41  *
42  *  Helper macro to encode 24-bit values into comma separated values.
43  *
44  *  @note @p _v is evaluated 3 times.
45  *
46  *  @param _v 24-bit integer in host endianness.
47  *
48  *  @return The comma separated values for the 24-bit value.
49  */
50 #define BT_BYTES_LIST_LE24(_v)         \
51 	BT_BYTES_LIST_LE16(_v),        \
52 	(((_v) >> 16) & 0xFFU)      \
53 
54 /** @brief Encode 32-bit value into array values in little-endian format.
55  *
56  *  Helper macro to encode 32-bit values into comma separated values.
57  *
58  *  @note @p _v is evaluated 4 times.
59  *
60  *  @param _v 32-bit integer in host endianness.
61  *
62  *  @return The comma separated values for the 32-bit value.
63  */
64 #define BT_BYTES_LIST_LE32(_v)         \
65 	BT_BYTES_LIST_LE24(_v),        \
66 	(((_v) >> 24) & 0xFFU)      \
67 
68 /** @brief Encode 40-bit value into array values in little-endian format.
69  *
70  *  Helper macro to encode 40-bit values into comma separated values.
71  *
72  *  @note @p _v is evaluated 5 times.
73  *
74  *  @param _v 40-bit integer in host endianness.
75  *
76  *  @return The comma separated values for the 40-bit value.
77  */
78 #define BT_BYTES_LIST_LE40(_v)         \
79 	BT_BYTES_LIST_LE24(_v),        \
80 	BT_BYTES_LIST_LE16((_v) >> 24) \
81 
82 /** @brief Encode 48-bit value into array values in little-endian format.
83  *
84  *  Helper macro to encode 48-bit values into comma separated values.
85  *
86  *  @note @p _v is evaluated 6 times.
87  *
88  *  @param _v 48-bit integer in host endianness.
89  *
90  *  @return The comma separated values for the 48-bit value.
91  */
92 #define BT_BYTES_LIST_LE48(_v)         \
93 	BT_BYTES_LIST_LE32(_v),        \
94 	BT_BYTES_LIST_LE16((_v) >> 32) \
95 
96 /** @brief Encode 64-bit value into array values in little-endian format.
97  *
98  *  Helper macro to encode 64-bit values into comma separated values.
99  *
100  *  @note @p _v is evaluated 8 times.
101  *
102  *  @param _v 64-bit integer in host endianness.
103  *
104  *  @return The comma separated values for the 64-bit value.
105  */
106 #define BT_BYTES_LIST_LE64(_v)         \
107 	BT_BYTES_LIST_LE32(_v),        \
108 	BT_BYTES_LIST_LE32((_v) >> 32) \
109 
110 /** @brief Encode 16-bit value into array values in big-endian format.
111  *
112  *  Helper macro to encode 16-bit values into comma separated values.
113  *
114  *  @note @p _v is evaluated 2 times.
115  *
116  *  @param _v 16-bit integer in host endianness.
117  *
118  *  @return The comma separated values for the 16-bit value.
119  */
120 #define BT_BYTES_LIST_BE16(_v) (((_v) >> 8) & 0xFFU), (((_v) >> 0) & 0xFFU)
121 
122 /** @brief Encode 24-bit value into array values in big-endian format.
123  *
124  *  Helper macro to encode 24-bit values into comma separated values.
125  *
126  *  @note @p _v is evaluated 3 times.
127  *
128  *  @param _v 24-bit integer in host endianness.
129  *
130  *  @return The comma separated values for the 24-bit value.
131  */
132 #define BT_BYTES_LIST_BE24(_v) (((_v) >> 16) & 0xFFU), BT_BYTES_LIST_BE16(_v)
133 
134 /** @brief Encode 32-bit value into array values in big-endian format.
135  *
136  *  Helper macro to encode 32-bit values into comma separated values.
137  *
138  *  @note @p _v is evaluated 4 times.
139  *
140  *  @param _v 32-bit integer in host endianness.
141  *
142  *  @return The comma separated values for the 32-bit value.
143  */
144 #define BT_BYTES_LIST_BE32(_v) (((_v) >> 24) & 0xFFU), BT_BYTES_LIST_BE24(_v)
145 
146 /** @brief Encode 40-bit value into array values in big-endian format.
147  *
148  *  Helper macro to encode 40-bit values into comma separated values.
149  *
150  *  @note @p _v is evaluated 5 times.
151  *
152  *  @param _v 40-bit integer in host endianness.
153  *
154  *  @return The comma separated values for the 40-bit value.
155  */
156 #define BT_BYTES_LIST_BE40(_v) BT_BYTES_LIST_BE16((_v) >> 24), BT_BYTES_LIST_BE24(_v)
157 
158 /** @brief Encode 48-bit value into array values in big-endian format.
159  *
160  *  Helper macro to encode 48-bit values into comma separated values.
161  *
162  *  @note @p _v is evaluated 6 times.
163  *
164  *  @param _v 48-bit integer in host endianness.
165  *
166  *  @return The comma separated values for the 48-bit value.
167  */
168 #define BT_BYTES_LIST_BE48(_v) BT_BYTES_LIST_BE16((_v) >> 32), BT_BYTES_LIST_BE32(_v)
169 
170 /** @brief Encode 64-bit value into array values in big-endian format.
171  *
172  *  Helper macro to encode 64-bit values into comma separated values.
173  *
174  *  @note @p _v is evaluated 8 times.
175  *
176  *  @param _v 64-bit integer in host endianness.
177  *
178  *  @return The comma separated values for the 64-bit value.
179  */
180 #define BT_BYTES_LIST_BE64(_v) BT_BYTES_LIST_BE32((_v) >> 32), BT_BYTES_LIST_BE32(_v)
181 
182 /**
183  * @}
184  */
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
190 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_BYTEORDER_H_ */
191