1 /*
2  * Copyright 2019 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 "bt_shim_btm"
18 
19 #include "main/shim/btm_api.h"
20 
21 #include <base/functional/callback.h>
22 
23 #include "hci/controller.h"
24 #include "hci/controller_interface.h"
25 #include "hci/le_advertising_manager.h"
26 #include "main/shim/acl.h"
27 #include "main/shim/entry.h"
28 #include "main/shim/helpers.h"
29 #include "main/shim/stack.h"
30 #include "stack/btm/btm_ble_sec.h"
31 #include "stack/btm/btm_dev.h"
32 #include "stack/include/btm_status.h"
33 #include "types/raw_address.h"
34 
BTM_ClearEventFilter()35 tBTM_STATUS bluetooth::shim::BTM_ClearEventFilter() {
36   GetController()->SetEventFilterClearAll();
37   return tBTM_STATUS::BTM_SUCCESS;
38 }
39 
BTM_ClearEventMask()40 tBTM_STATUS bluetooth::shim::BTM_ClearEventMask() {
41   GetController()->SetEventMask(0);
42   GetController()->LeSetEventMask(0);
43   return tBTM_STATUS::BTM_SUCCESS;
44 }
45 
BTM_ClearFilterAcceptList()46 tBTM_STATUS bluetooth::shim::BTM_ClearFilterAcceptList() {
47   Stack::GetInstance()->GetAcl()->ClearFilterAcceptList();
48   return tBTM_STATUS::BTM_SUCCESS;
49 }
50 
BTM_DisconnectAllAcls()51 tBTM_STATUS bluetooth::shim::BTM_DisconnectAllAcls() {
52   Stack::GetInstance()->GetAcl()->DisconnectAllForSuspend();
53   //  Stack::GetInstance()->GetAcl()->Shutdown();
54   return tBTM_STATUS::BTM_SUCCESS;
55 }
56 
BTM_SetEventFilterConnectionSetupAllDevices()57 tBTM_STATUS bluetooth::shim::BTM_SetEventFilterConnectionSetupAllDevices() {
58   // Autoplumbed
59   GetController()->SetEventFilterConnectionSetupAllDevices(
60           bluetooth::hci::AutoAcceptFlag::AUTO_ACCEPT_ON_ROLE_SWITCH_ENABLED);
61   return tBTM_STATUS::BTM_SUCCESS;
62 }
63 
BTM_AllowWakeByHid(std::vector<RawAddress> classic_hid_devices,std::vector<std::pair<RawAddress,uint8_t>> le_hid_devices)64 tBTM_STATUS bluetooth::shim::BTM_AllowWakeByHid(
65         std::vector<RawAddress> classic_hid_devices,
66         std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices) {
67   // First set ACL to suspended state.
68   Stack::GetInstance()->GetAcl()->SetSystemSuspendState(/*suspended=*/true);
69 
70   // Allow classic HID wake.
71   auto controller = GetController();
72   for (auto device : classic_hid_devices) {
73     controller->SetEventFilterConnectionSetupAddress(bluetooth::ToGdAddress(device),
74                                                      hci::AutoAcceptFlag::AUTO_ACCEPT_OFF);
75   }
76 
77   // Allow BLE HID
78   for (auto hid_address : le_hid_devices) {
79     std::promise<bool> accept_promise;
80     auto accept_future = accept_promise.get_future();
81 
82     tBLE_BD_ADDR bdadr = BTM_Sec_GetAddressWithType(hid_address.first);
83     Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(ToAddressWithType(bdadr.bda, bdadr.type),
84                                                            /*is_direct=*/false,
85                                                            std::move(accept_promise));
86 
87     accept_future.wait();
88   }
89 
90   return tBTM_STATUS::BTM_SUCCESS;
91 }
92 
BTM_RestoreFilterAcceptList(std::vector<std::pair<RawAddress,uint8_t>> le_devices)93 tBTM_STATUS bluetooth::shim::BTM_RestoreFilterAcceptList(
94         std::vector<std::pair<RawAddress, uint8_t>> le_devices) {
95   // First, mark ACL as no longer suspended.
96   Stack::GetInstance()->GetAcl()->SetSystemSuspendState(/*suspended=*/false);
97 
98   // Next, Allow BLE connection from all devices that need to be restored.
99   // This will also re-arm the LE connection.
100   for (auto address_pair : le_devices) {
101     std::promise<bool> accept_promise;
102     auto accept_future = accept_promise.get_future();
103 
104     tBLE_BD_ADDR bdadr = BTM_Sec_GetAddressWithType(address_pair.first);
105     Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(ToAddressWithType(bdadr.bda, bdadr.type),
106                                                            /*is_direct=*/false,
107                                                            std::move(accept_promise));
108 
109     accept_future.wait();
110   }
111 
112   return tBTM_STATUS::BTM_SUCCESS;
113 }
114 
BTM_SetDefaultEventMaskExcept(uint64_t mask,uint64_t le_mask)115 tBTM_STATUS bluetooth::shim::BTM_SetDefaultEventMaskExcept(uint64_t mask, uint64_t le_mask) {
116   uint64_t applied_mask = bluetooth::hci::Controller::kDefaultEventMask & ~(mask);
117   uint64_t applied_le_mask = bluetooth::hci::Controller::kDefaultLeEventMask & ~(le_mask);
118   GetController()->SetEventMask(applied_mask);
119   GetController()->LeSetEventMask(applied_le_mask);
120   return tBTM_STATUS::BTM_SUCCESS;
121 }
122 
BTM_SetEventFilterInquiryResultAllDevices()123 tBTM_STATUS bluetooth::shim::BTM_SetEventFilterInquiryResultAllDevices() {
124   // Autoplumbed
125   GetController()->SetEventFilterInquiryResultAllDevices();
126   return tBTM_STATUS::BTM_SUCCESS;
127 }
128 
BTM_BleResetId()129 tBTM_STATUS bluetooth::shim::BTM_BleResetId() {
130   btm_ble_reset_id();
131   return tBTM_STATUS::BTM_SUCCESS;
132 }
133 
BTM_BleGetNumberOfAdvertisingInstancesInUse(void)134 size_t bluetooth::shim::BTM_BleGetNumberOfAdvertisingInstancesInUse(void) {
135   return GetAdvertising()->GetNumberOfAdvertisingInstancesInUse();
136 }
137