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 #include "main/shim/acl_api.h"
18
19 #include <android_bluetooth_sysprop.h>
20 #include <base/location.h>
21 #include <com_android_bluetooth_flags.h>
22
23 #include <cstdint>
24 #include <future>
25 #include <optional>
26
27 #include "hci/acl_manager.h"
28 #include "hci/remote_name_request.h"
29 #include "main/shim/acl.h"
30 #include "main/shim/entry.h"
31 #include "main/shim/helpers.h"
32 #include "main/shim/stack.h"
33 #include "osi/include/allocator.h"
34 #include "osi/include/properties.h"
35 #include "stack/btm/btm_sec.h"
36 #include "stack/btm/security_device_record.h"
37 #include "stack/include/bt_hdr.h"
38 #include "stack/include/inq_hci_link_interface.h"
39 #include "stack/include/main_thread.h"
40 #include "types/ble_address_with_type.h"
41 #include "types/raw_address.h"
42 #ifndef PROPERTY_BLE_PRIVACY_OWN_ADDRESS_ENABLED
43 #define PROPERTY_BLE_PRIVACY_OWN_ADDRESS_ENABLED \
44 "bluetooth.core.gap.le.privacy.own_address_type.enabled"
45 #endif
46
ACL_CreateClassicConnection(const RawAddress & raw_address)47 void bluetooth::shim::ACL_CreateClassicConnection(
48 const RawAddress& raw_address) {
49 auto address = ToGdAddress(raw_address);
50 Stack::GetInstance()->GetAcl()->CreateClassicConnection(address);
51 }
52
ACL_CancelClassicConnection(const RawAddress & raw_address)53 void bluetooth::shim::ACL_CancelClassicConnection(
54 const RawAddress& raw_address) {
55 auto address = ToGdAddress(raw_address);
56 Stack::GetInstance()->GetAcl()->CancelClassicConnection(address);
57 }
58
ACL_AcceptLeConnectionFrom(const tBLE_BD_ADDR & legacy_address_with_type,bool is_direct)59 bool bluetooth::shim::ACL_AcceptLeConnectionFrom(
60 const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct) {
61 std::promise<bool> promise;
62 auto future = promise.get_future();
63 Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(
64 ToAddressWithTypeFromLegacy(legacy_address_with_type), is_direct,
65 std::move(promise));
66 return future.get();
67 }
68
ACL_IgnoreLeConnectionFrom(const tBLE_BD_ADDR & legacy_address_with_type)69 void bluetooth::shim::ACL_IgnoreLeConnectionFrom(
70 const tBLE_BD_ADDR& legacy_address_with_type) {
71 Stack::GetInstance()->GetAcl()->IgnoreLeConnectionFrom(
72 ToAddressWithTypeFromLegacy(legacy_address_with_type));
73 }
74
ACL_WriteData(uint16_t handle,BT_HDR * p_buf)75 void bluetooth::shim::ACL_WriteData(uint16_t handle, BT_HDR* p_buf) {
76 std::unique_ptr<bluetooth::packet::RawBuilder> packet = MakeUniquePacket(
77 p_buf->data + p_buf->offset + HCI_DATA_PREAMBLE_SIZE,
78 p_buf->len - HCI_DATA_PREAMBLE_SIZE, IsPacketFlushable(p_buf));
79 Stack::GetInstance()->GetAcl()->WriteData(handle, std::move(packet));
80 osi_free(p_buf);
81 }
82
ACL_Flush(uint16_t handle)83 void bluetooth::shim::ACL_Flush(uint16_t handle) {
84 Stack::GetInstance()->GetAcl()->Flush(handle);
85 }
86
ACL_SendConnectionParameterUpdateRequest(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)87 void bluetooth::shim::ACL_SendConnectionParameterUpdateRequest(
88 uint16_t handle, uint16_t conn_int_min, uint16_t conn_int_max,
89 uint16_t conn_latency, uint16_t conn_timeout, uint16_t min_ce_len,
90 uint16_t max_ce_len) {
91 Stack::GetInstance()->GetAcl()->UpdateConnectionParameters(
92 handle, conn_int_min, conn_int_max, conn_latency, conn_timeout,
93 min_ce_len, max_ce_len);
94 }
95
ACL_ConfigureLePrivacy(bool is_le_privacy_enabled)96 void bluetooth::shim::ACL_ConfigureLePrivacy(bool is_le_privacy_enabled) {
97 hci::LeAddressManager::AddressPolicy address_policy =
98 is_le_privacy_enabled
99 ? hci::LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS
100 : hci::LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS;
101 /* This is a Floss only flag. Android determines address policy according to
102 * privacy mode, hence it is not necessary to enable resolvable address with
103 * another sysprop */
104 if (com::android::bluetooth::flags::
105 floss_separate_host_privacy_and_llprivacy()) {
106 address_policy = hci::LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS;
107 if (osi_property_get_bool(PROPERTY_BLE_PRIVACY_OWN_ADDRESS_ENABLED,
108 is_le_privacy_enabled))
109 address_policy =
110 hci::LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS;
111 }
112
113 hci::AddressWithType empty_address_with_type(
114 hci::Address{}, hci::AddressType::RANDOM_DEVICE_ADDRESS);
115
116 /* Default to 7 minutes minimum, 15 minutes maximum for random address refreshing;
117 * device can override. */
118 auto minimum_rotation_time = std::chrono::minutes(
119 GET_SYSPROP(Ble, random_address_rotation_interval_min, 7));
120 auto maximum_rotation_time = std::chrono::minutes(
121 GET_SYSPROP(Ble, random_address_rotation_interval_max, 15));
122
123 Stack::GetInstance()
124 ->GetStackManager()
125 ->GetInstance<bluetooth::hci::AclManager>()
126 ->SetPrivacyPolicyForInitiatorAddress(
127 address_policy, empty_address_with_type, minimum_rotation_time,
128 maximum_rotation_time);
129 }
130
ACL_Disconnect(uint16_t handle,bool is_classic,tHCI_STATUS reason,std::string comment)131 void bluetooth::shim::ACL_Disconnect(uint16_t handle, bool is_classic,
132 tHCI_STATUS reason, std::string comment) {
133 (is_classic)
134 ? Stack::GetInstance()->GetAcl()->DisconnectClassic(handle, reason,
135 comment)
136 : Stack::GetInstance()->GetAcl()->DisconnectLe(handle, reason, comment);
137 }
138
ACL_Shutdown()139 void bluetooth::shim::ACL_Shutdown() {
140 Stack::GetInstance()->GetAcl()->Shutdown();
141 }
142
ACL_IgnoreAllLeConnections()143 void bluetooth::shim::ACL_IgnoreAllLeConnections() {
144 return Stack::GetInstance()->GetAcl()->ClearFilterAcceptList();
145 }
146
ACL_ReadConnectionAddress(uint16_t handle,RawAddress & conn_addr,tBLE_ADDR_TYPE * p_addr_type,bool ota_address)147 void bluetooth::shim::ACL_ReadConnectionAddress(uint16_t handle,
148 RawAddress& conn_addr,
149 tBLE_ADDR_TYPE* p_addr_type,
150 bool ota_address) {
151 auto local_address =
152 Stack::GetInstance()->GetAcl()->GetConnectionLocalAddress(handle,
153 ota_address);
154
155 conn_addr = ToRawAddress(local_address.GetAddress());
156 *p_addr_type = static_cast<tBLE_ADDR_TYPE>(local_address.GetAddressType());
157 }
158
ACL_ReadPeerConnectionAddress(uint16_t handle,RawAddress & conn_addr,tBLE_ADDR_TYPE * p_addr_type,bool ota_address)159 void bluetooth::shim::ACL_ReadPeerConnectionAddress(uint16_t handle,
160 RawAddress& conn_addr,
161 tBLE_ADDR_TYPE* p_addr_type,
162 bool ota_address) {
163 auto remote_ota_address =
164 Stack::GetInstance()->GetAcl()->GetConnectionPeerAddress(handle,
165 ota_address);
166
167 conn_addr = ToRawAddress(remote_ota_address.GetAddress());
168 *p_addr_type =
169 static_cast<tBLE_ADDR_TYPE>(remote_ota_address.GetAddressType());
170 }
171
ACL_GetAdvertisingSetConnectedTo(const RawAddress & addr)172 std::optional<uint8_t> bluetooth::shim::ACL_GetAdvertisingSetConnectedTo(
173 const RawAddress& addr) {
174 return Stack::GetInstance()->GetAcl()->GetAdvertisingSetConnectedTo(addr);
175 }
176
ACL_AddToAddressResolution(const tBLE_BD_ADDR & legacy_address_with_type,const Octet16 & peer_irk,const Octet16 & local_irk)177 void bluetooth::shim::ACL_AddToAddressResolution(
178 const tBLE_BD_ADDR& legacy_address_with_type, const Octet16& peer_irk,
179 const Octet16& local_irk) {
180 Stack::GetInstance()->GetAcl()->AddToAddressResolution(
181 ToAddressWithType(legacy_address_with_type.bda,
182 legacy_address_with_type.type),
183 peer_irk, local_irk);
184 }
185
ACL_RemoveFromAddressResolution(const tBLE_BD_ADDR & legacy_address_with_type)186 void bluetooth::shim::ACL_RemoveFromAddressResolution(
187 const tBLE_BD_ADDR& legacy_address_with_type) {
188 Stack::GetInstance()->GetAcl()->RemoveFromAddressResolution(ToAddressWithType(
189 legacy_address_with_type.bda, legacy_address_with_type.type));
190 }
191
ACL_ClearAddressResolution()192 void bluetooth::shim::ACL_ClearAddressResolution() {
193 Stack::GetInstance()->GetAcl()->ClearAddressResolution();
194 }
195
ACL_ClearFilterAcceptList()196 void bluetooth::shim::ACL_ClearFilterAcceptList() {
197 Stack::GetInstance()->GetAcl()->ClearFilterAcceptList();
198 }
ACL_LeSetDefaultSubrate(uint16_t subrate_min,uint16_t subrate_max,uint16_t max_latency,uint16_t cont_num,uint16_t sup_tout)199 void bluetooth::shim::ACL_LeSetDefaultSubrate(uint16_t subrate_min,
200 uint16_t subrate_max,
201 uint16_t max_latency,
202 uint16_t cont_num,
203 uint16_t sup_tout) {
204 Stack::GetInstance()->GetAcl()->LeSetDefaultSubrate(
205 subrate_min, subrate_max, max_latency, cont_num, sup_tout);
206 }
207
ACL_LeSubrateRequest(uint16_t hci_handle,uint16_t subrate_min,uint16_t subrate_max,uint16_t max_latency,uint16_t cont_num,uint16_t sup_tout)208 void bluetooth::shim::ACL_LeSubrateRequest(
209 uint16_t hci_handle, uint16_t subrate_min, uint16_t subrate_max,
210 uint16_t max_latency, uint16_t cont_num, uint16_t sup_tout) {
211 Stack::GetInstance()->GetAcl()->LeSubrateRequest(
212 hci_handle, subrate_min, subrate_max, max_latency, cont_num, sup_tout);
213 }
214
ACL_RemoteNameRequest(const RawAddress & addr,uint8_t page_scan_rep_mode,uint8_t,uint16_t clock_offset)215 void bluetooth::shim::ACL_RemoteNameRequest(const RawAddress& addr,
216 uint8_t page_scan_rep_mode,
217 uint8_t /* page_scan_mode */,
218 uint16_t clock_offset) {
219 bluetooth::shim::GetRemoteNameRequest()->StartRemoteNameRequest(
220 ToGdAddress(addr),
221 hci::RemoteNameRequestBuilder::Create(
222 ToGdAddress(addr), hci::PageScanRepetitionMode(page_scan_rep_mode),
223 clock_offset & (~BTM_CLOCK_OFFSET_VALID),
224 (clock_offset & BTM_CLOCK_OFFSET_VALID)
225 ? hci::ClockOffsetValid::VALID
226 : hci::ClockOffsetValid::INVALID),
227 GetGdShimHandler()->BindOnce([](hci::ErrorCode status) {
228 if (status != hci::ErrorCode::SUCCESS) {
229 do_in_main_thread(
230 FROM_HERE,
231 base::BindOnce(
232 [](hci::ErrorCode status) {
233 // NOTE: we intentionally don't supply the address, to match
234 // the legacy behavior.
235 // Callsites that want the address should use
236 // StartRemoteNameRequest() directly, rather than going
237 // through this shim.
238 btm_process_remote_name(nullptr, nullptr, 0,
239 static_cast<tHCI_STATUS>(status));
240 btm_sec_rmt_name_request_complete(
241 nullptr, nullptr, static_cast<tHCI_STATUS>(status));
242 },
243 status));
244 }
245 }),
246 GetGdShimHandler()->BindOnce(
247 [](RawAddress addr, uint64_t features) {
248 static_assert(sizeof(features) == 8);
249 do_in_main_thread(
250 FROM_HERE,
251 base::BindOnce(btm_sec_rmt_host_support_feat_evt, addr,
252 static_cast<uint8_t>(features & 0xff)));
253 },
254 addr),
255 GetGdShimHandler()->BindOnce(
256 [](RawAddress addr, hci::ErrorCode status,
257 std::array<uint8_t, 248> name) {
258 do_in_main_thread(
259 FROM_HERE,
260 base::BindOnce(
261 [](RawAddress addr, hci::ErrorCode status,
262 std::array<uint8_t, 248> name) {
263 btm_process_remote_name(&addr, name.data(), name.size(),
264 static_cast<tHCI_STATUS>(status));
265 btm_sec_rmt_name_request_complete(
266 &addr, name.data(), static_cast<tHCI_STATUS>(status));
267 },
268 addr, status, name));
269 },
270 addr));
271 }
272
ACL_CancelRemoteNameRequest(const RawAddress & addr)273 void bluetooth::shim::ACL_CancelRemoteNameRequest(const RawAddress& addr) {
274 bluetooth::shim::GetRemoteNameRequest()->CancelRemoteNameRequest(
275 ToGdAddress(addr));
276 }
277