• 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 #include "main/shim/acl_api.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <future>
22 #include <optional>
23 
24 #include "gd/hci/acl_manager.h"
25 #include "gd/hci/remote_name_request.h"
26 #include "main/shim/dumpsys.h"
27 #include "main/shim/entry.h"
28 #include "main/shim/helpers.h"
29 #include "main/shim/stack.h"
30 #include "osi/include/allocator.h"
31 #include "stack/btm/btm_sec.h"
32 #include "stack/btm/security_device_record.h"
33 #include "stack/include/bt_hdr.h"
34 #include "stack/include/btu.h"  // do_in_main_thread
35 #include "stack/include/inq_hci_link_interface.h"
36 #include "types/ble_address_with_type.h"
37 #include "types/raw_address.h"
38 
ACL_CreateClassicConnection(const RawAddress & raw_address)39 void bluetooth::shim::ACL_CreateClassicConnection(
40     const RawAddress& raw_address) {
41   auto address = ToGdAddress(raw_address);
42   Stack::GetInstance()->GetAcl()->CreateClassicConnection(address);
43 }
44 
ACL_CancelClassicConnection(const RawAddress & raw_address)45 void bluetooth::shim::ACL_CancelClassicConnection(
46     const RawAddress& raw_address) {
47   auto address = ToGdAddress(raw_address);
48   Stack::GetInstance()->GetAcl()->CancelClassicConnection(address);
49 }
50 
ACL_AcceptLeConnectionFrom(const tBLE_BD_ADDR & legacy_address_with_type,bool is_direct)51 bool bluetooth::shim::ACL_AcceptLeConnectionFrom(
52     const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct) {
53   std::promise<bool> promise;
54   auto future = promise.get_future();
55   Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(
56       ToAddressWithTypeFromLegacy(legacy_address_with_type), is_direct,
57       std::move(promise));
58   return future.get();
59 }
60 
ACL_IgnoreLeConnectionFrom(const tBLE_BD_ADDR & legacy_address_with_type)61 void bluetooth::shim::ACL_IgnoreLeConnectionFrom(
62     const tBLE_BD_ADDR& legacy_address_with_type) {
63   Stack::GetInstance()->GetAcl()->IgnoreLeConnectionFrom(
64       ToAddressWithTypeFromLegacy(legacy_address_with_type));
65 }
66 
ACL_WriteData(uint16_t handle,BT_HDR * p_buf)67 void bluetooth::shim::ACL_WriteData(uint16_t handle, BT_HDR* p_buf) {
68   std::unique_ptr<bluetooth::packet::RawBuilder> packet = MakeUniquePacket(
69       p_buf->data + p_buf->offset + HCI_DATA_PREAMBLE_SIZE,
70       p_buf->len - HCI_DATA_PREAMBLE_SIZE, IsPacketFlushable(p_buf));
71   Stack::GetInstance()->GetAcl()->WriteData(handle, std::move(packet));
72   osi_free(p_buf);
73 }
74 
ACL_ConfigureLePrivacy(bool is_le_privacy_enabled)75 void bluetooth::shim::ACL_ConfigureLePrivacy(bool is_le_privacy_enabled) {
76   hci::LeAddressManager::AddressPolicy address_policy =
77       is_le_privacy_enabled
78           ? hci::LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS
79           : hci::LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS;
80   hci::AddressWithType empty_address_with_type(
81       hci::Address{}, hci::AddressType::RANDOM_DEVICE_ADDRESS);
82   /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
83   auto minimum_rotation_time = std::chrono::minutes(7);
84   auto maximum_rotation_time = std::chrono::minutes(15);
85 
86   Stack::GetInstance()
87       ->GetStackManager()
88       ->GetInstance<bluetooth::hci::AclManager>()
89       ->SetPrivacyPolicyForInitiatorAddress(
90           address_policy, empty_address_with_type, minimum_rotation_time,
91           maximum_rotation_time);
92 }
93 
ACL_Disconnect(uint16_t handle,bool is_classic,tHCI_STATUS reason,std::string comment)94 void bluetooth::shim::ACL_Disconnect(uint16_t handle, bool is_classic,
95                                      tHCI_STATUS reason, std::string comment) {
96   (is_classic)
97       ? Stack::GetInstance()->GetAcl()->DisconnectClassic(handle, reason,
98                                                           comment)
99       : Stack::GetInstance()->GetAcl()->DisconnectLe(handle, reason, comment);
100 }
101 
ACL_Shutdown()102 void bluetooth::shim::ACL_Shutdown() {
103   Stack::GetInstance()->GetAcl()->Shutdown();
104 }
105 
ACL_IgnoreAllLeConnections()106 void bluetooth::shim::ACL_IgnoreAllLeConnections() {
107   return Stack::GetInstance()->GetAcl()->ClearFilterAcceptList();
108 }
109 
ACL_ReadConnectionAddress(const RawAddress & pseudo_addr,RawAddress & conn_addr,tBLE_ADDR_TYPE * p_addr_type)110 void bluetooth::shim::ACL_ReadConnectionAddress(const RawAddress& pseudo_addr,
111                                                 RawAddress& conn_addr,
112                                                 tBLE_ADDR_TYPE* p_addr_type) {
113   auto local_address =
114       Stack::GetInstance()->GetAcl()->GetConnectionLocalAddress(pseudo_addr);
115   conn_addr = ToRawAddress(local_address.GetAddress());
116   *p_addr_type = static_cast<tBLE_ADDR_TYPE>(local_address.GetAddressType());
117 }
118 
ACL_GetAdvertisingSetConnectedTo(const RawAddress & addr)119 std::optional<uint8_t> bluetooth::shim::ACL_GetAdvertisingSetConnectedTo(
120     const RawAddress& addr) {
121   return Stack::GetInstance()->GetAcl()->GetAdvertisingSetConnectedTo(addr);
122 }
123 
ACL_AddToAddressResolution(const tBLE_BD_ADDR & legacy_address_with_type,const Octet16 & peer_irk,const Octet16 & local_irk)124 void bluetooth::shim::ACL_AddToAddressResolution(
125     const tBLE_BD_ADDR& legacy_address_with_type, const Octet16& peer_irk,
126     const Octet16& local_irk) {
127   Stack::GetInstance()->GetAcl()->AddToAddressResolution(
128       ToAddressWithType(legacy_address_with_type.bda,
129                         legacy_address_with_type.type),
130       peer_irk, local_irk);
131 }
132 
ACL_RemoveFromAddressResolution(const tBLE_BD_ADDR & legacy_address_with_type)133 void bluetooth::shim::ACL_RemoveFromAddressResolution(
134     const tBLE_BD_ADDR& legacy_address_with_type) {
135   Stack::GetInstance()->GetAcl()->RemoveFromAddressResolution(ToAddressWithType(
136       legacy_address_with_type.bda, legacy_address_with_type.type));
137 }
138 
ACL_ClearAddressResolution()139 void bluetooth::shim::ACL_ClearAddressResolution() {
140   Stack::GetInstance()->GetAcl()->ClearAddressResolution();
141 }
142 
ACL_ClearFilterAcceptList()143 void bluetooth::shim::ACL_ClearFilterAcceptList() {
144   Stack::GetInstance()->GetAcl()->ClearFilterAcceptList();
145 }
ACL_LeSetDefaultSubrate(uint16_t subrate_min,uint16_t subrate_max,uint16_t max_latency,uint16_t cont_num,uint16_t sup_tout)146 void bluetooth::shim::ACL_LeSetDefaultSubrate(uint16_t subrate_min,
147                                               uint16_t subrate_max,
148                                               uint16_t max_latency,
149                                               uint16_t cont_num,
150                                               uint16_t sup_tout) {
151   Stack::GetInstance()->GetAcl()->LeSetDefaultSubrate(
152       subrate_min, subrate_max, max_latency, cont_num, sup_tout);
153 }
154 
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)155 void bluetooth::shim::ACL_LeSubrateRequest(
156     uint16_t hci_handle, uint16_t subrate_min, uint16_t subrate_max,
157     uint16_t max_latency, uint16_t cont_num, uint16_t sup_tout) {
158   Stack::GetInstance()->GetAcl()->LeSubrateRequest(
159       hci_handle, subrate_min, subrate_max, max_latency, cont_num, sup_tout);
160 }
161 
ACL_RemoteNameRequest(const RawAddress & addr,uint8_t page_scan_rep_mode,uint8_t page_scan_mode,uint16_t clock_offset)162 void bluetooth::shim::ACL_RemoteNameRequest(const RawAddress& addr,
163                                             uint8_t page_scan_rep_mode,
164                                             uint8_t page_scan_mode,
165                                             uint16_t clock_offset) {
166   bluetooth::shim::GetRemoteNameRequest()->StartRemoteNameRequest(
167       ToGdAddress(addr),
168       hci::RemoteNameRequestBuilder::Create(
169           ToGdAddress(addr), hci::PageScanRepetitionMode(page_scan_rep_mode),
170           clock_offset & (~BTM_CLOCK_OFFSET_VALID),
171           (clock_offset & BTM_CLOCK_OFFSET_VALID)
172               ? hci::ClockOffsetValid::VALID
173               : hci::ClockOffsetValid::INVALID),
174       GetGdShimHandler()->BindOnce([](hci::ErrorCode status) {
175         if (status != hci::ErrorCode::SUCCESS) {
176           do_in_main_thread(
177               FROM_HERE,
178               base::Bind(
179                   [](hci::ErrorCode status) {
180                     // NOTE: we intentionally don't supply the address, to match
181                     // the legacy behavior.
182                     // Callsites that want the address should use
183                     // StartRemoteNameRequest() directly, rather than going
184                     // through this shim.
185                     btm_process_remote_name(nullptr, nullptr, 0,
186                                             static_cast<tHCI_STATUS>(status));
187                     btm_sec_rmt_name_request_complete(
188                         nullptr, nullptr, static_cast<tHCI_STATUS>(status));
189                   },
190                   status));
191         }
192       }),
193       GetGdShimHandler()->BindOnce(
194           [](RawAddress addr, uint64_t features) {
195             static_assert(sizeof(features) == 8);
196             auto addr_array = addr.ToArray();
197             auto p = (uint8_t*)osi_malloc(addr_array.size() + sizeof(features));
198             std::copy(addr_array.rbegin(), addr_array.rend(), p);
199             for (int i = 0; i != sizeof(features); ++i) {
200               p[addr_array.size() + i] = features & ((1 << 8) - 1);
201               features >>= 8;
202             }
203             do_in_main_thread(FROM_HERE,
204                               base::Bind(btm_sec_rmt_host_support_feat_evt, p));
205           },
206           addr),
207       GetGdShimHandler()->BindOnce(
208           [](RawAddress addr, hci::ErrorCode status,
209              std::array<uint8_t, 248> name) {
210             do_in_main_thread(
211                 FROM_HERE,
212                 base::Bind(
213                     [](RawAddress addr, hci::ErrorCode status,
214                        std::array<uint8_t, 248> name) {
215                       auto p = (uint8_t*)osi_malloc(name.size());
216                       std::copy(name.begin(), name.end(), p);
217 
218                       btm_process_remote_name(&addr, p, name.size(),
219                                               static_cast<tHCI_STATUS>(status));
220                       btm_sec_rmt_name_request_complete(
221                           &addr, p, static_cast<tHCI_STATUS>(status));
222                     },
223                     addr, status, name));
224           },
225           addr));
226 }
227 
ACL_CancelRemoteNameRequest(const RawAddress & addr)228 void bluetooth::shim::ACL_CancelRemoteNameRequest(const RawAddress& addr) {
229   bluetooth::shim::GetRemoteNameRequest()->CancelRemoteNameRequest(
230       ToGdAddress(addr));
231 }
232