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.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
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"
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
> {
25 // Possible types of error raised while registering or unregistering
28 ERROR_UNSUPPORTED_PLATFORM
, // Bluetooth advertisement not supported on
30 ERROR_ADVERTISEMENT_ALREADY_EXISTS
, // An advertisement is already
32 ERROR_ADVERTISEMENT_DOES_NOT_EXIST
, // Unregistering an advertisement which
34 ERROR_ADVERTISEMENT_INVALID_LENGTH
, // Advertisement is not of a valid
36 INVALID_ADVERTISEMENT_ERROR_CODE
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
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>>;
53 // Structure that holds the data for an advertisement.
54 class DEVICE_BLUETOOTH_EXPORT Data
{
56 explicit Data(AdvertisementType type
);
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();
64 scoped_ptr
<UUIDList
> solicit_uuids() { return solicit_uuids_
.Pass(); }
65 scoped_ptr
<ServiceData
> service_data() { return service_data_
.Pass(); }
67 void set_service_uuids(scoped_ptr
<UUIDList
> service_uuids
) {
68 service_uuids_
= service_uuids
.Pass();
70 void set_manufacturer_data(scoped_ptr
<ManufacturerData
> manufacturer_data
) {
71 manufacturer_data_
= manufacturer_data
.Pass();
73 void set_solicit_uuids(scoped_ptr
<UUIDList
> solicit_uuids
) {
74 solicit_uuids
= solicit_uuids_
.Pass();
76 void set_service_data(scoped_ptr
<ServiceData
> service_data
) {
77 service_data
= service_data_
.Pass();
80 void set_include_tx_power(bool include_tx_power
) {
81 include_tx_power_
= include_tx_power
;
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_
;
94 DISALLOW_COPY_AND_ASSIGN(Data
);
97 // Interface for observing changes to this advertisement.
100 virtual ~Observer() {}
102 // Called when this advertisement is released and is no longer advertising.
103 virtual void AdvertisementReleased(
104 BluetoothAdvertisement
* advertisement
) = 0;
107 // Adds and removes observers for events for this advertisement.
108 void AddObserver(BluetoothAdvertisement::Observer
* observer
);
109 void RemoveObserver(BluetoothAdvertisement::Observer
* observer
);
111 // Unregisters this advertisement. Called on destruction of this object
112 // automatically but can be called directly to explicitly unregister this
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;
120 friend class base::RefCounted
<BluetoothAdvertisement
>;
122 BluetoothAdvertisement();
124 // The destructor will unregister this advertisement.
125 virtual ~BluetoothAdvertisement();
127 // List of observers interested in event notifications from us. Objects in
128 // |observers_| are expected to outlive a BluetoothAdvertisement object.
129 base::ObserverList
<BluetoothAdvertisement::Observer
> observers_
;
132 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisement
);
135 } // namespace device
137 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_