• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "stack/include/btu_hcif.h"
31 
32 #include <base/functional/bind.h>
33 #include <base/location.h>
34 #include <bluetooth/log.h>
35 #include <com_android_bluetooth_flags.h>
36 
37 #include <cstdint>
38 
39 #include "btm_iso_api.h"
40 #include "internal_include/bt_target.h"
41 #include "main/shim/hci_layer.h"
42 #include "main/shim/metrics_api.h"
43 #include "os/metrics.h"
44 #include "osi/include/allocator.h"
45 #include "stack/include/acl_api.h"
46 #include "stack/include/acl_hci_link_interface.h"
47 #include "stack/include/ble_hci_link_interface.h"
48 #include "stack/include/bt_hdr.h"
49 #include "stack/include/bt_types.h"
50 #include "stack/include/btm_ble_addr.h"
51 #include "stack/include/btm_iso_api.h"
52 #include "stack/include/btm_sec_api_types.h"
53 #include "stack/include/btm_status.h"
54 #include "stack/include/btu_hcif.h"
55 #include "stack/include/dev_hci_link_interface.h"
56 #include "stack/include/hci_error_code.h"
57 #include "stack/include/hci_evt_length.h"
58 #include "stack/include/inq_hci_link_interface.h"
59 #include "stack/include/main_thread.h"
60 #include "stack/include/sco_hci_link_interface.h"
61 #include "stack/include/sec_hci_link_interface.h"
62 #include "stack/include/smp_api.h"
63 #include "types/hci_role.h"
64 #include "types/raw_address.h"
65 
66 using namespace bluetooth;
67 using bluetooth::hci::IsoManager;
68 
69 /******************************************************************************/
70 /*            L O C A L    F U N C T I O N     P R O T O T Y P E S            */
71 /******************************************************************************/
72 static void btu_hcif_authentication_comp_evt(uint8_t* p);
73 static void btu_hcif_encryption_change_evt(uint8_t* p);
74 static void btu_hcif_encryption_change_evt_v2(uint8_t* p);
75 static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p, uint8_t evt_len);
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, void* context);
78 static void btu_hcif_mode_change_evt(uint8_t* p);
79 static void btu_hcif_link_key_notification_evt(const uint8_t* p);
80 static void btu_hcif_read_clock_off_comp_evt(uint8_t* p);
81 static void btu_hcif_esco_connection_comp_evt(const uint8_t* p);
82 static void btu_hcif_esco_connection_chg_evt(uint8_t* p);
83 
84 /* Parsing functions for btm functions */
85 
86 static void btu_hcif_sec_pin_code_request(const uint8_t* p);
87 static void btu_hcif_sec_link_key_request(const uint8_t* p);
88 static void btu_hcif_sec_rmt_host_support_feat_evt(const uint8_t* p);
89 static void btu_hcif_proc_sp_req_evt(tBTM_SP_EVT event, const uint8_t* p);
90 static void btu_hcif_rem_oob_req(const uint8_t* p);
91 static void btu_hcif_simple_pair_complete(const uint8_t* p);
92 static void btu_hcif_proc_sp_req_evt(const tBTM_SP_EVT event, const uint8_t* p);
93 static void btu_hcif_create_conn_cancel_complete(const uint8_t* p, uint16_t evt_len);
94 static void btu_hcif_read_local_oob_complete(const uint8_t* p, uint16_t evt_len);
95 static void btu_hcif_read_local_oob_extended_complete(const uint8_t* p, uint16_t evt_len);
96 
97 /* Simple Pairing Events */
98 static void btu_hcif_io_cap_request_evt(const uint8_t* p);
99 static void btu_hcif_io_cap_response_evt(const uint8_t* p);
100 
101 static void btu_ble_proc_ltk_req(uint8_t* p, uint16_t evt_len);
102 static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p);
103 
104 /**
105  * Log HCI event metrics that are not handled in special functions
106  * @param evt_code event code
107  * @param p_event pointer to event parameter, skipping paremter length
108  */
btu_hcif_log_event_metrics(uint8_t evt_code,const uint8_t * p_event)109 static void btu_hcif_log_event_metrics(uint8_t evt_code, const uint8_t* p_event) {
110   uint32_t cmd = android::bluetooth::hci::CMD_UNKNOWN;
111   uint16_t status = android::bluetooth::hci::STATUS_UNKNOWN;
112   uint16_t reason = android::bluetooth::hci::STATUS_UNKNOWN;
113   uint16_t handle = bluetooth::os::kUnknownConnectionHandle;
114   int64_t value = 0;
115 
116   RawAddress bda = RawAddress::kEmpty;
117   switch (evt_code) {
118     case HCI_IO_CAPABILITY_REQUEST_EVT:
119     case HCI_IO_CAPABILITY_RESPONSE_EVT:
120     case HCI_LINK_KEY_REQUEST_EVT:
121     case HCI_LINK_KEY_NOTIFICATION_EVT:
122     case HCI_USER_PASSKEY_REQUEST_EVT:
123     case HCI_USER_PASSKEY_NOTIFY_EVT:
124     case HCI_USER_CONFIRMATION_REQUEST_EVT:
125     case HCI_KEYPRESS_NOTIFY_EVT:
126     case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
127       STREAM_TO_BDADDR(bda, p_event);
128       bluetooth::shim::LogMetricClassicPairingEvent(bda, handle, cmd, evt_code, status, reason,
129                                                     value);
130       break;
131     case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
132       STREAM_TO_UINT8(status, p_event);
133       STREAM_TO_BDADDR(bda, p_event);
134       bluetooth::shim::LogMetricClassicPairingEvent(bda, handle, cmd, evt_code, status, reason,
135                                                     value);
136       break;
137     case HCI_AUTHENTICATION_COMP_EVT:
138       STREAM_TO_UINT8(status, p_event);
139       STREAM_TO_UINT16(handle, p_event);
140       handle = HCID_GET_HANDLE(handle);
141       bluetooth::shim::LogMetricClassicPairingEvent(bda, handle, cmd, evt_code, status, reason,
142                                                     value);
143       break;
144     case HCI_ENCRYPTION_CHANGE_EVT: {
145       uint8_t encryption_enabled;
146       STREAM_TO_UINT8(status, p_event);
147       STREAM_TO_UINT16(handle, p_event);
148       STREAM_TO_UINT8(encryption_enabled, p_event);
149       bluetooth::shim::LogMetricClassicPairingEvent(bda, handle, cmd, evt_code, status, reason,
150                                                     encryption_enabled);
151       break;
152     }
153     case HCI_ENCRYPTION_CHANGE_EVT_V2: {
154       uint8_t encryption_enabled;
155       uint8_t key_size;
156       STREAM_TO_UINT8(status, p_event);
157       STREAM_TO_UINT16(handle, p_event);
158       STREAM_TO_UINT8(encryption_enabled, p_event);
159       STREAM_TO_UINT8(key_size, p_event);
160       bluetooth::shim::LogMetricClassicPairingEvent(bda, handle, cmd, evt_code, status, reason,
161                                                     encryption_enabled);
162       break;
163     }
164     case HCI_ESCO_CONNECTION_COMP_EVT: {
165       uint8_t link_type;
166       STREAM_TO_UINT8(status, p_event);
167       STREAM_TO_UINT16(handle, p_event);
168       STREAM_TO_BDADDR(bda, p_event);
169       STREAM_TO_UINT8(link_type, p_event);
170       handle = HCID_GET_HANDLE(handle);
171       bluetooth::shim::LogMetricLinkLayerConnectionEvent(
172               &bda, handle, android::bluetooth::DIRECTION_UNKNOWN, link_type, cmd, evt_code,
173               android::bluetooth::hci::BLE_EVT_UNKNOWN, status, reason);
174       break;
175     }
176     case HCI_ESCO_CONNECTION_CHANGED_EVT: {
177       STREAM_TO_UINT8(status, p_event);
178       STREAM_TO_UINT16(handle, p_event);
179       handle = HCID_GET_HANDLE(handle);
180       bluetooth::shim::LogMetricLinkLayerConnectionEvent(
181               nullptr, handle, android::bluetooth::DIRECTION_UNKNOWN,
182               android::bluetooth::LINK_TYPE_UNKNOWN, cmd, evt_code,
183               android::bluetooth::hci::BLE_EVT_UNKNOWN, status, reason);
184       break;
185     }
186     // Ignore these events
187     case HCI_BLE_EVENT:
188       break;
189     case HCI_VENDOR_SPECIFIC_EVT:
190       break;
191 
192     case HCI_CONNECTION_COMP_EVT:     // EventCode::CONNECTION_COMPLETE
193     case HCI_CONNECTION_REQUEST_EVT:  // EventCode::CONNECTION_REQUEST
194     case HCI_DISCONNECTION_COMP_EVT:  // EventCode::DISCONNECTION_COMPLETE
195     case HCI_RMT_NAME_REQUEST_COMP_EVT:  // EventCode::REMOTE_NAME_REQUEST_COMPLETE
196     default:
197       log::error(
198               "Unexpectedly received event_code:0x{:02x} that should not be "
199               "handled here",
200               evt_code);
201       break;
202   }
203 }
204 
205 /*******************************************************************************
206  *
207  * Function         btu_hcif_process_event
208  *
209  * Description      This function is called when an event is received from
210  *                  the Host Controller.
211  *
212  * Returns          void
213  *
214  ******************************************************************************/
btu_hcif_process_event(uint8_t,const BT_HDR * p_msg)215 static void btu_hcif_process_event(uint8_t /* controller_id */, const BT_HDR* p_msg) {
216   uint8_t* p = (uint8_t*)(p_msg + 1) + p_msg->offset;
217   uint8_t hci_evt_code, hci_evt_len;
218   uint8_t ble_sub_code;
219   STREAM_TO_UINT8(hci_evt_code, p);
220   STREAM_TO_UINT8(hci_evt_len, p);
221 
222   // validate event size
223   if (hci_evt_len < hci_event_parameters_minimum_length[hci_evt_code]) {
224     log::warn("evt:0x{:2X}, malformed event of size {}", hci_evt_code, hci_evt_len);
225     return;
226   }
227 
228   btu_hcif_log_event_metrics(hci_evt_code, p);
229 
230   switch (hci_evt_code) {
231     case HCI_AUTHENTICATION_COMP_EVT:
232       btu_hcif_authentication_comp_evt(p);
233       break;
234     case HCI_ENCRYPTION_CHANGE_EVT:
235       btu_hcif_encryption_change_evt(p);
236       break;
237     case HCI_ENCRYPTION_CHANGE_EVT_V2:
238       btu_hcif_encryption_change_evt_v2(p);
239       break;
240     case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
241       btu_hcif_encryption_key_refresh_cmpl_evt(p);
242       break;
243     case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
244       btu_hcif_read_rmt_ext_features_comp_evt(p, hci_evt_len);
245       break;
246     case HCI_COMMAND_COMPLETE_EVT:
247       log::error(
248               "should not have received a command complete event. Someone didn't "
249               "go through the hci transmit_command function.");
250       break;
251     case HCI_COMMAND_STATUS_EVT:
252       log::error(
253               "should not have received a command status event. Someone didn't go "
254               "through the hci transmit_command function.");
255       break;
256     case HCI_MODE_CHANGE_EVT:
257       btu_hcif_mode_change_evt(p);
258       break;
259     case HCI_PIN_CODE_REQUEST_EVT:
260       btu_hcif_sec_pin_code_request(p);
261       break;
262     case HCI_LINK_KEY_REQUEST_EVT:
263       btu_hcif_sec_link_key_request(p);
264       break;
265     case HCI_LINK_KEY_NOTIFICATION_EVT:
266       btu_hcif_link_key_notification_evt(p);
267       break;
268     case HCI_READ_CLOCK_OFF_COMP_EVT:
269       btu_hcif_read_clock_off_comp_evt(p);
270       break;
271     case HCI_ESCO_CONNECTION_COMP_EVT:
272       btu_hcif_esco_connection_comp_evt(p);
273       break;
274     case HCI_ESCO_CONNECTION_CHANGED_EVT:
275       btu_hcif_esco_connection_chg_evt(p);
276       break;
277     case HCI_SNIFF_SUB_RATE_EVT:
278       btm_pm_proc_ssr_evt(p, hci_evt_len);
279       break;
280     case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
281       btu_hcif_sec_rmt_host_support_feat_evt(p);
282       break;
283     case HCI_IO_CAPABILITY_REQUEST_EVT:
284       btu_hcif_io_cap_request_evt(p);
285       break;
286     case HCI_IO_CAPABILITY_RESPONSE_EVT:
287       btu_hcif_io_cap_response_evt(p);
288       break;
289     case HCI_USER_CONFIRMATION_REQUEST_EVT:
290       btu_hcif_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
291       break;
292     case HCI_USER_PASSKEY_REQUEST_EVT:
293       btu_hcif_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
294       break;
295     case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
296       btu_hcif_rem_oob_req(p);
297       break;
298     case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
299       btu_hcif_simple_pair_complete(p);
300       break;
301     case HCI_USER_PASSKEY_NOTIFY_EVT:
302       btu_hcif_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
303       break;
304 
305     case HCI_BLE_EVENT: {
306       STREAM_TO_UINT8(ble_sub_code, p);
307 
308       uint8_t ble_evt_len = hci_evt_len - 1;
309       switch (ble_sub_code) {
310         case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
311           btm_ble_read_remote_features_complete(p, ble_evt_len);
312           break;
313         case HCI_BLE_LTK_REQ_EVT: /* received only at peripheral device */
314           btu_ble_proc_ltk_req(p, ble_evt_len);
315           break;
316 
317         case HCI_BLE_REQ_PEER_SCA_CPL_EVT:
318           btm_acl_process_sca_cmpl_pkt(ble_evt_len, p);
319           break;
320 
321         case HCI_BLE_CIS_EST_EVT:
322         case HCI_BLE_CREATE_BIG_CPL_EVT:
323         case HCI_BLE_TERM_BIG_CPL_EVT:
324         case HCI_BLE_CIS_REQ_EVT:
325         case HCI_BLE_BIG_SYNC_EST_EVT:
326         case HCI_BLE_BIG_SYNC_LOST_EVT:
327           IsoManager::GetInstance()->HandleHciEvent(ble_sub_code, p, ble_evt_len);
328           break;
329 
330         default:
331           log::error(
332                   "Unexpectedly received LE sub_event_code:0x{:02x} that should "
333                   "not be handled here",
334                   ble_sub_code);
335           break;
336       }
337     } break;
338 
339       // Events now captured by gd::hci_layer module
340     case HCI_VENDOR_SPECIFIC_EVT:
341     case HCI_HARDWARE_ERROR_EVT:
342     case HCI_NUM_COMPL_DATA_PKTS_EVT:     // EventCode::NUMBER_OF_COMPLETED_PACKETS
343     case HCI_CONNECTION_COMP_EVT:         // EventCode::CONNECTION_COMPLETE
344     case HCI_CONNECTION_REQUEST_EVT:      // EventCode::CONNECTION_REQUEST
345     case HCI_READ_RMT_FEATURES_COMP_EVT:  // EventCode::READ_REMOTE_SUPPORTED_FEATURES_COMPLETE
346     case HCI_READ_RMT_VERSION_COMP_EVT:   // EventCode::READ_REMOTE_VERSION_INFORMATION_COMPLETE
347     case HCI_ROLE_CHANGE_EVT:             // EventCode::ROLE_CHANGE
348     case HCI_DISCONNECTION_COMP_EVT:      // EventCode::DISCONNECTION_COMPLETE
349     case HCI_RMT_NAME_REQUEST_COMP_EVT:   // EventCode::REMOTE_NAME_REQUEST_COMPLETE
350     default:
351       log::error(
352               "Unexpectedly received event_code:0x{:02x} that should not be "
353               "handled here",
354               hci_evt_code);
355       break;
356   }
357 }
358 
btu_hcif_log_command_metrics(uint16_t opcode,const uint8_t * p_cmd,uint16_t cmd_status,bool is_cmd_status)359 static void btu_hcif_log_command_metrics(uint16_t opcode, const uint8_t* p_cmd, uint16_t cmd_status,
360                                          bool is_cmd_status) {
361   static uint16_t kUnknownBleEvt = android::bluetooth::hci::BLE_EVT_UNKNOWN;
362 
363   uint16_t hci_event = android::bluetooth::hci::EVT_COMMAND_STATUS;
364   if (!is_cmd_status) {
365     hci_event = android::bluetooth::hci::EVT_UNKNOWN;
366     cmd_status = android::bluetooth::hci::STATUS_UNKNOWN;
367   }
368 
369   RawAddress bd_addr;
370   uint16_t handle;
371   uint8_t reason;
372 
373   switch (opcode) {
374     case HCI_CREATE_CONNECTION:
375     case HCI_CREATE_CONNECTION_CANCEL:
376       STREAM_TO_BDADDR(bd_addr, p_cmd);
377       bluetooth::shim::LogMetricLinkLayerConnectionEvent(
378               &bd_addr, bluetooth::os::kUnknownConnectionHandle,
379               android::bluetooth::DIRECTION_OUTGOING, android::bluetooth::LINK_TYPE_ACL, opcode,
380               hci_event, kUnknownBleEvt, cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
381       break;
382     case HCI_DISCONNECT:
383       STREAM_TO_UINT16(handle, p_cmd);
384       STREAM_TO_UINT8(reason, p_cmd);
385       bluetooth::shim::LogMetricLinkLayerConnectionEvent(
386               nullptr, handle, android::bluetooth::DIRECTION_UNKNOWN,
387               android::bluetooth::LINK_TYPE_UNKNOWN, opcode, hci_event, kUnknownBleEvt, cmd_status,
388               reason);
389       break;
390     case HCI_SETUP_ESCO_CONNECTION:
391     case HCI_ENH_SETUP_ESCO_CONNECTION:
392       STREAM_TO_UINT16(handle, p_cmd);
393       bluetooth::shim::LogMetricLinkLayerConnectionEvent(
394               nullptr, handle, android::bluetooth::DIRECTION_OUTGOING,
395               android::bluetooth::LINK_TYPE_UNKNOWN, opcode, hci_event, kUnknownBleEvt, cmd_status,
396               android::bluetooth::hci::STATUS_UNKNOWN);
397       break;
398     case HCI_ACCEPT_CONNECTION_REQUEST:
399     case HCI_ACCEPT_ESCO_CONNECTION:
400     case HCI_ENH_ACCEPT_ESCO_CONNECTION:
401       STREAM_TO_BDADDR(bd_addr, p_cmd);
402       bluetooth::shim::LogMetricLinkLayerConnectionEvent(
403               &bd_addr, bluetooth::os::kUnknownConnectionHandle,
404               android::bluetooth::DIRECTION_INCOMING, android::bluetooth::LINK_TYPE_UNKNOWN, opcode,
405               hci_event, kUnknownBleEvt, cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
406       break;
407     case HCI_REJECT_CONNECTION_REQUEST:
408     case HCI_REJECT_ESCO_CONNECTION:
409       STREAM_TO_BDADDR(bd_addr, p_cmd);
410       STREAM_TO_UINT8(reason, p_cmd);
411       bluetooth::shim::LogMetricLinkLayerConnectionEvent(
412               &bd_addr, bluetooth::os::kUnknownConnectionHandle,
413               android::bluetooth::DIRECTION_INCOMING, android::bluetooth::LINK_TYPE_UNKNOWN, opcode,
414               hci_event, kUnknownBleEvt, cmd_status, reason);
415       break;
416 
417       // BLE Commands
418     case HCI_BLE_CREATE_LL_CONN: {
419       p_cmd += 2;  // Skip LE_Scan_Interval
420       p_cmd += 2;  // Skip LE_Scan_Window;
421       uint8_t initiator_filter_policy;
422       STREAM_TO_UINT8(initiator_filter_policy, p_cmd);
423       uint8_t peer_address_type;
424       STREAM_TO_UINT8(peer_address_type, p_cmd);
425       STREAM_TO_BDADDR(bd_addr, p_cmd);
426       // Peer address should not be used if initiator filter policy is not 0x00
427       const RawAddress* bd_addr_p = nullptr;
428       if (initiator_filter_policy == 0x00) {
429         bd_addr_p = &bd_addr;
430         if (peer_address_type == BLE_ADDR_PUBLIC_ID || peer_address_type == BLE_ADDR_RANDOM_ID) {
431           // if identity address is not matched, this address is invalid
432           if (!btm_identity_addr_to_random_pseudo(&bd_addr, &peer_address_type, false)) {
433             bd_addr_p = nullptr;
434           }
435         }
436       }
437       if (initiator_filter_policy == 0x00 || (cmd_status != HCI_SUCCESS && !is_cmd_status)) {
438         // Selectively log to avoid log spam due to acceptlist connections:
439         // - When doing non-acceptlist connection
440         // - When there is an error in command status
441         bluetooth::shim::LogMetricLinkLayerConnectionEvent(
442                 bd_addr_p, bluetooth::os::kUnknownConnectionHandle,
443                 android::bluetooth::DIRECTION_OUTGOING, android::bluetooth::LINK_TYPE_ACL, opcode,
444                 hci_event, kUnknownBleEvt, cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
445       }
446       break;
447     }
448     case HCI_LE_EXTENDED_CREATE_CONNECTION: {
449       uint8_t initiator_filter_policy;
450       STREAM_TO_UINT8(initiator_filter_policy, p_cmd);
451       p_cmd += 1;  // Skip Own_Address_Type
452       uint8_t peer_addr_type;
453       STREAM_TO_UINT8(peer_addr_type, p_cmd);
454       STREAM_TO_BDADDR(bd_addr, p_cmd);
455       // Peer address should not be used if initiator filter policy is not 0x00
456       const RawAddress* bd_addr_p = nullptr;
457       if (initiator_filter_policy == 0x00) {
458         bd_addr_p = &bd_addr;
459         // if identity address is not matched, this should be a static address
460         btm_identity_addr_to_random_pseudo(&bd_addr, &peer_addr_type, false);
461       }
462       if (initiator_filter_policy == 0x00 || (cmd_status != HCI_SUCCESS && !is_cmd_status)) {
463         // Selectively log to avoid log spam due to acceptlist connections:
464         // - When doing non-acceptlist connection
465         // - When there is an error in command status
466         bluetooth::shim::LogMetricLinkLayerConnectionEvent(
467                 bd_addr_p, bluetooth::os::kUnknownConnectionHandle,
468                 android::bluetooth::DIRECTION_OUTGOING, android::bluetooth::LINK_TYPE_ACL, opcode,
469                 hci_event, kUnknownBleEvt, cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
470       }
471       break;
472     }
473     case HCI_BLE_CREATE_CONN_CANCEL:
474       if (cmd_status != HCI_SUCCESS && !is_cmd_status) {
475         // Only log errors to prevent log spam due to acceptlist connections
476         bluetooth::shim::LogMetricLinkLayerConnectionEvent(
477                 nullptr, bluetooth::os::kUnknownConnectionHandle,
478                 android::bluetooth::DIRECTION_OUTGOING, android::bluetooth::LINK_TYPE_ACL, opcode,
479                 hci_event, kUnknownBleEvt, cmd_status, android::bluetooth::hci::STATUS_UNKNOWN);
480       }
481       break;
482     case HCI_READ_LOCAL_OOB_DATA:
483     case HCI_READ_LOCAL_OOB_EXTENDED_DATA:
484       bluetooth::shim::LogMetricClassicPairingEvent(
485               RawAddress::kEmpty, bluetooth::os::kUnknownConnectionHandle, opcode, hci_event,
486               cmd_status, android::bluetooth::hci::STATUS_UNKNOWN, 0);
487       break;
488     case HCI_WRITE_SIMPLE_PAIRING_MODE: {
489       uint8_t simple_pairing_mode;
490       STREAM_TO_UINT8(simple_pairing_mode, p_cmd);
491       bluetooth::shim::LogMetricClassicPairingEvent(
492               RawAddress::kEmpty, bluetooth::os::kUnknownConnectionHandle, opcode, hci_event,
493               cmd_status, android::bluetooth::hci::STATUS_UNKNOWN, simple_pairing_mode);
494       break;
495     }
496     case HCI_WRITE_SECURE_CONNS_SUPPORT: {
497       uint8_t secure_conn_host_support;
498       STREAM_TO_UINT8(secure_conn_host_support, p_cmd);
499       bluetooth::shim::LogMetricClassicPairingEvent(
500               RawAddress::kEmpty, bluetooth::os::kUnknownConnectionHandle, opcode, hci_event,
501               cmd_status, android::bluetooth::hci::STATUS_UNKNOWN, secure_conn_host_support);
502       break;
503     }
504     case HCI_AUTHENTICATION_REQUESTED:
505       STREAM_TO_UINT16(handle, p_cmd);
506       bluetooth::shim::LogMetricClassicPairingEvent(RawAddress::kEmpty, handle, opcode, hci_event,
507                                                     cmd_status,
508                                                     android::bluetooth::hci::STATUS_UNKNOWN, 0);
509       break;
510     case HCI_SET_CONN_ENCRYPTION: {
511       STREAM_TO_UINT16(handle, p_cmd);
512       uint8_t encryption_enable;
513       STREAM_TO_UINT8(encryption_enable, p_cmd);
514       bluetooth::shim::LogMetricClassicPairingEvent(
515               RawAddress::kEmpty, handle, opcode, hci_event, cmd_status,
516               android::bluetooth::hci::STATUS_UNKNOWN, encryption_enable);
517       break;
518     }
519     case HCI_DELETE_STORED_LINK_KEY: {
520       uint8_t delete_all_flag;
521       STREAM_TO_BDADDR(bd_addr, p_cmd);
522       STREAM_TO_UINT8(delete_all_flag, p_cmd);
523       bluetooth::shim::LogMetricClassicPairingEvent(
524               bd_addr, bluetooth::os::kUnknownConnectionHandle, opcode, hci_event, cmd_status,
525               android::bluetooth::hci::STATUS_UNKNOWN, delete_all_flag);
526       break;
527     }
528     case HCI_RMT_NAME_REQUEST:
529     case HCI_RMT_NAME_REQUEST_CANCEL:
530     case HCI_LINK_KEY_REQUEST_REPLY:
531     case HCI_LINK_KEY_REQUEST_NEG_REPLY:
532     case HCI_IO_CAPABILITY_REQUEST_REPLY:
533     case HCI_USER_CONF_REQUEST_REPLY:
534     case HCI_USER_CONF_VALUE_NEG_REPLY:
535     case HCI_USER_PASSKEY_REQ_REPLY:
536     case HCI_USER_PASSKEY_REQ_NEG_REPLY:
537     case HCI_REM_OOB_DATA_REQ_REPLY:
538     case HCI_REM_OOB_DATA_REQ_NEG_REPLY:
539       STREAM_TO_BDADDR(bd_addr, p_cmd);
540       bluetooth::shim::LogMetricClassicPairingEvent(
541               bd_addr, bluetooth::os::kUnknownConnectionHandle, opcode, hci_event, cmd_status,
542               android::bluetooth::hci::STATUS_UNKNOWN, 0);
543       break;
544     case HCI_IO_CAP_REQ_NEG_REPLY:
545       STREAM_TO_BDADDR(bd_addr, p_cmd);
546       STREAM_TO_UINT8(reason, p_cmd);
547       bluetooth::shim::LogMetricClassicPairingEvent(bd_addr,
548                                                     bluetooth::os::kUnknownConnectionHandle, opcode,
549                                                     hci_event, cmd_status, reason, 0);
550       break;
551   }
552 }
553 
554 /*******************************************************************************
555  *
556  * Function         btu_hcif_send_cmd
557  *
558  * Description      This function is called to send commands to the Host
559  *                  Controller.
560  *
561  * Returns          void
562  *
563  ******************************************************************************/
btu_hcif_send_cmd(uint8_t,const BT_HDR * p_buf)564 void btu_hcif_send_cmd(uint8_t /* controller_id */, const BT_HDR* p_buf) {
565   if (!p_buf) {
566     return;
567   }
568 
569   uint16_t opcode;
570   const uint8_t* stream = p_buf->data + p_buf->offset;
571 
572   STREAM_TO_UINT16(opcode, stream);
573 
574   // Skip parameter length before logging
575   stream++;
576   btu_hcif_log_command_metrics(opcode, stream, android::bluetooth::hci::STATUS_UNKNOWN, false);
577 
578   bluetooth::shim::hci_layer_get_interface()->transmit_command(p_buf, btu_hcif_command_complete_evt,
579                                                                btu_hcif_command_status_evt, NULL);
580 }
581 
582 using hci_cmd_cb = base::OnceCallback<void(uint8_t* /* return_parameters */,
583                                            uint16_t /* return_parameters_length*/)>;
584 
585 struct cmd_with_cb_data {
586   hci_cmd_cb cb;
587 };
588 
cmd_with_cb_data_init(cmd_with_cb_data * cb_wrapper)589 static void cmd_with_cb_data_init(cmd_with_cb_data* cb_wrapper) {
590   new (&cb_wrapper->cb) hci_cmd_cb;
591 }
592 
cmd_with_cb_data_cleanup(cmd_with_cb_data * cb_wrapper)593 static void cmd_with_cb_data_cleanup(cmd_with_cb_data* cb_wrapper) { cb_wrapper->cb.~hci_cmd_cb(); }
594 
595 /**
596  * Log command complete events that is not handled individually in this file
597  * @param opcode opcode of the command
598  * @param p_return_params pointer to returned parameter after parameter length
599  *                        field
600  */
btu_hcif_log_command_complete_metrics(uint16_t opcode,const uint8_t * p_return_params)601 static void btu_hcif_log_command_complete_metrics(uint16_t opcode, const uint8_t* p_return_params) {
602   uint16_t status = android::bluetooth::hci::STATUS_UNKNOWN;
603   uint16_t reason = android::bluetooth::hci::STATUS_UNKNOWN;
604   uint16_t hci_event = android::bluetooth::hci::EVT_COMMAND_COMPLETE;
605   RawAddress bd_addr = RawAddress::kEmpty;
606   switch (opcode) {
607     case HCI_DELETE_STORED_LINK_KEY:
608     case HCI_READ_LOCAL_OOB_DATA:
609     case HCI_READ_LOCAL_OOB_EXTENDED_DATA:
610     case HCI_WRITE_SIMPLE_PAIRING_MODE:
611     case HCI_WRITE_SECURE_CONNS_SUPPORT:
612       STREAM_TO_UINT8(status, p_return_params);
613       bluetooth::shim::LogMetricClassicPairingEvent(RawAddress::kEmpty,
614                                                     bluetooth::os::kUnknownConnectionHandle, opcode,
615                                                     hci_event, status, reason, 0);
616       break;
617     case HCI_READ_ENCR_KEY_SIZE: {
618       uint16_t handle;
619       uint8_t key_size;
620       STREAM_TO_UINT8(status, p_return_params);
621       STREAM_TO_UINT16(handle, p_return_params);
622       STREAM_TO_UINT8(key_size, p_return_params);
623       bluetooth::shim::LogMetricClassicPairingEvent(RawAddress::kEmpty, handle, opcode, hci_event,
624                                                     status, reason, key_size);
625       break;
626     }
627     case HCI_LINK_KEY_REQUEST_REPLY:
628     case HCI_LINK_KEY_REQUEST_NEG_REPLY:
629     case HCI_IO_CAPABILITY_REQUEST_REPLY:
630     case HCI_IO_CAP_REQ_NEG_REPLY:
631     case HCI_USER_CONF_REQUEST_REPLY:
632     case HCI_USER_CONF_VALUE_NEG_REPLY:
633     case HCI_USER_PASSKEY_REQ_REPLY:
634     case HCI_USER_PASSKEY_REQ_NEG_REPLY:
635     case HCI_REM_OOB_DATA_REQ_REPLY:
636     case HCI_REM_OOB_DATA_REQ_NEG_REPLY:
637       STREAM_TO_UINT8(status, p_return_params);
638       STREAM_TO_BDADDR(bd_addr, p_return_params);
639       bluetooth::shim::LogMetricClassicPairingEvent(bd_addr,
640                                                     bluetooth::os::kUnknownConnectionHandle, opcode,
641                                                     hci_event, status, reason, 0);
642       break;
643   }
644 }
645 
btu_hcif_command_complete_evt_with_cb_on_task(BT_HDR * event,void * context)646 static void btu_hcif_command_complete_evt_with_cb_on_task(BT_HDR* event, void* context) {
647   command_opcode_t opcode;
648   // 2 for event header: event code (1) + parameter length (1)
649   // 1 for num_hci_pkt command credit
650   uint8_t* stream = event->data + event->offset + 3;
651   STREAM_TO_UINT16(opcode, stream);
652 
653   btu_hcif_log_command_complete_metrics(opcode, stream);
654 
655   cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)context;
656   // 2 for event header: event code (1) + parameter length (1)
657   // 3 for command complete header: num_hci_pkt (1) + opcode (2)
658   uint16_t param_len = static_cast<uint16_t>(event->len - 5);
659   std::move(cb_wrapper->cb).Run(stream, param_len);
660   cmd_with_cb_data_cleanup(cb_wrapper);
661   osi_free(cb_wrapper);
662 
663   osi_free(event);
664 }
665 
btu_hcif_command_complete_evt_with_cb(BT_HDR * response,void * context)666 static void btu_hcif_command_complete_evt_with_cb(BT_HDR* response, void* context) {
667   do_in_main_thread(
668           base::BindOnce(btu_hcif_command_complete_evt_with_cb_on_task, response, context));
669 }
670 
btu_hcif_command_status_evt_with_cb_on_task(uint8_t status,BT_HDR * event,void * context)671 static void btu_hcif_command_status_evt_with_cb_on_task(uint8_t status, BT_HDR* event,
672                                                         void* context) {
673   command_opcode_t opcode;
674   uint8_t* stream = event->data + event->offset;
675   STREAM_TO_UINT16(opcode, stream);
676 
677   log::assert_that(status != 0, "assert failed: status != 0");
678 
679   // stream + 1 to skip parameter length field
680   // No need to check length since stream is written by us
681   btu_hcif_log_command_metrics(opcode, stream + 1, status, true);
682 
683   // report command status error
684   cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)context;
685   std::move(cb_wrapper->cb).Run(&status, sizeof(uint16_t));
686   cmd_with_cb_data_cleanup(cb_wrapper);
687   osi_free(cb_wrapper);
688 
689   osi_free(event);
690 }
691 
btu_hcif_command_status_evt_with_cb(uint8_t status,BT_HDR * command,void * context)692 static void btu_hcif_command_status_evt_with_cb(uint8_t status, BT_HDR* command, void* context) {
693   // Command is pending, we  report only error.
694   if (!status) {
695     osi_free(command);
696     return;
697   }
698 
699   do_in_main_thread(
700           base::BindOnce(btu_hcif_command_status_evt_with_cb_on_task, status, command, context));
701 }
702 
703 /* This function is called to send commands to the Host Controller. |cb| is
704  * called when command status event is called with error code, or when the
705  * command complete event is received. */
btu_hcif_send_cmd_with_cb(uint16_t opcode,uint8_t * params,uint8_t params_len,hci_cmd_cb cb)706 void btu_hcif_send_cmd_with_cb(uint16_t opcode, uint8_t* params, uint8_t params_len,
707                                hci_cmd_cb cb) {
708   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
709   uint8_t* pp = (uint8_t*)(p + 1);
710 
711   p->len = HCIC_PREAMBLE_SIZE + params_len;
712   p->offset = 0;
713 
714   UINT16_TO_STREAM(pp, opcode);
715   UINT8_TO_STREAM(pp, params_len);
716   if (params) {
717     memcpy(pp, params, params_len);
718   }
719 
720   btu_hcif_log_command_metrics(opcode, pp, android::bluetooth::hci::STATUS_UNKNOWN, false);
721 
722   cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)osi_malloc(sizeof(cmd_with_cb_data));
723 
724   cmd_with_cb_data_init(cb_wrapper);
725   cb_wrapper->cb = std::move(cb);
726 
727   bluetooth::shim::hci_layer_get_interface()->transmit_command(
728           p, btu_hcif_command_complete_evt_with_cb, btu_hcif_command_status_evt_with_cb,
729           (void*)cb_wrapper);
730 }
731 
732 /*******************************************************************************
733  *
734  * Function         btu_hcif_authentication_comp_evt
735  *
736  * Description      Process event HCI_AUTHENTICATION_COMP_EVT
737  *
738  * Returns          void
739  *
740  ******************************************************************************/
btu_hcif_authentication_comp_evt(uint8_t * p)741 static void btu_hcif_authentication_comp_evt(uint8_t* p) {
742   uint8_t status;
743   uint16_t handle;
744 
745   STREAM_TO_UINT8(status, p);
746   STREAM_TO_UINT16(handle, p);
747 
748   btm_sec_auth_complete(handle, static_cast<tHCI_STATUS>(status));
749 }
750 
751 /*******************************************************************************
752  *
753  * Function         btu_hcif_encryption_change_evt
754  *
755  * Description      Process event HCI_ENCRYPTION_CHANGE_EVT
756  *
757  * Returns          void
758  *
759  ******************************************************************************/
btu_hcif_encryption_change_evt(uint8_t * p)760 static void btu_hcif_encryption_change_evt(uint8_t* p) {
761   uint8_t status;
762   uint16_t handle;
763   uint8_t encr_enable;
764 
765   STREAM_TO_UINT8(status, p);
766   STREAM_TO_UINT16(handle, p);
767   STREAM_TO_UINT8(encr_enable, p);
768 
769   btm_sec_encryption_change_evt(handle, static_cast<tHCI_STATUS>(status), encr_enable, 0);
770 }
771 
772 /*******************************************************************************
773  *
774  * Function         btu_hcif_encryption_change_evt_v2
775  *
776  * Description      Process event HCI_ENCRYPTION_CHANGE_EVT_V2
777  *
778  * Returns          void
779  *
780  ******************************************************************************/
btu_hcif_encryption_change_evt_v2(uint8_t * p)781 static void btu_hcif_encryption_change_evt_v2(uint8_t* p) {
782   uint8_t status;
783   uint16_t handle;
784   uint8_t encr_enable;
785   uint8_t key_size;
786 
787   STREAM_TO_UINT8(status, p);
788   STREAM_TO_UINT16(handle, p);
789   STREAM_TO_UINT8(encr_enable, p);
790   STREAM_TO_UINT8(key_size, p);
791 
792   btm_sec_encryption_change_evt(handle, static_cast<tHCI_STATUS>(status), encr_enable, key_size);
793 }
794 
795 /*******************************************************************************
796  *
797  * Function         btu_hcif_read_rmt_ext_features_comp_evt
798  *
799  * Description      Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
800  *
801  * Returns          void
802  *
803  ******************************************************************************/
btu_hcif_read_rmt_ext_features_comp_evt(uint8_t * p,uint8_t evt_len)804 static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p, uint8_t evt_len) {
805   uint8_t* p_cur = p;
806   uint8_t status;
807   uint16_t handle;
808 
809   STREAM_TO_UINT8(status, p_cur);
810 
811   if (status == HCI_SUCCESS) {
812     btm_read_remote_ext_features_complete_raw(p, evt_len);
813   } else {
814     STREAM_TO_UINT16(handle, p_cur);
815     btm_read_remote_ext_features_failed(status, handle);
816   }
817 }
818 
819 /*******************************************************************************
820  *
821  * Function         btu_hcif_esco_connection_comp_evt
822  *
823  * Description      Process event HCI_ESCO_CONNECTION_COMP_EVT
824  *
825  * Returns          void
826  *
827  ******************************************************************************/
btu_hcif_esco_connection_comp_evt(const uint8_t * p)828 static void btu_hcif_esco_connection_comp_evt(const uint8_t* p) {
829   tBTM_ESCO_DATA data;
830   uint16_t handle;
831   RawAddress bda;
832   uint8_t status;
833 
834   STREAM_TO_UINT8(status, p);
835   STREAM_TO_UINT16(handle, p);
836   STREAM_TO_BDADDR(bda, p);
837 
838   STREAM_TO_UINT8(data.link_type, p);
839   STREAM_SKIP_UINT8(p);   // tx_interval
840   STREAM_SKIP_UINT8(p);   // retrans_window
841   STREAM_SKIP_UINT16(p);  // rx_pkt_len
842   STREAM_SKIP_UINT16(p);  // tx_pkt_len
843   STREAM_SKIP_UINT8(p);   // air_mode
844 
845   handle = HCID_GET_HANDLE(handle);
846   data.bd_addr = bda;
847   if (status == HCI_SUCCESS) {
848     log::assert_that(handle <= HCI_HANDLE_MAX,
849                      "Received eSCO connection complete event with invalid "
850                      "handle: 0x{:X} that should be <= 0x{:X}",
851                      handle, HCI_HANDLE_MAX);
852     btm_sco_connected(bda, handle, &data);
853   } else {
854     btm_sco_connection_failed(static_cast<tHCI_STATUS>(status), bda, handle, &data);
855   }
856 }
857 
858 /*******************************************************************************
859  *
860  * Function         btu_hcif_esco_connection_chg_evt
861  *
862  * Description      Process event HCI_ESCO_CONNECTION_CHANGED_EVT
863  *
864  * Returns          void
865  *
866  ******************************************************************************/
btu_hcif_esco_connection_chg_evt(uint8_t * p)867 static void btu_hcif_esco_connection_chg_evt(uint8_t* p) {
868   uint16_t handle;
869   uint16_t tx_pkt_len;
870   uint16_t rx_pkt_len;
871   uint8_t status;
872   uint8_t tx_interval;
873   uint8_t retrans_window;
874 
875   STREAM_TO_UINT8(status, p);
876   STREAM_TO_UINT16(handle, p);
877 
878   STREAM_TO_UINT8(tx_interval, p);
879   STREAM_TO_UINT8(retrans_window, p);
880   STREAM_TO_UINT16(rx_pkt_len, p);
881   STREAM_TO_UINT16(tx_pkt_len, p);
882 
883   handle = HCID_GET_HANDLE(handle);
884 }
885 
886 /*******************************************************************************
887  *
888  * Function         btu_hcif_hdl_command_complete
889  *
890  * Description      Handle command complete event
891  *
892  * Returns          void
893  *
894  ******************************************************************************/
btu_hcif_hdl_command_complete(uint16_t opcode,uint8_t * p,uint16_t evt_len)895 static void btu_hcif_hdl_command_complete(uint16_t opcode, uint8_t* p, uint16_t evt_len) {
896   switch (opcode) {
897     case HCI_SET_EVENT_FILTER:
898       break;
899 
900     case HCI_DELETE_STORED_LINK_KEY:
901       btm_delete_stored_link_key_complete(p, evt_len);
902       break;
903 
904     case HCI_READ_RSSI:
905       btm_read_rssi_complete(p, evt_len);
906       break;
907 
908     case HCI_READ_FAILED_CONTACT_COUNTER:
909       btm_read_failed_contact_counter_complete(p);
910       break;
911 
912     case HCI_READ_AUTOMATIC_FLUSH_TIMEOUT:
913       btm_read_automatic_flush_timeout_complete(p);
914       break;
915 
916     case HCI_READ_TRANSMIT_POWER_LEVEL:
917       btm_read_tx_power_complete(p, evt_len, false);
918       break;
919 
920     case HCI_CREATE_CONNECTION_CANCEL:
921       btu_hcif_create_conn_cancel_complete(p, evt_len);
922       break;
923 
924     case HCI_READ_LOCAL_OOB_DATA:
925       btu_hcif_read_local_oob_complete(p, evt_len);
926       break;
927 
928     case HCI_READ_LOCAL_OOB_EXTENDED_DATA:
929       btu_hcif_read_local_oob_extended_complete(p, evt_len);
930       break;
931 
932     case HCI_READ_INQ_TX_POWER_LEVEL:
933       break;
934 
935     case HCI_BLE_READ_ADV_CHNL_TX_POWER:
936       btm_read_tx_power_complete(p, evt_len, true);
937       break;
938 
939     case HCI_BLE_WRITE_ADV_ENABLE:
940       btm_ble_write_adv_enable_complete(p, evt_len);
941       break;
942 
943     case HCI_BLE_CREATE_LL_CONN:
944     case HCI_LE_EXTENDED_CREATE_CONNECTION:
945       // No command complete event for those commands according to spec
946       log::error("No command complete expected, but received!");
947       break;
948 
949     case HCI_BLE_TRANSMITTER_TEST:
950     case HCI_BLE_RECEIVER_TEST:
951     case HCI_BLE_TEST_END:
952       btm_ble_test_command_complete(p);
953       break;
954 
955     case HCI_BLE_ADD_DEV_RESOLVING_LIST:
956       btm_ble_add_resolving_list_entry_complete(p, evt_len);
957       break;
958 
959     case HCI_BLE_RM_DEV_RESOLVING_LIST:
960       btm_ble_remove_resolving_list_entry_complete(p, evt_len);
961       break;
962 
963     case HCI_BLE_CLEAR_RESOLVING_LIST:
964       btm_ble_clear_resolving_list_complete(p, evt_len);
965       break;
966 
967     case HCI_BLE_READ_RESOLVABLE_ADDR_PEER:
968       btm_ble_read_resolving_list_entry_complete(p, evt_len);
969       break;
970 
971     // Explicitly handled command complete events
972     case HCI_BLE_READ_RESOLVABLE_ADDR_LOCAL:
973     case HCI_BLE_SET_ADDR_RESOLUTION_ENABLE:
974     case HCI_BLE_SET_RAND_PRIV_ADDR_TIMEOUT:
975     case HCI_WRITE_CLASS_OF_DEVICE:
976     case HCI_WRITE_DEF_POLICY_SETTINGS:
977     case HCI_WRITE_EXT_INQ_RESPONSE:
978     case HCI_WRITE_INQSCAN_TYPE:
979     case HCI_WRITE_INQUIRYSCAN_CFG:
980     case HCI_WRITE_INQUIRY_MODE:
981     case HCI_WRITE_LINK_SUPER_TOUT:
982     case HCI_WRITE_PAGESCAN_CFG:
983     case HCI_WRITE_PAGESCAN_TYPE:
984     case HCI_WRITE_PAGE_TOUT:
985     case HCI_WRITE_SCAN_ENABLE:
986     case HCI_WRITE_VOICE_SETTINGS:
987       break;
988 
989     default:
990       log::error("Command complete for opcode:0x{:02x} should not be handled here", opcode);
991       break;
992   }
993 }
994 
995 /*******************************************************************************
996  *
997  * Function         btu_hcif_command_complete_evt
998  *
999  * Description      Process event HCI_COMMAND_COMPLETE_EVT
1000  *
1001  * Returns          void
1002  *
1003  ******************************************************************************/
btu_hcif_command_complete_evt_on_task(BT_HDR * event)1004 static void btu_hcif_command_complete_evt_on_task(BT_HDR* event) {
1005   command_opcode_t opcode;
1006   // 2 for event header: event code (1) + parameter length (1)
1007   // 1 for num_hci_pkt command credit
1008   uint8_t* stream = event->data + event->offset + 3;
1009   STREAM_TO_UINT16(opcode, stream);
1010 
1011   btu_hcif_log_command_complete_metrics(opcode, stream);
1012   // 2 for event header: event code (1) + parameter length (1)
1013   // 3 for command complete header: num_hci_pkt (1) + opcode (2)
1014   uint16_t param_len = static_cast<uint16_t>(event->len - 5);
1015   btu_hcif_hdl_command_complete(opcode, stream, param_len);
1016 
1017   osi_free(event);
1018 }
1019 
btu_hcif_command_complete_evt(BT_HDR * response,void *)1020 static void btu_hcif_command_complete_evt(BT_HDR* response, void* /* context */) {
1021   do_in_main_thread(base::BindOnce(btu_hcif_command_complete_evt_on_task, response));
1022 }
1023 
1024 /*******************************************************************************
1025  *
1026  * Function         btu_hcif_hdl_command_status
1027  *
1028  * Description      Handle a command status event
1029  *
1030  * Returns          void
1031  *
1032  ******************************************************************************/
btu_hcif_hdl_command_status(uint16_t opcode,uint8_t status,const uint8_t * p_cmd)1033 static void btu_hcif_hdl_command_status(uint16_t opcode, uint8_t status, const uint8_t* p_cmd) {
1034   log::assert_that(p_cmd != nullptr, "Null command for opcode 0x{:x}", opcode);
1035   p_cmd++;  // Skip parameter total length
1036 
1037   const tHCI_STATUS hci_status = to_hci_status_code(status);
1038 
1039   RawAddress bd_addr;
1040   uint16_t handle;
1041 
1042   switch (opcode) {
1043     case HCI_SWITCH_ROLE:
1044       if (status != HCI_SUCCESS) {
1045         // Tell BTM that the command failed
1046         STREAM_TO_BDADDR(bd_addr, p_cmd);
1047         btm_acl_role_changed(hci_status, bd_addr, HCI_ROLE_UNKNOWN);
1048       }
1049       break;
1050     case HCI_CREATE_CONNECTION:
1051       if (status != HCI_SUCCESS) {
1052         STREAM_TO_BDADDR(bd_addr, p_cmd);
1053         btm_acl_connected(bd_addr, HCI_INVALID_HANDLE, hci_status, 0);
1054       }
1055       break;
1056     case HCI_AUTHENTICATION_REQUESTED:
1057       if (status != HCI_SUCCESS) {
1058         // Device refused to start authentication
1059         // This is treated as an authentication failure
1060         btm_sec_auth_complete(HCI_INVALID_HANDLE, hci_status);
1061       }
1062       break;
1063     case HCI_SET_CONN_ENCRYPTION:
1064       if (status != HCI_SUCCESS) {
1065         // Device refused to start encryption
1066         // This is treated as an encryption failure
1067         btm_sec_encrypt_change(HCI_INVALID_HANDLE, hci_status, false, 0);
1068       }
1069       break;
1070     case HCI_READ_RMT_EXT_FEATURES:
1071       if (status != HCI_SUCCESS) {
1072         STREAM_TO_UINT16(handle, p_cmd);
1073         btm_read_remote_ext_features_failed(status, handle);
1074       }
1075       break;
1076     case HCI_SETUP_ESCO_CONNECTION:
1077     case HCI_ENH_SETUP_ESCO_CONNECTION:
1078       if (status != HCI_SUCCESS) {
1079         if (com::android::bluetooth::flags::fix_sco_command_status_handling()) {
1080           log::debug("flag: fix_sco_command_status_handling is enabled");
1081           btm_sco_create_command_status_failed(hci_status);
1082         } else {
1083           log::debug("flag: fix_sco_command_status_handling is disabled");
1084           STREAM_TO_UINT16(handle, p_cmd);
1085           RawAddress addr(RawAddress::kEmpty);
1086           btm_sco_connection_failed(hci_status, addr, handle, nullptr);
1087         }
1088       }
1089       break;
1090 
1091     case HCI_BLE_START_ENC:
1092       // Race condition: disconnection happened right before we send
1093       // "LE Encrypt", controller responds with no connection, we should
1094       // cancel the encryption attempt, rather than unpair the device.
1095       if (status == HCI_ERR_NO_CONNECTION) {
1096         smp_cancel_start_encryption_attempt();
1097       }
1098       break;
1099 
1100     // Link Policy Commands
1101     case HCI_EXIT_SNIFF_MODE:
1102     case HCI_EXIT_PARK_MODE:
1103       if (status != HCI_SUCCESS) {
1104         // Allow SCO initiation to continue if waiting for change mode event
1105         STREAM_TO_UINT16(handle, p_cmd);
1106         btm_sco_chk_pend_unpark(hci_status, handle);
1107       }
1108       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1109     case HCI_HOLD_MODE:
1110     case HCI_SNIFF_MODE:
1111     case HCI_PARK_MODE:
1112       btm_pm_proc_cmd_status(hci_status);
1113       break;
1114 
1115     // Command status event not handled by a specialized module
1116     case HCI_READ_RMT_CLOCK_OFFSET:    // 0x041f
1117     case HCI_CHANGE_CONN_PACKET_TYPE:  // 0x040f
1118       if (hci_status != HCI_SUCCESS) {
1119         log::warn("Received bad command status for opcode:0x{:02x} status:{}", opcode,
1120                   hci_status_code_text(hci_status));
1121       }
1122       break;
1123 
1124     default:
1125       log::error(
1126               "Command status for opcode:0x{:02x} should not be handled here "
1127               "status:{}",
1128               opcode, hci_status_code_text(hci_status));
1129   }
1130 }
1131 
btu_hcif_hdl_command_status(uint16_t opcode,uint8_t status,const uint8_t * p_cmd)1132 void bluetooth::legacy::testing::btu_hcif_hdl_command_status(uint16_t opcode, uint8_t status,
1133                                                              const uint8_t* p_cmd) {
1134   ::btu_hcif_hdl_command_status(opcode, status, p_cmd);
1135 }
1136 
btu_hcif_process_event(uint8_t controller_id,const BT_HDR * p_msg)1137 void bluetooth::legacy::testing::btu_hcif_process_event(uint8_t controller_id,
1138                                                         const BT_HDR* p_msg) {
1139   ::btu_hcif_process_event(controller_id, p_msg);
1140 }
1141 
1142 /*******************************************************************************
1143  *
1144  * Function         btu_hcif_command_status_evt
1145  *
1146  * Description      Process event HCI_COMMAND_STATUS_EVT
1147  *
1148  * Returns          void
1149  *
1150  ******************************************************************************/
btu_hcif_command_status_evt_on_task(uint8_t status,BT_HDR * event)1151 static void btu_hcif_command_status_evt_on_task(uint8_t status, BT_HDR* event) {
1152   command_opcode_t opcode;
1153   uint8_t* stream = event->data + event->offset;
1154   STREAM_TO_UINT16(opcode, stream);
1155 
1156   // stream + 1 to skip parameter length field
1157   // No need to check length since stream is written by us
1158   btu_hcif_log_command_metrics(opcode, stream + 1, status, true);
1159 
1160   btu_hcif_hdl_command_status(opcode, status, stream);
1161   osi_free(event);
1162 }
1163 
btu_hcif_command_status_evt(uint8_t status,BT_HDR * command,void *)1164 static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command, void* /* context */) {
1165   do_in_main_thread(base::BindOnce(btu_hcif_command_status_evt_on_task, status, command));
1166 }
1167 
1168 /*******************************************************************************
1169  *
1170  * Function         btu_hcif_mode_change_evt
1171  *
1172  * Description      Process event HCI_MODE_CHANGE_EVT
1173  *
1174  * Returns          void
1175  *
1176  ******************************************************************************/
btu_hcif_mode_change_evt(uint8_t * p)1177 static void btu_hcif_mode_change_evt(uint8_t* p) {
1178   uint8_t status;
1179   uint16_t handle;
1180   uint8_t current_mode;
1181   uint16_t interval;
1182 
1183   STREAM_TO_UINT8(status, p);
1184 
1185   STREAM_TO_UINT16(handle, p);
1186   STREAM_TO_UINT8(current_mode, p);
1187   STREAM_TO_UINT16(interval, p);
1188   btm_sco_chk_pend_unpark(static_cast<tHCI_STATUS>(status), handle);
1189   btm_pm_proc_mode_change(static_cast<tHCI_STATUS>(status), handle,
1190                           static_cast<tHCI_MODE>(current_mode), interval);
1191 
1192 #if (HID_DEV_INCLUDED == TRUE && HID_DEV_PM_INCLUDED == TRUE)
1193   hidd_pm_proc_mode_change(status, current_mode, interval);
1194 #endif
1195 }
1196 
1197 /* Parsing functions for btm functions */
1198 
btu_hcif_sec_pin_code_request(const uint8_t * p)1199 void btu_hcif_sec_pin_code_request(const uint8_t* p) {
1200   RawAddress bda;
1201 
1202   STREAM_TO_BDADDR(bda, p);
1203   btm_sec_pin_code_request(bda);
1204 }
btu_hcif_sec_link_key_request(const uint8_t * p)1205 void btu_hcif_sec_link_key_request(const uint8_t* p) {
1206   RawAddress bda;
1207   STREAM_TO_BDADDR(bda, p);
1208   btm_sec_link_key_request(bda);
1209 }
btu_hcif_rem_oob_req(const uint8_t * p)1210 void btu_hcif_rem_oob_req(const uint8_t* p) {
1211   RawAddress bda;
1212   STREAM_TO_BDADDR(bda, p);
1213   btm_rem_oob_req(bda);
1214 }
btu_hcif_simple_pair_complete(const uint8_t * p)1215 void btu_hcif_simple_pair_complete(const uint8_t* p) {
1216   RawAddress bd_addr;
1217   uint8_t status;
1218   status = *p++;
1219   STREAM_TO_BDADDR(bd_addr, p);
1220   btm_simple_pair_complete(bd_addr, status);
1221 }
btu_hcif_sec_rmt_host_support_feat_evt(const uint8_t * p)1222 void btu_hcif_sec_rmt_host_support_feat_evt(const uint8_t* p) {
1223   RawAddress bd_addr; /* peer address */
1224   uint8_t features_0;
1225 
1226   STREAM_TO_BDADDR(bd_addr, p);
1227   STREAM_TO_UINT8(features_0, p);
1228   btm_sec_rmt_host_support_feat_evt(bd_addr, features_0);
1229 }
btu_hcif_proc_sp_req_evt(tBTM_SP_EVT event,const uint8_t * p)1230 void btu_hcif_proc_sp_req_evt(tBTM_SP_EVT event, const uint8_t* p) {
1231   RawAddress bda;
1232   uint32_t value = 0;
1233 
1234   /* All events start with bd_addr */
1235   STREAM_TO_BDADDR(bda, p);
1236   switch (event) {
1237     case BTM_SP_CFM_REQ_EVT:
1238     case BTM_SP_KEY_NOTIF_EVT:
1239       STREAM_TO_UINT32(value, p);
1240       break;
1241     case BTM_SP_KEY_REQ_EVT:
1242       // No value needed.
1243       break;
1244     default:
1245       log::warn("unexpected event:{}", sp_evt_to_text(event));
1246       break;
1247   }
1248   btm_proc_sp_req_evt(event, bda, value);
1249 }
btu_hcif_create_conn_cancel_complete(const uint8_t * p,uint16_t evt_len)1250 void btu_hcif_create_conn_cancel_complete(const uint8_t* p, uint16_t evt_len) {
1251   uint8_t status;
1252 
1253   if (evt_len < 1 + BD_ADDR_LEN) {
1254     log::error("malformatted event packet, too short");
1255     return;
1256   }
1257 
1258   STREAM_TO_UINT8(status, p);
1259   RawAddress bd_addr;
1260   STREAM_TO_BDADDR(bd_addr, p);
1261   btm_create_conn_cancel_complete(status, bd_addr);
1262 }
btu_hcif_read_local_oob_complete(const uint8_t * p,uint16_t evt_len)1263 void btu_hcif_read_local_oob_complete(const uint8_t* p, uint16_t evt_len) {
1264   tBTM_SP_LOC_OOB evt_data = {};
1265   uint8_t status;
1266   if (evt_len < 1) {
1267     goto err_out;
1268   }
1269   STREAM_TO_UINT8(status, p);
1270   if (status == HCI_SUCCESS) {
1271     evt_data.status = tBTM_STATUS::BTM_SUCCESS;
1272   } else {
1273     evt_data.status = tBTM_STATUS::BTM_ERR_PROCESSING;
1274   }
1275   if (evt_len < 32 + 1) {
1276     goto err_out;
1277   }
1278   STREAM_TO_ARRAY16(evt_data.c_192.data(), p);
1279   STREAM_TO_ARRAY16(evt_data.r_192.data(), p);
1280   btm_read_local_oob_complete(evt_data);
1281   return;
1282 
1283 err_out:
1284   log::error("bogus event packet, too short");
1285 }
1286 
btu_hcif_read_local_oob_extended_complete(const uint8_t * p,uint16_t evt_len)1287 void btu_hcif_read_local_oob_extended_complete(const uint8_t* p, uint16_t evt_len) {
1288   if (evt_len < 64 + 1) {
1289     log::error("Invalid event length: {}", evt_len);
1290     return;
1291   }
1292 
1293   tBTM_SP_LOC_OOB evt_data = {};
1294   uint8_t status;
1295   STREAM_TO_UINT8(status, p);
1296   if (status == HCI_SUCCESS) {
1297     evt_data.status = tBTM_STATUS::BTM_SUCCESS;
1298   } else {
1299     evt_data.status = tBTM_STATUS::BTM_ERR_PROCESSING;
1300   }
1301 
1302   STREAM_TO_ARRAY16(evt_data.c_192.data(), p);
1303   STREAM_TO_ARRAY16(evt_data.r_192.data(), p);
1304   STREAM_TO_ARRAY16(evt_data.c_256.data(), p);
1305   STREAM_TO_ARRAY16(evt_data.r_256.data(), p);
1306   btm_read_local_oob_complete(evt_data);
1307 }
1308 
1309 /*******************************************************************************
1310  *
1311  * Function         btu_hcif_link_key_notification_evt
1312  *
1313  * Description      Process event HCI_LINK_KEY_NOTIFICATION_EVT
1314  *
1315  * Returns          void
1316  *
1317  ******************************************************************************/
btu_hcif_link_key_notification_evt(const uint8_t * p)1318 static void btu_hcif_link_key_notification_evt(const uint8_t* p) {
1319   RawAddress bda;
1320   Octet16 key;
1321   uint8_t key_type;
1322 
1323   STREAM_TO_BDADDR(bda, p);
1324   STREAM_TO_ARRAY16(key.data(), p);
1325   STREAM_TO_UINT8(key_type, p);
1326 
1327   btm_sec_link_key_notification(bda, key, key_type);
1328 }
1329 
1330 /*******************************************************************************
1331  *
1332  * Function         btu_hcif_read_clock_off_comp_evt
1333  *
1334  * Description      Process event HCI_READ_CLOCK_OFF_COMP_EVT
1335  *
1336  * Returns          void
1337  *
1338  ******************************************************************************/
btu_hcif_read_clock_off_comp_evt(uint8_t * p)1339 static void btu_hcif_read_clock_off_comp_evt(uint8_t* p) {
1340   uint8_t status;
1341   uint16_t handle;
1342   uint16_t clock_offset;
1343 
1344   STREAM_TO_UINT8(status, p);
1345 
1346   /* If failed to get clock offset just drop the result */
1347   if (status != HCI_SUCCESS) {
1348     return;
1349   }
1350 
1351   STREAM_TO_UINT16(handle, p);
1352   STREAM_TO_UINT16(clock_offset, p);
1353 
1354   handle = HCID_GET_HANDLE(handle);
1355 
1356   btm_sec_update_clock_offset(handle, clock_offset);
1357 }
1358 
1359 /**********************************************
1360  * Simple Pairing Events
1361  **********************************************/
1362 
1363 /*******************************************************************************
1364  *
1365  * Function         btu_hcif_io_cap_request_evt
1366  *
1367  * Description      Process event HCI_IO_CAPABILITY_REQUEST_EVT
1368  *
1369  * Returns          void
1370  *
1371  ******************************************************************************/
btu_hcif_io_cap_request_evt(const uint8_t * p)1372 static void btu_hcif_io_cap_request_evt(const uint8_t* p) {
1373   RawAddress bda;
1374   STREAM_TO_BDADDR(bda, p);
1375   btm_io_capabilities_req(bda);
1376 }
1377 
1378 /*******************************************************************************
1379  *
1380  * Function         btu_hcif_io_cap_request_evt
1381  *
1382  * Description      Process event HCI_IO_CAPABILITY_REQUEST_EVT
1383  *
1384  * Returns          void
1385  *
1386  ******************************************************************************/
btu_hcif_io_cap_response_evt(const uint8_t * p)1387 static void btu_hcif_io_cap_response_evt(const uint8_t* p) {
1388   tBTM_SP_IO_RSP evt_data;
1389 
1390   STREAM_TO_BDADDR(evt_data.bd_addr, p);
1391 
1392   uint8_t io_cap;
1393   STREAM_TO_UINT8(io_cap, p);
1394   evt_data.io_cap = static_cast<tBTM_IO_CAP>(io_cap);
1395 
1396   STREAM_TO_UINT8(evt_data.oob_data, p);
1397   STREAM_TO_UINT8(evt_data.auth_req, p);
1398   btm_io_capabilities_rsp(evt_data);
1399 }
1400 
1401 /**********************************************
1402  * End of Simple Pairing Events
1403  **********************************************/
1404 
btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t * p)1405 static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p) {
1406   uint8_t status;
1407   uint16_t handle;
1408 
1409   STREAM_TO_UINT8(status, p);
1410   STREAM_TO_UINT16(handle, p);
1411 
1412   btm_sec_encryption_key_refresh_complete(handle, static_cast<tHCI_STATUS>(status));
1413 }
1414 
1415 /**********************************************
1416  * BLE Events
1417  **********************************************/
1418 
btu_ble_proc_ltk_req(uint8_t * p,uint16_t evt_len)1419 static void btu_ble_proc_ltk_req(uint8_t* p, uint16_t evt_len) {
1420   uint16_t ediv, handle;
1421   uint8_t* pp;
1422 
1423   // following the spec in Core_v5.3/Vol 4/Part E
1424   // / 7.7.65.5 LE Long Term Key Request event
1425   // A BLE Long Term Key Request event contains:
1426   // - 1-byte subevent (already consumed in btu_hcif_process_event)
1427   // - 2-byte connection handler
1428   // - 8-byte random number
1429   // - 2 byte Encrypted_Diversifier
1430   if (evt_len < 2 + 8 + 2) {
1431     log::error("Event packet too short");
1432     return;
1433   }
1434 
1435   STREAM_TO_UINT16(handle, p);
1436   pp = p + 8;
1437   STREAM_TO_UINT16(ediv, pp);
1438   btm_ble_ltk_request(handle, p, ediv);
1439   /* This is empty until an upper layer cares about returning event */
1440 }
1441 
1442 /**********************************************
1443  * End of BLE Events Handler
1444  **********************************************/
1445 
btu_hci_msg_process(BT_HDR * p_msg)1446 void btu_hci_msg_process(BT_HDR* p_msg) {
1447   /* Determine the input message type. */
1448   switch (p_msg->event & BT_EVT_MASK) {
1449     case BT_EVT_TO_BTU_HCI_EVT:
1450       btu_hcif_process_event((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
1451       osi_free(p_msg);
1452       break;
1453 
1454     case BT_EVT_TO_BTU_HCI_ISO:
1455       IsoManager::GetInstance()->HandleIsoData(p_msg);
1456       osi_free(p_msg);
1457       break;
1458 
1459     default:
1460       osi_free(p_msg);
1461       break;
1462   }
1463 }
1464