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 <cstddef>
18 #include <cstdint>
19 #include <future>
20 
21 #include "gd/hci/acl_manager.h"
22 #include "main/shim/acl_api.h"
23 #include "main/shim/dumpsys.h"
24 #include "main/shim/helpers.h"
25 #include "main/shim/stack.h"
26 #include "types/ble_address_with_type.h"
27 #include "types/raw_address.h"
28 
ACL_CreateClassicConnection(const RawAddress & raw_address)29 void bluetooth::shim::ACL_CreateClassicConnection(
30     const RawAddress& raw_address) {
31   auto address = ToGdAddress(raw_address);
32   Stack::GetInstance()->GetAcl()->CreateClassicConnection(address);
33 }
34 
ACL_CancelClassicConnection(const RawAddress & raw_address)35 void bluetooth::shim::ACL_CancelClassicConnection(
36     const RawAddress& raw_address) {
37   auto address = ToGdAddress(raw_address);
38   Stack::GetInstance()->GetAcl()->CancelClassicConnection(address);
39 }
40 
ACL_AcceptLeConnectionFrom(const tBLE_BD_ADDR & legacy_address_with_type,bool is_direct)41 bool bluetooth::shim::ACL_AcceptLeConnectionFrom(
42     const tBLE_BD_ADDR& legacy_address_with_type, bool is_direct) {
43   std::promise<bool> promise;
44   auto future = promise.get_future();
45   Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(
46       ToAddressWithTypeFromLegacy(legacy_address_with_type), is_direct,
47       std::move(promise));
48   return future.get();
49 }
50 
ACL_IgnoreLeConnectionFrom(const tBLE_BD_ADDR & legacy_address_with_type)51 void bluetooth::shim::ACL_IgnoreLeConnectionFrom(
52     const tBLE_BD_ADDR& legacy_address_with_type) {
53   Stack::GetInstance()->GetAcl()->IgnoreLeConnectionFrom(
54       ToAddressWithTypeFromLegacy(legacy_address_with_type));
55 }
56 
ACL_WriteData(uint16_t handle,BT_HDR * p_buf)57 void bluetooth::shim::ACL_WriteData(uint16_t handle, BT_HDR* p_buf) {
58   std::unique_ptr<bluetooth::packet::RawBuilder> packet = MakeUniquePacket(
59       p_buf->data + p_buf->offset + HCI_DATA_PREAMBLE_SIZE,
60       p_buf->len - HCI_DATA_PREAMBLE_SIZE, IsPacketFlushable(p_buf));
61   Stack::GetInstance()->GetAcl()->WriteData(handle, std::move(packet));
62   osi_free(p_buf);
63 }
64 
ACL_ConfigureLePrivacy(bool is_le_privacy_enabled)65 void bluetooth::shim::ACL_ConfigureLePrivacy(bool is_le_privacy_enabled) {
66   hci::LeAddressManager::AddressPolicy address_policy =
67       is_le_privacy_enabled
68           ? hci::LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS
69           : hci::LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS;
70   hci::AddressWithType empty_address_with_type(
71       hci::Address{}, hci::AddressType::RANDOM_DEVICE_ADDRESS);
72   /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
73   auto minimum_rotation_time = std::chrono::minutes(7);
74   auto maximum_rotation_time = std::chrono::minutes(15);
75 
76   Stack::GetInstance()
77       ->GetStackManager()
78       ->GetInstance<bluetooth::hci::AclManager>()
79       ->SetPrivacyPolicyForInitiatorAddress(
80           address_policy, empty_address_with_type, minimum_rotation_time,
81           maximum_rotation_time);
82 }
83 
ACL_Disconnect(uint16_t handle,bool is_classic,tHCI_STATUS reason)84 void bluetooth::shim::ACL_Disconnect(uint16_t handle, bool is_classic,
85                                      tHCI_STATUS reason) {
86   (is_classic)
87       ? Stack::GetInstance()->GetAcl()->DisconnectClassic(handle, reason)
88       : Stack::GetInstance()->GetAcl()->DisconnectLe(handle, reason);
89 }
90 
ACL_Shutdown()91 void bluetooth::shim::ACL_Shutdown() {
92   Stack::GetInstance()->GetAcl()->Shutdown();
93 }
94 
ACL_IgnoreAllLeConnections()95 void bluetooth::shim::ACL_IgnoreAllLeConnections() {
96   return Stack::GetInstance()->GetAcl()->ClearAcceptList();
97 }
98 
ACL_ReadConnectionAddress(const RawAddress & pseudo_addr,RawAddress & conn_addr,uint8_t * p_addr_type)99 void bluetooth::shim::ACL_ReadConnectionAddress(const RawAddress& pseudo_addr,
100                                                 RawAddress& conn_addr,
101                                                 uint8_t* p_addr_type) {
102   auto local_address =
103       Stack::GetInstance()->GetAcl()->GetConnectionLocalAddress(pseudo_addr);
104   conn_addr = ToRawAddress(local_address.GetAddress());
105   *p_addr_type = static_cast<uint8_t>(local_address.GetAddressType());
106 }
107