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