1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 #ifndef __BLUETOOTH_H__
5 #define __BLUETOOTH_H__
6 #include "stdint.h"
7 #include "string.h"
8 #include "btif_sys_config.h"
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #ifndef BOOL_DEFINED
15 typedef unsigned int BOOL;      /* IGNORESTYLE */
16 #endif
17 
18 typedef unsigned int U32;
19 typedef unsigned short U16;
20 typedef unsigned char U8;
21 
22 typedef int S32;
23 typedef short S16;
24 typedef char S8;
25 
26 #ifndef U32_PTR_DEFINED
27 typedef U32 U32_PTR;
28 
29 #define U32_PTR_DEFINED
30 #endif /* U32_PTR_DEFINED */
31 
32 /* Variable sized integers. Used to optimize processor efficiency by
33  * using the most efficient data size for counters, arithmatic, etc.
34  */
35 typedef unsigned long I32;
36 
37 #ifndef XA_INTEGER_SIZE
38 #define XA_INTEGER_SIZE    4
39 #endif
40 
41 #if XA_INTEGER_SIZE == 4
42 typedef unsigned long I16;
43 typedef unsigned long I8;
44 #elif XA_INTEGER_SIZE == 2
45 typedef unsigned short I16;
46 typedef unsigned short I8;
47 #elif XA_INTEGER_SIZE == 1
48 typedef unsigned short I16;
49 typedef unsigned char I8;
50 #else
51 #error No XA_INTEGER_SIZE specified!
52 #endif
53 
54 typedef void (*PFV) (void);
55 
56 /* Boolean Definitions */
57 #ifndef TRUE
58 #define TRUE  (1==1)
59 #endif /* TRUE */
60 
61 #ifndef FALSE
62 #define FALSE (0==1)
63 #endif /* FALSE */
64 
65 #ifndef TimeT
66 typedef U32 TimeT;
67 #endif
68 
69 #ifndef BtPriority
70 typedef U8 BtPriority;
71 #endif
72 
73 /** Bluetooth Address */
74 typedef struct {
75     uint8_t address[6];
76 } __attribute__ ((packed)) bt_bdaddr_t;
77 
78 #define BTIF_BD_ADDR_SIZE    6
79 #define BTIF_LINK_KEY_SIZE   16
80 typedef struct _list_entr {
81     struct _list_entr *Flink;
82     struct _list_entr *Blink;
83     unsigned int resv;
84 } list_entry_t;
85 
86 /*---------------------------------------------------------------------------
87  *
88  *  Doubly-linked list manipulation routines.  Some are implemented as
89  *  macros but logically are procedures.
90  */
91 #ifndef BTIF_LIST_MACROS
92 //void InitializeListHead(ListEntry *head);
93 #define initialize_list_head(ListHead) (\
94     (ListHead)->Flink = (ListHead)->Blink = (ListHead) )
95 
96 #define initialize_list_entry(Entry) (\
97     (Entry)->Flink = (Entry)->Blink = 0 )
98 
99 #define is_entry_available(Entry) (\
100     ((Entry)->Flink == 0))
101 
102 #ifndef is_list_empty
103 //BOOL is_list_empty(ListEntry *head);
104 #define is_list_empty(ListHead) (\
105     ((ListHead)->Flink == (ListHead)))
106 #endif
107 
108 #define get_head_list(ListHead) (ListHead)->Flink
109 
110 #define get_tail_list(ListHead) (ListHead)->Blink
111 
112 #define get_next_node(Node)     (Node)->Flink
113 
114 #define get_prior_node(Node)    (Node)->Blink
115 
116 #define is_node_connected(n) (((n)->Blink->Flink == (n)) && ((n)->Flink->Blink == (n)))
117 BOOL is_list_circular(list_entry_t * list);
118 
119 #define list_assert(exp) (ASSERT(exp, "%s %s, %d\n", #exp, __func__, __LINE__))
120 
121 //void InsertTailList(ListEntry *head, ListEntry *entry);
122 void _insert_tail_list(list_entry_t * head, list_entry_t * entry);
123 
124 #define insert_tail_list(a, b) (list_assert(is_list_circular(a)), \
125                             _insert_tail_list(a, b), \
126                             list_assert(is_list_circular(a)))
127 
128 void insert_head_list(list_entry_t * head, list_entry_t * entry);
129 void _insert_head_list(list_entry_t * head, list_entry_t * entry);
130 
131 #define insert_head_list(a, b) (list_assert(is_list_circular(a)), \
132                             _insert_head_list(a, b), \
133                             list_assert(is_list_circular(a)))
134 
135 list_entry_t *remove_head_list(list_entry_t * head);
136 list_entry_t *_remove_head_list(list_entry_t * head);
137 
138 #define remove_head_list(a) (list_assert(is_list_circular(a)), \
139                             _remove_head_list(a))
140 
141 void remove_entry_list(list_entry_t * entry);
142 BOOL is_node_on_list(list_entry_t * head, list_entry_t * node);
143 U8 get_list_number(list_entry_t * head);
144 BOOL is_list_circular(list_entry_t * list);
145 void move_list(list_entry_t * dest, list_entry_t * src);
146 #endif
147 
148 #define iterate_list_safe(head, cur, next, type) \
149     for ( (cur) = (type) get_head_list(head) ; \
150           (next) = (type) get_next_node(&(cur)->node), \
151             (cur) != (type) (head); \
152           (cur) = (next))
153 
154 /*---------------------------------------------------------------------------
155  * IterateList()
156  *
157  *     Sets up ordinary traversal of a list. The current member must NOT
158  *     be removed during iteration. Must be followed by a block of code
159  *     containing the body of the iteration.
160  *
161  *     For example:
162  *     BtSecurityRecord *record;
163  *     IterateList(MEC(secList), record, BtSecurityRecord *) {
164  *         [...do something with "record", but do not remove it!...]
165  *     }
166  *
167  * Parameters:
168  *     head - Head of list (address of ListEntry structure)
169  *     cur - Variable to use for current list member
170  *     type - Structure type of cur and next.
171  */
172 #define iterate_list(head, cur, type) \
173     for ( (cur) = (type) get_head_list(&(head)) ; \
174           (cur) != (type) &(head); \
175           (cur) = (type) get_next_node(&(cur)->node) )
176 
177 enum _bt_status {
178     BT_STS_SUCCESS = 0,
179     BT_STS_FAILED = 1,
180     BT_STS_PENDING = 2,
181     BT_STS_BUSY = 11,
182     BT_STS_NO_RESOURCES = 12,
183     BT_STS_NOT_FOUND = 13,
184     BT_STS_DEVICE_NOT_FOUND = 14,
185     BT_STS_CONNECTION_FAILED = 15,
186     BT_STS_TIMEOUT = 16,
187     BT_STS_NO_CONNECTION = 17,
188     BT_STS_INVALID_PARM = 18,
189     BT_STS_IN_PROGRESS = 19,
190     BT_STS_RESTRICTED = 20,
191     BT_STS_INVALID_TYPE = 21,
192     BT_STS_HCI_INIT_ERR = 22,
193     BT_STS_NOT_SUPPORTED = 23,
194     BT_STS_IN_USE = 5,
195     BT_STS_SDP_CONT_STATE = 24,
196     BT_STS_CONTINUE =24,
197     BT_STS_CANCELLED = 25,
198 
199     /* The last defined status code */
200     BT_STS_LAST_CODE = BT_STS_CANCELLED,
201 };
202 
203 typedef uint32_t bt_status_t;
204 
205 typedef struct _evm_timer evm_timer_t;
206 typedef void (*evm_timer_notify) (evm_timer_t *);
207 
208 struct _evm_timer {
209     list_entry_t node;          /* Used internally by the Event Manager */
210     void *context;              /* Context area for use by callers */
211     evm_timer_notify func;      /* Function to call when timer fires */
212 
213     /* === Internal use only === */
214     TimeT time;                 /* Amount of time to wait */
215     TimeT startTime;            /* System time when the timer started */
216 };
217 
218 /*---------------------------------------------------------------------------
219  * btif_packet_flags type
220  *
221  *     This type is used by L2CAP and protocols that use directly L2CAP
222  *     to manage the status of a particular BtPacket.
223  */
224 typedef uint16_t btif_packet_flags;
225 
226 #define BTIF_BTP_FLAG_NONE       0x0000  /* No current flags */
227 #define BTIF_BTP_FLAG_INUSE      0x0001  /* Used only by packet owner */
228 #define BTIF_BTP_FLAG_LSYSTEM    0x0002  /* Used only by L2CAP */
229 #define BTIF_BTP_FLAG_TAIL       0x0004  /* Used only by L2CAP Applications */
230 #define BTIF_BTP_FLAG_RDEV       0x0008  /* Used only by L2CAP */
231 #define BTIF_BTP_FLAG_FCS        0x0010  /* FCS field is valid, set only by L2CAP */
232 #define BTIF_BTP_FLAG_NON_FLUSH  0x0020  /* Used by L2CAP, HCI or packet owner */
233 #define BTIF_BTP_FLAG_ENHANCED   0x0040  /* Used only by L2CAP */
234 #define BTIF_BTP_FLAG_SEGMENTED  0x0080  /* Used only by L2CAP */
235 #define BTIF_BTP_FLAG_TXDONE     0x0100  /* Used only by L2CAP */
236 #define BTIF_BTP_FLAG_USER       0x0200  /* Used only by L2CAP */
237 #define BTIF_BTP_FLAG_IMMEDIATE  0x0400  /* Used only by L2CAP */
238 
239 /* End of btif_packet_flags */
240 
241 #define BTIF_BT_PACKET_HEADER_LEN 25
242 
243 typedef struct {
244     list_entry_t node;
245     uint8_t *data;              /* Points to a buffer of user data.  */
246     uint16_t dataLen;           /* Indicates the length of "data" in bytes. */
247     uint16_t flags;             /* Must be initialized to BTIF_BTP_FLAG_NONE by
248                                  * applications running on top of L2CAP.
249                                  */
250 
251 #if   BTIF_L2CAP_PRIORITY == BTIF_ENABLED
252     BtPriority priority;
253 #endif
254 
255     /* Group: The following fields are for internal use only by the stack. */
256     void *ulpContext;
257     uint8_t *tail;
258     uint16_t tailLen;
259 
260 #ifdef  BTIF_XA_STATISTICS
261     U32 rfc_timer;
262     U32 hci_timer;
263     U32 l2cap_timer;
264 
265 #endif
266     uint16_t llpContext;
267     uint16_t remoteCid;
268 
269 #if  BTIF_L2CAP_NUM_ENHANCED_CHANNELS > 0
270     uint8_t segStart;
271     uint16_t segNum;
272     uint16_t segCount;
273     uint8_t fcs[2];
274 
275 #endif
276     uint8_t hciPackets;
277     uint8_t headerLen;
278     uint8_t header[BTIF_BT_PACKET_HEADER_LEN];
279 } btif_bt_packet_t;
280 
281 /*---------------------------------------------------------------------------
282  *            le_to_host16()
283  *---------------------------------------------------------------------------
284  *
285  * Synopsis:  Retrieve a 16-bit number from the given buffer. The number
286  *            is in Little-Endian format.
287  *
288  * Return:    16-bit number.
289  */
290 U16 le_to_host16(const U8 * ptr);
291 
292 /*---------------------------------------------------------------------------
293  *            be_to_host16()
294  *---------------------------------------------------------------------------
295  *
296  * Synopsis:  Retrieve a 16-bit number from the given buffer. The number
297  *            is in Big-Endian format.
298  *
299  * Return:    16-bit number.
300  */
301 U16 be_to_host16(const U8* ptr);
302 /*---------------------------------------------------------------------------
303  *            be_to_host32()
304  *---------------------------------------------------------------------------
305  *
306  * Synopsis:  Retrieve a 32-bit number from the given buffer. The number
307  *            is in Big-Endian format.
308  *
309  * Return:    32-bit number.
310  */
311 U32 be_to_host32(const U8* ptr);
312 /*---------------------------------------------------------------------------
313  *            store_le16()
314  *---------------------------------------------------------------------------
315  *
316  * Synopsis:  Store 16 bit value into a buffer in Little Endian format.
317  *
318  * Return:    void
319  */
320 void store_le16(U8 *buff, U16 le_value);
321 
322 /*---------------------------------------------------------------------------
323  *            store_le32()
324  *---------------------------------------------------------------------------
325  *
326  * Synopsis:  Store 32 bit value into a buffer in Little Endian format.
327  *
328  * Return:    void
329  */
330 void store_le32(U8 *buff, U32 le_value);
331 
332 /*---------------------------------------------------------------------------
333  *            store_be16()
334  *---------------------------------------------------------------------------
335  *
336  * Synopsis:  Store 16 bit value into a buffer in Big Endian format.
337  *
338  * Return:    void
339  */
340 void store_be16(U8 *buff, U16 be_value);
341 
342 /*---------------------------------------------------------------------------
343  *            store_be32()
344  *---------------------------------------------------------------------------
345  *
346  * Synopsis:  Store 32 bit value into a buffer in Big Endian format.
347  *
348  * Return:    void
349  */
350 void store_be32(U8 *buff, U32 be_value);
351 
352 #if defined(ENHANCED_STACK)
353 /* Copy, compare bluetooth Address */
ba_cmp(const bt_bdaddr_t * ba1,const bt_bdaddr_t * ba2)354 static inline int ba_cmp(const bt_bdaddr_t *ba1, const bt_bdaddr_t *ba2)
355 {
356     return memcmp(ba1, ba2, sizeof(bt_bdaddr_t ));
357 }
358 
ba_cpy(bt_bdaddr_t * dst,const bt_bdaddr_t * src)359 static inline void ba_cpy( bt_bdaddr_t *dst, const bt_bdaddr_t *src)
360 {
361     memcpy(dst, src, sizeof(bt_bdaddr_t ));
362 }
363 
364 #define BTIF_CTX_INIT(buff) \
365     POSSIBLY_UNUSED unsigned int __offset = 2; \
366     POSSIBLY_UNUSED unsigned char *__buff = buff;
367 
368 #define BTIF_CTX_STR_BUF(buff,len) \
369     memcpy(__buff+__offset, buff, len); \
370     __offset += len;
371 
372 #define BTIF_CTX_LDR_BUF(buff,len) \
373     memcpy(buff, __buff+__offset, len); \
374     __offset += len;
375 
376 #define BTIF_CTX_STR_VAL8(v) \
377     __buff[__offset] = v&0xFF; \
378     __offset += 1;
379 
380 #define BTIF_CTX_LDR_VAL8(v) \
381     v = __buff[__offset]; \
382     __offset += 1;
383 
384 #define BTIF_CTX_STR_VAL16(v) \
385     __buff[__offset] = v&0xFF; \
386     __buff[__offset+1] = (v>>8)&0xFF; \
387     __offset += 2;
388 
389 #define BTIF_CTX_LDR_VAL16(v) \
390     v = __buff[__offset]; \
391     v |= __buff[__offset+1]<<8; \
392     __offset += 2;
393 
394 #define BTIF_CTX_STR_VAL32(v) \
395     __buff[__offset] = v&0xFF; \
396     __buff[__offset+1] = (v>>8)&0xFF; \
397     __buff[__offset+2] = (v>>16)&0xFF; \
398     __buff[__offset+3] = (v>>24)&0xFF; \
399     __offset += 4;
400 
401 #define BTIF_CTX_LDR_VAL32(v) \
402     v = __buff[__offset]; \
403     v |= __buff[__offset+1]<<8; \
404     v |= __buff[__offset+2]<<16; \
405     v |= __buff[__offset+3]<<24; \
406     __offset += 4;
407 
408 #define BTIF_CTX_GET_BUF_CURR() __buff
409 
410 #define BTIF_CTX_GET_BUF_HEAD() __buff
411 
412 #define BTIF_CTX_GET_OFFSET() __offset
413 
414 #define BTIF_CTX_GET_DATA_LEN() (__buff[0] | __buff[1]<<8)
415 
416 #define BTIF_CTX_GET_TOTAL_LEN() (BTIF_CTX_GET_DATA_LEN()+2)
417 
418 #define BTIF_CTX_SAVE_UPDATE_DATA_LEN() \
419    __buff[0] = (__offset-2)&0xFF; \
420    __buff[1] = ((__offset-2)>>8)&0xFF;
421 
422 struct btif_ctx_content {
423     unsigned char *buff;
424     unsigned int buff_len;
425 };
426 #endif /* ENHANCED_STACK */
427 
428 #ifdef __cplusplus
429 }
430 #endif                          /*  */
431 #endif /*__BLUETOOTH_H__*/
432