1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
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
26 ******************************************************************************/
40 extern void btm_process_cancel_complete(UINT8 status, UINT8 mode);
41 extern void btm_ble_test_command_complete(UINT8 *p);
44 #define LOG_TAG "BTLD"
45 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE) && (!defined(LINUX_NATIVE)) )
46 #include <cutils/log.h>
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__)
54 /* BTE application task */
55 #if APPL_INCLUDED == TRUE
60 //Counter to track number of HCI command timeout
61 static int num_hci_cmds_timed_out;
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);
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);
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);
117 #if BTM_OOB_INCLUDED == TRUE
118 static void btu_hcif_rem_oob_request_evt (UINT8 *p);
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);
126 #if (BTM_SSR_INCLUDED == TRUE)
127 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len);
128 #endif /* BTM_SSR_INCLUDED == TRUE */
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 );
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, UINT16 evt_len);
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);
146 /*******************************************************************************
148 ** Function btu_hcif_store_cmd
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
156 *******************************************************************************/
157 static void btu_hcif_store_cmd (UINT8 controller_id, BT_HDR *p_buf)
159 tHCI_CMD_CB *p_hci_cmd_cb;
164 /* Validate controller ID */
165 if (controller_id >= BTU_MAX_LOCAL_CTRLS)
168 p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
169 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
171 /* get command opcode */
172 STREAM_TO_UINT16 (opcode, p);
174 /* don't do anything for certain commands */
175 if ((opcode == HCI_RESET) || (opcode == HCI_HOST_NUM_PACKETS_DONE))
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)
187 memcpy (p_cmd, p_buf, sizeof(BT_HDR));
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)
197 memcpy ((UINT8 *)(p_cmd + 1), (UINT8 *)(p_buf + 1), sizeof(void *));
200 memcpy ((UINT8 *)(p_cmd + 1) + p_cmd->offset,
201 (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
203 /* queue copy of cmd */
204 GKI_enqueue(&(p_hci_cmd_cb->cmd_cmpl_q), p_cmd);
207 if (BTU_CMD_CMPL_TIMEOUT > 0)
209 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
210 (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
211 BTU_CMD_CMPL_TIMEOUT);
215 /*******************************************************************************
217 ** Function btu_hcif_process_event
219 ** Description This function is called when an event is received from
220 ** the Host Controller.
224 *******************************************************************************/
225 void btu_hcif_process_event (UINT8 controller_id, BT_HDR *p_msg)
227 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
228 UINT8 hci_evt_code, hci_evt_len;
229 #if BLE_INCLUDED == TRUE
232 STREAM_TO_UINT8 (hci_evt_code, p);
233 STREAM_TO_UINT8 (hci_evt_len, p);
235 switch (hci_evt_code)
237 case HCI_INQUIRY_COMP_EVT:
238 btu_hcif_inquiry_comp_evt (p);
240 case HCI_INQUIRY_RESULT_EVT:
241 btu_hcif_inquiry_result_evt (p);
243 case HCI_INQUIRY_RSSI_RESULT_EVT:
244 btu_hcif_inquiry_rssi_result_evt (p);
246 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
247 case HCI_EXTENDED_INQUIRY_RESULT_EVT:
248 btu_hcif_extended_inquiry_result_evt (p);
251 case HCI_CONNECTION_COMP_EVT:
252 btu_hcif_connection_comp_evt (p);
254 case HCI_CONNECTION_REQUEST_EVT:
255 btu_hcif_connection_request_evt (p);
257 case HCI_DISCONNECTION_COMP_EVT:
258 btu_hcif_disconnection_comp_evt (p);
260 case HCI_AUTHENTICATION_COMP_EVT:
261 btu_hcif_authentication_comp_evt (p);
263 case HCI_RMT_NAME_REQUEST_COMP_EVT:
264 btu_hcif_rmt_name_request_comp_evt (p, hci_evt_len);
266 case HCI_ENCRYPTION_CHANGE_EVT:
267 btu_hcif_encryption_change_evt (p);
269 #if BLE_INCLUDED == TRUE
270 case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
271 btu_hcif_encryption_key_refresh_cmpl_evt(p);
274 case HCI_CHANGE_CONN_LINK_KEY_EVT:
275 btu_hcif_change_conn_link_key_evt (p);
277 case HCI_MASTER_LINK_KEY_COMP_EVT:
278 btu_hcif_master_link_key_comp_evt (p);
280 case HCI_READ_RMT_FEATURES_COMP_EVT:
281 btu_hcif_read_rmt_features_comp_evt (p);
283 case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
284 btu_hcif_read_rmt_ext_features_comp_evt (p);
286 case HCI_READ_RMT_VERSION_COMP_EVT:
287 btu_hcif_read_rmt_version_comp_evt (p);
289 case HCI_QOS_SETUP_COMP_EVT:
290 btu_hcif_qos_setup_comp_evt (p);
292 case HCI_COMMAND_COMPLETE_EVT:
293 btu_hcif_command_complete_evt (controller_id, p, hci_evt_len);
295 case HCI_COMMAND_STATUS_EVT:
296 btu_hcif_command_status_evt (controller_id, p);
298 case HCI_HARDWARE_ERROR_EVT:
299 btu_hcif_hardware_error_evt (p);
301 case HCI_FLUSH_OCCURED_EVT:
302 btu_hcif_flush_occured_evt ();
304 case HCI_ROLE_CHANGE_EVT:
305 btu_hcif_role_change_evt (p);
307 case HCI_NUM_COMPL_DATA_PKTS_EVT:
308 btu_hcif_num_compl_data_pkts_evt (p);
310 case HCI_MODE_CHANGE_EVT:
311 btu_hcif_mode_change_evt (p);
313 case HCI_RETURN_LINK_KEYS_EVT:
314 btu_hcif_return_link_keys_evt (p);
316 case HCI_PIN_CODE_REQUEST_EVT:
317 btu_hcif_pin_code_request_evt (p);
319 case HCI_LINK_KEY_REQUEST_EVT:
320 btu_hcif_link_key_request_evt (p);
322 case HCI_LINK_KEY_NOTIFICATION_EVT:
323 btu_hcif_link_key_notification_evt (p);
325 case HCI_LOOPBACK_COMMAND_EVT:
326 btu_hcif_loopback_command_evt ();
328 case HCI_DATA_BUF_OVERFLOW_EVT:
329 btu_hcif_data_buf_overflow_evt ();
331 case HCI_MAX_SLOTS_CHANGED_EVT:
332 btu_hcif_max_slots_changed_evt ();
334 case HCI_READ_CLOCK_OFF_COMP_EVT:
335 btu_hcif_read_clock_off_comp_evt (p);
337 case HCI_CONN_PKT_TYPE_CHANGE_EVT:
338 btu_hcif_conn_pkt_type_change_evt ();
340 case HCI_QOS_VIOLATION_EVT:
341 btu_hcif_qos_violation_evt (p);
343 case HCI_PAGE_SCAN_MODE_CHANGE_EVT:
344 btu_hcif_page_scan_mode_change_evt ();
346 case HCI_PAGE_SCAN_REP_MODE_CHNG_EVT:
347 btu_hcif_page_scan_rep_mode_chng_evt ();
349 case HCI_ESCO_CONNECTION_COMP_EVT:
350 btu_hcif_esco_connection_comp_evt (p);
352 case HCI_ESCO_CONNECTION_CHANGED_EVT:
353 btu_hcif_esco_connection_chg_evt (p);
355 #if (BTM_SSR_INCLUDED == TRUE)
356 case HCI_SNIFF_SUB_RATE_EVT:
357 btu_hcif_ssr_evt (p, hci_evt_len);
359 #endif /* BTM_SSR_INCLUDED == TRUE */
360 case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
361 btu_hcif_host_support_evt (p);
363 case HCI_IO_CAPABILITY_REQUEST_EVT:
364 btu_hcif_io_cap_request_evt (p);
366 case HCI_IO_CAPABILITY_RESPONSE_EVT:
367 btu_hcif_io_cap_response_evt (p);
369 case HCI_USER_CONFIRMATION_REQUEST_EVT:
370 btu_hcif_user_conf_request_evt (p);
372 case HCI_USER_PASSKEY_REQUEST_EVT:
373 btu_hcif_user_passkey_request_evt (p);
375 #if BTM_OOB_INCLUDED == TRUE
376 case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
377 btu_hcif_rem_oob_request_evt (p);
380 case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
381 btu_hcif_simple_pair_complete_evt (p);
383 case HCI_USER_PASSKEY_NOTIFY_EVT:
384 btu_hcif_user_passkey_notif_evt (p);
386 case HCI_KEYPRESS_NOTIFY_EVT:
387 btu_hcif_keypress_notif_evt (p);
389 case HCI_LINK_SUPER_TOUT_CHANGED_EVT:
390 btu_hcif_link_super_tout_evt (p);
392 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
393 case HCI_ENHANCED_FLUSH_COMPLETE_EVT:
394 btu_hcif_enhanced_flush_complete_evt ();
398 #if (BLE_INCLUDED == TRUE)
400 STREAM_TO_UINT8 (ble_sub_code, p);
402 HCI_TRACE_EVENT("BLE HCI(id=%d) event = 0x%02x)", hci_evt_code, ble_sub_code);
404 switch (ble_sub_code)
406 case HCI_BLE_ADV_PKT_RPT_EVT: /* result of inquiry */
407 btu_ble_process_adv_pkt(p);
409 case HCI_BLE_CONN_COMPLETE_EVT:
410 btu_ble_ll_conn_complete_evt(p, hci_evt_len);
412 case HCI_BLE_LL_CONN_PARAM_UPD_EVT:
413 btu_ble_ll_conn_param_upd_evt(p, hci_evt_len);
415 case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
416 btu_ble_read_remote_feat_evt(p);
418 case HCI_BLE_LTK_REQ_EVT: /* received only at slave device */
419 btu_ble_proc_ltk_req(p);
421 #if (BLE_LLT_INCLUDED == TRUE)
422 case HCI_BLE_RC_PARAM_REQ_EVT:
423 btu_ble_rc_param_req_evt(p);
429 #endif /* BLE_INCLUDED */
430 case HCI_VENDOR_SPECIFIC_EVT:
431 btm_vendor_specific_evt (p, hci_evt_len);
434 // reset the num_hci_cmds_timed_out upon receving any event from controller.
435 num_hci_cmds_timed_out = 0;
439 /*******************************************************************************
441 ** Function btu_hcif_send_cmd
443 ** Description This function is called to check if it can send commands
444 ** to the Host Controller. It may be passed the address of
449 *******************************************************************************/
450 void btu_hcif_send_cmd (UINT8 controller_id, BT_HDR *p_buf)
452 tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
454 /* If there are already commands in the queue, then enqueue this command */
455 if ((p_buf) && (!GKI_queue_is_empty(&p_hci_cmd_cb->cmd_xmit_q)))
457 GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
461 /* Allow for startup case, where no acks may be received */
462 if ( ((controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
463 && (p_hci_cmd_cb->cmd_window == 0)
464 && (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT)) )
466 p_hci_cmd_cb->cmd_window = GKI_queue_length(&p_hci_cmd_cb->cmd_xmit_q) + 1;
469 /* See if we can send anything */
470 while (p_hci_cmd_cb->cmd_window != 0)
473 p_buf = (BT_HDR *)GKI_dequeue (&(p_hci_cmd_cb->cmd_xmit_q));
477 btu_hcif_store_cmd(controller_id, p_buf);
478 p_hci_cmd_cb->cmd_window--;
480 if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
482 HCI_CMD_TO_LOWER(p_buf);
486 /* Unknown controller */
487 HCI_TRACE_WARNING("BTU HCI(ctrl id=%d) controller ID not recognized", controller_id);
498 GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
500 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
501 if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
503 /* check if controller can go to sleep */
504 btu_check_bt_sleep ();
511 /*******************************************************************************
513 ** Function btu_hcif_send_host_rdy_for_data
515 ** Description This function is called to check if it can send commands
516 ** to the Host Controller. It may be passed the address of
521 *******************************************************************************/
522 void btu_hcif_send_host_rdy_for_data(void)
524 UINT16 num_pkts[MAX_L2CAP_LINKS + 4]; /* 3 SCO connections */
525 UINT16 handles[MAX_L2CAP_LINKS + 4];
528 /* Get the L2CAP numbers */
529 num_ents = l2c_link_pkts_rcvd (num_pkts, handles);
531 /* Get the SCO numbers */
532 /* No SCO for now ?? */
536 btsnd_hcic_host_num_xmitted_pkts (num_ents, handles, num_pkts);
540 /*******************************************************************************
542 ** Function btu_hcif_inquiry_comp_evt
544 ** Description Process event HCI_INQUIRY_COMP_EVT
548 *******************************************************************************/
549 static void btu_hcif_inquiry_comp_evt (UINT8 *p)
553 STREAM_TO_UINT8 (status, p);
555 /* Tell inquiry processing that we are done */
556 btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
560 /*******************************************************************************
562 ** Function btu_hcif_inquiry_result_evt
564 ** Description Process event HCI_INQUIRY_RESULT_EVT
568 *******************************************************************************/
569 static void btu_hcif_inquiry_result_evt (UINT8 *p)
571 /* Store results in the cache */
572 btm_process_inq_results (p, BTM_INQ_RESULT_STANDARD);
575 /*******************************************************************************
577 ** Function btu_hcif_inquiry_rssi_result_evt
579 ** Description Process event HCI_INQUIRY_RSSI_RESULT_EVT
583 *******************************************************************************/
584 static void btu_hcif_inquiry_rssi_result_evt (UINT8 *p)
586 /* Store results in the cache */
587 btm_process_inq_results (p, BTM_INQ_RESULT_WITH_RSSI);
590 /*******************************************************************************
592 ** Function btu_hcif_extended_inquiry_result_evt
594 ** Description Process event HCI_EXTENDED_INQUIRY_RESULT_EVT
598 *******************************************************************************/
599 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
600 static void btu_hcif_extended_inquiry_result_evt (UINT8 *p)
602 /* Store results in the cache */
603 btm_process_inq_results (p, BTM_INQ_RESULT_EXTENDED);
607 /*******************************************************************************
609 ** Function btu_hcif_connection_comp_evt
611 ** Description Process event HCI_CONNECTION_COMP_EVT
615 *******************************************************************************/
616 static void btu_hcif_connection_comp_evt (UINT8 *p)
623 #if BTM_SCO_INCLUDED == TRUE
624 tBTM_ESCO_DATA esco_data;
627 STREAM_TO_UINT8 (status, p);
628 STREAM_TO_UINT16 (handle, p);
629 STREAM_TO_BDADDR (bda, p);
630 STREAM_TO_UINT8 (link_type, p);
631 STREAM_TO_UINT8 (enc_mode, p);
633 handle = HCID_GET_HANDLE (handle);
635 if (link_type == HCI_LINK_TYPE_ACL)
637 btm_sec_connected (bda, handle, status, enc_mode);
639 l2c_link_hci_conn_comp (status, handle, bda);
641 #if BTM_SCO_INCLUDED == TRUE
644 memset(&esco_data, 0, sizeof(tBTM_ESCO_DATA));
645 /* esco_data.link_type = HCI_LINK_TYPE_SCO; already zero */
646 memcpy (esco_data.bd_addr, bda, BD_ADDR_LEN);
647 btm_sco_connected (status, bda, handle, &esco_data);
649 #endif /* BTM_SCO_INCLUDED */
653 /*******************************************************************************
655 ** Function btu_hcif_connection_request_evt
657 ** Description Process event HCI_CONNECTION_REQUEST_EVT
661 *******************************************************************************/
662 static void btu_hcif_connection_request_evt (UINT8 *p)
668 STREAM_TO_BDADDR (bda, p);
669 STREAM_TO_DEVCLASS (dc, p);
670 STREAM_TO_UINT8 (link_type, p);
672 /* Pass request to security manager to check connect filters before */
673 /* passing request to l2cap */
674 if (link_type == HCI_LINK_TYPE_ACL)
676 btm_sec_conn_req (bda, dc);
678 #if BTM_SCO_INCLUDED == TRUE
681 btm_sco_conn_req (bda, dc, link_type);
683 #endif /* BTM_SCO_INCLUDED */
687 /*******************************************************************************
689 ** Function btu_hcif_disconnection_comp_evt
691 ** Description Process event HCI_DISCONNECTION_COMP_EVT
695 *******************************************************************************/
696 static void btu_hcif_disconnection_comp_evt (UINT8 *p)
702 STREAM_TO_UINT8 (status, p);
703 STREAM_TO_UINT16 (handle, p);
704 STREAM_TO_UINT8 (reason, p);
706 handle = HCID_GET_HANDLE (handle);
708 #if BTM_SCO_INCLUDED == TRUE
709 /* If L2CAP doesn't know about it, send it to SCO */
710 if (!l2c_link_hci_disc_comp (handle, reason))
711 btm_sco_removed (handle, reason);
713 l2c_link_hci_disc_comp (handle, reason);
714 #endif /* BTM_SCO_INCLUDED */
716 /* Notify security manager */
717 btm_sec_disconnected (handle, reason);
720 /*******************************************************************************
722 ** Function btu_hcif_authentication_comp_evt
724 ** Description Process event HCI_AUTHENTICATION_COMP_EVT
728 *******************************************************************************/
729 static void btu_hcif_authentication_comp_evt (UINT8 *p)
734 STREAM_TO_UINT8 (status, p);
735 STREAM_TO_UINT16 (handle, p);
737 btm_sec_auth_complete (handle, status);
741 /*******************************************************************************
743 ** Function btu_hcif_rmt_name_request_comp_evt
745 ** Description Process event HCI_RMT_NAME_REQUEST_COMP_EVT
749 *******************************************************************************/
750 static void btu_hcif_rmt_name_request_comp_evt (UINT8 *p, UINT16 evt_len)
755 STREAM_TO_UINT8 (status, p);
756 STREAM_TO_BDADDR (bd_addr, p);
758 evt_len -= (1 + BD_ADDR_LEN);
760 btm_process_remote_name (bd_addr, p, evt_len, status);
762 btm_sec_rmt_name_request_complete (bd_addr, p, status);
766 /*******************************************************************************
768 ** Function btu_hcif_encryption_change_evt
770 ** Description Process event HCI_ENCRYPTION_CHANGE_EVT
774 *******************************************************************************/
775 static void btu_hcif_encryption_change_evt (UINT8 *p)
781 STREAM_TO_UINT8 (status, p);
782 STREAM_TO_UINT16 (handle, p);
783 STREAM_TO_UINT8 (encr_enable, p);
785 btm_acl_encrypt_change (handle, status, encr_enable);
786 btm_sec_encrypt_change (handle, status, encr_enable);
790 /*******************************************************************************
792 ** Function btu_hcif_change_conn_link_key_evt
794 ** Description Process event HCI_CHANGE_CONN_LINK_KEY_EVT
798 *******************************************************************************/
799 static void btu_hcif_change_conn_link_key_evt (UINT8 *p)
804 STREAM_TO_UINT8 (status, p);
805 STREAM_TO_UINT16 (handle, p);
807 btm_acl_link_key_change (handle, status);
811 /*******************************************************************************
813 ** Function btu_hcif_master_link_key_comp_evt
815 ** Description Process event HCI_MASTER_LINK_KEY_COMP_EVT
819 *******************************************************************************/
820 static void btu_hcif_master_link_key_comp_evt (UINT8 *p)
826 STREAM_TO_UINT8 (status, p);
827 STREAM_TO_UINT16 (handle, p);
828 STREAM_TO_UINT8 (key_flg, p);
830 btm_sec_mkey_comp_event (handle, status, key_flg);
834 /*******************************************************************************
836 ** Function btu_hcif_read_rmt_features_comp_evt
838 ** Description Process event HCI_READ_RMT_FEATURES_COMP_EVT
842 *******************************************************************************/
843 static void btu_hcif_read_rmt_features_comp_evt (UINT8 *p)
845 btm_read_remote_features_complete(p);
848 /*******************************************************************************
850 ** Function btu_hcif_read_rmt_ext_features_comp_evt
852 ** Description Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
856 *******************************************************************************/
857 static void btu_hcif_read_rmt_ext_features_comp_evt (UINT8 *p)
863 STREAM_TO_UINT8 (status, p_cur);
865 if (status == HCI_SUCCESS)
866 btm_read_remote_ext_features_complete(p);
869 STREAM_TO_UINT16 (handle, p_cur);
870 btm_read_remote_ext_features_failed(status, handle);
874 /*******************************************************************************
876 ** Function btu_hcif_read_rmt_version_comp_evt
878 ** Description Process event HCI_READ_RMT_VERSION_COMP_EVT
882 *******************************************************************************/
883 static void btu_hcif_read_rmt_version_comp_evt (UINT8 *p)
885 btm_read_remote_version_complete (p);
889 /*******************************************************************************
891 ** Function btu_hcif_qos_setup_comp_evt
893 ** Description Process event HCI_QOS_SETUP_COMP_EVT
897 *******************************************************************************/
898 static void btu_hcif_qos_setup_comp_evt (UINT8 *p)
904 STREAM_TO_UINT8 (status, p);
905 STREAM_TO_UINT16 (handle, p);
906 STREAM_TO_UINT8 (flow.qos_flags, p);
907 STREAM_TO_UINT8 (flow.service_type, p);
908 STREAM_TO_UINT32 (flow.token_rate, p);
909 STREAM_TO_UINT32 (flow.peak_bandwidth, p);
910 STREAM_TO_UINT32 (flow.latency, p);
911 STREAM_TO_UINT32 (flow.delay_variation, p);
913 btm_qos_setup_complete(status, handle, &flow);
917 /*******************************************************************************
919 ** Function btu_hcif_esco_connection_comp_evt
921 ** Description Process event HCI_ESCO_CONNECTION_COMP_EVT
925 *******************************************************************************/
926 static void btu_hcif_esco_connection_comp_evt (UINT8 *p)
928 #if BTM_SCO_INCLUDED == TRUE
934 STREAM_TO_UINT8 (status, p);
935 STREAM_TO_UINT16 (handle, p);
936 STREAM_TO_BDADDR (bda, p);
938 STREAM_TO_UINT8 (data.link_type, p);
939 STREAM_TO_UINT8 (data.tx_interval, p);
940 STREAM_TO_UINT8 (data.retrans_window, p);
941 STREAM_TO_UINT16 (data.rx_pkt_len, p);
942 STREAM_TO_UINT16 (data.tx_pkt_len, p);
943 STREAM_TO_UINT8 (data.air_mode, p);
945 memcpy (data.bd_addr, bda, BD_ADDR_LEN);
946 btm_sco_connected (status, bda, handle, &data);
951 /*******************************************************************************
953 ** Function btu_hcif_esco_connection_chg_evt
955 ** Description Process event HCI_ESCO_CONNECTION_CHANGED_EVT
959 *******************************************************************************/
960 static void btu_hcif_esco_connection_chg_evt (UINT8 *p)
962 #if BTM_SCO_INCLUDED == TRUE
968 UINT8 retrans_window;
970 STREAM_TO_UINT8 (status, p);
971 STREAM_TO_UINT16 (handle, p);
973 STREAM_TO_UINT8 (tx_interval, p);
974 STREAM_TO_UINT8 (retrans_window, p);
975 STREAM_TO_UINT16 (rx_pkt_len, p);
976 STREAM_TO_UINT16 (tx_pkt_len, p);
978 btm_esco_proc_conn_chg (status, handle, tx_interval, retrans_window,
979 rx_pkt_len, tx_pkt_len);
983 /*******************************************************************************
985 ** Function btu_hcif_hdl_command_complete
987 ** Description Handle command complete event
991 *******************************************************************************/
992 static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_len,
998 btm_reset_complete (); /* BR/EDR */
1001 case HCI_INQUIRY_CANCEL:
1002 /* Tell inquiry processing that we are done */
1003 btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
1005 case HCI_SET_EVENT_FILTER:
1006 btm_event_filter_complete (p);
1009 case HCI_READ_STORED_LINK_KEY:
1010 btm_read_stored_link_key_complete (p);
1013 case HCI_WRITE_STORED_LINK_KEY:
1014 btm_write_stored_link_key_complete (p);
1017 case HCI_DELETE_STORED_LINK_KEY:
1018 btm_delete_stored_link_key_complete (p);
1021 case HCI_READ_LOCAL_VERSION_INFO:
1022 btm_read_local_version_complete (p, evt_len);
1025 case HCI_READ_POLICY_SETTINGS:
1026 btm_read_link_policy_complete (p);
1029 case HCI_READ_BUFFER_SIZE:
1030 btm_read_hci_buf_size_complete (p, evt_len);
1033 case HCI_READ_LOCAL_SUPPORTED_CMDS:
1034 btm_read_local_supported_cmds_complete (p);
1037 case HCI_READ_LOCAL_FEATURES:
1038 btm_read_local_features_complete (p, evt_len);
1041 case HCI_READ_LOCAL_EXT_FEATURES:
1042 btm_read_local_ext_features_complete (p, evt_len);
1045 case HCI_READ_LOCAL_NAME:
1046 btm_read_local_name_complete (p, evt_len);
1049 case HCI_READ_BD_ADDR:
1050 btm_read_local_addr_complete (p, evt_len);
1053 case HCI_GET_LINK_QUALITY:
1054 btm_read_link_quality_complete (p);
1058 btm_read_rssi_complete (p);
1061 case HCI_READ_TRANSMIT_POWER_LEVEL:
1062 btm_read_tx_power_complete(p, FALSE);
1065 case HCI_CREATE_CONNECTION_CANCEL:
1066 btm_create_conn_cancel_complete(p);
1069 case HCI_READ_LOCAL_OOB_DATA:
1070 #if BTM_OOB_INCLUDED == TRUE
1071 btm_read_local_oob_complete(p);
1076 case HCI_READ_INQ_TX_POWER_LEVEL:
1077 btm_read_linq_tx_power_complete (p);
1080 case HCI_WRITE_SIMPLE_PAIRING_MODE:
1081 btm_write_simple_paring_mode_complete (p);
1084 case HCI_WRITE_LE_HOST_SUPPORTED:
1085 btm_write_le_host_supported_complete (p);
1088 #if (BLE_INCLUDED == TRUE)
1089 /* BLE Commands sComplete*/
1090 case HCI_BLE_READ_WHITE_LIST_SIZE :
1091 btm_read_white_list_size_complete(p, evt_len);
1094 case HCI_BLE_ADD_WHITE_LIST:
1095 btm_ble_add_2_white_list_complete(*p);
1098 case HCI_BLE_CLEAR_WHITE_LIST:
1099 btm_ble_clear_white_list_complete(p, evt_len);
1102 case HCI_BLE_REMOVE_WHITE_LIST:
1103 btm_ble_remove_from_white_list_complete(p, evt_len);
1107 case HCI_BLE_ENCRYPT:
1108 btm_ble_rand_enc_complete (p, opcode, (tBTM_RAND_ENC_CB *)p_cplt_cback);
1111 case HCI_BLE_READ_BUFFER_SIZE:
1112 btm_read_ble_buf_size_complete(p, evt_len);
1115 case HCI_BLE_READ_LOCAL_SPT_FEAT:
1116 btm_read_ble_local_supported_features_complete(p, evt_len);
1119 case HCI_BLE_READ_ADV_CHNL_TX_POWER:
1120 btm_read_tx_power_complete(p, TRUE);
1123 case HCI_BLE_WRITE_ADV_ENABLE:
1124 btm_ble_write_adv_enable_complete(p);
1127 case HCI_BLE_READ_SUPPORTED_STATES:
1128 btm_read_ble_local_supported_states_complete(p, evt_len);
1131 case HCI_BLE_CREATE_LL_CONN:
1132 btm_ble_create_ll_conn_complete(*p);
1135 case HCI_BLE_TRANSMITTER_TEST:
1136 case HCI_BLE_RECEIVER_TEST:
1137 case HCI_BLE_TEST_END:
1138 btm_ble_test_command_complete(p);
1140 #endif /* (BLE_INCLUDED == TRUE) */
1143 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1144 btm_vsc_complete (p, opcode, evt_len, (tBTM_CMPL_CB *)p_cplt_cback);
1149 /*******************************************************************************
1151 ** Function btu_hcif_command_complete_evt
1153 ** Description Process event HCI_COMMAND_COMPLETE_EVT
1157 *******************************************************************************/
1158 static void btu_hcif_command_complete_evt (UINT8 controller_id, UINT8 *p, UINT16 evt_len)
1160 tHCI_CMD_CB *p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1163 void *p_cplt_cback = NULL;
1165 STREAM_TO_UINT8 (p_hci_cmd_cb->cmd_window, p);
1167 #if (defined(HCI_MAX_SIMUL_CMDS) && (HCI_MAX_SIMUL_CMDS > 0))
1168 if (p_hci_cmd_cb->cmd_window > HCI_MAX_SIMUL_CMDS)
1169 p_hci_cmd_cb->cmd_window = HCI_MAX_SIMUL_CMDS;
1172 STREAM_TO_UINT16 (cc_opcode, p);
1176 /* only do this for certain commands */
1177 if ((cc_opcode != HCI_RESET) && (cc_opcode != HCI_HOST_NUM_PACKETS_DONE) &&
1178 (cc_opcode != HCI_COMMAND_NONE))
1180 /* dequeue and free stored command */
1182 /* always use cmd code check, when one cmd timeout waiting for cmd_cmpl,
1183 it'll cause the rest of the command goes in wrong order */
1184 p_cmd = (BT_HDR *) GKI_getfirst (&p_hci_cmd_cb->cmd_cmpl_q);
1188 UINT16 opcode_dequeued;
1191 /* Make sure dequeued command is for the command_cplt received */
1192 p_dequeued = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1193 STREAM_TO_UINT16 (opcode_dequeued, p_dequeued);
1195 if (opcode_dequeued != cc_opcode)
1197 /* opcode does not match, check next command in the queue */
1198 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
1201 GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
1203 /* If command was a VSC, then extract command_complete callback */
1204 if ((cc_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
1205 #if BLE_INCLUDED == TRUE
1206 || (cc_opcode == HCI_BLE_RAND )
1207 || (cc_opcode == HCI_BLE_ENCRYPT)
1211 p_cplt_cback = *((void **)(p_cmd + 1));
1214 GKI_freebuf (p_cmd);
1219 /* if more commands in queue restart timer */
1220 if (BTU_CMD_CMPL_TIMEOUT > 0)
1222 if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1224 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1225 (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1226 BTU_CMD_CMPL_TIMEOUT);
1230 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
1236 btu_hcif_hdl_command_complete (cc_opcode, p, evt_len, p_cplt_cback);
1238 /* see if we can send more commands */
1239 btu_hcif_send_cmd (controller_id, NULL);
1243 /*******************************************************************************
1245 ** Function btu_hcif_hdl_command_status
1247 ** Description Handle a command status event
1251 *******************************************************************************/
1252 static void btu_hcif_hdl_command_status (UINT16 opcode, UINT8 status, UINT8 *p_cmd,
1253 void *p_vsc_status_cback)
1257 #if BTM_SCO_INCLUDED == TRUE
1258 tBTM_ESCO_DATA esco_data;
1261 #if BTM_PWR_MGR_INCLUDED == TRUE
1264 case HCI_EXIT_SNIFF_MODE:
1265 case HCI_EXIT_PARK_MODE:
1266 #if BTM_SCO_WAKE_PARKED_LINK == TRUE
1267 if (status != HCI_SUCCESS)
1269 /* Allow SCO initiation to continue if waiting for change mode event */
1272 p_cmd++; /* bypass length field */
1273 STREAM_TO_UINT16 (handle, p_cmd);
1274 btm_sco_chk_pend_unpark (status, handle);
1278 /* Case Falls Through */
1281 case HCI_SNIFF_MODE:
1283 btm_pm_proc_cmd_status(status);
1287 #endif /* BTM_PWR_MGR_INCLUDED */
1288 /* If command failed to start, we may need to tell BTM */
1289 if (status != HCI_SUCCESS)
1294 /* Tell inquiry processing that we are done */
1295 btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
1298 case HCI_RMT_NAME_REQUEST:
1299 /* Tell inquiry processing that we are done */
1300 btm_process_remote_name (NULL, NULL, 0, status);
1302 btm_sec_rmt_name_request_complete (NULL, NULL, status);
1305 case HCI_CHANGE_CONN_LINK_KEY:
1306 /* Let host know we're done with error */
1307 /* read handle out of stored command */
1311 STREAM_TO_UINT16 (handle, p_cmd);
1313 btm_acl_link_key_change (handle, status);
1317 case HCI_QOS_SETUP_COMP_EVT:
1318 /* Tell qos setup that we are done */
1319 btm_qos_setup_complete(status,0,NULL);
1322 case HCI_SWITCH_ROLE:
1323 /* Tell BTM that the command failed */
1324 /* read bd addr out of stored command */
1328 STREAM_TO_BDADDR (bd_addr, p_cmd);
1329 btm_acl_role_changed(status, bd_addr, BTM_ROLE_UNDEFINED);
1332 btm_acl_role_changed(status, NULL, BTM_ROLE_UNDEFINED);
1333 l2c_link_role_changed (NULL, BTM_ROLE_UNDEFINED, HCI_ERR_COMMAND_DISALLOWED);
1336 case HCI_CREATE_CONNECTION:
1337 /* read bd addr out of stored command */
1341 STREAM_TO_BDADDR (bd_addr, p_cmd);
1342 btm_sec_connected (bd_addr, HCI_INVALID_HANDLE, status, 0);
1343 l2c_link_hci_conn_comp (status, HCI_INVALID_HANDLE, bd_addr);
1347 case HCI_READ_RMT_EXT_FEATURES:
1350 p_cmd++; /* skip command length */
1351 STREAM_TO_UINT16 (handle, p_cmd);
1354 handle = HCI_INVALID_HANDLE;
1356 btm_read_remote_ext_features_failed(status, handle);
1359 case HCI_AUTHENTICATION_REQUESTED:
1360 /* Device refused to start authentication. That should be treated as authentication failure. */
1361 btm_sec_auth_complete (BTM_INVALID_HCI_HANDLE, status);
1364 case HCI_SET_CONN_ENCRYPTION:
1365 /* Device refused to start encryption. That should be treated as encryption failure. */
1366 btm_sec_encrypt_change (BTM_INVALID_HCI_HANDLE, status, FALSE);
1369 #if BLE_INCLUDED == TRUE
1370 case HCI_BLE_CREATE_LL_CONN:
1371 btm_ble_create_ll_conn_complete(status);
1375 #if BTM_SCO_INCLUDED == TRUE
1376 case HCI_SETUP_ESCO_CONNECTION:
1377 /* read handle out of stored command */
1381 STREAM_TO_UINT16 (handle, p_cmd);
1383 /* Determine if initial connection failed or is a change of setup */
1384 if (btm_is_sco_active(handle))
1385 btm_esco_proc_conn_chg (status, handle, 0, 0, 0, 0);
1387 btm_sco_connected (status, NULL, handle, &esco_data);
1392 /* This is commented out until an upper layer cares about returning event
1393 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
1394 case HCI_ENHANCED_FLUSH:
1399 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1400 btm_vsc_complete (&status, opcode, 1, (tBTM_CMPL_CB *)p_vsc_status_cback);
1407 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1408 btm_vsc_complete (&status, opcode, 1, (tBTM_CMPL_CB *)p_vsc_status_cback);
1410 #if BTM_PWR_MGR_INCLUDED == TRUE
1415 /*******************************************************************************
1417 ** Function btu_hcif_command_status_evt
1419 ** Description Process event HCI_COMMAND_STATUS_EVT
1423 *******************************************************************************/
1424 static void btu_hcif_command_status_evt (UINT8 controller_id, UINT8 *p)
1426 tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1430 BT_HDR *p_cmd = NULL;
1431 UINT8 *p_data = NULL;
1432 void *p_vsc_status_cback = NULL;
1434 STREAM_TO_UINT8 (status, p);
1435 STREAM_TO_UINT8 (p_hci_cmd_cb->cmd_window, p);
1437 #if (defined(HCI_MAX_SIMUL_CMDS) && (HCI_MAX_SIMUL_CMDS > 0))
1438 if (p_hci_cmd_cb->cmd_window > HCI_MAX_SIMUL_CMDS)
1439 p_hci_cmd_cb->cmd_window = HCI_MAX_SIMUL_CMDS;
1442 STREAM_TO_UINT16 (opcode, p);
1444 /* only do this for certain commands */
1445 if ((opcode != HCI_RESET) && (opcode != HCI_HOST_NUM_PACKETS_DONE) &&
1446 (opcode != HCI_COMMAND_NONE))
1448 /*look for corresponding command in cmd_queue*/
1449 p_cmd = (BT_HDR *) GKI_getfirst(&(p_hci_cmd_cb->cmd_cmpl_q));
1452 p_data = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1453 STREAM_TO_UINT16 (cmd_opcode, p_data);
1455 /* Make sure this command is for the command_status received */
1456 if (cmd_opcode != opcode)
1458 /* opcode does not match, check next command in the queue */
1459 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
1464 GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
1466 /* If command was a VSC, then extract command_status callback */
1467 if ((cmd_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1469 p_vsc_status_cback = *((void **)(p_cmd + 1));
1475 /* if more commands in queue restart timer */
1476 if (BTU_CMD_CMPL_TIMEOUT > 0)
1478 if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1480 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1481 (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1482 BTU_CMD_CMPL_TIMEOUT);
1486 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
1491 /* handle command */
1492 btu_hcif_hdl_command_status (opcode, status, p_data, p_vsc_status_cback);
1494 /* free stored command */
1497 GKI_freebuf (p_cmd);
1501 HCI_TRACE_WARNING("No command in queue matching opcode %d", opcode);
1504 /* See if we can forward any more commands */
1505 btu_hcif_send_cmd (controller_id, NULL);
1508 /*******************************************************************************
1510 ** Function btu_hcif_cmd_timeout
1512 ** Description Handle a command timeout
1516 *******************************************************************************/
1517 void btu_hcif_cmd_timeout (UINT8 controller_id)
1519 tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1522 void *p_cplt_cback = NULL;
1526 /* set the controller cmd window to 1, as if we received a response, so
1527 ** the flow of commands from the stack doesn't hang */
1528 p_hci_cmd_cb->cmd_window = 1;
1530 /* get queued command */
1531 if ((p_cmd = (BT_HDR *) GKI_dequeue (&(p_hci_cmd_cb->cmd_cmpl_q))) == NULL)
1533 HCI_TRACE_WARNING("Cmd timeout; no cmd in queue");
1537 /* if more commands in queue restart timer */
1538 if (BTU_CMD_CMPL_TIMEOUT > 0)
1540 if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1542 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1543 (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1544 BTU_CMD_CMPL_TIMEOUT);
1548 p = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1549 #if (NFC_INCLUDED == TRUE)
1550 if (controller_id == NFC_CONTROLLER_ID)
1552 //TODO call nfc_ncif_cmd_timeout
1553 HCI_TRACE_WARNING("BTU NCI command timeout - header 0x%02x%02x", p[0], p[1]);
1558 /* get opcode from stored command */
1559 STREAM_TO_UINT16 (opcode, p);
1562 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE))
1563 ALOGE("######################################################################");
1565 ALOGE("# WARNING : BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
1567 ALOGE("######################################################################");
1569 HCI_TRACE_WARNING("BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
1573 /* send stack a fake command complete or command status, but first determine
1579 case HCI_SNIFF_MODE:
1580 case HCI_EXIT_SNIFF_MODE:
1582 case HCI_EXIT_PARK_MODE:
1584 case HCI_RMT_NAME_REQUEST:
1585 case HCI_QOS_SETUP_COMP_EVT:
1586 case HCI_CREATE_CONNECTION:
1587 case HCI_CHANGE_CONN_LINK_KEY:
1588 case HCI_SWITCH_ROLE:
1589 case HCI_READ_RMT_EXT_FEATURES:
1590 case HCI_AUTHENTICATION_REQUESTED:
1591 case HCI_SET_CONN_ENCRYPTION:
1592 #if BTM_SCO_INCLUDED == TRUE
1593 case HCI_SETUP_ESCO_CONNECTION:
1595 /* fake a command status */
1596 btu_hcif_hdl_command_status (opcode, HCI_ERR_UNSPECIFIED, p, NULL);
1600 /* If vendor specific restore the callback function */
1601 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
1602 #if BLE_INCLUDED == TRUE
1603 || (opcode == HCI_BLE_RAND ) ||
1604 (opcode == HCI_BLE_ENCRYPT)
1608 p_cplt_cback = *((void **)(p_cmd + 1));
1611 /* fake a command complete; first create a fake event */
1612 event = HCI_ERR_UNSPECIFIED;
1613 btu_hcif_hdl_command_complete (opcode, (UINT8 *)&event, 1, p_cplt_cback);
1617 /* free stored command */
1620 num_hci_cmds_timed_out++;
1621 /* When we receive consecutive HCI cmd timeouts for >=BTM_MAX_HCI_CMD_TOUT_BEFORE_RESTART
1622 times, Bluetooth process will be killed and restarted */
1623 if (num_hci_cmds_timed_out >= BTM_MAX_HCI_CMD_TOUT_BEFORE_RESTART)
1625 HCI_TRACE_ERROR("Num consecutive HCI Cmd tout =%d Restarting BT process",num_hci_cmds_timed_out);
1627 usleep(10000); /* 10 milliseconds */
1628 /* Killing the process to force a restart as part of fault tolerance */
1629 kill(getpid(), SIGKILL);
1633 HCI_TRACE_WARNING("HCI Cmd timeout counter %d", num_hci_cmds_timed_out);
1635 /* If anyone wants device status notifications, give him one */
1636 btm_report_device_status (BTM_DEV_STATUS_CMD_TOUT);
1638 /* See if we can forward any more commands */
1639 btu_hcif_send_cmd (controller_id, NULL);
1642 /*******************************************************************************
1644 ** Function btu_hcif_hardware_error_evt
1646 ** Description Process event HCI_HARDWARE_ERROR_EVT
1650 *******************************************************************************/
1651 static void btu_hcif_hardware_error_evt (UINT8 *p)
1653 HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
1655 /* If anyone wants device status notifications, give him one. */
1656 btm_report_device_status (BTM_DEV_STATUS_DOWN);
1658 /* Reset the controller */
1659 if (BTM_IsDeviceUp())
1660 BTM_DeviceReset (NULL);
1664 /*******************************************************************************
1666 ** Function btu_hcif_flush_occured_evt
1668 ** Description Process event HCI_FLUSH_OCCURED_EVT
1672 *******************************************************************************/
1673 static void btu_hcif_flush_occured_evt (void)
1678 /*******************************************************************************
1680 ** Function btu_hcif_role_change_evt
1682 ** Description Process event HCI_ROLE_CHANGE_EVT
1686 *******************************************************************************/
1687 static void btu_hcif_role_change_evt (UINT8 *p)
1693 STREAM_TO_UINT8 (status, p);
1694 STREAM_TO_BDADDR (bda, p);
1695 STREAM_TO_UINT8 (role, p);
1697 l2c_link_role_changed (bda, role, status);
1698 btm_acl_role_changed(status, bda, role);
1702 /*******************************************************************************
1704 ** Function btu_hcif_num_compl_data_pkts_evt
1706 ** Description Process event HCI_NUM_COMPL_DATA_PKTS_EVT
1710 *******************************************************************************/
1711 static void btu_hcif_num_compl_data_pkts_evt (UINT8 *p)
1713 /* Process for L2CAP and SCO */
1714 l2c_link_process_num_completed_pkts (p);
1716 /* Send on to SCO */
1717 /*?? No SCO for now */
1720 /*******************************************************************************
1722 ** Function btu_hcif_mode_change_evt
1724 ** Description Process event HCI_MODE_CHANGE_EVT
1728 *******************************************************************************/
1729 static void btu_hcif_mode_change_evt (UINT8 *p)
1736 STREAM_TO_UINT8 (status, p);
1738 STREAM_TO_UINT16 (handle, p);
1739 STREAM_TO_UINT8 (current_mode, p);
1740 STREAM_TO_UINT16 (interval, p);
1741 #if BTM_PWR_MGR_INCLUDED == TRUE
1742 #if BTM_SCO_WAKE_PARKED_LINK == TRUE
1743 btm_sco_chk_pend_unpark (status, handle);
1745 btm_pm_proc_mode_change (status, handle, current_mode, interval);
1747 btm_process_mode_change (status, handle, current_mode, interval);
1748 #endif /* BTM_PWR_MGR_INCLUDED == TRUE */
1750 #if (HID_DEV_INCLUDED == TRUE) && (HID_DEV_PM_INCLUDED == TRUE)
1751 hidd_pm_proc_mode_change( status, current_mode, interval ) ;
1755 /*******************************************************************************
1757 ** Function btu_hcif_ssr_evt
1759 ** Description Process event HCI_SNIFF_SUB_RATE_EVT
1763 *******************************************************************************/
1764 #if (BTM_SSR_INCLUDED == TRUE)
1765 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len)
1767 #if (BTM_PWR_MGR_INCLUDED == TRUE)
1768 btm_pm_proc_ssr_evt(p, evt_len);
1774 /*******************************************************************************
1776 ** Function btu_hcif_return_link_keys_evt
1778 ** Description Process event HCI_RETURN_LINK_KEYS_EVT
1782 *******************************************************************************/
1784 static void btu_hcif_return_link_keys_evt (UINT8 *p)
1787 tBTM_RETURN_LINK_KEYS_EVT *result;
1789 /* get the number of link keys */
1792 /* If there are no link keys don't call the call back */
1796 /* Take one extra byte at the beginning to specify event */
1797 result = (tBTM_RETURN_LINK_KEYS_EVT *)(--p);
1798 result->event = BTM_CB_EVT_RETURN_LINK_KEYS;
1800 /* Call the BTM function to pass the link keys to application */
1801 btm_return_link_keys_evt (result);
1805 /*******************************************************************************
1807 ** Function btu_hcif_pin_code_request_evt
1809 ** Description Process event HCI_PIN_CODE_REQUEST_EVT
1813 *******************************************************************************/
1814 static void btu_hcif_pin_code_request_evt (UINT8 *p)
1818 STREAM_TO_BDADDR (bda, p);
1820 /* Tell L2CAP that there was a PIN code request, */
1821 /* it may need to stretch timeouts */
1822 l2c_pin_code_request (bda);
1824 btm_sec_pin_code_request (bda);
1828 /*******************************************************************************
1830 ** Function btu_hcif_link_key_request_evt
1832 ** Description Process event HCI_LINK_KEY_REQUEST_EVT
1836 *******************************************************************************/
1837 static void btu_hcif_link_key_request_evt (UINT8 *p)
1841 STREAM_TO_BDADDR (bda, p);
1842 btm_sec_link_key_request (bda);
1846 /*******************************************************************************
1848 ** Function btu_hcif_link_key_notification_evt
1850 ** Description Process event HCI_LINK_KEY_NOTIFICATION_EVT
1854 *******************************************************************************/
1855 static void btu_hcif_link_key_notification_evt (UINT8 *p)
1861 STREAM_TO_BDADDR (bda, p);
1862 STREAM_TO_ARRAY16 (key, p);
1863 STREAM_TO_UINT8 (key_type, p);
1865 btm_sec_link_key_notification (bda, key, key_type);
1869 /*******************************************************************************
1871 ** Function btu_hcif_loopback_command_evt
1873 ** Description Process event HCI_LOOPBACK_COMMAND_EVT
1877 *******************************************************************************/
1878 static void btu_hcif_loopback_command_evt (void)
1883 /*******************************************************************************
1885 ** Function btu_hcif_data_buf_overflow_evt
1887 ** Description Process event HCI_DATA_BUF_OVERFLOW_EVT
1891 *******************************************************************************/
1892 static void btu_hcif_data_buf_overflow_evt (void)
1897 /*******************************************************************************
1899 ** Function btu_hcif_max_slots_changed_evt
1901 ** Description Process event HCI_MAX_SLOTS_CHANGED_EVT
1905 *******************************************************************************/
1906 static void btu_hcif_max_slots_changed_evt (void)
1911 /*******************************************************************************
1913 ** Function btu_hcif_read_clock_off_comp_evt
1915 ** Description Process event HCI_READ_CLOCK_OFF_COMP_EVT
1919 *******************************************************************************/
1920 static void btu_hcif_read_clock_off_comp_evt (UINT8 *p)
1924 UINT16 clock_offset;
1926 STREAM_TO_UINT8 (status, p);
1928 /* If failed to get clock offset just drop the result */
1929 if (status != HCI_SUCCESS)
1932 STREAM_TO_UINT16 (handle, p);
1933 STREAM_TO_UINT16 (clock_offset, p);
1935 handle = HCID_GET_HANDLE (handle);
1937 btm_process_clk_off_comp_evt (handle, clock_offset);
1938 btm_sec_update_clock_offset (handle, clock_offset);
1942 /*******************************************************************************
1944 ** Function btu_hcif_conn_pkt_type_change_evt
1946 ** Description Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
1950 *******************************************************************************/
1951 static void btu_hcif_conn_pkt_type_change_evt (void)
1956 /*******************************************************************************
1958 ** Function btu_hcif_qos_violation_evt
1960 ** Description Process event HCI_QOS_VIOLATION_EVT
1964 *******************************************************************************/
1965 static void btu_hcif_qos_violation_evt (UINT8 *p)
1969 STREAM_TO_UINT16 (handle, p);
1971 handle = HCID_GET_HANDLE (handle);
1974 l2c_link_hci_qos_violation (handle);
1978 /*******************************************************************************
1980 ** Function btu_hcif_page_scan_mode_change_evt
1982 ** Description Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
1986 *******************************************************************************/
1987 static void btu_hcif_page_scan_mode_change_evt (void)
1992 /*******************************************************************************
1994 ** Function btu_hcif_page_scan_rep_mode_chng_evt
1996 ** Description Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
2000 *******************************************************************************/
2001 static void btu_hcif_page_scan_rep_mode_chng_evt (void)
2005 /**********************************************
2006 ** Simple Pairing Events
2007 ***********************************************/
2009 /*******************************************************************************
2011 ** Function btu_hcif_host_support_evt
2013 ** Description Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
2017 *******************************************************************************/
2018 static void btu_hcif_host_support_evt (UINT8 *p)
2020 btm_sec_rmt_host_support_feat_evt(p);
2023 /*******************************************************************************
2025 ** Function btu_hcif_io_cap_request_evt
2027 ** Description Process event HCI_IO_CAPABILITY_REQUEST_EVT
2031 *******************************************************************************/
2032 static void btu_hcif_io_cap_request_evt (UINT8 *p)
2034 btm_io_capabilities_req(p);
2038 /*******************************************************************************
2040 ** Function btu_hcif_io_cap_response_evt
2042 ** Description Process event HCI_IO_CAPABILITY_RESPONSE_EVT
2046 *******************************************************************************/
2047 static void btu_hcif_io_cap_response_evt (UINT8 *p)
2049 btm_io_capabilities_rsp(p);
2053 /*******************************************************************************
2055 ** Function btu_hcif_user_conf_request_evt
2057 ** Description Process event HCI_USER_CONFIRMATION_REQUEST_EVT
2061 *******************************************************************************/
2062 static void btu_hcif_user_conf_request_evt (UINT8 *p)
2064 btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
2068 /*******************************************************************************
2070 ** Function btu_hcif_user_passkey_request_evt
2072 ** Description Process event HCI_USER_PASSKEY_REQUEST_EVT
2076 *******************************************************************************/
2077 static void btu_hcif_user_passkey_request_evt (UINT8 *p)
2079 btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
2082 /*******************************************************************************
2084 ** Function btu_hcif_user_passkey_notif_evt
2086 ** Description Process event HCI_USER_PASSKEY_NOTIFY_EVT
2090 *******************************************************************************/
2091 static void btu_hcif_user_passkey_notif_evt (UINT8 *p)
2093 btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
2096 /*******************************************************************************
2098 ** Function btu_hcif_keypress_notif_evt
2100 ** Description Process event HCI_KEYPRESS_NOTIFY_EVT
2104 *******************************************************************************/
2105 static void btu_hcif_keypress_notif_evt (UINT8 *p)
2107 btm_keypress_notif_evt(p);
2110 /*******************************************************************************
2112 ** Function btu_hcif_link_super_tout_evt
2114 ** Description Process event HCI_LINK_SUPER_TOUT_CHANGED_EVT
2118 *******************************************************************************/
2119 static void btu_hcif_link_super_tout_evt (UINT8 *p)
2121 UINT16 handle, timeout;
2122 STREAM_TO_UINT16 (handle, p);
2123 STREAM_TO_UINT16 (timeout, p);
2125 btm_proc_lsto_evt(handle, timeout);
2128 /*******************************************************************************
2130 ** Function btu_hcif_rem_oob_request_evt
2132 ** Description Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
2136 *******************************************************************************/
2137 #if BTM_OOB_INCLUDED == TRUE
2138 static void btu_hcif_rem_oob_request_evt (UINT8 *p)
2144 /*******************************************************************************
2146 ** Function btu_hcif_simple_pair_complete_evt
2148 ** Description Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
2152 *******************************************************************************/
2153 static void btu_hcif_simple_pair_complete_evt (UINT8 *p)
2155 btm_simple_pair_complete(p);
2157 /*******************************************************************************
2159 ** Function btu_hcif_flush_cmd_queue
2161 ** Description Flush the HCI command complete queue and transmit queue when
2166 *******************************************************************************/
2167 void btu_hcif_flush_cmd_queue(void)
2171 btu_cb.hci_cmd_cb[0].cmd_window = 0;
2172 while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_cmpl_q)) != NULL)
2174 GKI_freebuf (p_cmd);
2176 while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_xmit_q)) != NULL)
2178 GKI_freebuf (p_cmd);
2182 /*******************************************************************************
2184 ** Function btu_hcif_enhanced_flush_complete_evt
2186 ** Description Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
2190 *******************************************************************************/
2191 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
2192 static void btu_hcif_enhanced_flush_complete_evt (void)
2194 /* This is empty until an upper layer cares about returning event */
2197 /**********************************************
2198 ** End of Simple Pairing Events
2199 ***********************************************/
2202 /**********************************************
2204 ***********************************************/
2205 #if (defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE)
2206 static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p)
2209 UINT8 enc_enable = 0;
2212 STREAM_TO_UINT8 (status, p);
2213 STREAM_TO_UINT16 (handle, p);
2215 if (status == HCI_SUCCESS) enc_enable = 1;
2217 btm_sec_encrypt_change (handle, status, enc_enable);
2220 static void btu_ble_process_adv_pkt (UINT8 *p)
2222 HCI_TRACE_EVENT("btu_ble_process_adv_pkt");
2224 btm_ble_process_adv_pkt(p);
2227 static void btu_ble_ll_conn_complete_evt ( UINT8 *p, UINT16 evt_len)
2229 btm_ble_conn_complete(p, evt_len);
2232 static void btu_ble_ll_conn_param_upd_evt (UINT8 *p, UINT16 evt_len)
2234 /* LE connection update has completed successfully as a master. */
2235 /* We can enable the update request if the result is a success. */
2236 /* extract the HCI handle first */
2240 STREAM_TO_UINT8 (status, p);
2241 STREAM_TO_UINT16 (handle, p);
2242 l2cble_process_conn_update_evt(handle, status);
2245 static void btu_ble_read_remote_feat_evt (UINT8 *p)
2247 btm_ble_read_remote_features_complete(p);
2250 static void btu_ble_proc_ltk_req (UINT8 *p)
2252 UINT16 ediv, handle;
2255 STREAM_TO_UINT16(handle, p);
2257 STREAM_TO_UINT16(ediv, pp);
2258 #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
2259 btm_ble_ltk_request(handle, p, ediv);
2261 /* This is empty until an upper layer cares about returning event */
2263 /**********************************************
2264 ** End of BLE Events Handler
2265 ***********************************************/
2266 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
2267 static void btu_ble_rc_param_req_evt(UINT8 *p)
2270 UINT16 int_min, int_max, latency, timeout;
2272 STREAM_TO_UINT16(handle, p);
2273 STREAM_TO_UINT16(int_min, p);
2274 STREAM_TO_UINT16(int_max, p);
2275 STREAM_TO_UINT16(latency, p);
2276 STREAM_TO_UINT16(timeout, p);
2278 l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency, timeout);
2280 #endif /* BLE_LLT_INCLUDED */
2282 #endif /* BLE_INCLUDED */