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_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "device/bluetooth/bluetooth_uuid.h"
17 class BluetoothGattDescriptor
;
18 class BluetoothGattService
;
20 // BluetoothGattCharacteristic represents a local or remote GATT characteristic.
21 // A GATT characteristic is a basic data element used to construct a GATT
22 // service. Hence, instances of a BluetoothGattCharacteristic are associated
23 // with a BluetoothGattService. There are two ways in which this class is used:
25 // 1. To represent GATT characteristics that belong to a service hosted by a
26 // a remote device. In this case the characteristic will be constructed by
28 // 2. To represent GATT characteristics that belong to a locally hosted
29 // service. To achieve this, users can construct instances of
30 // BluetoothGattCharacteristic directly and add it to the desired
31 // BluetoothGattService instance that represents a local service.
32 class BluetoothGattCharacteristic
{
34 // Values representing the possible properties of a characteristic, which
35 // define how the characteristic can be used. Each of these properties serve
36 // a role as defined in the Bluetooth Specification.
37 // |kPropertyExtendedProperties| is a special property that, if present,
38 // indicates that there is a characteristic descriptor (namely the
39 // "Characteristic Extended Properties Descriptor" with UUID 0x2900) that
40 // contains additional properties pertaining to the characteristic.
41 // The properties "ReliableWrite| and |WriteAuxiliaries| are retrieved from
42 // that characteristic.
45 kPropertyBroadcast
= 1 << 0,
46 kPropertyRead
= 1 << 1,
47 kPropertyWriteWithoutResponse
= 1 << 2,
48 kPropertyWrite
= 1 << 3,
49 kPropertyNotify
= 1 << 4,
50 kPropertyIndicate
= 1 << 5,
51 kPropertyAuthenticatedSignedWrites
= 1 << 6,
52 kPropertyExtendedProperties
= 1 << 7,
53 kPropertyReliableWrite
= 1 << 8,
54 kPropertyWriteableAuxiliaries
= 1 << 9
56 typedef uint32 Properties
;
58 // Values representing read, write, and encryption permissions for a
59 // characteristic's value. While attribute permissions for all GATT
60 // definitions have been set by the Bluetooth specification, characteristic
61 // value permissions are left up to the higher-level profile.
63 // Attribute permissions are distinct from the characteristic properties. For
64 // example, a characteristic may have the property |kPropertyRead| to make
65 // clients know that it is possible to read the characteristic value and have
66 // the permission |kPermissionReadEncrypted| to require a secure connection.
67 // It is up to the application to properly specify the permissions and
68 // properties for a local characteristic.
71 kPermissionRead
= 1 << 0,
72 kPermissionWrite
= 1 << 1,
73 kPermissionReadEncrypted
= 1 << 2,
74 kPermissionWriteEncrypted
= 1 << 3
76 typedef uint32 Permissions
;
78 // The ErrorCallback is used by methods to asynchronously report errors.
79 typedef base::Closure ErrorCallback
;
81 // The ValueCallback is used to return the value of a remote characteristic
82 // upon a read request.
83 typedef base::Callback
<void(const std::vector
<uint8
>&)> ValueCallback
;
85 // Constructs a BluetoothGattCharacteristic that can be associated with a
86 // local GATT service when the adapter is in the peripheral role. To
87 // associate the returned characteristic with a service, add it to a local
88 // service by calling BluetoothGattService::AddCharacteristic.
90 // This method constructs a characteristic with UUID |uuid|, initial cached
91 // value |value|, properties |properties|, and permissions |permissions|.
92 // |value| will be cached and returned for read requests and automatically set
93 // for write requests by default, unless an instance of
94 // BluetoothGattService::Delegate has been provided to the associated
95 // BluetoothGattService instance, in which case the delegate will handle read
96 // and write requests.
98 // NOTE: Don't explicitly set |kPropertyExtendedProperties| in |properties|.
99 // Instead, create and add a BluetoothGattDescriptor that represents the
100 // "Characteristic Extended Properties" descriptor and this will automatically
101 // set the correspoding bit in the characteristic's properties field. If
102 // |properties| has |kPropertyExtendedProperties| set, it will be ignored.
103 static BluetoothGattCharacteristic
* Create(const BluetoothUUID
& uuid
,
104 const std::vector
<uint8
>& value
,
105 Properties properties
,
106 Permissions permissions
);
108 // Identifier used to uniquely identify a GATT characteristic object. This is
109 // different from the characteristic UUID: while multiple characteristics with
110 // the same UUID can exist on a Bluetooth device, the identifier returned from
111 // this method is unique among all characteristics of a device. The contents
112 // of the identifier are platform specific.
113 virtual std::string
GetIdentifier() const = 0;
115 // The Bluetooth-specific UUID of the characteristic.
116 virtual BluetoothUUID
GetUUID() const = 0;
118 // Returns true, if this characteristic is hosted locally. If false, then this
119 // instance represents a remote GATT characteristic.
120 virtual bool IsLocal() const = 0;
122 // Returns the value of the characteristic. For remote characteristics, this
123 // is the most recently cached value. For local characteristics, this is the
124 // most recently updated value or the value retrieved from the delegate.
125 virtual const std::vector
<uint8
>& GetValue() const = 0;
127 // Returns a pointer to the GATT service this characteristic belongs to.
128 virtual BluetoothGattService
* GetService() const = 0;
130 // Returns the bitmask of characteristic properties.
131 virtual Properties
GetProperties() const = 0;
133 // Returns the bitmask of characteristic attribute permissions.
134 virtual Permissions
GetPermissions() const = 0;
136 // Returns the list of GATT characteristic descriptors that provide more
137 // information about this characteristic.
138 virtual std::vector
<BluetoothGattDescriptor
*>
139 GetDescriptors() const = 0;
141 // Returns the GATT characteristic descriptor with identifier |identifier| if
142 // it belongs to this GATT characteristic.
143 virtual BluetoothGattDescriptor
* GetDescriptor(
144 const std::string
& identifier
) const = 0;
146 // Adds a characteristic descriptor to the locally hosted characteristic
147 // represented by this instance. This method only makes sense for local
148 // characteristics and won't have an effect if this instance represents a
149 // remote GATT service and will return false. This method takes ownership
151 virtual bool AddDescriptor(BluetoothGattDescriptor
* descriptor
) = 0;
153 // For locally hosted characteristics, updates the characteristic's value.
154 // This will update the value that is visible to remote devices and send out
155 // any notifications and indications that have been configured. This method
156 // can be used in place of, and in conjunction with,
157 // BluetoothGattService::Delegate methods to send updates to remote devices,
158 // or simply to set update the cached value for read requests without having
159 // to implement the delegate methods.
161 // This method only makes sense for local characteristics and does nothing and
162 // returns false if this instance represents a remote characteristic.
163 virtual bool UpdateValue(const std::vector
<uint8
>& value
) = 0;
165 // Sends a read request to a remote characteristic to read its value.
166 // |callback| is called to return the read value on success and
167 // |error_callback| is called for failures.
168 virtual void ReadRemoteCharacteristic(
169 const ValueCallback
& callback
,
170 const ErrorCallback
& error_callback
) = 0;
172 // Sends a write request to a remote characteristic, to modify the
173 // characteristic's value with the new value |new_value|. |callback| is
174 // called to signal success and |error_callback| for failures. This method
175 // only applies to remote characteristics and will fail for those that are
177 virtual void WriteRemoteCharacteristic(
178 const std::vector
<uint8
>& new_value
,
179 const base::Closure
& callback
,
180 const ErrorCallback
& error_callback
) = 0;
183 BluetoothGattCharacteristic();
184 virtual ~BluetoothGattCharacteristic();
187 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristic
);
190 } // namespace device
192 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_