Home | History | Annotate | Download | only in bluetooth
      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 "build/build_config.h"
     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.
     41 #if defined(OS_LINUX)
     42     ERROR_INVALID_ADVERTISEMENT_INTERVAL,  // Advertisement interval specified
     43                                            // is out of valid range.
     44     ERROR_RESET_ADVERTISING,               // Error while resetting advertising.
     45 #endif
     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:
     66     explicit Data(AdvertisementType type);
     67     ~Data();
     68 
     69     AdvertisementType type() { return type_; }
     70     std::unique_ptr<UUIDList> service_uuids() {
     71       return std::move(service_uuids_);
     72     }
     73     std::unique_ptr<ManufacturerData> manufacturer_data() {
     74       return std::move(manufacturer_data_);
     75     }
     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     }
     82 
     83     void set_service_uuids(std::unique_ptr<UUIDList> service_uuids) {
     84       service_uuids_ = std::move(service_uuids);
     85     }
     86     void set_manufacturer_data(
     87         std::unique_ptr<ManufacturerData> manufacturer_data) {
     88       manufacturer_data_ = std::move(manufacturer_data);
     89     }
     90     void set_solicit_uuids(std::unique_ptr<UUIDList> solicit_uuids) {
     91       solicit_uuids_ = std::move(solicit_uuids);
     92     }
     93     void set_service_data(std::unique_ptr<ServiceData> service_data) {
     94       service_data_ = std::move(service_data);
     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_;
    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_;
    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.
    146   base::ObserverList<BluetoothAdvertisement::Observer> observers_;
    147 
    148  private:
    149   DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisement);
    150 };
    151 
    152 }  // namespace device
    153 
    154 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
    155