Roll src/third_party/WebKit 787a07c:716df21 (svn 201034:201036)
[chromium-blink-merge.git] / device / bluetooth / bluetooth_gatt_descriptor.h
blobc91de1cc3775f4e7134316e818ad0d70eb231829
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_DESCRIPTOR_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_DESCRIPTOR_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "device/bluetooth/bluetooth_export.h"
13 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
14 #include "device/bluetooth/bluetooth_uuid.h"
16 namespace device {
18 // BluetoothGattDescriptor represents a local or remote GATT characteristic
19 // descriptor. A GATT characteristic descriptor provides further information
20 // about a characteristic's value. They can be used to describe the
21 // characteristic's features or to control certain behaviors.
22 class DEVICE_BLUETOOTH_EXPORT BluetoothGattDescriptor {
23 public:
24 // The ErrorCallback is used by methods to asynchronously report errors.
25 typedef base::Callback<void(BluetoothGattService::GattErrorCode)>
26 ErrorCallback;
28 // The ValueCallback is used to return the value of a remote characteristic
29 // descriptor upon a read request.
30 typedef base::Callback<void(const std::vector<uint8>&)> ValueCallback;
32 // The Bluetooth Specification declares several predefined descriptors that
33 // profiles can use. The following are definitions for the list of UUIDs
34 // and descriptions of the characteristic descriptors that they represent.
35 // Possible values for and further information on each descriptor can be found
36 // in Core v4.0, Volume 3, Part G, Section 3.3.3. All of these descriptors are
37 // optional and may not be present for a given characteristic.
39 // The "Characteristic Extended Properties" descriptor. This defines
40 // additional "Characteristic Properties" which cannot fit into the allocated
41 // single octet property field of a characteristic. The value is a bit field
42 // and the two predefined bits, as per Bluetooth Core Specification v4.0, are:
44 // - Reliable Write: 0x0001
45 // - Writable Auxiliaries: 0x0002
47 static const BluetoothUUID& CharacteristicExtendedPropertiesUuid();
49 // The "Characteristic User Description" descriptor defines a UTF-8 string of
50 // variable size that is a user textual description of the associated
51 // characteristic's value. There can be only one instance of this descriptor
52 // per characteristic. This descriptor can be written to if the "Writable
53 // Auxiliaries" bit of the Characteristic Properties (via the "Characteristic
54 // Extended Properties" descriptor) has been set.
55 static const BluetoothUUID& CharacteristicUserDescriptionUuid();
57 // The "Client Characteristic Configuration" descriptor defines how the
58 // characteristic may be configured by a specific client. A server-side
59 // instance of this descriptor exists for each client that has bonded with
60 // the server and the value can be read and written by that client only. As
61 // of Core v4.0, this descriptor is used by clients to set up notifications
62 // and indications from a characteristic. The value is a bit field and the
63 // predefined bits are:
65 // - Default: 0x0000
66 // - Notification: 0x0001
67 // - Indication: 0x0002
69 static const BluetoothUUID& ClientCharacteristicConfigurationUuid();
71 // The "Server Characteristic Configuration" descriptor defines how the
72 // characteristic may be configured for the server. There is one instance
73 // of this descriptor for all clients and setting the value of this descriptor
74 // affects its configuration for all clients. As of Core v4.0, this descriptor
75 // is used to set up the server to broadcast the characteristic value if
76 // advertising resources are available. The value is a bit field and the
77 // predefined bits are:
79 // - Default: 0x0000
80 // - Broadcast: 0x0001
82 static const BluetoothUUID& ServerCharacteristicConfigurationUuid();
84 // The "Characteristic Presentation Format" declaration defines the format of
85 // the Characteristic Value. The value is composed of 7 octets which are
86 // divided into groups that represent different semantic meanings. For a
87 // detailed description of how the value of this descriptor should be
88 // interpreted, refer to Core v4.0, Volume 3, Part G, Section 3.3.3.5. If more
89 // than one declaration of this descriptor exists for a characteristic, then a
90 // "Characteristic Aggregate Format" descriptor must also exist for that
91 // characteristic.
92 static const BluetoothUUID& CharacteristicPresentationFormatUuid();
94 // The "Characteristic Aggregate Format" descriptor defines the format of an
95 // aggregated characteristic value. In GATT's underlying protocol, ATT, each
96 // attribute is identified by a handle that is unique for the hosting server.
97 // Multiple characteristics can share the same instance(s) of a
98 // "Characteristic Presentation Format" descriptor. The value of the
99 // "Characteristic Aggregate Format" descriptor contains a list of handles
100 // that each refer to a "Characteristic Presentation Format" descriptor that
101 // is used by that characteristic. Hence, exactly one instance of this
102 // descriptor must exist if more than one "Characteristic Presentation Format"
103 // descriptors exist for a characteristic.
105 // Applications that are using the device::Bluetooth API do not have access to
106 // the underlying handles and shouldn't use this descriptor to determine which
107 // "Characteristic Presentation Format" descriptors belong to a
108 // characteristic.
109 // The API will construct a BluetoothGattDescriptor object for each instance
110 // of "Characteristic Presentation Format" descriptor per instance of
111 // BluetoothGattCharacteristic that represents a remote characteristic.
112 // Similarly for local characteristics, implementations DO NOT need to create
113 // an instance of BluetoothGattDescriptor for this descriptor as this will be
114 // handled by the subsystem.
115 static const BluetoothUUID& CharacteristicAggregateFormatUuid();
117 // Constructs a BluetoothGattDescriptor that can be associated with a local
118 // GATT characteristic when the adapter is in the peripheral role. To
119 // associate the returned descriptor with a characteristic, add it to a local
120 // characteristic by calling BluetoothGattCharacteristic::AddDescriptor.
122 // This method constructs a characteristic descriptor with UUID |uuid| and the
123 // initial cached value |value|. |value| will be cached and returned for read
124 // requests and automatically modified for write requests by default, unless
125 // an instance of BluetoothGattService::Delegate has been provided to the
126 // associated BluetoothGattService instance, in which case the delegate will
127 // handle the read and write requests.
129 // Currently, only custom UUIDs, |kCharacteristicDescriptionUuid|, and
130 // |kCharacteristicPresentationFormat| are supported for locally hosted
131 // descriptors. This method will return NULL if |uuid| is any one of the
132 // unsupported predefined descriptor UUIDs.
133 static BluetoothGattDescriptor* Create(
134 const BluetoothUUID& uuid,
135 const std::vector<uint8>& value,
136 BluetoothGattCharacteristic::Permissions permissions);
138 // Identifier used to uniquely identify a GATT descriptor object. This is
139 // different from the descriptor UUID: while multiple descriptors with the
140 // same UUID can exist on a Bluetooth device, the identifier returned from
141 // this method is unique among all descriptors of a device. The contents of
142 // the identifier are platform specific.
143 virtual std::string GetIdentifier() const = 0;
145 // The Bluetooth-specific UUID of the characteristic descriptor.
146 virtual BluetoothUUID GetUUID() const = 0;
148 // Returns true, if this characteristic descriptor is hosted locally. If
149 // false, then this instance represents a remote descriptor.
150 virtual bool IsLocal() const = 0;
152 // Returns the value of the descriptor. For remote descriptors, this is the
153 // most recently cached value of the remote descriptor. For local descriptors
154 // this is the most recently updated value or the value retrieved from the
155 // delegate.
156 virtual const std::vector<uint8>& GetValue() const = 0;
158 // Returns a pointer to the GATT characteristic that this characteristic
159 // descriptor belongs to.
160 virtual BluetoothGattCharacteristic* GetCharacteristic() const = 0;
162 // Returns the bitmask of characteristic descriptor attribute permissions.
163 virtual BluetoothGattCharacteristic::Permissions GetPermissions() const = 0;
165 // Sends a read request to a remote characteristic descriptor to read its
166 // value. |callback| is called to return the read value on success and
167 // |error_callback| is called for failures.
168 virtual void ReadRemoteDescriptor(const ValueCallback& callback,
169 const ErrorCallback& error_callback) = 0;
171 // Sends a write request to a remote characteristic descriptor, to modify the
172 // value of the descriptor with the new value |new_value|. |callback| is
173 // called to signal success and |error_callback| for failures. This method
174 // only applies to remote descriptors and will fail for those that are locally
175 // hosted.
176 virtual void WriteRemoteDescriptor(
177 const std::vector<uint8>& new_value,
178 const base::Closure& callback,
179 const ErrorCallback& error_callback) = 0;
181 protected:
182 BluetoothGattDescriptor();
183 virtual ~BluetoothGattDescriptor();
185 private:
186 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptor);
189 } // namespace device
191 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_DESCRIPTOR_H_