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
21 #include <cstdint>
22
23 #include "common/init_flags.h"
24 #include "os/log.h"
25 #include "stack/btm/btm_ble_int.h"
26 #include "stack/btm/btm_dev.h"
27 #include "stack/btm/btm_int_types.h"
28 #include "stack/btm/btm_sec.h"
29 #include "stack/gatt/connection_manager.h"
30 #include "stack/include/acl_api.h"
31 #include "stack/include/btm_ble_addr.h"
32 #include "stack/include/btm_ble_privacy.h"
33 #include "stack/include/l2cap_hci_link_interface.h"
34 #include "types/raw_address.h"
35
36 using namespace bluetooth;
37
38 extern tBTM_CB btm_cb;
39
40 void btm_ble_increment_link_topology_mask(uint8_t link_role);
41
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)42 static bool acl_ble_common_connection(
43 const tBLE_BD_ADDR& address_with_type, uint16_t handle, tHCI_ROLE role,
44 bool is_in_security_db, uint16_t conn_interval, uint16_t conn_latency,
45 uint16_t conn_timeout, 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 // Inform any applications that a connection has completed.
52 if (!bluetooth::common::init_flags::
53 use_unified_connection_manager_is_enabled()) {
54 connection_manager::on_connection_complete(address_with_type.bda);
55 }
56
57 // Allocate or update the security device record for this device
58 btm_ble_connected(address_with_type.bda, handle, HCI_ENCRYPT_MODE_DISABLED,
59 role, address_with_type.type, is_in_security_db,
60 can_read_discoverable_characteristics);
61
62 // Update the link topology information for our device
63 btm_ble_increment_link_topology_mask(role);
64
65 // Inform l2cap of a potential connection.
66 if (!l2cble_conn_comp(handle, role, address_with_type.bda,
67 address_with_type.type, conn_interval, conn_latency,
68 conn_timeout)) {
69 btm_sec_disconnect(handle, HCI_ERR_PEER_USER, "stack::acl::ble_acl fail");
70 log::warn("Unable to complete l2cap connection");
71 return false;
72 }
73
74 /* Tell BTM Acl management about the link */
75 btm_acl_created(address_with_type.bda, handle, role, BT_TRANSPORT_LE);
76
77 return true;
78 }
79
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)80 void acl_ble_enhanced_connection_complete(
81 const tBLE_BD_ADDR& address_with_type, uint16_t handle, tHCI_ROLE role,
82 bool match, uint16_t conn_interval, uint16_t conn_latency,
83 uint16_t conn_timeout, const RawAddress& /* local_rpa */,
84 const RawAddress& peer_rpa, tBLE_ADDR_TYPE peer_addr_type,
85 bool can_read_discoverable_characteristics) {
86 if (!acl_ble_common_connection(address_with_type, handle, role, match,
87 conn_interval, conn_latency, conn_timeout,
88 can_read_discoverable_characteristics)) {
89 log::warn("Unable to create enhanced ble acl connection");
90 return;
91 }
92
93 if (peer_addr_type & BLE_ADDR_TYPE_ID_BIT)
94 btm_ble_refresh_peer_resolvable_private_addr(address_with_type.bda,
95 peer_rpa, BTM_BLE_ADDR_RRA);
96 btm_ble_update_mode_operation(role, &address_with_type.bda, HCI_SUCCESS);
97 }
98
maybe_resolve_received_address(const tBLE_BD_ADDR & address_with_type,tBLE_BD_ADDR * resolved_address_with_type)99 static bool maybe_resolve_received_address(
100 const tBLE_BD_ADDR& address_with_type,
101 tBLE_BD_ADDR* resolved_address_with_type) {
102 log::assert_that(resolved_address_with_type != nullptr,
103 "assert failed: resolved_address_with_type != nullptr");
104
105 *resolved_address_with_type = address_with_type;
106 return maybe_resolve_address(&resolved_address_with_type->bda,
107 &resolved_address_with_type->type);
108 }
109
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)110 void acl_ble_enhanced_connection_complete_from_shim(
111 const tBLE_BD_ADDR& address_with_type, uint16_t handle, tHCI_ROLE role,
112 uint16_t conn_interval, uint16_t conn_latency, uint16_t conn_timeout,
113 const RawAddress& local_rpa, const RawAddress& peer_rpa,
114 tBLE_ADDR_TYPE peer_addr_type, bool can_read_discoverable_characteristics) {
115 if (!bluetooth::common::init_flags::
116 use_unified_connection_manager_is_enabled()) {
117 connection_manager::on_connection_complete(address_with_type.bda);
118 }
119
120 tBLE_BD_ADDR resolved_address_with_type;
121 const bool is_in_security_db = maybe_resolve_received_address(
122 address_with_type, &resolved_address_with_type);
123
124 acl_set_locally_initiated(role == tHCI_ROLE::HCI_ROLE_CENTRAL);
125 acl_ble_enhanced_connection_complete(
126 resolved_address_with_type, handle, role, is_in_security_db,
127 conn_interval, conn_latency, conn_timeout, local_rpa, peer_rpa,
128 peer_addr_type, can_read_discoverable_characteristics);
129
130 // The legacy stack continues the LE connection after the read remote
131 // version complete has been received.
132 // maybe_chain_more_commands_after_read_remote_version_complete
133 }
134
acl_ble_connection_fail(const tBLE_BD_ADDR & address_with_type,uint16_t,bool,tHCI_STATUS status)135 void acl_ble_connection_fail(const tBLE_BD_ADDR& address_with_type,
136 uint16_t /* handle */, bool /* enhanced */,
137 tHCI_STATUS status) {
138 acl_set_locally_initiated(
139 true); // LE connection failures are always locally initiated
140 btm_acl_create_failed(address_with_type.bda, BT_TRANSPORT_LE, status);
141
142 if (status != HCI_ERR_ADVERTISING_TIMEOUT) {
143 btm_cb.ble_ctr_cb.set_connection_state_idle();
144 btm_ble_clear_topology_mask(BTM_BLE_STATE_INIT_BIT);
145 tBLE_BD_ADDR resolved_address_with_type;
146 maybe_resolve_received_address(address_with_type,
147 &resolved_address_with_type);
148 if (!bluetooth::common::init_flags::
149 use_unified_connection_manager_is_enabled()) {
150 connection_manager::on_connection_timed_out_from_shim(
151 resolved_address_with_type.bda);
152 }
153 log::warn("LE connection fail peer:{} bd_addr:{} hci_status:{}",
154 address_with_type, resolved_address_with_type.bda,
155 hci_status_code_text(status));
156 } else {
157 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
158 }
159 btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, &address_with_type.bda,
160 status);
161 }
162
163 void gatt_notify_conn_update(const RawAddress& remote, uint16_t interval,
164 uint16_t latency, uint16_t timeout,
165 tHCI_STATUS status);
acl_ble_update_event_received(tHCI_STATUS status,uint16_t handle,uint16_t interval,uint16_t latency,uint16_t timeout)166 void acl_ble_update_event_received(tHCI_STATUS status, uint16_t handle,
167 uint16_t interval, uint16_t latency,
168 uint16_t timeout) {
169 l2cble_process_conn_update_evt(handle, status, interval, latency, timeout);
170
171 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle);
172
173 if (!p_dev_rec) return;
174
175 gatt_notify_conn_update(p_dev_rec->ble.pseudo_addr, interval, latency,
176 timeout, status);
177 }
178
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)179 void acl_ble_data_length_change_event(uint16_t handle, uint16_t max_tx_octets,
180 uint16_t max_tx_time,
181 uint16_t max_rx_octets,
182 uint16_t max_rx_time) {
183 log::debug(
184 "Data length change event received handle:0x{:04x} max_tx_octets:{} "
185 "max_tx_time:{} max_rx_octets:{} max_rx_time:{}",
186 handle, max_tx_octets, max_tx_time, max_rx_octets, max_rx_time);
187 l2cble_process_data_length_change_event(handle, max_tx_octets, max_rx_octets);
188 }
189
btm_get_next_private_addrress_interval_ms()190 uint64_t btm_get_next_private_addrress_interval_ms() {
191 /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
192 const uint64_t interval_min_ms = (7 * 60 * 1000);
193 const uint64_t interval_random_part_max_ms = (8 * 60 * 1000);
194
195 return interval_min_ms + std::rand() % interval_random_part_max_ms;
196 }
197