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

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() {}
armansito 2015/04/24 22:11:00 New line after destructor definition.
rkc 2015/04/24 22:55:25 Done.
97 // Called when this advertisement is released and is no longer advertising.
98 virtual void AdvertisementReleased(
99 BluetoothAdvertisement* advertisement) = 0;
100 };
101
102 // Adds and removes observers for events for this advertisement.
103 void AddObserver(BluetoothAdvertisement::Observer* observer);
104 void RemoveObserver(BluetoothAdvertisement::Observer* observer);
105
106 // Unregisters this advertisement. Called on destruction of this object
107 // automatically but can be called directly to explicitly unregister this
108 // object.
109 typedef base::Closure SuccessCallback;
110 typedef base::Callback<void(ErrorCode)> ErrorCallback;
armansito 2015/04/24 22:11:00 You used "using" above, so be consistent.
rkc 2015/04/24 22:55:25 Done.
111 virtual void Unregister(const SuccessCallback& success_callback,
112 const ErrorCallback& error_callback) = 0;
113
114 protected:
115 friend class base::RefCounted<BluetoothAdvertisement>;
116
117 BluetoothAdvertisement();
armansito 2015/04/24 22:11:00 new line
rkc 2015/04/24 22:55:24 Done.
118 // The destructor will unregister this advertisement.
119 virtual ~BluetoothAdvertisement();
armansito 2015/04/24 22:11:00 Make this public, so that the owner of the object
rkc 2015/04/24 22:55:25 We can't. The class is ref-counted.
armansito 2015/04/24 23:13:13 Ah, didn't realize that.
120
121 // List of observers interested in event notifications from us. Objects in
122 // |observers_| are expected to outlive a BluetoothAdvertisement object.
123 ObserverList<BluetoothAdvertisement::Observer> observers_;
124
125 private:
126 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisement);
127 };
128
129 } // namespace device
130
131 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698