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