Mistake on this page?
Report an issue in GitHub or email us
bstream.h
Go to the documentation of this file.
1 /*************************************************************************************************/
2 /*!
3  * \file bstream.h
4  *
5  * \brief Byte stream to integer conversion macros.
6  *
7  * Copyright (c) 2009-2018 Arm Ltd. All Rights Reserved.
8  *
9  * Copyright (c) 2019-2020 Packetcraft, Inc.
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 /*************************************************************************************************/
24 #ifndef BSTREAM_H
25 #define BSTREAM_H
26 
27 #include "util/bda.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /*! \addtogroup WSF_UTIL_API
34  * \{ */
35 
36 /**************************************************************************************************
37  Macros
38 **************************************************************************************************/
39 
40 /**
41  * \name Macros for converting a little endian byte buffer to integers.
42  */
43 /**@{*/
44 /*! \brief convert little endian byte buffer to int16_t. */
45 #define BYTES_TO_INT16(n, p) {n = ((int16_t)(p)[0] + ((int16_t)(p)[1] << 8));}
46 /*! \brief convert little endian byte buffer to uint16_t. */
47 #define BYTES_TO_UINT16(n, p) {n = ((uint16_t)(p)[0] + ((uint16_t)(p)[1] << 8));}
48 /*! \brief convert little endian byte buffer to uint24_t. */
49 #define BYTES_TO_UINT24(n, p) {n = ((uint16_t)(p)[0] + ((uint16_t)(p)[1] << 8) + \
50  ((uint16_t)(p)[2] << 16));}
51 /*! \brief convert little endian byte buffer to uint32_t. */
52 #define BYTES_TO_UINT32(n, p) {n = ((uint32_t)(p)[0] + ((uint32_t)(p)[1] << 8) + \
53  ((uint32_t)(p)[2] << 16) + ((uint32_t)(p)[3] << 24));}
54 /*! \brief convert little endian byte buffer to uint40_t. */
55 #define BYTES_TO_UINT40(n, p) {n = ((uint64_t)(p)[0] + ((uint64_t)(p)[1] << 8) + \
56  ((uint64_t)(p)[2] << 16) + ((uint64_t)(p)[3] << 24) + \
57  ((uint64_t)(p)[4] << 32));}
58 /*! \brief convert little endian byte buffer to uint64_t. */
59 #define BYTES_TO_UINT64(n, p) {n = ((uint64_t)(p)[0] + ((uint64_t)(p)[1] << 8) + \
60  ((uint64_t)(p)[2] << 16) + ((uint64_t)(p)[3] << 24) + \
61  ((uint64_t)(p)[4] << 32) + ((uint64_t)(p)[5] << 40) + \
62  ((uint64_t)(p)[6] << 48) + ((uint64_t)(p)[7] << 56));}
63 /**@}*/
64 
65 /**
66  * \name Macros for converting a big endian byte buffer to integers.
67  */
68 /**@{*/
69 /*! \brief convert big endian byte buffer to uint16_t. */
70 #define BYTES_BE_TO_UINT16(n, p) {n = ((uint16_t)(p)[1] + ((uint16_t)(p)[0] << 8));}
71 /*! \brief convert big endian byte buffer to 24-bit uint32_t (MSB 0). */
72 #define BYTES_BE_TO_UINT24(n, p) {n = ((uint16_t)(p)[2] + ((uint16_t)(p)[1] << 8) + \
73  ((uint16_t)(p)[0] << 16));}
74 /*! \brief convert big endian byte buffer to uint32_t. */
75 #define BYTES_BE_TO_UINT32(n, p) {n = ((uint32_t)(p)[3] + ((uint32_t)(p)[2] << 8) + \
76  ((uint32_t)(p)[1] << 16) + ((uint32_t)(p)[0] << 24));}
77 /**@}*/
78 
79 /**
80  * \name Macros for converting little endian integers to array of bytes
81  */
82 /**@{*/
83 /*! \brief convert little endian uint16_t to array of bytes. */
84 #define UINT16_TO_BYTES(n) ((uint8_t) (n)), ((uint8_t)((n) >> 8))
85 /*! \brief convert little endian uint32_t to array of bytes. */
86 #define UINT32_TO_BYTES(n) ((uint8_t) (n)), ((uint8_t)((n) >> 8)), ((uint8_t)((n) >> 16)), ((uint8_t)((n) >> 24))
87 /**@}*/
88 
89 /**
90  * \name Macros for converting big endian integers to array of bytes
91  */
92 /**@{*/
93 /*! \brief convert big endian uint16_t to array of bytes. */
94 #define UINT16_TO_BE_BYTES(n) ((uint8_t)((n) >> 8)), ((uint8_t) (n))
95 /*! \brief convert 24-bit big endian uint32_t (MSB 0) to array of bytes. */
96 #define UINT24_TO_BE_BYTES(n) ((uint8_t)((n) >> 16)), ((uint8_t)((n) >> 8)), ((uint8_t) (n))
97 /*! \brief convert big endian uint32_t to array of bytes. */
98 #define UINT32_TO_BE_BYTES(n) ((uint8_t)((n) >> 24)), ((uint8_t)((n) >> 16)), ((uint8_t)((n) >> 8)), ((uint8_t) (n))
99 /**@}*/
100 
101 /**
102  * \name Macros for converting little endian integers to single bytes
103  */
104 /**@{*/
105 /*! \brief convert little endian uint16_t to byte 0. */
106 #define UINT16_TO_BYTE0(n) ((uint8_t) (n))
107 /*! \brief convert little endian uint16_t to byte 1. */
108 #define UINT16_TO_BYTE1(n) ((uint8_t) ((n) >> 8))
109 
110 /*! \brief convert little endian uint32_t to byte 0. */
111 #define UINT32_TO_BYTE0(n) ((uint8_t) (n))
112 /*! \brief convert little endian uint32_t to byte 1. */
113 #define UINT32_TO_BYTE1(n) ((uint8_t) ((n) >> 8))
114 /*! \brief convert little endian uint32_t to byte 2. */
115 #define UINT32_TO_BYTE2(n) ((uint8_t) ((n) >> 16))
116 /*! \brief convert little endian uint32_t to byte 3. */
117 #define UINT32_TO_BYTE3(n) ((uint8_t) ((n) >> 24))
118 /**@}*/
119 
120 /**
121  * \name Macros for converting a little endian byte stream to integers, with increment.
122  */
123 /**@{*/
124 /*! \brief convert little endian byte stream to uint8_t, incrementing one byte. */
125 #define BSTREAM_TO_INT8(n, p) {n = (int8_t)(*(p)++);}
126 /*! \brief convert little endian byte stream to int8_t, incrementing one byte. */
127 #define BSTREAM_TO_UINT8(n, p) {n = (uint8_t)(*(p)++);}
128 /*! \brief convert little endian byte stream to int16_t, incrementing two bytes. */
129 #define BSTREAM_TO_INT16(n, p) {BYTES_TO_INT16(n, p); p += 2;}
130 /*! \brief convert little endian byte stream to uint16_t, incrementing two bytes. */
131 #define BSTREAM_TO_UINT16(n, p) {BYTES_TO_UINT16(n, p); p += 2;}
132 /*! \brief convert little endian byte stream to uint24_t, incrementing three bytes. */
133 #define BSTREAM_TO_UINT24(n, p) {BYTES_TO_UINT24(n, p); p += 3;}
134 /*! \brief convert little endian byte stream to uint32_t, incrementing four bytes. */
135 #define BSTREAM_TO_UINT32(n, p) {BYTES_TO_UINT32(n, p); p += 4;}
136 /*! \brief convert little endian byte stream to uint40_t, incrementing five bytes. */
137 #define BSTREAM_TO_UINT40(n, p) {BYTES_TO_UINT40(n, p); p += 5;}
138 /*! \brief convert little endian byte stream to uint64_t, incrementing eigth bytes. */
139 #define BSTREAM_TO_UINT64(n, p) {n = BstreamToUint64(p); p += 8;}
140 /*! \brief convert little endian byte stream to six byte Bluetooth device address, incrementing six bytes. */
141 #define BSTREAM_TO_BDA(bda, p) {BdaCpy(bda, p); p += BDA_ADDR_LEN;}
142 /*! \brief convert little endian byte stream to eight byte Bluetooth device address, incrementing eight bytes. */
143 #define BSTREAM_TO_BDA64(bda, p) {bda = BstreamToBda64(p); p += BDA_ADDR_LEN;}
144 /**@}*/
145 
146 /**
147  * \name Macros for converting a big endian byte stream to integers, with increment.
148  */
149 /**@{*/
150 /*! \brief convert big endian byte stream to uint16_t, incrementing one byte. */
151 #define BSTREAM_BE_TO_UINT16(n, p) {BYTES_BE_TO_UINT16(n, p); p += 2;}
152 /*! \brief convert big endian byte stream to 24-bit uint32_t (MSB 0), incrementing one byte. */
153 #define BSTREAM_BE_TO_UINT24(n, p) {BYTES_BE_TO_UINT24(n, p); p += 3;}
154 /**@}*/
155 
156 /**
157  * \name Macros for converting integers to a little endian byte stream, with increment.
158  */
159 /**@{*/
160 /*! \brief convert uint8_t to little endian byte stream, incrementing one byte. */
161 #define UINT8_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n);}
162 /*! \brief convert uint16_t to little endian byte stream, incrementing two bytes. */
163 #define UINT16_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8);}
164 /*! \brief convert uint24_t to little endian byte stream, incrementing three bytes. */
165 #define UINT24_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
166  *(p)++ = (uint8_t)((n) >> 16);}
167 /*! \brief convert uint32_t to little endian byte stream, incrementing four bytes. */
168 #define UINT32_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
169  *(p)++ = (uint8_t)((n) >> 16); *(p)++ = (uint8_t)((n) >> 24);}
170 /*! \brief convert uint40_t to little endian byte stream, incrementing five bytes. */
171 #define UINT40_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
172  *(p)++ = (uint8_t)((n) >> 16); *(p)++ = (uint8_t)((n) >> 24); \
173  *(p)++ = (uint8_t)((n) >> 32);}
174 /*! \brief convert uint64_t to little endian byte stream, incrementing eight bytes. */
175 #define UINT64_TO_BSTREAM(p, n) {Uint64ToBstream(p, n); p += sizeof(uint64_t);}
176 /*! \brief convert six byte Bluetooth device address to little endian byte stream, incrementing six bytes. */
177 #define BDA_TO_BSTREAM(p, bda) {BdaCpy(p, bda); p += BDA_ADDR_LEN;}
178 /*! \brief convert eight byte Bluetooth device address to little endian byte stream, incrementing eigth bytes. */
179 #define BDA64_TO_BSTREAM(p, bda) {Bda64ToBstream(p, bda); p += BDA_ADDR_LEN;}
180 /**@}*/
181 
182 /**
183  * \name Macros for converting integers to a big endian byte stream, with increment.
184  */
185 /**@{*/
186 /*! \brief convert uint16_t to big endian byte stream, incrementing one byte. */
187 #define UINT16_TO_BE_BSTREAM(p, n) {*(p)++ = (uint8_t)((n) >> 8); *(p)++ = (uint8_t)(n);}
188 /*! \brief convert uint32_t to big endian byte stream, incrementing one byte. */
189 #define UINT32_TO_BE_BSTREAM(p, n) {*(p)++ = (uint8_t)((n) >> 24); *(p)++ = (uint8_t)((n) >> 16);\
190  *(p)++ = (uint8_t)((n) >> 8); *(p)++ = (uint8_t)(n);}
191 /**@}*/
192 
193 /**
194  * \name Macros for converting integers to a little endian byte stream, without increment.
195  */
196 /**@{*/
197 /*! \brief convert uint16_t to little endian byte stream. */
198 #define UINT16_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8);}
199 /*! \brief convert uint24_t to little endian byte stream. */
200 #define UINT24_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
201  (p)[2] = (uint8_t)((n) >> 16);}
202 /*! \brief convert uint32_t to little endian byte stream. */
203 #define UINT32_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
204  (p)[2] = (uint8_t)((n) >> 16); (p)[3] = (uint8_t)((n) >> 24);}
205 /*! \brief convert uint40_t to little endian byte stream. */
206 #define UINT40_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
207  (p)[2] = (uint8_t)((n) >> 16); (p)[3] = (uint8_t)((n) >> 24);\
208  (p)[4] = (uint8_t)((n) >> 32);}
209 /**@}*/
210 
211 /**
212  * \name Macros for converting integers to a big endian byte stream, without increment.
213  */
214 /**@{*/
215 /*! \brief convert uint16_t to big endian byte stream. */
216 #define UINT16_TO_BE_BUF(p, n) {(p)[0] = (uint8_t)((n) >> 8); (p)[1] = (uint8_t)(n);}
217 /*! \brief convert 24-bit uint32_t (MSB 0) to big endian byte stream. */
218 #define UINT24_TO_BE_BUF(p, n) {(p)[0] = (uint8_t)((n) >> 16); (p)[1] = (uint8_t)((n) >> 8); \
219  (p)[2] = (uint8_t)(n);}
220 /*! \brief convert uint32_t to big endian byte stream. */
221 #define UINT32_TO_BE_BUF(p, n) {(p)[0] = (uint8_t)((n) >> 24); (p)[1] = (uint8_t)((n) >> 16); \
222  (p)[2] = (uint8_t)((n) >> 8); (p)[3] = (uint8_t)(n);}
223 /**@}*/
224 
225 /**
226  * \name Macros for comparing a little endian byte buffer to integers.
227  */
228 /**@{*/
229 /*! \brief compare 2 byte little endian buffer with a uint16_t. */
230 #define BYTES_UINT16_CMP(p, n) ((p)[1] == UINT16_TO_BYTE1(n) && (p)[0] == UINT16_TO_BYTE0(n))
231 /**@}*/
232 
233 /**
234  * \name Macros for IEEE FLOAT type: exponent = byte 3, mantissa = bytes 2-0
235  */
236 /**@{*/
237 /*! \brief Convert float to uint32. */
238 #define FLT_TO_UINT32(m, e) ((m) | ((int32_t)(e) * 16777216))
239 /*! \brief Convert uint32_t to float. */
240 #define UINT32_TO_FLT(m, e, n) {m = UINT32_TO_FLT_M(n); e = UINT32_TO_FLT_E(n);}
241 /*! \brief Convert uint32_t to float mantissa component */
242 #define UINT32_TO_FLT_M(n) ((((n) & 0x00FFFFFF) >= 0x00800000) ? \
243  ((int32_t)(((n) | 0xFF000000))) : ((int32_t)((n) & 0x00FFFFFF)))
244 /*! \brief Convert uint32_t to float exponent component. */
245 #define UINT32_TO_FLT_E(n) ((int8_t)(n >> 24))
246 /**@}*/
247 
248 /**
249  * \name Macros for IEEE SFLOAT type: exponent = bits 15-12, mantissa = bits 11-0
250  */
251 /**@{*/
252 /*! \brief Convert sfloat to uint16_t */
253 #define SFLT_TO_UINT16(m, e) ((m) | (0xF000 & ((int16_t)(e) * 4096)))
254 /*! \brief Convert uint16_t to sfloat */
255 #define UINT16_TO_SFLT(m, e, n) {m = UINT16_TO_SFLT_M(n); e = UINT16_TO_SFLT_E(n);}
256 /*! \brief Convert uint16_T to sfloat mantissa component */
257 #define UINT16_TO_SFLT_M(n) ((((n) & 0x0FFF) >= 0x0800) ? \
258  ((int16_t)(((n) | 0xF000))) : ((int16_t)((n) & 0x0FFF)))
259 /*! \brief Convert uint16_T to sfloat exponent component */
260 #define UINT16_TO_SFLT_E(n) (((n >> 12) >= 0x0008) ? \
261  ((int8_t)(((n >> 12) | 0xF0))) : ((int8_t)(n >> 12)))
262 /**@}*/
263 
264 /**************************************************************************************************
265  Function Declarations
266 **************************************************************************************************/
267 
268 /*************************************************************************************************/
269 /*!
270  * \brief Convert bstream to BDA64.
271  *
272  * \param p Bstream pointer.
273  *
274  * \return Resulting BDA64 number.
275  */
276 /*************************************************************************************************/
277 uint64_t BstreamToBda64(const uint8_t *p);
278 
279 /*************************************************************************************************/
280 /*!
281  * \brief Convert bstream to uint64_t.
282  *
283  * \param p Bstream pointer.
284  *
285  * \return Resulting uint64_t number.
286  */
287 /*************************************************************************************************/
288 uint64_t BstreamToUint64(const uint8_t *p);
289 
290 /*************************************************************************************************/
291 /*!
292  * \brief Convert BDA64 to bstream.
293  *
294  * \param p Bstream pointer.
295  * \param bda uint64_t BDA.
296  */
297 /*************************************************************************************************/
298 void Bda64ToBstream(uint8_t *p, uint64_t bda);
299 
300 /*************************************************************************************************/
301 /*!
302  * \brief Convert uint64_t to bstream.
303  *
304  * \param p Bstream pointer.
305  * \param n uint64_t number.
306  */
307 /*************************************************************************************************/
308 void Uint64ToBstream(uint8_t *p, uint64_t n);
309 
310 /*! \} */ /* WSF_UTIL_API */
311 
312 #ifdef __cplusplus
313 };
314 #endif
315 
316 #endif /* BSTREAM_H */
void Bda64ToBstream(uint8_t *p, uint64_t bda)
Convert BDA64 to bstream.
void Uint64ToBstream(uint8_t *p, uint64_t n)
Convert uint64_t to bstream.
uint64_t BstreamToUint64(const uint8_t *p)
Convert bstream to uint64_t.
Bluetooth device address utilities.
uint64_t BstreamToBda64(const uint8_t *p)
Convert bstream to BDA64.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.