low_energy_scanner.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Copyright 2016 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. #pragma once
  17. #include <atomic>
  18. #include <functional>
  19. #include <map>
  20. #include <mutex>
  21. #include <base/macros.h>
  22. #include <bluetooth/uuid.h>
  23. #include "service/bluetooth_instance.h"
  24. #include "service/common/bluetooth/advertise_data.h"
  25. #include "service/common/bluetooth/advertise_settings.h"
  26. #include "service/common/bluetooth/low_energy_constants.h"
  27. #include "service/common/bluetooth/scan_filter.h"
  28. #include "service/common/bluetooth/scan_result.h"
  29. #include "service/common/bluetooth/scan_settings.h"
  30. #include "service/hal/bluetooth_gatt_interface.h"
  31. namespace bluetooth {
  32. class Adapter;
  33. // A LowEnergyScanner represents an application's handle to perform various
  34. // Bluetooth Low Energy GAP operations. Instances cannot be created directly and
  35. // should be obtained through the factory.
  36. class LowEnergyScanner : private hal::BluetoothGattInterface::ScannerObserver,
  37. public BluetoothInstance {
  38. public:
  39. // The Delegate interface is used to notify asynchronous events related to LE
  40. // scan.
  41. class Delegate {
  42. public:
  43. Delegate() = default;
  44. virtual ~Delegate() = default;
  45. // Called asynchronously to notify the delegate of nearby BLE advertisers
  46. // found during a device scan.
  47. virtual void OnScanResult(LowEnergyScanner* client,
  48. const ScanResult& scan_result) = 0;
  49. private:
  50. DISALLOW_COPY_AND_ASSIGN(Delegate);
  51. };
  52. // The destructor automatically unregisters this client instance from the
  53. // stack.
  54. ~LowEnergyScanner() override;
  55. // Assigns a delegate to this instance. |delegate| must out-live this
  56. // LowEnergyClient instance.
  57. void SetDelegate(Delegate* delegate);
  58. // Initiates a BLE device scan for this client using the given |settings| and
  59. // |filters|. See the documentation for ScanSettings and ScanFilter for how
  60. // these parameters can be configured. Return true on success, false
  61. // otherwise. Please see logs for details in case of error.
  62. bool StartScan(const ScanSettings& settings,
  63. const std::vector<ScanFilter>& filters);
  64. // Stops an ongoing BLE device scan for this client.
  65. bool StopScan();
  66. // Returns the current scan settings.
  67. const ScanSettings& scan_settings() const { return scan_settings_; }
  68. // BluetoothInstace overrides:
  69. const Uuid& GetAppIdentifier() const override;
  70. int GetInstanceId() const override;
  71. void ScanResultCallback(hal::BluetoothGattInterface* gatt_iface,
  72. const RawAddress& bda, int rssi,
  73. std::vector<uint8_t> adv_data) override;
  74. private:
  75. friend class LowEnergyScannerFactory;
  76. // Constructor shouldn't be called directly as instances are meant to be
  77. // obtained from the factory.
  78. LowEnergyScanner(Adapter& adapter, const Uuid& uuid, int scanner_id);
  79. // Calls and clears the pending callbacks.
  80. void InvokeAndClearStartCallback(BLEStatus status);
  81. void InvokeAndClearStopCallback(BLEStatus status);
  82. // Raw pointer to the Bluetooth Adapter.
  83. Adapter& adapter_;
  84. // See getters above for documentation.
  85. Uuid app_identifier_;
  86. int scanner_id_;
  87. // Protects device scan related members below.
  88. std::mutex scan_fields_lock_;
  89. // Current scan settings.
  90. ScanSettings scan_settings_;
  91. // If true, then this client have a BLE device scan in progress.
  92. std::atomic_bool scan_started_;
  93. // Raw handle to the Delegate, which must outlive this LowEnergyScanner
  94. // instance.
  95. std::mutex delegate_mutex_;
  96. Delegate* delegate_;
  97. DISALLOW_COPY_AND_ASSIGN(LowEnergyScanner);
  98. };
  99. // LowEnergyScannerFactory is used to register and obtain a per-application
  100. // LowEnergyScanner instance. Users should call RegisterInstance to obtain their
  101. // own unique LowEnergyScanner instance that has been registered with the
  102. // Bluetooth stack.
  103. class LowEnergyScannerFactory
  104. : private hal::BluetoothGattInterface::ScannerObserver,
  105. public BluetoothInstanceFactory {
  106. public:
  107. // Don't construct/destruct directly except in tests. Instead, obtain a handle
  108. // from an Adapter instance.
  109. explicit LowEnergyScannerFactory(Adapter& adapter);
  110. ~LowEnergyScannerFactory() override;
  111. // BluetoothInstanceFactory override:
  112. bool RegisterInstance(const Uuid& app_uuid,
  113. const RegisterCallback& callback) override;
  114. private:
  115. friend class LowEnergyScanner;
  116. // BluetoothGattInterface::ScannerObserver overrides:
  117. void RegisterScannerCallback(const RegisterCallback& callback,
  118. const Uuid& app_uuid, uint8_t scanner_id,
  119. uint8_t status);
  120. // Map of pending calls to register.
  121. std::mutex pending_calls_lock_;
  122. std::unordered_set<Uuid> pending_calls_;
  123. // Raw pointer to the Adapter that owns this factory.
  124. Adapter& adapter_;
  125. DISALLOW_COPY_AND_ASSIGN(LowEnergyScannerFactory);
  126. };
  127. } // namespace bluetooth