Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: device/bluetooth/bluetooth_advertisement.h

Issue 1054743003: Add CPP API for BLE advertisments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_ALREADY_EXISTS, // An advertisement is already registered.
29 ERROR_DOES_NOT_EXIST, // Unregistering an advertisement which is not
30 // registered.
31 ERROR_INVALID_LENGTH, // Advertisement is not of a valid length.
32 INVALID_ERROR_CODE
33 };
34
35 // Type of advertisement.
36 enum AdvertisementType {
37 // This advertises with the type set to ADV_NONCONN_IND, which indicates
38 // to receivers that our device is not connectable.
39 ADVERTISEMENT_TYPE_BROADCAST,
40 // This advertises with the type set to ADV_IND or ADV_SCAN_IND, which
41 // indicates to receivers that our device is connectable.
42 ADVERTISEMENT_TYPE_PERIPHERAL
43 };
44
45 using UUIDList = std::vector<std::string>;
46 using ManufacturerData = std::map<uint16_t, std::vector<uint8_t>>;
47 using ServiceData = std::map<std::string, std::vector<uint8_t>>;
48
49 // Structure that holds the data for an advertisement.
50 class Data {
51 public:
52 Data(AdvertisementType type);
53 ~Data();
54
55 AdvertisementType type() { return type_; }
56 scoped_ptr<UUIDList> service_uuids() { return service_uuids_.Pass(); }
57 scoped_ptr<ManufacturerData> manufacturer_data() {
58 return manufacturer_data_.Pass();
59 }
60 scoped_ptr<UUIDList> solicit_uuids() { return solicit_uuids_.Pass(); }
61 scoped_ptr<ServiceData> service_data() { return service_data_.Pass(); }
62
63 void set_service_uuids(scoped_ptr<UUIDList> service_uuids) {
64 service_uuids_ = service_uuids.Pass();
65 }
66 void set_manufacturer_data(scoped_ptr<ManufacturerData> manufacturer_data) {
67 manufacturer_data_ = manufacturer_data.Pass();
68 }
69 void set_solicit_uuids(scoped_ptr<UUIDList> solicit_uuids) {
70 solicit_uuids = solicit_uuids_.Pass();
71 }
72 void set_service_data(scoped_ptr<ServiceData> service_data) {
73 service_data = service_data_.Pass();
74 }
75
76 void set_include_tx_power(bool include_tx_power) {
77 include_tx_power_ = include_tx_power;
78 }
79
80 private:
81 Data();
82
83 AdvertisementType type_;
84 scoped_ptr<UUIDList> service_uuids_;
85 scoped_ptr<ManufacturerData> manufacturer_data_;
86 scoped_ptr<UUIDList> solicit_uuids_;
87 scoped_ptr<ServiceData> service_data_;
88 bool include_tx_power_;
89
90 DISALLOW_COPY_AND_ASSIGN(Data);
91 };
92
93 // Interface for observing changes to this advertisement.
94 class Observer {
95 public:
96 virtual ~Observer() {}
97
98 // Called when this advertisement is released and is no longer advertising.
99 virtual void AdvertisementReleased(
100 BluetoothAdvertisement* advertisement) = 0;
101 };
102
103 // Adds and removes observers for events for this advertisement.
104 void AddObserver(BluetoothAdvertisement::Observer* observer);
105 void RemoveObserver(BluetoothAdvertisement::Observer* observer);
106
107 // Unregisters this advertisement. Called on destruction of this object
108 // automatically but can be called directly to explicitly unregister this
109 // object.
110 using SuccessCallback = base::Closure;
111 using ErrorCallback = base::Callback<void(ErrorCode)>;
112 virtual void Unregister(const SuccessCallback& success_callback,
113 const ErrorCallback& error_callback) = 0;
114
115 protected:
116 friend class base::RefCounted<BluetoothAdvertisement>;
117
118 BluetoothAdvertisement();
119
120 // The destructor will unregister this advertisement.
121 virtual ~BluetoothAdvertisement();
122
123 // List of observers interested in event notifications from us. Objects in
124 // |observers_| are expected to outlive a BluetoothAdvertisement object.
125 ObserverList<BluetoothAdvertisement::Observer> observers_;
126
127 private:
128 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisement);
129 };
130
131 } // namespace device
132
133 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698