• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 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 #include <stdint.h>
24 #include <string.h>
25 
26 #ifndef FALSE
27 #define FALSE false
28 #endif
29 
30 #ifndef TRUE
31 #define TRUE true
32 #endif
33 
34 #ifdef __arm
35 #define PACKED __packed
36 #define INLINE __inline
37 #else
38 #define PACKED
39 #define INLINE
40 #endif
41 
42 /* READ WELL !!
43  *
44  * This section defines global events. These are events that cross layers.
45  * Any event that passes between layers MUST be one of these events. Tasks
46  * can use their own events internally, but a FUNDAMENTAL design issue is
47  * that global events MUST be one of these events defined below.
48  *
49  * The convention used is the the event name contains the layer that the
50  * event is going to.
51  */
52 #define BT_EVT_MASK 0xFF00
53 #define BT_SUB_EVT_MASK 0x00FF
54 /* To Bluetooth Upper Layers        */
55 /************************************/
56 /* L2CAP event */
57 #define BT_EVT_TO_BTU_L2C_EVT 0x0900
58 /* HCI Event                        */
59 #define BT_EVT_TO_BTU_HCI_EVT 0x1000
60 /* event from BR/EDR controller */
61 #define BT_EVT_TO_BTU_HCI_BR_EDR_EVT (0x0000 | BT_EVT_TO_BTU_HCI_EVT)
62 /* event from local AMP 1 controller */
63 #define BT_EVT_TO_BTU_HCI_AMP1_EVT (0x0001 | BT_EVT_TO_BTU_HCI_EVT)
64 /* event from local AMP 2 controller */
65 #define BT_EVT_TO_BTU_HCI_AMP2_EVT (0x0002 | BT_EVT_TO_BTU_HCI_EVT)
66 /* event from local AMP 3 controller */
67 #define BT_EVT_TO_BTU_HCI_AMP3_EVT (0x0003 | BT_EVT_TO_BTU_HCI_EVT)
68 
69 /* ACL Data from HCI                */
70 #define BT_EVT_TO_BTU_HCI_ACL 0x1100
71 /* SCO Data from HCI                */
72 #define BT_EVT_TO_BTU_HCI_SCO 0x1200
73 /* HCI Transport Error              */
74 #define BT_EVT_TO_BTU_HCIT_ERR 0x1300
75 
76 /* Serial Port Event                */
77 #define BT_EVT_TO_BTU_SP_EVT 0x1400
78 /* Serial Port Data                 */
79 #define BT_EVT_TO_BTU_SP_DATA 0x1500
80 
81 /* HCI command from upper layer     */
82 #define BT_EVT_TO_BTU_HCI_CMD 0x1600
83 
84 /* L2CAP segment(s) transmitted     */
85 #define BT_EVT_TO_BTU_L2C_SEG_XMIT 0x1900
86 
87 /* BlueStackTester event: incoming message from target */
88 #define BT_EVT_PROXY_INCOMING_MSG 0x1A00
89 
90 /* Insight BTSIM event */
91 #define BT_EVT_BTSIM 0x1B00
92 /* Insight Script Engine event */
93 #define BT_EVT_BTISE 0x1C00
94 
95 /* To LM                            */
96 /************************************/
97 /* HCI Command                      */
98 #define BT_EVT_TO_LM_HCI_CMD 0x2000
99 /* HCI ACL Data                     */
100 #define BT_EVT_TO_LM_HCI_ACL 0x2100
101 /* HCI SCO Data                     */
102 #define BT_EVT_TO_LM_HCI_SCO 0x2200
103 /* HCI Transport Error              */
104 #define BT_EVT_TO_LM_HCIT_ERR 0x2300
105 /* LC event                         */
106 #define BT_EVT_TO_LM_LC_EVT 0x2400
107 /* LC Received LMP command frame    */
108 #define BT_EVT_TO_LM_LC_LMP 0x2500
109 /* LC Received ACL data             */
110 #define BT_EVT_TO_LM_LC_ACL 0x2600
111 /* LC Received SCO data  (not used) */
112 #define BT_EVT_TO_LM_LC_SCO 0x2700
113 /* LMP data transmit complete       */
114 #define BT_EVT_TO_LM_LC_ACL_TX 0x2800
115 /* LMP Command transmit complete    */
116 #define BT_EVT_TO_LM_LC_LMPC_TX 0x2900
117 /* Data to be locally loopbacked    */
118 #define BT_EVT_TO_LM_LOCAL_ACL_LB 0x2a00
119 /* HCI ACL Data ack      (not used) */
120 #define BT_EVT_TO_LM_HCI_ACL_ACK 0x2b00
121 /* LM Diagnostics commands          */
122 #define BT_EVT_TO_LM_DIAG 0x2c00
123 
124 #define BT_EVT_TO_BTM_CMDS 0x2f00
125 #define BT_EVT_TO_BTM_PM_MDCHG_EVT (0x0001 | BT_EVT_TO_BTM_CMDS)
126 
127 #define BT_EVT_TO_TCS_CMDS 0x3000
128 
129 #define BT_EVT_TO_CTP_CMDS 0x3300
130 
131 /* ftp events */
132 #define BT_EVT_TO_FTP_SRVR_CMDS 0x3600
133 #define BT_EVT_TO_FTP_CLNT_CMDS 0x3700
134 
135 /* SIM Access Profile events */
136 #define BT_EVT_TO_BTU_SAP 0x3800
137 
138 /* opp events */
139 #define BT_EVT_TO_OPP_SRVR_CMDS 0x3900
140 #define BT_EVT_TO_OPP_CLNT_CMDS 0x3a00
141 
142 /* for NFC                          */
143 /************************************/
144 /* NCI Command, Notification or Data*/
145 #define BT_EVT_TO_NFC_NCI 0x4000
146 /* Initialization message */
147 #define BT_EVT_TO_NFC_INIT 0x4100
148 /* Low power */
149 #define BT_EVT_TO_NCI_LP 0x4200
150 /* Error notification to NFC Task */
151 #define BT_EVT_TO_NFC_ERR 0x4300
152 
153 /* events to NFCC simulation (NCI packets) */
154 #define BT_EVT_TO_NFCCSIM_NCI 0x4a00
155 
156 /* HCISU Events */
157 
158 #define BT_EVT_HCISU 0x5000
159 
160 #define BT_EVT_TO_HCISU_RECONFIG_EVT (0x0001 | BT_EVT_HCISU)
161 #define BT_EVT_TO_HCISU_UPDATE_BAUDRATE_EVT (0x0002 | BT_EVT_HCISU)
162 #define BT_EVT_TO_HCISU_LP_ENABLE_EVT (0x0003 | BT_EVT_HCISU)
163 #define BT_EVT_TO_HCISU_LP_DISABLE_EVT (0x0004 | BT_EVT_HCISU)
164 #define BT_EVT_TO_HCISU_LP_APP_SLEEPING_EVT (0x0005 | BT_EVT_HCISU)
165 #define BT_EVT_TO_HCISU_LP_ALLOW_BT_SLEEP_EVT (0x0006 | BT_EVT_HCISU)
166 #define BT_EVT_TO_HCISU_LP_WAKEUP_HOST_EVT (0x0007 | BT_EVT_HCISU)
167 #define BT_EVT_TO_HCISU_LP_RCV_H4IBSS_EVT (0x0008 | BT_EVT_HCISU)
168 #define BT_EVT_TO_HCISU_H5_RESET_EVT (0x0009 | BT_EVT_HCISU)
169 #define BT_EVT_HCISU_START_QUICK_TIMER (0x000a | BT_EVT_HCISU)
170 
171 #define BT_EVT_DATA_TO_AMP_1 0x5100
172 #define BT_EVT_DATA_TO_AMP_15 0x5f00
173 
174 /* HSP Events */
175 
176 #define BT_EVT_BTU_HSP2 0x6000
177 
178 #define BT_EVT_TO_BTU_HSP2_EVT (0x0001 | BT_EVT_BTU_HSP2)
179 
180 /* BPP Events */
181 #define BT_EVT_TO_BPP_PR_CMDS 0x6100  /* Printer Events */
182 #define BT_EVT_TO_BPP_SND_CMDS 0x6200 /* BPP Sender Events */
183 
184 /* BIP Events */
185 #define BT_EVT_TO_BIP_CMDS 0x6300
186 
187 /* HCRP Events */
188 
189 #define BT_EVT_BTU_HCRP 0x7000
190 
191 #define BT_EVT_TO_BTU_HCRP_EVT (0x0001 | BT_EVT_BTU_HCRP)
192 #define BT_EVT_TO_BTU_HCRPM_EVT (0x0002 | BT_EVT_BTU_HCRP)
193 
194 #define BT_EVT_BTU_HFP 0x8000
195 #define BT_EVT_TO_BTU_HFP_EVT (0x0001 | BT_EVT_BTU_HFP)
196 
197 #define BT_EVT_BTU_IPC_EVT 0x9000
198 #define BT_EVT_BTU_IPC_LOGMSG_EVT (0x0000 | BT_EVT_BTU_IPC_EVT)
199 #define BT_EVT_BTU_IPC_ACL_EVT (0x0001 | BT_EVT_BTU_IPC_EVT)
200 #define BT_EVT_BTU_IPC_BTU_EVT (0x0002 | BT_EVT_BTU_IPC_EVT)
201 #define BT_EVT_BTU_IPC_L2C_EVT (0x0003 | BT_EVT_BTU_IPC_EVT)
202 #define BT_EVT_BTU_IPC_L2C_MSG_EVT (0x0004 | BT_EVT_BTU_IPC_EVT)
203 #define BT_EVT_BTU_IPC_BTM_EVT (0x0005 | BT_EVT_BTU_IPC_EVT)
204 #define BT_EVT_BTU_IPC_AVDT_EVT (0x0006 | BT_EVT_BTU_IPC_EVT)
205 #define BT_EVT_BTU_IPC_SLIP_EVT (0x0007 | BT_EVT_BTU_IPC_EVT)
206 #define BT_EVT_BTU_IPC_MGMT_EVT (0x0008 | BT_EVT_BTU_IPC_EVT)
207 #define BT_EVT_BTU_IPC_BTTRC_EVT (0x0009 | BT_EVT_BTU_IPC_EVT)
208 #define BT_EVT_BTU_IPC_BURST_EVT (0x000A | BT_EVT_BTU_IPC_EVT)
209 
210 /* BTIF Events */
211 #define BT_EVT_BTIF 0xA000
212 #define BT_EVT_CONTEXT_SWITCH_EVT (0x0001 | BT_EVT_BTIF)
213 
214 /* Define the header of each buffer used in the Bluetooth stack.
215  */
216 typedef struct {
217   uint16_t event;
218   uint16_t len;
219   uint16_t offset;
220   uint16_t layer_specific;
221   uint8_t data[];
222 } BT_HDR;
223 
224 #define BT_HDR_SIZE (sizeof(BT_HDR))
225 
226 #define BT_PSM_SDP 0x0001
227 #define BT_PSM_RFCOMM 0x0003
228 #define BT_PSM_TCS 0x0005
229 #define BT_PSM_CTP 0x0007
230 #define BT_PSM_BNEP 0x000F
231 #define BT_PSM_HIDC 0x0011
232 #define BT_PSM_HIDI 0x0013
233 #define BT_PSM_UPNP 0x0015
234 #define BT_PSM_AVCTP 0x0017
235 #define BT_PSM_AVDTP 0x0019
236 #define BT_PSM_AVCTP_13 0x001B /* Advanced Control - Browsing */
237 #define BT_PSM_UDI_CP \
238   0x001D /* Unrestricted Digital Information Profile C-Plane  */
239 #define BT_PSM_ATT 0x001F /* Attribute Protocol  */
240 
241 /* These macros extract the HCI opcodes from a buffer
242  */
243 #define HCI_GET_CMD_HDR_OPCODE(p)                    \
244   (uint16_t)((*((uint8_t*)((p) + 1) + (p)->offset) + \
245               (*((uint8_t*)((p) + 1) + (p)->offset + 1) << 8)))
246 #define HCI_GET_CMD_HDR_PARAM_LEN(p) \
247   (uint8_t)(*((uint8_t*)((p) + 1) + (p)->offset + 2))
248 
249 #define HCI_GET_EVT_HDR_OPCODE(p) \
250   (uint8_t)(*((uint8_t*)((p) + 1) + (p)->offset))
251 #define HCI_GET_EVT_HDR_PARAM_LEN(p) \
252   (uint8_t)(*((uint8_t*)((p) + 1) + (p)->offset + 1))
253 
254 /*******************************************************************************
255  * Macros to get and put bytes to and from a stream (Little Endian format).
256  */
257 #define UINT64_TO_BE_STREAM(p, u64)  \
258   {                                  \
259     *(p)++ = (uint8_t)((u64) >> 56); \
260     *(p)++ = (uint8_t)((u64) >> 48); \
261     *(p)++ = (uint8_t)((u64) >> 40); \
262     *(p)++ = (uint8_t)((u64) >> 32); \
263     *(p)++ = (uint8_t)((u64) >> 24); \
264     *(p)++ = (uint8_t)((u64) >> 16); \
265     *(p)++ = (uint8_t)((u64) >> 8);  \
266     *(p)++ = (uint8_t)(u64);         \
267   }
268 #define UINT32_TO_STREAM(p, u32)     \
269   {                                  \
270     *(p)++ = (uint8_t)(u32);         \
271     *(p)++ = (uint8_t)((u32) >> 8);  \
272     *(p)++ = (uint8_t)((u32) >> 16); \
273     *(p)++ = (uint8_t)((u32) >> 24); \
274   }
275 #define UINT24_TO_STREAM(p, u24)     \
276   {                                  \
277     *(p)++ = (uint8_t)(u24);         \
278     *(p)++ = (uint8_t)((u24) >> 8);  \
279     *(p)++ = (uint8_t)((u24) >> 16); \
280   }
281 #define UINT16_TO_STREAM(p, u16)    \
282   {                                 \
283     *(p)++ = (uint8_t)(u16);        \
284     *(p)++ = (uint8_t)((u16) >> 8); \
285   }
286 #define UINT8_TO_STREAM(p, u8) \
287   { *(p)++ = (uint8_t)(u8); }
288 #define INT8_TO_STREAM(p, u8) \
289   { *(p)++ = (int8_t)(u8); }
290 #define ARRAY32_TO_STREAM(p, a)                                     \
291   {                                                                 \
292     int ijk;                                                        \
293     for (ijk = 0; ijk < 32; ijk++) *(p)++ = (uint8_t)(a)[31 - ijk]; \
294   }
295 #define ARRAY16_TO_STREAM(p, a)                                     \
296   {                                                                 \
297     int ijk;                                                        \
298     for (ijk = 0; ijk < 16; ijk++) *(p)++ = (uint8_t)(a)[15 - ijk]; \
299   }
300 #define ARRAY8_TO_STREAM(p, a)                                    \
301   {                                                               \
302     int ijk;                                                      \
303     for (ijk = 0; ijk < 8; ijk++) *(p)++ = (uint8_t)(a)[7 - ijk]; \
304   }
305 #define LAP_TO_STREAM(p, a)                     \
306   {                                             \
307     int ijk;                                    \
308     for (ijk = 0; ijk < LAP_LEN; ijk++)         \
309       *(p)++ = (uint8_t)(a)[LAP_LEN - 1 - ijk]; \
310   }
311 #define DEVCLASS_TO_STREAM(p, a)                      \
312   {                                                   \
313     int ijk;                                          \
314     for (ijk = 0; ijk < DEV_CLASS_LEN; ijk++)         \
315       *(p)++ = (uint8_t)(a)[DEV_CLASS_LEN - 1 - ijk]; \
316   }
317 #define ARRAY_TO_STREAM(p, a, len)                                \
318   {                                                               \
319     int ijk;                                                      \
320     for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[ijk]; \
321   }
322 #define REVERSE_ARRAY_TO_STREAM(p, a, len)                                  \
323   {                                                                         \
324     int ijk;                                                                \
325     for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[(len)-1 - ijk]; \
326   }
327 
328 #define STREAM_TO_INT8(u8, p) \
329   {                           \
330     (u8) = (*((int8_t*)p));   \
331     (p) += 1;                 \
332   }
333 #define STREAM_TO_UINT8(u8, p) \
334   {                            \
335     (u8) = (uint8_t)(*(p));    \
336     (p) += 1;                  \
337   }
338 #define STREAM_TO_UINT16(u16, p)                                  \
339   {                                                               \
340     (u16) = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); \
341     (p) += 2;                                                     \
342   }
343 #define STREAM_TO_UINT24(u32, p)                                      \
344   {                                                                   \
345     (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + \
346              ((((uint32_t)(*((p) + 2)))) << 16));                     \
347     (p) += 3;                                                         \
348   }
349 #define STREAM_TO_UINT32(u32, p)                                      \
350   {                                                                   \
351     (u32) = (((uint32_t)(*(p))) + ((((uint32_t)(*((p) + 1)))) << 8) + \
352              ((((uint32_t)(*((p) + 2)))) << 16) +                     \
353              ((((uint32_t)(*((p) + 3)))) << 24));                     \
354     (p) += 4;                                                         \
355   }
356 #define STREAM_TO_ARRAY32(a, p)                     \
357   {                                                 \
358     int ijk;                                        \
359     uint8_t* _pa = (uint8_t*)(a) + 31;              \
360     for (ijk = 0; ijk < 32; ijk++) *_pa-- = *(p)++; \
361   }
362 #define STREAM_TO_ARRAY16(a, p)                     \
363   {                                                 \
364     int ijk;                                        \
365     uint8_t* _pa = (uint8_t*)(a) + 15;              \
366     for (ijk = 0; ijk < 16; ijk++) *_pa-- = *(p)++; \
367   }
368 #define STREAM_TO_ARRAY8(a, p)                     \
369   {                                                \
370     int ijk;                                       \
371     uint8_t* _pa = (uint8_t*)(a) + 7;              \
372     for (ijk = 0; ijk < 8; ijk++) *_pa-- = *(p)++; \
373   }
374 #define STREAM_TO_DEVCLASS(a, p)                               \
375   {                                                            \
376     int ijk;                                                   \
377     uint8_t* _pa = (uint8_t*)(a) + DEV_CLASS_LEN - 1;          \
378     for (ijk = 0; ijk < DEV_CLASS_LEN; ijk++) *_pa-- = *(p)++; \
379   }
380 #define STREAM_TO_LAP(a, p)                               \
381   {                                                       \
382     int ijk;                                              \
383     uint8_t* plap = (uint8_t*)(a) + LAP_LEN - 1;          \
384     for (ijk = 0; ijk < LAP_LEN; ijk++) *plap-- = *(p)++; \
385   }
386 #define STREAM_TO_ARRAY(a, p, len)                                   \
387   {                                                                  \
388     int ijk;                                                         \
389     for (ijk = 0; ijk < (len); ijk++) ((uint8_t*)(a))[ijk] = *(p)++; \
390   }
391 #define REVERSE_STREAM_TO_ARRAY(a, p, len)             \
392   {                                                    \
393     int ijk;                                           \
394     uint8_t* _pa = (uint8_t*)(a) + (len)-1;            \
395     for (ijk = 0; ijk < (len); ijk++) *_pa-- = *(p)++; \
396   }
397 
398 #define STREAM_SKIP_UINT8(p) \
399   do {                       \
400     (p) += 1;                \
401   } while (0)
402 #define STREAM_SKIP_UINT16(p) \
403   do {                        \
404     (p) += 2;                 \
405   } while (0)
406 
407 /*******************************************************************************
408  * Macros to get and put bytes to and from a field (Little Endian format).
409  * These are the same as to stream, except the pointer is not incremented.
410  */
411 #define UINT32_TO_FIELD(p, u32)                    \
412   {                                                \
413     *(uint8_t*)(p) = (uint8_t)(u32);               \
414     *((uint8_t*)(p) + 1) = (uint8_t)((u32) >> 8);  \
415     *((uint8_t*)(p) + 2) = (uint8_t)((u32) >> 16); \
416     *((uint8_t*)(p) + 3) = (uint8_t)((u32) >> 24); \
417   }
418 #define UINT24_TO_FIELD(p, u24)                    \
419   {                                                \
420     *(uint8_t*)(p) = (uint8_t)(u24);               \
421     *((uint8_t*)(p) + 1) = (uint8_t)((u24) >> 8);  \
422     *((uint8_t*)(p) + 2) = (uint8_t)((u24) >> 16); \
423   }
424 #define UINT16_TO_FIELD(p, u16)                   \
425   {                                               \
426     *(uint8_t*)(p) = (uint8_t)(u16);              \
427     *((uint8_t*)(p) + 1) = (uint8_t)((u16) >> 8); \
428   }
429 #define UINT8_TO_FIELD(p, u8) \
430   { *(uint8_t*)(p) = (uint8_t)(u8); }
431 
432 /*******************************************************************************
433  * Macros to get and put bytes to and from a stream (Big Endian format)
434  */
435 #define UINT32_TO_BE_STREAM(p, u32)  \
436   {                                  \
437     *(p)++ = (uint8_t)((u32) >> 24); \
438     *(p)++ = (uint8_t)((u32) >> 16); \
439     *(p)++ = (uint8_t)((u32) >> 8);  \
440     *(p)++ = (uint8_t)(u32);         \
441   }
442 #define UINT24_TO_BE_STREAM(p, u24)  \
443   {                                  \
444     *(p)++ = (uint8_t)((u24) >> 16); \
445     *(p)++ = (uint8_t)((u24) >> 8);  \
446     *(p)++ = (uint8_t)(u24);         \
447   }
448 #define UINT16_TO_BE_STREAM(p, u16) \
449   {                                 \
450     *(p)++ = (uint8_t)((u16) >> 8); \
451     *(p)++ = (uint8_t)(u16);        \
452   }
453 #define UINT8_TO_BE_STREAM(p, u8) \
454   { *(p)++ = (uint8_t)(u8); }
455 #define ARRAY_TO_BE_STREAM(p, a, len)                             \
456   {                                                               \
457     int ijk;                                                      \
458     for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[ijk]; \
459   }
460 #define ARRAY_TO_BE_STREAM_REVERSE(p, a, len)                               \
461   {                                                                         \
462     int ijk;                                                                \
463     for (ijk = 0; ijk < (len); ijk++) *(p)++ = (uint8_t)(a)[(len)-ijk - 1]; \
464   }
465 
466 #define BE_STREAM_TO_UINT8(u8, p) \
467   {                               \
468     (u8) = (uint8_t)(*(p));       \
469     (p) += 1;                     \
470   }
471 #define BE_STREAM_TO_UINT16(u16, p)                                       \
472   {                                                                       \
473     (u16) = (uint16_t)(((uint16_t)(*(p)) << 8) + (uint16_t)(*((p) + 1))); \
474     (p) += 2;                                                             \
475   }
476 #define BE_STREAM_TO_UINT24(u32, p)                                     \
477   {                                                                     \
478     (u32) = (((uint32_t)(*((p) + 2))) + ((uint32_t)(*((p) + 1)) << 8) + \
479              ((uint32_t)(*(p)) << 16));                                 \
480     (p) += 3;                                                           \
481   }
482 #define BE_STREAM_TO_UINT32(u32, p)                                      \
483   {                                                                      \
484     (u32) = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) +    \
485              ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \
486     (p) += 4;                                                            \
487   }
488 #define BE_STREAM_TO_UINT64(u64, p)                                            \
489   {                                                                            \
490     (u64) = ((uint64_t)(*((p) + 7)) + ((uint64_t)(*((p) + 6)) << 8) +          \
491              ((uint64_t)(*((p) + 5)) << 16) + ((uint64_t)(*((p) + 4)) << 24) + \
492              ((uint64_t)(*((p) + 3)) << 32) + ((uint64_t)(*((p) + 2)) << 40) + \
493              ((uint64_t)(*((p) + 1)) << 48) + ((uint64_t)(*(p)) << 56));       \
494     (p) += 8;                                                                  \
495   }
496 #define BE_STREAM_TO_ARRAY(p, a, len)                                \
497   {                                                                  \
498     int ijk;                                                         \
499     for (ijk = 0; ijk < (len); ijk++) ((uint8_t*)(a))[ijk] = *(p)++; \
500   }
501 
502 /*******************************************************************************
503  * Macros to get and put bytes to and from a field (Big Endian format).
504  * These are the same as to stream, except the pointer is not incremented.
505  */
506 #define UINT32_TO_BE_FIELD(p, u32)                 \
507   {                                                \
508     *(uint8_t*)(p) = (uint8_t)((u32) >> 24);       \
509     *((uint8_t*)(p) + 1) = (uint8_t)((u32) >> 16); \
510     *((uint8_t*)(p) + 2) = (uint8_t)((u32) >> 8);  \
511     *((uint8_t*)(p) + 3) = (uint8_t)(u32);         \
512   }
513 #define UINT24_TO_BE_FIELD(p, u24)                \
514   {                                               \
515     *(uint8_t*)(p) = (uint8_t)((u24) >> 16);      \
516     *((uint8_t*)(p) + 1) = (uint8_t)((u24) >> 8); \
517     *((uint8_t*)(p) + 2) = (uint8_t)(u24);        \
518   }
519 #define UINT16_TO_BE_FIELD(p, u16)          \
520   {                                         \
521     *(uint8_t*)(p) = (uint8_t)((u16) >> 8); \
522     *((uint8_t*)(p) + 1) = (uint8_t)(u16);  \
523   }
524 #define UINT8_TO_BE_FIELD(p, u8) \
525   { *(uint8_t*)(p) = (uint8_t)(u8); }
526 
527 /* Common Bluetooth field definitions */
528 #define BD_ADDR_LEN 6 /* Device address length */
529 
530 #ifdef __cplusplus
531 #include <hardware/bluetooth.h>
532 
BDADDR_TO_STREAM(uint8_t * & p,const RawAddress & a)533 inline void BDADDR_TO_STREAM(uint8_t*& p, const RawAddress& a) {
534   for (int ijk = 0; ijk < BD_ADDR_LEN; ijk++)
535     *(p)++ = (uint8_t)(a.address)[BD_ADDR_LEN - 1 - ijk];
536 }
537 
STREAM_TO_BDADDR(RawAddress & a,uint8_t * & p)538 inline void STREAM_TO_BDADDR(RawAddress& a, uint8_t*& p) {
539   uint8_t* pbda = (uint8_t*)(a.address) + BD_ADDR_LEN - 1;
540   for (int ijk = 0; ijk < BD_ADDR_LEN; ijk++) *pbda-- = *(p)++;
541 }
542 
543 #endif
544 
545 #define AMP_KEY_TYPE_GAMP 0
546 #define AMP_KEY_TYPE_WIFI 1
547 #define AMP_KEY_TYPE_UWB 2
548 typedef uint8_t tAMP_KEY_TYPE;
549 
550 #define BT_OCTET8_LEN 8
551 typedef uint8_t BT_OCTET8[BT_OCTET8_LEN]; /* octet array: size 16 */
552 
553 #define LINK_KEY_LEN 16
554 typedef uint8_t LINK_KEY[LINK_KEY_LEN]; /* Link Key */
555 
556 #define AMP_LINK_KEY_LEN 32
557 typedef uint8_t
558     AMP_LINK_KEY[AMP_LINK_KEY_LEN]; /* Dedicated AMP and GAMP Link Keys */
559 
560 #define BT_OCTET16_LEN 16
561 typedef uint8_t BT_OCTET16[BT_OCTET16_LEN]; /* octet array: size 16 */
562 
563 #define PIN_CODE_LEN 16
564 typedef uint8_t PIN_CODE[PIN_CODE_LEN]; /* Pin Code (upto 128 bits) MSB is 0 */
565 typedef uint8_t* PIN_CODE_PTR;          /* Pointer to Pin Code */
566 
567 #define BT_OCTET32_LEN 32
568 typedef uint8_t BT_OCTET32[BT_OCTET32_LEN]; /* octet array: size 32 */
569 
570 #define DEV_CLASS_LEN 3
571 typedef uint8_t DEV_CLASS[DEV_CLASS_LEN]; /* Device class */
572 typedef uint8_t* DEV_CLASS_PTR;           /* Pointer to Device class */
573 
574 #define EXT_INQ_RESP_LEN 3
575 typedef uint8_t EXT_INQ_RESP[EXT_INQ_RESP_LEN]; /* Extended Inquiry Response */
576 typedef uint8_t* EXT_INQ_RESP_PTR; /* Pointer to Extended Inquiry Response */
577 
578 #define BD_NAME_LEN 248
579 typedef uint8_t BD_NAME[BD_NAME_LEN + 1]; /* Device name */
580 typedef uint8_t* BD_NAME_PTR;             /* Pointer to Device name */
581 
582 #define BD_FEATURES_LEN 8
583 typedef uint8_t
584     BD_FEATURES[BD_FEATURES_LEN]; /* LMP features supported by device */
585 
586 #define BT_EVENT_MASK_LEN 8
587 typedef uint8_t BT_EVENT_MASK[BT_EVENT_MASK_LEN]; /* Event Mask */
588 
589 #define LAP_LEN 3
590 typedef uint8_t LAP[LAP_LEN];     /* IAC as passed to Inquiry (LAP) */
591 typedef uint8_t INQ_LAP[LAP_LEN]; /* IAC as passed to Inquiry (LAP) */
592 
593 #define RAND_NUM_LEN 16
594 typedef uint8_t RAND_NUM[RAND_NUM_LEN];
595 
596 #define ACO_LEN 12
597 typedef uint8_t ACO[ACO_LEN]; /* Authenticated ciphering offset */
598 
599 #define COF_LEN 12
600 typedef uint8_t COF[COF_LEN]; /* ciphering offset number */
601 
602 typedef struct {
603   uint8_t qos_flags;          /* TBD */
604   uint8_t service_type;       /* see below */
605   uint32_t token_rate;        /* bytes/second */
606   uint32_t token_bucket_size; /* bytes */
607   uint32_t peak_bandwidth;    /* bytes/second */
608   uint32_t latency;           /* microseconds */
609   uint32_t delay_variation;   /* microseconds */
610 } FLOW_SPEC;
611 
612 /* Values for service_type */
613 #define NO_TRAFFIC 0
614 #define BEST_EFFORT 1
615 #define GUARANTEED 2
616 
617 /* Service class of the CoD */
618 #define SERV_CLASS_NETWORKING (1 << 1)
619 #define SERV_CLASS_RENDERING (1 << 2)
620 #define SERV_CLASS_CAPTURING (1 << 3)
621 #define SERV_CLASS_OBJECT_TRANSFER (1 << 4)
622 #define SERV_CLASS_OBJECT_AUDIO (1 << 5)
623 #define SERV_CLASS_OBJECT_TELEPHONY (1 << 6)
624 #define SERV_CLASS_OBJECT_INFORMATION (1 << 7)
625 
626 /* Second byte */
627 #define SERV_CLASS_LIMITED_DISC_MODE (0x20)
628 
629 /* Field size definitions. Note that byte lengths are rounded up. */
630 #define ACCESS_CODE_BIT_LEN 72
631 #define ACCESS_CODE_BYTE_LEN 9
632 #define SHORTENED_ACCESS_CODE_BIT_LEN 68
633 
634 typedef uint8_t ACCESS_CODE[ACCESS_CODE_BYTE_LEN];
635 
636 #define SYNTH_TX 1 /* want synth code to TRANSMIT at this freq */
637 #define SYNTH_RX 2 /* want synth code to RECEIVE at this freq */
638 
639 #define SYNC_REPS 1 /* repeats of sync word transmitted to start of burst */
640 
641 #define BT_1SEC_TIMEOUT_MS (1 * 1000) /* 1 second */
642 
643 /* Maximum UUID size - 16 bytes, and structure to hold any type of UUID. */
644 #define MAX_UUID_SIZE 16
645 typedef struct {
646 #define LEN_UUID_16 2
647 #define LEN_UUID_32 4
648 #define LEN_UUID_128 16
649 
650   uint16_t len;
651 
652   union {
653     uint16_t uuid16;
654     uint32_t uuid32;
655     uint8_t uuid128[MAX_UUID_SIZE];
656   } uu;
657 
658 } tBT_UUID;
659 
660 #define BT_EIR_FLAGS_TYPE 0x01
661 #define BT_EIR_MORE_16BITS_UUID_TYPE 0x02
662 #define BT_EIR_COMPLETE_16BITS_UUID_TYPE 0x03
663 #define BT_EIR_MORE_32BITS_UUID_TYPE 0x04
664 #define BT_EIR_COMPLETE_32BITS_UUID_TYPE 0x05
665 #define BT_EIR_MORE_128BITS_UUID_TYPE 0x06
666 #define BT_EIR_COMPLETE_128BITS_UUID_TYPE 0x07
667 #define BT_EIR_SHORTENED_LOCAL_NAME_TYPE 0x08
668 #define BT_EIR_COMPLETE_LOCAL_NAME_TYPE 0x09
669 #define BT_EIR_TX_POWER_LEVEL_TYPE 0x0A
670 #define BT_EIR_OOB_BD_ADDR_TYPE 0x0C
671 #define BT_EIR_OOB_COD_TYPE 0x0D
672 #define BT_EIR_OOB_SSP_HASH_C_TYPE 0x0E
673 #define BT_EIR_OOB_SSP_RAND_R_TYPE 0x0F
674 #define BT_EIR_SERVICE_DATA_TYPE 0x16
675 #define BT_EIR_SERVICE_DATA_16BITS_UUID_TYPE 0x16
676 #define BT_EIR_SERVICE_DATA_32BITS_UUID_TYPE 0x20
677 #define BT_EIR_SERVICE_DATA_128BITS_UUID_TYPE 0x21
678 #define BT_EIR_MANUFACTURER_SPECIFIC_TYPE 0xFF
679 
680 #define BT_OOB_COD_SIZE 3
681 #define BT_OOB_HASH_C_SIZE 16
682 #define BT_OOB_RAND_R_SIZE 16
683 
684 /* Broadcom proprietary UUIDs and reserved PSMs
685  *
686  * The lowest 4 bytes byte of the UUID or GUID depend on the feature. Typically,
687  * the value of those bytes will be the PSM or SCN.
688  */
689 #define BRCM_PROPRIETARY_UUID_BASE \
690   0xDA, 0x23, 0x41, 0x02, 0xA3, 0xBB, 0xC1, 0x71, 0xBA, 0x09, 0x6f, 0x21
691 #define BRCM_PROPRIETARY_GUID_BASE \
692   0xda23, 0x4102, 0xa3, 0xbb, 0xc1, 0x71, 0xba, 0x09, 0x6f, 0x21
693 
694 /* We will not allocate a PSM in the reserved range to 3rd party apps
695  */
696 #define BRCM_RESERVED_PSM_START 0x5AE1
697 #define BRCM_RESERVED_PSM_END 0x5AFF
698 
699 #define BRCM_UTILITY_SERVICE_PSM 0x5AE1
700 #define BRCM_MATCHER_PSM 0x5AE3
701 
702 /* Connection statistics
703  */
704 
705 /* Structure to hold connection stats */
706 #ifndef BT_CONN_STATS_DEFINED
707 #define BT_CONN_STATS_DEFINED
708 
709 /* These bits are used in the bIsConnected field */
710 #define BT_CONNECTED_USING_BREDR 1
711 #define BT_CONNECTED_USING_AMP 2
712 
713 typedef struct {
714   uint32_t is_connected;
715   int32_t rssi;
716   uint32_t bytes_sent;
717   uint32_t bytes_rcvd;
718   uint32_t duration;
719 } tBT_CONN_STATS;
720 
721 #endif
722 
723 /*****************************************************************************
724  *                          Low Energy definitions
725  *
726  * Address types
727  */
728 #define BLE_ADDR_PUBLIC 0x00
729 #define BLE_ADDR_RANDOM 0x01
730 #define BLE_ADDR_PUBLIC_ID 0x02
731 #define BLE_ADDR_RANDOM_ID 0x03
732 typedef uint8_t tBLE_ADDR_TYPE;
733 #define BLE_ADDR_TYPE_MASK (BLE_ADDR_RANDOM | BLE_ADDR_PUBLIC)
734 
735 #define BT_TRANSPORT_INVALID 0
736 #define BT_TRANSPORT_BR_EDR 1
737 #define BT_TRANSPORT_LE 2
738 typedef uint8_t tBT_TRANSPORT;
739 
740 #define PHY_LE_1M_MASK 1
741 #define PHY_LE_2M_MASK 2
742 #define PHY_LE_CODED_MASK 4
743 
744 #define BLE_ADDR_IS_STATIC(x) (((x)[0] & 0xC0) == 0xC0)
745 
746 #ifdef __cplusplus
747 struct tBLE_BD_ADDR {
748   tBLE_ADDR_TYPE type;
749   RawAddress bda;
750 };
751 #endif
752 
753 /* Device Types
754  */
755 #define BT_DEVICE_TYPE_BREDR 0x01
756 #define BT_DEVICE_TYPE_BLE 0x02
757 #define BT_DEVICE_TYPE_DUMO 0x03
758 typedef uint8_t tBT_DEVICE_TYPE;
759 /*****************************************************************************/
760 
761 /* Define trace levels */
762 #define BT_TRACE_LEVEL_NONE 0    /* No trace messages to be generated    */
763 #define BT_TRACE_LEVEL_ERROR 1   /* Error condition trace messages       */
764 #define BT_TRACE_LEVEL_WARNING 2 /* Warning condition trace messages     */
765 #define BT_TRACE_LEVEL_API 3     /* API traces                           */
766 #define BT_TRACE_LEVEL_EVENT 4   /* Debug messages for events            */
767 #define BT_TRACE_LEVEL_DEBUG 5   /* Full debug messages                  */
768 #define BT_TRACE_LEVEL_VERBOSE 6 /* Verbose debug messages               */
769 
770 #define MAX_TRACE_LEVEL 6
771 
772 /* Define New Trace Type Definition */
773 /* TRACE_CTRL_TYPE                  0x^^000000*/
774 #define TRACE_CTRL_MASK 0xff000000
775 #define TRACE_GET_CTRL(x) ((((uint32_t)(x)) & TRACE_CTRL_MASK) >> 24)
776 
777 #define TRACE_CTRL_GENERAL 0x00000000
778 #define TRACE_CTRL_STR_RESOURCE 0x01000000
779 #define TRACE_CTRL_SEQ_FLOW 0x02000000
780 #define TRACE_CTRL_MAX_NUM 3
781 
782 /* LAYER SPECIFIC                   0x00^^0000*/
783 #define TRACE_LAYER_MASK 0x00ff0000
784 #define TRACE_GET_LAYER(x) ((((uint32_t)(x)) & TRACE_LAYER_MASK) >> 16)
785 
786 #define TRACE_LAYER_NONE 0x00000000
787 #define TRACE_LAYER_USB 0x00010000
788 #define TRACE_LAYER_SERIAL 0x00020000
789 #define TRACE_LAYER_SOCKET 0x00030000
790 #define TRACE_LAYER_RS232 0x00040000
791 #define TRACE_LAYER_TRANS_MAX_NUM 5
792 #define TRACE_LAYER_TRANS_ALL 0x007f0000
793 #define TRACE_LAYER_LC 0x00050000
794 #define TRACE_LAYER_LM 0x00060000
795 #define TRACE_LAYER_HCI 0x00070000
796 #define TRACE_LAYER_L2CAP 0x00080000
797 #define TRACE_LAYER_RFCOMM 0x00090000
798 #define TRACE_LAYER_SDP 0x000a0000
799 #define TRACE_LAYER_TCS 0x000b0000
800 #define TRACE_LAYER_OBEX 0x000c0000
801 #define TRACE_LAYER_BTM 0x000d0000
802 #define TRACE_LAYER_ICP 0x00110000
803 #define TRACE_LAYER_HSP2 0x00120000
804 #define TRACE_LAYER_SPP 0x00130000
805 #define TRACE_LAYER_CTP 0x00140000
806 #define TRACE_LAYER_BPP 0x00150000
807 #define TRACE_LAYER_HCRP 0x00160000
808 #define TRACE_LAYER_FTP 0x00170000
809 #define TRACE_LAYER_OPP 0x00180000
810 #define TRACE_LAYER_BTU 0x00190000
811 #define TRACE_LAYER_GKI 0x001a0000 /* OBSOLETED */
812 #define TRACE_LAYER_BNEP 0x001b0000
813 #define TRACE_LAYER_PAN 0x001c0000
814 #define TRACE_LAYER_HFP 0x001d0000
815 #define TRACE_LAYER_HID 0x001e0000
816 #define TRACE_LAYER_BIP 0x001f0000
817 #define TRACE_LAYER_AVP 0x00200000
818 #define TRACE_LAYER_A2DP 0x00210000
819 #define TRACE_LAYER_SAP 0x00220000
820 #define TRACE_LAYER_AMP 0x00230000
821 #define TRACE_LAYER_MCA 0x00240000
822 #define TRACE_LAYER_ATT 0x00250000
823 #define TRACE_LAYER_SMP 0x00260000
824 #define TRACE_LAYER_NFC 0x00270000
825 #define TRACE_LAYER_NCI 0x00280000
826 #define TRACE_LAYER_LLCP 0x00290000
827 #define TRACE_LAYER_NDEF 0x002a0000
828 #define TRACE_LAYER_RW 0x002b0000
829 #define TRACE_LAYER_CE 0x002c0000
830 #define TRACE_LAYER_P2P 0x002d0000
831 #define TRACE_LAYER_SNEP 0x002e0000
832 #define TRACE_LAYER_CHO 0x002f0000
833 #define TRACE_LAYER_NFA 0x00300000
834 
835 #define TRACE_LAYER_MAX_NUM 0x0031
836 
837 /* TRACE_ORIGINATOR                 0x0000^^00*/
838 #define TRACE_ORG_MASK 0x0000ff00
839 #define TRACE_GET_ORG(x) ((((uint32_t)(x)) & TRACE_ORG_MASK) >> 8)
840 
841 #define TRACE_ORG_STACK 0x00000000
842 #define TRACE_ORG_HCI_TRANS 0x00000100
843 #define TRACE_ORG_PROTO_DISP 0x00000200
844 #define TRACE_ORG_RPC 0x00000300
845 #define TRACE_ORG_GKI 0x00000400 /* OBSOLETED */
846 #define TRACE_ORG_APPL 0x00000500
847 #define TRACE_ORG_SCR_WRAPPER 0x00000600
848 #define TRACE_ORG_SCR_ENGINE 0x00000700
849 #define TRACE_ORG_USER_SCR 0x00000800
850 #define TRACE_ORG_TESTER 0x00000900
851 #define TRACE_ORG_MAX_NUM 10 /* 32-bit mask; must be < 32 */
852 #define TRACE_LITE_ORG_MAX_NUM 6
853 #define TRACE_ORG_ALL 0x03ff
854 #define TRACE_ORG_RPC_TRANS 0x04
855 
856 #define TRACE_ORG_REG 0x00000909
857 #define TRACE_ORG_REG_SUCCESS 0x0000090a
858 
859 /* TRACE_TYPE                       0x000000^^*/
860 #define TRACE_TYPE_MASK 0x000000ff
861 #define TRACE_GET_TYPE(x) (((uint32_t)(x)) & TRACE_TYPE_MASK)
862 
863 #define TRACE_TYPE_ERROR 0x00000000
864 #define TRACE_TYPE_WARNING 0x00000001
865 #define TRACE_TYPE_API 0x00000002
866 #define TRACE_TYPE_EVENT 0x00000003
867 #define TRACE_TYPE_DEBUG 0x00000004
868 #define TRACE_TYPE_STACK_ONLY_MAX TRACE_TYPE_DEBUG
869 #define TRACE_TYPE_TX 0x00000005
870 #define TRACE_TYPE_RX 0x00000006
871 #define TRACE_TYPE_DEBUG_ASSERT 0x00000007
872 #define TRACE_TYPE_GENERIC 0x00000008
873 #define TRACE_TYPE_REG 0x00000009
874 #define TRACE_TYPE_REG_SUCCESS 0x0000000a
875 #define TRACE_TYPE_CMD_TX 0x0000000b
876 #define TRACE_TYPE_EVT_TX 0x0000000c
877 #define TRACE_TYPE_ACL_TX 0x0000000d
878 #define TRACE_TYPE_CMD_RX 0x0000000e
879 #define TRACE_TYPE_EVT_RX 0x0000000f
880 #define TRACE_TYPE_ACL_RX 0x00000010
881 #define TRACE_TYPE_TARGET_TRACE 0x00000011
882 #define TRACE_TYPE_SCO_TX 0x00000012
883 #define TRACE_TYPE_SCO_RX 0x00000013
884 
885 #define TRACE_TYPE_MAX_NUM 20
886 #define TRACE_TYPE_ALL 0xffff
887 
888 /* Define color for script type */
889 #define SCR_COLOR_DEFAULT 0
890 #define SCR_COLOR_TYPE_COMMENT 1
891 #define SCR_COLOR_TYPE_COMMAND 2
892 #define SCR_COLOR_TYPE_EVENT 3
893 #define SCR_COLOR_TYPE_SELECT 4
894 
895 /* Define protocol trace flag values */
896 #define SCR_PROTO_TRACE_HCI_SUMMARY 0x00000001
897 #define SCR_PROTO_TRACE_HCI_DATA 0x00000002
898 #define SCR_PROTO_TRACE_L2CAP 0x00000004
899 #define SCR_PROTO_TRACE_RFCOMM 0x00000008
900 #define SCR_PROTO_TRACE_SDP 0x00000010
901 #define SCR_PROTO_TRACE_TCS 0x00000020
902 #define SCR_PROTO_TRACE_OBEX 0x00000040
903 #define SCR_PROTO_TRACE_OAPP 0x00000080 /* OBEX Application Profile */
904 #define SCR_PROTO_TRACE_AMP 0x00000100
905 #define SCR_PROTO_TRACE_BNEP 0x00000200
906 #define SCR_PROTO_TRACE_AVP 0x00000400
907 #define SCR_PROTO_TRACE_MCA 0x00000800
908 #define SCR_PROTO_TRACE_ATT 0x00001000
909 #define SCR_PROTO_TRACE_SMP 0x00002000
910 #define SCR_PROTO_TRACE_NCI 0x00004000
911 #define SCR_PROTO_TRACE_LLCP 0x00008000
912 #define SCR_PROTO_TRACE_NDEF 0x00010000
913 #define SCR_PROTO_TRACE_RW 0x00020000
914 #define SCR_PROTO_TRACE_CE 0x00040000
915 #define SCR_PROTO_TRACE_SNEP 0x00080000
916 #define SCR_PROTO_TRACE_CHO 0x00100000
917 #define SCR_PROTO_TRACE_ALL 0x001fffff
918 #define SCR_PROTO_TRACE_HCI_LOGGING_VSE \
919   0x0800 /* Brcm vs event for logmsg and protocol traces */
920 
921 #define MAX_SCRIPT_TYPE 5
922 
923 #define TCS_PSM_INTERCOM 5
924 #define TCS_PSM_CORDLESS 7
925 #define BT_PSM_BNEP 0x000F
926 /* Define PSMs HID uses */
927 #define HID_PSM_CONTROL 0x0011
928 #define HID_PSM_INTERRUPT 0x0013
929 
930 /* Define a function for logging */
931 typedef void(BT_LOG_FUNC)(int trace_type, const char* fmt_str, ...);
932 
is_sample_ltk(const BT_OCTET16 ltk)933 static inline bool is_sample_ltk(const BT_OCTET16 ltk) {
934   /* Sample LTK from BT Spec 5.1 | Vol 6, Part C 1
935    * 0x4C68384139F574D836BCF34E9DFB01BF */
936   const uint8_t SAMPLE_LTK[] = {0xbf, 0x01, 0xfb, 0x9d, 0x4e, 0xf3, 0xbc, 0x36,
937                                 0xd8, 0x74, 0xf5, 0x39, 0x41, 0x38, 0x68, 0x4c};
938   return memcmp(ltk, SAMPLE_LTK, BT_OCTET16_LEN) == 0;
939 }
940 
941 #endif
942