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