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);
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 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
210 p_hci_cmd_cb->checked_hcisu = FALSE;
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);
218 /*******************************************************************************
220 ** Function btu_hcif_process_event
222 ** Description This function is called when an event is received from
223 ** the Host Controller.
227 *******************************************************************************/
228 void btu_hcif_process_event (UINT8 controller_id, BT_HDR *p_msg)
230 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
231 UINT8 hci_evt_code, hci_evt_len;
232 #if BLE_INCLUDED == TRUE
235 STREAM_TO_UINT8 (hci_evt_code, p);
236 STREAM_TO_UINT8 (hci_evt_len, p);
238 switch (hci_evt_code)
240 case HCI_INQUIRY_COMP_EVT:
241 btu_hcif_inquiry_comp_evt (p);
243 case HCI_INQUIRY_RESULT_EVT:
244 btu_hcif_inquiry_result_evt (p);
246 case HCI_INQUIRY_RSSI_RESULT_EVT:
247 btu_hcif_inquiry_rssi_result_evt (p);
249 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
250 case HCI_EXTENDED_INQUIRY_RESULT_EVT:
251 btu_hcif_extended_inquiry_result_evt (p);
254 case HCI_CONNECTION_COMP_EVT:
255 btu_hcif_connection_comp_evt (p);
257 case HCI_CONNECTION_REQUEST_EVT:
258 btu_hcif_connection_request_evt (p);
260 case HCI_DISCONNECTION_COMP_EVT:
261 btu_hcif_disconnection_comp_evt (p);
263 case HCI_AUTHENTICATION_COMP_EVT:
264 btu_hcif_authentication_comp_evt (p);
266 case HCI_RMT_NAME_REQUEST_COMP_EVT:
267 btu_hcif_rmt_name_request_comp_evt (p, hci_evt_len);
269 case HCI_ENCRYPTION_CHANGE_EVT:
270 btu_hcif_encryption_change_evt (p);
272 #if BLE_INCLUDED == TRUE
273 case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
274 btu_hcif_encryption_key_refresh_cmpl_evt(p);
277 case HCI_CHANGE_CONN_LINK_KEY_EVT:
278 btu_hcif_change_conn_link_key_evt (p);
280 case HCI_MASTER_LINK_KEY_COMP_EVT:
281 btu_hcif_master_link_key_comp_evt (p);
283 case HCI_READ_RMT_FEATURES_COMP_EVT:
284 btu_hcif_read_rmt_features_comp_evt (p);
286 case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
287 btu_hcif_read_rmt_ext_features_comp_evt (p);
289 case HCI_READ_RMT_VERSION_COMP_EVT:
290 btu_hcif_read_rmt_version_comp_evt (p);
292 case HCI_QOS_SETUP_COMP_EVT:
293 btu_hcif_qos_setup_comp_evt (p);
295 case HCI_COMMAND_COMPLETE_EVT:
296 btu_hcif_command_complete_evt (controller_id, p, hci_evt_len);
298 case HCI_COMMAND_STATUS_EVT:
299 btu_hcif_command_status_evt (controller_id, p);
301 case HCI_HARDWARE_ERROR_EVT:
302 btu_hcif_hardware_error_evt (p);
304 case HCI_FLUSH_OCCURED_EVT:
305 btu_hcif_flush_occured_evt ();
307 case HCI_ROLE_CHANGE_EVT:
308 btu_hcif_role_change_evt (p);
310 case HCI_NUM_COMPL_DATA_PKTS_EVT:
311 btu_hcif_num_compl_data_pkts_evt (p);
313 case HCI_MODE_CHANGE_EVT:
314 btu_hcif_mode_change_evt (p);
316 case HCI_RETURN_LINK_KEYS_EVT:
317 btu_hcif_return_link_keys_evt (p);
319 case HCI_PIN_CODE_REQUEST_EVT:
320 btu_hcif_pin_code_request_evt (p);
322 case HCI_LINK_KEY_REQUEST_EVT:
323 btu_hcif_link_key_request_evt (p);
325 case HCI_LINK_KEY_NOTIFICATION_EVT:
326 btu_hcif_link_key_notification_evt (p);
328 case HCI_LOOPBACK_COMMAND_EVT:
329 btu_hcif_loopback_command_evt ();
331 case HCI_DATA_BUF_OVERFLOW_EVT:
332 btu_hcif_data_buf_overflow_evt ();
334 case HCI_MAX_SLOTS_CHANGED_EVT:
335 btu_hcif_max_slots_changed_evt ();
337 case HCI_READ_CLOCK_OFF_COMP_EVT:
338 btu_hcif_read_clock_off_comp_evt (p);
340 case HCI_CONN_PKT_TYPE_CHANGE_EVT:
341 btu_hcif_conn_pkt_type_change_evt ();
343 case HCI_QOS_VIOLATION_EVT:
344 btu_hcif_qos_violation_evt (p);
346 case HCI_PAGE_SCAN_MODE_CHANGE_EVT:
347 btu_hcif_page_scan_mode_change_evt ();
349 case HCI_PAGE_SCAN_REP_MODE_CHNG_EVT:
350 btu_hcif_page_scan_rep_mode_chng_evt ();
352 case HCI_ESCO_CONNECTION_COMP_EVT:
353 btu_hcif_esco_connection_comp_evt (p);
355 case HCI_ESCO_CONNECTION_CHANGED_EVT:
356 btu_hcif_esco_connection_chg_evt (p);
358 #if (BTM_SSR_INCLUDED == TRUE)
359 case HCI_SNIFF_SUB_RATE_EVT:
360 btu_hcif_ssr_evt (p, hci_evt_len);
362 #endif /* BTM_SSR_INCLUDED == TRUE */
363 case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
364 btu_hcif_host_support_evt (p);
366 case HCI_IO_CAPABILITY_REQUEST_EVT:
367 btu_hcif_io_cap_request_evt (p);
369 case HCI_IO_CAPABILITY_RESPONSE_EVT:
370 btu_hcif_io_cap_response_evt (p);
372 case HCI_USER_CONFIRMATION_REQUEST_EVT:
373 btu_hcif_user_conf_request_evt (p);
375 case HCI_USER_PASSKEY_REQUEST_EVT:
376 btu_hcif_user_passkey_request_evt (p);
378 #if BTM_OOB_INCLUDED == TRUE
379 case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
380 btu_hcif_rem_oob_request_evt (p);
383 case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
384 btu_hcif_simple_pair_complete_evt (p);
386 case HCI_USER_PASSKEY_NOTIFY_EVT:
387 btu_hcif_user_passkey_notif_evt (p);
389 case HCI_KEYPRESS_NOTIFY_EVT:
390 btu_hcif_keypress_notif_evt (p);
392 case HCI_LINK_SUPER_TOUT_CHANGED_EVT:
393 btu_hcif_link_super_tout_evt (p);
395 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
396 case HCI_ENHANCED_FLUSH_COMPLETE_EVT:
397 btu_hcif_enhanced_flush_complete_evt ();
401 #if (BLE_INCLUDED == TRUE)
403 STREAM_TO_UINT8 (ble_sub_code, p);
405 HCI_TRACE_EVENT("BLE HCI(id=%d) event = 0x%02x)", hci_evt_code, ble_sub_code);
407 switch (ble_sub_code)
409 case HCI_BLE_ADV_PKT_RPT_EVT: /* result of inquiry */
410 btu_ble_process_adv_pkt(p);
412 case HCI_BLE_CONN_COMPLETE_EVT:
413 btu_ble_ll_conn_complete_evt(p, hci_evt_len);
415 case HCI_BLE_LL_CONN_PARAM_UPD_EVT:
416 btu_ble_ll_conn_param_upd_evt(p);
418 case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
419 btu_ble_read_remote_feat_evt(p);
421 case HCI_BLE_LTK_REQ_EVT: /* received only at slave device */
422 btu_ble_proc_ltk_req(p);
424 #if (BLE_LLT_INCLUDED == TRUE)
425 case HCI_BLE_RC_PARAM_REQ_EVT:
426 btu_ble_rc_param_req_evt(p);
432 #endif /* BLE_INCLUDED */
433 case HCI_VENDOR_SPECIFIC_EVT:
434 btm_vendor_specific_evt (p, hci_evt_len);
437 // reset the num_hci_cmds_timed_out upon receving any event from controller.
438 num_hci_cmds_timed_out = 0;
442 /*******************************************************************************
444 ** Function btu_hcif_send_cmd
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
452 *******************************************************************************/
453 void btu_hcif_send_cmd (UINT8 controller_id, BT_HDR *p_buf)
455 tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
457 #if ((L2CAP_HOST_FLOW_CTRL == TRUE)||defined(HCI_TESTER))
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))
465 GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
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)) )
474 p_hci_cmd_cb->cmd_window = p_hci_cmd_cb->cmd_xmit_q.count + 1;
477 /* See if we can send anything */
478 while (p_hci_cmd_cb->cmd_window != 0)
481 p_buf = (BT_HDR *)GKI_dequeue (&(p_hci_cmd_cb->cmd_xmit_q));
485 btu_hcif_store_cmd(controller_id, p_buf);
487 #if ((L2CAP_HOST_FLOW_CTRL == TRUE)||defined(HCI_TESTER))
488 pp = (UINT8 *)(p_buf + 1) + p_buf->offset;
490 STREAM_TO_UINT16 (code, pp);
493 * We do not need to decrease window for host flow control,
494 * host flow control does not receive an event back from controller
496 if (code != HCI_HOST_NUM_PACKETS_DONE)
498 p_hci_cmd_cb->cmd_window--;
500 if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
502 HCI_CMD_TO_LOWER(p_buf);
506 /* Unknown controller */
507 HCI_TRACE_WARNING("BTU HCI(ctrl id=%d) controller ID not recognized", controller_id);
518 GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
520 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
521 if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
523 /* check if controller can go to sleep */
524 btu_check_bt_sleep ();
531 /*******************************************************************************
533 ** Function btu_hcif_send_host_rdy_for_data
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
541 *******************************************************************************/
542 void btu_hcif_send_host_rdy_for_data(void)
544 UINT16 num_pkts[MAX_L2CAP_LINKS + 4]; /* 3 SCO connections */
545 UINT16 handles[MAX_L2CAP_LINKS + 4];
548 /* Get the L2CAP numbers */
549 num_ents = l2c_link_pkts_rcvd (num_pkts, handles);
551 /* Get the SCO numbers */
552 /* No SCO for now ?? */
556 btsnd_hcic_host_num_xmitted_pkts (num_ents, handles, num_pkts);
560 /*******************************************************************************
562 ** Function btu_hcif_inquiry_comp_evt
564 ** Description Process event HCI_INQUIRY_COMP_EVT
568 *******************************************************************************/
569 static void btu_hcif_inquiry_comp_evt (UINT8 *p)
573 STREAM_TO_UINT8 (status, p);
575 /* Tell inquiry processing that we are done */
576 btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
580 /*******************************************************************************
582 ** Function btu_hcif_inquiry_result_evt
584 ** Description Process event HCI_INQUIRY_RESULT_EVT
588 *******************************************************************************/
589 static void btu_hcif_inquiry_result_evt (UINT8 *p)
591 /* Store results in the cache */
592 btm_process_inq_results (p, BTM_INQ_RESULT_STANDARD);
595 /*******************************************************************************
597 ** Function btu_hcif_inquiry_rssi_result_evt
599 ** Description Process event HCI_INQUIRY_RSSI_RESULT_EVT
603 *******************************************************************************/
604 static void btu_hcif_inquiry_rssi_result_evt (UINT8 *p)
606 /* Store results in the cache */
607 btm_process_inq_results (p, BTM_INQ_RESULT_WITH_RSSI);
610 /*******************************************************************************
612 ** Function btu_hcif_extended_inquiry_result_evt
614 ** Description Process event HCI_EXTENDED_INQUIRY_RESULT_EVT
618 *******************************************************************************/
619 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
620 static void btu_hcif_extended_inquiry_result_evt (UINT8 *p)
622 /* Store results in the cache */
623 btm_process_inq_results (p, BTM_INQ_RESULT_EXTENDED);
627 /*******************************************************************************
629 ** Function btu_hcif_connection_comp_evt
631 ** Description Process event HCI_CONNECTION_COMP_EVT
635 *******************************************************************************/
636 static void btu_hcif_connection_comp_evt (UINT8 *p)
643 #if BTM_SCO_INCLUDED == TRUE
644 tBTM_ESCO_DATA esco_data;
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);
653 handle = HCID_GET_HANDLE (handle);
655 if (link_type == HCI_LINK_TYPE_ACL)
657 btm_sec_connected (bda, handle, status, enc_mode);
659 l2c_link_hci_conn_comp (status, handle, bda);
661 #if BTM_SCO_INCLUDED == TRUE
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);
669 #endif /* BTM_SCO_INCLUDED */
673 /*******************************************************************************
675 ** Function btu_hcif_connection_request_evt
677 ** Description Process event HCI_CONNECTION_REQUEST_EVT
681 *******************************************************************************/
682 static void btu_hcif_connection_request_evt (UINT8 *p)
688 STREAM_TO_BDADDR (bda, p);
689 STREAM_TO_DEVCLASS (dc, p);
690 STREAM_TO_UINT8 (link_type, p);
692 /* Pass request to security manager to check connect filters before */
693 /* passing request to l2cap */
694 if (link_type == HCI_LINK_TYPE_ACL)
696 btm_sec_conn_req (bda, dc);
698 #if BTM_SCO_INCLUDED == TRUE
701 btm_sco_conn_req (bda, dc, link_type);
703 #endif /* BTM_SCO_INCLUDED */
707 /*******************************************************************************
709 ** Function btu_hcif_disconnection_comp_evt
711 ** Description Process event HCI_DISCONNECTION_COMP_EVT
715 *******************************************************************************/
716 static void btu_hcif_disconnection_comp_evt (UINT8 *p)
722 STREAM_TO_UINT8 (status, p);
723 STREAM_TO_UINT16 (handle, p);
724 STREAM_TO_UINT8 (reason, p);
726 handle = HCID_GET_HANDLE (handle);
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);
733 l2c_link_hci_disc_comp (handle, reason);
734 #endif /* BTM_SCO_INCLUDED */
736 /* Notify security manager */
737 btm_sec_disconnected (handle, reason);
740 /*******************************************************************************
742 ** Function btu_hcif_authentication_comp_evt
744 ** Description Process event HCI_AUTHENTICATION_COMP_EVT
748 *******************************************************************************/
749 static void btu_hcif_authentication_comp_evt (UINT8 *p)
754 STREAM_TO_UINT8 (status, p);
755 STREAM_TO_UINT16 (handle, p);
757 btm_sec_auth_complete (handle, status);
761 /*******************************************************************************
763 ** Function btu_hcif_rmt_name_request_comp_evt
765 ** Description Process event HCI_RMT_NAME_REQUEST_COMP_EVT
769 *******************************************************************************/
770 static void btu_hcif_rmt_name_request_comp_evt (UINT8 *p, UINT16 evt_len)
775 STREAM_TO_UINT8 (status, p);
776 STREAM_TO_BDADDR (bd_addr, p);
778 evt_len -= (1 + BD_ADDR_LEN);
780 btm_process_remote_name (bd_addr, p, evt_len, status);
782 btm_sec_rmt_name_request_complete (bd_addr, p, status);
786 /*******************************************************************************
788 ** Function btu_hcif_encryption_change_evt
790 ** Description Process event HCI_ENCRYPTION_CHANGE_EVT
794 *******************************************************************************/
795 static void btu_hcif_encryption_change_evt (UINT8 *p)
801 STREAM_TO_UINT8 (status, p);
802 STREAM_TO_UINT16 (handle, p);
803 STREAM_TO_UINT8 (encr_enable, p);
805 btm_acl_encrypt_change (handle, status, encr_enable);
806 btm_sec_encrypt_change (handle, status, encr_enable);
810 /*******************************************************************************
812 ** Function btu_hcif_change_conn_link_key_evt
814 ** Description Process event HCI_CHANGE_CONN_LINK_KEY_EVT
818 *******************************************************************************/
819 static void btu_hcif_change_conn_link_key_evt (UINT8 *p)
824 STREAM_TO_UINT8 (status, p);
825 STREAM_TO_UINT16 (handle, p);
827 btm_acl_link_key_change (handle, status);
831 /*******************************************************************************
833 ** Function btu_hcif_master_link_key_comp_evt
835 ** Description Process event HCI_MASTER_LINK_KEY_COMP_EVT
839 *******************************************************************************/
840 static void btu_hcif_master_link_key_comp_evt (UINT8 *p)
846 STREAM_TO_UINT8 (status, p);
847 STREAM_TO_UINT16 (handle, p);
848 STREAM_TO_UINT8 (key_flg, p);
850 btm_sec_mkey_comp_event (handle, status, key_flg);
854 /*******************************************************************************
856 ** Function btu_hcif_read_rmt_features_comp_evt
858 ** Description Process event HCI_READ_RMT_FEATURES_COMP_EVT
862 *******************************************************************************/
863 static void btu_hcif_read_rmt_features_comp_evt (UINT8 *p)
865 btm_read_remote_features_complete(p);
868 /*******************************************************************************
870 ** Function btu_hcif_read_rmt_ext_features_comp_evt
872 ** Description Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
876 *******************************************************************************/
877 static void btu_hcif_read_rmt_ext_features_comp_evt (UINT8 *p)
883 STREAM_TO_UINT8 (status, p_cur);
885 if (status == HCI_SUCCESS)
886 btm_read_remote_ext_features_complete(p);
889 STREAM_TO_UINT16 (handle, p_cur);
890 btm_read_remote_ext_features_failed(status, handle);
894 /*******************************************************************************
896 ** Function btu_hcif_read_rmt_version_comp_evt
898 ** Description Process event HCI_READ_RMT_VERSION_COMP_EVT
902 *******************************************************************************/
903 static void btu_hcif_read_rmt_version_comp_evt (UINT8 *p)
905 btm_read_remote_version_complete (p);
909 /*******************************************************************************
911 ** Function btu_hcif_qos_setup_comp_evt
913 ** Description Process event HCI_QOS_SETUP_COMP_EVT
917 *******************************************************************************/
918 static void btu_hcif_qos_setup_comp_evt (UINT8 *p)
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);
933 btm_qos_setup_complete(status, handle, &flow);
937 /*******************************************************************************
939 ** Function btu_hcif_esco_connection_comp_evt
941 ** Description Process event HCI_ESCO_CONNECTION_COMP_EVT
945 *******************************************************************************/
946 static void btu_hcif_esco_connection_comp_evt (UINT8 *p)
948 #if BTM_SCO_INCLUDED == TRUE
954 STREAM_TO_UINT8 (status, p);
955 STREAM_TO_UINT16 (handle, p);
956 STREAM_TO_BDADDR (bda, p);
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);
965 memcpy (data.bd_addr, bda, BD_ADDR_LEN);
966 btm_sco_connected (status, bda, handle, &data);
971 /*******************************************************************************
973 ** Function btu_hcif_esco_connection_chg_evt
975 ** Description Process event HCI_ESCO_CONNECTION_CHANGED_EVT
979 *******************************************************************************/
980 static void btu_hcif_esco_connection_chg_evt (UINT8 *p)
982 #if BTM_SCO_INCLUDED == TRUE
988 UINT8 retrans_window;
990 STREAM_TO_UINT8 (status, p);
991 STREAM_TO_UINT16 (handle, p);
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);
998 btm_esco_proc_conn_chg (status, handle, tx_interval, retrans_window,
999 rx_pkt_len, tx_pkt_len);
1003 /*******************************************************************************
1005 ** Function btu_hcif_hdl_command_complete
1007 ** Description Handle command complete event
1011 *******************************************************************************/
1012 static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_len,
1018 btm_reset_complete (); /* BR/EDR */
1021 case HCI_INQUIRY_CANCEL:
1022 /* Tell inquiry processing that we are done */
1023 btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
1025 case HCI_SET_EVENT_FILTER:
1026 btm_event_filter_complete (p);
1029 case HCI_READ_STORED_LINK_KEY:
1030 btm_read_stored_link_key_complete (p);
1033 case HCI_WRITE_STORED_LINK_KEY:
1034 btm_write_stored_link_key_complete (p);
1037 case HCI_DELETE_STORED_LINK_KEY:
1038 btm_delete_stored_link_key_complete (p);
1041 case HCI_READ_LOCAL_VERSION_INFO:
1042 btm_read_local_version_complete (p, evt_len);
1045 case HCI_READ_POLICY_SETTINGS:
1046 btm_read_link_policy_complete (p);
1049 case HCI_READ_BUFFER_SIZE:
1050 btm_read_hci_buf_size_complete (p, evt_len);
1053 case HCI_READ_LOCAL_SUPPORTED_CMDS:
1054 btm_read_local_supported_cmds_complete (p);
1057 case HCI_READ_LOCAL_FEATURES:
1058 btm_read_local_features_complete (p, evt_len);
1061 case HCI_READ_LOCAL_EXT_FEATURES:
1062 btm_read_local_ext_features_complete (p, evt_len);
1065 case HCI_READ_LOCAL_NAME:
1066 btm_read_local_name_complete (p, evt_len);
1069 case HCI_READ_BD_ADDR:
1070 btm_read_local_addr_complete (p, evt_len);
1073 case HCI_GET_LINK_QUALITY:
1074 btm_read_link_quality_complete (p);
1078 btm_read_rssi_complete (p);
1081 case HCI_READ_TRANSMIT_POWER_LEVEL:
1082 btm_read_tx_power_complete(p, FALSE);
1085 case HCI_CREATE_CONNECTION_CANCEL:
1086 btm_create_conn_cancel_complete(p);
1089 case HCI_READ_LOCAL_OOB_DATA:
1090 #if BTM_OOB_INCLUDED == TRUE
1091 btm_read_local_oob_complete(p);
1096 case HCI_READ_INQ_TX_POWER_LEVEL:
1097 btm_read_linq_tx_power_complete (p);
1100 case HCI_WRITE_SIMPLE_PAIRING_MODE:
1101 btm_write_simple_paring_mode_complete (p);
1104 case HCI_WRITE_LE_HOST_SUPPORTED:
1105 btm_write_le_host_supported_complete (p);
1108 #if (BLE_INCLUDED == TRUE)
1110 case HCI_BLE_READ_WHITE_LIST_SIZE :
1111 btm_read_white_list_size_complete(p, evt_len);
1114 case HCI_BLE_ADD_WHITE_LIST:
1115 btm_ble_add_2_white_list_complete(*p);
1118 case HCI_BLE_CLEAR_WHITE_LIST:
1119 btm_ble_clear_white_list_complete(p, evt_len);
1122 case HCI_BLE_REMOVE_WHITE_LIST:
1123 btm_ble_remove_from_white_list_complete(p, evt_len);
1127 case HCI_BLE_ENCRYPT:
1129 btm_ble_rand_enc_complete (p, opcode, (tBTM_RAND_ENC_CB *)p_cplt_cback);
1131 case HCI_BLE_READ_BUFFER_SIZE:
1132 btm_read_ble_buf_size_complete(p, evt_len);
1135 case HCI_BLE_READ_LOCAL_SPT_FEAT:
1136 btm_read_ble_local_supported_features_complete(p, evt_len);
1139 case HCI_BLE_READ_ADV_CHNL_TX_POWER:
1140 btm_read_tx_power_complete(p, TRUE);
1143 case HCI_BLE_WRITE_ADV_ENABLE:
1144 btm_ble_write_adv_enable_complete(p);
1147 case HCI_BLE_READ_SUPPORTED_STATES:
1148 btm_read_ble_local_supported_states_complete(p, evt_len);
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);
1156 #endif /* (BLE_INCLUDED == TRUE) */
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);
1165 /*******************************************************************************
1167 ** Function btu_hcif_command_complete_evt
1169 ** Description Process event HCI_COMMAND_COMPLETE_EVT
1173 *******************************************************************************/
1174 static void btu_hcif_command_complete_evt (UINT8 controller_id, UINT8 *p, UINT16 evt_len)
1176 tHCI_CMD_CB *p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1179 void *p_cplt_cback = NULL;
1181 STREAM_TO_UINT8 (p_hci_cmd_cb->cmd_window, p);
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;
1188 STREAM_TO_UINT16 (cc_opcode, p);
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))
1196 /* dequeue and free stored command */
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);
1204 UINT16 opcode_dequeued;
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);
1211 if (opcode_dequeued != cc_opcode)
1213 /* opcode does not match, check next command in the queue */
1214 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
1217 GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
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)
1227 p_cplt_cback = *((void **)(p_cmd + 1));
1230 GKI_freebuf (p_cmd);
1235 /* if more commands in queue restart timer */
1236 if (BTU_CMD_CMPL_TIMEOUT > 0)
1238 if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1240 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
1241 p_hci_cmd_cb->checked_hcisu = FALSE;
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);
1249 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
1255 btu_hcif_hdl_command_complete (cc_opcode, p, evt_len, p_cplt_cback);
1257 /* see if we can send more commands */
1258 btu_hcif_send_cmd (controller_id, NULL);
1262 /*******************************************************************************
1264 ** Function btu_hcif_hdl_command_status
1266 ** Description Handle a command status event
1270 *******************************************************************************/
1271 static void btu_hcif_hdl_command_status (UINT16 opcode, UINT8 status, UINT8 *p_cmd,
1272 void *p_vsc_status_cback)
1276 #if BTM_SCO_INCLUDED == TRUE
1277 tBTM_ESCO_DATA esco_data;
1280 #if BTM_PWR_MGR_INCLUDED == TRUE
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)
1288 /* Allow SCO initiation to continue if waiting for change mode event */
1291 p_cmd++; /* bypass length field */
1292 STREAM_TO_UINT16 (handle, p_cmd);
1293 btm_sco_chk_pend_unpark (status, handle);
1297 /* Case Falls Through */
1300 case HCI_SNIFF_MODE:
1302 btm_pm_proc_cmd_status(status);
1306 #endif /* BTM_PWR_MGR_INCLUDED */
1307 /* If command failed to start, we may need to tell BTM */
1308 if (status != HCI_SUCCESS)
1313 /* Tell inquiry processing that we are done */
1314 btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
1317 case HCI_RMT_NAME_REQUEST:
1318 /* Tell inquiry processing that we are done */
1319 btm_process_remote_name (NULL, NULL, 0, status);
1321 btm_sec_rmt_name_request_complete (NULL, NULL, status);
1324 case HCI_CHANGE_CONN_LINK_KEY:
1325 /* Let host know we're done with error */
1326 /* read handle out of stored command */
1330 STREAM_TO_UINT16 (handle, p_cmd);
1332 btm_acl_link_key_change (handle, status);
1336 case HCI_QOS_SETUP_COMP_EVT:
1337 /* Tell qos setup that we are done */
1338 btm_qos_setup_complete(status,0,NULL);
1341 case HCI_SWITCH_ROLE:
1342 /* Tell BTM that the command failed */
1343 /* read bd addr out of stored command */
1347 STREAM_TO_BDADDR (bd_addr, p_cmd);
1348 btm_acl_role_changed(status, bd_addr, BTM_ROLE_UNDEFINED);
1351 btm_acl_role_changed(status, NULL, BTM_ROLE_UNDEFINED);
1352 l2c_link_role_changed (NULL, BTM_ROLE_UNDEFINED, HCI_ERR_COMMAND_DISALLOWED);
1355 case HCI_CREATE_CONNECTION:
1356 /* read bd addr out of stored command */
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);
1366 case HCI_READ_RMT_EXT_FEATURES:
1369 p_cmd++; /* skip command length */
1370 STREAM_TO_UINT16 (handle, p_cmd);
1373 handle = HCI_INVALID_HANDLE;
1375 btm_read_remote_ext_features_failed(status, handle);
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);
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);
1388 #if BTM_SCO_INCLUDED == TRUE
1389 case HCI_SETUP_ESCO_CONNECTION:
1390 /* read handle out of stored command */
1394 STREAM_TO_UINT16 (handle, p_cmd);
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);
1400 btm_sco_connected (status, NULL, handle, &esco_data);
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:
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);
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);
1423 #if BTM_PWR_MGR_INCLUDED == TRUE
1428 /*******************************************************************************
1430 ** Function btu_hcif_command_status_evt
1432 ** Description Process event HCI_COMMAND_STATUS_EVT
1436 *******************************************************************************/
1437 static void btu_hcif_command_status_evt (UINT8 controller_id, UINT8 *p)
1439 tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1443 BT_HDR *p_cmd = NULL;
1444 UINT8 *p_data = NULL;
1445 void *p_vsc_status_cback = NULL;
1447 STREAM_TO_UINT8 (status, p);
1448 STREAM_TO_UINT8 (p_hci_cmd_cb->cmd_window, p);
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;
1455 STREAM_TO_UINT16 (opcode, p);
1457 /* only do this for certain commands */
1458 if ((opcode != HCI_RESET) && (opcode != HCI_HOST_NUM_PACKETS_DONE) &&
1459 (opcode != HCI_COMMAND_NONE))
1461 /*look for corresponding command in cmd_queue*/
1462 p_cmd = (BT_HDR *) GKI_getfirst(&(p_hci_cmd_cb->cmd_cmpl_q));
1465 p_data = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1466 STREAM_TO_UINT16 (cmd_opcode, p_data);
1468 /* Make sure this command is for the command_status received */
1469 if (cmd_opcode != opcode)
1471 /* opcode does not match, check next command in the queue */
1472 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
1477 GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
1479 /* If command was a VSC, then extract command_status callback */
1480 if ((cmd_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1482 p_vsc_status_cback = *((void **)(p_cmd + 1));
1488 /* if more commands in queue restart timer */
1489 if (BTU_CMD_CMPL_TIMEOUT > 0)
1491 if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1493 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
1494 p_hci_cmd_cb->checked_hcisu = FALSE;
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);
1502 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
1507 /* handle command */
1508 btu_hcif_hdl_command_status (opcode, status, p_data, p_vsc_status_cback);
1510 /* free stored command */
1513 GKI_freebuf (p_cmd);
1517 HCI_TRACE_WARNING("No command in queue matching opcode %d", opcode);
1520 /* See if we can forward any more commands */
1521 btu_hcif_send_cmd (controller_id, NULL);
1524 /*******************************************************************************
1526 ** Function btu_hcif_cmd_timeout
1528 ** Description Handle a command timeout
1532 *******************************************************************************/
1533 void btu_hcif_cmd_timeout (UINT8 controller_id)
1535 tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1538 void *p_cplt_cback = NULL;
1542 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
1543 if (!(p_hci_cmd_cb->checked_hcisu))
1545 HCI_TRACE_WARNING("BTU HCI(id=%d) command timeout - double check HCISU", controller_id);
1547 /* trigger HCISU to read any pending data in transport buffer */
1548 GKI_send_event(HCISU_TASK, HCISU_EVT_MASK);
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. */
1554 p_hci_cmd_cb->checked_hcisu = TRUE;
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;
1564 /* get queued command */
1565 if ((p_cmd = (BT_HDR *) GKI_dequeue (&(p_hci_cmd_cb->cmd_cmpl_q))) == NULL)
1567 HCI_TRACE_WARNING("Cmd timeout; no cmd in queue");
1571 /* if more commands in queue restart timer */
1572 if (BTU_CMD_CMPL_TIMEOUT > 0)
1574 if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1576 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
1577 p_hci_cmd_cb->checked_hcisu = FALSE;
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);
1585 p = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1586 #if (NFC_INCLUDED == TRUE)
1587 if (controller_id == NFC_CONTROLLER_ID)
1589 //TODO call nfc_ncif_cmd_timeout
1590 HCI_TRACE_WARNING("BTU NCI command timeout - header 0x%02x%02x", p[0], p[1]);
1595 /* get opcode from stored command */
1596 STREAM_TO_UINT16 (opcode, p);
1599 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE))
1600 ALOGE("######################################################################");
1602 ALOGE("# WARNING : BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
1604 ALOGE("######################################################################");
1606 HCI_TRACE_WARNING("BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
1610 /* send stack a fake command complete or command status, but first determine
1616 case HCI_SNIFF_MODE:
1617 case HCI_EXIT_SNIFF_MODE:
1619 case HCI_EXIT_PARK_MODE:
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:
1632 /* fake a command status */
1633 btu_hcif_hdl_command_status (opcode, HCI_ERR_UNSPECIFIED, p, NULL);
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)
1645 p_cplt_cback = *((void **)(p_cmd + 1));
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);
1654 /* free stored command */
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)
1662 HCI_TRACE_ERROR("Num consecutive HCI Cmd tout =%d Restarting BT process",num_hci_cmds_timed_out);
1664 usleep(10000); /* 10 milliseconds */
1665 /* Killing the process to force a restart as part of fault tolerance */
1666 kill(getpid(), SIGKILL);
1670 HCI_TRACE_WARNING("HCI Cmd timeout counter %d", num_hci_cmds_timed_out);
1672 /* If anyone wants device status notifications, give him one */
1673 btm_report_device_status (BTM_DEV_STATUS_CMD_TOUT);
1675 /* See if we can forward any more commands */
1676 btu_hcif_send_cmd (controller_id, NULL);
1679 /*******************************************************************************
1681 ** Function btu_hcif_hardware_error_evt
1683 ** Description Process event HCI_HARDWARE_ERROR_EVT
1687 *******************************************************************************/
1688 static void btu_hcif_hardware_error_evt (UINT8 *p)
1690 HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
1692 /* If anyone wants device status notifications, give him one. */
1693 btm_report_device_status (BTM_DEV_STATUS_DOWN);
1695 /* Reset the controller */
1696 if (BTM_IsDeviceUp())
1697 BTM_DeviceReset (NULL);
1701 /*******************************************************************************
1703 ** Function btu_hcif_flush_occured_evt
1705 ** Description Process event HCI_FLUSH_OCCURED_EVT
1709 *******************************************************************************/
1710 static void btu_hcif_flush_occured_evt (void)
1715 /*******************************************************************************
1717 ** Function btu_hcif_role_change_evt
1719 ** Description Process event HCI_ROLE_CHANGE_EVT
1723 *******************************************************************************/
1724 static void btu_hcif_role_change_evt (UINT8 *p)
1730 STREAM_TO_UINT8 (status, p);
1731 STREAM_TO_BDADDR (bda, p);
1732 STREAM_TO_UINT8 (role, p);
1734 l2c_link_role_changed (bda, role, status);
1735 btm_acl_role_changed(status, bda, role);
1739 /*******************************************************************************
1741 ** Function btu_hcif_num_compl_data_pkts_evt
1743 ** Description Process event HCI_NUM_COMPL_DATA_PKTS_EVT
1747 *******************************************************************************/
1748 static void btu_hcif_num_compl_data_pkts_evt (UINT8 *p)
1750 /* Process for L2CAP and SCO */
1751 l2c_link_process_num_completed_pkts (p);
1753 /* Send on to SCO */
1754 /*?? No SCO for now */
1757 /*******************************************************************************
1759 ** Function btu_hcif_mode_change_evt
1761 ** Description Process event HCI_MODE_CHANGE_EVT
1765 *******************************************************************************/
1766 static void btu_hcif_mode_change_evt (UINT8 *p)
1773 STREAM_TO_UINT8 (status, p);
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);
1782 btm_pm_proc_mode_change (status, handle, current_mode, interval);
1784 btm_process_mode_change (status, handle, current_mode, interval);
1785 #endif /* BTM_PWR_MGR_INCLUDED == TRUE */
1787 #if (HID_DEV_INCLUDED == TRUE) && (HID_DEV_PM_INCLUDED == TRUE)
1788 hidd_pm_proc_mode_change( status, current_mode, interval ) ;
1792 /*******************************************************************************
1794 ** Function btu_hcif_ssr_evt
1796 ** Description Process event HCI_SNIFF_SUB_RATE_EVT
1800 *******************************************************************************/
1801 #if (BTM_SSR_INCLUDED == TRUE)
1802 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len)
1804 #if (BTM_PWR_MGR_INCLUDED == TRUE)
1805 btm_pm_proc_ssr_evt(p, evt_len);
1811 /*******************************************************************************
1813 ** Function btu_hcif_return_link_keys_evt
1815 ** Description Process event HCI_RETURN_LINK_KEYS_EVT
1819 *******************************************************************************/
1821 static void btu_hcif_return_link_keys_evt (UINT8 *p)
1824 tBTM_RETURN_LINK_KEYS_EVT *result;
1826 /* get the number of link keys */
1829 /* If there are no link keys don't call the call back */
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;
1837 /* Call the BTM function to pass the link keys to application */
1838 btm_return_link_keys_evt (result);
1842 /*******************************************************************************
1844 ** Function btu_hcif_pin_code_request_evt
1846 ** Description Process event HCI_PIN_CODE_REQUEST_EVT
1850 *******************************************************************************/
1851 static void btu_hcif_pin_code_request_evt (UINT8 *p)
1855 STREAM_TO_BDADDR (bda, p);
1857 /* Tell L2CAP that there was a PIN code request, */
1858 /* it may need to stretch timeouts */
1859 l2c_pin_code_request (bda);
1861 btm_sec_pin_code_request (bda);
1865 /*******************************************************************************
1867 ** Function btu_hcif_link_key_request_evt
1869 ** Description Process event HCI_LINK_KEY_REQUEST_EVT
1873 *******************************************************************************/
1874 static void btu_hcif_link_key_request_evt (UINT8 *p)
1878 STREAM_TO_BDADDR (bda, p);
1879 btm_sec_link_key_request (bda);
1883 /*******************************************************************************
1885 ** Function btu_hcif_link_key_notification_evt
1887 ** Description Process event HCI_LINK_KEY_NOTIFICATION_EVT
1891 *******************************************************************************/
1892 static void btu_hcif_link_key_notification_evt (UINT8 *p)
1898 STREAM_TO_BDADDR (bda, p);
1899 STREAM_TO_ARRAY16 (key, p);
1900 STREAM_TO_UINT8 (key_type, p);
1902 btm_sec_link_key_notification (bda, key, key_type);
1906 /*******************************************************************************
1908 ** Function btu_hcif_loopback_command_evt
1910 ** Description Process event HCI_LOOPBACK_COMMAND_EVT
1914 *******************************************************************************/
1915 static void btu_hcif_loopback_command_evt (void)
1920 /*******************************************************************************
1922 ** Function btu_hcif_data_buf_overflow_evt
1924 ** Description Process event HCI_DATA_BUF_OVERFLOW_EVT
1928 *******************************************************************************/
1929 static void btu_hcif_data_buf_overflow_evt (void)
1934 /*******************************************************************************
1936 ** Function btu_hcif_max_slots_changed_evt
1938 ** Description Process event HCI_MAX_SLOTS_CHANGED_EVT
1942 *******************************************************************************/
1943 static void btu_hcif_max_slots_changed_evt (void)
1948 /*******************************************************************************
1950 ** Function btu_hcif_read_clock_off_comp_evt
1952 ** Description Process event HCI_READ_CLOCK_OFF_COMP_EVT
1956 *******************************************************************************/
1957 static void btu_hcif_read_clock_off_comp_evt (UINT8 *p)
1961 UINT16 clock_offset;
1963 STREAM_TO_UINT8 (status, p);
1965 /* If failed to get clock offset just drop the result */
1966 if (status != HCI_SUCCESS)
1969 STREAM_TO_UINT16 (handle, p);
1970 STREAM_TO_UINT16 (clock_offset, p);
1972 handle = HCID_GET_HANDLE (handle);
1974 btm_process_clk_off_comp_evt (handle, clock_offset);
1975 btm_sec_update_clock_offset (handle, clock_offset);
1979 /*******************************************************************************
1981 ** Function btu_hcif_conn_pkt_type_change_evt
1983 ** Description Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
1987 *******************************************************************************/
1988 static void btu_hcif_conn_pkt_type_change_evt (void)
1993 /*******************************************************************************
1995 ** Function btu_hcif_qos_violation_evt
1997 ** Description Process event HCI_QOS_VIOLATION_EVT
2001 *******************************************************************************/
2002 static void btu_hcif_qos_violation_evt (UINT8 *p)
2006 STREAM_TO_UINT16 (handle, p);
2008 handle = HCID_GET_HANDLE (handle);
2011 l2c_link_hci_qos_violation (handle);
2015 /*******************************************************************************
2017 ** Function btu_hcif_page_scan_mode_change_evt
2019 ** Description Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
2023 *******************************************************************************/
2024 static void btu_hcif_page_scan_mode_change_evt (void)
2029 /*******************************************************************************
2031 ** Function btu_hcif_page_scan_rep_mode_chng_evt
2033 ** Description Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
2037 *******************************************************************************/
2038 static void btu_hcif_page_scan_rep_mode_chng_evt (void)
2042 /**********************************************
2043 ** Simple Pairing Events
2044 ***********************************************/
2046 /*******************************************************************************
2048 ** Function btu_hcif_host_support_evt
2050 ** Description Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
2054 *******************************************************************************/
2055 static void btu_hcif_host_support_evt (UINT8 *p)
2057 btm_sec_rmt_host_support_feat_evt(p);
2060 /*******************************************************************************
2062 ** Function btu_hcif_io_cap_request_evt
2064 ** Description Process event HCI_IO_CAPABILITY_REQUEST_EVT
2068 *******************************************************************************/
2069 static void btu_hcif_io_cap_request_evt (UINT8 *p)
2071 btm_io_capabilities_req(p);
2075 /*******************************************************************************
2077 ** Function btu_hcif_io_cap_response_evt
2079 ** Description Process event HCI_IO_CAPABILITY_RESPONSE_EVT
2083 *******************************************************************************/
2084 static void btu_hcif_io_cap_response_evt (UINT8 *p)
2086 btm_io_capabilities_rsp(p);
2090 /*******************************************************************************
2092 ** Function btu_hcif_user_conf_request_evt
2094 ** Description Process event HCI_USER_CONFIRMATION_REQUEST_EVT
2098 *******************************************************************************/
2099 static void btu_hcif_user_conf_request_evt (UINT8 *p)
2101 btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
2105 /*******************************************************************************
2107 ** Function btu_hcif_user_passkey_request_evt
2109 ** Description Process event HCI_USER_PASSKEY_REQUEST_EVT
2113 *******************************************************************************/
2114 static void btu_hcif_user_passkey_request_evt (UINT8 *p)
2116 btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
2119 /*******************************************************************************
2121 ** Function btu_hcif_user_passkey_notif_evt
2123 ** Description Process event HCI_USER_PASSKEY_NOTIFY_EVT
2127 *******************************************************************************/
2128 static void btu_hcif_user_passkey_notif_evt (UINT8 *p)
2130 btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
2133 /*******************************************************************************
2135 ** Function btu_hcif_keypress_notif_evt
2137 ** Description Process event HCI_KEYPRESS_NOTIFY_EVT
2141 *******************************************************************************/
2142 static void btu_hcif_keypress_notif_evt (UINT8 *p)
2144 btm_keypress_notif_evt(p);
2147 /*******************************************************************************
2149 ** Function btu_hcif_link_super_tout_evt
2151 ** Description Process event HCI_LINK_SUPER_TOUT_CHANGED_EVT
2155 *******************************************************************************/
2156 static void btu_hcif_link_super_tout_evt (UINT8 *p)
2158 UINT16 handle, timeout;
2159 STREAM_TO_UINT16 (handle, p);
2160 STREAM_TO_UINT16 (timeout, p);
2162 btm_proc_lsto_evt(handle, timeout);
2165 /*******************************************************************************
2167 ** Function btu_hcif_rem_oob_request_evt
2169 ** Description Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
2173 *******************************************************************************/
2174 #if BTM_OOB_INCLUDED == TRUE
2175 static void btu_hcif_rem_oob_request_evt (UINT8 *p)
2181 /*******************************************************************************
2183 ** Function btu_hcif_simple_pair_complete_evt
2185 ** Description Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
2189 *******************************************************************************/
2190 static void btu_hcif_simple_pair_complete_evt (UINT8 *p)
2192 btm_simple_pair_complete(p);
2194 /*******************************************************************************
2196 ** Function btu_hcif_flush_cmd_queue
2198 ** Description Flush the HCI command complete queue and transmit queue when
2203 *******************************************************************************/
2204 void btu_hcif_flush_cmd_queue(void)
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)
2211 GKI_freebuf (p_cmd);
2213 while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_xmit_q)) != NULL)
2215 GKI_freebuf (p_cmd);
2219 /*******************************************************************************
2221 ** Function btu_hcif_enhanced_flush_complete_evt
2223 ** Description Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
2227 *******************************************************************************/
2228 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
2229 static void btu_hcif_enhanced_flush_complete_evt (void)
2231 /* This is empty until an upper layer cares about returning event */
2234 /**********************************************
2235 ** End of Simple Pairing Events
2236 ***********************************************/
2239 /**********************************************
2241 ***********************************************/
2242 #if (defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE)
2243 static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p)
2246 UINT8 enc_enable = 0;
2249 STREAM_TO_UINT8 (status, p);
2250 STREAM_TO_UINT16 (handle, p);
2252 if (status == HCI_SUCCESS) enc_enable = 1;
2254 btm_sec_encrypt_change (handle, status, enc_enable);
2257 static void btu_ble_process_adv_pkt (UINT8 *p)
2259 HCI_TRACE_EVENT("btu_ble_process_adv_pkt");
2261 btm_ble_process_adv_pkt(p);
2264 static void btu_ble_ll_conn_complete_evt ( UINT8 *p, UINT16 evt_len)
2266 btm_ble_conn_complete(p, evt_len);
2269 static void btu_ble_ll_conn_param_upd_evt (UINT8 *p)
2271 /* This is empty until an upper layer cares about returning event */
2274 static void btu_ble_read_remote_feat_evt (UINT8 *p)
2276 btm_ble_read_remote_features_complete(p);
2279 static void btu_ble_proc_ltk_req (UINT8 *p)
2281 UINT16 ediv, handle;
2284 STREAM_TO_UINT16(handle, p);
2286 STREAM_TO_UINT16(ediv, pp);
2287 #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
2288 btm_ble_ltk_request(handle, p, ediv);
2290 /* This is empty until an upper layer cares about returning event */
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)
2299 UINT16 int_min, int_max, latency, timeout;
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);
2307 l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency, timeout);
2309 #endif /* BLE_LLT_INCLUDED */
2311 #endif /* BLE_INCLUDED */