| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_GATT_CHARACTERISTIC_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_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> | |
| 13 | 10 |
| 14 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/callback_forward.h" |
| 15 #include "base/macros.h" | 13 #include "base/macros.h" |
| 16 #include "device/bluetooth/bluetooth_export.h" | 14 #include "device/bluetooth/bluetooth_export.h" |
| 17 #include "device/bluetooth/bluetooth_gatt_service.h" | 15 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 18 #include "device/bluetooth/bluetooth_uuid.h" | 16 #include "device/bluetooth/bluetooth_uuid.h" |
| 19 | 17 |
| 20 namespace device { | 18 namespace device { |
| 21 | 19 |
| 22 class BluetoothGattDescriptor; | 20 class BluetoothRemoteGattDescriptor; |
| 23 class BluetoothGattNotifySession; | 21 class BluetoothGattNotifySession; |
| 24 | 22 |
| 25 // BluetoothGattCharacteristic represents a local or remote GATT characteristic. | 23 // BluetoothGattCharacteristic represents a local or remote GATT characteristic. |
| 26 // A GATT characteristic is a basic data element used to construct a GATT | 24 // A GATT characteristic is a basic data element used to construct a GATT |
| 27 // service. Hence, instances of a BluetoothGattCharacteristic are associated | 25 // service. Hence, instances of a BluetoothGattCharacteristic are associated |
| 28 // with a BluetoothGattService. There are two ways in which this class is used: | 26 // with a BluetoothRemoteGattService. There are two ways in which this class is |
| 29 // | 27 // used: |
| 30 // 1. To represent GATT characteristics that belong to a service hosted by a | |
| 31 // remote device. In this case the characteristic will be constructed by | |
| 32 // the subsystem. | |
| 33 // 2. To represent GATT characteristics that belong to a locally hosted | |
| 34 // service. To achieve this, users can construct instances of | |
| 35 // BluetoothGattCharacteristic directly and add it to the desired | |
| 36 // BluetoothGattService instance that represents a local service. | |
| 37 class DEVICE_BLUETOOTH_EXPORT BluetoothGattCharacteristic { | 28 class DEVICE_BLUETOOTH_EXPORT BluetoothGattCharacteristic { |
| 38 public: | 29 public: |
| 39 // Values representing the possible properties of a characteristic, which | 30 // Values representing the possible properties of a characteristic, which |
| 40 // define how the characteristic can be used. Each of these properties serve | 31 // define how the characteristic can be used. Each of these properties serve |
| 41 // a role as defined in the Bluetooth Specification. | 32 // a role as defined in the Bluetooth Specification. |
| 42 // |PROPERTY_EXTENDED_PROPERTIES| is a special property that, if present, | 33 // |PROPERTY_EXTENDED_PROPERTIES| is a special property that, if present, |
| 43 // indicates that there is a characteristic descriptor (namely the | 34 // indicates that there is a characteristic descriptor (namely the |
| 44 // "Characteristic Extended Properties Descriptor" with UUID 0x2900) that | 35 // "Characteristic Extended Properties Descriptor" with UUID 0x2900) that |
| 45 // contains additional properties pertaining to the characteristic. | 36 // contains additional properties pertaining to the characteristic. |
| 46 // The properties "ReliableWrite| and |WriteAuxiliaries| are retrieved from | 37 // The properties "ReliableWrite| and |WriteAuxiliaries| are retrieved from |
| (...skipping 30 matching lines...) Expand all Loading... |
| 77 PERMISSION_WRITE = 1 << 1, | 68 PERMISSION_WRITE = 1 << 1, |
| 78 PERMISSION_READ_ENCRYPTED = 1 << 2, | 69 PERMISSION_READ_ENCRYPTED = 1 << 2, |
| 79 PERMISSION_WRITE_ENCRYPTED = 1 << 3 | 70 PERMISSION_WRITE_ENCRYPTED = 1 << 3 |
| 80 }; | 71 }; |
| 81 typedef uint32_t Permissions; | 72 typedef uint32_t Permissions; |
| 82 | 73 |
| 83 // The ErrorCallback is used by methods to asynchronously report errors. | 74 // The ErrorCallback is used by methods to asynchronously report errors. |
| 84 typedef base::Callback<void(BluetoothGattService::GattErrorCode)> | 75 typedef base::Callback<void(BluetoothGattService::GattErrorCode)> |
| 85 ErrorCallback; | 76 ErrorCallback; |
| 86 | 77 |
| 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_t>&)> ValueCallback; | |
| 90 | |
| 91 // The NotifySessionCallback is used to return sessions after they have | |
| 92 // been successfully started. | |
| 93 typedef base::Callback<void(std::unique_ptr<BluetoothGattNotifySession>)> | |
| 94 NotifySessionCallback; | |
| 95 | |
| 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 | 78 // Identifier used to uniquely identify a GATT characteristic object. This is |
| 120 // different from the characteristic UUID: while multiple characteristics with | 79 // different from the characteristic UUID: while multiple characteristics with |
| 121 // the same UUID can exist on a Bluetooth device, the identifier returned from | 80 // 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 | 81 // this method is unique among all characteristics on the adapter. The |
| 123 // contents of the identifier are platform specific. | 82 // contents of the identifier are platform specific. |
| 124 virtual std::string GetIdentifier() const = 0; | 83 virtual std::string GetIdentifier() const = 0; |
| 125 | 84 |
| 126 // The Bluetooth-specific UUID of the characteristic. | 85 // The Bluetooth-specific UUID of the characteristic. |
| 127 virtual BluetoothUUID GetUUID() const = 0; | 86 virtual BluetoothUUID GetUUID() const = 0; |
| 128 | 87 |
| 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 | |
| 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_t>& GetValue() const = 0; | |
| 137 | |
| 138 // Returns a pointer to the GATT service this characteristic belongs to. | |
| 139 virtual BluetoothGattService* GetService() const = 0; | |
| 140 | |
| 141 // Returns the bitmask of characteristic properties. | 88 // Returns the bitmask of characteristic properties. |
| 142 virtual Properties GetProperties() const = 0; | 89 virtual Properties GetProperties() const = 0; |
| 143 | 90 |
| 144 // Returns the bitmask of characteristic attribute permissions. | 91 // Returns the bitmask of characteristic attribute permissions. |
| 145 virtual Permissions GetPermissions() const = 0; | 92 virtual Permissions GetPermissions() const = 0; |
| 146 | 93 |
| 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; | |
| 150 | |
| 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; | |
| 155 | |
| 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; | |
| 160 | |
| 161 // 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 | |
| 163 // 3.3.3.5 Characteristic Presentation Format]. | |
| 164 std::vector<BluetoothGattDescriptor*> GetDescriptorsByUUID( | |
| 165 const BluetoothUUID& uuid); | |
| 166 | |
| 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 | |
| 187 // notifications/indications. On success, the characteristic starts sending | |
| 188 // value notifications and |callback| is called with a session object whose | |
| 189 // ownership belongs to the caller. |error_callback| is called on errors. | |
| 190 // | |
| 191 // Writes to the Client Characteristic Configuration descriptor to enable | |
| 192 // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G | |
| 193 // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be | |
| 194 // present when notifications/indications are supported. If the descriptor is | |
| 195 // not present |error_callback| will be run. | |
| 196 virtual void StartNotifySession(const NotifySessionCallback& callback, | |
| 197 const ErrorCallback& error_callback) = 0; | |
| 198 | |
| 199 // Sends a read request to a remote characteristic to read its value. | |
| 200 // |callback| is called to return the read value on success and | |
| 201 // |error_callback| is called for failures. | |
| 202 virtual void ReadRemoteCharacteristic( | |
| 203 const ValueCallback& callback, | |
| 204 const ErrorCallback& error_callback) = 0; | |
| 205 | |
| 206 // Sends a write request to a remote characteristic, to modify the | |
| 207 // characteristic's value with the new value |new_value|. |callback| is | |
| 208 // called to signal success and |error_callback| for failures. This method | |
| 209 // only applies to remote characteristics and will fail for those that are | |
| 210 // locally hosted. | |
| 211 virtual void WriteRemoteCharacteristic( | |
| 212 const std::vector<uint8_t>& new_value, | |
| 213 const base::Closure& callback, | |
| 214 const ErrorCallback& error_callback) = 0; | |
| 215 | |
| 216 protected: | 94 protected: |
| 217 BluetoothGattCharacteristic(); | 95 BluetoothGattCharacteristic(); |
| 218 virtual ~BluetoothGattCharacteristic(); | 96 virtual ~BluetoothGattCharacteristic(); |
| 219 | 97 |
| 220 private: | 98 private: |
| 221 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristic); | 99 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristic); |
| 222 }; | 100 }; |
| 223 | 101 |
| 224 } // namespace device | 102 } // namespace device |
| 225 | 103 |
| 226 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_ | 104 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CHARACTERISTIC_H_ |
| OLD | NEW |