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 ******************************************************************************/
28 #define LOG_TAG "bt_btu_hcif"
30 #include <base/bind.h>
31 #include <base/callback.h>
32 #include <base/location.h>
33 #include <base/logging.h>
34 #include <base/threading/thread.h>
40 #include "bt_common.h"
46 #include "device/include/controller.h"
47 #include "hci_layer.h"
50 #include "osi/include/log.h"
51 #include "osi/include/osi.h"
53 using tracked_objects::Location;
55 extern void btm_process_cancel_complete(uint8_t status, uint8_t mode);
56 extern void btm_ble_test_command_complete(uint8_t* p);
58 /******************************************************************************/
59 /* L O C A L F U N C T I O N P R O T O T Y P E S */
60 /******************************************************************************/
61 static void btu_hcif_inquiry_comp_evt(uint8_t* p);
62 static void btu_hcif_inquiry_result_evt(uint8_t* p);
63 static void btu_hcif_inquiry_rssi_result_evt(uint8_t* p);
64 static void btu_hcif_extended_inquiry_result_evt(uint8_t* p);
66 static void btu_hcif_connection_comp_evt(uint8_t* p);
67 static void btu_hcif_connection_request_evt(uint8_t* p);
68 static void btu_hcif_disconnection_comp_evt(uint8_t* p);
69 static void btu_hcif_authentication_comp_evt(uint8_t* p);
70 static void btu_hcif_rmt_name_request_comp_evt(uint8_t* p, uint16_t evt_len);
71 static void btu_hcif_encryption_change_evt(uint8_t* p);
72 static void btu_hcif_read_rmt_features_comp_evt(uint8_t* p);
73 static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p);
74 static void btu_hcif_read_rmt_version_comp_evt(uint8_t* p);
75 static void btu_hcif_qos_setup_comp_evt(uint8_t* p);
76 static void btu_hcif_command_complete_evt(BT_HDR* response, void* context);
77 static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command,
79 static void btu_hcif_hardware_error_evt(uint8_t* p);
80 static void btu_hcif_flush_occured_evt(void);
81 static void btu_hcif_role_change_evt(uint8_t* p);
82 static void btu_hcif_num_compl_data_pkts_evt(uint8_t* p);
83 static void btu_hcif_mode_change_evt(uint8_t* p);
84 static void btu_hcif_pin_code_request_evt(uint8_t* p);
85 static void btu_hcif_link_key_request_evt(uint8_t* p);
86 static void btu_hcif_link_key_notification_evt(uint8_t* p);
87 static void btu_hcif_loopback_command_evt(void);
88 static void btu_hcif_data_buf_overflow_evt(void);
89 static void btu_hcif_max_slots_changed_evt(void);
90 static void btu_hcif_read_clock_off_comp_evt(uint8_t* p);
91 static void btu_hcif_conn_pkt_type_change_evt(void);
92 static void btu_hcif_qos_violation_evt(uint8_t* p);
93 static void btu_hcif_page_scan_mode_change_evt(void);
94 static void btu_hcif_page_scan_rep_mode_chng_evt(void);
95 static void btu_hcif_esco_connection_comp_evt(uint8_t* p);
96 static void btu_hcif_esco_connection_chg_evt(uint8_t* p);
98 /* Simple Pairing Events */
99 static void btu_hcif_host_support_evt(uint8_t* p);
100 static void btu_hcif_io_cap_request_evt(uint8_t* p);
101 static void btu_hcif_io_cap_response_evt(uint8_t* p);
102 static void btu_hcif_user_conf_request_evt(uint8_t* p);
103 static void btu_hcif_user_passkey_request_evt(uint8_t* p);
104 static void btu_hcif_user_passkey_notif_evt(uint8_t* p);
105 static void btu_hcif_keypress_notif_evt(uint8_t* p);
106 static void btu_hcif_rem_oob_request_evt(uint8_t* p);
108 static void btu_hcif_simple_pair_complete_evt(uint8_t* p);
109 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
110 static void btu_hcif_enhanced_flush_complete_evt(void);
113 #if (BTM_SSR_INCLUDED == TRUE)
114 static void btu_hcif_ssr_evt(uint8_t* p, uint16_t evt_len);
115 #endif /* BTM_SSR_INCLUDED == TRUE */
117 static void btu_ble_ll_conn_complete_evt(uint8_t* p, uint16_t evt_len);
118 static void btu_ble_read_remote_feat_evt(uint8_t* p);
119 static void btu_ble_ll_conn_param_upd_evt(uint8_t* p, uint16_t evt_len);
120 static void btu_ble_proc_ltk_req(uint8_t* p);
121 static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p);
122 static void btu_ble_data_length_change_evt(uint8_t* p, uint16_t evt_len);
123 #if (BLE_LLT_INCLUDED == TRUE)
124 static void btu_ble_rc_param_req_evt(uint8_t* p);
126 #if (BLE_PRIVACY_SPT == TRUE)
127 static void btu_ble_proc_enhanced_conn_cmpl(uint8_t* p, uint16_t evt_len);
130 static void do_in_hci_thread(const tracked_objects::Location& from_here,
131 const base::Closure& task) {
132 base::MessageLoop* hci_message_loop = get_message_loop();
133 if (!hci_message_loop || !hci_message_loop->task_runner().get()) {
134 LOG_ERROR(LOG_TAG, "%s: HCI message loop not running, accessed from %s",
135 __func__, from_here.ToString().c_str());
139 hci_message_loop->task_runner()->PostTask(from_here, task);
142 /*******************************************************************************
144 * Function btu_hcif_process_event
146 * Description This function is called when an event is received from
147 * the Host Controller.
151 ******************************************************************************/
152 void btu_hcif_process_event(UNUSED_ATTR uint8_t controller_id, BT_HDR* p_msg) {
153 uint8_t* p = (uint8_t*)(p_msg + 1) + p_msg->offset;
154 uint8_t hci_evt_code, hci_evt_len;
155 uint8_t ble_sub_code;
156 STREAM_TO_UINT8(hci_evt_code, p);
157 STREAM_TO_UINT8(hci_evt_len, p);
159 switch (hci_evt_code) {
160 case HCI_INQUIRY_COMP_EVT:
161 btu_hcif_inquiry_comp_evt(p);
163 case HCI_INQUIRY_RESULT_EVT:
164 btu_hcif_inquiry_result_evt(p);
166 case HCI_INQUIRY_RSSI_RESULT_EVT:
167 btu_hcif_inquiry_rssi_result_evt(p);
169 case HCI_EXTENDED_INQUIRY_RESULT_EVT:
170 btu_hcif_extended_inquiry_result_evt(p);
172 case HCI_CONNECTION_COMP_EVT:
173 btu_hcif_connection_comp_evt(p);
175 case HCI_CONNECTION_REQUEST_EVT:
176 btu_hcif_connection_request_evt(p);
178 case HCI_DISCONNECTION_COMP_EVT:
179 btu_hcif_disconnection_comp_evt(p);
181 case HCI_AUTHENTICATION_COMP_EVT:
182 btu_hcif_authentication_comp_evt(p);
184 case HCI_RMT_NAME_REQUEST_COMP_EVT:
185 btu_hcif_rmt_name_request_comp_evt(p, hci_evt_len);
187 case HCI_ENCRYPTION_CHANGE_EVT:
188 btu_hcif_encryption_change_evt(p);
190 case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
191 btu_hcif_encryption_key_refresh_cmpl_evt(p);
193 case HCI_READ_RMT_FEATURES_COMP_EVT:
194 btu_hcif_read_rmt_features_comp_evt(p);
196 case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
197 btu_hcif_read_rmt_ext_features_comp_evt(p);
199 case HCI_READ_RMT_VERSION_COMP_EVT:
200 btu_hcif_read_rmt_version_comp_evt(p);
202 case HCI_QOS_SETUP_COMP_EVT:
203 btu_hcif_qos_setup_comp_evt(p);
205 case HCI_COMMAND_COMPLETE_EVT:
207 "%s should not have received a command complete event. "
208 "Someone didn't go through the hci transmit_command function.",
211 case HCI_COMMAND_STATUS_EVT:
213 "%s should not have received a command status event. "
214 "Someone didn't go through the hci transmit_command function.",
217 case HCI_HARDWARE_ERROR_EVT:
218 btu_hcif_hardware_error_evt(p);
220 case HCI_FLUSH_OCCURED_EVT:
221 btu_hcif_flush_occured_evt();
223 case HCI_ROLE_CHANGE_EVT:
224 btu_hcif_role_change_evt(p);
226 case HCI_NUM_COMPL_DATA_PKTS_EVT:
227 btu_hcif_num_compl_data_pkts_evt(p);
229 case HCI_MODE_CHANGE_EVT:
230 btu_hcif_mode_change_evt(p);
232 case HCI_PIN_CODE_REQUEST_EVT:
233 btu_hcif_pin_code_request_evt(p);
235 case HCI_LINK_KEY_REQUEST_EVT:
236 btu_hcif_link_key_request_evt(p);
238 case HCI_LINK_KEY_NOTIFICATION_EVT:
239 btu_hcif_link_key_notification_evt(p);
241 case HCI_LOOPBACK_COMMAND_EVT:
242 btu_hcif_loopback_command_evt();
244 case HCI_DATA_BUF_OVERFLOW_EVT:
245 btu_hcif_data_buf_overflow_evt();
247 case HCI_MAX_SLOTS_CHANGED_EVT:
248 btu_hcif_max_slots_changed_evt();
250 case HCI_READ_CLOCK_OFF_COMP_EVT:
251 btu_hcif_read_clock_off_comp_evt(p);
253 case HCI_CONN_PKT_TYPE_CHANGE_EVT:
254 btu_hcif_conn_pkt_type_change_evt();
256 case HCI_QOS_VIOLATION_EVT:
257 btu_hcif_qos_violation_evt(p);
259 case HCI_PAGE_SCAN_MODE_CHANGE_EVT:
260 btu_hcif_page_scan_mode_change_evt();
262 case HCI_PAGE_SCAN_REP_MODE_CHNG_EVT:
263 btu_hcif_page_scan_rep_mode_chng_evt();
265 case HCI_ESCO_CONNECTION_COMP_EVT:
266 btu_hcif_esco_connection_comp_evt(p);
268 case HCI_ESCO_CONNECTION_CHANGED_EVT:
269 btu_hcif_esco_connection_chg_evt(p);
271 #if (BTM_SSR_INCLUDED == TRUE)
272 case HCI_SNIFF_SUB_RATE_EVT:
273 btu_hcif_ssr_evt(p, hci_evt_len);
275 #endif /* BTM_SSR_INCLUDED == TRUE */
276 case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
277 btu_hcif_host_support_evt(p);
279 case HCI_IO_CAPABILITY_REQUEST_EVT:
280 btu_hcif_io_cap_request_evt(p);
282 case HCI_IO_CAPABILITY_RESPONSE_EVT:
283 btu_hcif_io_cap_response_evt(p);
285 case HCI_USER_CONFIRMATION_REQUEST_EVT:
286 btu_hcif_user_conf_request_evt(p);
288 case HCI_USER_PASSKEY_REQUEST_EVT:
289 btu_hcif_user_passkey_request_evt(p);
291 case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
292 btu_hcif_rem_oob_request_evt(p);
294 case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
295 btu_hcif_simple_pair_complete_evt(p);
297 case HCI_USER_PASSKEY_NOTIFY_EVT:
298 btu_hcif_user_passkey_notif_evt(p);
300 case HCI_KEYPRESS_NOTIFY_EVT:
301 btu_hcif_keypress_notif_evt(p);
303 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
304 case HCI_ENHANCED_FLUSH_COMPLETE_EVT:
305 btu_hcif_enhanced_flush_complete_evt();
309 case HCI_BLE_EVENT: {
310 STREAM_TO_UINT8(ble_sub_code, p);
312 HCI_TRACE_EVENT("BLE HCI(id=%d) event = 0x%02x)", hci_evt_code,
315 uint8_t ble_evt_len = hci_evt_len - 1;
316 switch (ble_sub_code) {
317 case HCI_BLE_ADV_PKT_RPT_EVT: /* result of inquiry */
318 HCI_TRACE_EVENT("HCI_BLE_ADV_PKT_RPT_EVT");
319 btm_ble_process_adv_pkt(ble_evt_len, p);
321 case HCI_BLE_CONN_COMPLETE_EVT:
322 btu_ble_ll_conn_complete_evt(p, hci_evt_len);
324 case HCI_BLE_LL_CONN_PARAM_UPD_EVT:
325 btu_ble_ll_conn_param_upd_evt(p, hci_evt_len);
327 case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
328 btu_ble_read_remote_feat_evt(p);
330 case HCI_BLE_LTK_REQ_EVT: /* received only at slave device */
331 btu_ble_proc_ltk_req(p);
333 #if (BLE_PRIVACY_SPT == TRUE)
334 case HCI_BLE_ENHANCED_CONN_COMPLETE_EVT:
335 btu_ble_proc_enhanced_conn_cmpl(p, hci_evt_len);
338 #if (BLE_LLT_INCLUDED == TRUE)
339 case HCI_BLE_RC_PARAM_REQ_EVT:
340 btu_ble_rc_param_req_evt(p);
343 case HCI_BLE_DATA_LENGTH_CHANGE_EVT:
344 btu_ble_data_length_change_evt(p, hci_evt_len);
347 case HCI_BLE_PHY_UPDATE_COMPLETE_EVT:
348 btm_ble_process_phy_update_pkt(ble_evt_len, p);
351 case HCI_LE_EXTENDED_ADVERTISING_REPORT_EVT:
352 btm_ble_process_ext_adv_pkt(hci_evt_len, p);
355 case HCI_LE_ADVERTISING_SET_TERMINATED_EVT:
356 btm_le_on_advertising_set_terminated(p, hci_evt_len);
362 case HCI_VENDOR_SPECIFIC_EVT:
363 btm_vendor_specific_evt(p, hci_evt_len);
368 /*******************************************************************************
370 * Function btu_hcif_send_cmd
372 * Description This function is called to send commands to the Host
377 ******************************************************************************/
378 void btu_hcif_send_cmd(UNUSED_ATTR uint8_t controller_id, BT_HDR* p_buf) {
382 uint8_t* stream = p_buf->data + p_buf->offset;
383 void* vsc_callback = NULL;
385 STREAM_TO_UINT16(opcode, stream);
387 // Eww...horrible hackery here
388 /* If command was a VSC, then extract command_complete callback */
389 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC ||
390 (opcode == HCI_BLE_RAND) || (opcode == HCI_BLE_ENCRYPT)) {
391 vsc_callback = *((void**)(p_buf + 1));
394 hci_layer_get_interface()->transmit_command(
395 p_buf, btu_hcif_command_complete_evt, btu_hcif_command_status_evt,
399 using hci_cmd_cb = base::Callback<void(uint8_t* /* return_parameters */,
400 uint16_t /* return_parameters_length*/)>;
402 struct cmd_with_cb_data {
404 Location posted_from;
407 void cmd_with_cb_data_init(cmd_with_cb_data* cb_wrapper) {
408 new (&cb_wrapper->cb) hci_cmd_cb;
409 new (&cb_wrapper->posted_from) Location;
412 void cmd_with_cb_data_cleanup(cmd_with_cb_data* cb_wrapper) {
413 cb_wrapper->cb.~hci_cmd_cb();
414 cb_wrapper->posted_from.~Location();
417 static void btu_hcif_command_complete_evt_with_cb_on_task(BT_HDR* event,
419 command_opcode_t opcode;
421 event->data + event->offset +
422 3; // 2 to skip the event headers, 1 to skip the command credits
423 STREAM_TO_UINT16(opcode, stream);
425 cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)context;
426 HCI_TRACE_DEBUG("command complete for: %s",
427 cb_wrapper->posted_from.ToString().c_str());
428 cb_wrapper->cb.Run(stream, event->len - 5);
429 cmd_with_cb_data_cleanup(cb_wrapper);
430 osi_free(cb_wrapper);
435 static void btu_hcif_command_complete_evt_with_cb(BT_HDR* response,
437 do_in_hci_thread(FROM_HERE,
438 base::Bind(btu_hcif_command_complete_evt_with_cb_on_task,
442 static void btu_hcif_command_status_evt_with_cb_on_task(uint8_t status,
445 command_opcode_t opcode;
446 uint8_t* stream = event->data + event->offset;
447 STREAM_TO_UINT16(opcode, stream);
451 // report command status error
452 cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)context;
453 HCI_TRACE_DEBUG("command status for: %s",
454 cb_wrapper->posted_from.ToString().c_str());
455 cb_wrapper->cb.Run(&status, sizeof(uint16_t));
456 cmd_with_cb_data_cleanup(cb_wrapper);
457 osi_free(cb_wrapper);
462 static void btu_hcif_command_status_evt_with_cb(uint8_t status, BT_HDR* command,
464 // Command is pending, we report only error.
471 FROM_HERE, base::Bind(btu_hcif_command_status_evt_with_cb_on_task, status,
475 /* This function is called to send commands to the Host Controller. |cb| is
476 * called when command status event is called with error code, or when the
477 * command complete event is received. */
478 void btu_hcif_send_cmd_with_cb(const tracked_objects::Location& posted_from,
479 uint16_t opcode, uint8_t* params,
480 uint8_t params_len, hci_cmd_cb cb) {
481 BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
482 uint8_t* pp = (uint8_t*)(p + 1);
484 p->len = HCIC_PREAMBLE_SIZE + params_len;
487 UINT16_TO_STREAM(pp, opcode);
488 UINT8_TO_STREAM(pp, params_len);
490 memcpy(pp, params, params_len);
493 cmd_with_cb_data* cb_wrapper =
494 (cmd_with_cb_data*)osi_malloc(sizeof(cmd_with_cb_data));
496 cmd_with_cb_data_init(cb_wrapper);
498 cb_wrapper->posted_from = posted_from;
500 hci_layer_get_interface()->transmit_command(
501 p, btu_hcif_command_complete_evt_with_cb,
502 btu_hcif_command_status_evt_with_cb, (void*)cb_wrapper);
505 /*******************************************************************************
507 * Function btu_hcif_inquiry_comp_evt
509 * Description Process event HCI_INQUIRY_COMP_EVT
513 ******************************************************************************/
514 static void btu_hcif_inquiry_comp_evt(uint8_t* p) {
517 STREAM_TO_UINT8(status, p);
519 /* Tell inquiry processing that we are done */
520 btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
523 /*******************************************************************************
525 * Function btu_hcif_inquiry_result_evt
527 * Description Process event HCI_INQUIRY_RESULT_EVT
531 ******************************************************************************/
532 static void btu_hcif_inquiry_result_evt(uint8_t* p) {
533 /* Store results in the cache */
534 btm_process_inq_results(p, BTM_INQ_RESULT_STANDARD);
537 /*******************************************************************************
539 * Function btu_hcif_inquiry_rssi_result_evt
541 * Description Process event HCI_INQUIRY_RSSI_RESULT_EVT
545 ******************************************************************************/
546 static void btu_hcif_inquiry_rssi_result_evt(uint8_t* p) {
547 /* Store results in the cache */
548 btm_process_inq_results(p, BTM_INQ_RESULT_WITH_RSSI);
551 /*******************************************************************************
553 * Function btu_hcif_extended_inquiry_result_evt
555 * Description Process event HCI_EXTENDED_INQUIRY_RESULT_EVT
559 ******************************************************************************/
560 static void btu_hcif_extended_inquiry_result_evt(uint8_t* p) {
561 /* Store results in the cache */
562 btm_process_inq_results(p, BTM_INQ_RESULT_EXTENDED);
565 /*******************************************************************************
567 * Function btu_hcif_connection_comp_evt
569 * Description Process event HCI_CONNECTION_COMP_EVT
573 ******************************************************************************/
574 static void btu_hcif_connection_comp_evt(uint8_t* p) {
580 #if (BTM_SCO_INCLUDED == TRUE)
581 tBTM_ESCO_DATA esco_data;
584 STREAM_TO_UINT8(status, p);
585 STREAM_TO_UINT16(handle, p);
586 STREAM_TO_BDADDR(bda, p);
587 STREAM_TO_UINT8(link_type, p);
588 STREAM_TO_UINT8(enc_mode, p);
590 handle = HCID_GET_HANDLE(handle);
592 if (link_type == HCI_LINK_TYPE_ACL) {
593 btm_sec_connected(bda, handle, status, enc_mode);
595 l2c_link_hci_conn_comp(status, handle, bda);
597 #if (BTM_SCO_INCLUDED == TRUE)
599 memset(&esco_data, 0, sizeof(tBTM_ESCO_DATA));
600 /* esco_data.link_type = HCI_LINK_TYPE_SCO; already zero */
601 esco_data.bd_addr = bda;
602 btm_sco_connected(status, &bda, handle, &esco_data);
604 #endif /* BTM_SCO_INCLUDED */
607 /*******************************************************************************
609 * Function btu_hcif_connection_request_evt
611 * Description Process event HCI_CONNECTION_REQUEST_EVT
615 ******************************************************************************/
616 static void btu_hcif_connection_request_evt(uint8_t* p) {
621 STREAM_TO_BDADDR(bda, p);
622 STREAM_TO_DEVCLASS(dc, p);
623 STREAM_TO_UINT8(link_type, p);
625 /* Pass request to security manager to check connect filters before */
626 /* passing request to l2cap */
627 if (link_type == HCI_LINK_TYPE_ACL) {
628 btm_sec_conn_req(bda, dc);
630 #if (BTM_SCO_INCLUDED == TRUE)
632 btm_sco_conn_req(bda, dc, link_type);
634 #endif /* BTM_SCO_INCLUDED */
637 /*******************************************************************************
639 * Function btu_hcif_disconnection_comp_evt
641 * Description Process event HCI_DISCONNECTION_COMP_EVT
645 ******************************************************************************/
646 static void btu_hcif_disconnection_comp_evt(uint8_t* p) {
651 STREAM_TO_UINT16(handle, p);
652 STREAM_TO_UINT8(reason, p);
654 handle = HCID_GET_HANDLE(handle);
656 #if (BTM_SCO_INCLUDED == TRUE)
657 /* If L2CAP doesn't know about it, send it to SCO */
658 if (!l2c_link_hci_disc_comp(handle, reason)) btm_sco_removed(handle, reason);
660 l2c_link_hci_disc_comp(handle, reason);
661 #endif /* BTM_SCO_INCLUDED */
663 /* Notify security manager */
664 btm_sec_disconnected(handle, reason);
667 /*******************************************************************************
669 * Function btu_hcif_authentication_comp_evt
671 * Description Process event HCI_AUTHENTICATION_COMP_EVT
675 ******************************************************************************/
676 static void btu_hcif_authentication_comp_evt(uint8_t* p) {
680 STREAM_TO_UINT8(status, p);
681 STREAM_TO_UINT16(handle, p);
683 btm_sec_auth_complete(handle, status);
686 /*******************************************************************************
688 * Function btu_hcif_rmt_name_request_comp_evt
690 * Description Process event HCI_RMT_NAME_REQUEST_COMP_EVT
694 ******************************************************************************/
695 static void btu_hcif_rmt_name_request_comp_evt(uint8_t* p, uint16_t evt_len) {
699 STREAM_TO_UINT8(status, p);
700 STREAM_TO_BDADDR(bd_addr, p);
702 evt_len -= (1 + BD_ADDR_LEN);
704 btm_process_remote_name(&bd_addr, p, evt_len, status);
706 btm_sec_rmt_name_request_complete(&bd_addr, p, status);
709 constexpr uint8_t MIN_KEY_SIZE = 7;
711 static void read_encryption_key_size_complete_after_encryption_change(
712 uint8_t status, uint16_t handle, uint8_t key_size) {
713 if (status != HCI_SUCCESS) {
714 HCI_TRACE_WARNING("%s: disconnecting, status: 0x%02x", __func__, status);
715 btsnd_hcic_disconnect(handle, HCI_ERR_PEER_USER);
719 if (key_size < MIN_KEY_SIZE) {
720 android_errorWriteLog(0x534e4554, "124301137");
722 "%s encryption key too short, disconnecting. handle: 0x%02x, key_size: "
724 __func__, handle, key_size);
726 btsnd_hcic_disconnect(handle, HCI_ERR_HOST_REJECT_SECURITY);
730 // good key size - succeed
731 btm_acl_encrypt_change(handle, status, 1 /* enable */);
732 btm_sec_encrypt_change(handle, status, 1 /* enable */);
734 /*******************************************************************************
736 * Function btu_hcif_encryption_change_evt
738 * Description Process event HCI_ENCRYPTION_CHANGE_EVT
742 ******************************************************************************/
743 static void btu_hcif_encryption_change_evt(uint8_t* p) {
748 STREAM_TO_UINT8(status, p);
749 STREAM_TO_UINT16(handle, p);
750 STREAM_TO_UINT8(encr_enable, p);
752 if (status != HCI_SUCCESS || encr_enable == 0 ||
753 BTM_IsBleConnection(handle)) {
754 btm_acl_encrypt_change(handle, status, encr_enable);
755 btm_sec_encrypt_change(handle, status, encr_enable);
757 btsnd_hcic_read_encryption_key_size(
759 base::Bind(&read_encryption_key_size_complete_after_encryption_change));
763 /*******************************************************************************
765 * Function btu_hcif_read_rmt_features_comp_evt
767 * Description Process event HCI_READ_RMT_FEATURES_COMP_EVT
771 ******************************************************************************/
772 static void btu_hcif_read_rmt_features_comp_evt(uint8_t* p) {
773 btm_read_remote_features_complete(p);
776 /*******************************************************************************
778 * Function btu_hcif_read_rmt_ext_features_comp_evt
780 * Description Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
784 ******************************************************************************/
785 static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p) {
790 STREAM_TO_UINT8(status, p_cur);
792 if (status == HCI_SUCCESS)
793 btm_read_remote_ext_features_complete(p);
795 STREAM_TO_UINT16(handle, p_cur);
796 btm_read_remote_ext_features_failed(status, handle);
800 /*******************************************************************************
802 * Function btu_hcif_read_rmt_version_comp_evt
804 * Description Process event HCI_READ_RMT_VERSION_COMP_EVT
808 ******************************************************************************/
809 static void btu_hcif_read_rmt_version_comp_evt(uint8_t* p) {
810 btm_read_remote_version_complete(p);
813 /*******************************************************************************
815 * Function btu_hcif_qos_setup_comp_evt
817 * Description Process event HCI_QOS_SETUP_COMP_EVT
821 ******************************************************************************/
822 static void btu_hcif_qos_setup_comp_evt(uint8_t* p) {
827 STREAM_TO_UINT8(status, p);
828 STREAM_TO_UINT16(handle, p);
829 STREAM_TO_UINT8(flow.qos_flags, p);
830 STREAM_TO_UINT8(flow.service_type, p);
831 STREAM_TO_UINT32(flow.token_rate, p);
832 STREAM_TO_UINT32(flow.peak_bandwidth, p);
833 STREAM_TO_UINT32(flow.latency, p);
834 STREAM_TO_UINT32(flow.delay_variation, p);
836 btm_qos_setup_complete(status, handle, &flow);
839 /*******************************************************************************
841 * Function btu_hcif_esco_connection_comp_evt
843 * Description Process event HCI_ESCO_CONNECTION_COMP_EVT
847 ******************************************************************************/
848 static void btu_hcif_esco_connection_comp_evt(uint8_t* p) {
849 #if (BTM_SCO_INCLUDED == TRUE)
855 STREAM_TO_UINT8(status, p);
856 STREAM_TO_UINT16(handle, p);
857 STREAM_TO_BDADDR(bda, p);
859 STREAM_TO_UINT8(data.link_type, p);
860 STREAM_TO_UINT8(data.tx_interval, p);
861 STREAM_TO_UINT8(data.retrans_window, p);
862 STREAM_TO_UINT16(data.rx_pkt_len, p);
863 STREAM_TO_UINT16(data.tx_pkt_len, p);
864 STREAM_TO_UINT8(data.air_mode, p);
867 btm_sco_connected(status, &bda, handle, &data);
871 /*******************************************************************************
873 * Function btu_hcif_esco_connection_chg_evt
875 * Description Process event HCI_ESCO_CONNECTION_CHANGED_EVT
879 ******************************************************************************/
880 static void btu_hcif_esco_connection_chg_evt(uint8_t* p) {
881 #if (BTM_SCO_INCLUDED == TRUE)
887 uint8_t retrans_window;
889 STREAM_TO_UINT8(status, p);
890 STREAM_TO_UINT16(handle, p);
892 STREAM_TO_UINT8(tx_interval, p);
893 STREAM_TO_UINT8(retrans_window, p);
894 STREAM_TO_UINT16(rx_pkt_len, p);
895 STREAM_TO_UINT16(tx_pkt_len, p);
897 btm_esco_proc_conn_chg(status, handle, tx_interval, retrans_window,
898 rx_pkt_len, tx_pkt_len);
902 /*******************************************************************************
904 * Function btu_hcif_hdl_command_complete
906 * Description Handle command complete event
910 ******************************************************************************/
911 static void btu_hcif_hdl_command_complete(uint16_t opcode, uint8_t* p,
913 void* p_cplt_cback) {
915 case HCI_INQUIRY_CANCEL:
916 /* Tell inquiry processing that we are done */
917 btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
919 case HCI_SET_EVENT_FILTER:
920 btm_event_filter_complete(p);
923 case HCI_DELETE_STORED_LINK_KEY:
924 btm_delete_stored_link_key_complete(p);
927 case HCI_READ_LOCAL_NAME:
928 btm_read_local_name_complete(p, evt_len);
931 case HCI_GET_LINK_QUALITY:
932 btm_read_link_quality_complete(p);
936 btm_read_rssi_complete(p);
939 case HCI_READ_FAILED_CONTACT_COUNTER:
940 btm_read_failed_contact_counter_complete(p);
943 case HCI_READ_AUTOMATIC_FLUSH_TIMEOUT:
944 btm_read_automatic_flush_timeout_complete(p);
947 case HCI_READ_TRANSMIT_POWER_LEVEL:
948 btm_read_tx_power_complete(p, false);
951 case HCI_CREATE_CONNECTION_CANCEL:
952 btm_create_conn_cancel_complete(p);
955 case HCI_READ_LOCAL_OOB_DATA:
956 btm_read_local_oob_complete(p);
959 case HCI_READ_INQ_TX_POWER_LEVEL:
960 btm_read_inq_tx_power_complete(p);
963 /* BLE Commands sComplete*/
964 case HCI_BLE_ADD_WHITE_LIST:
965 btm_ble_add_2_white_list_complete(*p);
968 case HCI_BLE_CLEAR_WHITE_LIST:
969 btm_ble_clear_white_list_complete(p, evt_len);
972 case HCI_BLE_REMOVE_WHITE_LIST:
973 btm_ble_remove_from_white_list_complete(p, evt_len);
977 case HCI_BLE_ENCRYPT:
978 btm_ble_rand_enc_complete(p, opcode, (tBTM_RAND_ENC_CB*)p_cplt_cback);
981 case HCI_BLE_READ_ADV_CHNL_TX_POWER:
982 btm_read_tx_power_complete(p, true);
985 case HCI_BLE_WRITE_ADV_ENABLE:
986 btm_ble_write_adv_enable_complete(p);
989 case HCI_BLE_CREATE_LL_CONN:
990 btm_ble_create_ll_conn_complete(*p);
993 case HCI_BLE_TRANSMITTER_TEST:
994 case HCI_BLE_RECEIVER_TEST:
995 case HCI_BLE_TEST_END:
996 btm_ble_test_command_complete(p);
999 #if (BLE_PRIVACY_SPT == TRUE)
1000 case HCI_BLE_ADD_DEV_RESOLVING_LIST:
1001 btm_ble_add_resolving_list_entry_complete(p, evt_len);
1004 case HCI_BLE_RM_DEV_RESOLVING_LIST:
1005 btm_ble_remove_resolving_list_entry_complete(p, evt_len);
1008 case HCI_BLE_CLEAR_RESOLVING_LIST:
1009 btm_ble_clear_resolving_list_complete(p, evt_len);
1012 case HCI_BLE_READ_RESOLVABLE_ADDR_PEER:
1013 btm_ble_read_resolving_list_entry_complete(p, evt_len);
1016 case HCI_BLE_READ_RESOLVABLE_ADDR_LOCAL:
1017 case HCI_BLE_SET_ADDR_RESOLUTION_ENABLE:
1018 case HCI_BLE_SET_RAND_PRIV_ADDR_TIMOUT:
1022 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1023 btm_vsc_complete(p, opcode, evt_len, (tBTM_CMPL_CB*)p_cplt_cback);
1028 /*******************************************************************************
1030 * Function btu_hcif_command_complete_evt
1032 * Description Process event HCI_COMMAND_COMPLETE_EVT
1036 ******************************************************************************/
1037 static void btu_hcif_command_complete_evt_on_task(BT_HDR* event,
1039 command_opcode_t opcode;
1041 event->data + event->offset +
1042 3; // 2 to skip the event headers, 1 to skip the command credits
1043 STREAM_TO_UINT16(opcode, stream);
1045 btu_hcif_hdl_command_complete(
1048 5, // 3 for the command complete headers, 2 for the event headers
1054 static void btu_hcif_command_complete_evt(BT_HDR* response, void* context) {
1055 do_in_hci_thread(FROM_HERE, base::Bind(btu_hcif_command_complete_evt_on_task,
1056 response, context));
1059 /*******************************************************************************
1061 * Function btu_hcif_hdl_command_status
1063 * Description Handle a command status event
1067 ******************************************************************************/
1068 static void btu_hcif_hdl_command_status(uint16_t opcode, uint8_t status,
1070 void* p_vsc_status_cback) {
1073 #if (BTM_SCO_INCLUDED == TRUE)
1074 tBTM_ESCO_DATA esco_data;
1078 case HCI_EXIT_SNIFF_MODE:
1079 case HCI_EXIT_PARK_MODE:
1080 #if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
1081 if (status != HCI_SUCCESS) {
1082 /* Allow SCO initiation to continue if waiting for change mode event */
1083 if (p_cmd != NULL) {
1084 p_cmd++; /* bypass length field */
1085 STREAM_TO_UINT16(handle, p_cmd);
1086 btm_sco_chk_pend_unpark(status, handle);
1090 /* Case Falls Through */
1093 case HCI_SNIFF_MODE:
1095 btm_pm_proc_cmd_status(status);
1099 /* If command failed to start, we may need to tell BTM */
1100 if (status != HCI_SUCCESS) {
1103 /* Tell inquiry processing that we are done */
1104 btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
1107 case HCI_RMT_NAME_REQUEST:
1108 /* Tell inquiry processing that we are done */
1109 btm_process_remote_name(NULL, NULL, 0, status);
1111 btm_sec_rmt_name_request_complete(NULL, NULL, status);
1114 case HCI_QOS_SETUP_COMP_EVT:
1115 /* Tell qos setup that we are done */
1116 btm_qos_setup_complete(status, 0, NULL);
1119 case HCI_SWITCH_ROLE:
1120 /* Tell BTM that the command failed */
1121 /* read bd addr out of stored command */
1122 if (p_cmd != NULL) {
1124 STREAM_TO_BDADDR(bd_addr, p_cmd);
1125 btm_acl_role_changed(status, &bd_addr, BTM_ROLE_UNDEFINED);
1127 btm_acl_role_changed(status, NULL, BTM_ROLE_UNDEFINED);
1128 l2c_link_role_changed(nullptr, BTM_ROLE_UNDEFINED,
1129 HCI_ERR_COMMAND_DISALLOWED);
1132 case HCI_CREATE_CONNECTION:
1133 /* read bd addr out of stored command */
1134 if (p_cmd != NULL) {
1136 STREAM_TO_BDADDR(bd_addr, p_cmd);
1137 btm_sec_connected(bd_addr, HCI_INVALID_HANDLE, status, 0);
1138 l2c_link_hci_conn_comp(status, HCI_INVALID_HANDLE, bd_addr);
1142 case HCI_READ_RMT_EXT_FEATURES:
1143 if (p_cmd != NULL) {
1144 p_cmd++; /* skip command length */
1145 STREAM_TO_UINT16(handle, p_cmd);
1147 handle = HCI_INVALID_HANDLE;
1149 btm_read_remote_ext_features_failed(status, handle);
1152 case HCI_AUTHENTICATION_REQUESTED:
1153 /* Device refused to start authentication. That should be treated
1154 * as authentication failure. */
1155 btm_sec_auth_complete(BTM_INVALID_HCI_HANDLE, status);
1158 case HCI_SET_CONN_ENCRYPTION:
1159 /* Device refused to start encryption. That should be treated as
1160 * encryption failure. */
1161 btm_sec_encrypt_change(BTM_INVALID_HCI_HANDLE, status, false);
1164 case HCI_BLE_CREATE_LL_CONN:
1165 btm_ble_create_ll_conn_complete(status);
1168 #if (BTM_SCO_INCLUDED == TRUE)
1169 case HCI_SETUP_ESCO_CONNECTION:
1170 case HCI_ENH_SETUP_ESCO_CONNECTION:
1171 /* read handle out of stored command */
1172 if (p_cmd != NULL) {
1174 STREAM_TO_UINT16(handle, p_cmd);
1176 /* Determine if initial connection failed or is a change
1178 if (btm_is_sco_active(handle))
1179 btm_esco_proc_conn_chg(status, handle, 0, 0, 0, 0);
1181 btm_sco_connected(status, NULL, handle, &esco_data);
1186 /* This is commented out until an upper layer cares about returning
1188 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
1189 case HCI_ENHANCED_FLUSH:
1194 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1195 btm_vsc_complete(&status, opcode, 1,
1196 (tBTM_CMPL_CB*)p_vsc_status_cback);
1201 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1202 btm_vsc_complete(&status, opcode, 1,
1203 (tBTM_CMPL_CB*)p_vsc_status_cback);
1208 /*******************************************************************************
1210 * Function btu_hcif_command_status_evt
1212 * Description Process event HCI_COMMAND_STATUS_EVT
1216 ******************************************************************************/
1217 static void btu_hcif_command_status_evt_on_task(uint8_t status, BT_HDR* event,
1219 command_opcode_t opcode;
1220 uint8_t* stream = event->data + event->offset;
1221 STREAM_TO_UINT16(opcode, stream);
1223 btu_hcif_hdl_command_status(opcode, status, stream, context);
1227 static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command,
1229 do_in_hci_thread(FROM_HERE, base::Bind(btu_hcif_command_status_evt_on_task,
1230 status, command, context));
1233 /*******************************************************************************
1235 * Function btu_hcif_hardware_error_evt
1237 * Description Process event HCI_HARDWARE_ERROR_EVT
1241 ******************************************************************************/
1242 static void btu_hcif_hardware_error_evt(uint8_t* p) {
1243 HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
1245 /* If anyone wants device status notifications, give him one. */
1246 btm_report_device_status(BTM_DEV_STATUS_DOWN);
1248 /* Reset the controller */
1249 if (BTM_IsDeviceUp()) BTM_DeviceReset(NULL);
1252 /*******************************************************************************
1254 * Function btu_hcif_flush_occured_evt
1256 * Description Process event HCI_FLUSH_OCCURED_EVT
1260 ******************************************************************************/
1261 static void btu_hcif_flush_occured_evt(void) {}
1263 /*******************************************************************************
1265 * Function btu_hcif_role_change_evt
1267 * Description Process event HCI_ROLE_CHANGE_EVT
1271 ******************************************************************************/
1272 static void btu_hcif_role_change_evt(uint8_t* p) {
1277 STREAM_TO_UINT8(status, p);
1278 STREAM_TO_BDADDR(bda, p);
1279 STREAM_TO_UINT8(role, p);
1281 btm_blacklist_role_change_device(bda, status);
1282 l2c_link_role_changed(&bda, role, status);
1283 btm_acl_role_changed(status, &bda, role);
1286 /*******************************************************************************
1288 * Function btu_hcif_num_compl_data_pkts_evt
1290 * Description Process event HCI_NUM_COMPL_DATA_PKTS_EVT
1294 ******************************************************************************/
1295 static void btu_hcif_num_compl_data_pkts_evt(uint8_t* p) {
1296 /* Process for L2CAP and SCO */
1297 l2c_link_process_num_completed_pkts(p);
1299 /* Send on to SCO */
1300 /*?? No SCO for now */
1303 /*******************************************************************************
1305 * Function btu_hcif_mode_change_evt
1307 * Description Process event HCI_MODE_CHANGE_EVT
1311 ******************************************************************************/
1312 static void btu_hcif_mode_change_evt(uint8_t* p) {
1315 uint8_t current_mode;
1318 STREAM_TO_UINT8(status, p);
1320 STREAM_TO_UINT16(handle, p);
1321 STREAM_TO_UINT8(current_mode, p);
1322 STREAM_TO_UINT16(interval, p);
1323 #if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
1324 btm_sco_chk_pend_unpark(status, handle);
1326 btm_pm_proc_mode_change(status, handle, current_mode, interval);
1328 #if (HID_DEV_INCLUDED == TRUE && HID_DEV_PM_INCLUDED == TRUE)
1329 hidd_pm_proc_mode_change(status, current_mode, interval);
1333 /*******************************************************************************
1335 * Function btu_hcif_ssr_evt
1337 * Description Process event HCI_SNIFF_SUB_RATE_EVT
1341 ******************************************************************************/
1342 #if (BTM_SSR_INCLUDED == TRUE)
1343 static void btu_hcif_ssr_evt(uint8_t* p, uint16_t evt_len) {
1344 btm_pm_proc_ssr_evt(p, evt_len);
1348 /*******************************************************************************
1350 * Function btu_hcif_pin_code_request_evt
1352 * Description Process event HCI_PIN_CODE_REQUEST_EVT
1356 ******************************************************************************/
1357 static void btu_hcif_pin_code_request_evt(uint8_t* p) {
1360 STREAM_TO_BDADDR(bda, p);
1362 /* Tell L2CAP that there was a PIN code request, */
1363 /* it may need to stretch timeouts */
1364 l2c_pin_code_request(bda);
1366 btm_sec_pin_code_request(bda);
1369 /*******************************************************************************
1371 * Function btu_hcif_link_key_request_evt
1373 * Description Process event HCI_LINK_KEY_REQUEST_EVT
1377 ******************************************************************************/
1378 static void btu_hcif_link_key_request_evt(uint8_t* p) {
1381 STREAM_TO_BDADDR(bda, p);
1382 btm_sec_link_key_request(bda);
1385 /*******************************************************************************
1387 * Function btu_hcif_link_key_notification_evt
1389 * Description Process event HCI_LINK_KEY_NOTIFICATION_EVT
1393 ******************************************************************************/
1394 static void btu_hcif_link_key_notification_evt(uint8_t* p) {
1399 STREAM_TO_BDADDR(bda, p);
1400 STREAM_TO_ARRAY16(key, p);
1401 STREAM_TO_UINT8(key_type, p);
1403 btm_sec_link_key_notification(bda, key, key_type);
1406 /*******************************************************************************
1408 * Function btu_hcif_loopback_command_evt
1410 * Description Process event HCI_LOOPBACK_COMMAND_EVT
1414 ******************************************************************************/
1415 static void btu_hcif_loopback_command_evt(void) {}
1417 /*******************************************************************************
1419 * Function btu_hcif_data_buf_overflow_evt
1421 * Description Process event HCI_DATA_BUF_OVERFLOW_EVT
1425 ******************************************************************************/
1426 static void btu_hcif_data_buf_overflow_evt(void) {}
1428 /*******************************************************************************
1430 * Function btu_hcif_max_slots_changed_evt
1432 * Description Process event HCI_MAX_SLOTS_CHANGED_EVT
1436 ******************************************************************************/
1437 static void btu_hcif_max_slots_changed_evt(void) {}
1439 /*******************************************************************************
1441 * Function btu_hcif_read_clock_off_comp_evt
1443 * Description Process event HCI_READ_CLOCK_OFF_COMP_EVT
1447 ******************************************************************************/
1448 static void btu_hcif_read_clock_off_comp_evt(uint8_t* p) {
1451 uint16_t clock_offset;
1453 STREAM_TO_UINT8(status, p);
1455 /* If failed to get clock offset just drop the result */
1456 if (status != HCI_SUCCESS) return;
1458 STREAM_TO_UINT16(handle, p);
1459 STREAM_TO_UINT16(clock_offset, p);
1461 handle = HCID_GET_HANDLE(handle);
1463 btm_process_clk_off_comp_evt(handle, clock_offset);
1464 btm_sec_update_clock_offset(handle, clock_offset);
1467 /*******************************************************************************
1469 * Function btu_hcif_conn_pkt_type_change_evt
1471 * Description Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
1475 ******************************************************************************/
1476 static void btu_hcif_conn_pkt_type_change_evt(void) {}
1478 /*******************************************************************************
1480 * Function btu_hcif_qos_violation_evt
1482 * Description Process event HCI_QOS_VIOLATION_EVT
1486 ******************************************************************************/
1487 static void btu_hcif_qos_violation_evt(uint8_t* p) {
1490 STREAM_TO_UINT16(handle, p);
1492 handle = HCID_GET_HANDLE(handle);
1494 l2c_link_hci_qos_violation(handle);
1497 /*******************************************************************************
1499 * Function btu_hcif_page_scan_mode_change_evt
1501 * Description Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
1505 ******************************************************************************/
1506 static void btu_hcif_page_scan_mode_change_evt(void) {}
1508 /*******************************************************************************
1510 * Function btu_hcif_page_scan_rep_mode_chng_evt
1512 * Description Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
1516 ******************************************************************************/
1517 static void btu_hcif_page_scan_rep_mode_chng_evt(void) {}
1519 /**********************************************
1520 * Simple Pairing Events
1521 **********************************************/
1523 /*******************************************************************************
1525 * Function btu_hcif_host_support_evt
1527 * Description Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
1531 ******************************************************************************/
1532 static void btu_hcif_host_support_evt(uint8_t* p) {
1533 btm_sec_rmt_host_support_feat_evt(p);
1536 /*******************************************************************************
1538 * Function btu_hcif_io_cap_request_evt
1540 * Description Process event HCI_IO_CAPABILITY_REQUEST_EVT
1544 ******************************************************************************/
1545 static void btu_hcif_io_cap_request_evt(uint8_t* p) {
1547 STREAM_TO_BDADDR(bda, p);
1548 btm_io_capabilities_req(bda);
1551 /*******************************************************************************
1553 * Function btu_hcif_io_cap_response_evt
1555 * Description Process event HCI_IO_CAPABILITY_RESPONSE_EVT
1559 ******************************************************************************/
1560 static void btu_hcif_io_cap_response_evt(uint8_t* p) {
1561 btm_io_capabilities_rsp(p);
1564 /*******************************************************************************
1566 * Function btu_hcif_user_conf_request_evt
1568 * Description Process event HCI_USER_CONFIRMATION_REQUEST_EVT
1572 ******************************************************************************/
1573 static void btu_hcif_user_conf_request_evt(uint8_t* p) {
1574 btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
1577 /*******************************************************************************
1579 * Function btu_hcif_user_passkey_request_evt
1581 * Description Process event HCI_USER_PASSKEY_REQUEST_EVT
1585 ******************************************************************************/
1586 static void btu_hcif_user_passkey_request_evt(uint8_t* p) {
1587 btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
1590 /*******************************************************************************
1592 * Function btu_hcif_user_passkey_notif_evt
1594 * Description Process event HCI_USER_PASSKEY_NOTIFY_EVT
1598 ******************************************************************************/
1599 static void btu_hcif_user_passkey_notif_evt(uint8_t* p) {
1600 btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
1603 /*******************************************************************************
1605 * Function btu_hcif_keypress_notif_evt
1607 * Description Process event HCI_KEYPRESS_NOTIFY_EVT
1611 ******************************************************************************/
1612 static void btu_hcif_keypress_notif_evt(uint8_t* p) {
1613 btm_keypress_notif_evt(p);
1616 /*******************************************************************************
1618 * Function btu_hcif_rem_oob_request_evt
1620 * Description Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
1624 ******************************************************************************/
1625 static void btu_hcif_rem_oob_request_evt(uint8_t* p) { btm_rem_oob_req(p); }
1627 /*******************************************************************************
1629 * Function btu_hcif_simple_pair_complete_evt
1631 * Description Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
1635 ******************************************************************************/
1636 static void btu_hcif_simple_pair_complete_evt(uint8_t* p) {
1637 btm_simple_pair_complete(p);
1640 /*******************************************************************************
1642 * Function btu_hcif_enhanced_flush_complete_evt
1644 * Description Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
1648 ******************************************************************************/
1649 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
1650 static void btu_hcif_enhanced_flush_complete_evt(void) {
1651 /* This is empty until an upper layer cares about returning event */
1654 /**********************************************
1655 * End of Simple Pairing Events
1656 **********************************************/
1658 static void read_encryption_key_size_complete_after_key_refresh(
1659 uint8_t status, uint16_t handle, uint8_t key_size) {
1660 if (status != HCI_SUCCESS) {
1661 HCI_TRACE_WARNING("%s: disconnecting, status: 0x%02x", __func__, status);
1662 btsnd_hcic_disconnect(handle, HCI_ERR_PEER_USER);
1666 if (key_size < MIN_KEY_SIZE) {
1667 android_errorWriteLog(0x534e4554, "124301137");
1669 "%s encryption key too short, disconnecting. handle: 0x%02x, key_size: "
1671 __func__, handle, key_size);
1673 btsnd_hcic_disconnect(handle, HCI_ERR_HOST_REJECT_SECURITY);
1677 btm_sec_encrypt_change(handle, status, 1 /* enc_enable */);
1680 static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p) {
1684 STREAM_TO_UINT8(status, p);
1685 STREAM_TO_UINT16(handle, p);
1687 if (status != HCI_SUCCESS || BTM_IsBleConnection(handle)) {
1688 btm_sec_encrypt_change(handle, status, (status == HCI_SUCCESS) ? 1 : 0);
1690 btsnd_hcic_read_encryption_key_size(
1692 base::Bind(&read_encryption_key_size_complete_after_key_refresh));
1696 /**********************************************
1698 **********************************************/
1700 static void btu_ble_ll_conn_complete_evt(uint8_t* p, uint16_t evt_len) {
1701 btm_ble_conn_complete(p, evt_len, false);
1703 #if (BLE_PRIVACY_SPT == TRUE)
1704 static void btu_ble_proc_enhanced_conn_cmpl(uint8_t* p, uint16_t evt_len) {
1705 btm_ble_conn_complete(p, evt_len, true);
1709 extern void gatt_notify_conn_update(uint16_t handle, uint16_t interval,
1710 uint16_t latency, uint16_t timeout,
1713 static void btu_ble_ll_conn_param_upd_evt(uint8_t* p, uint16_t evt_len) {
1714 /* LE connection update has completed successfully as a master. */
1715 /* We can enable the update request if the result is a success. */
1716 /* extract the HCI handle first */
1723 STREAM_TO_UINT8(status, p);
1724 STREAM_TO_UINT16(handle, p);
1725 STREAM_TO_UINT16(interval, p);
1726 STREAM_TO_UINT16(latency, p);
1727 STREAM_TO_UINT16(timeout, p);
1729 l2cble_process_conn_update_evt(handle, status, interval, latency, timeout);
1731 gatt_notify_conn_update(handle & 0x0FFF, interval, latency, timeout, status);
1734 static void btu_ble_read_remote_feat_evt(uint8_t* p) {
1735 btm_ble_read_remote_features_complete(p);
1738 static void btu_ble_proc_ltk_req(uint8_t* p) {
1739 uint16_t ediv, handle;
1742 STREAM_TO_UINT16(handle, p);
1744 STREAM_TO_UINT16(ediv, pp);
1745 btm_ble_ltk_request(handle, p, ediv);
1746 /* This is empty until an upper layer cares about returning event */
1749 static void btu_ble_data_length_change_evt(uint8_t* p, uint16_t evt_len) {
1751 uint16_t tx_data_len;
1752 uint16_t rx_data_len;
1754 if (!controller_get_interface()->supports_ble_packet_extension()) {
1755 HCI_TRACE_WARNING("%s, request not supported", __func__);
1759 STREAM_TO_UINT16(handle, p);
1760 STREAM_TO_UINT16(tx_data_len, p);
1761 p += 2; /* Skip the TxTimer */
1762 STREAM_TO_UINT16(rx_data_len, p);
1764 l2cble_process_data_length_change_event(handle, tx_data_len, rx_data_len);
1767 /**********************************************
1768 * End of BLE Events Handler
1769 **********************************************/
1770 #if (BLE_LLT_INCLUDED == TRUE)
1771 static void btu_ble_rc_param_req_evt(uint8_t* p) {
1773 uint16_t int_min, int_max, latency, timeout;
1775 STREAM_TO_UINT16(handle, p);
1776 STREAM_TO_UINT16(int_min, p);
1777 STREAM_TO_UINT16(int_max, p);
1778 STREAM_TO_UINT16(latency, p);
1779 STREAM_TO_UINT16(timeout, p);
1781 l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency,
1784 #endif /* BLE_LLT_INCLUDED */