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++)     \
115       *(p)++ = (uint8_t)(a)[15 - ijk]; \
116   }
117 #define ARRAY8_TO_STREAM(p, a)        \
118   {                                   \
119     int ijk;                          \
120     for (ijk = 0; ijk < 8; ijk++)     \
121       *(p)++ = (uint8_t)(a)[7 - ijk]; \
122   }
123 #define LAP_TO_STREAM(p, a)                     \
124   {                                             \
125     int ijk;                                    \
126     for (ijk = 0; ijk < LAP_LEN; ijk++)         \
127       *(p)++ = (uint8_t)(a)[LAP_LEN - 1 - ijk]; \
128   }
129 #define ARRAY_TO_STREAM(p, a, len)    \
130   {                                   \
131     int ijk;                          \
132     for (ijk = 0; ijk < (len); ijk++) \
133       *(p)++ = (uint8_t)(a)[ijk];     \
134   }
135 #define STREAM_TO_INT8(u8, p) \
136   {                           \
137     (u8) = (*((int8_t*)(p))); \
138     (p) += 1;                 \
139   }
140 #define STREAM_TO_UINT8(u8, p) \
141   {                            \
142     (u8) = (uint8_t)(*(p));    \
143     (p) += 1;                  \
144   }
145 #define STREAM_TO_UINT16(u16, p)                                  \
146   {                                                               \
147     (u16) = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); \
148     (p) += 2;                                                     \
149   }
150 #define STREAM_TO_UINT24(u32, p)                                      \
151   {                                                                   \
152     (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + \
153              ((((uint32_t)(*((p) + 2)))) << 16));                     \
154     (p) += 3;                                                         \
155   }
156 #define STREAM_TO_UINT32(u32, p)                                                       \
157   {                                                                                    \
158     (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) +                  \
159              ((((uint32_t)(*((p) + 2)))) << 16) + ((((uint32_t)(*((p) + 3)))) << 24)); \
160     (p) += 4;                                                                          \
161   }
162 #define STREAM_TO_UINT64(u64, p)                                                       \
163   {                                                                                    \
164     (u64) = (((uint64_t)(*(p))) + ((((uint64_t)(*((p) + 1)))) << 8) +                  \
165              ((((uint64_t)(*((p) + 2)))) << 16) + ((((uint64_t)(*((p) + 3)))) << 24) + \
166              ((((uint64_t)(*((p) + 4)))) << 32) + ((((uint64_t)(*((p) + 5)))) << 40) + \
167              ((((uint64_t)(*((p) + 6)))) << 48) + ((((uint64_t)(*((p) + 7)))) << 56)); \
168     (p) += 8;                                                                          \
169   }
170 #define STREAM_TO_ARRAY16(a, p)        \
171   {                                    \
172     int ijk;                           \
173     uint8_t* _pa = (uint8_t*)(a) + 15; \
174     for (ijk = 0; ijk < 16; ijk++)     \
175       *_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++)     \
182       *_pa-- = *(p)++;                \
183   }
184 #define STREAM_TO_LAP(a, p)                      \
185   {                                              \
186     int ijk;                                     \
187     uint8_t* plap = (uint8_t*)(a) + LAP_LEN - 1; \
188     for (ijk = 0; ijk < LAP_LEN; ijk++)          \
189       *plap-- = *(p)++;                          \
190   }
191 #define STREAM_TO_ARRAY(a, p, len)    \
192   {                                   \
193     int ijk;                          \
194     for (ijk = 0; ijk < (len); ijk++) \
195       ((uint8_t*)(a))[ijk] = *(p)++;  \
196   }
197 #define STREAM_SKIP_UINT8(p) \
198   do {                       \
199     (p) += 1;                \
200   } while (0)
201 #define STREAM_SKIP_UINT16(p) \
202   do {                        \
203     (p) += 2;                 \
204   } while (0)
205 #define STREAM_SKIP_UINT32(p) \
206   do {                        \
207     (p) += 4;                 \
208   } while (0)
209 
210 /*******************************************************************************
211  * Macros to get and put bytes to and from a stream (Big Endian format)
212  */
213 #define UINT32_TO_BE_STREAM(p, u32)  \
214   {                                  \
215     *(p)++ = (uint8_t)((u32) >> 24); \
216     *(p)++ = (uint8_t)((u32) >> 16); \
217     *(p)++ = (uint8_t)((u32) >> 8);  \
218     *(p)++ = (uint8_t)(u32);         \
219   }
220 #define UINT24_TO_BE_STREAM(p, u24)  \
221   {                                  \
222     *(p)++ = (uint8_t)((u24) >> 16); \
223     *(p)++ = (uint8_t)((u24) >> 8);  \
224     *(p)++ = (uint8_t)(u24);         \
225   }
226 #define UINT16_TO_BE_STREAM(p, u16) \
227   {                                 \
228     *(p)++ = (uint8_t)((u16) >> 8); \
229     *(p)++ = (uint8_t)(u16);        \
230   }
231 #define UINT8_TO_BE_STREAM(p, u8) \
232   { *(p)++ = (uint8_t)(u8); }
233 #define ARRAY_TO_BE_STREAM(p, a, len) \
234   {                                   \
235     int ijk;                          \
236     for (ijk = 0; ijk < (len); ijk++) \
237       *(p)++ = (uint8_t)(a)[ijk];     \
238   }
239 #define BE_STREAM_TO_UINT8(u8, p) \
240   {                               \
241     (u8) = (uint8_t)(*(p));       \
242     (p) += 1;                     \
243   }
244 #define BE_STREAM_TO_UINT16(u16, p)                                       \
245   {                                                                       \
246     (u16) = (uint16_t)(((uint16_t)(*(p)) << 8) + (uint16_t)(*((p) + 1))); \
247     (p) += 2;                                                             \
248   }
249 #define BE_STREAM_TO_UINT24(u32, p)                                                                \
250   {                                                                                                \
251     (u32) = (((uint32_t)(*((p) + 2))) + ((uint32_t)(*((p) + 1)) << 8) + ((uint32_t)(*(p)) << 16)); \
252     (p) += 3;                                                                                      \
253   }
254 #define BE_STREAM_TO_UINT32(u32, p)                                      \
255   {                                                                      \
256     (u32) = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) +    \
257              ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \
258     (p) += 4;                                                            \
259   }
260 #define BE_STREAM_TO_UINT64(u64, p)                                            \
261   {                                                                            \
262     (u64) = ((uint64_t)(*((p) + 7)) + ((uint64_t)(*((p) + 6)) << 8) +          \
263              ((uint64_t)(*((p) + 5)) << 16) + ((uint64_t)(*((p) + 4)) << 24) + \
264              ((uint64_t)(*((p) + 3)) << 32) + ((uint64_t)(*((p) + 2)) << 40) + \
265              ((uint64_t)(*((p) + 1)) << 48) + ((uint64_t)(*(p)) << 56));       \
266     (p) += 8;                                                                  \
267   }
268 #define BE_STREAM_TO_ARRAY(p, a, len) \
269   {                                   \
270     int ijk;                          \
271     for (ijk = 0; ijk < (len); ijk++) \
272       ((uint8_t*)(a))[ijk] = *(p)++;  \
273   }
274 
275 /*******************************************************************************
276  * Macros to get and put bytes to and from a field (Big Endian format).
277  * These are the same as to stream, except the pointer is not incremented.
278  */
279 #define UINT32_TO_BE_FIELD(p, u32)                 \
280   {                                                \
281     *(uint8_t*)(p) = (uint8_t)((u32) >> 24);       \
282     *((uint8_t*)(p) + 1) = (uint8_t)((u32) >> 16); \
283     *((uint8_t*)(p) + 2) = (uint8_t)((u32) >> 8);  \
284     *((uint8_t*)(p) + 3) = (uint8_t)(u32);         \
285   }
286 #define UINT16_TO_BE_FIELD(p, u16)          \
287   {                                         \
288     *(uint8_t*)(p) = (uint8_t)((u16) >> 8); \
289     *((uint8_t*)(p) + 1) = (uint8_t)(u16);  \
290   }
291 
292 /* Common Bluetooth field definitions */
293 
294 #define BT_1SEC_TIMEOUT_MS (1 * 1000) /* 1 second */
295 
296 #endif
297