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