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