rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 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 | |
| 5 | #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_ |
| 6 | #define DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_ |
| 7 | |
avi | 7171ac7 | 2015-12-23 04:26:52 +0900 | [diff] [blame] | 8 | #include <stdint.h> |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 9 | |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 10 | #include <map> |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 11 | #include <memory> |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 12 | #include <string> |
dcheng | f10f6a2 | 2015-12-28 07:05:10 +0900 | [diff] [blame] | 13 | #include <utility> |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | #include "base/callback.h" |
avi | 7171ac7 | 2015-12-23 04:26:52 +0900 | [diff] [blame] | 17 | #include "base/macros.h" |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 18 | #include "base/memory/ref_counted.h" |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 19 | #include "base/observer_list.h" |
Michael Giuffrida | 405bd06 | 2017-08-02 22:22:44 +0900 | [diff] [blame] | 20 | #include "build/build_config.h" |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 21 | #include "device/bluetooth/bluetooth_export.h" |
| 22 | |
| 23 | namespace device { |
| 24 | |
| 25 | // BluetoothAdvertisement represents an advertisement which advertises over the |
| 26 | // LE channel during its lifetime. |
| 27 | class DEVICE_BLUETOOTH_EXPORT BluetoothAdvertisement |
| 28 | : public base::RefCounted<BluetoothAdvertisement> { |
| 29 | public: |
| 30 | // Possible types of error raised while registering or unregistering |
| 31 | // advertisements. |
| 32 | enum ErrorCode { |
| 33 | ERROR_UNSUPPORTED_PLATFORM, // Bluetooth advertisement not supported on |
| 34 | // current platform. |
| 35 | ERROR_ADVERTISEMENT_ALREADY_EXISTS, // An advertisement is already |
| 36 | // registered. |
| 37 | ERROR_ADVERTISEMENT_DOES_NOT_EXIST, // Unregistering an advertisement which |
| 38 | // is not registered. |
| 39 | ERROR_ADVERTISEMENT_INVALID_LENGTH, // Advertisement is not of a valid |
| 40 | // length. |
Michael Giuffrida | 405bd06 | 2017-08-02 22:22:44 +0900 | [diff] [blame] | 41 | #if defined(OS_LINUX) |
steel | 977f51f | 2016-09-22 12:40:55 +0900 | [diff] [blame] | 42 | ERROR_INVALID_ADVERTISEMENT_INTERVAL, // Advertisement interval specified |
| 43 | // is out of valid range. |
sonnysasaka | 00489b7 | 2017-06-29 04:12:35 +0900 | [diff] [blame] | 44 | ERROR_RESET_ADVERTISING, // Error while resetting advertising. |
steel | 977f51f | 2016-09-22 12:40:55 +0900 | [diff] [blame] | 45 | #endif |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 46 | INVALID_ADVERTISEMENT_ERROR_CODE |
| 47 | }; |
| 48 | |
| 49 | // Type of advertisement. |
| 50 | enum AdvertisementType { |
| 51 | // This advertises with the type set to ADV_NONCONN_IND, which indicates |
| 52 | // to receivers that our device is not connectable. |
| 53 | ADVERTISEMENT_TYPE_BROADCAST, |
| 54 | // This advertises with the type set to ADV_IND or ADV_SCAN_IND, which |
| 55 | // indicates to receivers that our device is connectable. |
| 56 | ADVERTISEMENT_TYPE_PERIPHERAL |
| 57 | }; |
| 58 | |
| 59 | using UUIDList = std::vector<std::string>; |
| 60 | using ManufacturerData = std::map<uint16_t, std::vector<uint8_t>>; |
| 61 | using ServiceData = std::map<std::string, std::vector<uint8_t>>; |
| 62 | |
| 63 | // Structure that holds the data for an advertisement. |
| 64 | class DEVICE_BLUETOOTH_EXPORT Data { |
| 65 | public: |
primiano | 854fd71 | 2015-05-28 02:37:00 +0900 | [diff] [blame] | 66 | explicit Data(AdvertisementType type); |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 67 | ~Data(); |
| 68 | |
| 69 | AdvertisementType type() { return type_; } |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 70 | std::unique_ptr<UUIDList> service_uuids() { |
| 71 | return std::move(service_uuids_); |
| 72 | } |
| 73 | std::unique_ptr<ManufacturerData> manufacturer_data() { |
dcheng | f10f6a2 | 2015-12-28 07:05:10 +0900 | [diff] [blame] | 74 | return std::move(manufacturer_data_); |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 75 | } |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 76 | std::unique_ptr<UUIDList> solicit_uuids() { |
| 77 | return std::move(solicit_uuids_); |
| 78 | } |
| 79 | std::unique_ptr<ServiceData> service_data() { |
| 80 | return std::move(service_data_); |
| 81 | } |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 82 | |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 83 | void set_service_uuids(std::unique_ptr<UUIDList> service_uuids) { |
dcheng | f10f6a2 | 2015-12-28 07:05:10 +0900 | [diff] [blame] | 84 | service_uuids_ = std::move(service_uuids); |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 85 | } |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 86 | void set_manufacturer_data( |
| 87 | std::unique_ptr<ManufacturerData> manufacturer_data) { |
dcheng | f10f6a2 | 2015-12-28 07:05:10 +0900 | [diff] [blame] | 88 | manufacturer_data_ = std::move(manufacturer_data); |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 89 | } |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 90 | void set_solicit_uuids(std::unique_ptr<UUIDList> solicit_uuids) { |
dcheng | f10f6a2 | 2015-12-28 07:05:10 +0900 | [diff] [blame] | 91 | solicit_uuids_ = std::move(solicit_uuids); |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 92 | } |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 93 | void set_service_data(std::unique_ptr<ServiceData> service_data) { |
dcheng | f10f6a2 | 2015-12-28 07:05:10 +0900 | [diff] [blame] | 94 | service_data_ = std::move(service_data); |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void set_include_tx_power(bool include_tx_power) { |
| 98 | include_tx_power_ = include_tx_power; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | Data(); |
| 103 | |
| 104 | AdvertisementType type_; |
thakis | 7a0187b | 2016-04-11 13:37:25 +0900 | [diff] [blame] | 105 | std::unique_ptr<UUIDList> service_uuids_; |
| 106 | std::unique_ptr<ManufacturerData> manufacturer_data_; |
| 107 | std::unique_ptr<UUIDList> solicit_uuids_; |
| 108 | std::unique_ptr<ServiceData> service_data_; |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 109 | bool include_tx_power_; |
| 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(Data); |
| 112 | }; |
| 113 | |
| 114 | // Interface for observing changes to this advertisement. |
| 115 | class Observer { |
| 116 | public: |
| 117 | virtual ~Observer() {} |
| 118 | |
| 119 | // Called when this advertisement is released and is no longer advertising. |
| 120 | virtual void AdvertisementReleased( |
| 121 | BluetoothAdvertisement* advertisement) = 0; |
| 122 | }; |
| 123 | |
| 124 | // Adds and removes observers for events for this advertisement. |
| 125 | void AddObserver(BluetoothAdvertisement::Observer* observer); |
| 126 | void RemoveObserver(BluetoothAdvertisement::Observer* observer); |
| 127 | |
| 128 | // Unregisters this advertisement. Called on destruction of this object |
| 129 | // automatically but can be called directly to explicitly unregister this |
| 130 | // object. |
| 131 | using SuccessCallback = base::Closure; |
| 132 | using ErrorCallback = base::Callback<void(ErrorCode)>; |
| 133 | virtual void Unregister(const SuccessCallback& success_callback, |
| 134 | const ErrorCallback& error_callback) = 0; |
| 135 | |
| 136 | protected: |
| 137 | friend class base::RefCounted<BluetoothAdvertisement>; |
| 138 | |
| 139 | BluetoothAdvertisement(); |
| 140 | |
| 141 | // The destructor will unregister this advertisement. |
| 142 | virtual ~BluetoothAdvertisement(); |
| 143 | |
| 144 | // List of observers interested in event notifications from us. Objects in |
| 145 | // |observers_| are expected to outlive a BluetoothAdvertisement object. |
brettw | 184b350 | 2015-06-04 01:31:43 +0900 | [diff] [blame] | 146 | base::ObserverList<BluetoothAdvertisement::Observer> observers_; |
rkc | 1918539 | 2015-04-29 02:28:52 +0900 | [diff] [blame] | 147 | |
| 148 | private: |
| 149 | DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisement); |
| 150 | }; |
| 151 | |
| 152 | } // namespace device |
| 153 | |
| 154 | #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_ |