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