1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #ifndef BT_TYPES_H
20 #define BT_TYPES_H
21 
22 #include <stdbool.h>
23 
24 #include "stack/include/bt_device_type.h"
25 #include "stack/include/bt_hdr.h"
26 #ifdef __cplusplus
27 #include "include/hardware/bluetooth.h"
28 #include "stack/include/bt_octets.h"
29 #include "types/bluetooth/uuid.h"
30 #include "types/raw_address.h"
31 #endif  // __cplusplus
32 
33 /* READ WELL !!
34  *
35  * This section defines global events. These are events that cross layers.
36  * Any event that passes between layers MUST be one of these events. Tasks
37  * can use their own events internally, but a FUNDAMENTAL design issue is
38  * that global events MUST be one of these events defined below.
39  *
40  * The convention used is the the event name contains the layer that the
41  * event is going to.
42  */
43 #define BT_EVT_MASK 0xFF00
44 #define BT_SUB_EVT_MASK 0x00FF
45 /* To Bluetooth Upper Layers        */
46 /************************************/
47 /* HCI Event                        */
48 #define BT_EVT_TO_BTU_HCI_EVT 0x1000
49 /* SCO Data from HCI                */
50 #define BT_EVT_TO_BTU_HCI_SCO 0x1200
51 /* HCI Transport Error              */
52 #define BT_EVT_TO_BTU_HCIT_ERR 0x1300
53 
54 /* Serial Port Data                 */
55 #define BT_EVT_TO_BTU_SP_DATA 0x1500
56 
57 /* ISO Data from HCI                */
58 #define BT_EVT_TO_BTU_HCI_ISO 0x1700
59 
60 /* To LM                            */
61 /************************************/
62 /* HCI Command                      */
63 #define BT_EVT_TO_LM_HCI_CMD 0x2000
64 /* HCI ACL Data                     */
65 #define BT_EVT_TO_LM_HCI_ACL 0x2100
66 /* HCI SCO Data                     */
67 #define BT_EVT_TO_LM_HCI_SCO 0x2200
68 /* HCI ISO Data                     */
69 #define BT_EVT_TO_LM_HCI_ISO 0x2d00
70 
71 /* ISO Layer specific */
72 #define BT_ISO_HDR_CONTAINS_TS (0x0001)
73 #define BT_ISO_HDR_OFFSET_POINTS_DATA (0x0002)
74 
75 /*******************************************************************************
76  * Macros to get and put bytes to and from a stream (Little Endian format).
77  */
78 #define UINT64_TO_BE_STREAM(p, u64)  \
79   {                                  \
80     *(p)++ = (uint8_t)((u64) >> 56); \
81     *(p)++ = (uint8_t)((u64) >> 48); \
82     *(p)++ = (uint8_t)((u64) >> 40); \
83     *(p)++ = (uint8_t)((u64) >> 32); \
84     *(p)++ = (uint8_t)((u64) >> 24); \
85     *(p)++ = (uint8_t)((u64) >> 16); \
86     *(p)++ = (uint8_t)((u64) >> 8);  \
87     *(p)++ = (uint8_t)(u64);         \
88   }
89 #define UINT32_TO_STREAM(p, u32)     \
90   {                                  \
91     *(p)++ = (uint8_t)(u32);         \
92     *(p)++ = (uint8_t)((u32) >> 8);  \
93     *(p)++ = (uint8_t)((u32) >> 16); \
94     *(p)++ = (uint8_t)((u32) >> 24); \
95   }
96 #define UINT24_TO_STREAM(p, u24)     \
97   {                                  \
98     *(p)++ = (uint8_t)(u24);         \
99     *(p)++ = (uint8_t)((u24) >> 8);  \
100     *(p)++ = (uint8_t)((u24) >> 16); \
101   }
102 #define UINT16_TO_STREAM(p, u16)    \
103   {                                 \
104     *(p)++ = (uint8_t)(u16);        \
105     *(p)++ = (uint8_t)((u16) >> 8); \
106   }
107 #define UINT8_TO_STREAM(p, u8) \
108   { *(p)++ = (uint8_t)(u8); }
109 #define INT8_TO_STREAM(p, u8) \
110   { *(p)++ = (int8_t)(u8); }
111 #define ARRAY16_TO_STREAM(p, a)                                     \
112   {                                                                 \
113     int ijk;                                                        \
114     for (ijk = 0; ijk < 16; ijk++) *(p)++ = (uint8_t)(a)[15 - ijk]; \
115   }
116 #define ARRAY8_TO_STREAM(p, a)                                    \
117   {                                                               \
118     int ijk;                                                      \
119     for (ijk = 0; ijk < 8; ijk++) *(p)++ = (uint8_t)(a)[7 - ijk]; \
120   }
121 #define LAP_TO_STREAM(p, a)                     \
122   {                                             \
123     int ijk;                                    \
124     for (ijk = 0; ijk < LAP_LEN; ijk++)         \
125       *(p)++ = (uint8_t)(a)[LAP_LEN - 1 - ijk]; \
126   }
127 #define ARRAY_TO_STREAM(p, a, len)                                \
128   {                                                               \
129     int ijk;                                                      \
130     for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[ijk]; \
131   }
132 #define STREAM_TO_INT8(u8, p)   \
133   {                             \
134     (u8) = (*((int8_t*)(p)));   \
135     (p) += 1;                   \
136   }
137 #define STREAM_TO_UINT8(u8, p) \
138   {                            \
139     (u8) = (uint8_t)(*(p));    \
140     (p) += 1;                  \
141   }
142 #define STREAM_TO_UINT16(u16, p)                                  \
143   {                                                               \
144     (u16) = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); \
145     (p) += 2;                                                     \
146   }
147 #define STREAM_TO_UINT24(u32, p)                                      \
148   {                                                                   \
149     (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + \
150              ((((uint32_t)(*((p) + 2)))) << 16));                     \
151     (p) += 3;                                                         \
152   }
153 #define STREAM_TO_UINT32(u32, p)                                      \
154   {                                                                   \
155     (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + \
156              ((((uint32_t)(*((p) + 2)))) << 16) +                     \
157              ((((uint32_t)(*((p) + 3)))) << 24));                     \
158     (p) += 4;                                                         \
159   }
160 #define STREAM_TO_UINT64(u64, p)                                      \
161   {                                                                   \
162     (u64) = (((uint64_t)(*(p))) + ((((uint64_t)(*((p) + 1)))) << 8) + \
163              ((((uint64_t)(*((p) + 2)))) << 16) +                     \
164              ((((uint64_t)(*((p) + 3)))) << 24) +                     \
165              ((((uint64_t)(*((p) + 4)))) << 32) +                     \
166              ((((uint64_t)(*((p) + 5)))) << 40) +                     \
167              ((((uint64_t)(*((p) + 6)))) << 48) +                     \
168              ((((uint64_t)(*((p) + 7)))) << 56));                     \
169     (p) += 8;                                                         \
170   }
171 #define STREAM_TO_ARRAY16(a, p)                     \
172   {                                                 \
173     int ijk;                                        \
174     uint8_t* _pa = (uint8_t*)(a) + 15;              \
175     for (ijk = 0; ijk < 16; ijk++) *_pa-- = *(p)++; \
176   }
177 #define STREAM_TO_ARRAY8(a, p)                     \
178   {                                                \
179     int ijk;                                       \
180     uint8_t* _pa = (uint8_t*)(a) + 7;              \
181     for (ijk = 0; ijk < 8; ijk++) *_pa-- = *(p)++; \
182   }
183 #define STREAM_TO_LAP(a, p)                               \
184   {                                                       \
185     int ijk;                                              \
186     uint8_t* plap = (uint8_t*)(a) + LAP_LEN - 1;          \
187     for (ijk = 0; ijk < LAP_LEN; ijk++) *plap-- = *(p)++; \
188   }
189 #define STREAM_TO_ARRAY(a, p, len)                                   \
190   {                                                                  \
191     int ijk;                                                         \
192     for (ijk = 0; ijk < (len); ijk++) ((uint8_t*)(a))[ijk] = *(p)++; \
193   }
194 #define STREAM_SKIP_UINT8(p) \
195   do {                       \
196     (p) += 1;                \
197   } while (0)
198 #define STREAM_SKIP_UINT16(p) \
199   do {                        \
200     (p) += 2;                 \
201   } while (0)
202 #define STREAM_SKIP_UINT32(p) \
203   do {                        \
204     (p) += 4;                 \
205   } while (0)
206 
207 /*******************************************************************************
208  * Macros to get and put bytes to and from a stream (Big Endian format)
209  */
210 #define UINT32_TO_BE_STREAM(p, u32)  \
211   {                                  \
212     *(p)++ = (uint8_t)((u32) >> 24); \
213     *(p)++ = (uint8_t)((u32) >> 16); \
214     *(p)++ = (uint8_t)((u32) >> 8);  \
215     *(p)++ = (uint8_t)(u32);         \
216   }
217 #define UINT24_TO_BE_STREAM(p, u24)  \
218   {                                  \
219     *(p)++ = (uint8_t)((u24) >> 16); \
220     *(p)++ = (uint8_t)((u24) >> 8);  \
221     *(p)++ = (uint8_t)(u24);         \
222   }
223 #define UINT16_TO_BE_STREAM(p, u16) \
224   {                                 \
225     *(p)++ = (uint8_t)((u16) >> 8); \
226     *(p)++ = (uint8_t)(u16);        \
227   }
228 #define UINT8_TO_BE_STREAM(p, u8) \
229   { *(p)++ = (uint8_t)(u8); }
230 #define ARRAY_TO_BE_STREAM(p, a, len)                             \
231   {                                                               \
232     int ijk;                                                      \
233     for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[ijk]; \
234   }
235 #define BE_STREAM_TO_UINT8(u8, p) \
236   {                               \
237     (u8) = (uint8_t)(*(p));       \
238     (p) += 1;                     \
239   }
240 #define BE_STREAM_TO_UINT16(u16, p)                                       \
241   {                                                                       \
242     (u16) = (uint16_t)(((uint16_t)(*(p)) << 8) + (uint16_t)(*((p) + 1))); \
243     (p) += 2;                                                             \
244   }
245 #define BE_STREAM_TO_UINT24(u32, p)                                     \
246   {                                                                     \
247     (u32) = (((uint32_t)(*((p) + 2))) + ((uint32_t)(*((p) + 1)) << 8) + \
248              ((uint32_t)(*(p)) << 16));                                 \
249     (p) += 3;                                                           \
250   }
251 #define BE_STREAM_TO_UINT32(u32, p)                                      \
252   {                                                                      \
253     (u32) = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) +    \
254              ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \
255     (p) += 4;                                                            \
256   }
257 #define BE_STREAM_TO_UINT64(u64, p)                                            \
258   {                                                                            \
259     (u64) = ((uint64_t)(*((p) + 7)) + ((uint64_t)(*((p) + 6)) << 8) +          \
260              ((uint64_t)(*((p) + 5)) << 16) + ((uint64_t)(*((p) + 4)) << 24) + \
261              ((uint64_t)(*((p) + 3)) << 32) + ((uint64_t)(*((p) + 2)) << 40) + \
262              ((uint64_t)(*((p) + 1)) << 48) + ((uint64_t)(*(p)) << 56));       \
263     (p) += 8;                                                                  \
264   }
265 #define BE_STREAM_TO_ARRAY(p, a, len)                                \
266   {                                                                  \
267     int ijk;                                                         \
268     for (ijk = 0; ijk < (len); ijk++) ((uint8_t*)(a))[ijk] = *(p)++; \
269   }
270 
271 /*******************************************************************************
272  * Macros to get and put bytes to and from a field (Big Endian format).
273  * These are the same as to stream, except the pointer is not incremented.
274  */
275 #define UINT32_TO_BE_FIELD(p, u32)                 \
276   {                                                \
277     *(uint8_t*)(p) = (uint8_t)((u32) >> 24);       \
278     *((uint8_t*)(p) + 1) = (uint8_t)((u32) >> 16); \
279     *((uint8_t*)(p) + 2) = (uint8_t)((u32) >> 8);  \
280     *((uint8_t*)(p) + 3) = (uint8_t)(u32);         \
281   }
282 #define UINT16_TO_BE_FIELD(p, u16)          \
283   {                                         \
284     *(uint8_t*)(p) = (uint8_t)((u16) >> 8); \
285     *((uint8_t*)(p) + 1) = (uint8_t)(u16);  \
286   }
287 
288 /* Common Bluetooth field definitions */
289 
290 #define BT_1SEC_TIMEOUT_MS (1 * 1000) /* 1 second */
291 
292 #endif
293