1 /*
2 * Copyright 2021 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 <base/logging.h>
18 #include <string.h>
19
20 #include <queue>
21 #include <vector>
22
23 #include "ble_scanner.h"
24 #include "ble_scanner_hci_interface.h"
25 #include "bt_target.h"
26 #include "btm_int_types.h"
27 #include "device/include/controller.h"
28 #include "osi/include/alarm.h"
29 #include "stack/btm/btm_ble_int.h"
30 #include "stack_config.h"
31
32 std::mutex lock1;
33
34 namespace {
35
36 class BleScanningManagerImpl;
37
38 BleScanningManager* instance;
39 base::WeakPtr<BleScanningManagerImpl> instance_weakptr;
40
status_callback(uint8_t status)41 static void status_callback(uint8_t status) {
42 VLOG(1) << __func__ << " Received status_cb with status:" << status;
43 }
44
45 class BleScanningManagerImpl
46 : public BleScanningManager,
47 public BleScannerHciInterface::ScanEventObserver {
48 public:
BleScanningManagerImpl(BleScannerHciInterface * interface)49 BleScanningManagerImpl(BleScannerHciInterface* interface)
50 : hci_interface(interface), weak_factory_(this) {}
51
~BleScanningManagerImpl()52 ~BleScanningManagerImpl() {}
53
PeriodicScanStart(uint8_t options,uint8_t set_id,uint8_t adv_addr_type,const RawAddress & adv_addr,uint16_t skip_num,uint16_t sync_timeout,uint8_t sync_cte_type)54 void PeriodicScanStart(uint8_t options, uint8_t set_id, uint8_t adv_addr_type,
55 const RawAddress& adv_addr, uint16_t skip_num,
56 uint16_t sync_timeout,
57 uint8_t sync_cte_type) override {
58 GetHciInterface()->PeriodicScanStart(options, set_id, adv_addr_type,
59 adv_addr, skip_num, sync_timeout,
60 sync_cte_type);
61 }
62
PeriodicScanCancelStart()63 void PeriodicScanCancelStart() override {
64 GetHciInterface()->PeriodicScanCancelStart(base::Bind(&status_callback));
65 }
66
PeriodicScanTerminate(uint16_t sync_handle)67 void PeriodicScanTerminate(uint16_t sync_handle) override {
68 GetHciInterface()->PeriodicScanTerminate(sync_handle,
69 base::Bind(&status_callback));
70 }
71
PeriodicAdvSyncTransfer(const RawAddress & bd_addr,uint16_t service_data,uint16_t sync_handle,BleScannerHciInterface::handle_cb command_complete)72 void PeriodicAdvSyncTransfer(
73 const RawAddress& bd_addr, uint16_t service_data, uint16_t sync_handle,
74 BleScannerHciInterface::handle_cb command_complete) override {
75 GetHciInterface()->PeriodicAdvSyncTransfer(bd_addr, service_data,
76 sync_handle, command_complete);
77 }
78
PeriodicAdvSetInfoTransfer(const RawAddress & bd_addr,uint16_t service_data,uint8_t adv_handle,handle_cb command_complete)79 void PeriodicAdvSetInfoTransfer(const RawAddress& bd_addr,
80 uint16_t service_data, uint8_t adv_handle,
81 handle_cb command_complete) override {
82 GetHciInterface()->PeriodicAdvSetInfoTransfer(bd_addr, service_data,
83 adv_handle, command_complete);
84 }
85
SetPeriodicAdvSyncTransferParams(const RawAddress & bd_addr,uint8_t mode,uint16_t skip,uint16_t sync_timeout,uint8_t cte_type,bool set_defaults,status_cb command_complete)86 void SetPeriodicAdvSyncTransferParams(const RawAddress& bd_addr, uint8_t mode,
87 uint16_t skip, uint16_t sync_timeout,
88 uint8_t cte_type, bool set_defaults,
89 status_cb command_complete) override {
90 GetHciInterface()->SetPeriodicAdvSyncTransferParams(
91 bd_addr, mode, skip, sync_timeout, cte_type, set_defaults,
92 command_complete);
93 }
94
OnPeriodicScanResult(uint16_t sync_handle,uint8_t tx_power,int8_t rssi,uint8_t cte_type,uint8_t pkt_data_status,uint8_t pkt_data_len,const uint8_t * pkt_data)95 void OnPeriodicScanResult(uint16_t sync_handle, uint8_t tx_power, int8_t rssi,
96 uint8_t cte_type, uint8_t pkt_data_status,
97 uint8_t pkt_data_len,
98 const uint8_t* pkt_data) override {
99 btm_ble_periodic_adv_report(sync_handle, tx_power, rssi, cte_type,
100 pkt_data_status, pkt_data_len, pkt_data);
101 }
102
OnPeriodicScanEstablished(uint8_t status,uint16_t sync_handle,uint8_t set_id,uint8_t adv_addr_type,const RawAddress & adv_addr,uint8_t adv_phy,uint16_t adv_interval,uint8_t adv_clock_accuracy)103 void OnPeriodicScanEstablished(uint8_t status, uint16_t sync_handle,
104 uint8_t set_id, uint8_t adv_addr_type,
105 const RawAddress& adv_addr, uint8_t adv_phy,
106 uint16_t adv_interval,
107 uint8_t adv_clock_accuracy) override {
108 btm_ble_periodic_adv_sync_established(status, sync_handle, set_id,
109 adv_addr_type, adv_addr, adv_phy,
110 adv_interval, adv_clock_accuracy);
111 }
112
OnPeriodicScanLost(uint16_t sync_handle)113 void OnPeriodicScanLost(uint16_t sync_handle) override {
114 btm_ble_periodic_adv_sync_lost(sync_handle);
115 }
116
GetWeakPtr()117 base::WeakPtr<BleScanningManagerImpl> GetWeakPtr() {
118 return weak_factory_.GetWeakPtr();
119 }
120
121 private:
GetHciInterface()122 BleScannerHciInterface* GetHciInterface() { return hci_interface; }
123 BleScannerHciInterface* hci_interface = nullptr;
124
125 // Member variables should appear before the WeakPtrFactory, to ensure
126 // that any WeakPtrs are invalidated before its members
127 // variable's destructors are executed, rendering them invalid.
128 base::WeakPtrFactory<BleScanningManagerImpl> weak_factory_;
129 };
130
131 } // namespace
132
Initialize(BleScannerHciInterface * interface)133 void BleScanningManager::Initialize(BleScannerHciInterface* interface) {
134 instance = new BleScanningManagerImpl(interface);
135 instance_weakptr = ((BleScanningManagerImpl*)instance)->GetWeakPtr();
136 }
137
IsInitialized()138 bool BleScanningManager::IsInitialized() { return instance; }
139
Get()140 base::WeakPtr<BleScanningManager> BleScanningManager::Get() {
141 return instance_weakptr;
142 };
143
CleanUp()144 void BleScanningManager::CleanUp() {
145 delete instance;
146 instance = nullptr;
147 };
148
149 /**
150 * This function initializes the scanning manager.
151 **/
btm_ble_scanner_init()152 void btm_ble_scanner_init() {
153 BleScannerHciInterface::Initialize();
154 if (BleScannerHciInterface::Get()) {
155 BleScanningManager::Initialize(BleScannerHciInterface::Get());
156 } else {
157 VLOG(1) << __func__ << " BleScannerHciInterface::Get() returns null";
158 }
159 if ((BleScannerHciInterface::Get()) && (BleScanningManager::Get())) {
160 BleScannerHciInterface::Get()->SetScanEventObserver(
161 (BleScanningManagerImpl*)BleScanningManager::Get().get());
162 } else {
163 VLOG(1) << __func__ << " BleScannerHciInterface or BleScanningManager is null";
164 }
165 }
166
167 /*******************************************************************************
168 *
169 * Function btm_ble_scanner_cleanup
170 *
171 * Description This function cleans up scanner control block.
172 *
173 * Parameters
174 * Returns void
175 *
176 ******************************************************************************/
btm_ble_scanner_cleanup(void)177 void btm_ble_scanner_cleanup(void) {
178 std::lock_guard<std::mutex> lock(lock1);
179 BleScanningManager::CleanUp();
180 BleScannerHciInterface::CleanUp();
181 }
182