Roll WebRTC 8837:8852, Libjingle 8836:8851
[chromium-blink-merge.git] / device / bluetooth / bluetooth_gatt_characteristic.h
blobf3a36e8928e58226c89036c7711a483985833c28
1 // Copyright 2014 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_GATT_CHARACTERISTIC_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "device/bluetooth/bluetooth_export.h"
15 #include "device/bluetooth/bluetooth_gatt_service.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
18 namespace device {
20 class BluetoothGattDescriptor;
21 class BluetoothGattNotifySession;
23 // BluetoothGattCharacteristic represents a local or remote GATT characteristic.
24 // A GATT characteristic is a basic data element used to construct a GATT
25 // service. Hence, instances of a BluetoothGattCharacteristic are associated
26 // with a BluetoothGattService. There are two ways in which this class is used:
28 // 1. To represent GATT characteristics that belong to a service hosted by a
29 // remote device. In this case the characteristic will be constructed by
30 // the subsystem.
31 // 2. To represent GATT characteristics that belong to a locally hosted
32 // service. To achieve this, users can construct instances of
33 // BluetoothGattCharacteristic directly and add it to the desired
34 // BluetoothGattService instance that represents a local service.
35 class DEVICE_BLUETOOTH_EXPORT BluetoothGattCharacteristic {
36 public:
37 // Values representing the possible properties of a characteristic, which
38 // define how the characteristic can be used. Each of these properties serve
39 // a role as defined in the Bluetooth Specification.
40 // |PROPERTY_EXTENDED_PROPERTIES| is a special property that, if present,
41 // indicates that there is a characteristic descriptor (namely the
42 // "Characteristic Extended Properties Descriptor" with UUID 0x2900) that
43 // contains additional properties pertaining to the characteristic.
44 // The properties "ReliableWrite| and |WriteAuxiliaries| are retrieved from
45 // that characteristic.
46 enum Property {
47 PROPERTY_NONE = 0,
48 PROPERTY_BROADCAST = 1 << 0,
49 PROPERTY_READ = 1 << 1,
50 PROPERTY_WRITE_WITHOUT_RESPONSE = 1 << 2,
51 PROPERTY_WRITE = 1 << 3,
52 PROPERTY_NOTIFY = 1 << 4,
53 PROPERTY_INDICATE = 1 << 5,
54 PROPERTY_AUTHENTICATED_SIGNED_WRITES = 1 << 6,
55 PROPERTY_EXTENDED_PROPERTIES = 1 << 7,
56 PROPERTY_RELIABLE_WRITE = 1 << 8,
57 PROPERTY_WRITABLE_AUXILIARIES = 1 << 9
59 typedef uint32 Properties;
61 // Values representing read, write, and encryption permissions for a
62 // characteristic's value. While attribute permissions for all GATT
63 // definitions have been set by the Bluetooth specification, characteristic
64 // value permissions are left up to the higher-level profile.
66 // Attribute permissions are distinct from the characteristic properties. For
67 // example, a characteristic may have the property |PROPERTY_READ| to make
68 // clients know that it is possible to read the characteristic value and have
69 // the permission |PERMISSION_READ_ENCRYPTED| to require a secure connection.
70 // It is up to the application to properly specify the permissions and
71 // properties for a local characteristic.
72 enum Permission {
73 PERMISSION_NONE = 0,
74 PERMISSION_READ = 1 << 0,
75 PERMISSION_WRITE = 1 << 1,
76 PERMISSION_READ_ENCRYPTED = 1 << 2,
77 PERMISSION_WRITE_ENCRYPTED = 1 << 3
79 typedef uint32 Permissions;
81 // The ErrorCallback is used by methods to asynchronously report errors.
82 typedef base::Callback<void(BluetoothGattService::GattErrorCode)>
83 ErrorCallback;
85 // The ValueCallback is used to return the value of a remote characteristic
86 // upon a read request.
87 typedef base::Callback<void(const std::vector<uint8>&)> ValueCallback;
89 // The NotifySessionCallback is used to return sessions after they have
90 // been successfully started.
91 typedef base::Callback<void(scoped_ptr<BluetoothGattNotifySession>)>
92 NotifySessionCallback;
94 // Constructs a BluetoothGattCharacteristic that can be associated with a
95 // local GATT service when the adapter is in the peripheral role. To
96 // associate the returned characteristic with a service, add it to a local
97 // service by calling BluetoothGattService::AddCharacteristic.
99 // This method constructs a characteristic with UUID |uuid|, initial cached
100 // value |value|, properties |properties|, and permissions |permissions|.
101 // |value| will be cached and returned for read requests and automatically set
102 // for write requests by default, unless an instance of
103 // BluetoothGattService::Delegate has been provided to the associated
104 // BluetoothGattService instance, in which case the delegate will handle read
105 // and write requests.
107 // NOTE: Don't explicitly set |PROPERTY_EXTENDED_PROPERTIES| in |properties|.
108 // Instead, create and add a BluetoothGattDescriptor that represents the
109 // "Characteristic Extended Properties" descriptor and this will automatically
110 // set the correspoding bit in the characteristic's properties field. If
111 // |properties| has |PROPERTY_EXTENDED_PROPERTIES| set, it will be ignored.
112 static BluetoothGattCharacteristic* Create(const BluetoothUUID& uuid,
113 const std::vector<uint8>& value,
114 Properties properties,
115 Permissions permissions);
117 // Identifier used to uniquely identify a GATT characteristic object. This is
118 // different from the characteristic UUID: while multiple characteristics with
119 // the same UUID can exist on a Bluetooth device, the identifier returned from
120 // this method is unique among all characteristics of a device. The contents
121 // of the identifier are platform specific.
122 virtual std::string GetIdentifier() const = 0;
124 // The Bluetooth-specific UUID of the characteristic.
125 virtual BluetoothUUID GetUUID() const = 0;
127 // Returns true, if this characteristic is hosted locally. If false, then this
128 // instance represents a remote GATT characteristic.
129 virtual bool IsLocal() const = 0;
131 // Returns the value of the characteristic. For remote characteristics, this
132 // is the most recently cached value. For local characteristics, this is the
133 // most recently updated value or the value retrieved from the delegate.
134 virtual const std::vector<uint8>& GetValue() const = 0;
136 // Returns a pointer to the GATT service this characteristic belongs to.
137 virtual BluetoothGattService* GetService() const = 0;
139 // Returns the bitmask of characteristic properties.
140 virtual Properties GetProperties() const = 0;
142 // Returns the bitmask of characteristic attribute permissions.
143 virtual Permissions GetPermissions() const = 0;
145 // Returns whether or not this characteristic is currently sending value
146 // updates in the form of a notification or indication.
147 virtual bool IsNotifying() const = 0;
149 // Returns the list of GATT characteristic descriptors that provide more
150 // information about this characteristic.
151 virtual std::vector<BluetoothGattDescriptor*>
152 GetDescriptors() const = 0;
154 // Returns the GATT characteristic descriptor with identifier |identifier| if
155 // it belongs to this GATT characteristic.
156 virtual BluetoothGattDescriptor* GetDescriptor(
157 const std::string& identifier) const = 0;
159 // Adds a characteristic descriptor to the locally hosted characteristic
160 // represented by this instance. This method only makes sense for local
161 // characteristics and won't have an effect if this instance represents a
162 // remote GATT service and will return false. This method takes ownership
163 // of |descriptor|.
164 virtual bool AddDescriptor(BluetoothGattDescriptor* descriptor) = 0;
166 // For locally hosted characteristics, updates the characteristic's value.
167 // This will update the value that is visible to remote devices and send out
168 // any notifications and indications that have been configured. This method
169 // can be used in place of, and in conjunction with,
170 // BluetoothGattService::Delegate methods to send updates to remote devices,
171 // or simply to set update the cached value for read requests without having
172 // to implement the delegate methods.
174 // This method only makes sense for local characteristics and does nothing and
175 // returns false if this instance represents a remote characteristic.
176 virtual bool UpdateValue(const std::vector<uint8>& value) = 0;
178 // Starts a notify session for the remote characteristic, if it supports
179 // notifications/indications. On success, the characteristic starts sending
180 // value notifications and |callback| is called with a session object whose
181 // ownership belongs to the caller. |error_callback| is called on errors.
182 virtual void StartNotifySession(const NotifySessionCallback& callback,
183 const ErrorCallback& error_callback) = 0;
185 // Sends a read request to a remote characteristic to read its value.
186 // |callback| is called to return the read value on success and
187 // |error_callback| is called for failures.
188 virtual void ReadRemoteCharacteristic(
189 const ValueCallback& callback,
190 const ErrorCallback& error_callback) = 0;
192 // Sends a write request to a remote characteristic, to modify the
193 // characteristic's value with the new value |new_value|. |callback| is
194 // called to signal success and |error_callback| for failures. This method
195 // only applies to remote characteristics and will fail for those that are
196 // locally hosted.
197 virtual void WriteRemoteCharacteristic(
198 const std::vector<uint8>& new_value,
199 const base::Closure& callback,
200 const ErrorCallback& error_callback) = 0;
202 protected:
203 BluetoothGattCharacteristic();
204 virtual ~BluetoothGattCharacteristic();
206 private:
207 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristic);
210 } // namespace device
212 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_