1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BLE_CENTRAL_MANAGER_IMPL_H 17 #define BLE_CENTRAL_MANAGER_IMPL_H 18 19 #include <map> 20 #include <vector> 21 #include <set> 22 23 #include "ble_defs.h" 24 #include "ble_scan_filter/include/i_ble_scan_filter.h" 25 #include "dispatcher.h" 26 #include "gap_le_if.h" 27 #include "interface_adapter_ble.h" 28 #include "log.h" 29 #include "log_util.h" 30 #include "semaphore_utils.h" 31 #include "timer.h" 32 33 /* 34 * @brief The Bluetooth subsystem. 35 */ 36 namespace OHOS { 37 namespace bluetooth { 38 /** 39 * @brief BLE scan manager. 40 */ 41 class BleCentralManagerImpl { 42 public: 43 /** 44 * @brief Constructor. 45 */ 46 explicit BleCentralManagerImpl( 47 IBleCentralManagerCallback &callback, IAdapterBle &bleAdapter, utility::Dispatcher &dispatch); 48 49 /** 50 * @brief Destructor. 51 */ 52 ~BleCentralManagerImpl(); 53 54 /** 55 * @brief Start Bluetooth LE scan with filters. 56 * 57 * @param [in] for finding exact BLE devices. 58 */ 59 void StartScan() const; 60 61 /** 62 * @brief Start Bluetooth LE scan with filters,dutyRatio and matching mode. 63 * 64 * @param [in] for finding exact BLE devices. 65 * @param [in] callback type. 66 */ 67 void StartScan(const BleScanSettingsImpl &setting) const; 68 69 /** 70 * @brief Stops Bluetooth LE scan 71 */ 72 void StopScan() const; 73 74 /** 75 * @brief Start or stop Bluetooth LE scan 76 */ 77 void StartOrStopScan(const STOP_SCAN_TYPE &scanType, bool isStartScan = false) const; 78 79 /** 80 * @brief config scan filter. 81 * 82 * @param [in] filter filter list 83 * @return ret 84 */ 85 int ConfigScanFilter(int32_t scannerId, const std::vector<BleScanFilterImpl> &filter); 86 87 /** 88 * @brief remove scan filter. 89 * 90 * @param [in] scannerId scanner id 91 */ 92 void RemoveScanFilter(int32_t scannerId); 93 94 /** 95 * @brief Get device address type 96 * 97 * @param [in] address 98 * @return @c address type 99 */ 100 int GetDeviceAddrType(const std::string &address) const; 101 102 /** 103 * @brief Get scan status 104 * 105 * @return @c scan status 106 */ 107 int GetScanStatus() const; 108 109 /** 110 * @brief find scan device 111 * 112 * @param [in] device address 113 * @return @c true: finded, otherwise false 114 */ 115 int GetDeviceType(const std::string &address) const; 116 117 /** 118 * @brief deregister scan callback to gap 119 * 120 * @return @c status. 121 */ 122 int DeregisterCallbackToGap() const; 123 124 /** 125 * @brief get device name 126 * 127 * @return @c name. 128 */ 129 std::string GetDeviceName(const std::string &address) const; 130 131 /** 132 * @brief clear scan result 133 * 134 */ 135 void ClearResults() const; 136 137 /** 138 * @brief Alloc scan object id 139 * 140 * @return scannerId. 141 */ 142 int32_t AllocScannerId(); 143 144 /** 145 * @brief Remove scan object id. 146 * 147 * @param scannerId scanner id. 148 */ 149 void RemoveScannerId(int32_t scannerId); 150 151 /** 152 * @brief clear scannerIds 153 * 154 */ 155 void ClearScannerIds(); 156 157 private: 158 /** 159 * @brief Perform an active or passive scan? 160 * The default is a passive scan. 161 * @param [in] active If true, perform an active scan otherwise a passive scan. 162 */ 163 void SetActiveScan(bool active) const; 164 165 /** 166 * @brief Set the interval to scan. 167 * @param [in] The interval in msecs. 168 */ 169 void SetInterval(uint16_t intervalMSecs) const; 170 171 /** 172 * @brief Start scan. 173 * 174 * @param [in] enable. 175 * @return @c status 176 */ 177 int SetScanEnable(bool enable) const; 178 int SetExScanEnable(bool enable) const; 179 180 /** 181 * @brief Set the window to actively scan. 182 * @param [in] windowMSecs How long to actively scan. 183 */ 184 void SetWindow(uint16_t windowMSecs) const; 185 186 /** 187 * @brief Start scanning. 188 * @param [in] are we continue scan (true) or we want to clear stored devices (false) 189 * @return True if scan started or false if there was an error. 190 */ 191 bool Start(bool isContinue = false) const; 192 193 /** 194 * @brief Stop scan. 195 */ 196 void Stop() const; 197 198 /** 199 * @brief Set scan parameter. 200 * 201 */ 202 int SetScanParamToGap() const; 203 int SetExScanParamToGap() const; 204 205 /** 206 * @brief scan callback result 207 * 208 * @param [in] advertising type. 209 * @param [in] peer address. 210 * @param [in] scan data length. 211 * @param [in] rssi value. 212 * @param [in] context. 213 */ 214 static void AdvertisingReport(uint8_t advType, const BtAddr *peerAddr, GapAdvReportParam reportParam, 215 const BtAddr *currentAddr, void *context); 216 static void ExAdvertisingReport( 217 uint8_t advType, const BtAddr *addr, GapExAdvReportParam reportParam, const BtAddr *currentAddr, void *context); 218 219 void AdvertisingReportTask( 220 uint8_t advType, const BtAddr &peerAddr, const std::vector<uint8_t> &data, int8_t rssi) const; 221 void ExAdvertisingReportTask(uint8_t advType, const BtAddr &peerAddr, const std::vector<uint8_t> &data, int8_t rssi, 222 const BtAddr &peerCurrentAddr) const; 223 bool AddPeripheralDevice(uint8_t advType, const BtAddr &peerAddr, const std::vector<uint8_t> &data, int8_t rssi, 224 const BlePeripheralDevice &dev) const; 225 /** 226 * @brief set scan parameters callback from gap 227 * 228 * @param [in] status. 229 * @param [in] context. 230 */ 231 static void ScanSetParamResult(uint8_t status, void *context); 232 static void ScanExSetParamResult(uint8_t status, void *context); 233 234 /** 235 * @brief set scan enable callback from gap 236 * 237 * @param [in] status. 238 * @param [in] context. 239 */ 240 static void ScanSetEnableResult(uint8_t status, void *context); 241 static void ScanExSetEnableResult(uint8_t status, void *context); 242 243 /** 244 * @brief find scan device 245 * 246 * @param [in] device address 247 * @return @c true: finded, otherwise false 248 */ 249 bool FindDevice(const std::string &address, BlePeripheralDevice &dev) const; 250 251 /** 252 * @brief get scan inteval from scan mode 253 * 254 * @param [in] scan mode 255 */ 256 void SetScanModeDuration(int scanMode, int type) const; 257 258 /** 259 * @brief set low power duration 260 * 261 * @param [in] type callback type 262 */ 263 void SetLowPowerDuration(int type) const; 264 265 /** 266 * @brief set balanced duration 267 * 268 * @param [in] type callback type 269 */ 270 void SetBalancedDuration(int type) const; 271 272 /** 273 * @brief set low latency duration 274 * 275 * @param [in] type callback type 276 */ 277 void SetLowLatencyDuration(int type) const; 278 279 /** 280 * @brief set duty cycle duration 281 * 282 * @param [in] type callback type 283 */ 284 void SetDutyCycle2Duration(int type) const; 285 286 /** 287 * @brief set duty cycle duration 288 * 289 * @param [in] type callback type 290 */ 291 void SetDutyCycle10Duration(int type) const; 292 293 /** 294 * @brief set duty cycle duration 295 * 296 * @param [in] type callback type 297 */ 298 void SetDutyCycle25Duration(int type) const; 299 300 /** 301 * @brief set duty cycle duration 302 * 303 * @param [in] type callback type 304 */ 305 void SetDutyCycle100Duration(int type) const; 306 307 /** 308 * @brief Gap scan event 309 * 310 * @param [in] event gap event. 311 * @param [in] status gap callback status. 312 */ 313 void HandleGapEvent(const BLE_GAP_CB_EVENT &event, int status) const; 314 315 /** 316 * @brief Gap extend scan event 317 * 318 * @param [in] event gap event. 319 * @param [in] status gap callback status. 320 */ 321 void HandleGapExScanEvent(const BLE_GAP_CB_EVENT &event, int status) const; 322 323 /** 324 * @brief timer call back 325 * 326 */ 327 static void TimerCallback(void *context); 328 329 /** 330 * @brief Set scan enable callback from gap 331 * 332 * @param [in] status. 333 */ 334 void ScanSetEnableResultTask(uint8_t status) const; 335 void ScanExSetEnableResultTask(uint8_t status) const; 336 337 static bool CheckBleScanMode(uint8_t falg); 338 void GapScanParamSetCompleteEvt(int status) const; 339 void GapScanResultEvt() const; 340 void GapScanDelayReportResultEvt() const; 341 void GapScanStartCompleteEvt(int status) const; 342 void GapScanStopCompleteEvt(int status) const; 343 void GapScanResolvingCompletEvt(int status, bool isStart) const; 344 345 void GapExScanParamSetCompleteEvt(int status) const; 346 void GapExScanResultEvt() const; 347 void GapExScanDelayReportResultEvt() const; 348 void GapExScanStartCompleteEvt(int status) const; 349 void GapExScanStopCompleteEvt(int status) const; 350 void GapExScanResolvingCompletEvt(int status, bool isStart) const; 351 bool SetScanParamOrExScanParamToGap() const; 352 bool SetLegacyScanParamToGap() const; 353 bool SetExtendScanParamToGap() const; 354 355 static void DirectedAdvertisingReport(uint8_t advType, const BtAddr *addr, GapDirectedAdvReportParam reportParam, 356 const BtAddr *currentAddr, void *context); 357 static void ScanTimeoutEvent(void *context); 358 bool ExtractIncompleteData(uint8_t advType, const std::string &advertisedAddress, const std::vector<uint8_t> &data, 359 std::vector<uint8_t> &completeData) const; 360 361 static void AddBleScanFilterResult(uint8_t result, void *context); 362 static void StartBleScanFilterResult(uint8_t result, void *context); 363 static void DeleteBleScanFilterResult(uint8_t result, void *context); 364 static void StopBleScanFilterResult(uint8_t result, void *context); 365 void HandleAddBleScanFilterResult(uint8_t result); 366 void HandleDeleteBleScanFilterResult(uint8_t result); 367 void HandleStartBleScanFilterResult(uint8_t result); 368 void HandleStopBleScanFilterResult(uint8_t result); 369 void LoadBleScanFilterLib(); 370 void UnloadBleScanFilterLib(); 371 bool CheckScanFilterConfig(const std::vector<BleScanFilterImpl> &filters); 372 void PushFilterToWaitList(BleScanFilterImpl filter, int scannerId, uint8_t action); 373 void PushStartOrStopAction(const int scannerId, uint8_t action); 374 void AddBleScanFilter(BleScanFilterImpl filter); 375 void DeleteBleScanFilter(BleScanFilterImpl filter); 376 void StartBleScanFilter(); 377 void StopBleScanFilter(); 378 void HandleWaitFilters(); 379 void DoFilterStatusBad(); 380 void TryConfigScanFilter(int scannerId); 381 void BleScanFilterParamAddDeviceAddress(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 382 void BleScanFilterParamAddName(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 383 void BleScanFilterParamAddServiceUuid(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 384 void BleScanFilterParamAddSolicitationUuid(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 385 void BleScanFilterParamAddServiceData(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 386 void BleScanFilterParamAddManufactureData(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 387 388 /// scan callback 389 IBleCentralManagerCallback *centralManagerCallbacks_ = nullptr; 390 bool isDuplicates_ = false; 391 IAdapterBle *bleAdapter_ = nullptr; 392 /// The dispatcher that is used to switch to the thread. 393 utility::Dispatcher *dispatcher_ = nullptr; 394 std::map<std::string, std::vector<uint8_t>> incompleteData_{}; 395 IBleScanFilter* bleScanFilter_ = nullptr; 396 void *bleScanFilterLib_ = nullptr; 397 398 /// filter action 399 static const uint8_t FILTER_ACTION_ADD = 0x00; 400 static const uint8_t FILTER_ACTION_DELETE = 0x01; 401 static const uint8_t FILTER_ACTION_START = 0x02; 402 static const uint8_t FILTER_ACTION_STOP = 0x03; 403 404 std::set<int32_t> scannerIds_ {}; 405 406 BT_DISALLOW_COPY_AND_ASSIGN(BleCentralManagerImpl); 407 DECLARE_IMPL(); 408 }; 409 } // namespace bluetooth 410 } // namespace OHOS 411 412 #endif // BLE_CENTRAL_MANAGER_IMPL_H 413