Fix "#if defined(DEBUG)" statements
[chromium-blink-merge.git] / device / bluetooth / bluetooth_gatt_characteristic.h
blob522c3d018a713f1a4930631a04d787718883646f
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 // a 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 // TODO(jamuraa): per chromium.org/developers/coding-style these should
37 // be MACRO_STYLE instead of kCamelCase. (crbug.com/418696)
39 // Values representing the possible properties of a characteristic, which
40 // define how the characteristic can be used. Each of these properties serve
41 // a role as defined in the Bluetooth Specification.
42 // |PROPERTY_EXTENDED_PROPERTIES| is a special property that, if present,
43 // indicates that there is a characteristic descriptor (namely the
44 // "Characteristic Extended Properties Descriptor" with UUID 0x2900) that
45 // contains additional properties pertaining to the characteristic.
46 // The properties "ReliableWrite| and |WriteAuxiliaries| are retrieved from
47 // that characteristic.
48 enum Property {
49 PROPERTY_NONE = 0,
50 PROPERTY_BROADCAST = 1 << 0,
51 PROPERTY_READ = 1 << 1,
52 PROPERTY_WRITE_WITHOUT_RESPONSE = 1 << 2,
53 PROPERTY_WRITE = 1 << 3,
54 PROPERTY_NOTIFY = 1 << 4,
55 PROPERTY_INDICATE = 1 << 5,
56 PROPERTY_AUTHENTICATED_SIGNED_WRITES = 1 << 6,
57 PROPERTY_EXTENDED_PROPERTIES = 1 << 7,
58 PROPERTY_RELIABLE_WRITE = 1 << 8,
59 PROPERTY_WRITABLE_AUXILIARIES = 1 << 9
61 typedef uint32 Properties;
63 // Values representing read, write, and encryption permissions for a
64 // characteristic's value. While attribute permissions for all GATT
65 // definitions have been set by the Bluetooth specification, characteristic
66 // value permissions are left up to the higher-level profile.
68 // Attribute permissions are distinct from the characteristic properties. For
69 // example, a characteristic may have the property |PROPERTY_READ| to make
70 // clients know that it is possible to read the characteristic value and have
71 // the permission |PERMISSION_READ_ENCRYPTED| to require a secure connection.
72 // It is up to the application to properly specify the permissions and
73 // properties for a local characteristic.
74 enum Permission {
75 PERMISSION_NONE = 0,
76 PERMISSION_READ = 1 << 0,
77 PERMISSION_WRITE = 1 << 1,
78 PERMISSION_READ_ENCRYPTED = 1 << 2,
79 PERMISSION_WRITE_ENCRYPTED = 1 << 3
81 typedef uint32 Permissions;
83 // The ErrorCallback is used by methods to asynchronously report errors.
84 typedef base::Callback<void(BluetoothGattService::GattErrorCode)>
85 ErrorCallback;
87 // The ValueCallback is used to return the value of a remote characteristic
88 // upon a read request.
89 typedef base::Callback<void(const std::vector<uint8>&)> ValueCallback;
91 // The NotifySessionCallback is used to return sessions after they have
92 // been successfully started.
93 typedef base::Callback<void(scoped_ptr<BluetoothGattNotifySession>)>
94 NotifySessionCallback;
96 // Constructs a BluetoothGattCharacteristic that can be associated with a
97 // local GATT service when the adapter is in the peripheral role. To
98 // associate the returned characteristic with a service, add it to a local
99 // service by calling BluetoothGattService::AddCharacteristic.
101 // This method constructs a characteristic with UUID |uuid|, initial cached
102 // value |value|, properties |properties|, and permissions |permissions|.
103 // |value| will be cached and returned for read requests and automatically set
104 // for write requests by default, unless an instance of
105 // BluetoothGattService::Delegate has been provided to the associated
106 // BluetoothGattService instance, in which case the delegate will handle read
107 // and write requests.
109 // NOTE: Don't explicitly set |PROPERTY_EXTENDED_PROPERTIES| in |properties|.
110 // Instead, create and add a BluetoothGattDescriptor that represents the
111 // "Characteristic Extended Properties" descriptor and this will automatically
112 // set the correspoding bit in the characteristic's properties field. If
113 // |properties| has |PROPERTY_EXTENDED_PROPERTIES| set, it will be ignored.
114 static BluetoothGattCharacteristic* Create(const BluetoothUUID& uuid,
115 const std::vector<uint8>& value,
116 Properties properties,
117 Permissions permissions);
119 // Identifier used to uniquely identify a GATT characteristic object. This is
120 // different from the characteristic UUID: while multiple characteristics with
121 // the same UUID can exist on a Bluetooth device, the identifier returned from
122 // this method is unique among all characteristics of a device. The contents
123 // of the identifier are platform specific.
124 virtual std::string GetIdentifier() const = 0;
126 // The Bluetooth-specific UUID of the characteristic.
127 virtual BluetoothUUID GetUUID() const = 0;
129 // Returns true, if this characteristic is hosted locally. If false, then this
130 // instance represents a remote GATT characteristic.
131 virtual bool IsLocal() const = 0;
133 // Returns the value of the characteristic. For remote characteristics, this
134 // is the most recently cached value. For local characteristics, this is the
135 // most recently updated value or the value retrieved from the delegate.
136 virtual const std::vector<uint8>& GetValue() const = 0;
138 // Returns a pointer to the GATT service this characteristic belongs to.
139 virtual BluetoothGattService* GetService() const = 0;
141 // Returns the bitmask of characteristic properties.
142 virtual Properties GetProperties() const = 0;
144 // Returns the bitmask of characteristic attribute permissions.
145 virtual Permissions GetPermissions() const = 0;
147 // Returns whether or not this characteristic is currently sending value
148 // updates in the form of a notification or indication.
149 virtual bool IsNotifying() const = 0;
151 // Returns the list of GATT characteristic descriptors that provide more
152 // information about this characteristic.
153 virtual std::vector<BluetoothGattDescriptor*>
154 GetDescriptors() const = 0;
156 // Returns the GATT characteristic descriptor with identifier |identifier| if
157 // it belongs to this GATT characteristic.
158 virtual BluetoothGattDescriptor* GetDescriptor(
159 const std::string& identifier) const = 0;
161 // Adds a characteristic descriptor to the locally hosted characteristic
162 // represented by this instance. This method only makes sense for local
163 // characteristics and won't have an effect if this instance represents a
164 // remote GATT service and will return false. This method takes ownership
165 // of |descriptor|.
166 virtual bool AddDescriptor(BluetoothGattDescriptor* descriptor) = 0;
168 // For locally hosted characteristics, updates the characteristic's value.
169 // This will update the value that is visible to remote devices and send out
170 // any notifications and indications that have been configured. This method
171 // can be used in place of, and in conjunction with,
172 // BluetoothGattService::Delegate methods to send updates to remote devices,
173 // or simply to set update the cached value for read requests without having
174 // to implement the delegate methods.
176 // This method only makes sense for local characteristics and does nothing and
177 // returns false if this instance represents a remote characteristic.
178 virtual bool UpdateValue(const std::vector<uint8>& value) = 0;
180 // Starts a notify session for the remote characteristic, if it supports
181 // notifications/indications. On success, the characteristic starts sending
182 // value notifications and |callback| is called with a session object whose
183 // ownership belongs to the caller. |error_callback| is called on errors.
184 virtual void StartNotifySession(const NotifySessionCallback& callback,
185 const ErrorCallback& error_callback) = 0;
187 // Sends a read request to a remote characteristic to read its value.
188 // |callback| is called to return the read value on success and
189 // |error_callback| is called for failures.
190 virtual void ReadRemoteCharacteristic(
191 const ValueCallback& callback,
192 const ErrorCallback& error_callback) = 0;
194 // Sends a write request to a remote characteristic, to modify the
195 // characteristic's value with the new value |new_value|. |callback| is
196 // called to signal success and |error_callback| for failures. This method
197 // only applies to remote characteristics and will fail for those that are
198 // locally hosted.
199 virtual void WriteRemoteCharacteristic(
200 const std::vector<uint8>& new_value,
201 const base::Closure& callback,
202 const ErrorCallback& error_callback) = 0;
204 protected:
205 BluetoothGattCharacteristic();
206 virtual ~BluetoothGattCharacteristic();
208 private:
209 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristic);
212 } // namespace device
214 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_