1 /******************************************************************************
2  *
3  *  Copyright 2000-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  *  Name:          btm_acl.cc
22  *
23  *  Description:   This file contains functions that handle ACL connections.
24  *                 This includes operations such as hold and sniff modes,
25  *                 supported packet types.
26  *
27  *                 This module contains both internal and external (API)
28  *                 functions. External (API) functions are distinguishable
29  *                 by their names beginning with uppercase BTM.
30  *
31  *
32  *****************************************************************************/
33 
34 #define LOG_TAG "btm_acl"
35 
36 #include <bluetooth/log.h>
37 
38 #include <cstdint>
39 
40 #include "bta/include/bta_dm_acl.h"
41 #include "bta/sys/bta_sys.h"
42 #include "common/init_flags.h"
43 #include "common/metrics.h"
44 #include "device/include/device_iot_config.h"
45 #include "device/include/interop.h"
46 #include "hci/controller_interface.h"
47 #include "include/l2cap_hci_link_interface.h"
48 #include "internal_include/bt_target.h"
49 #include "main/shim/acl_api.h"
50 #include "main/shim/dumpsys.h"
51 #include "main/shim/entry.h"
52 #include "main/shim/helpers.h"
53 #include "os/log.h"
54 #include "os/parameter_provider.h"
55 #include "osi/include/allocator.h"
56 #include "osi/include/properties.h"
57 #include "osi/include/stack_power_telemetry.h"
58 #include "rust/src/connection/ffi/connection_shim.h"
59 #include "rust/src/core/ffi/types.h"
60 #include "stack/acl/acl.h"
61 #include "stack/acl/peer_packet_types.h"
62 #include "stack/btm/btm_dev.h"
63 #include "stack/btm/btm_int_types.h"
64 #include "stack/btm/btm_sco.h"
65 #include "stack/btm/btm_sec.h"
66 #include "stack/btm/security_device_record.h"
67 #include "stack/include/acl_api.h"
68 #include "stack/include/acl_api_types.h"
69 #include "stack/include/acl_hci_link_interface.h"
70 #include "stack/include/bt_hdr.h"
71 #include "stack/include/bt_types.h"
72 #include "stack/include/btm_api.h"
73 #include "stack/include/btm_ble_api.h"
74 #include "stack/include/btm_iso_api.h"
75 #include "stack/include/hci_error_code.h"
76 #include "stack/include/hcimsgs.h"
77 #include "stack/include/l2cap_acl_interface.h"
78 #include "stack/include/l2cdefs.h"
79 #include "stack/include/main_thread.h"
80 #include "types/hci_role.h"
81 #include "types/raw_address.h"
82 
83 #ifndef PROPERTY_LINK_SUPERVISION_TIMEOUT
84 #define PROPERTY_LINK_SUPERVISION_TIMEOUT \
85   "bluetooth.core.acl.link_supervision_timeout"
86 #endif
87 
88 using namespace bluetooth;
89 using bluetooth::legacy::hci::GetInterface;
90 
91 void BTM_update_version_info(const RawAddress& bd_addr,
92                              const remote_version_info& remote_version_info);
93 
94 static void find_in_device_record(const RawAddress& bd_addr,
95                                   tBLE_BD_ADDR* address_with_type);
96 void l2c_link_hci_conn_comp(tHCI_STATUS status, uint16_t handle,
97                             const RawAddress& p_bda);
98 
99 void BTM_db_reset(void);
100 
101 extern tBTM_CB btm_cb;
102 void btm_iot_save_remote_properties(tACL_CONN* p_acl_cb);
103 void btm_iot_save_remote_versions(tACL_CONN* p_acl_cb);
104 
105 struct StackAclBtmAcl {
106   tACL_CONN* acl_allocate_connection();
107   tACL_CONN* acl_get_connection_from_handle(uint16_t handle);
108   tACL_CONN* btm_bda_to_acl(const RawAddress& bda, tBT_TRANSPORT transport);
109   bool change_connection_packet_types(tACL_CONN& link,
110                                       const uint16_t new_packet_type_bitmask);
111   void btm_establish_continue(tACL_CONN* p_acl_cb);
112   void btm_set_default_link_policy(tLINK_POLICY settings);
113   void btm_acl_role_changed(tHCI_STATUS hci_status, const RawAddress& bd_addr,
114                             tHCI_ROLE new_role);
115   void hci_start_role_switch_to_central(tACL_CONN& p_acl);
set_default_packet_types_supportedStackAclBtmAcl116   void set_default_packet_types_supported(uint16_t packet_types_supported) {
117     btm_cb.acl_cb_.btm_acl_pkt_types_supported = packet_types_supported;
118   }
119   void btm_acl_consolidate(const RawAddress& identity_addr,
120                            const RawAddress& rpa);
121 };
122 
123 struct RoleChangeView {
124   tHCI_ROLE new_role = HCI_ROLE_UNKNOWN;
125   RawAddress bd_addr;
126 };
127 
128 namespace {
129 StackAclBtmAcl internal_;
130 std::unique_ptr<RoleChangeView> delayed_role_change_ = nullptr;
131 }
132 
133 typedef struct {
134   uint16_t handle;
135   uint16_t hci_len;
136 } __attribute__((packed)) acl_header_t;
137 
138 constexpr uint8_t BTM_MAX_SW_ROLE_FAILED_ATTEMPTS = 3;
139 
140 /* Define masks for supported and exception 2.0 ACL packet types
141  */
142 constexpr uint16_t BTM_ACL_SUPPORTED_PKTS_MASK =
143     (HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1 | HCI_PKT_TYPES_MASK_DM3 |
144      HCI_PKT_TYPES_MASK_DH3 | HCI_PKT_TYPES_MASK_DM5 | HCI_PKT_TYPES_MASK_DH5);
145 
146 constexpr uint16_t BTM_ACL_EXCEPTION_PKTS_MASK =
147     (HCI_PKT_TYPES_MASK_NO_2_DH1 | HCI_PKT_TYPES_MASK_NO_3_DH1 |
148      HCI_PKT_TYPES_MASK_NO_2_DH3 | HCI_PKT_TYPES_MASK_NO_3_DH3 |
149      HCI_PKT_TYPES_MASK_NO_2_DH5 | HCI_PKT_TYPES_MASK_NO_3_DH5);
150 
IsEprAvailable(const tACL_CONN & p_acl)151 static bool IsEprAvailable(const tACL_CONN& p_acl) {
152   if (!p_acl.peer_lmp_feature_valid[0]) {
153     log::warn("Checking incomplete feature page read");
154     return false;
155   }
156   return HCI_ATOMIC_ENCRYPT_SUPPORTED(p_acl.peer_lmp_feature_pages[0]) &&
157          bluetooth::shim::GetController()->SupportsEncryptionPause();
158 }
159 
160 static void btm_process_remote_ext_features(tACL_CONN* p_acl_cb,
161                                             uint8_t max_page_number);
162 static void btm_read_failed_contact_counter_timeout(void* data);
163 static void btm_read_remote_ext_features(uint16_t handle, uint8_t page_number);
164 static void btm_read_rssi_timeout(void* data);
165 static void btm_read_tx_power_timeout(void* data);
166 static void check_link_policy(tLINK_POLICY* settings);
167 void btm_set_link_policy(tACL_CONN* conn, tLINK_POLICY policy);
168 
169 namespace {
NotifyAclLinkUp(tACL_CONN & p_acl)170 void NotifyAclLinkUp(tACL_CONN& p_acl) {
171   if (p_acl.link_up_issued) {
172     log::info("Already notified BTA layer that the link is up");
173     return;
174   }
175   p_acl.link_up_issued = true;
176   BTA_dm_acl_up(p_acl.remote_addr, p_acl.transport, p_acl.hci_handle);
177 }
178 
NotifyAclLinkDown(tACL_CONN & p_acl)179 void NotifyAclLinkDown(tACL_CONN& p_acl) {
180   /* Only notify if link up has had a chance to be issued */
181   if (p_acl.link_up_issued) {
182     p_acl.link_up_issued = false;
183     BTA_dm_acl_down(p_acl.remote_addr, p_acl.transport);
184   }
185 }
186 
NotifyAclRoleSwitchComplete(const RawAddress & bda,tHCI_ROLE new_role,tHCI_STATUS hci_status)187 void NotifyAclRoleSwitchComplete(const RawAddress& bda, tHCI_ROLE new_role,
188                                  tHCI_STATUS hci_status) {
189   BTA_dm_report_role_change(bda, new_role, hci_status);
190 }
191 
NotifyAclFeaturesReadComplete(tACL_CONN & p_acl,uint8_t max_page_number)192 void NotifyAclFeaturesReadComplete(tACL_CONN& p_acl, uint8_t max_page_number) {
193   btm_process_remote_ext_features(&p_acl, max_page_number);
194   btm_set_link_policy(&p_acl, btm_cb.acl_cb_.DefaultLinkPolicy());
195   BTA_dm_notify_remote_features_complete(p_acl.remote_addr);
196 }
197 
198 }  // namespace
199 
disconnect_acl(tACL_CONN & p_acl,tHCI_STATUS reason,std::string comment)200 static void disconnect_acl(tACL_CONN& p_acl, tHCI_STATUS reason,
201                            std::string comment) {
202   log::info("Disconnecting peer:{} reason:{} comment:{}", p_acl.remote_addr,
203             hci_error_code_text(reason), comment);
204   p_acl.disconnect_reason = reason;
205 
206   return bluetooth::shim::ACL_Disconnect(
207       p_acl.hci_handle, p_acl.is_transport_br_edr(), reason, comment);
208 }
209 
hci_start_role_switch_to_central(tACL_CONN & p_acl)210 void StackAclBtmAcl::hci_start_role_switch_to_central(tACL_CONN& p_acl) {
211   GetInterface().StartRoleSwitch(p_acl.remote_addr,
212                                  static_cast<uint8_t>(HCI_ROLE_CENTRAL));
213   p_acl.set_switch_role_in_progress();
214   p_acl.rs_disc_pending = BTM_SEC_RS_PENDING;
215 }
216 
hci_btm_set_link_supervision_timeout(tACL_CONN & link,uint16_t timeout)217 void hci_btm_set_link_supervision_timeout(tACL_CONN& link, uint16_t timeout) {
218   if (link.link_role != HCI_ROLE_CENTRAL) {
219     /* Only send if current role is Central; 2.0 spec requires this */
220     log::warn("Can only set link supervision timeout if central role:{}",
221               RoleText(link.link_role));
222     return;
223   }
224 
225   if (!bluetooth::shim::GetController()->IsSupported(
226           bluetooth::hci::OpCode::WRITE_LINK_SUPERVISION_TIMEOUT)) {
227     log::warn(
228         "UNSUPPORTED by controller write link supervision timeout:{:.2f}ms "
229         "bd_addr:{}",
230         supervision_timeout_to_seconds(timeout), link.RemoteAddress());
231     return;
232   }
233   log::debug("Setting link supervision timeout:{:.2f}s peer:{}",
234              double(timeout) * 0.01, link.RemoteAddress());
235   link.link_super_tout = timeout;
236   btsnd_hcic_write_link_super_tout(link.Handle(), timeout);
237 }
238 
239 /* 3 seconds timeout waiting for responses */
240 #define BTM_DEV_REPLY_TIMEOUT_MS (3 * 1000)
241 
BTM_acl_after_controller_started()242 void BTM_acl_after_controller_started() {
243   internal_.btm_set_default_link_policy(
244       HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH | HCI_ENABLE_HOLD_MODE |
245       HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE);
246 
247   /* Create ACL supported packet types mask */
248   uint16_t btm_acl_pkt_types_supported =
249       (HCI_PKT_TYPES_MASK_DH1 + HCI_PKT_TYPES_MASK_DM1);
250 
251   if (bluetooth::shim::GetController()->Supports3SlotPackets())
252     btm_acl_pkt_types_supported |=
253         (HCI_PKT_TYPES_MASK_DH3 + HCI_PKT_TYPES_MASK_DM3);
254 
255   if (bluetooth::shim::GetController()->Supports5SlotPackets())
256     btm_acl_pkt_types_supported |=
257         (HCI_PKT_TYPES_MASK_DH5 + HCI_PKT_TYPES_MASK_DM5);
258 
259   /* Add in EDR related ACL types */
260   if (!bluetooth::shim::GetController()->SupportsClassic2mPhy()) {
261     btm_acl_pkt_types_supported |=
262         (HCI_PKT_TYPES_MASK_NO_2_DH1 + HCI_PKT_TYPES_MASK_NO_2_DH3 +
263          HCI_PKT_TYPES_MASK_NO_2_DH5);
264   }
265 
266   if (!bluetooth::shim::GetController()->SupportsClassic3mPhy()) {
267     btm_acl_pkt_types_supported |=
268         (HCI_PKT_TYPES_MASK_NO_3_DH1 + HCI_PKT_TYPES_MASK_NO_3_DH3 +
269          HCI_PKT_TYPES_MASK_NO_3_DH5);
270   }
271 
272   /* Check to see if 3 and 5 slot packets are available */
273   if (bluetooth::shim::GetController()->SupportsClassic2mPhy() ||
274       bluetooth::shim::GetController()->SupportsClassic3mPhy()) {
275     if (!bluetooth::shim::GetController()->Supports3SlotEdrPackets())
276       btm_acl_pkt_types_supported |=
277           (HCI_PKT_TYPES_MASK_NO_2_DH3 + HCI_PKT_TYPES_MASK_NO_3_DH3);
278 
279     if (!bluetooth::shim::GetController()->Supports5SlotEdrPackets())
280       btm_acl_pkt_types_supported |=
281           (HCI_PKT_TYPES_MASK_NO_2_DH5 + HCI_PKT_TYPES_MASK_NO_3_DH5);
282   }
283   internal_.set_default_packet_types_supported(btm_acl_pkt_types_supported);
284 }
285 
286 /*******************************************************************************
287  *
288  * Function        btm_bda_to_acl
289  *
290  * Description     This function returns the FIRST acl_db entry for the passed
291  *                 BDA.
292  *
293  * Parameters      bda : BD address of the remote device
294  *                 transport : Physical transport used for ACL connection
295  *                 (BR/EDR or LE)
296  *
297  * Returns         Returns pointer to the ACL DB for the requested BDA if found.
298  *                 nullptr if not found.
299  *
300  ******************************************************************************/
btm_bda_to_acl(const RawAddress & bda,tBT_TRANSPORT transport)301 tACL_CONN* StackAclBtmAcl::btm_bda_to_acl(const RawAddress& bda,
302                                           tBT_TRANSPORT transport) {
303   tACL_CONN* p_acl = &btm_cb.acl_cb_.acl_db[0];
304   for (uint8_t index = 0; index < MAX_L2CAP_LINKS; index++, p_acl++) {
305     if ((p_acl->in_use) && p_acl->remote_addr == bda &&
306         p_acl->transport == transport) {
307       return p_acl;
308     }
309   }
310   return nullptr;
311 }
312 
acl_get_connection_from_address(const RawAddress & bd_addr,tBT_TRANSPORT transport)313 tACL_CONN* acl_get_connection_from_address(const RawAddress& bd_addr,
314                                            tBT_TRANSPORT transport) {
315   return internal_.btm_bda_to_acl(bd_addr, transport);
316 }
317 
btm_acl_consolidate(const RawAddress & identity_addr,const RawAddress & rpa)318 void StackAclBtmAcl::btm_acl_consolidate(const RawAddress& identity_addr,
319                                          const RawAddress& rpa) {
320   tACL_CONN* p_acl = &btm_cb.acl_cb_.acl_db[0];
321   for (uint8_t index = 0; index < MAX_L2CAP_LINKS; index++, p_acl++) {
322     if (!p_acl->in_use) continue;
323 
324     if (p_acl->remote_addr == rpa) {
325       log::info("consolidate {} -> {}", rpa, identity_addr);
326       p_acl->remote_addr = identity_addr;
327       return;
328     }
329   }
330 }
331 
btm_acl_consolidate(const RawAddress & identity_addr,const RawAddress & rpa)332 void btm_acl_consolidate(const RawAddress& identity_addr,
333                          const RawAddress& rpa) {
334   return internal_.btm_acl_consolidate(identity_addr, rpa);
335 }
336 
337 /*******************************************************************************
338  *
339  * Function         btm_handle_to_acl_index
340  *
341  * Description      This function returns the FIRST acl_db entry for the passed
342  *                  hci_handle.
343  *
344  * Returns          index to the acl_db or MAX_L2CAP_LINKS.
345  *
346  ******************************************************************************/
btm_handle_to_acl_index(uint16_t hci_handle)347 uint8_t btm_handle_to_acl_index(uint16_t hci_handle) {
348   tACL_CONN* p = &btm_cb.acl_cb_.acl_db[0];
349   uint8_t xx;
350   for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++) {
351     if ((p->in_use) && (p->hci_handle == hci_handle)) {
352       break;
353     }
354   }
355 
356   /* If here, no BD Addr found */
357   return (xx);
358 }
359 
acl_get_connection_from_handle(uint16_t hci_handle)360 tACL_CONN* StackAclBtmAcl::acl_get_connection_from_handle(uint16_t hci_handle) {
361   uint8_t index = btm_handle_to_acl_index(hci_handle);
362   if (index >= MAX_L2CAP_LINKS) return nullptr;
363   return &btm_cb.acl_cb_.acl_db[index];
364 }
365 
acl_get_connection_from_handle(uint16_t handle)366 tACL_CONN* acl_get_connection_from_handle(uint16_t handle) {
367   return internal_.acl_get_connection_from_handle(handle);
368 }
369 
btm_acl_process_sca_cmpl_pkt(uint8_t len,uint8_t * data)370 void btm_acl_process_sca_cmpl_pkt(uint8_t len, uint8_t* data) {
371   uint16_t handle;
372   uint8_t sca;
373   uint8_t status;
374 
375   if (len < 4) {
376     log::warn("Malformatted packet, not containing enough data");
377     return;
378   }
379 
380   STREAM_TO_UINT8(status, data);
381 
382   if (status != HCI_SUCCESS) {
383     log::warn("Peer SCA Command complete failed:{}",
384               hci_error_code_text(static_cast<tHCI_STATUS>(status)));
385     return;
386   }
387 
388   STREAM_TO_UINT16(handle, data);
389   STREAM_TO_UINT8(sca, data);
390 
391   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
392   if (p_acl == nullptr) {
393     log::warn("Unable to find active acl");
394     return;
395   }
396   p_acl->sca = sca;
397 }
398 
acl_allocate_connection()399 tACL_CONN* StackAclBtmAcl::acl_allocate_connection() {
400   tACL_CONN* p_acl = &btm_cb.acl_cb_.acl_db[0];
401   for (uint8_t xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl++) {
402     if (!p_acl->in_use) {
403       return p_acl;
404     }
405   }
406   return nullptr;
407 }
408 
btm_acl_created(const RawAddress & bda,uint16_t hci_handle,tHCI_ROLE link_role,tBT_TRANSPORT transport)409 void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
410                      tHCI_ROLE link_role, tBT_TRANSPORT transport) {
411   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bda, transport);
412   if (p_acl != (tACL_CONN*)NULL) {
413     p_acl->hci_handle = hci_handle;
414     p_acl->link_role = link_role;
415     p_acl->transport = transport;
416     if (transport == BT_TRANSPORT_BR_EDR) {
417       btm_set_link_policy(p_acl, btm_cb.acl_cb_.DefaultLinkPolicy());
418     }
419     log::warn(
420         "Unable to create duplicate acl when one already exists handle:{} "
421         "role:{} transport:{}",
422         hci_handle, RoleText(link_role), bt_transport_text(transport));
423     return;
424   }
425 
426   p_acl = internal_.acl_allocate_connection();
427   if (p_acl == nullptr) {
428     log::warn("Unable to find active acl");
429     return;
430   }
431 
432   p_acl->in_use = true;
433   p_acl->hci_handle = hci_handle;
434   p_acl->link_role = link_role;
435   p_acl->link_up_issued = false;
436   p_acl->remote_addr = bda;
437   p_acl->sca = 0xFF;
438   p_acl->transport = transport;
439   p_acl->switch_role_failed_attempts = 0;
440   p_acl->reset_switch_role();
441 
442   log::debug(
443       "Created new ACL connection peer:{} role:{} handle:0x{:04x} transport:{}",
444       bda, RoleText(p_acl->link_role), hci_handle,
445       bt_transport_text(transport));
446 
447   if (p_acl->is_transport_br_edr()) {
448     BTM_PM_OnConnected(hci_handle, bda);
449     btm_set_link_policy(p_acl, btm_cb.acl_cb_.DefaultLinkPolicy());
450   }
451 
452   // save remote properties to iot conf file
453   btm_iot_save_remote_properties(p_acl);
454 
455   /* if BR/EDR do something more */
456   if (transport == BT_TRANSPORT_BR_EDR) {
457     btsnd_hcic_read_rmt_clk_offset(hci_handle);
458   }
459 
460   if (transport == BT_TRANSPORT_LE) {
461     btm_ble_get_acl_remote_addr(hci_handle, p_acl->active_remote_addr,
462                                 &p_acl->active_remote_addr_type);
463 
464     if (bluetooth::shim::GetController()
465             ->SupportsBlePeripheralInitiatedFeaturesExchange() ||
466         link_role == HCI_ROLE_CENTRAL) {
467       btsnd_hcic_ble_read_remote_feat(p_acl->hci_handle);
468     } else {
469       internal_.btm_establish_continue(p_acl);
470     }
471   }
472 }
473 
btm_acl_create_failed(const RawAddress & bda,tBT_TRANSPORT transport,tHCI_STATUS hci_status)474 void btm_acl_create_failed(const RawAddress& bda, tBT_TRANSPORT transport,
475                            tHCI_STATUS hci_status) {
476   BTA_dm_acl_up_failed(bda, transport, hci_status);
477 }
478 
479 /*******************************************************************************
480  *
481  * Function         btm_acl_removed
482  *
483  * Description      This function is called by L2CAP when an ACL connection
484  *                  is removed. Since only L2CAP creates ACL links, we use
485  *                  the L2CAP link index as our index into the control blocks.
486  *
487  * Returns          void
488  *
489  ******************************************************************************/
btm_acl_removed(uint16_t handle)490 void btm_acl_removed(uint16_t handle) {
491   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
492   if (p_acl == nullptr) {
493     log::warn("Unable to find active acl");
494     return;
495   }
496   p_acl->in_use = false;
497   NotifyAclLinkDown(*p_acl);
498   if (p_acl->is_transport_br_edr()) {
499     BTM_PM_OnDisconnected(handle);
500   }
501   p_acl->Reset();
502 }
503 
504 /*******************************************************************************
505  *
506  * Function         btm_acl_device_down
507  *
508  * Description      This function is called when the local device is deemed
509  *                  to be down. It notifies L2CAP of the failure.
510  *
511  * Returns          void
512  *
513  ******************************************************************************/
btm_acl_device_down(void)514 void btm_acl_device_down(void) {
515   tACL_CONN* p = &btm_cb.acl_cb_.acl_db[0];
516   uint16_t xx;
517   for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++) {
518     if (p->in_use) {
519       l2c_link_hci_disc_comp(p->hci_handle, HCI_ERR_HW_FAILURE);
520     }
521   }
522   BTM_db_reset();
523 }
524 
BTM_GetRole(const RawAddress & remote_bd_addr,tHCI_ROLE * p_role)525 tBTM_STATUS BTM_GetRole(const RawAddress& remote_bd_addr, tHCI_ROLE* p_role) {
526   if (p_role == nullptr) {
527     return BTM_ILLEGAL_VALUE;
528   }
529   *p_role = HCI_ROLE_UNKNOWN;
530 
531   tACL_CONN* p_acl =
532       internal_.btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR);
533   if (p_acl == nullptr) {
534     log::warn("Unable to find active acl");
535     return BTM_UNKNOWN_ADDR;
536   }
537   *p_role = p_acl->link_role;
538   return BTM_SUCCESS;
539 }
540 
541 /*******************************************************************************
542  *
543  * Function         BTM_SwitchRoleToCentral
544  *
545  * Description      This function is called to switch role between central and
546  *                  peripheral.  If role is already set it will do nothing.
547  *
548  * Returns          BTM_SUCCESS if already in specified role.
549  *                  BTM_CMD_STARTED if command issued to controller.
550  *                  BTM_NO_RESOURCES if couldn't allocate memory to issue
551  *                                   command
552  *                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
553  *                  BTM_MODE_UNSUPPORTED if local device does not support role
554  *                                       switching
555  *                  BTM_BUSY if the previous command is not completed
556  *
557  ******************************************************************************/
BTM_SwitchRoleToCentral(const RawAddress & remote_bd_addr)558 tBTM_STATUS BTM_SwitchRoleToCentral(const RawAddress& remote_bd_addr) {
559   if (!bluetooth::shim::GetController()->SupportsRoleSwitch()) {
560     log::info("Local controller does not support role switching");
561     return BTM_MODE_UNSUPPORTED;
562   }
563 
564   tACL_CONN* p_acl =
565       internal_.btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR);
566   if (p_acl == nullptr) {
567     log::warn("Unable to find active acl");
568     return BTM_UNKNOWN_ADDR;
569   }
570 
571   if (p_acl->link_role == HCI_ROLE_CENTRAL) {
572     log::info("Requested role is already in effect");
573     return BTM_SUCCESS;
574   }
575 
576   if (interop_match_addr(INTEROP_DISABLE_ROLE_SWITCH, &remote_bd_addr)) {
577     log::info("Remote device is on list preventing role switch");
578     return BTM_DEV_RESTRICT_LISTED;
579   }
580 
581   if (BTM_IsScoActiveByBdaddr(remote_bd_addr)) {
582     log::info("An active SCO to device prevents role switch at this time");
583     return BTM_NO_RESOURCES;
584   }
585 
586   if (!p_acl->is_switch_role_idle()) {
587     log::info("Role switch is already progress");
588     return BTM_BUSY;
589   }
590 
591   if (interop_match_addr(INTEROP_DYNAMIC_ROLE_SWITCH, &remote_bd_addr)) {
592     log::debug("Device restrict listed under INTEROP_DYNAMIC_ROLE_SWITCH");
593     return BTM_DEV_RESTRICT_LISTED;
594   }
595 
596   tBTM_PM_MODE pwr_mode;
597   if (!BTM_ReadPowerMode(p_acl->remote_addr, &pwr_mode)) {
598     log::warn(
599         "Unable to find device to read current power mode prior to role "
600         "switch");
601     return BTM_UNKNOWN_ADDR;
602   };
603 
604   if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF) {
605     if (!BTM_SetLinkPolicyActiveMode(p_acl->remote_addr)) {
606       log::warn("Unable to set link policy active before attempting switch");
607       return BTM_WRONG_MODE;
608     }
609     p_acl->set_switch_role_changing();
610   }
611   /* some devices do not support switch while encryption is on */
612   else {
613     if (p_acl->is_encrypted && !IsEprAvailable(*p_acl)) {
614       /* bypass turning off encryption if change link key is already doing it */
615       p_acl->set_encryption_off();
616       p_acl->set_switch_role_encryption_off();
617     } else {
618       internal_.hci_start_role_switch_to_central(*p_acl);
619     }
620   }
621 
622   return BTM_CMD_STARTED;
623 }
624 
625 /*******************************************************************************
626  *
627  * Function         btm_acl_encrypt_change
628  *
629  * Description      This function is when encryption of the connection is
630  *                  completed by the LM.  Checks to see if a role switch or
631  *                  change of link key was active and initiates or continues
632  *                  process if needed.
633  *
634  * Returns          void
635  *
636  ******************************************************************************/
btm_acl_encrypt_change(uint16_t handle,uint8_t,uint8_t encr_enable)637 void btm_acl_encrypt_change(uint16_t handle, uint8_t /* status */,
638                             uint8_t encr_enable) {
639   tACL_CONN* p = internal_.acl_get_connection_from_handle(handle);
640   if (p == nullptr) {
641     log::warn("Unable to find active acl");
642     return;
643   }
644 
645   /* Common Criteria mode only: if we are trying to drop encryption on an
646    * encrypted connection, drop the connection */
647   if (bluetooth::os::ParameterProvider::IsCommonCriteriaMode()) {
648     if (p->is_encrypted && !encr_enable) {
649       log::error(
650           "attempting to decrypt encrypted connection, disconnecting. handle: "
651           "0x{:x}",
652           handle);
653 
654       acl_disconnect_from_handle(handle, HCI_ERR_HOST_REJECT_SECURITY,
655                                  "stack::btu::btu_hcif::read_drop_encryption "
656                                  "Connection Already Encrypted");
657       return;
658     }
659   }
660 
661   p->is_encrypted = encr_enable;
662 
663   /* Process Role Switch if active */
664   if (p->is_switch_role_encryption_off()) {
665     /* if encryption turn off failed we still will try to switch role */
666     if (encr_enable) {
667       p->set_encryption_idle();
668       p->reset_switch_role();
669     } else {
670       p->set_encryption_switching();
671       p->set_switch_role_switching();
672     }
673     internal_.hci_start_role_switch_to_central(*p);
674   }
675   /* Finished enabling Encryption after role switch */
676   else if (p->is_switch_role_encryption_on()) {
677     p->reset_switch_role();
678     p->set_encryption_idle();
679     NotifyAclRoleSwitchComplete(
680         btm_cb.acl_cb_.switch_role_ref_data.remote_bd_addr,
681         btm_cb.acl_cb_.switch_role_ref_data.role,
682         btm_cb.acl_cb_.switch_role_ref_data.hci_status);
683 
684     /* If a disconnect is pending, issue it now that role switch has completed
685      */
686     if (p->rs_disc_pending == BTM_SEC_DISC_PENDING) {
687       disconnect_acl(*p, HCI_ERR_PEER_USER,
688                      "stack::acl::btm_acl::encrypt after role switch");
689     }
690     p->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
691   }
692 }
693 
check_link_policy(tLINK_POLICY * settings)694 static void check_link_policy(tLINK_POLICY* settings) {
695   if ((*settings & HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH) &&
696       (!bluetooth::shim::GetController()->SupportsRoleSwitch())) {
697     *settings &= (~HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
698     log::info("Role switch not supported (settings: 0x{:04x})", *settings);
699   }
700   if ((*settings & HCI_ENABLE_HOLD_MODE) &&
701       (!bluetooth::shim::GetController()->SupportsHoldMode())) {
702     *settings &= (~HCI_ENABLE_HOLD_MODE);
703     log::info("hold not supported (settings: 0x{:04x})", *settings);
704   }
705   if ((*settings & HCI_ENABLE_SNIFF_MODE) &&
706       (!bluetooth::shim::GetController()->SupportsSniffMode())) {
707     *settings &= (~HCI_ENABLE_SNIFF_MODE);
708     log::info("sniff not supported (settings: 0x{:04x})", *settings);
709   }
710   if ((*settings & HCI_ENABLE_PARK_MODE) &&
711       (!bluetooth::shim::GetController()->SupportsParkMode())) {
712     *settings &= (~HCI_ENABLE_PARK_MODE);
713     log::info("park not supported (settings: 0x{:04x})", *settings);
714   }
715 }
716 
btm_set_link_policy(tACL_CONN * conn,tLINK_POLICY policy)717 void btm_set_link_policy(tACL_CONN* conn, tLINK_POLICY policy) {
718   conn->link_policy = policy;
719   check_link_policy(&conn->link_policy);
720   if ((conn->link_policy & HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH) &&
721       interop_match_addr(INTEROP_DISABLE_SNIFF, &(conn->remote_addr))) {
722     conn->link_policy &= (~HCI_ENABLE_SNIFF_MODE);
723   }
724   btsnd_hcic_write_policy_set(conn->hci_handle,
725                               static_cast<uint16_t>(conn->link_policy));
726 }
727 
btm_toggle_policy_on_for(const RawAddress & peer_addr,uint16_t flag)728 static void btm_toggle_policy_on_for(const RawAddress& peer_addr,
729                                      uint16_t flag) {
730   auto conn = internal_.btm_bda_to_acl(peer_addr, BT_TRANSPORT_BR_EDR);
731   if (!conn) {
732     log::warn("Unable to find active acl");
733     return;
734   }
735   btm_set_link_policy(conn, conn->link_policy | flag);
736 }
737 
btm_toggle_policy_off_for(const RawAddress & peer_addr,uint16_t flag)738 static void btm_toggle_policy_off_for(const RawAddress& peer_addr,
739                                       uint16_t flag) {
740   auto conn = internal_.btm_bda_to_acl(peer_addr, BT_TRANSPORT_BR_EDR);
741   if (!conn) {
742     log::warn("Unable to find active acl");
743     return;
744   }
745   btm_set_link_policy(conn, conn->link_policy & ~flag);
746 }
747 
BTM_is_sniff_allowed_for(const RawAddress & peer_addr)748 bool BTM_is_sniff_allowed_for(const RawAddress& peer_addr) {
749   auto conn = internal_.btm_bda_to_acl(peer_addr, BT_TRANSPORT_BR_EDR);
750   if (!conn) {
751     log::warn("Unable to find active acl");
752     return false;
753   }
754   return conn->link_policy & HCI_ENABLE_SNIFF_MODE;
755 }
756 
BTM_unblock_sniff_mode_for(const RawAddress & peer_addr)757 void BTM_unblock_sniff_mode_for(const RawAddress& peer_addr) {
758   btm_toggle_policy_on_for(peer_addr, HCI_ENABLE_SNIFF_MODE);
759 }
760 
BTM_block_sniff_mode_for(const RawAddress & peer_addr)761 void BTM_block_sniff_mode_for(const RawAddress& peer_addr) {
762   btm_toggle_policy_off_for(peer_addr, HCI_ENABLE_SNIFF_MODE);
763 }
764 
BTM_unblock_role_switch_for(const RawAddress & peer_addr)765 void BTM_unblock_role_switch_for(const RawAddress& peer_addr) {
766   btm_toggle_policy_on_for(peer_addr, HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
767 }
768 
BTM_block_role_switch_for(const RawAddress & peer_addr)769 void BTM_block_role_switch_for(const RawAddress& peer_addr) {
770   btm_toggle_policy_off_for(peer_addr, HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
771 }
772 
BTM_unblock_role_switch_and_sniff_mode_for(const RawAddress & peer_addr)773 void BTM_unblock_role_switch_and_sniff_mode_for(const RawAddress& peer_addr) {
774   btm_toggle_policy_on_for(
775       peer_addr, HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
776 }
777 
BTM_block_role_switch_and_sniff_mode_for(const RawAddress & peer_addr)778 void BTM_block_role_switch_and_sniff_mode_for(const RawAddress& peer_addr) {
779   btm_toggle_policy_off_for(
780       peer_addr, HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
781 }
782 
btm_set_default_link_policy(tLINK_POLICY settings)783 void StackAclBtmAcl::btm_set_default_link_policy(tLINK_POLICY settings) {
784   check_link_policy(&settings);
785   btm_cb.acl_cb_.btm_def_link_policy = settings;
786   btsnd_hcic_write_def_policy_set(settings);
787 }
788 
BTM_default_unblock_role_switch()789 void BTM_default_unblock_role_switch() {
790   internal_.btm_set_default_link_policy(btm_cb.acl_cb_.DefaultLinkPolicy() |
791                                         HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
792 }
793 
794 extern void bta_gattc_continue_discovery_if_needed(const RawAddress& bd_addr,
795                                                    uint16_t acl_handle);
796 
797 /*******************************************************************************
798  *
799  * Function         btm_read_remote_version_complete
800  *
801  * Description      This function is called when the command complete message
802  *                  is received from the HCI for the remote version info.
803  *
804  * Returns          void
805  *
806  ******************************************************************************/
maybe_chain_more_commands_after_read_remote_version_complete(uint8_t,uint16_t handle)807 static void maybe_chain_more_commands_after_read_remote_version_complete(
808     uint8_t /* status */, uint16_t handle) {
809   tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
810   if (p_acl_cb == nullptr) {
811     log::warn("Received remote version complete for unknown device");
812     return;
813   }
814 
815   switch (p_acl_cb->transport) {
816     case BT_TRANSPORT_LE:
817       l2cble_notify_le_connection(p_acl_cb->remote_addr);
818       l2cble_use_preferred_conn_params(p_acl_cb->remote_addr);
819       bta_gattc_continue_discovery_if_needed(p_acl_cb->remote_addr,
820                                              p_acl_cb->Handle());
821       break;
822     case BT_TRANSPORT_BR_EDR:
823       /**
824        * When running legacy stack continue chain of executing various
825        * read commands.  Skip when gd_acl is enabled because that
826        * module handles all remote read functionality.
827        */
828       break;
829     default:
830       log::error("Unable to determine transport:{} device:{}",
831                  bt_transport_text(p_acl_cb->transport), p_acl_cb->remote_addr);
832   }
833 
834   // save remote versions to iot conf file
835   btm_iot_save_remote_versions(p_acl_cb);
836 }
837 
btm_process_remote_version_complete(uint8_t status,uint16_t handle,uint8_t lmp_version,uint16_t manufacturer,uint16_t lmp_subversion)838 void btm_process_remote_version_complete(uint8_t status, uint16_t handle,
839                                          uint8_t lmp_version,
840                                          uint16_t manufacturer,
841                                          uint16_t lmp_subversion) {
842   tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
843   if (p_acl_cb == nullptr) {
844     log::warn("Received remote version complete for unknown acl");
845     return;
846   }
847   p_acl_cb->remote_version_received = true;
848 
849   if (status == HCI_SUCCESS) {
850     p_acl_cb->remote_version_info.lmp_version = lmp_version;
851     p_acl_cb->remote_version_info.manufacturer = manufacturer;
852     p_acl_cb->remote_version_info.lmp_subversion = lmp_subversion;
853     p_acl_cb->remote_version_info.valid = true;
854     BTM_update_version_info(p_acl_cb->RemoteAddress(),
855                             p_acl_cb->remote_version_info);
856 
857     bluetooth::common::LogRemoteVersionInfo(handle, status, lmp_version,
858                                             manufacturer, lmp_subversion);
859   } else {
860     bluetooth::common::LogRemoteVersionInfo(handle, status, 0, 0, 0);
861   }
862 }
863 
btm_read_remote_version_complete(tHCI_STATUS status,uint16_t handle,uint8_t lmp_version,uint16_t manufacturer,uint16_t lmp_subversion)864 void btm_read_remote_version_complete(tHCI_STATUS status, uint16_t handle,
865                                       uint8_t lmp_version,
866                                       uint16_t manufacturer,
867                                       uint16_t lmp_subversion) {
868   btm_process_remote_version_complete(status, handle, lmp_version, manufacturer,
869                                       lmp_subversion);
870   maybe_chain_more_commands_after_read_remote_version_complete(status, handle);
871 }
872 
873 /*******************************************************************************
874  *
875  * Function         btm_process_remote_ext_features
876  *
877  * Description      Local function called to process all extended features pages
878  *                  read from a remote device.
879  *
880  * Returns          void
881  *
882  ******************************************************************************/
btm_process_remote_ext_features(tACL_CONN * p_acl_cb,uint8_t max_page_number)883 void btm_process_remote_ext_features(tACL_CONN* p_acl_cb,
884                                      uint8_t max_page_number) {
885   log::assert_that(p_acl_cb != nullptr, "assert failed: p_acl_cb != nullptr");
886   if (!p_acl_cb->peer_lmp_feature_valid[max_page_number]) {
887     log::warn("Checking remote features but remote feature read is incomplete");
888   }
889 
890   bool ssp_supported =
891       HCI_SSP_HOST_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[1]);
892   bool secure_connections_supported =
893       HCI_SC_HOST_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[1]);
894   bool role_switch_supported =
895       HCI_SWITCH_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[0]);
896   bool br_edr_supported =
897       !HCI_BREDR_NOT_SPT_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[0]);
898   bool le_supported =
899       HCI_LE_SPT_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[0]) &&
900       HCI_LE_HOST_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[1]);
901   btm_sec_set_peer_sec_caps(p_acl_cb->hci_handle, ssp_supported,
902                             secure_connections_supported, role_switch_supported,
903                             br_edr_supported, le_supported);
904 }
905 
906 /*******************************************************************************
907  *
908  * Function         btm_read_remote_ext_features
909  *
910  * Description      Local function called to send a read remote extended
911  *                  features
912  *
913  * Returns          void
914  *
915  ******************************************************************************/
btm_read_remote_ext_features(uint16_t handle,uint8_t page_number)916 void btm_read_remote_ext_features(uint16_t handle, uint8_t page_number) {
917   btsnd_hcic_rmt_ext_features(handle, page_number);
918 }
919 
920 /*******************************************************************************
921  *
922  * Function         btm_read_remote_ext_features_complete
923  *
924  * Description      This function is called when the remote extended features
925  *                  complete event is received from the HCI.
926  *
927  * Returns          void
928  *
929  ******************************************************************************/
btm_read_remote_ext_features_complete_raw(uint8_t * p,uint8_t evt_len)930 void btm_read_remote_ext_features_complete_raw(uint8_t* p, uint8_t evt_len) {
931   uint8_t page_num, max_page;
932   uint16_t handle;
933 
934   if (evt_len < HCI_EXT_FEATURES_SUCCESS_EVT_LEN) {
935     log::warn("Remote extended feature length too short. length={}", evt_len);
936     return;
937   }
938 
939   ++p;
940   STREAM_TO_UINT16(handle, p);
941   STREAM_TO_UINT8(page_num, p);
942   STREAM_TO_UINT8(max_page, p);
943 
944   if (max_page > HCI_EXT_FEATURES_PAGE_MAX) {
945     log::warn("Too many max pages read page={} unknown", max_page);
946     return;
947   }
948 
949   if (page_num > HCI_EXT_FEATURES_PAGE_MAX) {
950     log::warn("Too many received pages num_page={} invalid", page_num);
951     return;
952   }
953 
954   if (page_num > max_page) {
955     log::warn("num_page={}, max_page={} invalid", page_num, max_page);
956   }
957 
958   btm_read_remote_ext_features_complete(handle, page_num, max_page, p);
959 }
960 
btm_read_remote_ext_features_complete(uint16_t handle,uint8_t page_num,uint8_t max_page,uint8_t * features)961 void btm_read_remote_ext_features_complete(uint16_t handle, uint8_t page_num,
962                                            uint8_t max_page,
963                                            uint8_t* features) {
964   /* Validate parameters */
965   auto* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
966   if (p_acl_cb == nullptr) {
967     log::warn("Unable to find active acl");
968     return;
969   }
970 
971   /* Copy the received features page */
972   STREAM_TO_ARRAY(p_acl_cb->peer_lmp_feature_pages[page_num], features,
973                   HCI_FEATURE_BYTES_PER_PAGE);
974   p_acl_cb->peer_lmp_feature_valid[page_num] = true;
975 
976   /* save remote extended features to iot conf file */
977   std::string key = IOT_CONF_KEY_RT_EXT_FEATURES "_" + std::to_string(page_num);
978 
979   DEVICE_IOT_CONFIG_ADDR_SET_BIN(p_acl_cb->remote_addr, key,
980                                  p_acl_cb->peer_lmp_feature_pages[page_num],
981                                  BD_FEATURES_LEN);
982 
983   /* If there is the next remote features page and
984    * we have space to keep this page data - read this page */
985   if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX)) {
986     page_num++;
987     log::debug("BTM reads next remote extended features page ({})", page_num);
988     btm_read_remote_ext_features(handle, page_num);
989     return;
990   }
991 
992   /* Reading of remote feature pages is complete */
993   log::debug("BTM reached last remote extended features page ({})", page_num);
994 
995   /* Process the pages */
996   btm_process_remote_ext_features(p_acl_cb, max_page);
997 
998   /* Continue with HCI connection establishment */
999   internal_.btm_establish_continue(p_acl_cb);
1000 }
1001 
1002 /*******************************************************************************
1003  *
1004  * Function         btm_read_remote_ext_features_failed
1005  *
1006  * Description      This function is called when the remote extended features
1007  *                  complete event returns a failed status.
1008  *
1009  * Returns          void
1010  *
1011  ******************************************************************************/
btm_read_remote_ext_features_failed(uint8_t status,uint16_t handle)1012 void btm_read_remote_ext_features_failed(uint8_t status, uint16_t handle) {
1013   log::warn("status 0x{:02x} for handle {}", status, handle);
1014 
1015   tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
1016   if (p_acl_cb == nullptr) {
1017     log::warn("Unable to find active acl");
1018     return;
1019   }
1020 
1021   /* Process supported features only */
1022   btm_process_remote_ext_features(p_acl_cb, 0);
1023 
1024   /* Continue HCI connection establishment */
1025   internal_.btm_establish_continue(p_acl_cb);
1026 }
1027 
1028 /*******************************************************************************
1029  *
1030  * Function         btm_establish_continue
1031  *
1032  * Description      This function is called when the command complete message
1033  *                  is received from the HCI for the read local link policy
1034  *                  request.
1035  *
1036  * Returns          void
1037  *
1038  ******************************************************************************/
btm_establish_continue(tACL_CONN * p_acl)1039 void StackAclBtmAcl::btm_establish_continue(tACL_CONN* p_acl) {
1040   log::assert_that(p_acl != nullptr, "assert failed: p_acl != nullptr");
1041 
1042   if (p_acl->is_transport_br_edr()) {
1043     /* For now there are a some devices that do not like sending */
1044     /* commands events and data at the same time. */
1045     /* Set the packet types to the default allowed by the device */
1046     const uint16_t default_packet_type_mask =
1047         btm_cb.acl_cb_.DefaultPacketTypes();
1048     if (!internal_.change_connection_packet_types(*p_acl,
1049                                                   default_packet_type_mask)) {
1050       log::error(
1051           "Unable to change connection packet type types:{:04x} address:{}",
1052           default_packet_type_mask, p_acl->RemoteAddress());
1053     }
1054     btm_set_link_policy(p_acl, btm_cb.acl_cb_.DefaultLinkPolicy());
1055   }
1056   NotifyAclLinkUp(*p_acl);
1057 }
1058 
btm_establish_continue_from_address(const RawAddress & bda,tBT_TRANSPORT transport)1059 void btm_establish_continue_from_address(const RawAddress& bda,
1060                                          tBT_TRANSPORT transport) {
1061   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bda, transport);
1062   if (p_acl == nullptr) {
1063     log::warn("Unable to find active acl");
1064     return;
1065   }
1066   internal_.btm_establish_continue(p_acl);
1067 }
1068 
1069 /*******************************************************************************
1070  *
1071  * Function         BTM_GetLinkSuperTout
1072  *
1073  * Description      Read the link supervision timeout value of the connection
1074  *
1075  * Returns          status of the operation
1076  *
1077  ******************************************************************************/
BTM_GetLinkSuperTout(const RawAddress & remote_bda,uint16_t * p_timeout)1078 tBTM_STATUS BTM_GetLinkSuperTout(const RawAddress& remote_bda,
1079                                  uint16_t* p_timeout) {
1080   log::assert_that(p_timeout != nullptr, "assert failed: p_timeout != nullptr");
1081   const tACL_CONN* p_acl =
1082       internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1083   if (p_acl == nullptr) {
1084     log::warn("Unable to find active acl");
1085     return BTM_UNKNOWN_ADDR;
1086   }
1087   *p_timeout = p_acl->link_super_tout;
1088   return BTM_SUCCESS;
1089 }
1090 
1091 /*******************************************************************************
1092  *
1093  * Function         BTM_SetLinkSuperTout
1094  *
1095  * Description      Create and send HCI "Write Link Supervision Timeout" command
1096  *
1097  * Returns          status of the operation
1098  *
1099  ******************************************************************************/
BTM_SetLinkSuperTout(const RawAddress & remote_bda,uint16_t timeout)1100 tBTM_STATUS BTM_SetLinkSuperTout(const RawAddress& remote_bda,
1101                                  uint16_t timeout) {
1102   tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1103   if (p_acl == nullptr) {
1104     log::warn("Unable to find active acl");
1105     return BTM_UNKNOWN_ADDR;
1106   }
1107 
1108   /* Only send if current role is Central; 2.0 spec requires this */
1109   if (p_acl->link_role == HCI_ROLE_CENTRAL) {
1110     if (!bluetooth::shim::GetController()->IsSupported(
1111             bluetooth::hci::OpCode::WRITE_LINK_SUPERVISION_TIMEOUT)) {
1112       log::warn(
1113           "UNSUPPORTED by controller write link supervision timeout:{:.2f}ms "
1114           "bd_addr:{}",
1115           supervision_timeout_to_seconds(timeout), remote_bda);
1116       return BTM_MODE_UNSUPPORTED;
1117     }
1118     p_acl->link_super_tout = timeout;
1119     btsnd_hcic_write_link_super_tout(p_acl->hci_handle, timeout);
1120     log::debug("Set supervision timeout:{:.2f}ms bd_addr:{}",
1121                supervision_timeout_to_seconds(timeout), remote_bda);
1122     return BTM_CMD_STARTED;
1123   } else {
1124     log::warn(
1125         "Role is peripheral so unable to set supervision timeout:{:.2f}ms "
1126         "bd_addr:{}",
1127         supervision_timeout_to_seconds(timeout), remote_bda);
1128     return BTM_SUCCESS;
1129   }
1130 }
1131 
BTM_IsAclConnectionUp(const RawAddress & remote_bda,tBT_TRANSPORT transport)1132 bool BTM_IsAclConnectionUp(const RawAddress& remote_bda,
1133                            tBT_TRANSPORT transport) {
1134   return internal_.btm_bda_to_acl(remote_bda, transport) != nullptr;
1135 }
1136 
BTM_IsAclConnectionUpAndHandleValid(const RawAddress & remote_bda,tBT_TRANSPORT transport)1137 bool BTM_IsAclConnectionUpAndHandleValid(const RawAddress& remote_bda,
1138                                          tBT_TRANSPORT transport) {
1139   tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, transport);
1140   if (p_acl == nullptr) {
1141     return false;
1142   }
1143   return p_acl->hci_handle != HCI_INVALID_HANDLE;
1144 }
1145 
1146 /*******************************************************************************
1147  *
1148  * Function         BTM_GetNumAclLinks
1149  *
1150  * Description      This function is called to count the number of
1151  *                  ACL links that are active.
1152  *
1153  * Returns          uint16_t Number of active ACL links
1154  *
1155  ******************************************************************************/
BTM_GetNumAclLinks(void)1156 uint16_t BTM_GetNumAclLinks(void) {
1157   return static_cast<uint16_t>(btm_cb.acl_cb_.NumberOfActiveLinks());
1158 }
1159 
1160 /*******************************************************************************
1161  *
1162  * Function         btm_get_acl_disc_reason_code
1163  *
1164  * Description      This function is called to get the disconnection reason code
1165  *                  returned by the HCI at disconnection complete event.
1166  *
1167  * Returns          true if connection is up, else false.
1168  *
1169  ******************************************************************************/
btm_get_acl_disc_reason_code(void)1170 tHCI_REASON btm_get_acl_disc_reason_code(void) {
1171   return btm_cb.acl_cb_.get_disconnect_reason();
1172 }
1173 
1174 /*******************************************************************************
1175  *
1176  * Function         btm_is_acl_locally_initiated
1177  *
1178  * Description      This function is called to get which side initiates the
1179  *                  connection, at HCI connection complete event.
1180  *
1181  * Returns          true if connection is locally initiated, else false.
1182  *
1183  ******************************************************************************/
btm_is_acl_locally_initiated(void)1184 bool btm_is_acl_locally_initiated(void) {
1185   return btm_cb.acl_cb_.is_locally_initiated();
1186 }
1187 
1188 /*******************************************************************************
1189  *
1190  * Function         BTM_GetHCIConnHandle
1191  *
1192  * Description      This function is called to get the handle for an ACL
1193  *                  connection to a specific remote BD Address.
1194  *
1195  * Returns          the handle of the connection, or HCI_INVALID_HANDLE if none.
1196  *
1197  ******************************************************************************/
BTM_GetHCIConnHandle(const RawAddress & remote_bda,tBT_TRANSPORT transport)1198 uint16_t BTM_GetHCIConnHandle(const RawAddress& remote_bda,
1199                               tBT_TRANSPORT transport) {
1200   tACL_CONN* p;
1201   p = internal_.btm_bda_to_acl(remote_bda, transport);
1202   if (p != (tACL_CONN*)NULL) {
1203     return (p->hci_handle);
1204   }
1205 
1206   /* If here, no BD Addr found */
1207   return HCI_INVALID_HANDLE;
1208 }
1209 
1210 /*******************************************************************************
1211  *
1212  * Function         BTM_IsPhy2mSupported
1213  *
1214  * Description      This function is called to check PHY 2M support
1215  *                  from peer device
1216  * Returns          True when PHY 2M supported false otherwise
1217  *
1218  ******************************************************************************/
BTM_IsPhy2mSupported(const RawAddress & remote_bda,tBT_TRANSPORT transport)1219 bool BTM_IsPhy2mSupported(const RawAddress& remote_bda, tBT_TRANSPORT transport) {
1220   tACL_CONN* p;
1221   log::verbose("BTM_IsPhy2mSupported");
1222   p = internal_.btm_bda_to_acl(remote_bda, transport);
1223   if (p == (tACL_CONN*)NULL) {
1224     log::verbose("BTM_IsPhy2mSupported: no connection");
1225     return false;
1226   }
1227 
1228   if (!p->peer_le_features_valid) {
1229     log::warn("Checking remote features but remote feature read is incomplete");
1230   }
1231   return HCI_LE_2M_PHY_SUPPORTED(p->peer_le_features);
1232 }
1233 
1234 /*******************************************************************************
1235  *
1236  * Function         BTM_RequestPeerSCA
1237  *
1238  * Description      This function is called to request sleep clock accuracy
1239  *                  from peer device
1240  *
1241  ******************************************************************************/
BTM_RequestPeerSCA(const RawAddress & remote_bda,tBT_TRANSPORT transport)1242 void BTM_RequestPeerSCA(const RawAddress& remote_bda, tBT_TRANSPORT transport) {
1243   tACL_CONN* p;
1244   p = internal_.btm_bda_to_acl(remote_bda, transport);
1245   if (p == (tACL_CONN*)NULL) {
1246     log::warn("Unable to find active acl");
1247     return;
1248   }
1249 
1250   btsnd_hcic_req_peer_sca(p->hci_handle);
1251 }
1252 
1253 /*******************************************************************************
1254  *
1255  * Function         BTM_GetPeerSCA
1256  *
1257  * Description      This function is called to get peer sleep clock accuracy
1258  *
1259  * Returns          SCA or 0xFF if SCA was never previously requested, request
1260  *                  is not supported by peer device or ACL does not exist
1261  *
1262  ******************************************************************************/
BTM_GetPeerSCA(const RawAddress & remote_bda,tBT_TRANSPORT transport)1263 uint8_t BTM_GetPeerSCA(const RawAddress& remote_bda, tBT_TRANSPORT transport) {
1264   tACL_CONN* p;
1265   p = internal_.btm_bda_to_acl(remote_bda, transport);
1266   if (p != (tACL_CONN*)NULL) {
1267     return (p->sca);
1268   }
1269   log::warn("Unable to find active acl");
1270 
1271   /* If here, no BD Addr found */
1272   return (0xFF);
1273 }
1274 
1275 /*******************************************************************************
1276  *
1277  * Function         btm_rejectlist_role_change_device
1278  *
1279  * Description      This function is used to rejectlist the device if the role
1280  *                  switch fails for maximum number of times. It also removes
1281  *                  the device from the black list if the role switch succeeds.
1282  *
1283  * Input Parms      bd_addr - remote BD addr
1284  *                  hci_status - role switch status
1285  *
1286  * Returns          void
1287  *
1288  *******************************************************************************/
btm_rejectlist_role_change_device(const RawAddress & bd_addr,uint8_t hci_status)1289 void btm_rejectlist_role_change_device(const RawAddress& bd_addr,
1290                                        uint8_t hci_status) {
1291   tACL_CONN* p = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
1292 
1293   if (!p) {
1294     log::warn("Unable to find active acl");
1295     return;
1296   }
1297   if (hci_status == HCI_SUCCESS) {
1298     p->switch_role_failed_attempts = 0;
1299     return;
1300   }
1301 
1302   /* check for carkits */
1303   const uint32_t cod_audio_device =
1304       (BTM_COD_SERVICE_AUDIO | BTM_COD_MAJOR_AUDIO) << 8;
1305   DEV_CLASS dev_class = btm_get_dev_class(bd_addr);
1306   if (dev_class == kDevClassEmpty) return;
1307   const uint32_t cod =
1308       ((dev_class[0] << 16) | (dev_class[1] << 8) | dev_class[2]) & 0xffffff;
1309   if ((hci_status != HCI_SUCCESS) &&
1310       (p->is_switch_role_switching_or_in_progress()) &&
1311       ((cod & cod_audio_device) == cod_audio_device) &&
1312       (!interop_match_addr(INTEROP_DYNAMIC_ROLE_SWITCH, &bd_addr))) {
1313     p->switch_role_failed_attempts++;
1314     if (p->switch_role_failed_attempts == BTM_MAX_SW_ROLE_FAILED_ATTEMPTS) {
1315       log::warn(
1316           "Device {} rejectlisted for role switching - multiple role switch "
1317           "failed attempts: {}",
1318           bd_addr, p->switch_role_failed_attempts);
1319       interop_database_add(INTEROP_DYNAMIC_ROLE_SWITCH, &bd_addr, 3);
1320     }
1321   }
1322 }
1323 
1324 /*******************************************************************************
1325  *
1326  * Function         acl_cache_role
1327  *
1328  * Description      This function caches the role of the device associated
1329  *                  with the given address. This happens if we get a role change
1330  *                  before connection complete. The cached role is propagated
1331  *                  when ACL Link is created.
1332  *
1333  * Returns          void
1334  *
1335  ******************************************************************************/
1336 
acl_cache_role(const RawAddress & bd_addr,tHCI_ROLE new_role,bool overwrite_cache)1337 void acl_cache_role(const RawAddress& bd_addr, tHCI_ROLE new_role,
1338                     bool overwrite_cache) {
1339   if (overwrite_cache || delayed_role_change_ == nullptr) {
1340     RoleChangeView role_change;
1341     role_change.new_role = new_role;
1342     role_change.bd_addr = bd_addr;
1343     delayed_role_change_ =
1344         std::make_unique<RoleChangeView>(std::move(role_change));
1345   }
1346 }
1347 
1348 /*******************************************************************************
1349  *
1350  * Function         btm_acl_role_changed
1351  *
1352  * Description      This function is called whan a link's central/peripheral
1353  *role change event or command status event (with error) is received. It updates
1354  *the link control block, and calls the registered callback with status and role
1355  *(if registered).
1356  *
1357  * Returns          void
1358  *
1359  ******************************************************************************/
btm_acl_role_changed(tHCI_STATUS hci_status,const RawAddress & bd_addr,tHCI_ROLE new_role)1360 void StackAclBtmAcl::btm_acl_role_changed(tHCI_STATUS hci_status,
1361                                           const RawAddress& bd_addr,
1362                                           tHCI_ROLE new_role) {
1363   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
1364   if (p_acl == nullptr) {
1365     // If we get a role change before connection complete, we cache the new
1366     // role here and then propagate it when ACL Link is created.
1367     acl_cache_role(bd_addr, new_role, /*overwrite_cache=*/true);
1368     log::warn("Unable to find active acl");
1369     return;
1370   }
1371 
1372   tBTM_ROLE_SWITCH_CMPL* p_switch_role = &btm_cb.acl_cb_.switch_role_ref_data;
1373   log::debug("Role change event received peer:{} hci_status:{} new_role:{}",
1374              bd_addr, hci_error_code_text(hci_status), RoleText(new_role));
1375 
1376   p_switch_role->hci_status = hci_status;
1377   if (hci_status == HCI_SUCCESS) {
1378     p_switch_role->role = new_role;
1379     p_switch_role->remote_bd_addr = bd_addr;
1380 
1381     /* Update cached value */
1382     p_acl->link_role = new_role;
1383 
1384     /* Reload LSTO: link supervision timeout is reset in the LM after a role
1385      * switch */
1386     if (new_role == HCI_ROLE_CENTRAL) {
1387       uint16_t link_supervision_timeout =
1388           osi_property_get_int32(PROPERTY_LINK_SUPERVISION_TIMEOUT, 8000);
1389       BTM_SetLinkSuperTout(bd_addr, link_supervision_timeout);
1390     }
1391   } else {
1392     new_role = p_acl->link_role;
1393   }
1394 
1395   /* Check if any SCO req is pending for role change */
1396   btm_sco_chk_pend_rolechange(p_acl->hci_handle);
1397 
1398   /* if switching state is switching we need to turn encryption on */
1399   /* if idle, we did not change encryption */
1400   if (p_acl->is_switch_role_switching()) {
1401     p_acl->set_encryption_on();
1402     p_acl->set_switch_role_encryption_on();
1403     return;
1404   }
1405 
1406   /* Set the switch_role_state to IDLE since the reply received from HCI */
1407   /* regardless of its result either success or failed. */
1408   if (p_acl->is_switch_role_in_progress()) {
1409     p_acl->set_encryption_idle();
1410     p_acl->reset_switch_role();
1411   }
1412 
1413   BTA_dm_report_role_change(bd_addr, new_role, hci_status);
1414   btm_sec_role_changed(hci_status, bd_addr, new_role);
1415 
1416   /* If a disconnect is pending, issue it now that role switch has completed */
1417   if (p_acl->rs_disc_pending == BTM_SEC_DISC_PENDING) {
1418     disconnect_acl(*p_acl, HCI_ERR_PEER_USER,
1419                    "stack::acl::btm_acl::role after role switch");
1420   }
1421   p_acl->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
1422 }
1423 
btm_acl_role_changed(tHCI_STATUS hci_status,const RawAddress & bd_addr,tHCI_ROLE new_role)1424 void btm_acl_role_changed(tHCI_STATUS hci_status, const RawAddress& bd_addr,
1425                           tHCI_ROLE new_role) {
1426   btm_rejectlist_role_change_device(bd_addr, hci_status);
1427 
1428   if (hci_status == HCI_SUCCESS) {
1429     l2c_link_role_changed(&bd_addr, new_role, hci_status);
1430   } else {
1431     l2c_link_role_changed(nullptr, HCI_ROLE_UNKNOWN,
1432                           HCI_ERR_COMMAND_DISALLOWED);
1433   }
1434   internal_.btm_acl_role_changed(hci_status, bd_addr, new_role);
1435 }
1436 
1437 /*******************************************************************************
1438  *
1439  * Function         change_connection_packet_types
1440  *
1441  * Description      This function sets the packet types used for a specific
1442  *                  ACL connection. It is called internally by btm_acl_created
1443  *                  or by an application/profile by BTM_SetPacketTypes.
1444  *
1445  * Returns          status of the operation
1446  *
1447  ******************************************************************************/
change_connection_packet_types(tACL_CONN & link,const uint16_t new_packet_type_mask)1448 bool StackAclBtmAcl::change_connection_packet_types(
1449     tACL_CONN& link, const uint16_t new_packet_type_mask) {
1450   // Start with the default configured packet types
1451   const uint16_t default_packet_type_mask = btm_cb.acl_cb_.DefaultPacketTypes();
1452 
1453   uint16_t packet_type_mask =
1454       default_packet_type_mask &
1455       (new_packet_type_mask & BTM_ACL_SUPPORTED_PKTS_MASK);
1456 
1457   /* OR in any exception packet types if at least 2.0 version of spec */
1458   packet_type_mask |=
1459       ((new_packet_type_mask & BTM_ACL_EXCEPTION_PKTS_MASK) |
1460        (BTM_ACL_EXCEPTION_PKTS_MASK & default_packet_type_mask));
1461 
1462   /* Exclude packet types not supported by the peer */
1463   if (link.peer_lmp_feature_valid[0]) {
1464     PeerPacketTypes peer_packet_types(link.peer_lmp_feature_pages[0]);
1465     packet_type_mask &= peer_packet_types.acl.supported;
1466     packet_type_mask |= peer_packet_types.acl.unsupported;
1467   } else {
1468     log::info(
1469         "Unable to include remote supported packet types as read feature "
1470         "incomplete");
1471     log::info("TIP: Maybe wait until read feature complete beforehand");
1472   }
1473 
1474   if (packet_type_mask == 0) {
1475     log::warn("Unable to send controller illegal change packet mask:0x{:04x}",
1476               packet_type_mask);
1477     return false;
1478   }
1479 
1480   link.pkt_types_mask = packet_type_mask;
1481   GetInterface().ChangeConnectionPacketType(link.Handle(), link.pkt_types_mask);
1482   log::debug("Started change connection packet type:0x{:04x} address:{}",
1483              link.pkt_types_mask, link.RemoteAddress());
1484   return true;
1485 }
1486 
btm_set_packet_types_from_address(const RawAddress & bd_addr,uint16_t pkt_types)1487 void btm_set_packet_types_from_address(const RawAddress& bd_addr,
1488                                        uint16_t pkt_types) {
1489   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
1490   if (p_acl == nullptr) {
1491     log::warn("Unable to find active acl");
1492     return;
1493   }
1494 
1495   if (!internal_.change_connection_packet_types(*p_acl, pkt_types)) {
1496     log::error(
1497         "Unable to change connection packet type types:{:04x} address:{}",
1498         pkt_types, bd_addr);
1499   }
1500 }
1501 
1502 /*******************************************************************************
1503  *
1504  * Function         BTM_GetMaxPacketSize
1505  *
1506  * Returns          Returns maximum packet size that can be used for current
1507  *                  connection, 0 if connection is not established
1508  *
1509  ******************************************************************************/
BTM_GetMaxPacketSize(const RawAddress & addr)1510 uint16_t BTM_GetMaxPacketSize(const RawAddress& addr) {
1511   tACL_CONN* p = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1512   uint16_t pkt_types = 0;
1513   uint16_t pkt_size = 0;
1514   if (p != NULL) {
1515     pkt_types = p->pkt_types_mask;
1516   } else {
1517     /* Special case for when info for the local device is requested */
1518     if (addr == bluetooth::ToRawAddress(
1519                     bluetooth::shim::GetController()->GetMacAddress())) {
1520       pkt_types = btm_cb.acl_cb_.DefaultPacketTypes();
1521     }
1522   }
1523 
1524   if (pkt_types) {
1525     if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_3_DH5))
1526       pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
1527     else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_2_DH5))
1528       pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
1529     else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_3_DH3))
1530       pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
1531     else if (pkt_types & HCI_PKT_TYPES_MASK_DH5)
1532       pkt_size = HCI_DH5_PACKET_SIZE;
1533     else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_2_DH3))
1534       pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
1535     else if (pkt_types & HCI_PKT_TYPES_MASK_DM5)
1536       pkt_size = HCI_DM5_PACKET_SIZE;
1537     else if (pkt_types & HCI_PKT_TYPES_MASK_DH3)
1538       pkt_size = HCI_DH3_PACKET_SIZE;
1539     else if (pkt_types & HCI_PKT_TYPES_MASK_DM3)
1540       pkt_size = HCI_DM3_PACKET_SIZE;
1541     else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_3_DH1))
1542       pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
1543     else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_2_DH1))
1544       pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
1545     else if (pkt_types & HCI_PKT_TYPES_MASK_DH1)
1546       pkt_size = HCI_DH1_PACKET_SIZE;
1547     else if (pkt_types & HCI_PKT_TYPES_MASK_DM1)
1548       pkt_size = HCI_DM1_PACKET_SIZE;
1549   }
1550 
1551   return (pkt_size);
1552 }
1553 
1554 /*******************************************************************************
1555  *
1556  * Function         BTM_IsRemoteVersionReceived
1557  *
1558  * Returns          Returns true if "LE Read remote version info" was already
1559  *                  received on LE transport for this device.
1560  *
1561  ******************************************************************************/
BTM_IsRemoteVersionReceived(const RawAddress & addr)1562 bool BTM_IsRemoteVersionReceived(const RawAddress& addr) {
1563   const tACL_CONN* p_acl = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_LE);
1564   if (p_acl == nullptr) {
1565     return false;
1566   }
1567 
1568   return p_acl->remote_version_received;
1569 }
1570 
1571 /*******************************************************************************
1572  *
1573  * Function         BTM_ReadRemoteVersion
1574  *
1575  * Returns          If connected report peer device info
1576  *
1577  ******************************************************************************/
BTM_ReadRemoteVersion(const RawAddress & addr,uint8_t * lmp_version,uint16_t * manufacturer,uint16_t * lmp_sub_version)1578 bool BTM_ReadRemoteVersion(const RawAddress& addr, uint8_t* lmp_version,
1579                            uint16_t* manufacturer, uint16_t* lmp_sub_version) {
1580   const tACL_CONN* p_acl = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1581   if (p_acl == nullptr) {
1582     p_acl = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_LE);
1583     if (p_acl == nullptr) {
1584       log::warn("Unable to find active acl");
1585       return false;
1586     }
1587   }
1588 
1589   if (!p_acl->remote_version_info.valid) {
1590     log::warn("Remote version information is invalid");
1591     return false;
1592   }
1593 
1594   if (lmp_version) *lmp_version = p_acl->remote_version_info.lmp_version;
1595 
1596   if (manufacturer) *manufacturer = p_acl->remote_version_info.manufacturer;
1597 
1598   if (lmp_sub_version)
1599     *lmp_sub_version = p_acl->remote_version_info.lmp_subversion;
1600 
1601   return true;
1602 }
1603 
1604 /*******************************************************************************
1605  *
1606  * Function         BTM_ReadRemoteFeatures
1607  *
1608  * Returns          pointer to the remote supported features mask (8 bytes)
1609  *
1610  ******************************************************************************/
BTM_ReadRemoteFeatures(const RawAddress & addr)1611 uint8_t* BTM_ReadRemoteFeatures(const RawAddress& addr) {
1612   tACL_CONN* p = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1613   if (p == NULL) {
1614     log::warn("Unable to find active acl");
1615     return (NULL);
1616   }
1617 
1618   return (p->peer_lmp_feature_pages[0]);
1619 }
1620 
1621 /*******************************************************************************
1622  *
1623  * Function         BTM_ReadRSSI
1624  *
1625  * Description      This function is called to read the link policy settings.
1626  *                  The address of link policy results are returned in the
1627  *                  callback.
1628  *                  (tBTM_RSSI_RESULT)
1629  *
1630  * Returns          BTM_CMD_STARTED if successfully initiated or error code
1631  *
1632  ******************************************************************************/
BTM_ReadRSSI(const RawAddress & remote_bda,tBTM_CMPL_CB * p_cb)1633 tBTM_STATUS BTM_ReadRSSI(const RawAddress& remote_bda, tBTM_CMPL_CB* p_cb) {
1634   tACL_CONN* p = NULL;
1635   tBT_DEVICE_TYPE dev_type;
1636   tBLE_ADDR_TYPE addr_type;
1637 
1638   /* If someone already waiting on the version, do not allow another */
1639   if (btm_cb.devcb.p_rssi_cmpl_cb) return (BTM_BUSY);
1640 
1641   BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
1642 
1643   if (dev_type & BT_DEVICE_TYPE_BLE) {
1644     p = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
1645   }
1646 
1647   if (p == NULL && dev_type & BT_DEVICE_TYPE_BREDR) {
1648     p = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1649   }
1650 
1651   if (p) {
1652     btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
1653     alarm_set_on_mloop(btm_cb.devcb.read_rssi_timer, BTM_DEV_REPLY_TIMEOUT_MS,
1654                        btm_read_rssi_timeout, NULL);
1655 
1656     btsnd_hcic_read_rssi(p->hci_handle);
1657     return (BTM_CMD_STARTED);
1658   }
1659   log::warn("Unable to find active acl");
1660 
1661   /* If here, no BD Addr found */
1662   return (BTM_UNKNOWN_ADDR);
1663 }
1664 
1665 /*******************************************************************************
1666  *
1667  * Function         BTM_ReadFailedContactCounter
1668  *
1669  * Description      This function is called to read the failed contact counter.
1670  *                  The result is returned in the callback.
1671  *                  (tBTM_FAILED_CONTACT_COUNTER_RESULT)
1672  *
1673  * Returns          BTM_CMD_STARTED if successfully initiated or error code
1674  *
1675  ******************************************************************************/
BTM_ReadFailedContactCounter(const RawAddress & remote_bda,tBTM_CMPL_CB * p_cb)1676 tBTM_STATUS BTM_ReadFailedContactCounter(const RawAddress& remote_bda,
1677                                          tBTM_CMPL_CB* p_cb) {
1678   tACL_CONN* p;
1679   tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
1680   tBT_DEVICE_TYPE dev_type;
1681   tBLE_ADDR_TYPE addr_type;
1682 
1683   /* If someone already waiting on the result, do not allow another */
1684   if (btm_cb.devcb.p_failed_contact_counter_cmpl_cb) return (BTM_BUSY);
1685 
1686   BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
1687   if (dev_type == BT_DEVICE_TYPE_BLE) transport = BT_TRANSPORT_LE;
1688 
1689   p = internal_.btm_bda_to_acl(remote_bda, transport);
1690   if (p != (tACL_CONN*)NULL) {
1691     btm_cb.devcb.p_failed_contact_counter_cmpl_cb = p_cb;
1692     alarm_set_on_mloop(btm_cb.devcb.read_failed_contact_counter_timer,
1693                        BTM_DEV_REPLY_TIMEOUT_MS,
1694                        btm_read_failed_contact_counter_timeout, NULL);
1695 
1696     btsnd_hcic_read_failed_contact_counter(p->hci_handle);
1697     return (BTM_CMD_STARTED);
1698   }
1699   log::warn("Unable to find active acl");
1700 
1701   /* If here, no BD Addr found */
1702   return (BTM_UNKNOWN_ADDR);
1703 }
1704 
1705 /*******************************************************************************
1706  *
1707  * Function         BTM_ReadTxPower
1708  *
1709  * Description      This function is called to read the current
1710  *                  TX power of the connection. The tx power level results
1711  *                  are returned in the callback.
1712  *                  (tBTM_RSSI_RESULT)
1713  *
1714  * Returns          BTM_CMD_STARTED if successfully initiated or error code
1715  *
1716  ******************************************************************************/
BTM_ReadTxPower(const RawAddress & remote_bda,tBT_TRANSPORT transport,tBTM_CMPL_CB * p_cb)1717 tBTM_STATUS BTM_ReadTxPower(const RawAddress& remote_bda,
1718                             tBT_TRANSPORT transport, tBTM_CMPL_CB* p_cb) {
1719   tACL_CONN* p;
1720 #define BTM_READ_RSSI_TYPE_CUR 0x00
1721 #define BTM_READ_RSSI_TYPE_MAX 0X01
1722 
1723   log::verbose("RemBdAddr: {}", remote_bda);
1724 
1725   /* If someone already waiting on the version, do not allow another */
1726   if (btm_cb.devcb.p_tx_power_cmpl_cb) return (BTM_BUSY);
1727 
1728   p = internal_.btm_bda_to_acl(remote_bda, transport);
1729   if (p != (tACL_CONN*)NULL) {
1730     btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
1731     alarm_set_on_mloop(btm_cb.devcb.read_tx_power_timer,
1732                        BTM_DEV_REPLY_TIMEOUT_MS, btm_read_tx_power_timeout,
1733                        NULL);
1734 
1735     if (p->transport == BT_TRANSPORT_LE) {
1736       btm_cb.devcb.read_tx_pwr_addr = remote_bda;
1737       btsnd_hcic_ble_read_adv_chnl_tx_power();
1738     } else {
1739       btsnd_hcic_read_tx_power(p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
1740     }
1741 
1742     return (BTM_CMD_STARTED);
1743   }
1744 
1745   log::warn("Unable to find active acl");
1746 
1747   /* If here, no BD Addr found */
1748   return (BTM_UNKNOWN_ADDR);
1749 }
1750 
1751 /*******************************************************************************
1752  *
1753  * Function         btm_read_tx_power_timeout
1754  *
1755  * Description      Callback when reading the tx power times out.
1756  *
1757  * Returns          void
1758  *
1759  ******************************************************************************/
btm_read_tx_power_timeout(void *)1760 void btm_read_tx_power_timeout(void* /* data */) {
1761   tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
1762   btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
1763   if (p_cb) (*p_cb)((void*)NULL);
1764 }
1765 
1766 /*******************************************************************************
1767  *
1768  * Function         btm_read_tx_power_complete
1769  *
1770  * Description      This function is called when the command complete message
1771  *                  is received from the HCI for the read tx power request.
1772  *
1773  * Returns          void
1774  *
1775  ******************************************************************************/
btm_read_tx_power_complete(uint8_t * p,uint16_t evt_len,bool is_ble)1776 void btm_read_tx_power_complete(uint8_t* p, uint16_t evt_len, bool is_ble) {
1777   tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
1778   tBTM_TX_POWER_RESULT result;
1779 
1780   alarm_cancel(btm_cb.devcb.read_tx_power_timer);
1781   btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
1782 
1783   /* If there was a registered callback, call it */
1784   if (p_cb) {
1785     if (evt_len < 1) {
1786       goto err_out;
1787     }
1788 
1789     STREAM_TO_UINT8(result.hci_status, p);
1790 
1791     if (result.hci_status == HCI_SUCCESS) {
1792       result.status = BTM_SUCCESS;
1793 
1794       if (!is_ble) {
1795         uint16_t handle;
1796 
1797         if (evt_len < 4) {
1798           goto err_out;
1799         }
1800 
1801         STREAM_TO_UINT16(handle, p);
1802         STREAM_TO_UINT8(result.tx_power, p);
1803 
1804         tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
1805         if (p_acl_cb != nullptr) {
1806           result.rem_bda = p_acl_cb->remote_addr;
1807         }
1808       } else {
1809         if (evt_len < 2) {
1810           goto err_out;
1811         }
1812 
1813         STREAM_TO_UINT8(result.tx_power, p);
1814         result.rem_bda = btm_cb.devcb.read_tx_pwr_addr;
1815       }
1816       log::debug(
1817           "Transmit power complete: tx_power:{} hci status:{}", result.tx_power,
1818           hci_error_code_text(static_cast<tHCI_STATUS>(result.hci_status)));
1819     } else {
1820       result.status = BTM_ERR_PROCESSING;
1821     }
1822 
1823     (*p_cb)(&result);
1824   }
1825 
1826   return;
1827 
1828  err_out:
1829    log::error("Bogus event packet, too short");
1830 }
1831 
1832 /*******************************************************************************
1833  *
1834  * Function         btm_read_rssi_timeout
1835  *
1836  * Description      Callback when reading the RSSI times out.
1837  *
1838  * Returns          void
1839  *
1840  ******************************************************************************/
btm_read_rssi_timeout(void *)1841 void btm_read_rssi_timeout(void* /* data */) {
1842   tBTM_RSSI_RESULT result;
1843   tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
1844   btm_cb.devcb.p_rssi_cmpl_cb = NULL;
1845   result.status = BTM_DEVICE_TIMEOUT;
1846   if (p_cb) (*p_cb)(&result);
1847 }
1848 
1849 /*******************************************************************************
1850  *
1851  * Function         btm_read_rssi_complete
1852  *
1853  * Description      This function is called when the command complete message
1854  *                  is received from the HCI for the read rssi request.
1855  *
1856  * Returns          void
1857  *
1858  ******************************************************************************/
btm_read_rssi_complete(uint8_t * p,uint16_t evt_len)1859 void btm_read_rssi_complete(uint8_t* p, uint16_t evt_len) {
1860   tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
1861   tBTM_RSSI_RESULT result;
1862 
1863   alarm_cancel(btm_cb.devcb.read_rssi_timer);
1864   btm_cb.devcb.p_rssi_cmpl_cb = NULL;
1865 
1866   /* If there was a registered callback, call it */
1867   if (p_cb) {
1868     if (evt_len < 1) {
1869       goto err_out;
1870     }
1871 
1872     STREAM_TO_UINT8(result.hci_status, p);
1873     result.status = BTM_ERR_PROCESSING;
1874 
1875     if (result.hci_status == HCI_SUCCESS) {
1876       uint16_t handle;
1877 
1878       if (evt_len < 4) {
1879         goto err_out;
1880       }
1881       STREAM_TO_UINT16(handle, p);
1882 
1883       STREAM_TO_UINT8(result.rssi, p);
1884       log::debug("Read rrsi complete rssi:{} hci status:{}", result.rssi,
1885                  hci_status_code_text(to_hci_status_code(result.hci_status)));
1886 
1887       tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
1888       if (p_acl_cb != nullptr) {
1889         result.rem_bda = p_acl_cb->remote_addr;
1890         result.status = BTM_SUCCESS;
1891       }
1892     }
1893     (*p_cb)(&result);
1894   }
1895 
1896   return;
1897 
1898  err_out:
1899    log::error("Bogus event packet, too short");
1900 }
1901 
1902 /*******************************************************************************
1903  *
1904  * Function         btm_read_failed_contact_counter_timeout
1905  *
1906  * Description      Callback when reading the failed contact counter times out.
1907  *
1908  * Returns          void
1909  *
1910  ******************************************************************************/
btm_read_failed_contact_counter_timeout(void *)1911 void btm_read_failed_contact_counter_timeout(void* /* data */) {
1912   tBTM_FAILED_CONTACT_COUNTER_RESULT result;
1913   tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_failed_contact_counter_cmpl_cb;
1914   btm_cb.devcb.p_failed_contact_counter_cmpl_cb = NULL;
1915   result.status = BTM_DEVICE_TIMEOUT;
1916   if (p_cb) (*p_cb)(&result);
1917 }
1918 
1919 /*******************************************************************************
1920  *
1921  * Function         btm_read_failed_contact_counter_complete
1922  *
1923  * Description      This function is called when the command complete message
1924  *                  is received from the HCI for the read failed contact
1925  *                  counter request.
1926  *
1927  * Returns          void
1928  *
1929  ******************************************************************************/
btm_read_failed_contact_counter_complete(uint8_t * p)1930 void btm_read_failed_contact_counter_complete(uint8_t* p) {
1931   tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_failed_contact_counter_cmpl_cb;
1932   tBTM_FAILED_CONTACT_COUNTER_RESULT result;
1933 
1934   alarm_cancel(btm_cb.devcb.read_failed_contact_counter_timer);
1935   btm_cb.devcb.p_failed_contact_counter_cmpl_cb = NULL;
1936 
1937   /* If there was a registered callback, call it */
1938   if (p_cb) {
1939     uint16_t handle;
1940     STREAM_TO_UINT8(result.hci_status, p);
1941 
1942     if (result.hci_status == HCI_SUCCESS) {
1943       result.status = BTM_SUCCESS;
1944 
1945       STREAM_TO_UINT16(handle, p);
1946 
1947       STREAM_TO_UINT16(result.failed_contact_counter, p);
1948       log::debug("Failed contact counter complete: counter {}, hci status:{}",
1949                  result.failed_contact_counter,
1950                  hci_status_code_text(to_hci_status_code(result.hci_status)));
1951 
1952       tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
1953       if (p_acl_cb != nullptr) {
1954         result.rem_bda = p_acl_cb->remote_addr;
1955       }
1956     } else {
1957       result.status = BTM_ERR_PROCESSING;
1958     }
1959 
1960     (*p_cb)(&result);
1961   }
1962 }
1963 
1964 /*******************************************************************************
1965  *
1966  * Function         btm_read_automatic_flush_timeout_complete
1967  *
1968  * Description      This function is called when the command complete message
1969  *                  is received from the HCI for the read automatic flush
1970  *                  timeout request.
1971  *
1972  * Returns          void
1973  *
1974  ******************************************************************************/
btm_read_automatic_flush_timeout_complete(uint8_t * p)1975 void btm_read_automatic_flush_timeout_complete(uint8_t* p) {
1976   tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_automatic_flush_timeout_cmpl_cb;
1977   tBTM_AUTOMATIC_FLUSH_TIMEOUT_RESULT result;
1978 
1979   alarm_cancel(btm_cb.devcb.read_automatic_flush_timeout_timer);
1980   btm_cb.devcb.p_automatic_flush_timeout_cmpl_cb = nullptr;
1981 
1982   /* If there was a registered callback, call it */
1983   if (p_cb) {
1984     uint16_t handle;
1985     STREAM_TO_UINT8(result.hci_status, p);
1986     result.status = BTM_ERR_PROCESSING;
1987 
1988     if (result.hci_status == HCI_SUCCESS) {
1989       result.status = BTM_SUCCESS;
1990 
1991       STREAM_TO_UINT16(handle, p);
1992       STREAM_TO_UINT16(result.automatic_flush_timeout, p);
1993       log::debug(
1994           "Read automatic flush timeout complete timeout:{} hci_status:{}",
1995           result.automatic_flush_timeout,
1996           hci_error_code_text(static_cast<tHCI_STATUS>(result.hci_status)));
1997 
1998       tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
1999       if (p_acl_cb != nullptr) {
2000         result.rem_bda = p_acl_cb->remote_addr;
2001       }
2002     }
2003     (*p_cb)(&result);
2004   }
2005 }
2006 
2007 /*******************************************************************************
2008  *
2009  * Function         btm_remove_acl
2010  *
2011  * Description      This function is called to disconnect an ACL connection
2012  *
2013  * Returns          BTM_SUCCESS if successfully initiated, otherwise
2014  *                  BTM_UNKNOWN_ADDR.
2015  *
2016  ******************************************************************************/
btm_remove_acl(const RawAddress & bd_addr,tBT_TRANSPORT transport)2017 tBTM_STATUS btm_remove_acl(const RawAddress& bd_addr, tBT_TRANSPORT transport) {
2018   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, transport);
2019   if (p_acl == nullptr) {
2020     log::warn("Unable to find active acl");
2021     return BTM_UNKNOWN_ADDR;
2022   }
2023 
2024   if (p_acl->Handle() == HCI_INVALID_HANDLE) {
2025     log::warn("Cannot remove unknown acl bd_addr:{} transport:{}", bd_addr,
2026               bt_transport_text(transport));
2027     return BTM_UNKNOWN_ADDR;
2028   }
2029 
2030   if (p_acl->rs_disc_pending == BTM_SEC_RS_PENDING) {
2031     log::debug(
2032         "Delay disconnect until role switch is complete bd_addr:{} "
2033         "transport:{}",
2034         bd_addr, bt_transport_text(transport));
2035     p_acl->rs_disc_pending = BTM_SEC_DISC_PENDING;
2036     return BTM_SUCCESS;
2037   }
2038 
2039   disconnect_acl(*p_acl, HCI_ERR_PEER_USER,
2040                  "stack::acl::btm_acl::btm_remove_acl");
2041   return BTM_SUCCESS;
2042 }
2043 
btm_cont_rswitch_from_handle(uint16_t hci_handle)2044 void btm_cont_rswitch_from_handle(uint16_t hci_handle) {
2045   tACL_CONN* p = internal_.acl_get_connection_from_handle(hci_handle);
2046   if (p == nullptr) {
2047     log::warn("Role switch received but with no active ACL");
2048     return;
2049   }
2050 
2051   /* Check to see if encryption needs to be turned off if pending
2052    change of link key or role switch */
2053   if (p->is_switch_role_mode_change()) {
2054     /* Must turn off Encryption first if necessary */
2055     /* Some devices do not support switch or change of link key while encryption
2056      * is on */
2057     if (p->is_encrypted && !IsEprAvailable(*p)) {
2058       p->set_encryption_off();
2059       if (p->is_switch_role_mode_change()) {
2060         p->set_switch_role_encryption_off();
2061       }
2062     } else /* Encryption not used or EPR supported, continue with switch
2063               and/or change of link key */
2064     {
2065       if (p->is_switch_role_mode_change()) {
2066         internal_.hci_start_role_switch_to_central(*p);
2067       }
2068     }
2069   }
2070 }
2071 
2072 /*******************************************************************************
2073  *
2074  * Function         btm_acl_notif_conn_collision
2075  *
2076  * Description      Send connection collision event to upper layer if registered
2077  *
2078  *
2079  ******************************************************************************/
btm_acl_notif_conn_collision(const RawAddress & bda)2080 void btm_acl_notif_conn_collision(const RawAddress& bda) {
2081   do_in_main_thread(FROM_HERE, base::BindOnce(bta_sys_notify_collision, bda));
2082 }
2083 
BTM_BLE_IS_RESOLVE_BDA(const RawAddress & x)2084 bool BTM_BLE_IS_RESOLVE_BDA(const RawAddress& x) {
2085   return ((x.address)[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB;
2086 }
2087 
acl_refresh_remote_address(const RawAddress & identity_address,tBLE_ADDR_TYPE identity_address_type,const RawAddress & bda,tBLE_RAND_ADDR_TYPE rra_type,const RawAddress & rpa)2088 bool acl_refresh_remote_address(const RawAddress& identity_address,
2089                                 tBLE_ADDR_TYPE identity_address_type,
2090                                 const RawAddress& bda,
2091                                 tBLE_RAND_ADDR_TYPE rra_type,
2092                                 const RawAddress& rpa) {
2093   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bda, BT_TRANSPORT_LE);
2094   if (p_acl == nullptr) {
2095     log::warn("Unable to find active acl");
2096     return false;
2097   }
2098 
2099   if (rra_type == BTM_BLE_ADDR_PSEUDO) {
2100     /* use identity address, resolvable_private_addr is empty */
2101     if (rpa.IsEmpty()) {
2102       p_acl->active_remote_addr_type = identity_address_type;
2103       p_acl->active_remote_addr = identity_address;
2104     } else {
2105       p_acl->active_remote_addr_type = BLE_ADDR_RANDOM;
2106       p_acl->active_remote_addr = rpa;
2107     }
2108   } else {
2109     p_acl->active_remote_addr_type = static_cast<tBLE_ADDR_TYPE>(rra_type);
2110     p_acl->active_remote_addr = rpa;
2111   }
2112 
2113   log::debug("active_remote_addr_type: {}", p_acl->active_remote_addr_type);
2114   return true;
2115 }
2116 
acl_peer_supports_ble_connection_parameters_request(const RawAddress & remote_bda)2117 bool acl_peer_supports_ble_connection_parameters_request(
2118     const RawAddress& remote_bda) {
2119   tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
2120   if (p_acl == nullptr) {
2121     log::warn("Unable to find active acl");
2122     return false;
2123   }
2124   if (!p_acl->peer_le_features_valid) {
2125     log::warn("Checking remote features but remote feature read is incomplete");
2126   }
2127   return HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl->peer_le_features);
2128 }
2129 
acl_ble_connection_parameters_request(uint16_t handle,uint16_t conn_int_min,uint16_t conn_int_max,uint16_t conn_latency,uint16_t conn_timeout,uint16_t min_ce_len,uint16_t max_ce_len)2130 void acl_ble_connection_parameters_request(
2131     uint16_t handle, uint16_t conn_int_min, uint16_t conn_int_max,
2132     uint16_t conn_latency, uint16_t conn_timeout, uint16_t min_ce_len,
2133     uint16_t max_ce_len) {
2134   bluetooth::shim::ACL_SendConnectionParameterUpdateRequest(
2135       handle, conn_int_min, conn_int_max, conn_latency, conn_timeout,
2136       min_ce_len, max_ce_len);
2137 }
2138 
acl_peer_supports_sniff_subrating(const RawAddress & remote_bda)2139 bool acl_peer_supports_sniff_subrating(const RawAddress& remote_bda) {
2140   tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
2141   if (p_acl == nullptr) {
2142     log::warn("Unable to find active acl");
2143     return false;
2144   }
2145   if (!p_acl->peer_lmp_feature_valid[0]) {
2146     log::warn("Checking remote features but remote feature read is incomplete");
2147   }
2148   return HCI_SNIFF_SUB_RATE_SUPPORTED(p_acl->peer_lmp_feature_pages[0]);
2149 }
2150 
acl_peer_supports_ble_connection_subrating(const RawAddress & remote_bda)2151 bool acl_peer_supports_ble_connection_subrating(const RawAddress& remote_bda) {
2152   tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
2153   if (p_acl == nullptr) {
2154     log::warn("Unable to find active acl");
2155     return false;
2156   }
2157   if (!p_acl->peer_le_features_valid) {
2158     log::warn("Checking remote features but remote feature read is incomplete");
2159   }
2160   return HCI_LE_CONN_SUBRATING_SUPPORT(p_acl->peer_le_features);
2161 }
2162 
acl_peer_supports_ble_connection_subrating_host(const RawAddress & remote_bda)2163 bool acl_peer_supports_ble_connection_subrating_host(
2164     const RawAddress& remote_bda) {
2165   tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
2166   if (p_acl == nullptr) {
2167     log::warn("Unable to find active acl");
2168     return false;
2169   }
2170   if (!p_acl->peer_le_features_valid) {
2171     log::warn("Checking remote features but remote feature read is incomplete");
2172   }
2173   return HCI_LE_CONN_SUBRATING_HOST_SUPPORT(p_acl->peer_le_features);
2174 }
2175 
2176 /*******************************************************************************
2177  *
2178  * Function         BTM_ReadConnectionAddr
2179  *
2180  * Description      This function is called to get the local LE device address
2181  *                  information.
2182  *
2183  * Returns          void
2184  *
2185  ******************************************************************************/
BTM_ReadConnectionAddr(const RawAddress & remote_bda,RawAddress & local_conn_addr,tBLE_ADDR_TYPE * p_addr_type,bool ota_address)2186 void BTM_ReadConnectionAddr(const RawAddress& remote_bda,
2187                             RawAddress& local_conn_addr,
2188                             tBLE_ADDR_TYPE* p_addr_type, bool ota_address) {
2189   tBTM_SEC_DEV_REC* p_sec_rec = btm_find_dev(remote_bda);
2190   if (p_sec_rec == nullptr) {
2191     log::warn("No matching known device {} in record", remote_bda);
2192     return;
2193   }
2194 
2195   bluetooth::shim::ACL_ReadConnectionAddress(
2196       p_sec_rec->ble_hci_handle, local_conn_addr, p_addr_type, ota_address);
2197 }
2198 
2199 /*******************************************************************************
2200  *
2201  * Function         BTM_IsBleConnection
2202  *
2203  * Description      This function is called to check if the connection handle
2204  *                  for an LE link
2205  *
2206  * Returns          true if connection is LE link, otherwise false.
2207  *
2208  ******************************************************************************/
BTM_IsBleConnection(uint16_t hci_handle)2209 bool BTM_IsBleConnection(uint16_t hci_handle) {
2210   const tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
2211   if (p_acl == nullptr) return false;
2212   return p_acl->is_transport_ble();
2213 }
2214 
acl_address_from_handle(uint16_t handle)2215 const RawAddress acl_address_from_handle(uint16_t handle) {
2216   tACL_CONN* p_acl = acl_get_connection_from_handle(handle);
2217   if (p_acl == nullptr) {
2218     return RawAddress::kEmpty;
2219   }
2220   return p_acl->remote_addr;
2221 }
2222 
acl_is_switch_role_idle(const RawAddress & bd_addr,tBT_TRANSPORT transport)2223 bool acl_is_switch_role_idle(const RawAddress& bd_addr,
2224                              tBT_TRANSPORT transport) {
2225   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, transport);
2226   if (p_acl == nullptr) {
2227     log::warn("Unable to find active acl");
2228     return false;
2229   }
2230   return p_acl->is_switch_role_idle();
2231 }
2232 
2233 /*******************************************************************************
2234  *
2235  * Function       BTM_ReadRemoteConnectionAddr
2236  *
2237  * Description    This function is read the LE remote device address used in
2238  *                connection establishment
2239  *
2240  * Parameters     pseudo_addr: pseudo random address available
2241  *                conn_addr:connection address used
2242  *                p_addr_type : BD Address type, Public or Random of the address
2243  *                              used
2244  *                ota_address: When use if remote used RPA in OTA it will be
2245  *returned.
2246  *
2247  * Returns        bool, true if connection to remote device exists, else false
2248  *
2249  ******************************************************************************/
BTM_ReadRemoteConnectionAddr(const RawAddress & pseudo_addr,RawAddress & conn_addr,tBLE_ADDR_TYPE * p_addr_type,bool ota_address)2250 bool BTM_ReadRemoteConnectionAddr(const RawAddress& pseudo_addr,
2251                                   RawAddress& conn_addr,
2252                                   tBLE_ADDR_TYPE* p_addr_type,
2253                                   bool ota_address) {
2254   tBTM_SEC_DEV_REC* p_sec_rec = btm_find_dev(pseudo_addr);
2255   if (p_sec_rec == nullptr) {
2256     log::warn("No matching known device {} in record", pseudo_addr);
2257     return false;
2258   }
2259 
2260   bluetooth::shim::ACL_ReadPeerConnectionAddress(
2261       p_sec_rec->ble_hci_handle, conn_addr, p_addr_type, ota_address);
2262   return true;
2263 }
2264 
acl_link_role_from_handle(uint16_t handle)2265 uint8_t acl_link_role_from_handle(uint16_t handle) {
2266   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
2267   if (p_acl == nullptr) {
2268     return HCI_ROLE_UNKNOWN;
2269   }
2270   return p_acl->link_role;
2271 }
2272 
acl_peer_supports_ble_packet_extension(uint16_t hci_handle)2273 bool acl_peer_supports_ble_packet_extension(uint16_t hci_handle) {
2274   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
2275   if (p_acl == nullptr) {
2276     return false;
2277   }
2278   if (!p_acl->peer_le_features_valid) {
2279     log::warn("Checking remote features but remote feature read is incomplete");
2280   }
2281   return HCI_LE_DATA_LEN_EXT_SUPPORTED(p_acl->peer_le_features);
2282 }
2283 
acl_peer_supports_ble_2m_phy(uint16_t hci_handle)2284 bool acl_peer_supports_ble_2m_phy(uint16_t hci_handle) {
2285   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
2286   if (p_acl == nullptr) {
2287     return false;
2288   }
2289   if (!p_acl->peer_le_features_valid) {
2290     log::warn("Checking remote features but remote feature read is incomplete");
2291   }
2292   return HCI_LE_2M_PHY_SUPPORTED(p_acl->peer_le_features);
2293 }
2294 
acl_peer_supports_ble_coded_phy(uint16_t hci_handle)2295 bool acl_peer_supports_ble_coded_phy(uint16_t hci_handle) {
2296   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
2297   if (p_acl == nullptr) {
2298     return false;
2299   }
2300   if (!p_acl->peer_le_features_valid) {
2301     log::warn("Checking remote features but remote feature read is incomplete");
2302     return false;
2303   }
2304   return HCI_LE_CODED_PHY_SUPPORTED(p_acl->peer_le_features);
2305 }
2306 
acl_set_disconnect_reason(tHCI_STATUS acl_disc_reason)2307 void acl_set_disconnect_reason(tHCI_STATUS acl_disc_reason) {
2308   btm_cb.acl_cb_.set_disconnect_reason(acl_disc_reason);
2309 }
2310 
acl_set_locally_initiated(bool locally_initiated)2311 void acl_set_locally_initiated(bool locally_initiated) {
2312   btm_cb.acl_cb_.set_locally_initiated(locally_initiated);
2313 }
2314 
acl_is_role_switch_allowed()2315 bool acl_is_role_switch_allowed() {
2316   return btm_cb.acl_cb_.DefaultLinkPolicy() &
2317          HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH;
2318 }
2319 
acl_get_supported_packet_types()2320 uint16_t acl_get_supported_packet_types() {
2321   return btm_cb.acl_cb_.DefaultPacketTypes();
2322 }
2323 
acl_set_peer_le_features_from_handle(uint16_t hci_handle,const uint8_t * p)2324 bool acl_set_peer_le_features_from_handle(uint16_t hci_handle,
2325                                           const uint8_t* p) {
2326   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
2327   if (p_acl == nullptr) {
2328     return false;
2329   }
2330   STREAM_TO_ARRAY(p_acl->peer_le_features, p, BD_FEATURES_LEN);
2331   p_acl->peer_le_features_valid = true;
2332   log::debug("Completed le feature read request");
2333 
2334   /* save LE remote supported features to iot conf file */
2335   std::string key = IOT_CONF_KEY_RT_SUPP_FEATURES "_" + std::to_string(0);
2336 
2337   DEVICE_IOT_CONFIG_ADDR_SET_BIN(p_acl->remote_addr, key,
2338                                  p_acl->peer_le_features, BD_FEATURES_LEN);
2339   return true;
2340 }
2341 
on_acl_br_edr_connected(const RawAddress & bda,uint16_t handle,uint8_t enc_mode,bool locally_initiated)2342 void on_acl_br_edr_connected(const RawAddress& bda, uint16_t handle,
2343                              uint8_t enc_mode, bool locally_initiated) {
2344   power_telemetry::GetInstance().LogLinkDetails(handle, bda, true, true);
2345   if (delayed_role_change_ != nullptr && delayed_role_change_->bd_addr == bda) {
2346     btm_sec_connected(bda, handle, HCI_SUCCESS, enc_mode,
2347                       delayed_role_change_->new_role);
2348   } else {
2349     btm_sec_connected(bda, handle, HCI_SUCCESS, enc_mode);
2350   }
2351   delayed_role_change_ = nullptr;
2352   l2c_link_hci_conn_comp(HCI_SUCCESS, handle, bda);
2353   uint16_t link_supervision_timeout =
2354       osi_property_get_int32(PROPERTY_LINK_SUPERVISION_TIMEOUT, 8000);
2355   BTM_SetLinkSuperTout(bda, link_supervision_timeout);
2356 
2357   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
2358   if (p_acl == nullptr) {
2359     log::warn("Unable to find active acl");
2360     return;
2361   }
2362 
2363   acl_set_locally_initiated(locally_initiated);
2364 
2365   /*
2366    * The legacy code path informs the upper layer via the BTA
2367    * layer after all relevant read_remote_ commands are complete.
2368    * The GD code path has ownership of the read_remote_ commands
2369    * and thus may inform the upper layers about the connection.
2370    */
2371   NotifyAclLinkUp(*p_acl);
2372 }
2373 
on_acl_br_edr_failed(const RawAddress & bda,tHCI_STATUS status,bool locally_initiated)2374 void on_acl_br_edr_failed(const RawAddress& bda, tHCI_STATUS status,
2375                           bool locally_initiated) {
2376   log::assert_that(status != HCI_SUCCESS,
2377                    "Successful connection entering failing code path");
2378   if (delayed_role_change_ != nullptr && delayed_role_change_->bd_addr == bda) {
2379     btm_sec_connected(bda, HCI_INVALID_HANDLE, status, false,
2380                       delayed_role_change_->new_role);
2381   } else {
2382     btm_sec_connected(bda, HCI_INVALID_HANDLE, status, false);
2383   }
2384   delayed_role_change_ = nullptr;
2385   l2c_link_hci_conn_comp(status, HCI_INVALID_HANDLE, bda);
2386 
2387   acl_set_locally_initiated(locally_initiated);
2388   btm_acl_create_failed(bda, BT_TRANSPORT_BR_EDR, status);
2389 }
2390 
btm_acl_connected(const RawAddress & bda,uint16_t handle,tHCI_STATUS status,uint8_t enc_mode)2391 void btm_acl_connected(const RawAddress& bda, uint16_t handle,
2392                        tHCI_STATUS status, uint8_t enc_mode) {
2393   switch (status) {
2394     case HCI_SUCCESS:
2395       power_telemetry::GetInstance().LogLinkDetails(handle, bda, true, true);
2396       return on_acl_br_edr_connected(bda, handle, enc_mode,
2397                                      true /* locally_initiated */);
2398     default:
2399       return on_acl_br_edr_failed(bda, status, /* locally_initiated */ true);
2400   }
2401 }
2402 
btm_acl_disconnected(tHCI_STATUS status,uint16_t handle,tHCI_REASON reason)2403 void btm_acl_disconnected(tHCI_STATUS status, uint16_t handle,
2404                           tHCI_REASON reason) {
2405   if (status != HCI_SUCCESS) {
2406     log::warn("Received disconnect with error:{}", hci_error_code_text(status));
2407   }
2408   power_telemetry::GetInstance().LogLinkDetails(handle, RawAddress::kEmpty,
2409                                                 false, true);
2410   /* There can be a case when we rejected PIN code authentication */
2411   /* otherwise save a new reason */
2412   if (btm_get_acl_disc_reason_code() != HCI_ERR_HOST_REJECT_SECURITY) {
2413     acl_set_disconnect_reason(static_cast<tHCI_STATUS>(reason));
2414   }
2415 
2416   /* Let L2CAP know about it */
2417   l2c_link_hci_disc_comp(handle, reason);
2418 
2419   /* Notify security manager */
2420   btm_sec_disconnected(handle, reason,
2421                        "stack::acl::btm_acl::btm_acl_disconnected");
2422 }
2423 
btm_connection_request(const RawAddress & bda,const bluetooth::hci::ClassOfDevice & cod)2424 void btm_connection_request(const RawAddress& bda,
2425                             const bluetooth::hci::ClassOfDevice& cod) {
2426   // Copy Cod information
2427   DEV_CLASS dc;
2428 
2429   /* Some device may request a connection before we are done with the HCI_Reset
2430    * sequence */
2431   if (bluetooth::shim::GetController() == nullptr) {
2432     log::verbose("Security Manager: connect request when device not ready");
2433     btsnd_hcic_reject_conn(bda, HCI_ERR_HOST_REJECT_DEVICE);
2434     return;
2435   }
2436 
2437   dc[0] = cod.cod[2], dc[1] = cod.cod[1], dc[2] = cod.cod[0];
2438 
2439   btm_sec_conn_req(bda, dc);
2440 }
2441 
acl_disconnect_from_handle(uint16_t handle,tHCI_STATUS reason,std::string comment)2442 void acl_disconnect_from_handle(uint16_t handle, tHCI_STATUS reason,
2443                                 std::string comment) {
2444   acl_disconnect_after_role_switch(handle, reason, comment);
2445 }
2446 
2447 // BLUETOOTH CORE SPECIFICATION Version 5.4 | Vol 4, Part E
2448 // 7.1.6 Disconnect command
2449 // Only a subset of reasons are valid and will be accepted
2450 // by the controller
is_disconnect_reason_valid(const tHCI_REASON & reason)2451 bool is_disconnect_reason_valid(const tHCI_REASON& reason) {
2452   switch (reason) {
2453     case HCI_ERR_AUTH_FAILURE:
2454     case HCI_ERR_PEER_USER:
2455     case HCI_ERR_REMOTE_LOW_RESOURCE:
2456     case HCI_ERR_REMOTE_POWER_OFF:
2457     case HCI_ERR_UNSUPPORTED_REM_FEATURE:
2458     case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
2459     case HCI_ERR_UNACCEPT_CONN_INTERVAL:
2460       return true;
2461     default:
2462       break;
2463   }
2464   return false;
2465 }
2466 
acl_disconnect_after_role_switch(uint16_t conn_handle,tHCI_STATUS reason,std::string comment)2467 void acl_disconnect_after_role_switch(uint16_t conn_handle, tHCI_STATUS reason,
2468                                       std::string comment) {
2469   if (!is_disconnect_reason_valid(reason)) {
2470     log::warn(
2471         "Controller will not accept invalid reason parameter:{} instead "
2472         "sending:{}",
2473         hci_error_code_text(reason), hci_error_code_text(HCI_ERR_PEER_USER));
2474     reason = HCI_ERR_PEER_USER;
2475   }
2476 
2477   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(conn_handle);
2478   if (p_acl == nullptr) {
2479     log::error("Sending disconnect for unknown acl:{} PLEASE FIX", conn_handle);
2480     GetInterface().Disconnect(conn_handle, reason);
2481     return;
2482   }
2483 
2484   /* If a role switch is in progress, delay the HCI Disconnect to avoid
2485    * controller problem */
2486   if (p_acl->rs_disc_pending == BTM_SEC_RS_PENDING) {
2487     log::debug(
2488         "Role switch in progress - Set DISC Pending flag in "
2489         "btm_sec_send_hci_disconnect to delay disconnect");
2490     p_acl->rs_disc_pending = BTM_SEC_DISC_PENDING;
2491   } else {
2492     log::debug("Sending acl disconnect reason:{} [{}]",
2493                hci_error_code_text(reason), reason);
2494     disconnect_acl(*p_acl, reason, comment);
2495   }
2496 }
2497 
acl_send_data_packet_br_edr(const RawAddress & bd_addr,BT_HDR * p_buf)2498 void acl_send_data_packet_br_edr(const RawAddress& bd_addr, BT_HDR* p_buf) {
2499     tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
2500     if (p_acl == nullptr) {
2501       log::warn("Acl br_edr data write for unknown device:{}", bd_addr);
2502       osi_free(p_buf);
2503       return;
2504     }
2505     power_telemetry::GetInstance().LogTxAclPktData(p_buf->len);
2506     return bluetooth::shim::ACL_WriteData(p_acl->hci_handle, p_buf);
2507 }
2508 
acl_send_data_packet_ble(const RawAddress & bd_addr,BT_HDR * p_buf)2509 void acl_send_data_packet_ble(const RawAddress& bd_addr, BT_HDR* p_buf) {
2510     tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE);
2511     if (p_acl == nullptr) {
2512       log::warn("Acl le data write for unknown device:{}", bd_addr);
2513       osi_free(p_buf);
2514       return;
2515     }
2516     power_telemetry::GetInstance().LogTxAclPktData(p_buf->len);
2517     return bluetooth::shim::ACL_WriteData(p_acl->hci_handle, p_buf);
2518 }
2519 
acl_write_automatic_flush_timeout(const RawAddress & bd_addr,uint16_t flush_timeout_in_ticks)2520 void acl_write_automatic_flush_timeout(const RawAddress& bd_addr,
2521                                        uint16_t flush_timeout_in_ticks) {
2522   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
2523   if (p_acl == nullptr) {
2524     log::warn("Unable to find active acl");
2525     return;
2526   }
2527   if (p_acl->flush_timeout_in_ticks == flush_timeout_in_ticks) {
2528     log::info(
2529         "Ignoring since cached value is same as requested flush_timeout:{}",
2530         flush_timeout_in_ticks);
2531     return;
2532   }
2533   flush_timeout_in_ticks &= HCI_MAX_AUTOMATIC_FLUSH_TIMEOUT;
2534   p_acl->flush_timeout_in_ticks = flush_timeout_in_ticks;
2535   btsnd_hcic_write_auto_flush_tout(p_acl->hci_handle, flush_timeout_in_ticks);
2536 }
2537 
acl_create_le_connection_with_id(uint8_t id,const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type)2538 bool acl_create_le_connection_with_id(uint8_t id, const RawAddress& bd_addr,
2539                                       tBLE_ADDR_TYPE addr_type) {
2540   tBLE_BD_ADDR address_with_type{
2541       .type = addr_type,
2542       .bda = bd_addr,
2543   };
2544 
2545   find_in_device_record(bd_addr, &address_with_type);
2546 
2547   log::debug("Creating le direct connection to:{} type:{} (initial type: {})",
2548              address_with_type, AddressTypeText(address_with_type.type),
2549              AddressTypeText(addr_type));
2550 
2551   if (address_with_type.type == BLE_ADDR_ANONYMOUS) {
2552     log::warn(
2553         "Creating le direct connection to:{}, address type 'anonymous' is "
2554         "invalid",
2555         address_with_type);
2556     return false;
2557   }
2558 
2559   if (bluetooth::common::init_flags::
2560           use_unified_connection_manager_is_enabled()) {
2561     bluetooth::connection::GetConnectionManager().start_direct_connection(
2562         id, bluetooth::core::ToRustAddress(address_with_type));
2563   } else {
2564     bluetooth::shim::ACL_AcceptLeConnectionFrom(address_with_type,
2565                                                 /* is_direct */ true);
2566   }
2567   return true;
2568 }
2569 
acl_create_le_connection_with_id(uint8_t id,const RawAddress & bd_addr)2570 bool acl_create_le_connection_with_id(uint8_t id, const RawAddress& bd_addr) {
2571   return acl_create_le_connection_with_id(id, bd_addr, BLE_ADDR_PUBLIC);
2572 }
2573 
acl_create_le_connection(const RawAddress & bd_addr)2574 bool acl_create_le_connection(const RawAddress& bd_addr) {
2575   return acl_create_le_connection_with_id(CONN_MGR_ID_L2CAP, bd_addr);
2576 }
2577 
acl_rcv_acl_data(BT_HDR * p_msg)2578 void acl_rcv_acl_data(BT_HDR* p_msg) {
2579   acl_header_t acl_header{
2580       .handle = HCI_INVALID_HANDLE,
2581       .hci_len = 0,
2582   };
2583   const uint8_t* p = (uint8_t*)(p_msg + 1) + p_msg->offset;
2584 
2585   STREAM_TO_UINT16(acl_header.handle, p);
2586   acl_header.handle = HCID_GET_HANDLE(acl_header.handle);
2587 
2588   power_telemetry::GetInstance().LogRxAclPktData(p_msg->len);
2589   STREAM_TO_UINT16(acl_header.hci_len, p);
2590   if (acl_header.hci_len < L2CAP_PKT_OVERHEAD ||
2591       acl_header.hci_len != p_msg->len - sizeof(acl_header)) {
2592     log::warn("Received mismatched hci header length:{} data_len:{}",
2593               acl_header.hci_len, p_msg->len - sizeof(acl_header));
2594     osi_free(p_msg);
2595     return;
2596   }
2597   l2c_rcv_acl_data(p_msg);
2598 }
2599 
acl_packets_completed(uint16_t handle,uint16_t credits)2600 void acl_packets_completed(uint16_t handle, uint16_t credits) {
2601   l2c_packets_completed(handle, credits);
2602   bluetooth::hci::IsoManager::GetInstance()->HandleNumComplDataPkts(handle,
2603                                                                     credits);
2604 }
2605 
acl_process_supported_features(uint16_t handle,uint64_t features)2606 void acl_process_supported_features(uint16_t handle, uint64_t features) {
2607   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
2608   if (p_acl == nullptr) {
2609     log::warn("Unable to find active acl");
2610     return;
2611   }
2612   const uint8_t current_page_number = 0;
2613 
2614   memcpy(p_acl->peer_lmp_feature_pages[current_page_number],
2615          (uint8_t*)&features, sizeof(uint64_t));
2616   p_acl->peer_lmp_feature_valid[current_page_number] = true;
2617 
2618   log::debug(
2619       "Copied supported feature pages handle:{} current_page_number:{} "
2620       "features:{}",
2621       handle, current_page_number,
2622       bd_features_text(p_acl->peer_lmp_feature_pages[current_page_number]));
2623 
2624   if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl->peer_lmp_feature_pages[0])) &&
2625       (bluetooth::shim::GetController()->IsSupported(
2626           bluetooth::hci::OpCode::READ_REMOTE_EXTENDED_FEATURES))) {
2627     log::debug("Waiting for remote extended feature response to arrive");
2628   } else {
2629     log::debug("No more remote features outstanding so notify upper layer");
2630     NotifyAclFeaturesReadComplete(*p_acl, current_page_number);
2631   }
2632 }
2633 
acl_process_extended_features(uint16_t handle,uint8_t current_page_number,uint8_t max_page_number,uint64_t features)2634 void acl_process_extended_features(uint16_t handle, uint8_t current_page_number,
2635                                    uint8_t max_page_number, uint64_t features) {
2636   if (current_page_number > HCI_EXT_FEATURES_PAGE_MAX) {
2637     log::warn("Unable to process current_page_number:{}", current_page_number);
2638     return;
2639   }
2640   tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
2641   if (p_acl == nullptr) {
2642     log::warn("Unable to find active acl");
2643     return;
2644   }
2645   memcpy(p_acl->peer_lmp_feature_pages[current_page_number],
2646          (uint8_t*)&features, sizeof(uint64_t));
2647   p_acl->peer_lmp_feature_valid[current_page_number] = true;
2648 
2649   log::debug(
2650       "Copied extended feature pages handle:{} current_page_number:{} "
2651       "max_page_number:{} features:{}",
2652       handle, current_page_number, max_page_number,
2653       bd_features_text(p_acl->peer_lmp_feature_pages[current_page_number]));
2654 
2655   if (max_page_number == 0 || max_page_number == current_page_number) {
2656     NotifyAclFeaturesReadComplete(*p_acl, max_page_number);
2657   }
2658 }
2659 
ACL_RegisterClient(struct acl_client_callback_s *)2660 void ACL_RegisterClient(struct acl_client_callback_s* /* callbacks */) {
2661   log::debug("UNIMPLEMENTED");
2662 }
2663 
ACL_UnregisterClient(struct acl_client_callback_s *)2664 void ACL_UnregisterClient(struct acl_client_callback_s* /* callbacks */) {
2665   log::debug("UNIMPLEMENTED");
2666 }
2667 
btm_acl_for_bda(const RawAddress & bd_addr,tBT_TRANSPORT transport)2668 tACL_CONN* btm_acl_for_bda(const RawAddress& bd_addr, tBT_TRANSPORT transport) {
2669   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, transport);
2670   if (p_acl == nullptr) {
2671     log::warn("Unable to find active acl");
2672     return nullptr;
2673   }
2674   return p_acl;
2675 }
2676 
find_in_device_record(const RawAddress & bd_addr,tBLE_BD_ADDR * address_with_type)2677 void find_in_device_record(const RawAddress& bd_addr,
2678                            tBLE_BD_ADDR* address_with_type) {
2679   const tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
2680   if (p_dev_rec == nullptr) {
2681     return;
2682   }
2683 
2684   if (p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) {
2685     if (p_dev_rec->ble.identity_address_with_type.bda.IsEmpty()) {
2686       *address_with_type = {.type = p_dev_rec->ble.AddressType(),
2687                             .bda = bd_addr};
2688       return;
2689     }
2690     *address_with_type = p_dev_rec->ble.identity_address_with_type;
2691     return;
2692   }
2693   *address_with_type = {.type = BLE_ADDR_PUBLIC, .bda = bd_addr};
2694   return;
2695 }
2696 
2697 /*******************************************************************************
2698  *
2699  * Function         btm_acl_flush
2700  *
2701  * Description      This function is called by L2CAP to flush an ACL link.
2702  *
2703  * Returns          void
2704  *
2705  ******************************************************************************/
btm_acl_flush(uint16_t handle)2706 void btm_acl_flush(uint16_t handle) { bluetooth::shim::ACL_Flush(handle); }
2707