Cordio Stack and Cordio Profiles  r2p3-02rel0
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  * Arm Ltd. confidential and proprietary.
9  *
10  * IMPORTANT. Your use of this file is governed by a Software License Agreement
11  * ("Agreement") that must be accepted in order to download or otherwise receive a
12  * copy of this file. You may not use or copy this file for any purpose other than
13  * as described in the Agreement. If you do not agree to all of the terms of the
14  * Agreement do not use this file and delete all copies in your possession or control;
15  * if you do not have a copy of the Agreement, you must contact Arm Ltd. prior
16  * to any use, copying or further distribution of this software.
17  */
18 /*************************************************************************************************/
19 #ifndef BSTREAM_H
20 #define BSTREAM_H
21 
22 #include "util/bda.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /*! \addtogroup WSF_UTIL_API
29  * \{ */
30 
31 /**************************************************************************************************
32  Macros
33 **************************************************************************************************/
34 
35 /**
36  * \name Macros for converting a little endian byte buffer to integers.
37  */
38 /**@{*/
39 /*! \brief convert little endian byte buffer to uint16_t. */
40 #define BYTES_TO_UINT16(n, p) {n = ((uint16_t)(p)[0] + ((uint16_t)(p)[1] << 8));}
41 /*! \brief convert little endian byte buffer to uint24_t. */
42 #define BYTES_TO_UINT24(n, p) {n = ((uint16_t)(p)[0] + ((uint16_t)(p)[1] << 8) + \
43  ((uint16_t)(p)[2] << 16));}
44 /*! \brief convert little endian byte buffer to uint32_t. */
45 #define BYTES_TO_UINT32(n, p) {n = ((uint32_t)(p)[0] + ((uint32_t)(p)[1] << 8) + \
46  ((uint32_t)(p)[2] << 16) + ((uint32_t)(p)[3] << 24));}
47 /*! \brief convert little endian byte buffer to uint40_t. */
48 #define BYTES_TO_UINT40(n, p) {n = ((uint64_t)(p)[0] + ((uint64_t)(p)[1] << 8) + \
49  ((uint64_t)(p)[2] << 16) + ((uint64_t)(p)[3] << 24) + \
50  ((uint64_t)(p)[4] << 32));}
51 /*! \brief convert little endian byte buffer to uint64_t. */
52 #define BYTES_TO_UINT64(n, p) {n = ((uint64_t)(p)[0] + ((uint64_t)(p)[1] << 8) + \
53  ((uint64_t)(p)[2] << 16) + ((uint64_t)(p)[3] << 24) + \
54  ((uint64_t)(p)[4] << 32) + ((uint64_t)(p)[5] << 40) + \
55  ((uint64_t)(p)[6] << 48) + ((uint64_t)(p)[7] << 56));}
56 /**@}*/
57 
58 /**
59  * \name Macros for converting little endian integers to array of bytes
60  */
61 /**@{*/
62 /*! \brief convert little endian uint16_t to array of bytes. */
63 #define UINT16_TO_BYTES(n) ((uint8_t) (n)), ((uint8_t)((n) >> 8))
64 /*! \brief convert little endian uint32_t to array of bytes. */
65 #define UINT32_TO_BYTES(n) ((uint8_t) (n)), ((uint8_t)((n) >> 8)), ((uint8_t)((n) >> 16)), ((uint8_t)((n) >> 24))
66 /**@}*/
67 
68 /**
69  * \name Macros for converting little endian integers to single bytes
70  */
71 /**@{*/
72 /*! \brief convert little endian uint16_t to byte 0. */
73 #define UINT16_TO_BYTE0(n) ((uint8_t) (n))
74 /*! \brief convert little endian uint16_t to byte 1. */
75 #define UINT16_TO_BYTE1(n) ((uint8_t) ((n) >> 8))
76 
77 /*! \brief convert little endian uint32_t to byte 0. */
78 #define UINT32_TO_BYTE0(n) ((uint8_t) (n))
79 /*! \brief convert little endian uint32_t to byte 1. */
80 #define UINT32_TO_BYTE1(n) ((uint8_t) ((n) >> 8))
81 /*! \brief convert little endian uint32_t to byte 2. */
82 #define UINT32_TO_BYTE2(n) ((uint8_t) ((n) >> 16))
83 /*! \brief convert little endian uint32_t to byte 3. */
84 #define UINT32_TO_BYTE3(n) ((uint8_t) ((n) >> 24))
85 /**@}*/
86 
87 /**
88  * \name Macros for converting a little endian byte stream to integers, with increment.
89  */
90 /**@{*/
91 /*! \brief convert little endian byte stream to uint8_t, incrementing one byte. */
92 #define BSTREAM_TO_INT8(n, p) {n = (int8_t)(*(p)++);}
93 /*! \brief convert little endian byte stream to int8_t, incrementing one byte. */
94 #define BSTREAM_TO_UINT8(n, p) {n = (uint8_t)(*(p)++);}
95 /*! \brief convert little endian byte stream to uint16_t, incrementing two bytes. */
96 #define BSTREAM_TO_UINT16(n, p) {BYTES_TO_UINT16(n, p); p += 2;}
97 /*! \brief convert little endian byte stream to uint24_t, incrementing three bytes. */
98 #define BSTREAM_TO_UINT24(n, p) {BYTES_TO_UINT24(n, p); p += 3;}
99 /*! \brief convert little endian byte stream to uint32_t, incrementing four bytes. */
100 #define BSTREAM_TO_UINT32(n, p) {BYTES_TO_UINT32(n, p); p += 4;}
101 /*! \brief convert little endian byte stream to uint40_t, incrementing five bytes. */
102 #define BSTREAM_TO_UINT40(n, p) {BYTES_TO_UINT40(n, p); p += 5;}
103 /*! \brief convert little endian byte stream to uint64_t, incrementing eigth bytes. */
104 #define BSTREAM_TO_UINT64(n, p) {n = BstreamToUint64(p); p += 8;}
105 /*! \brief convert little endian byte stream to six byte Bluetooth device address, incrementing six bytes. */
106 #define BSTREAM_TO_BDA(bda, p) {BdaCpy(bda, p); p += BDA_ADDR_LEN;}
107 /*! \brief convert little endian byte stream to eight byte Bluetooth device address, incrementing eight bytes. */
108 #define BSTREAM_TO_BDA64(bda, p) {bda = BstreamToBda64(p); p += BDA_ADDR_LEN;}
109 /**@}*/
110 
111 /**
112  * \name Macros for converting integers to a little endian byte stream, with increment.
113  */
114 /**@{*/
115 /*! \brief convert uint8_t to little endian byte stream, incrementing one byte. */
116 #define UINT8_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n);}
117 /*! \brief convert uint16_t to little endian byte stream, incrementing two bytes. */
118 #define UINT16_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8);}
119 /*! \brief convert uint24_t to little endian byte stream, incrementing three bytes. */
120 #define UINT24_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
121  *(p)++ = (uint8_t)((n) >> 16);}
122 /*! \brief convert uint32_t to little endian byte stream, incrementing four bytes. */
123 #define UINT32_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
124  *(p)++ = (uint8_t)((n) >> 16); *(p)++ = (uint8_t)((n) >> 24);}
125 /*! \brief convert uint40_t to little endian byte stream, incrementing five bytes. */
126 #define UINT40_TO_BSTREAM(p, n) {*(p)++ = (uint8_t)(n); *(p)++ = (uint8_t)((n) >> 8); \
127  *(p)++ = (uint8_t)((n) >> 16); *(p)++ = (uint8_t)((n) >> 24); \
128  *(p)++ = (uint8_t)((n) >> 32);}
129 /*! \brief convert uint64_t to little endian byte stream, incrementing eight bytes. */
130 #define UINT64_TO_BSTREAM(p, n) {Uint64ToBstream(p, n); p += sizeof(uint64_t);}
131 /*! \brief convert six byte Bluetooth device address to little endian byte stream, incrementing six bytes. */
132 #define BDA_TO_BSTREAM(p, bda) {BdaCpy(p, bda); p += BDA_ADDR_LEN;}
133 /*! \brief convert eight byte Bluetooth device address to little endian byte stream, incrementing eigth bytes. */
134 #define BDA64_TO_BSTREAM(p, bda) {Bda64ToBstream(p, bda); p += BDA_ADDR_LEN;}
135 /**@}*/
136 
137 /**
138  * \name Macros for converting integers to a little endian byte stream, without increment.
139  */
140 /**@{*/
141 /*! \brief convert uint16_t to little endian byte stream. */
142 #define UINT16_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8);}
143 /*! \brief convert uint24_t to little endian byte stream. */
144 #define UINT24_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
145  (p)[2] = (uint8_t)((n) >> 16);}
146 /*! \brief convert uint32_t to little endian byte stream. */
147 #define UINT32_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
148  (p)[2] = (uint8_t)((n) >> 16); (p)[3] = (uint8_t)((n) >> 24);}
149 /*! \brief convert uint40_t to little endian byte stream. */
150 #define UINT40_TO_BUF(p, n) {(p)[0] = (uint8_t)(n); (p)[1] = (uint8_t)((n) >> 8); \
151  (p)[2] = (uint8_t)((n) >> 16); (p)[3] = (uint8_t)((n) >> 24);\
152  (p)[4] = (uint8_t)((n) >> 32);}
153 /**@}*/
154 
155 /**
156  * \name Macros for comparing a little endian byte buffer to integers.
157  */
158 /**@{*/
159 /*! \brief compare 2 byte little endian buffer with a uint16_t. */
160 #define BYTES_UINT16_CMP(p, n) ((p)[1] == UINT16_TO_BYTE1(n) && (p)[0] == UINT16_TO_BYTE0(n))
161 /**@}*/
162 
163 /**
164  * \name Macros for IEEE FLOAT type: exponent = byte 3, mantissa = bytes 2-0
165  */
166 /**@{*/
167 /*! \brief Convert float to uint32. */
168 #define FLT_TO_UINT32(m, e) ((m) | ((int32_t)(e) * 16777216))
169 /*! \brief Convert uint32_t to float. */
170 #define UINT32_TO_FLT(m, e, n) {m = UINT32_TO_FLT_M(n); e = UINT32_TO_FLT_E(n);}
171 /*! \brief Convert uint32_t to float mantissa component */
172 #define UINT32_TO_FLT_M(n) ((((n) & 0x00FFFFFF) >= 0x00800000) ? \
173  ((int32_t)(((n) | 0xFF000000))) : ((int32_t)((n) & 0x00FFFFFF)))
174 /*! \brief Convert uint32_t to float exponent component. */
175 #define UINT32_TO_FLT_E(n) ((int8_t)(n >> 24))
176 /**@}*/
177 
178 /**
179  * \name Macros for IEEE SFLOAT type: exponent = bits 15-12, mantissa = bits 11-0
180  */
181 /**@{*/
182 /*! \brief Convert sfloat to uint16_t */
183 #define SFLT_TO_UINT16(m, e) ((m) | (0xF000 & ((int16_t)(e) * 4096)))
184 /*! \brief Convert uint16_t to sfloat */
185 #define UINT16_TO_SFLT(m, e, n) {m = UINT16_TO_SFLT_M(n); e = UINT16_TO_SFLT_E(n);}
186 /*! \brief Convert uint16_T to sfloat mantissa component */
187 #define UINT16_TO_SFLT_M(n) ((((n) & 0x0FFF) >= 0x0800) ? \
188  ((int16_t)(((n) | 0xF000))) : ((int16_t)((n) & 0x0FFF)))
189 /*! \brief Convert uint16_T to sfloat exponent component */
190 #define UINT16_TO_SFLT_E(n) (((n >> 12) >= 0x0008) ? \
191  ((int8_t)(((n >> 12) | 0xF0))) : ((int8_t)(n >> 12)))
192 /**@}*/
193 
194 /**************************************************************************************************
195  Function Declarations
196 **************************************************************************************************/
197 
198 /*************************************************************************************************/
199 /*!
200  * \brief Convert bstream to BDA64.
201  *
202  * \param p Bstream pointer.
203  *
204  * \return Resulting BDA64 number.
205  */
206 /*************************************************************************************************/
208 
209 /*************************************************************************************************/
210 /*!
211  * \brief Convert bstream to uint64_t.
212  *
213  * \param p Bstream pointer.
214  *
215  * \return Resulting uint64_t number.
216  */
217 /*************************************************************************************************/
219 
220 /*************************************************************************************************/
221 /*!
222  * \brief Convert BDA64 to bstream.
223  *
224  * \param p Bstream pointer.
225  * \param bda uint64_t BDA.
226  *
227  * \return None.
228  */
229 /*************************************************************************************************/
230 void Bda64ToBstream(uint8_t *p, uint64_t bda);
231 
232 /*************************************************************************************************/
233 /*!
234  * \brief Convert uint64_t to bstream.
235  *
236  * \param p Bstream pointer.
237  * \param n uint64_t number.
238  *
239  * \return None.
240  */
241 /*************************************************************************************************/
242 void Uint64ToBstream(uint8_t *p, uint64_t n);
243 
244 /*! \} */ /* WSF_UTIL_API */
245 
246 #ifdef __cplusplus
247 };
248 #endif
249 
250 #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.
unsigned long long uint64_t
Unsigned 64-bit value.
Definition: wsf_types.h:73
Bluetooth device address utilities.
uint64_t BstreamToBda64(const uint8_t *p)
Convert bstream to BDA64.
unsigned char uint8_t
Unsigned 8-bit value.
Definition: wsf_types.h:63