bluetooth_advertisement.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/callback.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/observer_list.h"
  15. #include "build/build_config.h"
  16. #include "device/bluetooth/bluetooth_export.h"
  17. namespace device {
  18. // BluetoothAdvertisement represents an advertisement which advertises over the
  19. // LE channel during its lifetime.
  20. class DEVICE_BLUETOOTH_EXPORT BluetoothAdvertisement
  21. : public base::RefCounted<BluetoothAdvertisement> {
  22. public:
  23. // Possible types of error raised while registering or unregistering
  24. // advertisements.
  25. enum ErrorCode {
  26. ERROR_UNSUPPORTED_PLATFORM, // Bluetooth advertisement not supported on
  27. // current platform.
  28. ERROR_ADVERTISEMENT_ALREADY_EXISTS, // An advertisement is already
  29. // registered.
  30. ERROR_ADVERTISEMENT_DOES_NOT_EXIST, // Unregistering an advertisement which
  31. // is not registered.
  32. ERROR_ADVERTISEMENT_INVALID_LENGTH, // Advertisement is not of a valid
  33. // length.
  34. ERROR_STARTING_ADVERTISEMENT, // Error when starting the advertisement
  35. // through a platform API.
  36. ERROR_RESET_ADVERTISING, // Error while resetting advertising.
  37. ERROR_ADAPTER_POWERED_OFF, // Error because the adapter is off
  38. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  39. ERROR_INVALID_ADVERTISEMENT_INTERVAL, // Advertisement interval specified
  40. // is out of valid range.
  41. #endif
  42. INVALID_ADVERTISEMENT_ERROR_CODE
  43. };
  44. // Type of advertisement.
  45. enum AdvertisementType {
  46. // This advertises with the type set to ADV_NONCONN_IND, which indicates
  47. // to receivers that our device is not connectable.
  48. ADVERTISEMENT_TYPE_BROADCAST,
  49. // This advertises with the type set to ADV_IND or ADV_SCAN_IND, which
  50. // indicates to receivers that our device is connectable.
  51. ADVERTISEMENT_TYPE_PERIPHERAL
  52. };
  53. using UUIDList = std::vector<std::string>;
  54. using ManufacturerData = std::map<uint16_t, std::vector<uint8_t>>;
  55. using ServiceData = std::map<std::string, std::vector<uint8_t>>;
  56. using ScanResponseData = std::map<uint8_t, std::vector<uint8_t>>;
  57. // Structure that holds the data for an advertisement.
  58. class DEVICE_BLUETOOTH_EXPORT Data {
  59. public:
  60. explicit Data(AdvertisementType type);
  61. Data(const Data&) = delete;
  62. Data& operator=(const Data&) = delete;
  63. ~Data();
  64. AdvertisementType type() { return type_; }
  65. std::unique_ptr<UUIDList> service_uuids() {
  66. return std::move(service_uuids_);
  67. }
  68. std::unique_ptr<ManufacturerData> manufacturer_data() {
  69. return std::move(manufacturer_data_);
  70. }
  71. std::unique_ptr<UUIDList> solicit_uuids() {
  72. return std::move(solicit_uuids_);
  73. }
  74. std::unique_ptr<ServiceData> service_data() {
  75. return std::move(service_data_);
  76. }
  77. std::unique_ptr<ScanResponseData> scan_response_data() {
  78. return std::move(scan_response_data_);
  79. }
  80. void set_service_uuids(std::unique_ptr<UUIDList> service_uuids) {
  81. service_uuids_ = std::move(service_uuids);
  82. }
  83. void set_manufacturer_data(
  84. std::unique_ptr<ManufacturerData> manufacturer_data) {
  85. manufacturer_data_ = std::move(manufacturer_data);
  86. }
  87. void set_solicit_uuids(std::unique_ptr<UUIDList> solicit_uuids) {
  88. solicit_uuids_ = std::move(solicit_uuids);
  89. }
  90. void set_service_data(std::unique_ptr<ServiceData> service_data) {
  91. service_data_ = std::move(service_data);
  92. }
  93. void set_scan_response_data(
  94. std::unique_ptr<ScanResponseData> scan_response_data) {
  95. scan_response_data_ = std::move(scan_response_data);
  96. }
  97. void set_include_tx_power(bool include_tx_power) {
  98. include_tx_power_ = include_tx_power;
  99. }
  100. private:
  101. Data();
  102. AdvertisementType type_;
  103. std::unique_ptr<UUIDList> service_uuids_;
  104. std::unique_ptr<ManufacturerData> manufacturer_data_;
  105. std::unique_ptr<UUIDList> solicit_uuids_;
  106. std::unique_ptr<ServiceData> service_data_;
  107. std::unique_ptr<ScanResponseData> scan_response_data_;
  108. bool include_tx_power_;
  109. };
  110. // Interface for observing changes to this advertisement.
  111. class Observer {
  112. public:
  113. virtual ~Observer() {}
  114. // Called when this advertisement is released and is no longer advertising.
  115. virtual void AdvertisementReleased(
  116. BluetoothAdvertisement* advertisement) = 0;
  117. };
  118. BluetoothAdvertisement(const BluetoothAdvertisement&) = delete;
  119. BluetoothAdvertisement& operator=(const BluetoothAdvertisement&) = delete;
  120. // Adds and removes observers for events for this advertisement.
  121. void AddObserver(BluetoothAdvertisement::Observer* observer);
  122. void RemoveObserver(BluetoothAdvertisement::Observer* observer);
  123. // Unregisters this advertisement. Called on destruction of this object
  124. // automatically but can be called directly to explicitly unregister this
  125. // object.
  126. using SuccessCallback = base::OnceClosure;
  127. using ErrorCallback = base::OnceCallback<void(ErrorCode)>;
  128. virtual void Unregister(SuccessCallback success_callback,
  129. ErrorCallback error_callback) = 0;
  130. protected:
  131. friend class base::RefCounted<BluetoothAdvertisement>;
  132. BluetoothAdvertisement();
  133. // The destructor will unregister this advertisement.
  134. virtual ~BluetoothAdvertisement();
  135. // List of observers interested in event notifications from us. Objects in
  136. // |observers_| are expected to outlive a BluetoothAdvertisement object.
  137. base::ObserverList<BluetoothAdvertisement::Observer>::Unchecked observers_;
  138. };
  139. } // namespace device
  140. #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_