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