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,dutyRatio and matching mode. 56 * 57 * @param [in] for finding exact BLE devices. 58 * @param [in] callback type. 59 */ 60 void StartScan(const BleScanSettingsImpl &setting) const; 61 62 /** 63 * @brief Stops Bluetooth LE scan 64 */ 65 void StopScan() const; 66 67 /** 68 * @brief Start or stop Bluetooth LE scan 69 */ 70 void StartOrStopScan(const STOP_SCAN_TYPE &scanType, bool isStartScan = false) const; 71 72 /** 73 * @brief config scan filter. 74 * 75 * @param [in] filter filter list 76 * @return ret 77 */ 78 int ConfigScanFilter(int32_t scannerId, const std::vector<BleScanFilterImpl> &filter); 79 80 /** 81 * @brief remove scan filter. 82 * 83 * @param [in] scannerId scanner id 84 */ 85 void RemoveScanFilter(int32_t scannerId); 86 87 /** 88 * @brief Get device address type 89 * 90 * @param [in] address 91 * @return @c address type 92 */ 93 int GetDeviceAddrType(const std::string &address) const; 94 95 /** 96 * @brief Get scan status 97 * 98 * @return @c scan status 99 */ 100 int GetScanStatus() const; 101 102 /** 103 * @brief find scan device 104 * 105 * @param [in] device address 106 * @return @c true: finded, otherwise false 107 */ 108 int GetDeviceType(const std::string &address) const; 109 110 /** 111 * @brief deregister scan callback to gap 112 * 113 * @return @c status. 114 */ 115 int DeregisterCallbackToGap() const; 116 117 /** 118 * @brief get device name 119 * 120 * @return @c name. 121 */ 122 std::string GetDeviceName(const std::string &address) const; 123 124 /** 125 * @brief clear scan result 126 * 127 */ 128 void ClearResults() const; 129 130 /** 131 * @brief Alloc scan object id 132 * 133 * @return scannerId. 134 */ 135 int32_t AllocScannerId(); 136 137 /** 138 * @brief Remove scan object id. 139 * 140 * @param scannerId scanner id. 141 */ 142 void RemoveScannerId(int32_t scannerId); 143 144 /** 145 * @brief clear scannerIds 146 * 147 */ 148 void ClearScannerIds(); 149 150 private: 151 /** 152 * @brief Perform an active or passive scan? 153 * The default is a passive scan. 154 * @param [in] active If true, perform an active scan otherwise a passive scan. 155 */ 156 void SetActiveScan(bool active) const; 157 158 /** 159 * @brief Set the interval to scan. 160 * @param [in] The interval in msecs. 161 */ 162 void SetInterval(uint16_t intervalMSecs) const; 163 164 /** 165 * @brief Start scan. 166 * 167 * @param [in] enable. 168 * @return @c status 169 */ 170 int SetScanEnable(bool enable) const; 171 int SetExScanEnable(bool enable) const; 172 173 /** 174 * @brief Set the window to actively scan. 175 * @param [in] windowMSecs How long to actively scan. 176 */ 177 void SetWindow(uint16_t windowMSecs) const; 178 179 /** 180 * @brief Start scanning. 181 * @param [in] are we continue scan (true) or we want to clear stored devices (false) 182 * @return True if scan started or false if there was an error. 183 */ 184 bool Start(bool isContinue = false) const; 185 186 /** 187 * @brief Stop scan. 188 */ 189 void Stop() const; 190 191 /** 192 * @brief Set scan parameter. 193 * 194 */ 195 int SetScanParamToGap() const; 196 int SetExScanParamToGap() const; 197 198 /** 199 * @brief scan callback result 200 * 201 * @param [in] advertising type. 202 * @param [in] peer address. 203 * @param [in] scan data length. 204 * @param [in] rssi value. 205 * @param [in] context. 206 */ 207 static void AdvertisingReport(uint8_t advType, const BtAddr *peerAddr, GapAdvReportParam reportParam, 208 const BtAddr *currentAddr, void *context); 209 static void ExAdvertisingReport( 210 uint8_t advType, const BtAddr *addr, GapExAdvReportParam reportParam, const BtAddr *currentAddr, void *context); 211 212 void AdvertisingReportTask( 213 uint8_t advType, const BtAddr &peerAddr, const std::vector<uint8_t> &data, int8_t rssi) const; 214 void ExAdvertisingReportTask(uint8_t advType, const BtAddr &peerAddr, const std::vector<uint8_t> &data, int8_t rssi, 215 const BtAddr &peerCurrentAddr) const; 216 bool AddPeripheralDevice(uint8_t advType, const BtAddr &peerAddr, const std::vector<uint8_t> &data, int8_t rssi, 217 const BlePeripheralDevice &dev) const; 218 /** 219 * @brief set scan parameters callback from gap 220 * 221 * @param [in] status. 222 * @param [in] context. 223 */ 224 static void ScanSetParamResult(uint8_t status, void *context); 225 static void ScanExSetParamResult(uint8_t status, void *context); 226 227 /** 228 * @brief set scan enable callback from gap 229 * 230 * @param [in] status. 231 * @param [in] context. 232 */ 233 static void ScanSetEnableResult(uint8_t status, void *context); 234 static void ScanExSetEnableResult(uint8_t status, void *context); 235 236 /** 237 * @brief find scan device 238 * 239 * @param [in] device address 240 * @return @c true: finded, otherwise false 241 */ 242 bool FindDevice(const std::string &address, BlePeripheralDevice &dev) const; 243 244 /** 245 * @brief get scan inteval from scan mode 246 * 247 * @param [in] scan mode 248 */ 249 void SetScanModeDuration(int scanMode, int type) const; 250 251 /** 252 * @brief set low power duration 253 * 254 * @param [in] type callback type 255 */ 256 void SetLowPowerDuration(int type) const; 257 258 /** 259 * @brief set balanced duration 260 * 261 * @param [in] type callback type 262 */ 263 void SetBalancedDuration(int type) const; 264 265 /** 266 * @brief set low latency duration 267 * 268 * @param [in] type callback type 269 */ 270 void SetLowLatencyDuration(int type) const; 271 272 /** 273 * @brief set duty cycle duration 274 * 275 * @param [in] type callback type 276 */ 277 void SetDutyCycle2Duration(int type) const; 278 279 /** 280 * @brief set duty cycle duration 281 * 282 * @param [in] type callback type 283 */ 284 void SetDutyCycle10Duration(int type) const; 285 286 /** 287 * @brief set duty cycle duration 288 * 289 * @param [in] type callback type 290 */ 291 void SetDutyCycle25Duration(int type) const; 292 293 /** 294 * @brief set duty cycle duration 295 * 296 * @param [in] type callback type 297 */ 298 void SetDutyCycle100Duration(int type) const; 299 300 /** 301 * @brief set duty cycle duration 302 * 303 * @param [in] type callback type 304 */ 305 void SetDutyCycle50Duration(int type) const; 306 307 /** 308 * @brief set duty cycle duration 309 * 310 * @param [in] type callback type 311 */ 312 void SetDutyCycle1030Duration(int type) const; 313 314 /** 315 * @brief set duty cycle duration 316 * 317 * @param [in] type callback type 318 */ 319 void SetDutyCycle230Duration(int type) const; 320 321 /** 322 * @brief set duty cycle duration 323 * 324 * @param [in] type callback type 325 */ 326 void SetDutyCycle75Duration(int type) const; 327 328 /** 329 * @brief set duty cycle duration 330 * 331 * @param [in] type callback type 332 */ 333 void SetDutyCycle5030Duration(int type) const; 334 335 /** 336 * @brief Gap scan event 337 * 338 * @param [in] event gap event. 339 * @param [in] status gap callback status. 340 */ 341 void HandleGapEvent(const BLE_GAP_CB_EVENT &event, int status) const; 342 343 /** 344 * @brief Gap extend scan event 345 * 346 * @param [in] event gap event. 347 * @param [in] status gap callback status. 348 */ 349 void HandleGapExScanEvent(const BLE_GAP_CB_EVENT &event, int status) const; 350 351 /** 352 * @brief timer call back 353 * 354 */ 355 static void TimerCallback(void *context); 356 357 /** 358 * @brief Set scan enable callback from gap 359 * 360 * @param [in] status. 361 */ 362 void ScanSetEnableResultTask(uint8_t status) const; 363 void ScanExSetEnableResultTask(uint8_t status) const; 364 365 static bool CheckBleScanMode(uint8_t falg); 366 void GapScanParamSetCompleteEvt(int status) const; 367 void GapScanResultEvt() const; 368 void GapScanDelayReportResultEvt() const; 369 void GapScanStartCompleteEvt(int status) const; 370 void GapScanStopCompleteEvt(int status) const; 371 void GapScanResolvingCompletEvt(int status, bool isStart) const; 372 373 void GapExScanParamSetCompleteEvt(int status) const; 374 void GapExScanResultEvt() const; 375 void GapExScanDelayReportResultEvt() const; 376 void GapExScanStartCompleteEvt(int status) const; 377 void GapExScanStopCompleteEvt(int status) const; 378 void GapExScanResolvingCompletEvt(int status, bool isStart) const; 379 bool SetScanParamOrExScanParamToGap() const; 380 bool SetLegacyScanParamToGap() const; 381 bool SetExtendScanParamToGap() const; 382 383 static void DirectedAdvertisingReport(uint8_t advType, const BtAddr *addr, GapDirectedAdvReportParam reportParam, 384 const BtAddr *currentAddr, void *context); 385 static void ScanTimeoutEvent(void *context); 386 bool ExtractIncompleteData(uint8_t advType, const std::string &advertisedAddress, const std::vector<uint8_t> &data, 387 std::vector<uint8_t> &completeData) const; 388 389 static void AddBleScanFilterResult(uint8_t result, void *context); 390 static void StartBleScanFilterResult(uint8_t result, void *context); 391 static void DeleteBleScanFilterResult(uint8_t result, void *context); 392 static void StopBleScanFilterResult(uint8_t result, void *context); 393 void HandleAddBleScanFilterResult(uint8_t result); 394 void HandleDeleteBleScanFilterResult(uint8_t result); 395 void HandleStartBleScanFilterResult(uint8_t result); 396 void HandleStopBleScanFilterResult(uint8_t result); 397 void LoadBleScanFilterLib(); 398 void UnloadBleScanFilterLib(); 399 bool CheckScanFilterConfig(const std::vector<BleScanFilterImpl> &filters); 400 void PushFilterToWaitList(BleScanFilterImpl filter, int scannerId, uint8_t action); 401 void PushStartOrStopAction(const int scannerId, uint8_t action); 402 void AddBleScanFilter(BleScanFilterImpl filter); 403 void DeleteBleScanFilter(BleScanFilterImpl filter); 404 void StartBleScanFilter(); 405 void StopBleScanFilter(); 406 void HandleWaitFilters(); 407 void DoFilterStatusBad(); 408 void TryConfigScanFilter(int scannerId); 409 void BleScanFilterParamAddDeviceAddress(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 410 void BleScanFilterParamAddName(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 411 void BleScanFilterParamAddServiceUuid(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 412 void BleScanFilterParamAddSolicitationUuid(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 413 void BleScanFilterParamAddServiceData(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 414 void BleScanFilterParamAddManufactureData(BleScanFilterParam &filterParam, BleScanFilterImpl filter); 415 416 /// scan callback 417 IBleCentralManagerCallback *centralManagerCallbacks_ = nullptr; 418 bool isDuplicates_ = false; 419 IAdapterBle *bleAdapter_ = nullptr; 420 /// The dispatcher that is used to switch to the thread. 421 utility::Dispatcher *dispatcher_ = nullptr; 422 std::map<std::string, std::vector<uint8_t>> incompleteData_{}; 423 IBleScanFilter* bleScanFilter_ = nullptr; 424 void *bleScanFilterLib_ = nullptr; 425 426 /// filter action 427 static const uint8_t FILTER_ACTION_ADD = 0x00; 428 static const uint8_t FILTER_ACTION_DELETE = 0x01; 429 static const uint8_t FILTER_ACTION_START = 0x02; 430 static const uint8_t FILTER_ACTION_STOP = 0x03; 431 432 std::set<int32_t> scannerIds_ {}; 433 434 BT_DISALLOW_COPY_AND_ASSIGN(BleCentralManagerImpl); 435 DECLARE_IMPL(); 436 }; 437 } // namespace bluetooth 438 } // namespace OHOS 439 440 #endif // BLE_CENTRAL_MANAGER_IMPL_H 441