1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __HCI_API__H__
6 #define __HCI_API__H__
7 #include "bluetooth.h"
8 #include "stdbool.h"
9 #if defined(IBRT)
10 #include "me_api.h"
11 #endif
12 
13 /*---------------------------------------------------------------------------
14  * btif_hci_handle_t type
15  *
16  */
17 typedef uint16_t btif_hci_handle_t;
18 
19 #define BTIF_ACTIVE_BROADCAST       0x4000
20 
21 #define BTIF_PICONET_BROADCAST      0x8000
22 
23 #define BTIF_INVALID_HCI_HANDLE      0xFFFF
24 
25 #define BTIF_ACTIVE_BC_HANDLE       (0x0F01 | ACTIVE_BROADCAST)
26 
27 #define BTIF_PICONET_BC_HANDLE       (0x0F00 | PICONET_BROADCAST)
28 
29 #define BTIF_PBF_MASK               0x3000
30 
31 #define BTIF_FIRST_NON_FLUSH_PKT    0x0000
32 
33 #define BTIF_CONTINUING_PACKET      0x1000
34 
35 #define BTIF_FIRST_FLUSHABLE_PKT    0x2000
36 
37 #define BTIF_FIRST_PACKET           0x2000
38 
39 #define BTIF_SCO_DATA_CORRECT       0x0000
40 
41 #define BTIF_SCO_DATA_INVALID       0x1000
42 
43 #define BTIF_SCO_DATA_NONE          0x2000
44 
45 #define BTIF_SCO_DATA_PARTIAL       0x3000
46 
47 /* End of btif_hci_handle_t */
48 
49 /* Group: Link control commands */
50 #define  BTIF_HCC_INQUIRY                      0x0401
51 #define  BTIF_HCC_INQUIRY_CANCEL               0x0402
52 #define  BTIF_HCC_START_PERIODIC_INQ_MODE      0x0403
53 #define  BTIF_HCC_EXIT_PERIODIC_INQ_MODE       0x0404
54 #define  BTIF_HCC_CREATE_CONNECTION            0x0405
55 #define  BTIF_HCC_DISCONNECT                   0x0406
56 #define  BTIF_HCC_ADD_SCO_CONNECTION           0x0407   /* Not in 1.2 */
57 #define  BTIF_HCC_CREATE_CONNECTION_CANCEL     0x0408   /* 1.2 */
58 #define  BTIF_HCC_ACCEPT_CON_REQ               0x0409
59 #define  BTIF_HCC_REJECT_CON_REQ               0x040A
60 #define  BTIF_HCC_LINK_KEY_REQ_REPL            0x040B
61 #define  BTIF_HCC_LINK_KEY_REQ_NEG_REPL        0x040C
62 #define  BTIF_HCC_PIN_CODE_REQ_REPL            0x040D
63 #define  BTIF_HCC_PIN_CODE_REQ_NEG_REPL        0x040E
64 #define  BTIF_HCC_CHNG_CONN_PACKET_TYPE        0x040F
65 #define  BTIF_HCC_AUTH_REQ                     0x0411
66 #define  BTIF_HCC_SET_CONN_ENCRYPT             0x0413
67 #define  BTIF_HCC_CHNG_CONN_LINK_KEY           0x0415
68 #define  BTIF_HCC_MASTER_LINK_KEY              0x0417
69 #define  BTIF_HCC_REM_NAME_REQ                 0x0419
70 #define  BTIF_HCC_REM_NAME_REQ_CANCEL          0x041A   /* 1.2 */
71 #define  BTIF_HCC_READ_REMOTE_FEATURES         0x041B
72 #define  BTIF_HCC_READ_REMOTE_EXT_FEATURES     0x041C   /* 1.2 */
73 #define  BTIF_HCC_READ_REMOTE_VERSION          0x041D
74 #define  BTIF_HCC_READ_CLOCK_OFFSET            0x041F
75 #define  BTIF_HCC_READ_LMP_HANDLE              0x0420   /* 1.2 */
76 #define  BTIF_HCC_EXCHANGE_FIXED_INFO          0x0421   /* 1.2, not in 2.1 */
77 #define  BTIF_HCC_EXCHANGE_ALIAS_INFO          0x0422   /* 1.2, not in 2.1 */
78 #define  BTIF_HCC_PRIVATE_PAIRING_REQ_REPL     0x0423   /* 1.2, not in 2.1 */
79 #define  BTIF_HCC_PRIVATE_PAIRING_REQ_NEG_REPL 0x0424   /* 1.2, not in 2.1 */
80 #define  BTIF_HCC_GENERATED_ALIAS              0x0425   /* 1.2, not in 2.1 */
81 #define  BTIF_HCC_ALIAS_ADDRESS_REQ_REPL       0x0426   /* 1.2, not in 2.1 */
82 #define  BTIF_HCC_ALIAS_ADDRESS_REQ_NEG_REPL   0x0427   /* 1.2, not in 2.1 */
83 #define  BTIF_HCC_SETUP_SYNC_CONNECTION        0x0428   /* 1.2 */
84 #define  BTIF_HCC_ACCEPT_SYNC_CON_REQ          0x0429   /* 1.2 */
85 #define  BTIF_HCC_REJECT_SYNC_CON_REQ          0x042A   /* 1.2 */
86 #define  BTIF_HCC_IO_CAPABILITY_RESPONSE       0x042B   /* 2.1 */
87 #define  BTIF_HCC_USER_CONFIRM_REQ_REPL        0x042C   /* 2.1 */
88 #define  BTIF_HCC_USER_CONFIRM_REQ_NEG_REPL    0x042D   /* 2.1 */
89 #define  BTIF_HCC_USER_PASSKEY_REQ_REPL        0x042E   /* 2.1 */
90 #define  BTIF_HCC_USER_PASSKEY_REQ_NEG_REPL    0x042F   /* 2.1 */
91 #define  BTIF_HCC_REM_OOB_DATA_REQ_REPL        0x0430   /* 2.1 */
92 #define  BTIF_HCC_REM_OOB_DATA_REQ_NEG_REPL    0x0433   /* 2.1 */
93 #define  BTIF_HCC_IO_CAPABILITY_NEG_REPL       0x0434   /* 2.1 */
94 
95 /* Group: Link policy commands */
96 #define  BTIF_HCC_HOLD_MODE                    0x0801
97 #define  BTIF_HCC_SNIFF_MODE                   0x0803
98 #define  BTIF_HCC_EXIT_SNIFF_MODE              0x0804
99 #define  BTIF_HCC_PARK_MODE                    0x0805
100 #define  BTIF_HCC_EXIT_PARK_MODE               0x0806
101 #define  BTIF_HCC_QOS_SETUP                    0x0807
102 #define  BTIF_HCC_ROLE_DISCOVERY               0x0809
103 #define  BTIF_HCC_SWITCH_ROLE                  0x080B
104 #define  BTIF_HCC_READ_LINK_POLICY             0x080C
105 #define  BTIF_HCC_WRITE_LINK_POLICY            0x080D
106 #define  BTIF_HCC_READ_DEFAULT_LINK_POLICY     0x080E   /* 1.2 */
107 #define  BTIF_HCC_WRITE_DEFAULT_LINK_POLICY    0x080F   /* 1.2 */
108 #define  BTIF_HCC_FLOW_SPECIFICATION           0x0810   /* 1.2 */
109 #define  BTIF_HCC_SNIFF_SUBRATING              0x0811   /* 2.1 */
110 
111 /* Group: Host controller and baseband commands */
112 #define BTIF_HCC_SET_EVENT_MASK               0x0C01
113 #define BTIF_HCC_RESET                        0x0C03
114 #define BTIF_HCC_EVENT_FILTER                 0x0C05
115 #define BTIF_HCC_FLUSH                        0x0C08
116 #define BTIF_HCC_READ_PIN_TYPE                0x0C09
117 #define BTIF_HCC_WRITE_PIN_TYPE               0x0C0A
118 #define BTIF_HCC_CREATE_NEW_UNIT_KEY          0x0C0B
119 #define BTIF_HCC_READ_STORED_LINK_KEY         0x0C0D
120 #define BTIF_HCC_WRITE_STORED_LINK_KEY        0x0C11
121 #define BTIF_HCC_DEL_STORED_LINK_KEY          0x0C12
122 #if BTIF_BT_BEST_SYNC_CONFIG == BTIF_ENABLED
123 #define BTIF_HCC_WRITE_SYNC_CONFIG            0xFC51
124 #endif /*  */
125 #define BTIF_HCC_CHNG_LOCAL_NAME              0x0C13
126 #define BTIF_HCC_READ_LOCAL_NAME              0x0C14
127 #define BTIF_HCC_READ_CONN_ACCEPT_TIMEOUT     0x0C15
128 #define BTIF_HCC_WRITE_CONN_ACCEPT_TIMEOUT    0x0C16
129 #define BTIF_HCC_READ_PAGE_TIMEOUT            0x0C17
130 #define BTIF_HCC_WRITE_PAGE_TIMEOUT           0x0C18
131 #define BTIF_HCC_READ_SCAN_ENABLE             0x0C19
132 #define BTIF_HCC_WRITE_SCAN_ENABLE            0x0C1A
133 #define BTIF_HCC_READ_PAGE_SCAN_ACTIVITY      0x0C1B
134 #define BTIF_HCC_WRITE_PAGE_SCAN_ACTIVITY     0x0C1C
135 #define BTIF_HCC_READ_INQ_SCAN_ACTIVITY       0x0C1D
136 #define BTIF_HCC_WRITE_INQ_SCAN_ACTIVITY      0x0C1E
137 #define BTIF_HCC_READ_AUTH_ENABLE             0x0C1F
138 #define BTIF_HCC_WRITE_AUTH_ENABLE            0x0C20
139 #define BTIF_HCC_READ_ENCRYPT_MODE            0x0C21    /* Not in 2.1 */
140 #define BTIF_HCC_WRITE_ENCRYPT_MODE           0x0C22    /* Not in 2.1 */
141 #define BTIF_HCC_READ_CLASS_OF_DEVICE         0x0C23
142 #define BTIF_HCC_WRITE_CLASS_OF_DEVICE        0x0C24
143 #define BTIF_HCC_READ_VOICE_SETTING           0x0C25
144 #define BTIF_HCC_WRITE_VOICE_SETTING          0x0C26
145 #define BTIF_HCC_READ_AUTO_FLUSH_TIMEOUT      0x0C27
146 #define BTIF_HCC_WRITE_AUTO_FLUSH_TIMEOUT     0x0C28
147 #define BTIF_HCC_READ_NUM_BC_RETRANSMIT       0x0C29
148 #define BTIF_HCC_WRITE_NUM_BC_RETRANSMIT      0x0C2A
149 #define BTIF_HCC_READ_HOLD_MODE_ACTIVITY      0x0C2B
150 #define BTIF_HCC_WRITE_HOLD_MODE_ACTIVITY     0x0C2C
151 #define BTIF_HCC_READ_XMIT_POWER_LEVEL        0x0C2D
152 #define BTIF_HCC_READ_SCO_FC_ENABLE           0x0C2E
153 #define BTIF_HCC_WRITE_SCO_FC_ENABLE          0x0C2F
154 #define BTIF_HCC_SET_CTRLR_TO_HOST_FLOW_CTRL  0x0C31
155 #define BTIF_HCC_HOST_BUFFER_SIZE             0x0C33
156 #define BTIF_HCC_HOST_NUM_COMPLETED_PACKETS   0x0C35
157 #define BTIF_HCC_READ_LINK_SUPERV_TIMEOUT     0x0C36
158 #define BTIF_HCC_WRITE_LINK_SUPERV_TIMEOUT    0x0C37
159 #define BTIF_HCC_READ_NUM_IAC                 0x0C38
160 #define BTIF_HCC_READ_CURRENT_IAC_LAP         0x0C39
161 #define BTIF_HCC_WRITE_CURRENT_IAC_LAP        0x0C3A
162 #define BTIF_HCC_READ_PAGE_SCAN_PERIOD_MODE   0x0C3B    /* Not in 2.1 */
163 #define BTIF_HCC_WRITE_PAGE_SCAN_PERIOD_MODE  0x0C3C    /* Not in 2.1 */
164 #define BTIF_HCC_READ_PAGE_SCAN_MODE          0x0C3D    /* Not in 1.2 */
165 #define BTIF_HCC_WRITE_PAGE_SCAN_MODE         0x0C3E    /* Not in 1.2 */
166 #define BTIF_HCC_SET_AFH_HOST_CHAN_CLASS      0x0C3F    /* 1.2 */
167 #define BTIF_HCC_READ_INQ_SCAN_TYPE           0x0C42    /* 1.2 */
168 #define BTIF_HCC_WRITE_INQ_SCAN_TYPE          0x0C43    /* 1.2 */
169 #define BTIF_HCC_READ_INQ_MODE                0x0C44    /* 1.2 */
170 #define BTIF_HCC_WRITE_INQ_MODE               0x0C45    /* 1.2 */
171 #define BTIF_HCC_READ_PAGE_SCAN_TYPE          0x0C46    /* 1.2 */
172 #define BTIF_HCC_WRITE_PAGE_SCAN_TYPE         0x0C47    /* 1.2 */
173 #define BTIF_HCC_READ_AFH_CHAN_ASSESS_MODE    0x0C48    /* 1.2 */
174 #define BTIF_HCC_WRITE_AFH_CHAN_ASSESS_MODE   0x0C49    /* 1.2 */
175 #define BTIF_HCC_READ_ANONYMITY_MODE          0x0C4A    /* 1.2, not in 2.1 */
176 #define BTIF_HCC_WRITE_ANONYMITY_MODE         0x0C4B    /* 1.2, not in 2.1 */
177 #define BTIF_HCC_READ_ALIAS_AUTH_ENABLE       0x0C4C    /* 1.2, not in 2.1 */
178 #define BTIF_HCC_WRITE_ALIAS_AUTH_ENABLE      0x0C4D    /* 1.2, not in 2.1 */
179 #define BTIF_HCC_READ_ANON_ADDR_CHNG_PARM     0x0C4E    /* 1.2, not in 2.1 */
180 #define BTIF_HCC_WRITE_ANON_ADDR_CHNG_PARM    0x0C4F    /* 1.2, not in 2.1 */
181 #define BTIF_HCC_RESET_FIXED_ADDR_COUNTER     0x0C50    /* 1.2, not in 2.1 */
182 #define BTIF_HCC_READ_EXTENDED_INQ_RESP       0x0C51    /* 2.1 */
183 #define BTIF_HCC_WRITE_EXTENDED_INQ_RESP      0x0C52    /* 2.1 */
184 #define BTIF_HCC_REFRESH_ENCRYPT_KEY          0x0C53    /* 2.1 */
185 #define BTIF_HCC_READ_SIMPLE_PAIRING_MODE     0x0C55    /* 2.1 */
186 #define BTIF_HCC_WRITE_SIMPLE_PAIRING_MODE    0x0C56    /* 2.1 */
187 #define BTIF_HCC_READ_LOCAL_OOB_DATA          0x0C57    /* 2.1 */
188 #define BTIF_HCC_READ_INQ_RESP_TRANS_PWR_LVL  0x0C58    /* 2.1 */
189 #define BTIF_HCC_WRITE_INQ_TRANS_PWR_LVL      0x0C59    /* 2.1 */
190 #define BTIF_HCC_READ_DEF_ERR_DATA_REPORTING  0x0C5A    /* 2.1 */
191 #define BTIF_HCC_WRITE_DEF_ERR_DATA_REPORTING 0x0C5B    /* 2.1 */
192 #define BTIF_HCC_ENHANCED_FLUSH               0x0C5F    /* 2.1 */
193 #define BTIF_HCC_SEND_KEYPRESS_NOTIFY         0x0C60    /* 2.1 */
194 
195 /* Represents an invalid Host Controller handle. */
196 #define BTIF_HCI_INVALID_HANDLE      0xFFFF
197 
198 typedef uint8_t btif_hci_buf_flags_t;
199 
200 #define BTIF_HCI_COMMAND_DATA_PACKET 0x01
201 #define BTIF_HCI_ACL_DATA_PACKET     0x02
202 #define BTIF_HCI_SCO_DATA_PACKET     0x03
203 #define BTIF_HCI_EVENT_PACKET        0x04
204 #define BTIF_HCI_BUFTYPE_COMMAND      0x01
205 #define BTIF_HCI_BUFTYPE_EVENT        0x02  /* Never sent as an HciPacket */
206 #define BTIF_HCI_BUFTYPE_ACL_DATA     0x04
207 #define BTIF_HCI_BUFTYPE_SCO_DATA     0x08
208 #define BTIF_HCI_BUFTYPE_INTERMEDIATE 0x10  /* Intermediate part of an L2CAP packet */
209 #define BTIF_HCI_BUFTYPE_L2CAP        0x20  /* L2CAP owns this buffer */
210 
211 #ifdef __IAG_BLE_INCLUDE__
212 #define BTIF_HCI_BUFTYPE_BLE          0x40
213 #endif /*  */
214 
215 typedef struct {
216     list_entry_t node;          /* Node element for linked list management. */
217     uint16_t len;               /* Length of data in "buffer". */
218     uint8_t *buffer;            /* Pointer to received data buffer. */
219     uint8_t flags;              /* Flags to identify the type of packet.
220                                  * Actually uses the HciBufFlags type.
221                                  */
222 
223 #ifdef BTIF_XA_STATISTICS
224     uint32_t hci_timer;
225 
226 #endif                          /* XA_STATISTICS == XA_ENABLED */
227 } btif_hci_buffer_t;
228 
229 typedef struct {
230     uint8_t event;              /* The type of HCI event received  */
231     U8 len;                     /* Length of the event parameters */
232     U8 *parms;                  /* Event-specific parameters */
233 #ifdef __IAG_BLE_INCLUDE__
234     btif_hci_buffer_t *rx_buff; /* Received packet  */
235 #endif
236 } hci_event_t;
237 
238 
239 
240 typedef void (*btif_tws_data_received_callback_func)(uint8_t * data, uint16_t dataLen);
241 
242 typedef void (*btif_bt_hci_dbg_ll_monitor_callback_func)(const unsigned char *buf,
243                                                           unsigned int buf_len);
244 
245 typedef void (*btif_bt_hci_dbg_trace_warning_evt_code_callback_func)(const unsigned char *buf,
246                                                                       unsigned int buf_len);
247 
248 typedef btif_hci_buffer_t *btif_rx_buff_handle_t;
249 
250 typedef void (*btif_tws_role_switch_cmp_callback_func) (uint16_t conhdl, uint8_t ret);
251 
252 typedef void (*btif_bt_addr_exchanged_callback_func) (uint8_t * newBtAddr);
253 
254 typedef void (*btif_hci_vendor_event_callback_func)(uint8_t* pbuf, uint32_t length);
255 
256 #if defined(IBRT)
257 typedef void (*btif_ibrt_connect_ind_callback_func)(uint16_t conhdl,uint8_t ret);
258 
259 typedef void (*btif_ibrt_disconnect_ind_callback_func)(uint16_t conhdl,uint8_t ret);
260 
261 typedef void (*btif_ibrt_switch_cmp_callback_func)(uint16_t conhdl,uint8_t role);
262 
263 typedef void (*btif_ibrt_env_init_cmp_callback_func)(uint8_t sniffer_role);
264 
265 typedef void (*btif_ibrt_prase_data_callback_func)(btif_hci_buffer_t* hcibuffer,uint16_t conhdl);
266 #endif
267 
268 #ifdef __cplusplus
269 extern "C" {
270 #endif
271 
272     bool btif_hci_Is_any_pending_hci_xfer(void);
273 
274     void btif_HciBypassProcessReceivedDataExt(uint32_t(*ext_fn) (void));
275 
276     void btif_hci_send_complete_packet_command_right_now(uint16_t handle, uint8_t num);
277 
278     list_entry_t *btif_hci_get_rx_buffer_list(void);
279 
280     void btif_hci_rxbuffer_free(btif_rx_buff_handle_t rxBuffHandle);
281 
282     void btif_register_hci_ll_monitor_evt_code_callback(
283                                     btif_bt_hci_dbg_ll_monitor_callback_func func);
284 
285     void btif_register_hci_dbg_trace_warning_evt_code_callback
286                         (btif_bt_hci_dbg_trace_warning_evt_code_callback_func func);
287 
288     unsigned short btif_hci_h4_receive_msg_generic(bool isSimulated, const uint8_t * buf,
289                                                    uint32_t size);
290 
291     void btif_hci_clean_host_num_of_completed_packet(uint16_t connHanlde);
292 
293     bool btif_hci_restore_new_master_num_of_completed_packet(uint16_t connHanlde);
294 
295     void btif_register_tws_role_switch_cmp_callback(btif_tws_role_switch_cmp_callback_func func);
296 
297     void btif_register_bt_addr_exchanged_callback(btif_bt_addr_exchanged_callback_func func);
298 
299     uint32_t btif_hci_current_rx_packet_complete(void);
300 
301     uint32_t btif_hci_current_rx_aclfreelist_cnt(void);
302 
303     uint32_t btif_hci_get_acl_buffers_num(void);
304 
305     void *btif_hci_get_PktsCompleteTimerHandler(void);
306 
307     int btif_hci_init(void);
308 
309     void btif_register_vendor_event_callback(btif_hci_vendor_event_callback_func func);
310     uint8_t* btif_hci_peek_hci_tx_buf(uint8_t tx_type,uint16_t handle);
311     void btif_hci_ahead_hci_tx_buf(uint8_t* hci_buf);
312 
313 #if defined(IBRT)
314     uint8_t btif_hci_disconnect_connection_direct(uint16_t conn_handle, uint8_t reason);
315 	uint8_t btif_hci_tws_switch_direct(uint8_t op);
316     uint32_t btif_save_hci_ctx(uint8_t * ctx_buffer, uint16_t dev_id);
317     uint32_t btif_set_hci_ctx(uint8_t* ctx_buffer, uint16_t dev_id);
318     uint16_t btif_hci_get_con_hci_handle(uint8_t con_idx);
319     uint8_t btif_hci_get_con_conflags(uint8_t con_idx);
320     void btif_hci_register_tx_buf_tss_process_callback(btif_callback_ext3 cb);
321 
322 #endif
323 
324 #ifdef __cplusplus
325 }
326 #endif
327 #endif /*__HCI_API__H__*/
328 
329