OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | |
10 #include <memory> | |
11 #include <string> | 9 #include <string> |
12 #include <vector> | 10 #include <vector> |
13 | 11 |
14 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/callback_forward.h" |
15 #include "base/macros.h" | 14 #include "base/macros.h" |
16 #include "device/bluetooth/bluetooth_export.h" | 15 #include "device/bluetooth/bluetooth_export.h" |
17 #include "device/bluetooth/bluetooth_gatt_service.h" | 16 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
18 #include "device/bluetooth/bluetooth_uuid.h" | 17 #include "device/bluetooth/bluetooth_uuid.h" |
19 | 18 |
20 namespace device { | 19 namespace device { |
21 | 20 |
22 class BluetoothGattDescriptor; | |
23 class BluetoothGattNotifySession; | 21 class BluetoothGattNotifySession; |
| 22 class BluetoothRemoteGattDescriptor; |
| 23 class BluetoothRemoteGattService; |
24 | 24 |
25 // BluetoothGattCharacteristic represents a local or remote GATT characteristic. | 25 // BluetoothRemoteGattCharacteristic represents a remote GATT characteristic. |
26 // A GATT characteristic is a basic data element used to construct a GATT | 26 // This class is used to represent GATT characteristics that belong to a service |
27 // service. Hence, instances of a BluetoothGattCharacteristic are associated | 27 // hosted by a remote device. In this case the characteristic will be |
28 // with a BluetoothGattService. There are two ways in which this class is used: | 28 // constructed by the subsystem. |
29 // | 29 // |
30 // 1. To represent GATT characteristics that belong to a service hosted by a | 30 // Note: We use virtual inheritance on the GATT characteristic since it will be |
31 // remote device. In this case the characteristic will be constructed by | 31 // inherited by platform specific versions of the GATT characteristic classes |
32 // the subsystem. | 32 // also. The platform specific remote GATT characteristic classes will inherit |
33 // 2. To represent GATT characteristics that belong to a locally hosted | 33 // both this class and their GATT characteristic class, hence causing an |
34 // service. To achieve this, users can construct instances of | 34 // inheritance diamond. |
35 // BluetoothGattCharacteristic directly and add it to the desired | 35 class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic |
36 // BluetoothGattService instance that represents a local service. | 36 : public virtual BluetoothGattCharacteristic { |
37 class DEVICE_BLUETOOTH_EXPORT BluetoothGattCharacteristic { | |
38 public: | 37 public: |
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 | |
60 }; | |
61 typedef uint32_t Properties; | |
62 | |
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. | |
67 // | |
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 | |
80 }; | |
81 typedef uint32_t Permissions; | |
82 | |
83 // The ErrorCallback is used by methods to asynchronously report errors. | |
84 typedef base::Callback<void(BluetoothGattService::GattErrorCode)> | |
85 ErrorCallback; | |
86 | |
87 // The ValueCallback is used to return the value of a remote characteristic | 38 // The ValueCallback is used to return the value of a remote characteristic |
88 // upon a read request. | 39 // upon a read request. |
89 typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback; | 40 typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback; |
90 | 41 |
91 // The NotifySessionCallback is used to return sessions after they have | 42 // The NotifySessionCallback is used to return sessions after they have |
92 // been successfully started. | 43 // been successfully started. |
93 typedef base::Callback<void(std::unique_ptr<BluetoothGattNotifySession>)> | 44 typedef base::Callback<void(std::unique_ptr<BluetoothGattNotifySession>)> |
94 NotifySessionCallback; | 45 NotifySessionCallback; |
95 | 46 |
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. | |
100 // | |
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. | |
108 // | |
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_t>& value, | |
116 Properties properties, | |
117 Permissions permissions); | |
118 | |
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 on the adapter. The | |
123 // contents of the identifier are platform specific. | |
124 virtual std::string GetIdentifier() const = 0; | |
125 | |
126 // The Bluetooth-specific UUID of the characteristic. | |
127 virtual BluetoothUUID GetUUID() const = 0; | |
128 | |
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; | |
132 | |
133 // Returns the value of the characteristic. For remote characteristics, this | 47 // Returns the value of the characteristic. For remote characteristics, this |
134 // is the most recently cached value. For local characteristics, this is the | 48 // is the most recently cached value. For local characteristics, this is the |
135 // most recently updated value or the value retrieved from the delegate. | 49 // most recently updated value or the value retrieved from the delegate. |
136 virtual const std::vector<uint8_t>& GetValue() const = 0; | 50 virtual const std::vector<uint8_t>& GetValue() const = 0; |
137 | 51 |
138 // Returns a pointer to the GATT service this characteristic belongs to. | 52 // Returns a pointer to the GATT service this characteristic belongs to. |
139 virtual BluetoothGattService* GetService() const = 0; | 53 virtual BluetoothRemoteGattService* GetService() const = 0; |
140 | |
141 // Returns the bitmask of characteristic properties. | |
142 virtual Properties GetProperties() const = 0; | |
143 | |
144 // Returns the bitmask of characteristic attribute permissions. | |
145 virtual Permissions GetPermissions() const = 0; | |
146 | 54 |
147 // Returns whether or not this characteristic is currently sending value | 55 // Returns whether or not this characteristic is currently sending value |
148 // updates in the form of a notification or indication. | 56 // updates in the form of a notification or indication. |
149 virtual bool IsNotifying() const = 0; | 57 virtual bool IsNotifying() const = 0; |
150 | 58 |
151 // Returns the list of GATT characteristic descriptors that provide more | 59 // Returns the list of GATT characteristic descriptors that provide more |
152 // information about this characteristic. | 60 // information about this characteristic. |
153 virtual std::vector<BluetoothGattDescriptor*> | 61 virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() |
154 GetDescriptors() const = 0; | 62 const = 0; |
155 | 63 |
156 // Returns the GATT characteristic descriptor with identifier |identifier| if | 64 // Returns the GATT characteristic descriptor with identifier |identifier| if |
157 // it belongs to this GATT characteristic. | 65 // it belongs to this GATT characteristic. |
158 virtual BluetoothGattDescriptor* GetDescriptor( | 66 virtual BluetoothRemoteGattDescriptor* GetDescriptor( |
159 const std::string& identifier) const = 0; | 67 const std::string& identifier) const = 0; |
160 | 68 |
161 // Returns the GATT characteristic descriptors that match |uuid|. There may be | 69 // Returns the GATT characteristic descriptors that match |uuid|. There may be |
162 // multiple, as illustrated by Core Bluetooth Specification [V4.2 Vol 3 Part G | 70 // multiple, as illustrated by Core Bluetooth Specification [V4.2 Vol 3 Part G |
163 // 3.3.3.5 Characteristic Presentation Format]. | 71 // 3.3.3.5 Characteristic Presentation Format]. |
164 std::vector<BluetoothGattDescriptor*> GetDescriptorsByUUID( | 72 std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID( |
165 const BluetoothUUID& uuid); | 73 const BluetoothUUID& uuid); |
166 | 74 |
167 // Adds a characteristic descriptor to the locally hosted characteristic | |
168 // represented by this instance. This method only makes sense for local | |
169 // characteristics and won't have an effect if this instance represents a | |
170 // remote GATT service and will return false. This method takes ownership | |
171 // of |descriptor|. | |
172 virtual bool AddDescriptor(BluetoothGattDescriptor* descriptor) = 0; | |
173 | |
174 // For locally hosted characteristics, updates the characteristic's value. | |
175 // This will update the value that is visible to remote devices and send out | |
176 // any notifications and indications that have been configured. This method | |
177 // can be used in place of, and in conjunction with, | |
178 // BluetoothGattService::Delegate methods to send updates to remote devices, | |
179 // or simply to set update the cached value for read requests without having | |
180 // to implement the delegate methods. | |
181 // | |
182 // This method only makes sense for local characteristics and does nothing and | |
183 // returns false if this instance represents a remote characteristic. | |
184 virtual bool UpdateValue(const std::vector<uint8_t>& value) = 0; | |
185 | |
186 // Starts a notify session for the remote characteristic, if it supports | 75 // Starts a notify session for the remote characteristic, if it supports |
187 // notifications/indications. On success, the characteristic starts sending | 76 // notifications/indications. On success, the characteristic starts sending |
188 // value notifications and |callback| is called with a session object whose | 77 // value notifications and |callback| is called with a session object whose |
189 // ownership belongs to the caller. |error_callback| is called on errors. | 78 // ownership belongs to the caller. |error_callback| is called on errors. |
190 // | 79 // |
191 // Writes to the Client Characteristic Configuration descriptor to enable | 80 // Writes to the Client Characteristic Configuration descriptor to enable |
192 // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G | 81 // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G |
193 // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be | 82 // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be |
194 // present when notifications/indications are supported. If the descriptor is | 83 // present when notifications/indications are supported. If the descriptor is |
195 // not present |error_callback| will be run. | 84 // not present |error_callback| will be run. |
(...skipping 11 matching lines...) Expand all Loading... |
207 // characteristic's value with the new value |new_value|. |callback| is | 96 // characteristic's value with the new value |new_value|. |callback| is |
208 // called to signal success and |error_callback| for failures. This method | 97 // called to signal success and |error_callback| for failures. This method |
209 // only applies to remote characteristics and will fail for those that are | 98 // only applies to remote characteristics and will fail for those that are |
210 // locally hosted. | 99 // locally hosted. |
211 virtual void WriteRemoteCharacteristic( | 100 virtual void WriteRemoteCharacteristic( |
212 const std::vector<uint8_t>& new_value, | 101 const std::vector<uint8_t>& new_value, |
213 const base::Closure& callback, | 102 const base::Closure& callback, |
214 const ErrorCallback& error_callback) = 0; | 103 const ErrorCallback& error_callback) = 0; |
215 | 104 |
216 protected: | 105 protected: |
217 BluetoothGattCharacteristic(); | 106 BluetoothRemoteGattCharacteristic(); |
218 virtual ~BluetoothGattCharacteristic(); | 107 ~BluetoothRemoteGattCharacteristic() override; |
219 | 108 |
220 private: | 109 private: |
221 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristic); | 110 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristic); |
222 }; | 111 }; |
223 | 112 |
224 } // namespace device | 113 } // namespace device |
225 | 114 |
226 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_ | 115 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ |
OLD | NEW |