OSDN Git Service

23204a789c88690bedb81410478044aa7bbd307b
[android-x86/system-bt.git] / stack / btu / btu_hcif.c
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 /******************************************************************************
20  *
21  *  This file contains functions that interface with the HCI transport. On
22  *  the receive side, it routes events to the appropriate handler, e.g.
23  *  L2CAP, ScoMgr. On the transmit side, it manages the command
24  *  transmission.
25  *
26  ******************************************************************************/
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "gki.h"
33 #include "bt_types.h"
34 #include "hcimsgs.h"
35 #include "btu.h"
36 #include "l2c_int.h"
37 #include "btm_api.h"
38 #include "btm_int.h"
39
40 extern void btm_process_cancel_complete(UINT8 status, UINT8 mode);
41 extern void btm_ble_test_command_complete(UINT8 *p);
42
43 // btla-specific ++
44 #define LOG_TAG "BTLD"
45 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE) && (!defined(LINUX_NATIVE)) )
46 #include <cutils/log.h>
47 #else
48 #define LOGV(format, ...)  fprintf (stdout, LOG_TAG format"\n", ## __VA_ARGS__)
49 #define LOGE(format, ...)  fprintf (stderr, LOG_TAG format"\n", ## __VA_ARGS__)
50 #define LOGI(format, ...)  fprintf (stdout, LOG_TAG format"\n", ## __VA_ARGS__)
51 #endif
52
53 // btla-specific ++
54 /* BTE application task */
55 #if APPL_INCLUDED == TRUE
56 #include "bte_appl.h"
57 #endif
58 // btla-specific --
59
60 //Counter to track number of HCI command timeout
61 static int num_hci_cmds_timed_out;
62
63 /********************************************************************************/
64 /*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
65 /********************************************************************************/
66 static void btu_hcif_inquiry_comp_evt (UINT8 *p);
67 static void btu_hcif_inquiry_result_evt (UINT8 *p);
68 static void btu_hcif_inquiry_rssi_result_evt (UINT8 *p);
69 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
70 static void btu_hcif_extended_inquiry_result_evt (UINT8 *p);
71 #endif
72
73 static void btu_hcif_connection_comp_evt (UINT8 *p);
74 static void btu_hcif_connection_request_evt (UINT8 *p);
75 static void btu_hcif_disconnection_comp_evt (UINT8 *p);
76 static void btu_hcif_authentication_comp_evt (UINT8 *p);
77 static void btu_hcif_rmt_name_request_comp_evt (UINT8 *p, UINT16 evt_len);
78 static void btu_hcif_encryption_change_evt (UINT8 *p);
79 static void btu_hcif_change_conn_link_key_evt (UINT8 *p);
80 static void btu_hcif_master_link_key_comp_evt (UINT8 *p);
81 static void btu_hcif_read_rmt_features_comp_evt (UINT8 *p);
82 static void btu_hcif_read_rmt_ext_features_comp_evt (UINT8 *p);
83 static void btu_hcif_read_rmt_version_comp_evt (UINT8 *p);
84 static void btu_hcif_qos_setup_comp_evt (UINT8 *p);
85 static void btu_hcif_command_complete_evt (UINT8 controller_id, UINT8 *p, UINT16 evt_len);
86 static void btu_hcif_command_status_evt (UINT8 controller_id, UINT8 *p);
87 static void btu_hcif_hardware_error_evt (UINT8 *p);
88 static void btu_hcif_flush_occured_evt (void);
89 static void btu_hcif_role_change_evt (UINT8 *p);
90 static void btu_hcif_num_compl_data_pkts_evt (UINT8 *p);
91 static void btu_hcif_mode_change_evt (UINT8 *p);
92 static void btu_hcif_return_link_keys_evt (UINT8 *p);
93 static void btu_hcif_pin_code_request_evt (UINT8 *p);
94 static void btu_hcif_link_key_request_evt (UINT8 *p);
95 static void btu_hcif_link_key_notification_evt (UINT8 *p);
96 static void btu_hcif_loopback_command_evt (void);
97 static void btu_hcif_data_buf_overflow_evt (void);
98 static void btu_hcif_max_slots_changed_evt (void);
99 static void btu_hcif_read_clock_off_comp_evt (UINT8 *p);
100 static void btu_hcif_conn_pkt_type_change_evt (void);
101 static void btu_hcif_qos_violation_evt (UINT8 *p);
102 static void btu_hcif_page_scan_mode_change_evt (void);
103 static void btu_hcif_page_scan_rep_mode_chng_evt (void);
104 static void btu_hcif_esco_connection_comp_evt(UINT8 *p);
105 static void btu_hcif_esco_connection_chg_evt(UINT8 *p);
106
107 /* Simple Pairing Events */
108 static void btu_hcif_host_support_evt (UINT8 *p);
109 static void btu_hcif_io_cap_request_evt (UINT8 *p);
110 static void btu_hcif_io_cap_response_evt (UINT8 *p);
111 static void btu_hcif_user_conf_request_evt (UINT8 *p);
112 static void btu_hcif_user_passkey_request_evt (UINT8 *p);
113 static void btu_hcif_user_passkey_notif_evt (UINT8 *p);
114 static void btu_hcif_keypress_notif_evt (UINT8 *p);
115 static void btu_hcif_link_super_tout_evt (UINT8 *p);
116
117     #if BTM_OOB_INCLUDED == TRUE
118 static void btu_hcif_rem_oob_request_evt (UINT8 *p);
119     #endif
120
121 static void btu_hcif_simple_pair_complete_evt (UINT8 *p);
122     #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
123 static void btu_hcif_enhanced_flush_complete_evt (void);
124     #endif
125
126     #if (BTM_SSR_INCLUDED == TRUE)
127 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len);
128     #endif /* BTM_SSR_INCLUDED == TRUE */
129
130     #if (HID_DEV_INCLUDED == TRUE) && (HID_DEV_PM_INCLUDED == TRUE)
131 extern void hidd_pm_proc_mode_change( UINT8 hci_status, UINT8 mode, UINT16 interval );
132     #endif
133
134
135     #if BLE_INCLUDED == TRUE
136 static void btu_ble_ll_conn_complete_evt (UINT8 *p, UINT16 evt_len);
137 static void btu_ble_process_adv_pkt (UINT8 *p);
138 static void btu_ble_read_remote_feat_evt (UINT8 *p);
139 static void btu_ble_ll_conn_param_upd_evt (UINT8 *p);
140 static void btu_ble_proc_ltk_req (UINT8 *p);
141 static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p);
142 #if (BLE_LLT_INCLUDED == TRUE)
143 static void btu_ble_rc_param_req_evt(UINT8 *p);
144 #endif
145     #endif
146 /*******************************************************************************
147 **
148 ** Function         btu_hcif_store_cmd
149 **
150 ** Description      This function stores a copy of an outgoing command and
151 **                  and sets a timer waiting for a event in response to the
152 **                  command.
153 **
154 ** Returns          void
155 **
156 *******************************************************************************/
157 static void btu_hcif_store_cmd (UINT8 controller_id, BT_HDR *p_buf)
158 {
159     tHCI_CMD_CB *p_hci_cmd_cb;
160     UINT16  opcode;
161     BT_HDR  *p_cmd;
162     UINT8   *p;
163
164     /* Validate controller ID */
165     if (controller_id >= BTU_MAX_LOCAL_CTRLS)
166         return;
167
168     p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
169     p = (UINT8 *)(p_buf + 1) + p_buf->offset;
170
171     /* get command opcode */
172     STREAM_TO_UINT16 (opcode, p);
173
174     /* don't do anything for certain commands */
175     if ((opcode == HCI_RESET) || (opcode == HCI_HOST_NUM_PACKETS_DONE))
176     {
177         return;
178     }
179
180     /* allocate buffer (HCI_GET_CMD_BUF will either get a buffer from HCI_CMD_POOL or from 'best-fit' pool) */
181     if ((p_cmd = HCI_GET_CMD_BUF(p_buf->len + p_buf->offset - HCIC_PREAMBLE_SIZE)) == NULL)
182     {
183         return;
184     }
185
186     /* copy buffer */
187     memcpy (p_cmd, p_buf, sizeof(BT_HDR));
188
189     /* If vendor specific save the callback function */
190     if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
191 #if BLE_INCLUDED == TRUE
192         || (opcode == HCI_BLE_RAND )
193         || (opcode == HCI_BLE_ENCRYPT)
194 #endif
195        )
196     {
197         memcpy ((UINT8 *)(p_cmd + 1), (UINT8 *)(p_buf + 1), sizeof(void *));
198     }
199
200     memcpy ((UINT8 *)(p_cmd + 1) + p_cmd->offset,
201             (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
202
203     /* queue copy of cmd */
204     GKI_enqueue(&(p_hci_cmd_cb->cmd_cmpl_q), p_cmd);
205
206     /* start timer */
207     if (BTU_CMD_CMPL_TIMEOUT > 0)
208     {
209 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
210         p_hci_cmd_cb->checked_hcisu = FALSE;
211 #endif
212         btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
213                          (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
214                          BTU_CMD_CMPL_TIMEOUT);
215     }
216 }
217
218 /*******************************************************************************
219 **
220 ** Function         btu_hcif_process_event
221 **
222 ** Description      This function is called when an event is received from
223 **                  the Host Controller.
224 **
225 ** Returns          void
226 **
227 *******************************************************************************/
228 void btu_hcif_process_event (UINT8 controller_id, BT_HDR *p_msg)
229 {
230     UINT8   *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
231     UINT8   hci_evt_code, hci_evt_len;
232 #if BLE_INCLUDED == TRUE
233     UINT8   ble_sub_code;
234 #endif
235     STREAM_TO_UINT8  (hci_evt_code, p);
236     STREAM_TO_UINT8  (hci_evt_len, p);
237
238     switch (hci_evt_code)
239     {
240         case HCI_INQUIRY_COMP_EVT:
241             btu_hcif_inquiry_comp_evt (p);
242             break;
243         case HCI_INQUIRY_RESULT_EVT:
244             btu_hcif_inquiry_result_evt (p);
245             break;
246         case HCI_INQUIRY_RSSI_RESULT_EVT:
247             btu_hcif_inquiry_rssi_result_evt (p);
248             break;
249 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
250         case HCI_EXTENDED_INQUIRY_RESULT_EVT:
251             btu_hcif_extended_inquiry_result_evt (p);
252             break;
253 #endif
254         case HCI_CONNECTION_COMP_EVT:
255             btu_hcif_connection_comp_evt (p);
256             break;
257         case HCI_CONNECTION_REQUEST_EVT:
258             btu_hcif_connection_request_evt (p);
259             break;
260         case HCI_DISCONNECTION_COMP_EVT:
261             btu_hcif_disconnection_comp_evt (p);
262             break;
263         case HCI_AUTHENTICATION_COMP_EVT:
264             btu_hcif_authentication_comp_evt (p);
265             break;
266         case HCI_RMT_NAME_REQUEST_COMP_EVT:
267             btu_hcif_rmt_name_request_comp_evt (p, hci_evt_len);
268             break;
269         case HCI_ENCRYPTION_CHANGE_EVT:
270             btu_hcif_encryption_change_evt (p);
271             break;
272 #if BLE_INCLUDED == TRUE
273         case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
274             btu_hcif_encryption_key_refresh_cmpl_evt(p);
275             break;
276 #endif
277         case HCI_CHANGE_CONN_LINK_KEY_EVT:
278             btu_hcif_change_conn_link_key_evt (p);
279             break;
280         case HCI_MASTER_LINK_KEY_COMP_EVT:
281             btu_hcif_master_link_key_comp_evt (p);
282             break;
283         case HCI_READ_RMT_FEATURES_COMP_EVT:
284             btu_hcif_read_rmt_features_comp_evt (p);
285             break;
286         case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
287             btu_hcif_read_rmt_ext_features_comp_evt (p);
288             break;
289         case HCI_READ_RMT_VERSION_COMP_EVT:
290             btu_hcif_read_rmt_version_comp_evt (p);
291             break;
292         case HCI_QOS_SETUP_COMP_EVT:
293             btu_hcif_qos_setup_comp_evt (p);
294             break;
295         case HCI_COMMAND_COMPLETE_EVT:
296             btu_hcif_command_complete_evt (controller_id, p, hci_evt_len);
297             break;
298         case HCI_COMMAND_STATUS_EVT:
299             btu_hcif_command_status_evt (controller_id, p);
300             break;
301         case HCI_HARDWARE_ERROR_EVT:
302             btu_hcif_hardware_error_evt (p);
303             break;
304         case HCI_FLUSH_OCCURED_EVT:
305             btu_hcif_flush_occured_evt ();
306             break;
307         case HCI_ROLE_CHANGE_EVT:
308             btu_hcif_role_change_evt (p);
309             break;
310         case HCI_NUM_COMPL_DATA_PKTS_EVT:
311             btu_hcif_num_compl_data_pkts_evt (p);
312             break;
313         case HCI_MODE_CHANGE_EVT:
314             btu_hcif_mode_change_evt (p);
315             break;
316         case HCI_RETURN_LINK_KEYS_EVT:
317             btu_hcif_return_link_keys_evt (p);
318             break;
319         case HCI_PIN_CODE_REQUEST_EVT:
320             btu_hcif_pin_code_request_evt (p);
321             break;
322         case HCI_LINK_KEY_REQUEST_EVT:
323             btu_hcif_link_key_request_evt (p);
324             break;
325         case HCI_LINK_KEY_NOTIFICATION_EVT:
326             btu_hcif_link_key_notification_evt (p);
327             break;
328         case HCI_LOOPBACK_COMMAND_EVT:
329             btu_hcif_loopback_command_evt ();
330             break;
331         case HCI_DATA_BUF_OVERFLOW_EVT:
332             btu_hcif_data_buf_overflow_evt ();
333             break;
334         case HCI_MAX_SLOTS_CHANGED_EVT:
335             btu_hcif_max_slots_changed_evt ();
336             break;
337         case HCI_READ_CLOCK_OFF_COMP_EVT:
338             btu_hcif_read_clock_off_comp_evt (p);
339             break;
340         case HCI_CONN_PKT_TYPE_CHANGE_EVT:
341             btu_hcif_conn_pkt_type_change_evt ();
342             break;
343         case HCI_QOS_VIOLATION_EVT:
344             btu_hcif_qos_violation_evt (p);
345             break;
346         case HCI_PAGE_SCAN_MODE_CHANGE_EVT:
347             btu_hcif_page_scan_mode_change_evt ();
348             break;
349         case HCI_PAGE_SCAN_REP_MODE_CHNG_EVT:
350             btu_hcif_page_scan_rep_mode_chng_evt ();
351             break;
352         case HCI_ESCO_CONNECTION_COMP_EVT:
353             btu_hcif_esco_connection_comp_evt (p);
354             break;
355         case HCI_ESCO_CONNECTION_CHANGED_EVT:
356             btu_hcif_esco_connection_chg_evt (p);
357             break;
358 #if (BTM_SSR_INCLUDED == TRUE)
359         case HCI_SNIFF_SUB_RATE_EVT:
360             btu_hcif_ssr_evt (p, hci_evt_len);
361             break;
362 #endif  /* BTM_SSR_INCLUDED == TRUE */
363         case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
364             btu_hcif_host_support_evt (p);
365             break;
366         case HCI_IO_CAPABILITY_REQUEST_EVT:
367             btu_hcif_io_cap_request_evt (p);
368             break;
369         case HCI_IO_CAPABILITY_RESPONSE_EVT:
370             btu_hcif_io_cap_response_evt (p);
371             break;
372         case HCI_USER_CONFIRMATION_REQUEST_EVT:
373             btu_hcif_user_conf_request_evt (p);
374             break;
375         case HCI_USER_PASSKEY_REQUEST_EVT:
376             btu_hcif_user_passkey_request_evt (p);
377             break;
378 #if BTM_OOB_INCLUDED == TRUE
379         case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
380             btu_hcif_rem_oob_request_evt (p);
381             break;
382 #endif
383         case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
384             btu_hcif_simple_pair_complete_evt (p);
385             break;
386         case HCI_USER_PASSKEY_NOTIFY_EVT:
387             btu_hcif_user_passkey_notif_evt (p);
388             break;
389         case HCI_KEYPRESS_NOTIFY_EVT:
390             btu_hcif_keypress_notif_evt (p);
391             break;
392         case HCI_LINK_SUPER_TOUT_CHANGED_EVT:
393             btu_hcif_link_super_tout_evt (p);
394             break;
395 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
396         case HCI_ENHANCED_FLUSH_COMPLETE_EVT:
397             btu_hcif_enhanced_flush_complete_evt ();
398             break;
399 #endif
400
401 #if (BLE_INCLUDED == TRUE)
402         case HCI_BLE_EVENT:
403             STREAM_TO_UINT8  (ble_sub_code, p);
404
405             HCI_TRACE_EVENT("BLE HCI(id=%d) event = 0x%02x)", hci_evt_code,  ble_sub_code);
406
407             switch (ble_sub_code)
408             {
409                 case HCI_BLE_ADV_PKT_RPT_EVT: /* result of inquiry */
410                     btu_ble_process_adv_pkt(p);
411                     break;
412                 case HCI_BLE_CONN_COMPLETE_EVT:
413                     btu_ble_ll_conn_complete_evt(p, hci_evt_len);
414                     break;
415                 case HCI_BLE_LL_CONN_PARAM_UPD_EVT:
416                     btu_ble_ll_conn_param_upd_evt(p);
417                     break;
418                 case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
419                     btu_ble_read_remote_feat_evt(p);
420                     break;
421                 case HCI_BLE_LTK_REQ_EVT: /* received only at slave device */
422                     btu_ble_proc_ltk_req(p);
423                     break;
424 #if (BLE_LLT_INCLUDED == TRUE)
425                case HCI_BLE_RC_PARAM_REQ_EVT:
426                     btu_ble_rc_param_req_evt(p);
427                     break;
428 #endif
429
430             }
431             break;
432 #endif /* BLE_INCLUDED */
433         case HCI_VENDOR_SPECIFIC_EVT:
434                 btm_vendor_specific_evt (p, hci_evt_len);
435             break;
436     }
437     // reset the  num_hci_cmds_timed_out upon receving any event from controller.
438     num_hci_cmds_timed_out = 0;
439 }
440
441
442 /*******************************************************************************
443 **
444 ** Function         btu_hcif_send_cmd
445 **
446 ** Description      This function is called to check if it can send commands
447 **                  to the Host Controller. It may be passed the address of
448 **                  a packet to send.
449 **
450 ** Returns          void
451 **
452 *******************************************************************************/
453 void btu_hcif_send_cmd (UINT8 controller_id, BT_HDR *p_buf)
454 {
455     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
456
457 #if ((L2CAP_HOST_FLOW_CTRL == TRUE)||defined(HCI_TESTER))
458     UINT8 *pp;
459     UINT16 code;
460 #endif
461
462     /* If there are already commands in the queue, then enqueue this command */
463     if ((p_buf) && (p_hci_cmd_cb->cmd_xmit_q.count))
464     {
465         GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
466         p_buf = NULL;
467     }
468
469     /* Allow for startup case, where no acks may be received */
470     if ( ((controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
471          && (p_hci_cmd_cb->cmd_window == 0)
472          && (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT)) )
473     {
474         p_hci_cmd_cb->cmd_window = p_hci_cmd_cb->cmd_xmit_q.count + 1;
475     }
476
477     /* See if we can send anything */
478     while (p_hci_cmd_cb->cmd_window != 0)
479     {
480         if (!p_buf)
481             p_buf = (BT_HDR *)GKI_dequeue (&(p_hci_cmd_cb->cmd_xmit_q));
482
483         if (p_buf)
484         {
485             btu_hcif_store_cmd(controller_id, p_buf);
486
487 #if ((L2CAP_HOST_FLOW_CTRL == TRUE)||defined(HCI_TESTER))
488             pp = (UINT8 *)(p_buf + 1) + p_buf->offset;
489
490             STREAM_TO_UINT16 (code, pp);
491
492             /*
493              * We do not need to decrease window for host flow control,
494              * host flow control does not receive an event back from controller
495              */
496             if (code != HCI_HOST_NUM_PACKETS_DONE)
497 #endif
498                 p_hci_cmd_cb->cmd_window--;
499
500             if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
501             {
502                 HCI_CMD_TO_LOWER(p_buf);
503             }
504             else
505             {
506                 /* Unknown controller */
507                 HCI_TRACE_WARNING("BTU HCI(ctrl id=%d) controller ID not recognized", controller_id);
508                 GKI_freebuf(p_buf);;
509             }
510
511             p_buf = NULL;
512         }
513         else
514             break;
515     }
516
517     if (p_buf)
518         GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
519
520 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
521     if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
522     {
523         /* check if controller can go to sleep */
524         btu_check_bt_sleep ();
525     }
526 #endif
527
528 }
529
530
531 /*******************************************************************************
532 **
533 ** Function         btu_hcif_send_host_rdy_for_data
534 **
535 ** Description      This function is called to check if it can send commands
536 **                  to the Host Controller. It may be passed the address of
537 **                  a packet to send.
538 **
539 ** Returns          void
540 **
541 *******************************************************************************/
542 void btu_hcif_send_host_rdy_for_data(void)
543 {
544     UINT16      num_pkts[MAX_L2CAP_LINKS + 4];      /* 3 SCO connections */
545     UINT16      handles[MAX_L2CAP_LINKS + 4];
546     UINT8       num_ents;
547
548     /* Get the L2CAP numbers */
549     num_ents = l2c_link_pkts_rcvd (num_pkts, handles);
550
551     /* Get the SCO numbers */
552     /* No SCO for now ?? */
553
554     if (num_ents)
555     {
556         btsnd_hcic_host_num_xmitted_pkts (num_ents, handles, num_pkts);
557     }
558 }
559
560 /*******************************************************************************
561 **
562 ** Function         btu_hcif_inquiry_comp_evt
563 **
564 ** Description      Process event HCI_INQUIRY_COMP_EVT
565 **
566 ** Returns          void
567 **
568 *******************************************************************************/
569 static void btu_hcif_inquiry_comp_evt (UINT8 *p)
570 {
571     UINT8   status;
572
573     STREAM_TO_UINT8    (status, p);
574
575     /* Tell inquiry processing that we are done */
576     btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
577 }
578
579
580 /*******************************************************************************
581 **
582 ** Function         btu_hcif_inquiry_result_evt
583 **
584 ** Description      Process event HCI_INQUIRY_RESULT_EVT
585 **
586 ** Returns          void
587 **
588 *******************************************************************************/
589 static void btu_hcif_inquiry_result_evt (UINT8 *p)
590 {
591     /* Store results in the cache */
592     btm_process_inq_results (p, BTM_INQ_RESULT_STANDARD);
593 }
594
595 /*******************************************************************************
596 **
597 ** Function         btu_hcif_inquiry_rssi_result_evt
598 **
599 ** Description      Process event HCI_INQUIRY_RSSI_RESULT_EVT
600 **
601 ** Returns          void
602 **
603 *******************************************************************************/
604 static void btu_hcif_inquiry_rssi_result_evt (UINT8 *p)
605 {
606     /* Store results in the cache */
607     btm_process_inq_results (p, BTM_INQ_RESULT_WITH_RSSI);
608 }
609
610 /*******************************************************************************
611 **
612 ** Function         btu_hcif_extended_inquiry_result_evt
613 **
614 ** Description      Process event HCI_EXTENDED_INQUIRY_RESULT_EVT
615 **
616 ** Returns          void
617 **
618 *******************************************************************************/
619 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
620 static void btu_hcif_extended_inquiry_result_evt (UINT8 *p)
621 {
622     /* Store results in the cache */
623     btm_process_inq_results (p, BTM_INQ_RESULT_EXTENDED);
624 }
625 #endif
626
627 /*******************************************************************************
628 **
629 ** Function         btu_hcif_connection_comp_evt
630 **
631 ** Description      Process event HCI_CONNECTION_COMP_EVT
632 **
633 ** Returns          void
634 **
635 *******************************************************************************/
636 static void btu_hcif_connection_comp_evt (UINT8 *p)
637 {
638     UINT8       status;
639     UINT16      handle;
640     BD_ADDR     bda;
641     UINT8       link_type;
642     UINT8       enc_mode;
643 #if BTM_SCO_INCLUDED == TRUE
644     tBTM_ESCO_DATA  esco_data;
645 #endif
646
647     STREAM_TO_UINT8    (status, p);
648     STREAM_TO_UINT16   (handle, p);
649     STREAM_TO_BDADDR   (bda, p);
650     STREAM_TO_UINT8    (link_type, p);
651     STREAM_TO_UINT8    (enc_mode, p);
652
653     handle = HCID_GET_HANDLE (handle);
654
655     if (link_type == HCI_LINK_TYPE_ACL)
656     {
657         btm_sec_connected (bda, handle, status, enc_mode);
658
659         l2c_link_hci_conn_comp (status, handle, bda);
660     }
661 #if BTM_SCO_INCLUDED == TRUE
662     else
663     {
664         memset(&esco_data, 0, sizeof(tBTM_ESCO_DATA));
665         /* esco_data.link_type = HCI_LINK_TYPE_SCO; already zero */
666         memcpy (esco_data.bd_addr, bda, BD_ADDR_LEN);
667         btm_sco_connected (status, bda, handle, &esco_data);
668     }
669 #endif /* BTM_SCO_INCLUDED */
670 }
671
672
673 /*******************************************************************************
674 **
675 ** Function         btu_hcif_connection_request_evt
676 **
677 ** Description      Process event HCI_CONNECTION_REQUEST_EVT
678 **
679 ** Returns          void
680 **
681 *******************************************************************************/
682 static void btu_hcif_connection_request_evt (UINT8 *p)
683 {
684     BD_ADDR     bda;
685     DEV_CLASS   dc;
686     UINT8       link_type;
687
688     STREAM_TO_BDADDR   (bda, p);
689     STREAM_TO_DEVCLASS (dc, p);
690     STREAM_TO_UINT8    (link_type, p);
691
692     /* Pass request to security manager to check connect filters before */
693     /* passing request to l2cap */
694     if (link_type == HCI_LINK_TYPE_ACL)
695     {
696         btm_sec_conn_req (bda, dc);
697     }
698 #if BTM_SCO_INCLUDED == TRUE
699     else
700     {
701         btm_sco_conn_req (bda, dc, link_type);
702     }
703 #endif /* BTM_SCO_INCLUDED */
704 }
705
706
707 /*******************************************************************************
708 **
709 ** Function         btu_hcif_disconnection_comp_evt
710 **
711 ** Description      Process event HCI_DISCONNECTION_COMP_EVT
712 **
713 ** Returns          void
714 **
715 *******************************************************************************/
716 static void btu_hcif_disconnection_comp_evt (UINT8 *p)
717 {
718     UINT8   status;
719     UINT16  handle;
720     UINT8   reason;
721
722     STREAM_TO_UINT8  (status, p);
723     STREAM_TO_UINT16 (handle, p);
724     STREAM_TO_UINT8  (reason, p);
725
726     handle = HCID_GET_HANDLE (handle);
727
728 #if BTM_SCO_INCLUDED == TRUE
729     /* If L2CAP doesn't know about it, send it to SCO */
730     if (!l2c_link_hci_disc_comp (handle, reason))
731         btm_sco_removed (handle, reason);
732 #else
733     l2c_link_hci_disc_comp (handle, reason);
734 #endif /* BTM_SCO_INCLUDED */
735
736     /* Notify security manager */
737     btm_sec_disconnected (handle, reason);
738 }
739
740 /*******************************************************************************
741 **
742 ** Function         btu_hcif_authentication_comp_evt
743 **
744 ** Description      Process event HCI_AUTHENTICATION_COMP_EVT
745 **
746 ** Returns          void
747 **
748 *******************************************************************************/
749 static void btu_hcif_authentication_comp_evt (UINT8 *p)
750 {
751     UINT8   status;
752     UINT16  handle;
753
754     STREAM_TO_UINT8  (status, p);
755     STREAM_TO_UINT16 (handle, p);
756
757     btm_sec_auth_complete (handle, status);
758 }
759
760
761 /*******************************************************************************
762 **
763 ** Function         btu_hcif_rmt_name_request_comp_evt
764 **
765 ** Description      Process event HCI_RMT_NAME_REQUEST_COMP_EVT
766 **
767 ** Returns          void
768 **
769 *******************************************************************************/
770 static void btu_hcif_rmt_name_request_comp_evt (UINT8 *p, UINT16 evt_len)
771 {
772     UINT8   status;
773     BD_ADDR bd_addr;
774
775     STREAM_TO_UINT8 (status, p);
776     STREAM_TO_BDADDR (bd_addr, p);
777
778     evt_len -= (1 + BD_ADDR_LEN);
779
780     btm_process_remote_name (bd_addr, p, evt_len, status);
781
782     btm_sec_rmt_name_request_complete (bd_addr, p, status);
783 }
784
785
786 /*******************************************************************************
787 **
788 ** Function         btu_hcif_encryption_change_evt
789 **
790 ** Description      Process event HCI_ENCRYPTION_CHANGE_EVT
791 **
792 ** Returns          void
793 **
794 *******************************************************************************/
795 static void btu_hcif_encryption_change_evt (UINT8 *p)
796 {
797     UINT8   status;
798     UINT16  handle;
799     UINT8   encr_enable;
800
801     STREAM_TO_UINT8  (status, p);
802     STREAM_TO_UINT16 (handle, p);
803     STREAM_TO_UINT8  (encr_enable, p);
804
805     btm_acl_encrypt_change (handle, status, encr_enable);
806     btm_sec_encrypt_change (handle, status, encr_enable);
807 }
808
809
810 /*******************************************************************************
811 **
812 ** Function         btu_hcif_change_conn_link_key_evt
813 **
814 ** Description      Process event HCI_CHANGE_CONN_LINK_KEY_EVT
815 **
816 ** Returns          void
817 **
818 *******************************************************************************/
819 static void btu_hcif_change_conn_link_key_evt (UINT8 *p)
820 {
821     UINT8   status;
822     UINT16  handle;
823
824     STREAM_TO_UINT8  (status, p);
825     STREAM_TO_UINT16 (handle, p);
826
827     btm_acl_link_key_change (handle, status);
828 }
829
830
831 /*******************************************************************************
832 **
833 ** Function         btu_hcif_master_link_key_comp_evt
834 **
835 ** Description      Process event HCI_MASTER_LINK_KEY_COMP_EVT
836 **
837 ** Returns          void
838 **
839 *******************************************************************************/
840 static void btu_hcif_master_link_key_comp_evt (UINT8 *p)
841 {
842     UINT8   status;
843     UINT16  handle;
844     UINT8   key_flg;
845
846     STREAM_TO_UINT8  (status, p);
847     STREAM_TO_UINT16 (handle, p);
848     STREAM_TO_UINT8  (key_flg, p);
849
850     btm_sec_mkey_comp_event (handle, status, key_flg);
851 }
852
853
854 /*******************************************************************************
855 **
856 ** Function         btu_hcif_read_rmt_features_comp_evt
857 **
858 ** Description      Process event HCI_READ_RMT_FEATURES_COMP_EVT
859 **
860 ** Returns          void
861 **
862 *******************************************************************************/
863 static void btu_hcif_read_rmt_features_comp_evt (UINT8 *p)
864 {
865     btm_read_remote_features_complete(p);
866 }
867
868 /*******************************************************************************
869 **
870 ** Function         btu_hcif_read_rmt_ext_features_comp_evt
871 **
872 ** Description      Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
873 **
874 ** Returns          void
875 **
876 *******************************************************************************/
877 static void btu_hcif_read_rmt_ext_features_comp_evt (UINT8 *p)
878 {
879     UINT8 *p_cur = p;
880     UINT8 status;
881     UINT16 handle;
882
883     STREAM_TO_UINT8 (status, p_cur);
884
885     if (status == HCI_SUCCESS)
886         btm_read_remote_ext_features_complete(p);
887     else
888     {
889         STREAM_TO_UINT16 (handle, p_cur);
890         btm_read_remote_ext_features_failed(status, handle);
891     }
892 }
893
894 /*******************************************************************************
895 **
896 ** Function         btu_hcif_read_rmt_version_comp_evt
897 **
898 ** Description      Process event HCI_READ_RMT_VERSION_COMP_EVT
899 **
900 ** Returns          void
901 **
902 *******************************************************************************/
903 static void btu_hcif_read_rmt_version_comp_evt (UINT8 *p)
904 {
905     btm_read_remote_version_complete (p);
906 }
907
908
909 /*******************************************************************************
910 **
911 ** Function         btu_hcif_qos_setup_comp_evt
912 **
913 ** Description      Process event HCI_QOS_SETUP_COMP_EVT
914 **
915 ** Returns          void
916 **
917 *******************************************************************************/
918 static void btu_hcif_qos_setup_comp_evt (UINT8 *p)
919 {
920     UINT8 status;
921     UINT16 handle;
922     FLOW_SPEC flow;
923
924     STREAM_TO_UINT8 (status, p);
925     STREAM_TO_UINT16 (handle, p);
926     STREAM_TO_UINT8 (flow.qos_flags, p);
927     STREAM_TO_UINT8 (flow.service_type, p);
928     STREAM_TO_UINT32 (flow.token_rate, p);
929     STREAM_TO_UINT32 (flow.peak_bandwidth, p);
930     STREAM_TO_UINT32 (flow.latency, p);
931     STREAM_TO_UINT32 (flow.delay_variation, p);
932
933     btm_qos_setup_complete(status, handle, &flow);
934 }
935
936
937 /*******************************************************************************
938 **
939 ** Function         btu_hcif_esco_connection_comp_evt
940 **
941 ** Description      Process event HCI_ESCO_CONNECTION_COMP_EVT
942 **
943 ** Returns          void
944 **
945 *******************************************************************************/
946 static void btu_hcif_esco_connection_comp_evt (UINT8 *p)
947 {
948 #if BTM_SCO_INCLUDED == TRUE
949     tBTM_ESCO_DATA  data;
950     UINT16          handle;
951     BD_ADDR         bda;
952     UINT8           status;
953
954     STREAM_TO_UINT8 (status, p);
955     STREAM_TO_UINT16 (handle, p);
956     STREAM_TO_BDADDR (bda, p);
957
958     STREAM_TO_UINT8 (data.link_type, p);
959     STREAM_TO_UINT8 (data.tx_interval, p);
960     STREAM_TO_UINT8 (data.retrans_window, p);
961     STREAM_TO_UINT16 (data.rx_pkt_len, p);
962     STREAM_TO_UINT16 (data.tx_pkt_len, p);
963     STREAM_TO_UINT8 (data.air_mode, p);
964
965     memcpy (data.bd_addr, bda, BD_ADDR_LEN);
966     btm_sco_connected (status, bda, handle, &data);
967 #endif
968 }
969
970
971 /*******************************************************************************
972 **
973 ** Function         btu_hcif_esco_connection_chg_evt
974 **
975 ** Description      Process event HCI_ESCO_CONNECTION_CHANGED_EVT
976 **
977 ** Returns          void
978 **
979 *******************************************************************************/
980 static void btu_hcif_esco_connection_chg_evt (UINT8 *p)
981 {
982 #if BTM_SCO_INCLUDED == TRUE
983     UINT16  handle;
984     UINT16  tx_pkt_len;
985     UINT16  rx_pkt_len;
986     UINT8   status;
987     UINT8   tx_interval;
988     UINT8   retrans_window;
989
990     STREAM_TO_UINT8 (status, p);
991     STREAM_TO_UINT16 (handle, p);
992
993     STREAM_TO_UINT8 (tx_interval, p);
994     STREAM_TO_UINT8 (retrans_window, p);
995     STREAM_TO_UINT16 (rx_pkt_len, p);
996     STREAM_TO_UINT16 (tx_pkt_len, p);
997
998     btm_esco_proc_conn_chg (status, handle, tx_interval, retrans_window,
999                             rx_pkt_len, tx_pkt_len);
1000 #endif
1001 }
1002
1003 /*******************************************************************************
1004 **
1005 ** Function         btu_hcif_hdl_command_complete
1006 **
1007 ** Description      Handle command complete event
1008 **
1009 ** Returns          void
1010 **
1011 *******************************************************************************/
1012 static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_len,
1013                                            void *p_cplt_cback)
1014 {
1015     switch (opcode)
1016     {
1017         case HCI_RESET:
1018             btm_reset_complete ();  /* BR/EDR */
1019             break;
1020
1021         case HCI_INQUIRY_CANCEL:
1022             /* Tell inquiry processing that we are done */
1023             btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
1024             break;
1025         case HCI_SET_EVENT_FILTER:
1026             btm_event_filter_complete (p);
1027             break;
1028
1029         case HCI_READ_STORED_LINK_KEY:
1030             btm_read_stored_link_key_complete (p);
1031             break;
1032
1033         case HCI_WRITE_STORED_LINK_KEY:
1034             btm_write_stored_link_key_complete (p);
1035             break;
1036
1037         case HCI_DELETE_STORED_LINK_KEY:
1038             btm_delete_stored_link_key_complete (p);
1039             break;
1040
1041         case HCI_READ_LOCAL_VERSION_INFO:
1042             btm_read_local_version_complete (p, evt_len);
1043             break;
1044
1045         case HCI_READ_POLICY_SETTINGS:
1046             btm_read_link_policy_complete (p);
1047             break;
1048
1049         case HCI_READ_BUFFER_SIZE:
1050             btm_read_hci_buf_size_complete (p, evt_len);
1051             break;
1052
1053         case HCI_READ_LOCAL_SUPPORTED_CMDS:
1054             btm_read_local_supported_cmds_complete (p);
1055             break;
1056
1057         case HCI_READ_LOCAL_FEATURES:
1058             btm_read_local_features_complete (p, evt_len);
1059             break;
1060
1061         case HCI_READ_LOCAL_EXT_FEATURES:
1062             btm_read_local_ext_features_complete (p, evt_len);
1063             break;
1064
1065         case HCI_READ_LOCAL_NAME:
1066             btm_read_local_name_complete (p, evt_len);
1067             break;
1068
1069         case HCI_READ_BD_ADDR:
1070             btm_read_local_addr_complete (p, evt_len);
1071             break;
1072
1073         case HCI_GET_LINK_QUALITY:
1074             btm_read_link_quality_complete (p);
1075             break;
1076
1077         case HCI_READ_RSSI:
1078             btm_read_rssi_complete (p);
1079             break;
1080
1081         case HCI_READ_TRANSMIT_POWER_LEVEL:
1082             btm_read_tx_power_complete(p, FALSE);
1083             break;
1084
1085         case HCI_CREATE_CONNECTION_CANCEL:
1086             btm_create_conn_cancel_complete(p);
1087             break;
1088
1089         case HCI_READ_LOCAL_OOB_DATA:
1090 #if BTM_OOB_INCLUDED == TRUE
1091             btm_read_local_oob_complete(p);
1092 #endif
1093             break;
1094
1095
1096         case HCI_READ_INQ_TX_POWER_LEVEL:
1097             btm_read_linq_tx_power_complete (p);
1098             break;
1099
1100         case HCI_WRITE_SIMPLE_PAIRING_MODE:
1101             btm_write_simple_paring_mode_complete (p);
1102             break;
1103
1104         case HCI_WRITE_LE_HOST_SUPPORTED:
1105             btm_write_le_host_supported_complete (p);
1106             break;
1107
1108 #if (BLE_INCLUDED == TRUE)
1109 /* BLE Commands */
1110         case HCI_BLE_READ_WHITE_LIST_SIZE :
1111             btm_read_white_list_size_complete(p, evt_len);
1112             break;
1113
1114         case HCI_BLE_ADD_WHITE_LIST:
1115             btm_ble_add_2_white_list_complete(*p);
1116             break;
1117
1118         case HCI_BLE_CLEAR_WHITE_LIST:
1119             btm_ble_clear_white_list_complete(p, evt_len);
1120             break;
1121
1122         case HCI_BLE_REMOVE_WHITE_LIST:
1123             btm_ble_remove_from_white_list_complete(p, evt_len);
1124             break;
1125
1126         case HCI_BLE_RAND:
1127         case HCI_BLE_ENCRYPT:
1128
1129             btm_ble_rand_enc_complete (p, opcode, (tBTM_RAND_ENC_CB *)p_cplt_cback);
1130             break;
1131         case HCI_BLE_READ_BUFFER_SIZE:
1132             btm_read_ble_buf_size_complete(p, evt_len);
1133             break;
1134
1135         case HCI_BLE_READ_LOCAL_SPT_FEAT:
1136             btm_read_ble_local_supported_features_complete(p, evt_len);
1137             break;
1138
1139         case HCI_BLE_READ_ADV_CHNL_TX_POWER:
1140             btm_read_tx_power_complete(p, TRUE);
1141             break;
1142
1143         case HCI_BLE_WRITE_ADV_ENABLE:
1144             btm_ble_write_adv_enable_complete(p);
1145             break;
1146
1147         case HCI_BLE_READ_SUPPORTED_STATES:
1148             btm_read_ble_local_supported_states_complete(p, evt_len);
1149             break;
1150
1151         case HCI_BLE_TRANSMITTER_TEST:
1152         case HCI_BLE_RECEIVER_TEST:
1153         case HCI_BLE_TEST_END:
1154             btm_ble_test_command_complete(p);
1155             break;
1156 #endif /* (BLE_INCLUDED == TRUE) */
1157
1158         default:
1159             if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1160                 btm_vsc_complete (p, opcode, evt_len, (tBTM_CMPL_CB *)p_cplt_cback);
1161             break;
1162     }
1163 }
1164
1165 /*******************************************************************************
1166 **
1167 ** Function         btu_hcif_command_complete_evt
1168 **
1169 ** Description      Process event HCI_COMMAND_COMPLETE_EVT
1170 **
1171 ** Returns          void
1172 **
1173 *******************************************************************************/
1174 static void btu_hcif_command_complete_evt (UINT8 controller_id, UINT8 *p, UINT16 evt_len)
1175 {
1176     tHCI_CMD_CB *p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1177     UINT16      cc_opcode;
1178     BT_HDR      *p_cmd;
1179     void        *p_cplt_cback = NULL;
1180
1181     STREAM_TO_UINT8  (p_hci_cmd_cb->cmd_window, p);
1182
1183 #if (defined(HCI_MAX_SIMUL_CMDS) && (HCI_MAX_SIMUL_CMDS > 0))
1184     if (p_hci_cmd_cb->cmd_window > HCI_MAX_SIMUL_CMDS)
1185         p_hci_cmd_cb->cmd_window = HCI_MAX_SIMUL_CMDS;
1186 #endif
1187
1188     STREAM_TO_UINT16 (cc_opcode, p);
1189
1190     evt_len -= 3;
1191
1192     /* only do this for certain commands */
1193     if ((cc_opcode != HCI_RESET) && (cc_opcode != HCI_HOST_NUM_PACKETS_DONE) &&
1194         (cc_opcode != HCI_COMMAND_NONE))
1195     {
1196         /* dequeue and free stored command */
1197
1198 /* always use cmd code check, when one cmd timeout waiting for cmd_cmpl,
1199    it'll cause the rest of the command goes in wrong order                  */
1200         p_cmd = (BT_HDR *) GKI_getfirst (&p_hci_cmd_cb->cmd_cmpl_q);
1201
1202         while (p_cmd)
1203         {
1204             UINT16 opcode_dequeued;
1205             UINT8  *p_dequeued;
1206
1207             /* Make sure dequeued command is for the command_cplt received */
1208             p_dequeued = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1209             STREAM_TO_UINT16 (opcode_dequeued, p_dequeued);
1210
1211             if (opcode_dequeued != cc_opcode)
1212             {
1213                 /* opcode does not match, check next command in the queue */
1214                 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
1215                 continue;
1216             }
1217             GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
1218
1219             /* If command was a VSC, then extract command_complete callback */
1220             if ((cc_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
1221 #if BLE_INCLUDED == TRUE
1222                 || (cc_opcode == HCI_BLE_RAND )
1223                 || (cc_opcode == HCI_BLE_ENCRYPT)
1224 #endif
1225                )
1226             {
1227                 p_cplt_cback = *((void **)(p_cmd + 1));
1228             }
1229
1230             GKI_freebuf (p_cmd);
1231
1232             break;
1233         }
1234
1235         /* if more commands in queue restart timer */
1236         if (BTU_CMD_CMPL_TIMEOUT > 0)
1237         {
1238             if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1239             {
1240 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
1241                 p_hci_cmd_cb->checked_hcisu = FALSE;
1242 #endif
1243                 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1244                                  (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1245                                  BTU_CMD_CMPL_TIMEOUT);
1246             }
1247             else
1248             {
1249                 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
1250             }
1251         }
1252     }
1253
1254     /* handle event */
1255     btu_hcif_hdl_command_complete (cc_opcode, p, evt_len, p_cplt_cback);
1256
1257     /* see if we can send more commands */
1258     btu_hcif_send_cmd (controller_id, NULL);
1259 }
1260
1261
1262 /*******************************************************************************
1263 **
1264 ** Function         btu_hcif_hdl_command_status
1265 **
1266 ** Description      Handle a command status event
1267 **
1268 ** Returns          void
1269 **
1270 *******************************************************************************/
1271 static void btu_hcif_hdl_command_status (UINT16 opcode, UINT8 status, UINT8 *p_cmd,
1272                                          void *p_vsc_status_cback)
1273 {
1274     BD_ADDR         bd_addr;
1275     UINT16          handle;
1276 #if BTM_SCO_INCLUDED == TRUE
1277     tBTM_ESCO_DATA  esco_data;
1278 #endif
1279
1280 #if BTM_PWR_MGR_INCLUDED == TRUE
1281     switch (opcode)
1282     {
1283         case HCI_EXIT_SNIFF_MODE:
1284         case HCI_EXIT_PARK_MODE:
1285 #if BTM_SCO_WAKE_PARKED_LINK == TRUE
1286             if (status != HCI_SUCCESS)
1287             {
1288                 /* Allow SCO initiation to continue if waiting for change mode event */
1289                 if (p_cmd != NULL)
1290                 {
1291                     p_cmd++;    /* bypass length field */
1292                     STREAM_TO_UINT16 (handle, p_cmd);
1293                     btm_sco_chk_pend_unpark (status, handle);
1294                 }
1295             }
1296 #endif
1297             /* Case Falls Through */
1298
1299         case HCI_HOLD_MODE:
1300         case HCI_SNIFF_MODE:
1301         case HCI_PARK_MODE:
1302             btm_pm_proc_cmd_status(status);
1303             break;
1304
1305         default:
1306 #endif  /* BTM_PWR_MGR_INCLUDED */
1307             /* If command failed to start, we may need to tell BTM */
1308             if (status != HCI_SUCCESS)
1309             {
1310                 switch (opcode)
1311                 {
1312                     case HCI_INQUIRY:
1313                         /* Tell inquiry processing that we are done */
1314                         btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
1315                         break;
1316
1317                     case HCI_RMT_NAME_REQUEST:
1318                         /* Tell inquiry processing that we are done */
1319                         btm_process_remote_name (NULL, NULL, 0, status);
1320
1321                         btm_sec_rmt_name_request_complete (NULL, NULL, status);
1322                         break;
1323
1324                     case HCI_CHANGE_CONN_LINK_KEY:
1325                         /* Let host know we're done with error */
1326                         /* read handle out of stored command */
1327                         if (p_cmd != NULL)
1328                         {
1329                             p_cmd++;
1330                             STREAM_TO_UINT16 (handle, p_cmd);
1331
1332                             btm_acl_link_key_change (handle, status);
1333                         }
1334                         break;
1335
1336                     case HCI_QOS_SETUP_COMP_EVT:
1337                         /* Tell qos setup that we are done */
1338                         btm_qos_setup_complete(status,0,NULL);
1339                         break;
1340
1341                     case HCI_SWITCH_ROLE:
1342                         /* Tell BTM that the command failed */
1343                         /* read bd addr out of stored command */
1344                         if (p_cmd != NULL)
1345                         {
1346                             p_cmd++;
1347                             STREAM_TO_BDADDR (bd_addr, p_cmd);
1348                             btm_acl_role_changed(status, bd_addr, BTM_ROLE_UNDEFINED);
1349                         }
1350                         else
1351                             btm_acl_role_changed(status, NULL, BTM_ROLE_UNDEFINED);
1352                         l2c_link_role_changed (NULL, BTM_ROLE_UNDEFINED, HCI_ERR_COMMAND_DISALLOWED);
1353                         break;
1354
1355                     case HCI_CREATE_CONNECTION:
1356                         /* read bd addr out of stored command */
1357                         if (p_cmd != NULL)
1358                         {
1359                             p_cmd++;
1360                             STREAM_TO_BDADDR (bd_addr, p_cmd);
1361                             btm_sec_connected (bd_addr, HCI_INVALID_HANDLE, status, 0);
1362                             l2c_link_hci_conn_comp (status, HCI_INVALID_HANDLE, bd_addr);
1363                         }
1364                         break;
1365
1366                     case HCI_READ_RMT_EXT_FEATURES:
1367                         if (p_cmd != NULL)
1368                         {
1369                             p_cmd++; /* skip command length */
1370                             STREAM_TO_UINT16 (handle, p_cmd);
1371                         }
1372                         else
1373                             handle = HCI_INVALID_HANDLE;
1374
1375                         btm_read_remote_ext_features_failed(status, handle);
1376                         break;
1377
1378                     case HCI_AUTHENTICATION_REQUESTED:
1379                         /* Device refused to start authentication.  That should be treated as authentication failure. */
1380                         btm_sec_auth_complete (BTM_INVALID_HCI_HANDLE, status);
1381                         break;
1382
1383                     case HCI_SET_CONN_ENCRYPTION:
1384                         /* Device refused to start encryption.  That should be treated as encryption failure. */
1385                         btm_sec_encrypt_change (BTM_INVALID_HCI_HANDLE, status, FALSE);
1386                         break;
1387
1388 #if BTM_SCO_INCLUDED == TRUE
1389                     case HCI_SETUP_ESCO_CONNECTION:
1390                         /* read handle out of stored command */
1391                         if (p_cmd != NULL)
1392                         {
1393                             p_cmd++;
1394                             STREAM_TO_UINT16 (handle, p_cmd);
1395
1396                             /* Determine if initial connection failed or is a change of setup */
1397                             if (btm_is_sco_active(handle))
1398                                 btm_esco_proc_conn_chg (status, handle, 0, 0, 0, 0);
1399                             else
1400                                 btm_sco_connected (status, NULL, handle, &esco_data);
1401                         }
1402                         break;
1403 #endif
1404
1405 /* This is commented out until an upper layer cares about returning event
1406 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
1407             case HCI_ENHANCED_FLUSH:
1408                 break;
1409 #endif
1410 */
1411                     default:
1412                         if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1413                             btm_vsc_complete (&status, opcode, 1, (tBTM_CMPL_CB *)p_vsc_status_cback);
1414                         break;
1415                 }
1416
1417             }
1418             else
1419             {
1420                 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1421                     btm_vsc_complete (&status, opcode, 1, (tBTM_CMPL_CB *)p_vsc_status_cback);
1422             }
1423 #if BTM_PWR_MGR_INCLUDED == TRUE
1424     }
1425 #endif
1426 }
1427
1428 /*******************************************************************************
1429 **
1430 ** Function         btu_hcif_command_status_evt
1431 **
1432 ** Description      Process event HCI_COMMAND_STATUS_EVT
1433 **
1434 ** Returns          void
1435 **
1436 *******************************************************************************/
1437 static void btu_hcif_command_status_evt (UINT8 controller_id, UINT8 *p)
1438 {
1439     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1440     UINT8       status;
1441     UINT16      opcode;
1442     UINT16      cmd_opcode;
1443     BT_HDR      *p_cmd = NULL;
1444     UINT8       *p_data = NULL;
1445     void        *p_vsc_status_cback = NULL;
1446
1447     STREAM_TO_UINT8  (status, p);
1448     STREAM_TO_UINT8  (p_hci_cmd_cb->cmd_window, p);
1449
1450 #if (defined(HCI_MAX_SIMUL_CMDS) && (HCI_MAX_SIMUL_CMDS > 0))
1451     if (p_hci_cmd_cb->cmd_window > HCI_MAX_SIMUL_CMDS)
1452         p_hci_cmd_cb->cmd_window = HCI_MAX_SIMUL_CMDS;
1453 #endif
1454
1455     STREAM_TO_UINT16 (opcode, p);
1456
1457     /* only do this for certain commands */
1458     if ((opcode != HCI_RESET) && (opcode != HCI_HOST_NUM_PACKETS_DONE) &&
1459         (opcode != HCI_COMMAND_NONE))
1460     {
1461         /*look for corresponding command in cmd_queue*/
1462         p_cmd = (BT_HDR *) GKI_getfirst(&(p_hci_cmd_cb->cmd_cmpl_q));
1463         while (p_cmd)
1464         {
1465             p_data = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1466             STREAM_TO_UINT16 (cmd_opcode, p_data);
1467
1468             /* Make sure this  command is for the command_status received */
1469             if (cmd_opcode != opcode)
1470             {
1471                 /* opcode does not match, check next command in the queue */
1472                 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
1473                 continue;
1474             }
1475             else
1476             {
1477                 GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
1478
1479                 /* If command was a VSC, then extract command_status callback */
1480                  if ((cmd_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1481                 {
1482                     p_vsc_status_cback = *((void **)(p_cmd + 1));
1483                 }
1484                 break;
1485             }
1486         }
1487
1488         /* if more commands in queue restart timer */
1489         if (BTU_CMD_CMPL_TIMEOUT > 0)
1490         {
1491             if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1492             {
1493 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
1494                 p_hci_cmd_cb->checked_hcisu = FALSE;
1495 #endif
1496                 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1497                                  (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1498                                  BTU_CMD_CMPL_TIMEOUT);
1499             }
1500             else
1501             {
1502                 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
1503             }
1504         }
1505     }
1506
1507     /* handle command */
1508     btu_hcif_hdl_command_status (opcode, status, p_data, p_vsc_status_cback);
1509
1510     /* free stored command */
1511     if (p_cmd != NULL)
1512     {
1513         GKI_freebuf (p_cmd);
1514     }
1515     else
1516     {
1517         HCI_TRACE_WARNING("No command in queue matching opcode %d", opcode);
1518     }
1519
1520     /* See if we can forward any more commands */
1521     btu_hcif_send_cmd (controller_id, NULL);
1522 }
1523
1524 /*******************************************************************************
1525 **
1526 ** Function         btu_hcif_cmd_timeout
1527 **
1528 ** Description      Handle a command timeout
1529 **
1530 ** Returns          void
1531 **
1532 *******************************************************************************/
1533 void btu_hcif_cmd_timeout (UINT8 controller_id)
1534 {
1535     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1536     BT_HDR  *p_cmd;
1537     UINT8   *p;
1538     void    *p_cplt_cback = NULL;
1539     UINT16  opcode;
1540     UINT16  event;
1541
1542 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
1543     if (!(p_hci_cmd_cb->checked_hcisu))
1544     {
1545         HCI_TRACE_WARNING("BTU HCI(id=%d) command timeout - double check HCISU", controller_id);
1546
1547         /* trigger HCISU to read any pending data in transport buffer */
1548         GKI_send_event(HCISU_TASK, HCISU_EVT_MASK);
1549
1550         btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1551                          (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1552                          2); /* start short timer, if timer is set to 1 then it could expire before HCISU checks. */
1553
1554         p_hci_cmd_cb->checked_hcisu = TRUE;
1555
1556         return;
1557     }
1558 #endif
1559
1560     /* set the controller cmd window to 1, as if we received a response, so
1561     ** the flow of commands from the stack doesn't hang */
1562     p_hci_cmd_cb->cmd_window = 1;
1563
1564     /* get queued command */
1565     if ((p_cmd = (BT_HDR *) GKI_dequeue (&(p_hci_cmd_cb->cmd_cmpl_q))) == NULL)
1566     {
1567         HCI_TRACE_WARNING("Cmd timeout; no cmd in queue");
1568         return;
1569     }
1570
1571     /* if more commands in queue restart timer */
1572     if (BTU_CMD_CMPL_TIMEOUT > 0)
1573     {
1574         if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1575         {
1576 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
1577             p_hci_cmd_cb->checked_hcisu = FALSE;
1578 #endif
1579             btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1580                              (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1581                              BTU_CMD_CMPL_TIMEOUT);
1582         }
1583     }
1584
1585     p = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1586 #if (NFC_INCLUDED == TRUE)
1587     if (controller_id == NFC_CONTROLLER_ID)
1588     {
1589         //TODO call nfc_ncif_cmd_timeout
1590         HCI_TRACE_WARNING("BTU NCI command timeout - header 0x%02x%02x", p[0], p[1]);
1591         return;
1592     }
1593 #endif
1594
1595     /* get opcode from stored command */
1596     STREAM_TO_UINT16 (opcode, p);
1597
1598 // btla-specific ++
1599 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE))
1600     ALOGE("######################################################################");
1601     ALOGE("#");
1602     ALOGE("# WARNING : BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
1603     ALOGE("#");
1604     ALOGE("######################################################################");
1605 #else
1606     HCI_TRACE_WARNING("BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
1607 #endif
1608 // btla-specific ++
1609
1610     /* send stack a fake command complete or command status, but first determine
1611     ** which to send
1612     */
1613     switch (opcode)
1614     {
1615         case HCI_HOLD_MODE:
1616         case HCI_SNIFF_MODE:
1617         case HCI_EXIT_SNIFF_MODE:
1618         case HCI_PARK_MODE:
1619         case HCI_EXIT_PARK_MODE:
1620         case HCI_INQUIRY:
1621         case HCI_RMT_NAME_REQUEST:
1622         case HCI_QOS_SETUP_COMP_EVT:
1623         case HCI_CREATE_CONNECTION:
1624         case HCI_CHANGE_CONN_LINK_KEY:
1625         case HCI_SWITCH_ROLE:
1626         case HCI_READ_RMT_EXT_FEATURES:
1627         case HCI_AUTHENTICATION_REQUESTED:
1628         case HCI_SET_CONN_ENCRYPTION:
1629 #if BTM_SCO_INCLUDED == TRUE
1630         case HCI_SETUP_ESCO_CONNECTION:
1631 #endif
1632             /* fake a command status */
1633             btu_hcif_hdl_command_status (opcode, HCI_ERR_UNSPECIFIED, p, NULL);
1634             break;
1635
1636         default:
1637             /* If vendor specific restore the callback function */
1638             if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
1639 #if BLE_INCLUDED == TRUE
1640                 || (opcode == HCI_BLE_RAND ) ||
1641                 (opcode == HCI_BLE_ENCRYPT)
1642 #endif
1643                )
1644             {
1645                 p_cplt_cback = *((void **)(p_cmd + 1));
1646             }
1647
1648             /* fake a command complete; first create a fake event */
1649             event = HCI_ERR_UNSPECIFIED;
1650             btu_hcif_hdl_command_complete (opcode, (UINT8 *)&event, 1, p_cplt_cback);
1651             break;
1652     }
1653
1654     /* free stored command */
1655     GKI_freebuf(p_cmd);
1656
1657     num_hci_cmds_timed_out++;
1658     /* When we receive consecutive HCI cmd timeouts for >=BTM_MAX_HCI_CMD_TOUT_BEFORE_RESTART
1659      times, Bluetooth process will be killed and restarted */
1660     if (num_hci_cmds_timed_out >= BTM_MAX_HCI_CMD_TOUT_BEFORE_RESTART)
1661     {
1662         HCI_TRACE_ERROR("Num consecutive HCI Cmd tout =%d Restarting BT process",num_hci_cmds_timed_out);
1663
1664         usleep(10000); /* 10 milliseconds */
1665         /* Killing the process to force a restart as part of fault tolerance */
1666         kill(getpid(), SIGKILL);
1667     }
1668     else
1669     {
1670         HCI_TRACE_WARNING("HCI Cmd timeout counter %d", num_hci_cmds_timed_out);
1671
1672         /* If anyone wants device status notifications, give him one */
1673         btm_report_device_status (BTM_DEV_STATUS_CMD_TOUT);
1674     }
1675     /* See if we can forward any more commands */
1676     btu_hcif_send_cmd (controller_id, NULL);
1677 }
1678
1679 /*******************************************************************************
1680 **
1681 ** Function         btu_hcif_hardware_error_evt
1682 **
1683 ** Description      Process event HCI_HARDWARE_ERROR_EVT
1684 **
1685 ** Returns          void
1686 **
1687 *******************************************************************************/
1688 static void btu_hcif_hardware_error_evt (UINT8 *p)
1689 {
1690     HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
1691
1692     /* If anyone wants device status notifications, give him one. */
1693     btm_report_device_status (BTM_DEV_STATUS_DOWN);
1694
1695     /* Reset the controller */
1696     if (BTM_IsDeviceUp())
1697         BTM_DeviceReset (NULL);
1698 }
1699
1700
1701 /*******************************************************************************
1702 **
1703 ** Function         btu_hcif_flush_occured_evt
1704 **
1705 ** Description      Process event HCI_FLUSH_OCCURED_EVT
1706 **
1707 ** Returns          void
1708 **
1709 *******************************************************************************/
1710 static void btu_hcif_flush_occured_evt (void)
1711 {
1712 }
1713
1714
1715 /*******************************************************************************
1716 **
1717 ** Function         btu_hcif_role_change_evt
1718 **
1719 ** Description      Process event HCI_ROLE_CHANGE_EVT
1720 **
1721 ** Returns          void
1722 **
1723 *******************************************************************************/
1724 static void btu_hcif_role_change_evt (UINT8 *p)
1725 {
1726     UINT8       status;
1727     BD_ADDR     bda;
1728     UINT8       role;
1729
1730     STREAM_TO_UINT8 (status, p);
1731     STREAM_TO_BDADDR (bda, p);
1732     STREAM_TO_UINT8  (role, p);
1733
1734     l2c_link_role_changed (bda, role, status);
1735     btm_acl_role_changed(status, bda, role);
1736 }
1737
1738
1739 /*******************************************************************************
1740 **
1741 ** Function         btu_hcif_num_compl_data_pkts_evt
1742 **
1743 ** Description      Process event HCI_NUM_COMPL_DATA_PKTS_EVT
1744 **
1745 ** Returns          void
1746 **
1747 *******************************************************************************/
1748 static void btu_hcif_num_compl_data_pkts_evt (UINT8 *p)
1749 {
1750     /* Process for L2CAP and SCO */
1751     l2c_link_process_num_completed_pkts (p);
1752
1753     /* Send on to SCO */
1754     /*?? No SCO for now */
1755 }
1756
1757 /*******************************************************************************
1758 **
1759 ** Function         btu_hcif_mode_change_evt
1760 **
1761 ** Description      Process event HCI_MODE_CHANGE_EVT
1762 **
1763 ** Returns          void
1764 **
1765 *******************************************************************************/
1766 static void btu_hcif_mode_change_evt (UINT8 *p)
1767 {
1768     UINT8       status;
1769     UINT16      handle;
1770     UINT8       current_mode;
1771     UINT16      interval;
1772
1773     STREAM_TO_UINT8 (status, p);
1774
1775     STREAM_TO_UINT16 (handle, p);
1776     STREAM_TO_UINT8 (current_mode, p);
1777     STREAM_TO_UINT16 (interval, p);
1778 #if BTM_PWR_MGR_INCLUDED == TRUE
1779 #if BTM_SCO_WAKE_PARKED_LINK == TRUE
1780     btm_sco_chk_pend_unpark (status, handle);
1781 #endif
1782     btm_pm_proc_mode_change (status, handle, current_mode, interval);
1783 #else
1784     btm_process_mode_change (status, handle, current_mode, interval);
1785 #endif /* BTM_PWR_MGR_INCLUDED == TRUE */
1786
1787 #if (HID_DEV_INCLUDED == TRUE) && (HID_DEV_PM_INCLUDED == TRUE)
1788     hidd_pm_proc_mode_change( status, current_mode, interval ) ;
1789 #endif
1790 }
1791
1792 /*******************************************************************************
1793 **
1794 ** Function         btu_hcif_ssr_evt
1795 **
1796 ** Description      Process event HCI_SNIFF_SUB_RATE_EVT
1797 **
1798 ** Returns          void
1799 **
1800 *******************************************************************************/
1801     #if (BTM_SSR_INCLUDED == TRUE)
1802 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len)
1803 {
1804 #if (BTM_PWR_MGR_INCLUDED == TRUE)
1805     btm_pm_proc_ssr_evt(p, evt_len);
1806 #endif
1807 }
1808     #endif
1809
1810
1811 /*******************************************************************************
1812 **
1813 ** Function         btu_hcif_return_link_keys_evt
1814 **
1815 ** Description      Process event HCI_RETURN_LINK_KEYS_EVT
1816 **
1817 ** Returns          void
1818 **
1819 *******************************************************************************/
1820
1821 static void btu_hcif_return_link_keys_evt (UINT8 *p)
1822 {
1823     UINT8                       num_keys;
1824     tBTM_RETURN_LINK_KEYS_EVT   *result;
1825
1826     /* get the number of link keys */
1827     num_keys = *p;
1828
1829     /* If there are no link keys don't call the call back */
1830     if (!num_keys)
1831         return;
1832
1833     /* Take one extra byte at the beginning to specify event */
1834     result = (tBTM_RETURN_LINK_KEYS_EVT *)(--p);
1835     result->event = BTM_CB_EVT_RETURN_LINK_KEYS;
1836
1837     /* Call the BTM function to pass the link keys to application */
1838     btm_return_link_keys_evt (result);
1839 }
1840
1841
1842 /*******************************************************************************
1843 **
1844 ** Function         btu_hcif_pin_code_request_evt
1845 **
1846 ** Description      Process event HCI_PIN_CODE_REQUEST_EVT
1847 **
1848 ** Returns          void
1849 **
1850 *******************************************************************************/
1851 static void btu_hcif_pin_code_request_evt (UINT8 *p)
1852 {
1853     BD_ADDR  bda;
1854
1855     STREAM_TO_BDADDR (bda, p);
1856
1857     /* Tell L2CAP that there was a PIN code request,  */
1858     /* it may need to stretch timeouts                */
1859     l2c_pin_code_request (bda);
1860
1861     btm_sec_pin_code_request (bda);
1862 }
1863
1864
1865 /*******************************************************************************
1866 **
1867 ** Function         btu_hcif_link_key_request_evt
1868 **
1869 ** Description      Process event HCI_LINK_KEY_REQUEST_EVT
1870 **
1871 ** Returns          void
1872 **
1873 *******************************************************************************/
1874 static void btu_hcif_link_key_request_evt (UINT8 *p)
1875 {
1876     BD_ADDR  bda;
1877
1878     STREAM_TO_BDADDR (bda, p);
1879     btm_sec_link_key_request (bda);
1880 }
1881
1882
1883 /*******************************************************************************
1884 **
1885 ** Function         btu_hcif_link_key_notification_evt
1886 **
1887 ** Description      Process event HCI_LINK_KEY_NOTIFICATION_EVT
1888 **
1889 ** Returns          void
1890 **
1891 *******************************************************************************/
1892 static void btu_hcif_link_key_notification_evt (UINT8 *p)
1893 {
1894     BD_ADDR  bda;
1895     LINK_KEY key;
1896     UINT8    key_type;
1897
1898     STREAM_TO_BDADDR (bda, p);
1899     STREAM_TO_ARRAY16 (key, p);
1900     STREAM_TO_UINT8 (key_type, p);
1901
1902     btm_sec_link_key_notification (bda, key, key_type);
1903 }
1904
1905
1906 /*******************************************************************************
1907 **
1908 ** Function         btu_hcif_loopback_command_evt
1909 **
1910 ** Description      Process event HCI_LOOPBACK_COMMAND_EVT
1911 **
1912 ** Returns          void
1913 **
1914 *******************************************************************************/
1915 static void btu_hcif_loopback_command_evt (void)
1916 {
1917 }
1918
1919
1920 /*******************************************************************************
1921 **
1922 ** Function         btu_hcif_data_buf_overflow_evt
1923 **
1924 ** Description      Process event HCI_DATA_BUF_OVERFLOW_EVT
1925 **
1926 ** Returns          void
1927 **
1928 *******************************************************************************/
1929 static void btu_hcif_data_buf_overflow_evt (void)
1930 {
1931 }
1932
1933
1934 /*******************************************************************************
1935 **
1936 ** Function         btu_hcif_max_slots_changed_evt
1937 **
1938 ** Description      Process event HCI_MAX_SLOTS_CHANGED_EVT
1939 **
1940 ** Returns          void
1941 **
1942 *******************************************************************************/
1943 static void btu_hcif_max_slots_changed_evt (void)
1944 {
1945 }
1946
1947
1948 /*******************************************************************************
1949 **
1950 ** Function         btu_hcif_read_clock_off_comp_evt
1951 **
1952 ** Description      Process event HCI_READ_CLOCK_OFF_COMP_EVT
1953 **
1954 ** Returns          void
1955 **
1956 *******************************************************************************/
1957 static void btu_hcif_read_clock_off_comp_evt (UINT8 *p)
1958 {
1959     UINT8       status;
1960     UINT16      handle;
1961     UINT16      clock_offset;
1962
1963     STREAM_TO_UINT8  (status, p);
1964
1965     /* If failed to get clock offset just drop the result */
1966     if (status != HCI_SUCCESS)
1967         return;
1968
1969     STREAM_TO_UINT16 (handle, p);
1970     STREAM_TO_UINT16 (clock_offset, p);
1971
1972     handle = HCID_GET_HANDLE (handle);
1973
1974     btm_process_clk_off_comp_evt (handle, clock_offset);
1975     btm_sec_update_clock_offset (handle, clock_offset);
1976 }
1977
1978
1979 /*******************************************************************************
1980 **
1981 ** Function         btu_hcif_conn_pkt_type_change_evt
1982 **
1983 ** Description      Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
1984 **
1985 ** Returns          void
1986 **
1987 *******************************************************************************/
1988 static void btu_hcif_conn_pkt_type_change_evt (void)
1989 {
1990 }
1991
1992
1993 /*******************************************************************************
1994 **
1995 ** Function         btu_hcif_qos_violation_evt
1996 **
1997 ** Description      Process event HCI_QOS_VIOLATION_EVT
1998 **
1999 ** Returns          void
2000 **
2001 *******************************************************************************/
2002 static void btu_hcif_qos_violation_evt (UINT8 *p)
2003 {
2004     UINT16   handle;
2005
2006     STREAM_TO_UINT16 (handle, p);
2007
2008     handle = HCID_GET_HANDLE (handle);
2009
2010
2011     l2c_link_hci_qos_violation (handle);
2012 }
2013
2014
2015 /*******************************************************************************
2016 **
2017 ** Function         btu_hcif_page_scan_mode_change_evt
2018 **
2019 ** Description      Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
2020 **
2021 ** Returns          void
2022 **
2023 *******************************************************************************/
2024 static void btu_hcif_page_scan_mode_change_evt (void)
2025 {
2026 }
2027
2028
2029 /*******************************************************************************
2030 **
2031 ** Function         btu_hcif_page_scan_rep_mode_chng_evt
2032 **
2033 ** Description      Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
2034 **
2035 ** Returns          void
2036 **
2037 *******************************************************************************/
2038 static void btu_hcif_page_scan_rep_mode_chng_evt (void)
2039 {
2040 }
2041
2042 /**********************************************
2043 ** Simple Pairing Events
2044 ***********************************************/
2045
2046 /*******************************************************************************
2047 **
2048 ** Function         btu_hcif_host_support_evt
2049 **
2050 ** Description      Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
2051 **
2052 ** Returns          void
2053 **
2054 *******************************************************************************/
2055 static void btu_hcif_host_support_evt (UINT8 *p)
2056 {
2057     btm_sec_rmt_host_support_feat_evt(p);
2058 }
2059
2060 /*******************************************************************************
2061 **
2062 ** Function         btu_hcif_io_cap_request_evt
2063 **
2064 ** Description      Process event HCI_IO_CAPABILITY_REQUEST_EVT
2065 **
2066 ** Returns          void
2067 **
2068 *******************************************************************************/
2069 static void btu_hcif_io_cap_request_evt (UINT8 *p)
2070 {
2071     btm_io_capabilities_req(p);
2072 }
2073
2074
2075 /*******************************************************************************
2076 **
2077 ** Function         btu_hcif_io_cap_response_evt
2078 **
2079 ** Description      Process event HCI_IO_CAPABILITY_RESPONSE_EVT
2080 **
2081 ** Returns          void
2082 **
2083 *******************************************************************************/
2084 static void btu_hcif_io_cap_response_evt (UINT8 *p)
2085 {
2086     btm_io_capabilities_rsp(p);
2087 }
2088
2089
2090 /*******************************************************************************
2091 **
2092 ** Function         btu_hcif_user_conf_request_evt
2093 **
2094 ** Description      Process event HCI_USER_CONFIRMATION_REQUEST_EVT
2095 **
2096 ** Returns          void
2097 **
2098 *******************************************************************************/
2099 static void btu_hcif_user_conf_request_evt (UINT8 *p)
2100 {
2101     btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
2102 }
2103
2104
2105 /*******************************************************************************
2106 **
2107 ** Function         btu_hcif_user_passkey_request_evt
2108 **
2109 ** Description      Process event HCI_USER_PASSKEY_REQUEST_EVT
2110 **
2111 ** Returns          void
2112 **
2113 *******************************************************************************/
2114 static void btu_hcif_user_passkey_request_evt (UINT8 *p)
2115 {
2116     btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
2117 }
2118
2119 /*******************************************************************************
2120 **
2121 ** Function         btu_hcif_user_passkey_notif_evt
2122 **
2123 ** Description      Process event HCI_USER_PASSKEY_NOTIFY_EVT
2124 **
2125 ** Returns          void
2126 **
2127 *******************************************************************************/
2128 static void btu_hcif_user_passkey_notif_evt (UINT8 *p)
2129 {
2130     btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
2131 }
2132
2133 /*******************************************************************************
2134 **
2135 ** Function         btu_hcif_keypress_notif_evt
2136 **
2137 ** Description      Process event HCI_KEYPRESS_NOTIFY_EVT
2138 **
2139 ** Returns          void
2140 **
2141 *******************************************************************************/
2142 static void btu_hcif_keypress_notif_evt (UINT8 *p)
2143 {
2144     btm_keypress_notif_evt(p);
2145 }
2146
2147 /*******************************************************************************
2148 **
2149 ** Function         btu_hcif_link_super_tout_evt
2150 **
2151 ** Description      Process event HCI_LINK_SUPER_TOUT_CHANGED_EVT
2152 **
2153 ** Returns          void
2154 **
2155 *******************************************************************************/
2156 static void btu_hcif_link_super_tout_evt (UINT8 *p)
2157 {
2158     UINT16 handle, timeout;
2159     STREAM_TO_UINT16 (handle, p);
2160     STREAM_TO_UINT16 (timeout, p);
2161
2162     btm_proc_lsto_evt(handle, timeout);
2163 }
2164
2165 /*******************************************************************************
2166 **
2167 ** Function         btu_hcif_rem_oob_request_evt
2168 **
2169 ** Description      Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
2170 **
2171 ** Returns          void
2172 **
2173 *******************************************************************************/
2174     #if BTM_OOB_INCLUDED == TRUE
2175 static void btu_hcif_rem_oob_request_evt (UINT8 *p)
2176 {
2177     btm_rem_oob_req(p);
2178 }
2179     #endif
2180
2181 /*******************************************************************************
2182 **
2183 ** Function         btu_hcif_simple_pair_complete_evt
2184 **
2185 ** Description      Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
2186 **
2187 ** Returns          void
2188 **
2189 *******************************************************************************/
2190 static void btu_hcif_simple_pair_complete_evt (UINT8 *p)
2191 {
2192     btm_simple_pair_complete(p);
2193 }
2194 /*******************************************************************************
2195 **
2196 ** Function         btu_hcif_flush_cmd_queue
2197 **
2198 ** Description      Flush the HCI command complete queue and transmit queue when
2199 **                  needed.
2200 **
2201 ** Returns          void
2202 **
2203 *******************************************************************************/
2204 void btu_hcif_flush_cmd_queue(void)
2205 {
2206     BT_HDR *p_cmd;
2207
2208     btu_cb.hci_cmd_cb[0].cmd_window = 0;
2209     while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_cmpl_q)) != NULL)
2210     {
2211         GKI_freebuf (p_cmd);
2212     }
2213     while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_xmit_q)) != NULL)
2214     {
2215         GKI_freebuf (p_cmd);
2216     }
2217 }
2218
2219 /*******************************************************************************
2220 **
2221 ** Function         btu_hcif_enhanced_flush_complete_evt
2222 **
2223 ** Description      Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
2224 **
2225 ** Returns          void
2226 **
2227 *******************************************************************************/
2228 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
2229 static void btu_hcif_enhanced_flush_complete_evt (void)
2230 {
2231 /* This is empty until an upper layer cares about returning event */
2232 }
2233 #endif
2234 /**********************************************
2235 ** End of Simple Pairing Events
2236 ***********************************************/
2237
2238
2239 /**********************************************
2240 ** BLE Events
2241 ***********************************************/
2242 #if (defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE)
2243 static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p)
2244 {
2245     UINT8   status;
2246     UINT8   enc_enable = 0;
2247     UINT16  handle;
2248
2249     STREAM_TO_UINT8  (status, p);
2250     STREAM_TO_UINT16 (handle, p);
2251
2252     if (status == HCI_SUCCESS) enc_enable = 1;
2253
2254     btm_sec_encrypt_change (handle, status, enc_enable);
2255 }
2256
2257 static void btu_ble_process_adv_pkt (UINT8 *p)
2258 {
2259     HCI_TRACE_EVENT("btu_ble_process_adv_pkt");
2260
2261     btm_ble_process_adv_pkt(p);
2262 }
2263
2264 static void btu_ble_ll_conn_complete_evt ( UINT8 *p, UINT16 evt_len)
2265 {
2266     btm_ble_conn_complete(p, evt_len);
2267 }
2268
2269 static void btu_ble_ll_conn_param_upd_evt (UINT8 *p)
2270 {
2271     /* This is empty until an upper layer cares about returning event */
2272 }
2273
2274 static void btu_ble_read_remote_feat_evt (UINT8 *p)
2275 {
2276     btm_ble_read_remote_features_complete(p);
2277 }
2278
2279 static void btu_ble_proc_ltk_req (UINT8 *p)
2280 {
2281     UINT16 ediv, handle;
2282     UINT8   *pp;
2283
2284     STREAM_TO_UINT16(handle, p);
2285     pp = p + 8;
2286     STREAM_TO_UINT16(ediv, pp);
2287 #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
2288     btm_ble_ltk_request(handle, p, ediv);
2289 #endif
2290     /* This is empty until an upper layer cares about returning event */
2291 }
2292 /**********************************************
2293 ** End of BLE Events Handler
2294 ***********************************************/
2295 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
2296 static void btu_ble_rc_param_req_evt(UINT8 *p)
2297 {
2298     UINT16 handle;
2299     UINT16  int_min, int_max, latency, timeout;
2300
2301     STREAM_TO_UINT16(handle, p);
2302     STREAM_TO_UINT16(int_min, p);
2303     STREAM_TO_UINT16(int_max, p);
2304     STREAM_TO_UINT16(latency, p);
2305     STREAM_TO_UINT16(timeout, p);
2306
2307     l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency, timeout);
2308 }
2309 #endif /* BLE_LLT_INCLUDED */
2310
2311 #endif /* BLE_INCLUDED */
2312