OSDN Git Service

8243471fc2b3a3345d2f48db50295b65a8bba64a
[android-x86/system-bt.git] / stack / btu / btu_hcif.cc
1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /******************************************************************************
20  *
21  *  This file contains functions that interface with the HCI transport. On
22  *  the receive side, it routes events to the appropriate handler, e.g.
23  *  L2CAP, ScoMgr. On the transmit side, it manages the command
24  *  transmission.
25  *
26  ******************************************************************************/
27
28 #define LOG_TAG "bt_btu_hcif"
29
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>
35 #include <frameworks/base/core/proto/android/bluetooth/enums.pb.h>
36 #include <frameworks/base/core/proto/android/bluetooth/hci/enums.pb.h>
37 #include <statslog.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "bt_common.h"
43 #include "bt_types.h"
44 #include "bt_utils.h"
45 #include "btm_api.h"
46 #include "btm_int.h"
47 #include "btu.h"
48 #include "common/metrics.h"
49 #include "device/include/controller.h"
50 #include "hci_layer.h"
51 #include "hcimsgs.h"
52 #include "l2c_int.h"
53 #include "osi/include/log.h"
54 #include "osi/include/osi.h"
55
56 using base::Location;
57
58 extern void btm_process_cancel_complete(uint8_t status, uint8_t mode);
59 extern void btm_ble_test_command_complete(uint8_t* p);
60 extern void smp_cancel_start_encryption_attempt();
61
62 /******************************************************************************/
63 /*            L O C A L    F U N C T I O N     P R O T O T Y P E S            */
64 /******************************************************************************/
65 static void btu_hcif_inquiry_comp_evt(uint8_t* p);
66 static void btu_hcif_inquiry_result_evt(uint8_t* p);
67 static void btu_hcif_inquiry_rssi_result_evt(uint8_t* p);
68 static void btu_hcif_extended_inquiry_result_evt(uint8_t* p);
69
70 static void btu_hcif_connection_comp_evt(uint8_t* p);
71 static void btu_hcif_connection_request_evt(uint8_t* p);
72 static void btu_hcif_disconnection_comp_evt(uint8_t* p);
73 static void btu_hcif_authentication_comp_evt(uint8_t* p);
74 static void btu_hcif_rmt_name_request_comp_evt(uint8_t* p, uint16_t evt_len);
75 static void btu_hcif_encryption_change_evt(uint8_t* p);
76 static void btu_hcif_read_rmt_features_comp_evt(uint8_t* p);
77 static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p);
78 static void btu_hcif_read_rmt_version_comp_evt(uint8_t* p);
79 static void btu_hcif_qos_setup_comp_evt(uint8_t* p);
80 static void btu_hcif_command_complete_evt(BT_HDR* response, void* context);
81 static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command,
82                                         void* context);
83 static void btu_hcif_hardware_error_evt(uint8_t* p);
84 static void btu_hcif_flush_occured_evt(void);
85 static void btu_hcif_role_change_evt(uint8_t* p);
86 static void btu_hcif_num_compl_data_pkts_evt(uint8_t* p);
87 static void btu_hcif_mode_change_evt(uint8_t* p);
88 static void btu_hcif_pin_code_request_evt(uint8_t* p);
89 static void btu_hcif_link_key_request_evt(uint8_t* p);
90 static void btu_hcif_link_key_notification_evt(uint8_t* p);
91 static void btu_hcif_loopback_command_evt(void);
92 static void btu_hcif_data_buf_overflow_evt(void);
93 static void btu_hcif_max_slots_changed_evt(void);
94 static void btu_hcif_read_clock_off_comp_evt(uint8_t* p);
95 static void btu_hcif_conn_pkt_type_change_evt(void);
96 static void btu_hcif_qos_violation_evt(uint8_t* p);
97 static void btu_hcif_page_scan_mode_change_evt(void);
98 static void btu_hcif_page_scan_rep_mode_chng_evt(void);
99 static void btu_hcif_esco_connection_comp_evt(uint8_t* p);
100 static void btu_hcif_esco_connection_chg_evt(uint8_t* p);
101
102 /* Simple Pairing Events */
103 static void btu_hcif_host_support_evt(uint8_t* p);
104 static void btu_hcif_io_cap_request_evt(uint8_t* p);
105 static void btu_hcif_io_cap_response_evt(uint8_t* p);
106 static void btu_hcif_user_conf_request_evt(uint8_t* p);
107 static void btu_hcif_user_passkey_request_evt(uint8_t* p);
108 static void btu_hcif_user_passkey_notif_evt(uint8_t* p);
109 static void btu_hcif_keypress_notif_evt(uint8_t* p);
110 static void btu_hcif_rem_oob_request_evt(uint8_t* p);
111
112 static void btu_hcif_simple_pair_complete_evt(uint8_t* p);
113 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
114 static void btu_hcif_enhanced_flush_complete_evt(void);
115 #endif
116
117 #if (BTM_SSR_INCLUDED == TRUE)
118 static void btu_hcif_ssr_evt(uint8_t* p, uint16_t evt_len);
119 #endif /* BTM_SSR_INCLUDED == TRUE */
120
121 static void btu_ble_ll_conn_complete_evt(uint8_t* p, uint16_t evt_len);
122 static void btu_ble_read_remote_feat_evt(uint8_t* p);
123 static void btu_ble_ll_conn_param_upd_evt(uint8_t* p, uint16_t evt_len);
124 static void btu_ble_proc_ltk_req(uint8_t* p);
125 static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p);
126 static void btu_ble_data_length_change_evt(uint8_t* p, uint16_t evt_len);
127 #if (BLE_LLT_INCLUDED == TRUE)
128 static void btu_ble_rc_param_req_evt(uint8_t* p);
129 #endif
130 #if (BLE_PRIVACY_SPT == TRUE)
131 static void btu_ble_proc_enhanced_conn_cmpl(uint8_t* p, uint16_t evt_len);
132 #endif
133
134 /**
135  * Log HCI event metrics that are not handled in special functions
136  * @param evt_code event code
137  * @param p_event pointer to event parameter, skipping paremter length
138  */
139 void btu_hcif_log_event_metrics(uint8_t evt_code, uint8_t* p_event) {
140   uint32_t cmd = android::bluetooth::hci::CMD_UNKNOWN;
141   uint16_t status = android::bluetooth::hci::STATUS_UNKNOWN;
142   uint16_t reason = android::bluetooth::hci::STATUS_UNKNOWN;
143   uint16_t handle = bluetooth::common::kUnknownConnectionHandle;
144
145   RawAddress bda = RawAddress::kEmpty;
146   switch (evt_code) {
147     case HCI_IO_CAPABILITY_REQUEST_EVT:
148     case HCI_IO_CAPABILITY_RESPONSE_EVT:
149     case HCI_LINK_KEY_REQUEST_EVT:
150     case HCI_LINK_KEY_NOTIFICATION_EVT:
151     case HCI_USER_PASSKEY_REQUEST_EVT:
152     case HCI_USER_PASSKEY_NOTIFY_EVT:
153     case HCI_USER_CONFIRMATION_REQUEST_EVT:
154     case HCI_KEYPRESS_NOTIFY_EVT:
155     case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
156       STREAM_TO_BDADDR(bda, p_event);
157       bluetooth::common::LogClassicPairingEvent(bda, handle, cmd, evt_code,
158                                                 status, reason);
159       break;
160     case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
161     case HCI_RMT_NAME_REQUEST_COMP_EVT:
162       STREAM_TO_UINT8(status, p_event);
163       STREAM_TO_BDADDR(bda, p_event);
164       bluetooth::common::LogClassicPairingEvent(bda, handle, cmd, evt_code,
165                                                 status, reason);
166       break;
167     case HCI_AUTHENTICATION_COMP_EVT:
168       STREAM_TO_UINT8(status, p_event);
169       STREAM_TO_UINT16(handle, p_event);
170       handle = HCID_GET_HANDLE(handle);
171       bluetooth::common::LogClassicPairingEvent(bda, handle, cmd, evt_code,
172                                                 status, reason);
173       break;
174     case HCI_ENCRYPTION_CHANGE_EVT: {
175       uint8_t encryption_enabled;
176       STREAM_TO_UINT8(status, p_event);
177       STREAM_TO_UINT16(handle, p_event);
178       STREAM_TO_UINT8(encryption_enabled, p_event);
179       // Use reason field to store encryption enabled
180       bluetooth::common::LogClassicPairingEvent(bda, handle, cmd, evt_code,
181                                                 status, encryption_enabled);
182       break;
183     }
184     case HCI_CONNECTION_COMP_EVT: {
185       uint8_t link_type;
186       STREAM_TO_UINT8(status, p_event);
187       STREAM_TO_UINT16(handle, p_event);
188       STREAM_TO_BDADDR(bda, p_event);
189       STREAM_TO_UINT8(link_type, p_event);
190       handle = HCID_GET_HANDLE(handle);
191       bluetooth::common::LogLinkLayerConnectionEvent(
192           &bda, handle, android::bluetooth::DIRECTION_UNKNOWN, link_type, cmd,
193           evt_code, android::bluetooth::hci::BLE_EVT_UNKNOWN, status, reason);
194       break;
195     }
196     case HCI_CONNECTION_REQUEST_EVT: {
197       DEV_CLASS dc;
198       uint8_t link_type;
199       STREAM_TO_BDADDR(bda, p_event);
200       STREAM_TO_DEVCLASS(dc, p_event);
201       STREAM_TO_UINT8(link_type, p_event);
202       bluetooth::common::LogLinkLayerConnectionEvent(
203           &bda, bluetooth::common::kUnknownConnectionHandle,
204           android::bluetooth::DIRECTION_INCOMING, link_type, cmd, evt_code,
205           android::bluetooth::hci::BLE_EVT_UNKNOWN, status, reason);
206       break;
207     }
208     case HCI_DISCONNECTION_COMP_EVT: {
209       STREAM_TO_UINT8(status, p_event);
210       STREAM_TO_UINT16(handle, p_event);
211       STREAM_TO_UINT8(reason, p_event);
212       handle = HCID_GET_HANDLE(handle);
213       bluetooth::common::LogLinkLayerConnectionEvent(
214           nullptr, handle, android::bluetooth::DIRECTION_UNKNOWN,
215           android::bluetooth::LINK_TYPE_UNKNOWN, cmd, evt_code,
216           android::bluetooth::hci::BLE_EVT_UNKNOWN, status, reason);
217       break;
218     }
219     case HCI_ESCO_CONNECTION_COMP_EVT: {
220       uint8_t link_type;
221       STREAM_TO_UINT8(status, p_event);
222       STREAM_TO_UINT16(handle, p_event);
223       STREAM_TO_BDADDR(bda, p_event);
224       STREAM_TO_UINT8(link_type, p_event);
225       handle = HCID_GET_HANDLE(handle);
226       bluetooth::common::LogLinkLayerConnectionEvent(
227           &bda, handle, android::bluetooth::DIRECTION_UNKNOWN, link_type, cmd,
228           evt_code, android::bluetooth::hci::BLE_EVT_UNKNOWN, status, reason);
229       break;
230     }
231     case HCI_ESCO_CONNECTION_CHANGED_EVT: {
232       STREAM_TO_UINT8(status, p_event);
233       STREAM_TO_UINT16(handle, p_event);
234       handle = HCID_GET_HANDLE(handle);
235       bluetooth::common::LogLinkLayerConnectionEvent(
236           nullptr, handle, android::bluetooth::DIRECTION_UNKNOWN,
237           android::bluetooth::LINK_TYPE_UNKNOWN, cmd, evt_code,
238           android::bluetooth::hci::BLE_EVT_UNKNOWN, status, reason);
239       break;
240     }
241   }
242 }
243
244 /*******************************************************************************
245  *
246  * Function         btu_hcif_process_event
247  *
248  * Description      This function is called when an event is received from
249  *                  the Host Controller.
250  *
251  * Returns          void
252  *
253  ******************************************************************************/
254 void btu_hcif_process_event(UNUSED_ATTR uint8_t controller_id, BT_HDR* p_msg) {
255   uint8_t* p = (uint8_t*)(p_msg + 1) + p_msg->offset;
256   uint8_t hci_evt_code, hci_evt_len;
257   uint8_t ble_sub_code;
258   STREAM_TO_UINT8(hci_evt_code, p);
259   STREAM_TO_UINT8(hci_evt_len, p);
260
261   btu_hcif_log_event_metrics(hci_evt_code, p);
262
263   switch (hci_evt_code) {
264     case HCI_INQUIRY_COMP_EVT:
265       btu_hcif_inquiry_comp_evt(p);
266       break;
267     case HCI_INQUIRY_RESULT_EVT:
268       btu_hcif_inquiry_result_evt(p);
269       break;
270     case HCI_INQUIRY_RSSI_RESULT_EVT:
271       btu_hcif_inquiry_rssi_result_evt(p);
272       break;
273     case HCI_EXTENDED_INQUIRY_RESULT_EVT:
274       btu_hcif_extended_inquiry_result_evt(p);
275       break;
276     case HCI_CONNECTION_COMP_EVT:
277       btu_hcif_connection_comp_evt(p);
278       break;
279     case HCI_CONNECTION_REQUEST_EVT:
280       btu_hcif_connection_request_evt(p);
281       break;
282     case HCI_DISCONNECTION_COMP_EVT:
283       btu_hcif_disconnection_comp_evt(p);
284       break;
285     case HCI_AUTHENTICATION_COMP_EVT:
286       btu_hcif_authentication_comp_evt(p);
287       break;
288     case HCI_RMT_NAME_REQUEST_COMP_EVT:
289       btu_hcif_rmt_name_request_comp_evt(p, hci_evt_len);
290       break;
291     case HCI_ENCRYPTION_CHANGE_EVT:
292       btu_hcif_encryption_change_evt(p);
293       break;
294     case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
295       btu_hcif_encryption_key_refresh_cmpl_evt(p);
296       break;
297     case HCI_READ_RMT_FEATURES_COMP_EVT:
298       btu_hcif_read_rmt_features_comp_evt(p);
299       break;
300     case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
301       btu_hcif_read_rmt_ext_features_comp_evt(p);
302       break;
303     case HCI_READ_RMT_VERSION_COMP_EVT:
304       btu_hcif_read_rmt_version_comp_evt(p);
305       break;
306     case HCI_QOS_SETUP_COMP_EVT:
307       btu_hcif_qos_setup_comp_evt(p);
308       break;
309     case HCI_COMMAND_COMPLETE_EVT:
310       LOG_ERROR(LOG_TAG,
311                 "%s should not have received a command complete event. "
312                 "Someone didn't go through the hci transmit_command function.",
313                 __func__);
314       break;
315     case HCI_COMMAND_STATUS_EVT:
316       LOG_ERROR(LOG_TAG,
317                 "%s should not have received a command status event. "
318                 "Someone didn't go through the hci transmit_command function.",
319                 __func__);
320       break;
321     case HCI_HARDWARE_ERROR_EVT:
322       btu_hcif_hardware_error_evt(p);
323       break;
324     case HCI_FLUSH_OCCURED_EVT:
325       btu_hcif_flush_occured_evt();
326       break;
327     case HCI_ROLE_CHANGE_EVT:
328       btu_hcif_role_change_evt(p);
329       break;
330     case HCI_NUM_COMPL_DATA_PKTS_EVT:
331       btu_hcif_num_compl_data_pkts_evt(p);
332       break;
333     case HCI_MODE_CHANGE_EVT:
334       btu_hcif_mode_change_evt(p);
335       break;
336     case HCI_PIN_CODE_REQUEST_EVT:
337       btu_hcif_pin_code_request_evt(p);
338       break;
339     case HCI_LINK_KEY_REQUEST_EVT:
340       btu_hcif_link_key_request_evt(p);
341       break;
342     case HCI_LINK_KEY_NOTIFICATION_EVT:
343       btu_hcif_link_key_notification_evt(p);
344       break;
345     case HCI_LOOPBACK_COMMAND_EVT:
346       btu_hcif_loopback_command_evt();
347       break;
348     case HCI_DATA_BUF_OVERFLOW_EVT:
349       btu_hcif_data_buf_overflow_evt();
350       break;
351     case HCI_MAX_SLOTS_CHANGED_EVT:
352       btu_hcif_max_slots_changed_evt();
353       break;
354     case HCI_READ_CLOCK_OFF_COMP_EVT:
355       btu_hcif_read_clock_off_comp_evt(p);
356       break;
357     case HCI_CONN_PKT_TYPE_CHANGE_EVT:
358       btu_hcif_conn_pkt_type_change_evt();
359       break;
360     case HCI_QOS_VIOLATION_EVT:
361       btu_hcif_qos_violation_evt(p);
362       break;
363     case HCI_PAGE_SCAN_MODE_CHANGE_EVT:
364       btu_hcif_page_scan_mode_change_evt();
365       break;
366     case HCI_PAGE_SCAN_REP_MODE_CHNG_EVT:
367       btu_hcif_page_scan_rep_mode_chng_evt();
368       break;
369     case HCI_ESCO_CONNECTION_COMP_EVT:
370       btu_hcif_esco_connection_comp_evt(p);
371       break;
372     case HCI_ESCO_CONNECTION_CHANGED_EVT:
373       btu_hcif_esco_connection_chg_evt(p);
374       break;
375 #if (BTM_SSR_INCLUDED == TRUE)
376     case HCI_SNIFF_SUB_RATE_EVT:
377       btu_hcif_ssr_evt(p, hci_evt_len);
378       break;
379 #endif /* BTM_SSR_INCLUDED == TRUE */
380     case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
381       btu_hcif_host_support_evt(p);
382       break;
383     case HCI_IO_CAPABILITY_REQUEST_EVT:
384       btu_hcif_io_cap_request_evt(p);
385       break;
386     case HCI_IO_CAPABILITY_RESPONSE_EVT:
387       btu_hcif_io_cap_response_evt(p);
388       break;
389     case HCI_USER_CONFIRMATION_REQUEST_EVT:
390       btu_hcif_user_conf_request_evt(p);
391       break;
392     case HCI_USER_PASSKEY_REQUEST_EVT:
393       btu_hcif_user_passkey_request_evt(p);
394       break;
395     case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
396       btu_hcif_rem_oob_request_evt(p);
397       break;
398     case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
399       btu_hcif_simple_pair_complete_evt(p);
400       break;
401     case HCI_USER_PASSKEY_NOTIFY_EVT:
402       btu_hcif_user_passkey_notif_evt(p);
403       break;
404     case HCI_KEYPRESS_NOTIFY_EVT:
405       btu_hcif_keypress_notif_evt(p);
406       break;
407 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
408     case HCI_ENHANCED_FLUSH_COMPLETE_EVT:
409       btu_hcif_enhanced_flush_complete_evt();
410       break;
411 #endif
412
413     case HCI_BLE_EVENT: {
414       STREAM_TO_UINT8(ble_sub_code, p);
415
416       HCI_TRACE_EVENT("BLE HCI(id=%d) event = 0x%02x)", hci_evt_code,
417                       ble_sub_code);
418
419       uint8_t ble_evt_len = hci_evt_len - 1;
420       switch (ble_sub_code) {
421         case HCI_BLE_ADV_PKT_RPT_EVT: /* result of inquiry */
422           HCI_TRACE_EVENT("HCI_BLE_ADV_PKT_RPT_EVT");
423           btm_ble_process_adv_pkt(ble_evt_len, p);
424           break;
425         case HCI_BLE_CONN_COMPLETE_EVT:
426           btu_ble_ll_conn_complete_evt(p, hci_evt_len);
427           break;
428         case HCI_BLE_LL_CONN_PARAM_UPD_EVT:
429           btu_ble_ll_conn_param_upd_evt(p, hci_evt_len);
430           break;
431         case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
432           btu_ble_read_remote_feat_evt(p);
433           break;
434         case HCI_BLE_LTK_REQ_EVT: /* received only at slave device */
435           btu_ble_proc_ltk_req(p);
436           break;
437 #if (BLE_PRIVACY_SPT == TRUE)
438         case HCI_BLE_ENHANCED_CONN_COMPLETE_EVT:
439           btu_ble_proc_enhanced_conn_cmpl(p, hci_evt_len);
440           break;
441 #endif
442 #if (BLE_LLT_INCLUDED == TRUE)
443         case HCI_BLE_RC_PARAM_REQ_EVT:
444           btu_ble_rc_param_req_evt(p);
445           break;
446 #endif
447         case HCI_BLE_DATA_LENGTH_CHANGE_EVT:
448           btu_ble_data_length_change_evt(p, hci_evt_len);
449           break;
450
451         case HCI_BLE_PHY_UPDATE_COMPLETE_EVT:
452           btm_ble_process_phy_update_pkt(ble_evt_len, p);
453           break;
454
455         case HCI_LE_EXTENDED_ADVERTISING_REPORT_EVT:
456           btm_ble_process_ext_adv_pkt(hci_evt_len, p);
457           break;
458
459         case HCI_LE_ADVERTISING_SET_TERMINATED_EVT:
460           btm_le_on_advertising_set_terminated(p, hci_evt_len);
461           break;
462       }
463       break;
464     }
465
466     case HCI_VENDOR_SPECIFIC_EVT:
467       btm_vendor_specific_evt(p, hci_evt_len);
468       break;
469   }
470 }
471
472 static void btu_hcif_log_command_metrics(uint16_t opcode, uint8_t* p_cmd,
473                                          uint16_t cmd_status,
474                                          bool is_cmd_status) {
475   static uint16_t kUnknownBleEvt = android::bluetooth::hci::BLE_EVT_UNKNOWN;
476
477   uint16_t hci_event = android::bluetooth::hci::EVT_COMMAND_STATUS;
478   if (!is_cmd_status) {
479     hci_event = android::bluetooth::hci::EVT_UNKNOWN;
480     cmd_status = android::bluetooth::hci::STATUS_UNKNOWN;
481   }
482
483   RawAddress bd_addr;
484   uint16_t handle;
485   uint8_t reason;
486
487   switch (opcode) {
488     case HCI_CREATE_CONNECTION:
489     case HCI_CREATE_CONNECTION_CANCEL:
490       STREAM_TO_BDADDR(bd_addr, p_cmd);
491       bluetooth::common::LogLinkLayerConnectionEvent(
492           &bd_addr, bluetooth::common::kUnknownConnectionHandle,
493           android::bluetooth::DIRECTION_OUTGOING,
494           android::bluetooth::LINK_TYPE_ACL, opcode, hci_event, kUnknownBleEvt,
495           cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
496       break;
497     case HCI_DISCONNECT:
498       STREAM_TO_UINT16(handle, p_cmd);
499       STREAM_TO_UINT8(reason, p_cmd);
500       bluetooth::common::LogLinkLayerConnectionEvent(
501           nullptr, handle, android::bluetooth::DIRECTION_UNKNOWN,
502           android::bluetooth::LINK_TYPE_UNKNOWN, opcode, hci_event,
503           kUnknownBleEvt, cmd_status, reason);
504       break;
505     case HCI_SETUP_ESCO_CONNECTION:
506     case HCI_ENH_SETUP_ESCO_CONNECTION:
507       STREAM_TO_UINT16(handle, p_cmd);
508       bluetooth::common::LogLinkLayerConnectionEvent(
509           nullptr, handle, android::bluetooth::DIRECTION_OUTGOING,
510           android::bluetooth::LINK_TYPE_UNKNOWN, opcode, hci_event,
511           kUnknownBleEvt, cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
512       break;
513     case HCI_ACCEPT_CONNECTION_REQUEST:
514     case HCI_ACCEPT_ESCO_CONNECTION:
515     case HCI_ENH_ACCEPT_ESCO_CONNECTION:
516       STREAM_TO_BDADDR(bd_addr, p_cmd);
517       bluetooth::common::LogLinkLayerConnectionEvent(
518           &bd_addr, bluetooth::common::kUnknownConnectionHandle,
519           android::bluetooth::DIRECTION_INCOMING,
520           android::bluetooth::LINK_TYPE_UNKNOWN, opcode, hci_event,
521           kUnknownBleEvt, cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
522       break;
523     case HCI_REJECT_CONNECTION_REQUEST:
524     case HCI_REJECT_ESCO_CONNECTION:
525       STREAM_TO_BDADDR(bd_addr, p_cmd);
526       STREAM_TO_UINT8(reason, p_cmd);
527       bluetooth::common::LogLinkLayerConnectionEvent(
528           &bd_addr, bluetooth::common::kUnknownConnectionHandle,
529           android::bluetooth::DIRECTION_INCOMING,
530           android::bluetooth::LINK_TYPE_UNKNOWN, opcode, hci_event,
531           kUnknownBleEvt, cmd_status, reason);
532       break;
533
534       // BLE Commands
535     case HCI_BLE_CREATE_LL_CONN: {
536       p_cmd += 2;  // Skip LE_Scan_Interval
537       p_cmd += 2;  // Skip LE_Scan_Window;
538       uint8_t initiator_filter_policy;
539       STREAM_TO_UINT8(initiator_filter_policy, p_cmd);
540       uint8_t peer_address_type;
541       STREAM_TO_UINT8(peer_address_type, p_cmd);
542       STREAM_TO_BDADDR(bd_addr, p_cmd);
543       // Peer address should not be used if initiator filter policy is not 0x00
544       const RawAddress* bd_addr_p = nullptr;
545       if (initiator_filter_policy == 0x00) {
546         bd_addr_p = &bd_addr;
547 #if (BLE_PRIVACY_SPT == TRUE)
548         if (peer_address_type == BLE_ADDR_PUBLIC_ID ||
549             peer_address_type == BLE_ADDR_RANDOM_ID) {
550           // if identity address is not matched, this address is invalid
551           if (!btm_identity_addr_to_random_pseudo(&bd_addr, &peer_address_type,
552                                                   false)) {
553             bd_addr_p = nullptr;
554           }
555         }
556 #endif
557       }
558       if (initiator_filter_policy == 0x00 ||
559           (cmd_status != HCI_SUCCESS && !is_cmd_status)) {
560         // Selectively log to avoid log spam due to whitelist connections:
561         // - When doing non-whitelist connection
562         // - When there is an error in command status
563         bluetooth::common::LogLinkLayerConnectionEvent(
564             bd_addr_p, bluetooth::common::kUnknownConnectionHandle,
565             android::bluetooth::DIRECTION_OUTGOING,
566             android::bluetooth::LINK_TYPE_ACL, opcode, hci_event,
567             kUnknownBleEvt, cmd_status,
568             android::bluetooth::hci::STATUS_UNKNOWN);
569       }
570       break;
571     }
572     case HCI_LE_EXTENDED_CREATE_CONNECTION: {
573       uint8_t initiator_filter_policy;
574       STREAM_TO_UINT8(initiator_filter_policy, p_cmd);
575       p_cmd += 1;  // Skip Own_Address_Type
576       uint8_t peer_addr_type;
577       STREAM_TO_UINT8(peer_addr_type, p_cmd);
578       STREAM_TO_BDADDR(bd_addr, p_cmd);
579       // Peer address should not be used if initiator filter policy is not 0x00
580       const RawAddress* bd_addr_p = nullptr;
581       if (initiator_filter_policy == 0x00) {
582         bd_addr_p = &bd_addr;
583 #if (BLE_PRIVACY_SPT == TRUE)
584         // if identity address is not matched, this should be a static address
585         btm_identity_addr_to_random_pseudo(&bd_addr, &peer_addr_type, false);
586 #endif
587       }
588       if (initiator_filter_policy == 0x00 ||
589           (cmd_status != HCI_SUCCESS && !is_cmd_status)) {
590         // Selectively log to avoid log spam due to whitelist connections:
591         // - When doing non-whitelist connection
592         // - When there is an error in command status
593         bluetooth::common::LogLinkLayerConnectionEvent(
594             bd_addr_p, bluetooth::common::kUnknownConnectionHandle,
595             android::bluetooth::DIRECTION_OUTGOING,
596             android::bluetooth::LINK_TYPE_ACL, opcode, hci_event,
597             kUnknownBleEvt, cmd_status,
598             android::bluetooth::hci::STATUS_UNKNOWN);
599       }
600       break;
601     }
602     case HCI_BLE_CREATE_CONN_CANCEL:
603       if (cmd_status != HCI_SUCCESS && !is_cmd_status) {
604         // Only log errors to prevent log spam due to whitelist connections
605         bluetooth::common::LogLinkLayerConnectionEvent(
606             nullptr, bluetooth::common::kUnknownConnectionHandle,
607             android::bluetooth::DIRECTION_OUTGOING,
608             android::bluetooth::LINK_TYPE_ACL, opcode, hci_event,
609             kUnknownBleEvt, cmd_status,
610             android::bluetooth::hci::STATUS_UNKNOWN);
611       }
612       break;
613     case HCI_BLE_CLEAR_WHITE_LIST:
614       bluetooth::common::LogLinkLayerConnectionEvent(
615           nullptr, bluetooth::common::kUnknownConnectionHandle,
616           android::bluetooth::DIRECTION_INCOMING,
617           android::bluetooth::LINK_TYPE_ACL, opcode, hci_event, kUnknownBleEvt,
618           cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
619       break;
620     case HCI_BLE_ADD_WHITE_LIST:
621     case HCI_BLE_REMOVE_WHITE_LIST: {
622       uint8_t peer_addr_type;
623       STREAM_TO_UINT8(peer_addr_type, p_cmd);
624       STREAM_TO_BDADDR(bd_addr, p_cmd);
625       const RawAddress* bd_addr_p = nullptr;
626       // When peer_addr_type is 0xFF, bd_addr should be ignored per BT spec
627       if (peer_addr_type != BLE_ADDR_ANONYMOUS) {
628         bd_addr_p = &bd_addr;
629 #if (BLE_PRIVACY_SPT == TRUE)
630         bool addr_is_rpa = peer_addr_type == BLE_ADDR_RANDOM &&
631                            BTM_BLE_IS_RESOLVE_BDA(bd_addr);
632         // Only try to match identity address for pseudo if address is not RPA
633         if (!addr_is_rpa) {
634           // if identity address is not matched, this should be a static address
635           btm_identity_addr_to_random_pseudo(&bd_addr, &peer_addr_type, false);
636         }
637 #endif
638       }
639       bluetooth::common::LogLinkLayerConnectionEvent(
640           bd_addr_p, bluetooth::common::kUnknownConnectionHandle,
641           android::bluetooth::DIRECTION_INCOMING,
642           android::bluetooth::LINK_TYPE_ACL, opcode, hci_event, kUnknownBleEvt,
643           cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
644       break;
645     }
646     case HCI_READ_LOCAL_OOB_DATA:
647       bluetooth::common::LogClassicPairingEvent(
648           RawAddress::kEmpty, bluetooth::common::kUnknownConnectionHandle,
649           opcode, hci_event, cmd_status,
650           android::bluetooth::hci::STATUS_UNKNOWN);
651       break;
652     case HCI_WRITE_SIMPLE_PAIRING_MODE: {
653       uint8_t simple_pairing_mode;
654       STREAM_TO_UINT8(simple_pairing_mode, p_cmd);
655       // Use reason field to log simple pairing mode
656       bluetooth::common::LogClassicPairingEvent(
657           RawAddress::kEmpty, bluetooth::common::kUnknownConnectionHandle,
658           opcode, hci_event, cmd_status, simple_pairing_mode);
659       break;
660     }
661     case HCI_WRITE_SECURE_CONNS_SUPPORT: {
662       uint8_t secure_conn_host_support;
663       STREAM_TO_UINT8(secure_conn_host_support, p_cmd);
664       // Use reason field to log secure connection host support
665       bluetooth::common::LogClassicPairingEvent(
666           RawAddress::kEmpty, bluetooth::common::kUnknownConnectionHandle,
667           opcode, hci_event, cmd_status, secure_conn_host_support);
668       break;
669     }
670     case HCI_AUTHENTICATION_REQUESTED:
671       STREAM_TO_UINT16(handle, p_cmd);
672       bluetooth::common::LogClassicPairingEvent(
673           RawAddress::kEmpty, handle, opcode, hci_event, cmd_status,
674           android::bluetooth::hci::STATUS_UNKNOWN);
675       break;
676     case HCI_SET_CONN_ENCRYPTION: {
677       STREAM_TO_UINT16(handle, p_cmd);
678       uint8_t encryption_enable;
679       STREAM_TO_UINT8(encryption_enable, p_cmd);
680       // Use reason field to log encryption enable
681       bluetooth::common::LogClassicPairingEvent(RawAddress::kEmpty, handle,
682                                                 opcode, hci_event, cmd_status,
683                                                 encryption_enable);
684       break;
685     }
686     case HCI_DELETE_STORED_LINK_KEY: {
687       uint8_t delete_all_flag;
688       STREAM_TO_BDADDR(bd_addr, p_cmd);
689       STREAM_TO_UINT8(delete_all_flag, p_cmd);
690       // Use reason field to log delete_all_flag
691       bluetooth::common::LogClassicPairingEvent(
692           bd_addr, bluetooth::common::kUnknownConnectionHandle, opcode,
693           hci_event, cmd_status, delete_all_flag);
694       break;
695     }
696     case HCI_RMT_NAME_REQUEST:
697     case HCI_RMT_NAME_REQUEST_CANCEL:
698     case HCI_LINK_KEY_REQUEST_REPLY:
699     case HCI_LINK_KEY_REQUEST_NEG_REPLY:
700     case HCI_IO_CAPABILITY_REQUEST_REPLY:
701     case HCI_USER_CONF_REQUEST_REPLY:
702     case HCI_USER_CONF_VALUE_NEG_REPLY:
703     case HCI_USER_PASSKEY_REQ_REPLY:
704     case HCI_USER_PASSKEY_REQ_NEG_REPLY:
705     case HCI_REM_OOB_DATA_REQ_REPLY:
706     case HCI_REM_OOB_DATA_REQ_NEG_REPLY:
707       STREAM_TO_BDADDR(bd_addr, p_cmd);
708       bluetooth::common::LogClassicPairingEvent(
709           bd_addr, bluetooth::common::kUnknownConnectionHandle, opcode,
710           hci_event, cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
711       break;
712     case HCI_IO_CAP_REQ_NEG_REPLY:
713       STREAM_TO_BDADDR(bd_addr, p_cmd);
714       STREAM_TO_UINT8(reason, p_cmd);
715       bluetooth::common::LogClassicPairingEvent(
716           bd_addr, bluetooth::common::kUnknownConnectionHandle, opcode,
717           hci_event, cmd_status, reason);
718       break;
719   }
720 }
721
722 /*******************************************************************************
723  *
724  * Function         btu_hcif_send_cmd
725  *
726  * Description      This function is called to send commands to the Host
727  *                  Controller.
728  *
729  * Returns          void
730  *
731  ******************************************************************************/
732 void btu_hcif_send_cmd(UNUSED_ATTR uint8_t controller_id, BT_HDR* p_buf) {
733   if (!p_buf) return;
734
735   uint16_t opcode;
736   uint8_t* stream = p_buf->data + p_buf->offset;
737   void* vsc_callback = NULL;
738
739   STREAM_TO_UINT16(opcode, stream);
740
741   // Eww...horrible hackery here
742   /* If command was a VSC, then extract command_complete callback */
743   if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC ||
744       (opcode == HCI_BLE_RAND) || (opcode == HCI_BLE_ENCRYPT)) {
745     vsc_callback = *((void**)(p_buf + 1));
746   }
747
748   // Skip parameter length before logging
749   stream++;
750   btu_hcif_log_command_metrics(opcode, stream,
751                                android::bluetooth::hci::STATUS_UNKNOWN, false);
752
753   hci_layer_get_interface()->transmit_command(
754       p_buf, btu_hcif_command_complete_evt, btu_hcif_command_status_evt,
755       vsc_callback);
756 }
757
758 using hci_cmd_cb = base::OnceCallback<void(
759     uint8_t* /* return_parameters */, uint16_t /* return_parameters_length*/)>;
760
761 struct cmd_with_cb_data {
762   hci_cmd_cb cb;
763   Location posted_from;
764 };
765
766 void cmd_with_cb_data_init(cmd_with_cb_data* cb_wrapper) {
767   new (&cb_wrapper->cb) hci_cmd_cb;
768   new (&cb_wrapper->posted_from) Location;
769 }
770
771 void cmd_with_cb_data_cleanup(cmd_with_cb_data* cb_wrapper) {
772   cb_wrapper->cb.~hci_cmd_cb();
773   cb_wrapper->posted_from.~Location();
774 }
775
776 /**
777  * Log command complete events that is not handled individually in this file
778  * @param opcode opcode of the command
779  * @param p_return_params pointer to returned parameter after parameter length
780  *                        field
781  */
782 static void btu_hcif_log_command_complete_metrics(uint16_t opcode,
783                                                   uint8_t* p_return_params) {
784   uint16_t status = android::bluetooth::hci::STATUS_UNKNOWN;
785   uint16_t reason = android::bluetooth::hci::STATUS_UNKNOWN;
786   uint16_t hci_event = android::bluetooth::hci::EVT_COMMAND_COMPLETE;
787   uint16_t hci_ble_event = android::bluetooth::hci::BLE_EVT_UNKNOWN;
788   RawAddress bd_addr = RawAddress::kEmpty;
789   switch (opcode) {
790     case HCI_BLE_CLEAR_WHITE_LIST:
791     case HCI_BLE_ADD_WHITE_LIST:
792     case HCI_BLE_REMOVE_WHITE_LIST: {
793       STREAM_TO_UINT8(status, p_return_params);
794       bluetooth::common::LogLinkLayerConnectionEvent(
795           nullptr, bluetooth::common::kUnknownConnectionHandle,
796           android::bluetooth::DIRECTION_INCOMING,
797           android::bluetooth::LINK_TYPE_ACL, opcode, hci_event, hci_ble_event,
798           status, reason);
799       break;
800     }
801     case HCI_DELETE_STORED_LINK_KEY:
802     case HCI_READ_LOCAL_OOB_DATA:
803     case HCI_WRITE_SIMPLE_PAIRING_MODE:
804     case HCI_WRITE_SECURE_CONNS_SUPPORT:
805       STREAM_TO_UINT8(status, p_return_params);
806       bluetooth::common::LogClassicPairingEvent(
807           RawAddress::kEmpty, bluetooth::common::kUnknownConnectionHandle,
808           opcode, hci_event, status, reason);
809       break;
810     case HCI_LINK_KEY_REQUEST_REPLY:
811     case HCI_LINK_KEY_REQUEST_NEG_REPLY:
812     case HCI_IO_CAPABILITY_REQUEST_REPLY:
813     case HCI_IO_CAP_REQ_NEG_REPLY:
814     case HCI_USER_CONF_REQUEST_REPLY:
815     case HCI_USER_CONF_VALUE_NEG_REPLY:
816     case HCI_USER_PASSKEY_REQ_REPLY:
817     case HCI_USER_PASSKEY_REQ_NEG_REPLY:
818     case HCI_REM_OOB_DATA_REQ_REPLY:
819     case HCI_REM_OOB_DATA_REQ_NEG_REPLY:
820       STREAM_TO_UINT8(status, p_return_params);
821       STREAM_TO_BDADDR(bd_addr, p_return_params);
822       bluetooth::common::LogClassicPairingEvent(
823           bd_addr, bluetooth::common::kUnknownConnectionHandle, opcode,
824           hci_event, status, reason);
825       break;
826   }
827 }
828
829 static void btu_hcif_command_complete_evt_with_cb_on_task(BT_HDR* event,
830                                                           void* context) {
831   command_opcode_t opcode;
832   // 2 for event header: event code (1) + parameter length (1)
833   // 1 for num_hci_pkt command credit
834   uint8_t* stream = event->data + event->offset + 3;
835   STREAM_TO_UINT16(opcode, stream);
836
837   btu_hcif_log_command_complete_metrics(opcode, stream);
838
839   cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)context;
840   HCI_TRACE_DEBUG("command complete for: %s",
841                   cb_wrapper->posted_from.ToString().c_str());
842   // 2 for event header: event code (1) + parameter length (1)
843   // 3 for command complete header: num_hci_pkt (1) + opcode (2)
844   uint16_t param_len = static_cast<uint16_t>(event->len - 5);
845   std::move(cb_wrapper->cb).Run(stream, param_len);
846   cmd_with_cb_data_cleanup(cb_wrapper);
847   osi_free(cb_wrapper);
848
849   osi_free(event);
850 }
851
852 static void btu_hcif_command_complete_evt_with_cb(BT_HDR* response,
853                                                   void* context) {
854   do_in_main_thread(FROM_HERE,
855                     base::Bind(btu_hcif_command_complete_evt_with_cb_on_task,
856                                response, context));
857 }
858
859 static void btu_hcif_command_status_evt_with_cb_on_task(uint8_t status,
860                                                         BT_HDR* event,
861                                                         void* context) {
862   command_opcode_t opcode;
863   uint8_t* stream = event->data + event->offset;
864   STREAM_TO_UINT16(opcode, stream);
865
866   CHECK(status != 0);
867
868   // stream + 1 to skip parameter length field
869   // No need to check length since stream is written by us
870   btu_hcif_log_command_metrics(opcode, stream + 1, status, true);
871
872   // report command status error
873   cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)context;
874   HCI_TRACE_DEBUG("command status for: %s",
875                   cb_wrapper->posted_from.ToString().c_str());
876   std::move(cb_wrapper->cb).Run(&status, sizeof(uint16_t));
877   cmd_with_cb_data_cleanup(cb_wrapper);
878   osi_free(cb_wrapper);
879
880   osi_free(event);
881 }
882
883 static void btu_hcif_command_status_evt_with_cb(uint8_t status, BT_HDR* command,
884                                                 void* context) {
885   // Command is pending, we  report only error.
886   if (!status) {
887     osi_free(command);
888     return;
889   }
890
891   do_in_main_thread(
892       FROM_HERE, base::Bind(btu_hcif_command_status_evt_with_cb_on_task, status,
893                             command, context));
894 }
895
896 /* This function is called to send commands to the Host Controller. |cb| is
897  * called when command status event is called with error code, or when the
898  * command complete event is received. */
899 void btu_hcif_send_cmd_with_cb(const Location& posted_from, uint16_t opcode,
900                                uint8_t* params, uint8_t params_len,
901                                hci_cmd_cb cb) {
902   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
903   uint8_t* pp = (uint8_t*)(p + 1);
904
905   p->len = HCIC_PREAMBLE_SIZE + params_len;
906   p->offset = 0;
907
908   UINT16_TO_STREAM(pp, opcode);
909   UINT8_TO_STREAM(pp, params_len);
910   if (params) {
911     memcpy(pp, params, params_len);
912   }
913
914   btu_hcif_log_command_metrics(opcode, pp,
915                                android::bluetooth::hci::STATUS_UNKNOWN, false);
916
917   cmd_with_cb_data* cb_wrapper =
918       (cmd_with_cb_data*)osi_malloc(sizeof(cmd_with_cb_data));
919
920   cmd_with_cb_data_init(cb_wrapper);
921   cb_wrapper->cb = std::move(cb);
922   cb_wrapper->posted_from = posted_from;
923
924   hci_layer_get_interface()->transmit_command(
925       p, btu_hcif_command_complete_evt_with_cb,
926       btu_hcif_command_status_evt_with_cb, (void*)cb_wrapper);
927 }
928
929 /*******************************************************************************
930  *
931  * Function         btu_hcif_inquiry_comp_evt
932  *
933  * Description      Process event HCI_INQUIRY_COMP_EVT
934  *
935  * Returns          void
936  *
937  ******************************************************************************/
938 static void btu_hcif_inquiry_comp_evt(uint8_t* p) {
939   uint8_t status;
940
941   STREAM_TO_UINT8(status, p);
942
943   /* Tell inquiry processing that we are done */
944   btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
945 }
946
947 /*******************************************************************************
948  *
949  * Function         btu_hcif_inquiry_result_evt
950  *
951  * Description      Process event HCI_INQUIRY_RESULT_EVT
952  *
953  * Returns          void
954  *
955  ******************************************************************************/
956 static void btu_hcif_inquiry_result_evt(uint8_t* p) {
957   /* Store results in the cache */
958   btm_process_inq_results(p, BTM_INQ_RESULT_STANDARD);
959 }
960
961 /*******************************************************************************
962  *
963  * Function         btu_hcif_inquiry_rssi_result_evt
964  *
965  * Description      Process event HCI_INQUIRY_RSSI_RESULT_EVT
966  *
967  * Returns          void
968  *
969  ******************************************************************************/
970 static void btu_hcif_inquiry_rssi_result_evt(uint8_t* p) {
971   /* Store results in the cache */
972   btm_process_inq_results(p, BTM_INQ_RESULT_WITH_RSSI);
973 }
974
975 /*******************************************************************************
976  *
977  * Function         btu_hcif_extended_inquiry_result_evt
978  *
979  * Description      Process event HCI_EXTENDED_INQUIRY_RESULT_EVT
980  *
981  * Returns          void
982  *
983  ******************************************************************************/
984 static void btu_hcif_extended_inquiry_result_evt(uint8_t* p) {
985   /* Store results in the cache */
986   btm_process_inq_results(p, BTM_INQ_RESULT_EXTENDED);
987 }
988
989 /*******************************************************************************
990  *
991  * Function         btu_hcif_connection_comp_evt
992  *
993  * Description      Process event HCI_CONNECTION_COMP_EVT
994  *
995  * Returns          void
996  *
997  ******************************************************************************/
998 static void btu_hcif_connection_comp_evt(uint8_t* p) {
999   uint8_t status;
1000   uint16_t handle;
1001   RawAddress bda;
1002   uint8_t link_type;
1003   uint8_t enc_mode;
1004   tBTM_ESCO_DATA esco_data;
1005
1006   STREAM_TO_UINT8(status, p);
1007   STREAM_TO_UINT16(handle, p);
1008   STREAM_TO_BDADDR(bda, p);
1009   STREAM_TO_UINT8(link_type, p);
1010   STREAM_TO_UINT8(enc_mode, p);
1011
1012   handle = HCID_GET_HANDLE(handle);
1013
1014   if (status != HCI_SUCCESS) {
1015     HCI_TRACE_DEBUG(
1016         "%s: Connection failed: status=%d, handle=%d, link_type=%d, "
1017         "enc_mode=%d",
1018         __func__, status, handle, link_type, enc_mode);
1019   }
1020
1021   if (link_type == HCI_LINK_TYPE_ACL) {
1022     btm_sec_connected(bda, handle, status, enc_mode);
1023
1024     l2c_link_hci_conn_comp(status, handle, bda);
1025   } else {
1026     memset(&esco_data, 0, sizeof(tBTM_ESCO_DATA));
1027     /* esco_data.link_type = HCI_LINK_TYPE_SCO; already zero */
1028     esco_data.bd_addr = bda;
1029     btm_sco_connected(status, &bda, handle, &esco_data);
1030   }
1031 }
1032
1033 /*******************************************************************************
1034  *
1035  * Function         btu_hcif_connection_request_evt
1036  *
1037  * Description      Process event HCI_CONNECTION_REQUEST_EVT
1038  *
1039  * Returns          void
1040  *
1041  ******************************************************************************/
1042 static void btu_hcif_connection_request_evt(uint8_t* p) {
1043   RawAddress bda;
1044   DEV_CLASS dc;
1045   uint8_t link_type;
1046
1047   STREAM_TO_BDADDR(bda, p);
1048   STREAM_TO_DEVCLASS(dc, p);
1049   STREAM_TO_UINT8(link_type, p);
1050
1051   /* Pass request to security manager to check connect filters before */
1052   /* passing request to l2cap */
1053   if (link_type == HCI_LINK_TYPE_ACL) {
1054     btm_sec_conn_req(bda, dc);
1055   } else {
1056     btm_sco_conn_req(bda, dc, link_type);
1057   }
1058 }
1059
1060 /*******************************************************************************
1061  *
1062  * Function         btu_hcif_disconnection_comp_evt
1063  *
1064  * Description      Process event HCI_DISCONNECTION_COMP_EVT
1065  *
1066  * Returns          void
1067  *
1068  ******************************************************************************/
1069 static void btu_hcif_disconnection_comp_evt(uint8_t* p) {
1070   uint8_t status;
1071   uint16_t handle;
1072   uint8_t reason;
1073
1074   STREAM_TO_UINT8(status, p);
1075   STREAM_TO_UINT16(handle, p);
1076   STREAM_TO_UINT8(reason, p);
1077
1078   handle = HCID_GET_HANDLE(handle);
1079
1080   if ((reason != HCI_ERR_CONN_CAUSE_LOCAL_HOST) &&
1081       (reason != HCI_ERR_PEER_USER)) {
1082     /* Uncommon disconnection reasons */
1083     HCI_TRACE_DEBUG("%s: Got Disconn Complete Event: reason=%d, handle=%d",
1084                     __func__, reason, handle);
1085   }
1086
1087   /* If L2CAP doesn't know about it, send it to SCO */
1088   if (!l2c_link_hci_disc_comp(handle, reason)) btm_sco_removed(handle, reason);
1089
1090   /* Notify security manager */
1091   btm_sec_disconnected(handle, reason);
1092 }
1093
1094 /*******************************************************************************
1095  *
1096  * Function         btu_hcif_authentication_comp_evt
1097  *
1098  * Description      Process event HCI_AUTHENTICATION_COMP_EVT
1099  *
1100  * Returns          void
1101  *
1102  ******************************************************************************/
1103 static void btu_hcif_authentication_comp_evt(uint8_t* p) {
1104   uint8_t status;
1105   uint16_t handle;
1106
1107   STREAM_TO_UINT8(status, p);
1108   STREAM_TO_UINT16(handle, p);
1109
1110   btm_sec_auth_complete(handle, status);
1111 }
1112
1113 /*******************************************************************************
1114  *
1115  * Function         btu_hcif_rmt_name_request_comp_evt
1116  *
1117  * Description      Process event HCI_RMT_NAME_REQUEST_COMP_EVT
1118  *
1119  * Returns          void
1120  *
1121  ******************************************************************************/
1122 static void btu_hcif_rmt_name_request_comp_evt(uint8_t* p, uint16_t evt_len) {
1123   uint8_t status;
1124   RawAddress bd_addr;
1125
1126   STREAM_TO_UINT8(status, p);
1127   STREAM_TO_BDADDR(bd_addr, p);
1128
1129   evt_len -= (1 + BD_ADDR_LEN);
1130
1131   btm_process_remote_name(&bd_addr, p, evt_len, status);
1132
1133   btm_sec_rmt_name_request_complete(&bd_addr, p, status);
1134 }
1135
1136 /*******************************************************************************
1137  *
1138  * Function         btu_hcif_encryption_change_evt
1139  *
1140  * Description      Process event HCI_ENCRYPTION_CHANGE_EVT
1141  *
1142  * Returns          void
1143  *
1144  ******************************************************************************/
1145 static void btu_hcif_encryption_change_evt(uint8_t* p) {
1146   uint8_t status;
1147   uint16_t handle;
1148   uint8_t encr_enable;
1149
1150   STREAM_TO_UINT8(status, p);
1151   STREAM_TO_UINT16(handle, p);
1152   STREAM_TO_UINT8(encr_enable, p);
1153
1154   if (status == HCI_ERR_CONNECTION_TOUT) {
1155     smp_cancel_start_encryption_attempt();
1156     return;
1157   }
1158
1159   btm_acl_encrypt_change(handle, status, encr_enable);
1160   btm_sec_encrypt_change(handle, status, encr_enable);
1161 }
1162
1163 /*******************************************************************************
1164  *
1165  * Function         btu_hcif_read_rmt_features_comp_evt
1166  *
1167  * Description      Process event HCI_READ_RMT_FEATURES_COMP_EVT
1168  *
1169  * Returns          void
1170  *
1171  ******************************************************************************/
1172 static void btu_hcif_read_rmt_features_comp_evt(uint8_t* p) {
1173   btm_read_remote_features_complete(p);
1174 }
1175
1176 /*******************************************************************************
1177  *
1178  * Function         btu_hcif_read_rmt_ext_features_comp_evt
1179  *
1180  * Description      Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
1181  *
1182  * Returns          void
1183  *
1184  ******************************************************************************/
1185 static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p) {
1186   uint8_t* p_cur = p;
1187   uint8_t status;
1188   uint16_t handle;
1189
1190   STREAM_TO_UINT8(status, p_cur);
1191
1192   if (status == HCI_SUCCESS)
1193     btm_read_remote_ext_features_complete(p);
1194   else {
1195     STREAM_TO_UINT16(handle, p_cur);
1196     btm_read_remote_ext_features_failed(status, handle);
1197   }
1198 }
1199
1200 /*******************************************************************************
1201  *
1202  * Function         btu_hcif_read_rmt_version_comp_evt
1203  *
1204  * Description      Process event HCI_READ_RMT_VERSION_COMP_EVT
1205  *
1206  * Returns          void
1207  *
1208  ******************************************************************************/
1209 static void btu_hcif_read_rmt_version_comp_evt(uint8_t* p) {
1210   btm_read_remote_version_complete(p);
1211 }
1212
1213 /*******************************************************************************
1214  *
1215  * Function         btu_hcif_qos_setup_comp_evt
1216  *
1217  * Description      Process event HCI_QOS_SETUP_COMP_EVT
1218  *
1219  * Returns          void
1220  *
1221  ******************************************************************************/
1222 static void btu_hcif_qos_setup_comp_evt(uint8_t* p) {
1223   uint8_t status;
1224   uint16_t handle;
1225   FLOW_SPEC flow;
1226
1227   STREAM_TO_UINT8(status, p);
1228   STREAM_TO_UINT16(handle, p);
1229   STREAM_TO_UINT8(flow.qos_flags, p);
1230   STREAM_TO_UINT8(flow.service_type, p);
1231   STREAM_TO_UINT32(flow.token_rate, p);
1232   STREAM_TO_UINT32(flow.peak_bandwidth, p);
1233   STREAM_TO_UINT32(flow.latency, p);
1234   STREAM_TO_UINT32(flow.delay_variation, p);
1235
1236   btm_qos_setup_complete(status, handle, &flow);
1237 }
1238
1239 /*******************************************************************************
1240  *
1241  * Function         btu_hcif_esco_connection_comp_evt
1242  *
1243  * Description      Process event HCI_ESCO_CONNECTION_COMP_EVT
1244  *
1245  * Returns          void
1246  *
1247  ******************************************************************************/
1248 static void btu_hcif_esco_connection_comp_evt(uint8_t* p) {
1249   tBTM_ESCO_DATA data;
1250   uint16_t handle;
1251   RawAddress bda;
1252   uint8_t status;
1253
1254   STREAM_TO_UINT8(status, p);
1255   STREAM_TO_UINT16(handle, p);
1256   STREAM_TO_BDADDR(bda, p);
1257
1258   STREAM_TO_UINT8(data.link_type, p);
1259   STREAM_TO_UINT8(data.tx_interval, p);
1260   STREAM_TO_UINT8(data.retrans_window, p);
1261   STREAM_TO_UINT16(data.rx_pkt_len, p);
1262   STREAM_TO_UINT16(data.tx_pkt_len, p);
1263   STREAM_TO_UINT8(data.air_mode, p);
1264
1265   handle = HCID_GET_HANDLE(handle);
1266
1267   data.bd_addr = bda;
1268   btm_sco_connected(status, &bda, handle, &data);
1269 }
1270
1271 /*******************************************************************************
1272  *
1273  * Function         btu_hcif_esco_connection_chg_evt
1274  *
1275  * Description      Process event HCI_ESCO_CONNECTION_CHANGED_EVT
1276  *
1277  * Returns          void
1278  *
1279  ******************************************************************************/
1280 static void btu_hcif_esco_connection_chg_evt(uint8_t* p) {
1281   uint16_t handle;
1282   uint16_t tx_pkt_len;
1283   uint16_t rx_pkt_len;
1284   uint8_t status;
1285   uint8_t tx_interval;
1286   uint8_t retrans_window;
1287
1288   STREAM_TO_UINT8(status, p);
1289   STREAM_TO_UINT16(handle, p);
1290
1291   STREAM_TO_UINT8(tx_interval, p);
1292   STREAM_TO_UINT8(retrans_window, p);
1293   STREAM_TO_UINT16(rx_pkt_len, p);
1294   STREAM_TO_UINT16(tx_pkt_len, p);
1295
1296   handle = HCID_GET_HANDLE(handle);
1297
1298   btm_esco_proc_conn_chg(status, handle, tx_interval, retrans_window,
1299                          rx_pkt_len, tx_pkt_len);
1300 }
1301
1302 /*******************************************************************************
1303  *
1304  * Function         btu_hcif_hdl_command_complete
1305  *
1306  * Description      Handle command complete event
1307  *
1308  * Returns          void
1309  *
1310  ******************************************************************************/
1311 static void btu_hcif_hdl_command_complete(uint16_t opcode, uint8_t* p,
1312                                           uint16_t evt_len,
1313                                           void* p_cplt_cback) {
1314   switch (opcode) {
1315     case HCI_INQUIRY_CANCEL:
1316       /* Tell inquiry processing that we are done */
1317       btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
1318       break;
1319     case HCI_SET_EVENT_FILTER:
1320       btm_event_filter_complete(p);
1321       break;
1322
1323     case HCI_DELETE_STORED_LINK_KEY:
1324       btm_delete_stored_link_key_complete(p);
1325       break;
1326
1327     case HCI_READ_LOCAL_NAME:
1328       btm_read_local_name_complete(p, evt_len);
1329       break;
1330
1331     case HCI_GET_LINK_QUALITY:
1332       btm_read_link_quality_complete(p);
1333       break;
1334
1335     case HCI_READ_RSSI:
1336       btm_read_rssi_complete(p);
1337       break;
1338
1339     case HCI_READ_FAILED_CONTACT_COUNTER:
1340       btm_read_failed_contact_counter_complete(p);
1341       break;
1342
1343     case HCI_READ_AUTOMATIC_FLUSH_TIMEOUT:
1344       btm_read_automatic_flush_timeout_complete(p);
1345       break;
1346
1347     case HCI_READ_TRANSMIT_POWER_LEVEL:
1348       btm_read_tx_power_complete(p, false);
1349       break;
1350
1351     case HCI_CREATE_CONNECTION_CANCEL:
1352       btm_create_conn_cancel_complete(p);
1353       break;
1354
1355     case HCI_READ_LOCAL_OOB_DATA:
1356       btm_read_local_oob_complete(p);
1357       break;
1358
1359     case HCI_READ_INQ_TX_POWER_LEVEL:
1360       btm_read_inq_tx_power_complete(p);
1361       break;
1362
1363     /* BLE Commands sComplete*/
1364     case HCI_BLE_RAND:
1365     case HCI_BLE_ENCRYPT:
1366       btm_ble_rand_enc_complete(p, opcode, (tBTM_RAND_ENC_CB*)p_cplt_cback);
1367       break;
1368
1369     case HCI_BLE_READ_ADV_CHNL_TX_POWER:
1370       btm_read_tx_power_complete(p, true);
1371       break;
1372
1373     case HCI_BLE_WRITE_ADV_ENABLE:
1374       btm_ble_write_adv_enable_complete(p);
1375       break;
1376
1377     case HCI_BLE_CREATE_LL_CONN:
1378     case HCI_LE_EXTENDED_CREATE_CONNECTION:
1379       // No command complete event for those commands according to spec
1380       LOG(ERROR) << "No command complete expected, but received!";
1381       break;
1382
1383     case HCI_BLE_CREATE_CONN_CANCEL:
1384       btm_ble_create_conn_cancel_complete(p);
1385       break;
1386
1387     case HCI_BLE_TRANSMITTER_TEST:
1388     case HCI_BLE_RECEIVER_TEST:
1389     case HCI_BLE_TEST_END:
1390       btm_ble_test_command_complete(p);
1391       break;
1392
1393 #if (BLE_PRIVACY_SPT == TRUE)
1394     case HCI_BLE_ADD_DEV_RESOLVING_LIST:
1395       btm_ble_add_resolving_list_entry_complete(p, evt_len);
1396       break;
1397
1398     case HCI_BLE_RM_DEV_RESOLVING_LIST:
1399       btm_ble_remove_resolving_list_entry_complete(p, evt_len);
1400       break;
1401
1402     case HCI_BLE_CLEAR_RESOLVING_LIST:
1403       btm_ble_clear_resolving_list_complete(p, evt_len);
1404       break;
1405
1406     case HCI_BLE_READ_RESOLVABLE_ADDR_PEER:
1407       btm_ble_read_resolving_list_entry_complete(p, evt_len);
1408       break;
1409
1410     case HCI_BLE_READ_RESOLVABLE_ADDR_LOCAL:
1411     case HCI_BLE_SET_ADDR_RESOLUTION_ENABLE:
1412     case HCI_BLE_SET_RAND_PRIV_ADDR_TIMOUT:
1413       break;
1414 #endif
1415     default:
1416       if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1417         btm_vsc_complete(p, opcode, evt_len, (tBTM_VSC_CMPL_CB*)p_cplt_cback);
1418       break;
1419   }
1420 }
1421
1422 /*******************************************************************************
1423  *
1424  * Function         btu_hcif_command_complete_evt
1425  *
1426  * Description      Process event HCI_COMMAND_COMPLETE_EVT
1427  *
1428  * Returns          void
1429  *
1430  ******************************************************************************/
1431 static void btu_hcif_command_complete_evt_on_task(BT_HDR* event,
1432                                                   void* context) {
1433   command_opcode_t opcode;
1434   // 2 for event header: event code (1) + parameter length (1)
1435   // 1 for num_hci_pkt command credit
1436   uint8_t* stream = event->data + event->offset + 3;
1437   STREAM_TO_UINT16(opcode, stream);
1438
1439   btu_hcif_log_command_complete_metrics(opcode, stream);
1440   // 2 for event header: event code (1) + parameter length (1)
1441   // 3 for command complete header: num_hci_pkt (1) + opcode (2)
1442   uint16_t param_len = static_cast<uint16_t>(event->len - 5);
1443   btu_hcif_hdl_command_complete(opcode, stream, param_len, context);
1444
1445   osi_free(event);
1446 }
1447
1448 static void btu_hcif_command_complete_evt(BT_HDR* response, void* context) {
1449   do_in_main_thread(FROM_HERE, base::Bind(btu_hcif_command_complete_evt_on_task,
1450                                           response, context));
1451 }
1452
1453 /*******************************************************************************
1454  *
1455  * Function         btu_hcif_hdl_command_status
1456  *
1457  * Description      Handle a command status event
1458  *
1459  * Returns          void
1460  *
1461  ******************************************************************************/
1462 static void btu_hcif_hdl_command_status(uint16_t opcode, uint8_t status,
1463                                         uint8_t* p_cmd,
1464                                         void* p_vsc_status_cback) {
1465   CHECK_NE(p_cmd, nullptr) << "Null command for opcode 0x" << loghex(opcode);
1466   p_cmd++;  // Skip parameter total length
1467
1468   RawAddress bd_addr;
1469   uint16_t handle;
1470
1471   switch (opcode) {
1472     // Link Control Commands
1473     case HCI_INQUIRY:
1474       if (status != HCI_SUCCESS) {
1475         // Tell inquiry processing that we are done
1476         btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
1477       }
1478       break;
1479     case HCI_QOS_SETUP:
1480       if (status != HCI_SUCCESS) {
1481         // Tell qos setup that we are done
1482         btm_qos_setup_complete(status, 0, nullptr);
1483       }
1484       break;
1485     case HCI_SWITCH_ROLE:
1486       if (status != HCI_SUCCESS) {
1487         // Tell BTM that the command failed
1488         STREAM_TO_BDADDR(bd_addr, p_cmd);
1489         btm_acl_role_changed(status, &bd_addr, BTM_ROLE_UNDEFINED);
1490         l2c_link_role_changed(nullptr, BTM_ROLE_UNDEFINED,
1491                               HCI_ERR_COMMAND_DISALLOWED);
1492       }
1493       break;
1494     case HCI_CREATE_CONNECTION:
1495       if (status != HCI_SUCCESS) {
1496         STREAM_TO_BDADDR(bd_addr, p_cmd);
1497         btm_sec_connected(bd_addr, HCI_INVALID_HANDLE, status, 0);
1498         l2c_link_hci_conn_comp(status, HCI_INVALID_HANDLE, bd_addr);
1499       }
1500       break;
1501     case HCI_AUTHENTICATION_REQUESTED:
1502       if (status != HCI_SUCCESS) {
1503         // Device refused to start authentication
1504         // This is treated as an authentication failure
1505         btm_sec_auth_complete(BTM_INVALID_HCI_HANDLE, status);
1506       }
1507       break;
1508     case HCI_SET_CONN_ENCRYPTION:
1509       if (status != HCI_SUCCESS) {
1510         // Device refused to start encryption
1511         // This is treated as an encryption failure
1512         btm_sec_encrypt_change(BTM_INVALID_HCI_HANDLE, status, false);
1513       }
1514       break;
1515     case HCI_RMT_NAME_REQUEST:
1516       if (status != HCI_SUCCESS) {
1517         // Tell inquiry processing that we are done
1518         btm_process_remote_name(nullptr, nullptr, 0, status);
1519         btm_sec_rmt_name_request_complete(nullptr, nullptr, status);
1520       }
1521       break;
1522     case HCI_READ_RMT_EXT_FEATURES:
1523       if (status != HCI_SUCCESS) {
1524         STREAM_TO_UINT16(handle, p_cmd);
1525         btm_read_remote_ext_features_failed(status, handle);
1526       }
1527       break;
1528     case HCI_SETUP_ESCO_CONNECTION:
1529     case HCI_ENH_SETUP_ESCO_CONNECTION:
1530       if (status != HCI_SUCCESS) {
1531         STREAM_TO_UINT16(handle, p_cmd);
1532         // Determine if initial connection failed or is a change of setup
1533         if (btm_is_sco_active(handle)) {
1534           btm_esco_proc_conn_chg(status, handle, 0, 0, 0, 0);
1535         } else {
1536           btm_sco_connected(status, nullptr, handle, nullptr);
1537         }
1538       }
1539       break;
1540
1541     // BLE Commands
1542     case HCI_BLE_CREATE_LL_CONN:
1543     case HCI_LE_EXTENDED_CREATE_CONNECTION:
1544       if (status != HCI_SUCCESS) {
1545         btm_ble_create_ll_conn_complete(status);
1546       }
1547       break;
1548     case HCI_BLE_START_ENC:
1549       // Race condition: disconnection happened right before we send
1550       // "LE Encrypt", controller responds with no connection, we should
1551       // cancel the encryption attempt, rather than unpair the device.
1552       if (status == HCI_ERR_NO_CONNECTION) {
1553         smp_cancel_start_encryption_attempt();
1554       }
1555       break;
1556
1557     // Link Policy Commands
1558     case HCI_EXIT_SNIFF_MODE:
1559     case HCI_EXIT_PARK_MODE:
1560       if (status != HCI_SUCCESS) {
1561         // Allow SCO initiation to continue if waiting for change mode event
1562         STREAM_TO_UINT16(handle, p_cmd);
1563         btm_sco_chk_pend_unpark(status, handle);
1564       }
1565       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1566     case HCI_HOLD_MODE:
1567     case HCI_SNIFF_MODE:
1568     case HCI_PARK_MODE:
1569       btm_pm_proc_cmd_status(status);
1570       break;
1571
1572     default:
1573       if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC) {
1574         btm_vsc_complete(&status, opcode, 1,
1575                          (tBTM_VSC_CMPL_CB*)p_vsc_status_cback);
1576       }
1577   }
1578 }
1579
1580 /*******************************************************************************
1581  *
1582  * Function         btu_hcif_command_status_evt
1583  *
1584  * Description      Process event HCI_COMMAND_STATUS_EVT
1585  *
1586  * Returns          void
1587  *
1588  ******************************************************************************/
1589 static void btu_hcif_command_status_evt_on_task(uint8_t status, BT_HDR* event,
1590                                                 void* context) {
1591   command_opcode_t opcode;
1592   uint8_t* stream = event->data + event->offset;
1593   STREAM_TO_UINT16(opcode, stream);
1594
1595   // stream + 1 to skip parameter length field
1596   // No need to check length since stream is written by us
1597   btu_hcif_log_command_metrics(opcode, stream + 1, status, true);
1598
1599   btu_hcif_hdl_command_status(opcode, status, stream, context);
1600   osi_free(event);
1601 }
1602
1603 static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command,
1604                                         void* context) {
1605   do_in_main_thread(FROM_HERE, base::Bind(btu_hcif_command_status_evt_on_task,
1606                                           status, command, context));
1607 }
1608
1609 /*******************************************************************************
1610  *
1611  * Function         btu_hcif_hardware_error_evt
1612  *
1613  * Description      Process event HCI_HARDWARE_ERROR_EVT
1614  *
1615  * Returns          void
1616  *
1617  ******************************************************************************/
1618 static void btu_hcif_hardware_error_evt(uint8_t* p) {
1619   HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
1620
1621   /* If anyone wants device status notifications, give him one. */
1622   btm_report_device_status(BTM_DEV_STATUS_DOWN);
1623
1624   /* Reset the controller */
1625   if (BTM_IsDeviceUp()) BTM_DeviceReset(NULL);
1626 }
1627
1628 /*******************************************************************************
1629  *
1630  * Function         btu_hcif_flush_occured_evt
1631  *
1632  * Description      Process event HCI_FLUSH_OCCURED_EVT
1633  *
1634  * Returns          void
1635  *
1636  ******************************************************************************/
1637 static void btu_hcif_flush_occured_evt(void) {}
1638
1639 /*******************************************************************************
1640  *
1641  * Function         btu_hcif_role_change_evt
1642  *
1643  * Description      Process event HCI_ROLE_CHANGE_EVT
1644  *
1645  * Returns          void
1646  *
1647  ******************************************************************************/
1648 static void btu_hcif_role_change_evt(uint8_t* p) {
1649   uint8_t status;
1650   RawAddress bda;
1651   uint8_t role;
1652
1653   STREAM_TO_UINT8(status, p);
1654   STREAM_TO_BDADDR(bda, p);
1655   STREAM_TO_UINT8(role, p);
1656
1657   btm_blacklist_role_change_device(bda, status);
1658   l2c_link_role_changed(&bda, role, status);
1659   btm_acl_role_changed(status, &bda, role);
1660 }
1661
1662 /*******************************************************************************
1663  *
1664  * Function         btu_hcif_num_compl_data_pkts_evt
1665  *
1666  * Description      Process event HCI_NUM_COMPL_DATA_PKTS_EVT
1667  *
1668  * Returns          void
1669  *
1670  ******************************************************************************/
1671 static void btu_hcif_num_compl_data_pkts_evt(uint8_t* p) {
1672   /* Process for L2CAP and SCO */
1673   l2c_link_process_num_completed_pkts(p);
1674
1675   /* Send on to SCO */
1676   /*?? No SCO for now */
1677 }
1678
1679 /*******************************************************************************
1680  *
1681  * Function         btu_hcif_mode_change_evt
1682  *
1683  * Description      Process event HCI_MODE_CHANGE_EVT
1684  *
1685  * Returns          void
1686  *
1687  ******************************************************************************/
1688 static void btu_hcif_mode_change_evt(uint8_t* p) {
1689   uint8_t status;
1690   uint16_t handle;
1691   uint8_t current_mode;
1692   uint16_t interval;
1693
1694   STREAM_TO_UINT8(status, p);
1695
1696   STREAM_TO_UINT16(handle, p);
1697   STREAM_TO_UINT8(current_mode, p);
1698   STREAM_TO_UINT16(interval, p);
1699   btm_sco_chk_pend_unpark(status, handle);
1700   btm_pm_proc_mode_change(status, handle, current_mode, interval);
1701
1702 #if (HID_DEV_INCLUDED == TRUE && HID_DEV_PM_INCLUDED == TRUE)
1703   hidd_pm_proc_mode_change(status, current_mode, interval);
1704 #endif
1705 }
1706
1707 /*******************************************************************************
1708  *
1709  * Function         btu_hcif_ssr_evt
1710  *
1711  * Description      Process event HCI_SNIFF_SUB_RATE_EVT
1712  *
1713  * Returns          void
1714  *
1715  ******************************************************************************/
1716 #if (BTM_SSR_INCLUDED == TRUE)
1717 static void btu_hcif_ssr_evt(uint8_t* p, uint16_t evt_len) {
1718   btm_pm_proc_ssr_evt(p, evt_len);
1719 }
1720 #endif
1721
1722 /*******************************************************************************
1723  *
1724  * Function         btu_hcif_pin_code_request_evt
1725  *
1726  * Description      Process event HCI_PIN_CODE_REQUEST_EVT
1727  *
1728  * Returns          void
1729  *
1730  ******************************************************************************/
1731 static void btu_hcif_pin_code_request_evt(uint8_t* p) {
1732   RawAddress bda;
1733
1734   STREAM_TO_BDADDR(bda, p);
1735
1736   /* Tell L2CAP that there was a PIN code request,  */
1737   /* it may need to stretch timeouts                */
1738   l2c_pin_code_request(bda);
1739
1740   btm_sec_pin_code_request(bda);
1741 }
1742
1743 /*******************************************************************************
1744  *
1745  * Function         btu_hcif_link_key_request_evt
1746  *
1747  * Description      Process event HCI_LINK_KEY_REQUEST_EVT
1748  *
1749  * Returns          void
1750  *
1751  ******************************************************************************/
1752 static void btu_hcif_link_key_request_evt(uint8_t* p) {
1753   RawAddress bda;
1754
1755   STREAM_TO_BDADDR(bda, p);
1756   btm_sec_link_key_request(bda);
1757 }
1758
1759 /*******************************************************************************
1760  *
1761  * Function         btu_hcif_link_key_notification_evt
1762  *
1763  * Description      Process event HCI_LINK_KEY_NOTIFICATION_EVT
1764  *
1765  * Returns          void
1766  *
1767  ******************************************************************************/
1768 static void btu_hcif_link_key_notification_evt(uint8_t* p) {
1769   RawAddress bda;
1770   Octet16 key;
1771   uint8_t key_type;
1772
1773   STREAM_TO_BDADDR(bda, p);
1774   STREAM_TO_ARRAY16(key.data(), p);
1775   STREAM_TO_UINT8(key_type, p);
1776
1777   btm_sec_link_key_notification(bda, key, key_type);
1778 }
1779
1780 /*******************************************************************************
1781  *
1782  * Function         btu_hcif_loopback_command_evt
1783  *
1784  * Description      Process event HCI_LOOPBACK_COMMAND_EVT
1785  *
1786  * Returns          void
1787  *
1788  ******************************************************************************/
1789 static void btu_hcif_loopback_command_evt(void) {}
1790
1791 /*******************************************************************************
1792  *
1793  * Function         btu_hcif_data_buf_overflow_evt
1794  *
1795  * Description      Process event HCI_DATA_BUF_OVERFLOW_EVT
1796  *
1797  * Returns          void
1798  *
1799  ******************************************************************************/
1800 static void btu_hcif_data_buf_overflow_evt(void) {}
1801
1802 /*******************************************************************************
1803  *
1804  * Function         btu_hcif_max_slots_changed_evt
1805  *
1806  * Description      Process event HCI_MAX_SLOTS_CHANGED_EVT
1807  *
1808  * Returns          void
1809  *
1810  ******************************************************************************/
1811 static void btu_hcif_max_slots_changed_evt(void) {}
1812
1813 /*******************************************************************************
1814  *
1815  * Function         btu_hcif_read_clock_off_comp_evt
1816  *
1817  * Description      Process event HCI_READ_CLOCK_OFF_COMP_EVT
1818  *
1819  * Returns          void
1820  *
1821  ******************************************************************************/
1822 static void btu_hcif_read_clock_off_comp_evt(uint8_t* p) {
1823   uint8_t status;
1824   uint16_t handle;
1825   uint16_t clock_offset;
1826
1827   STREAM_TO_UINT8(status, p);
1828
1829   /* If failed to get clock offset just drop the result */
1830   if (status != HCI_SUCCESS) return;
1831
1832   STREAM_TO_UINT16(handle, p);
1833   STREAM_TO_UINT16(clock_offset, p);
1834
1835   handle = HCID_GET_HANDLE(handle);
1836
1837   btm_process_clk_off_comp_evt(handle, clock_offset);
1838   btm_sec_update_clock_offset(handle, clock_offset);
1839 }
1840
1841 /*******************************************************************************
1842  *
1843  * Function         btu_hcif_conn_pkt_type_change_evt
1844  *
1845  * Description      Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
1846  *
1847  * Returns          void
1848  *
1849  ******************************************************************************/
1850 static void btu_hcif_conn_pkt_type_change_evt(void) {}
1851
1852 /*******************************************************************************
1853  *
1854  * Function         btu_hcif_qos_violation_evt
1855  *
1856  * Description      Process event HCI_QOS_VIOLATION_EVT
1857  *
1858  * Returns          void
1859  *
1860  ******************************************************************************/
1861 static void btu_hcif_qos_violation_evt(uint8_t* p) {
1862   uint16_t handle;
1863
1864   STREAM_TO_UINT16(handle, p);
1865
1866   handle = HCID_GET_HANDLE(handle);
1867
1868   l2c_link_hci_qos_violation(handle);
1869 }
1870
1871 /*******************************************************************************
1872  *
1873  * Function         btu_hcif_page_scan_mode_change_evt
1874  *
1875  * Description      Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
1876  *
1877  * Returns          void
1878  *
1879  ******************************************************************************/
1880 static void btu_hcif_page_scan_mode_change_evt(void) {}
1881
1882 /*******************************************************************************
1883  *
1884  * Function         btu_hcif_page_scan_rep_mode_chng_evt
1885  *
1886  * Description      Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
1887  *
1888  * Returns          void
1889  *
1890  ******************************************************************************/
1891 static void btu_hcif_page_scan_rep_mode_chng_evt(void) {}
1892
1893 /**********************************************
1894  * Simple Pairing Events
1895  **********************************************/
1896
1897 /*******************************************************************************
1898  *
1899  * Function         btu_hcif_host_support_evt
1900  *
1901  * Description      Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
1902  *
1903  * Returns          void
1904  *
1905  ******************************************************************************/
1906 static void btu_hcif_host_support_evt(uint8_t* p) {
1907   btm_sec_rmt_host_support_feat_evt(p);
1908 }
1909
1910 /*******************************************************************************
1911  *
1912  * Function         btu_hcif_io_cap_request_evt
1913  *
1914  * Description      Process event HCI_IO_CAPABILITY_REQUEST_EVT
1915  *
1916  * Returns          void
1917  *
1918  ******************************************************************************/
1919 static void btu_hcif_io_cap_request_evt(uint8_t* p) {
1920   RawAddress bda;
1921   STREAM_TO_BDADDR(bda, p);
1922   btm_io_capabilities_req(bda);
1923 }
1924
1925 /*******************************************************************************
1926  *
1927  * Function         btu_hcif_io_cap_response_evt
1928  *
1929  * Description      Process event HCI_IO_CAPABILITY_RESPONSE_EVT
1930  *
1931  * Returns          void
1932  *
1933  ******************************************************************************/
1934 static void btu_hcif_io_cap_response_evt(uint8_t* p) {
1935   btm_io_capabilities_rsp(p);
1936 }
1937
1938 /*******************************************************************************
1939  *
1940  * Function         btu_hcif_user_conf_request_evt
1941  *
1942  * Description      Process event HCI_USER_CONFIRMATION_REQUEST_EVT
1943  *
1944  * Returns          void
1945  *
1946  ******************************************************************************/
1947 static void btu_hcif_user_conf_request_evt(uint8_t* p) {
1948   btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
1949 }
1950
1951 /*******************************************************************************
1952  *
1953  * Function         btu_hcif_user_passkey_request_evt
1954  *
1955  * Description      Process event HCI_USER_PASSKEY_REQUEST_EVT
1956  *
1957  * Returns          void
1958  *
1959  ******************************************************************************/
1960 static void btu_hcif_user_passkey_request_evt(uint8_t* p) {
1961   btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
1962 }
1963
1964 /*******************************************************************************
1965  *
1966  * Function         btu_hcif_user_passkey_notif_evt
1967  *
1968  * Description      Process event HCI_USER_PASSKEY_NOTIFY_EVT
1969  *
1970  * Returns          void
1971  *
1972  ******************************************************************************/
1973 static void btu_hcif_user_passkey_notif_evt(uint8_t* p) {
1974   btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
1975 }
1976
1977 /*******************************************************************************
1978  *
1979  * Function         btu_hcif_keypress_notif_evt
1980  *
1981  * Description      Process event HCI_KEYPRESS_NOTIFY_EVT
1982  *
1983  * Returns          void
1984  *
1985  ******************************************************************************/
1986 static void btu_hcif_keypress_notif_evt(uint8_t* p) {
1987   btm_keypress_notif_evt(p);
1988 }
1989
1990 /*******************************************************************************
1991  *
1992  * Function         btu_hcif_rem_oob_request_evt
1993  *
1994  * Description      Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
1995  *
1996  * Returns          void
1997  *
1998  ******************************************************************************/
1999 static void btu_hcif_rem_oob_request_evt(uint8_t* p) { btm_rem_oob_req(p); }
2000
2001 /*******************************************************************************
2002  *
2003  * Function         btu_hcif_simple_pair_complete_evt
2004  *
2005  * Description      Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
2006  *
2007  * Returns          void
2008  *
2009  ******************************************************************************/
2010 static void btu_hcif_simple_pair_complete_evt(uint8_t* p) {
2011   btm_simple_pair_complete(p);
2012 }
2013
2014 /*******************************************************************************
2015  *
2016  * Function         btu_hcif_enhanced_flush_complete_evt
2017  *
2018  * Description      Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
2019  *
2020  * Returns          void
2021  *
2022  ******************************************************************************/
2023 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
2024 static void btu_hcif_enhanced_flush_complete_evt(void) {
2025   /* This is empty until an upper layer cares about returning event */
2026 }
2027 #endif
2028 /**********************************************
2029  * End of Simple Pairing Events
2030  **********************************************/
2031
2032 /**********************************************
2033  * BLE Events
2034  **********************************************/
2035 static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p) {
2036   uint8_t status;
2037   uint8_t enc_enable = 0;
2038   uint16_t handle;
2039
2040   STREAM_TO_UINT8(status, p);
2041   STREAM_TO_UINT16(handle, p);
2042
2043   if (status == HCI_SUCCESS) enc_enable = 1;
2044
2045   btm_sec_encrypt_change(handle, status, enc_enable);
2046 }
2047
2048 static void btu_ble_ll_conn_complete_evt(uint8_t* p, uint16_t evt_len) {
2049   btm_ble_conn_complete(p, evt_len, false);
2050 }
2051 #if (BLE_PRIVACY_SPT == TRUE)
2052 static void btu_ble_proc_enhanced_conn_cmpl(uint8_t* p, uint16_t evt_len) {
2053   btm_ble_conn_complete(p, evt_len, true);
2054 }
2055 #endif
2056
2057 extern void gatt_notify_conn_update(uint16_t handle, uint16_t interval,
2058                                     uint16_t latency, uint16_t timeout,
2059                                     uint8_t status);
2060
2061 static void btu_ble_ll_conn_param_upd_evt(uint8_t* p, uint16_t evt_len) {
2062   /* LE connection update has completed successfully as a master. */
2063   /* We can enable the update request if the result is a success. */
2064   /* extract the HCI handle first */
2065   uint8_t status;
2066   uint16_t handle;
2067   uint16_t interval;
2068   uint16_t latency;
2069   uint16_t timeout;
2070
2071   STREAM_TO_UINT8(status, p);
2072   STREAM_TO_UINT16(handle, p);
2073   STREAM_TO_UINT16(interval, p);
2074   STREAM_TO_UINT16(latency, p);
2075   STREAM_TO_UINT16(timeout, p);
2076
2077   l2cble_process_conn_update_evt(handle, status, interval, latency, timeout);
2078
2079   gatt_notify_conn_update(handle & 0x0FFF, interval, latency, timeout, status);
2080 }
2081
2082 static void btu_ble_read_remote_feat_evt(uint8_t* p) {
2083   btm_ble_read_remote_features_complete(p);
2084 }
2085
2086 static void btu_ble_proc_ltk_req(uint8_t* p) {
2087   uint16_t ediv, handle;
2088   uint8_t* pp;
2089
2090   STREAM_TO_UINT16(handle, p);
2091   pp = p + 8;
2092   STREAM_TO_UINT16(ediv, pp);
2093   btm_ble_ltk_request(handle, p, ediv);
2094   /* This is empty until an upper layer cares about returning event */
2095 }
2096
2097 static void btu_ble_data_length_change_evt(uint8_t* p, uint16_t evt_len) {
2098   uint16_t handle;
2099   uint16_t tx_data_len;
2100   uint16_t rx_data_len;
2101
2102   if (!controller_get_interface()->supports_ble_packet_extension()) {
2103     HCI_TRACE_WARNING("%s, request not supported", __func__);
2104     return;
2105   }
2106
2107   STREAM_TO_UINT16(handle, p);
2108   STREAM_TO_UINT16(tx_data_len, p);
2109   p += 2; /* Skip the TxTimer */
2110   STREAM_TO_UINT16(rx_data_len, p);
2111
2112   l2cble_process_data_length_change_event(handle, tx_data_len, rx_data_len);
2113 }
2114
2115 /**********************************************
2116  * End of BLE Events Handler
2117  **********************************************/
2118 #if (BLE_LLT_INCLUDED == TRUE)
2119 static void btu_ble_rc_param_req_evt(uint8_t* p) {
2120   uint16_t handle;
2121   uint16_t int_min, int_max, latency, timeout;
2122
2123   STREAM_TO_UINT16(handle, p);
2124   STREAM_TO_UINT16(int_min, p);
2125   STREAM_TO_UINT16(int_max, p);
2126   STREAM_TO_UINT16(latency, p);
2127   STREAM_TO_UINT16(timeout, p);
2128
2129   l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency,
2130                                       timeout);
2131 }
2132 #endif /* BLE_LLT_INCLUDED */