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 /**
111  * @}
112  */
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_BYTEORDER_H_ */
119