low_energy_advertiser.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Copyright 2015 Google, Inc.
  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 LowEnergyAdvertiser 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 LowEnergyAdvertiser : public BluetoothInstance {
  37. public:
  38. // The destructor automatically unregisters this client instance from the
  39. // stack.
  40. ~LowEnergyAdvertiser() override;
  41. // Callback type used to return the result of asynchronous operations below.
  42. using StatusCallback = std::function<void(BLEStatus)>;
  43. // Starts advertising based on the given advertising and scan response
  44. // data and the provided |settings|. Reports the result of the operation in
  45. // |callback|. Return true on success, false otherwise. Please see logs for
  46. // details in case of error.
  47. bool StartAdvertising(const AdvertiseSettings& settings,
  48. const AdvertiseData& advertise_data,
  49. const AdvertiseData& scan_response,
  50. const StatusCallback& callback);
  51. // Stops advertising if it was already started. Reports the result of the
  52. // operation in |callback|.
  53. bool StopAdvertising(const StatusCallback& callback);
  54. // Returns true if advertising has been started.
  55. bool IsAdvertisingStarted() const;
  56. // Returns the state of pending advertising operations.
  57. bool IsStartingAdvertising() const;
  58. bool IsStoppingAdvertising() const;
  59. // Returns the current advertising settings.
  60. const AdvertiseSettings& advertise_settings() const {
  61. return advertise_settings_;
  62. }
  63. // BluetoothClientInstace overrides:
  64. const Uuid& GetAppIdentifier() const override;
  65. int GetInstanceId() const override;
  66. private:
  67. friend class LowEnergyAdvertiserFactory;
  68. // Constructor shouldn't be called directly as instances are meant to be
  69. // obtained from the factory.
  70. LowEnergyAdvertiser(const Uuid& uuid, int advertiser_id);
  71. // BluetoothGattInterface::AdvertiserObserver overrides:
  72. void SetDataCallback(uint8_t advertiser_id, uint8_t status);
  73. void SetParamsCallback(uint8_t advertiser_id, uint8_t status);
  74. void EnableCallback(bool enable, uint8_t advertiser_id, uint8_t status);
  75. // Calls and clears the pending callbacks.
  76. void InvokeAndClearStartCallback(BLEStatus status);
  77. void InvokeAndClearStopCallback(BLEStatus status);
  78. // See getters above for documentation.
  79. Uuid app_identifier_;
  80. int advertiser_id_;
  81. // Protects advertising-related members below.
  82. std::mutex adv_fields_lock_;
  83. // Latest advertising settings.
  84. AdvertiseSettings advertise_settings_;
  85. std::atomic_bool adv_started_;
  86. std::unique_ptr<StatusCallback> adv_start_callback_;
  87. std::unique_ptr<StatusCallback> adv_stop_callback_;
  88. DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiser);
  89. };
  90. // LowEnergyAdvertiserFactory is used to register and obtain a per-application
  91. // LowEnergyAdvertiser instance. Users should call RegisterInstance to obtain
  92. // their
  93. // own unique LowEnergyAdvertiser instance that has been registered with the
  94. // Bluetooth stack.
  95. class LowEnergyAdvertiserFactory : public BluetoothInstanceFactory {
  96. public:
  97. // Don't construct/destruct directly except in tests. Instead, obtain a handle
  98. // from an Adapter instance.
  99. explicit LowEnergyAdvertiserFactory();
  100. ~LowEnergyAdvertiserFactory() override;
  101. // BluetoothInstanceFactory override:
  102. bool RegisterInstance(const Uuid& app_uuid,
  103. const RegisterCallback& callback) override;
  104. private:
  105. friend class LowEnergyAdvertiser;
  106. // BluetoothGattInterface::AdvertiserObserver overrides:
  107. void RegisterAdvertiserCallback(const RegisterCallback& callback,
  108. const Uuid& app_uuid, uint8_t advertiser_id,
  109. uint8_t status);
  110. // Map of pending calls to register.
  111. std::mutex pending_calls_lock_;
  112. std::unordered_set<Uuid> pending_calls_;
  113. DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiserFactory);
  114. };
  115. } // namespace bluetooth