blob: d379f72453a3868b6cef82de2c4676005c905ba5 [file] [log] [blame]
rkc19185392015-04-29 02:28:52 +09001// 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
avi7171ac72015-12-23 04:26:52 +09008#include <stdint.h>
thakis7a0187b2016-04-11 13:37:25 +09009
rkc19185392015-04-29 02:28:52 +090010#include <map>
thakis7a0187b2016-04-11 13:37:25 +090011#include <memory>
rkc19185392015-04-29 02:28:52 +090012#include <string>
dchengf10f6a22015-12-28 07:05:10 +090013#include <utility>
rkc19185392015-04-29 02:28:52 +090014#include <vector>
15
16#include "base/callback.h"
avi7171ac72015-12-23 04:26:52 +090017#include "base/macros.h"
rkc19185392015-04-29 02:28:52 +090018#include "base/memory/ref_counted.h"
rkc19185392015-04-29 02:28:52 +090019#include "base/observer_list.h"
Michael Giuffrida405bd062017-08-02 22:22:44 +090020#include "build/build_config.h"
rkc19185392015-04-29 02:28:52 +090021#include "device/bluetooth/bluetooth_export.h"
22
23namespace device {
24
25// BluetoothAdvertisement represents an advertisement which advertises over the
26// LE channel during its lifetime.
27class 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 Giuffrida405bd062017-08-02 22:22:44 +090041#if defined(OS_LINUX)
steel977f51f2016-09-22 12:40:55 +090042 ERROR_INVALID_ADVERTISEMENT_INTERVAL, // Advertisement interval specified
43 // is out of valid range.
sonnysasaka00489b72017-06-29 04:12:35 +090044 ERROR_RESET_ADVERTISING, // Error while resetting advertising.
steel977f51f2016-09-22 12:40:55 +090045#endif
rkc19185392015-04-29 02:28:52 +090046 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:
primiano854fd712015-05-28 02:37:00 +090066 explicit Data(AdvertisementType type);
rkc19185392015-04-29 02:28:52 +090067 ~Data();
68
69 AdvertisementType type() { return type_; }
thakis7a0187b2016-04-11 13:37:25 +090070 std::unique_ptr<UUIDList> service_uuids() {
71 return std::move(service_uuids_);
72 }
73 std::unique_ptr<ManufacturerData> manufacturer_data() {
dchengf10f6a22015-12-28 07:05:10 +090074 return std::move(manufacturer_data_);
rkc19185392015-04-29 02:28:52 +090075 }
thakis7a0187b2016-04-11 13:37:25 +090076 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 }
rkc19185392015-04-29 02:28:52 +090082
thakis7a0187b2016-04-11 13:37:25 +090083 void set_service_uuids(std::unique_ptr<UUIDList> service_uuids) {
dchengf10f6a22015-12-28 07:05:10 +090084 service_uuids_ = std::move(service_uuids);
rkc19185392015-04-29 02:28:52 +090085 }
thakis7a0187b2016-04-11 13:37:25 +090086 void set_manufacturer_data(
87 std::unique_ptr<ManufacturerData> manufacturer_data) {
dchengf10f6a22015-12-28 07:05:10 +090088 manufacturer_data_ = std::move(manufacturer_data);
rkc19185392015-04-29 02:28:52 +090089 }
thakis7a0187b2016-04-11 13:37:25 +090090 void set_solicit_uuids(std::unique_ptr<UUIDList> solicit_uuids) {
dchengf10f6a22015-12-28 07:05:10 +090091 solicit_uuids_ = std::move(solicit_uuids);
rkc19185392015-04-29 02:28:52 +090092 }
thakis7a0187b2016-04-11 13:37:25 +090093 void set_service_data(std::unique_ptr<ServiceData> service_data) {
dchengf10f6a22015-12-28 07:05:10 +090094 service_data_ = std::move(service_data);
rkc19185392015-04-29 02:28:52 +090095 }
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_;
thakis7a0187b2016-04-11 13:37:25 +0900105 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_;
rkc19185392015-04-29 02:28:52 +0900109 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.
brettw184b3502015-06-04 01:31:43 +0900146 base::ObserverList<BluetoothAdvertisement::Observer> observers_;
rkc19185392015-04-29 02:28:52 +0900147
148 private:
149 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisement);
150};
151
152} // namespace device
153
154#endif // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_