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