• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "acl"
18 
19 #include <bluetooth/log.h>
20 #include <com_android_bluetooth_flags.h>
21 
22 #include <cstdint>
23 
24 #include "stack/btm/btm_ble_int.h"
25 #include "stack/btm/btm_dev.h"
26 #include "stack/btm/btm_int_types.h"
27 #include "stack/btm/btm_sec.h"
28 #include "stack/connection_manager/connection_manager.h"
29 #include "stack/include/acl_api.h"
30 #include "stack/include/ble_acl_interface.h"
31 #include "stack/include/btm_ble_addr.h"
32 #include "stack/include/btm_ble_privacy.h"
33 #include "stack/include/gatt_api.h"
34 #include "stack/include/l2cap_hci_link_interface.h"
35 #include "types/raw_address.h"
36 
37 using namespace bluetooth;
38 
39 extern tBTM_CB btm_cb;
40 
acl_ble_common_connection(const tBLE_BD_ADDR & address_with_type,uint16_t handle,tHCI_ROLE role,bool is_in_security_db,uint16_t conn_interval,uint16_t conn_latency,uint16_t conn_timeout,bool can_read_discoverable_characteristics)41 static bool acl_ble_common_connection(const tBLE_BD_ADDR& address_with_type, uint16_t handle,
42                                       tHCI_ROLE role, bool is_in_security_db,
43                                       uint16_t conn_interval, uint16_t conn_latency,
44                                       uint16_t conn_timeout,
45                                       bool can_read_discoverable_characteristics) {
46   if (role == HCI_ROLE_CENTRAL) {
47     btm_cb.ble_ctr_cb.set_connection_state_idle();
48     btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
49   }
50 
51   // Allocate or update the security device record for this device
52   btm_ble_connected(address_with_type.bda, handle, HCI_ENCRYPT_MODE_DISABLED, role,
53                     address_with_type.type, is_in_security_db,
54                     can_read_discoverable_characteristics);
55 
56   // Update the link topology information for our device
57   btm_ble_increment_link_topology_mask(role);
58 
59   // Inform l2cap of a potential connection.
60   if (!l2cble_conn_comp(handle, role, address_with_type.bda, address_with_type.type, conn_interval,
61                         conn_latency, conn_timeout)) {
62     btm_sec_disconnect(handle, HCI_ERR_PEER_USER, "stack::acl::ble_acl fail");
63     log::warn("Unable to complete l2cap connection");
64     return false;
65   }
66 
67   /* Tell BTM Acl management about the link */
68   btm_acl_created(address_with_type.bda, handle, role, BT_TRANSPORT_LE);
69 
70   return true;
71 }
72 
acl_ble_enhanced_connection_complete(const tBLE_BD_ADDR & address_with_type,uint16_t handle,tHCI_ROLE role,bool match,uint16_t conn_interval,uint16_t conn_latency,uint16_t conn_timeout,const RawAddress &,const RawAddress & peer_rpa,tBLE_ADDR_TYPE peer_addr_type,bool can_read_discoverable_characteristics)73 void acl_ble_enhanced_connection_complete(const tBLE_BD_ADDR& address_with_type, uint16_t handle,
74                                           tHCI_ROLE role, bool match, uint16_t conn_interval,
75                                           uint16_t conn_latency, uint16_t conn_timeout,
76                                           const RawAddress& /* local_rpa */,
77                                           const RawAddress& peer_rpa, tBLE_ADDR_TYPE peer_addr_type,
78                                           bool can_read_discoverable_characteristics) {
79   if (!acl_ble_common_connection(address_with_type, handle, role, match, conn_interval,
80                                  conn_latency, conn_timeout,
81                                  can_read_discoverable_characteristics)) {
82     log::warn("Unable to create enhanced ble acl connection");
83     return;
84   }
85 
86   if (peer_addr_type & BLE_ADDR_TYPE_ID_BIT) {
87     btm_ble_refresh_peer_resolvable_private_addr(address_with_type.bda, peer_rpa, BTM_BLE_ADDR_RRA);
88   }
89   btm_ble_update_mode_operation(role, &address_with_type.bda, HCI_SUCCESS);
90 }
91 
maybe_resolve_received_address(const tBLE_BD_ADDR & address_with_type,tBLE_BD_ADDR * resolved_address_with_type)92 static bool maybe_resolve_received_address(const tBLE_BD_ADDR& address_with_type,
93                                            tBLE_BD_ADDR* resolved_address_with_type) {
94   log::assert_that(resolved_address_with_type != nullptr,
95                    "assert failed: resolved_address_with_type != nullptr");
96 
97   *resolved_address_with_type = address_with_type;
98   return maybe_resolve_address(&resolved_address_with_type->bda, &resolved_address_with_type->type);
99 }
100 
acl_ble_enhanced_connection_complete_from_shim(const tBLE_BD_ADDR & address_with_type,uint16_t handle,tHCI_ROLE role,uint16_t conn_interval,uint16_t conn_latency,uint16_t conn_timeout,const RawAddress & local_rpa,const RawAddress & peer_rpa,tBLE_ADDR_TYPE peer_addr_type,bool can_read_discoverable_characteristics)101 void acl_ble_enhanced_connection_complete_from_shim(
102         const tBLE_BD_ADDR& address_with_type, uint16_t handle, tHCI_ROLE role,
103         uint16_t conn_interval, uint16_t conn_latency, uint16_t conn_timeout,
104         const RawAddress& local_rpa, const RawAddress& peer_rpa, tBLE_ADDR_TYPE peer_addr_type,
105         bool can_read_discoverable_characteristics) {
106   tBLE_BD_ADDR resolved_address_with_type;
107   const bool is_in_security_db =
108           maybe_resolve_received_address(address_with_type, &resolved_address_with_type);
109 
110   acl_set_locally_initiated(role == tHCI_ROLE::HCI_ROLE_CENTRAL);
111   acl_ble_enhanced_connection_complete(
112           resolved_address_with_type, handle, role, is_in_security_db, conn_interval, conn_latency,
113           conn_timeout, local_rpa, peer_rpa, peer_addr_type, can_read_discoverable_characteristics);
114 
115   // The legacy stack continues the LE connection after the read remote
116   // version complete has been received.
117   // maybe_chain_more_commands_after_read_remote_version_complete
118 }
119 
acl_ble_connection_fail(const tBLE_BD_ADDR & address_with_type,uint16_t,bool,tHCI_STATUS status)120 void acl_ble_connection_fail(const tBLE_BD_ADDR& address_with_type, uint16_t /* handle */,
121                              bool /* enhanced */, tHCI_STATUS status) {
122   acl_set_locally_initiated(true);  // LE connection failures are always locally initiated
123   btm_acl_create_failed(address_with_type.bda, BT_TRANSPORT_LE, status);
124 
125   if (status != HCI_ERR_ADVERTISING_TIMEOUT) {
126     btm_cb.ble_ctr_cb.set_connection_state_idle();
127     btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
128     tBLE_BD_ADDR resolved_address_with_type;
129     maybe_resolve_received_address(address_with_type, &resolved_address_with_type);
130     connection_manager::on_connection_timed_out_from_shim(resolved_address_with_type.bda);
131     log::warn("LE connection fail peer:{} bd_addr:{} hci_status:{}", address_with_type,
132               resolved_address_with_type.bda, hci_status_code_text(status));
133   } else {
134     btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
135   }
136   btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, &address_with_type.bda, status);
137 }
138 
acl_ble_update_event_received(tHCI_STATUS status,uint16_t handle,uint16_t interval,uint16_t latency,uint16_t timeout)139 void acl_ble_update_event_received(tHCI_STATUS status, uint16_t handle, uint16_t interval,
140                                    uint16_t latency, uint16_t timeout) {
141   l2cble_process_conn_update_evt(handle, status, interval, latency, timeout);
142 
143   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle);
144 
145   if (!p_dev_rec) {
146     return;
147   }
148 
149   gatt_notify_conn_update(p_dev_rec->ble.pseudo_addr, interval, latency, timeout, status);
150 }
151 
acl_ble_update_request_event_received(uint16_t handle,uint16_t interval_min,uint16_t interval_max,uint16_t latency,uint16_t timeout)152 void acl_ble_update_request_event_received(uint16_t handle, uint16_t interval_min,
153                                            uint16_t interval_max, uint16_t latency,
154                                            uint16_t timeout) {
155   l2cble_process_rc_param_request_evt(handle, interval_min, interval_max, latency, timeout);
156 }
157 
acl_ble_data_length_change_event(uint16_t handle,uint16_t max_tx_octets,uint16_t max_tx_time,uint16_t max_rx_octets,uint16_t max_rx_time)158 void acl_ble_data_length_change_event(uint16_t handle, uint16_t max_tx_octets, uint16_t max_tx_time,
159                                       uint16_t max_rx_octets, uint16_t max_rx_time) {
160   log::debug(
161           "Data length change event received handle:0x{:04x} max_tx_octets:{} "
162           "max_tx_time:{} max_rx_octets:{} max_rx_time:{}",
163           handle, max_tx_octets, max_tx_time, max_rx_octets, max_rx_time);
164   l2cble_process_data_length_change_event(handle, max_tx_octets, max_rx_octets);
165 }
166 
btm_get_next_private_address_interval_ms()167 uint64_t btm_get_next_private_address_interval_ms() {
168   /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
169   const uint64_t interval_min_ms = (7 * 60 * 1000);
170   const uint64_t interval_random_part_max_ms = (8 * 60 * 1000);
171 
172   return interval_min_ms + std::rand() % interval_random_part_max_ms;
173 }
174