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