| 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_SERVICE_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string> | |
| 10 #include <vector> | 9 #include <vector> |
| 11 | 10 |
| 12 #include "base/callback.h" | 11 #include "base/callback.h" |
| 13 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 14 #include "base/macros.h" | 13 #include "base/macros.h" |
| 15 #include "device/bluetooth/bluetooth_export.h" | 14 #include "device/bluetooth/bluetooth_export.h" |
| 15 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 16 #include "device/bluetooth/bluetooth_uuid.h" | 16 #include "device/bluetooth/bluetooth_uuid.h" |
| 17 | 17 |
| 18 namespace device { | 18 namespace device { |
| 19 | 19 |
| 20 class BluetoothDevice; | 20 class BluetoothLocalGattCharacteristic; |
| 21 class BluetoothGattCharacteristic; | 21 class BluetoothLocalGattDescriptor; |
| 22 class BluetoothGattDescriptor; | |
| 23 | 22 |
| 24 // BluetoothGattService represents a local or remote GATT service. A GATT | 23 // BluetoothLocalGattService represents a local GATT service. |
| 25 // service is hosted by a peripheral and represents a collection of data in | |
| 26 // the form of GATT characteristics and a set of included GATT services if this | |
| 27 // service is what is called "a primary service". | |
| 28 // | 24 // |
| 29 // Instances of the BluetoothGattService class are used for two functions: | 25 // Instances of the BluetoothLocalGattService class are used to represent a |
| 30 // 1. To represent GATT attribute hierarchies that have been received from a | 26 // locally hosted GATT attribute hierarchy when the local |
| 31 // remote Bluetooth GATT peripheral. Such BluetoothGattService instances | 27 // adapter is used in the "peripheral" role. Such instances are meant to be |
| 32 // are constructed and owned by a BluetoothDevice. | 28 // constructed directly and registered. Once registered, a GATT attribute |
| 29 // hierarchy will be visible to remote devices in the "central" role. |
| 33 // | 30 // |
| 34 // 2. To represent a locally hosted GATT attribute hierarchy when the local | 31 // Note: We use virtual inheritance on the gatt service since it will be |
| 35 // adapter is used in the "peripheral" role. Such instances are meant to be | 32 // inherited by platform specific versions of the gatt service classes also. The |
| 36 // constructed directly and registered. Once registered, a GATT attribute | 33 // platform specific local gatt service classes will inherit both this class and |
| 37 // hierarchy will be visible to remote devices in the "central" role. | 34 // their gatt service class, hence causing an inheritance diamond. |
| 38 class DEVICE_BLUETOOTH_EXPORT BluetoothGattService { | 35 class DEVICE_BLUETOOTH_EXPORT BluetoothLocalGattService |
| 36 : public virtual BluetoothGattService { |
| 39 public: | 37 public: |
| 40 // The Delegate class is used to send certain events that need to be handled | 38 // The Delegate class is used to send certain events that need to be handled |
| 41 // when the device is in peripheral mode. The delegate handles read and write | 39 // when the device is in peripheral mode. The delegate handles read and write |
| 42 // requests that are issued from remote clients. | 40 // requests that are issued from remote clients. |
| 43 class Delegate { | 41 class Delegate { |
| 44 public: | 42 public: |
| 45 // Callbacks used for communicating GATT request responses. | 43 // Callbacks used for communicating GATT request responses. |
| 46 typedef base::Callback<void(const std::vector<uint8_t>)> ValueCallback; | 44 typedef base::Callback<void(const std::vector<uint8_t>)> ValueCallback; |
| 47 typedef base::Closure ErrorCallback; | 45 typedef base::Closure ErrorCallback; |
| 48 | 46 |
| 49 // Called when a remote device in the central role requests to read the | 47 // Called when a remote device in the central role requests to read the |
| 50 // value of the characteristic |characteristic| starting at offset |offset|. | 48 // value of the characteristic |characteristic| starting at offset |offset|. |
| 51 // This method is only called if the characteristic was specified as | 49 // This method is only called if the characteristic was specified as |
| 52 // readable and any authentication and authorization challenges were | 50 // readable and any authentication and authorization challenges were |
| 53 // satisfied by the remote device. | 51 // satisfied by the remote device. |
| 54 // | 52 // |
| 55 // To respond to the request with success and return the requested value, | 53 // To respond to the request with success and return the requested value, |
| 56 // the delegate must invoke |callback| with the value. Doing so will | 54 // the delegate must invoke |callback| with the value. Doing so will |
| 57 // automatically update the value property of |characteristic|. To respond | 55 // automatically update the value property of |characteristic|. To respond |
| 58 // to the request with failure (e.g. if an invalid offset was given), | 56 // to the request with failure (e.g. if an invalid offset was given), |
| 59 // delegates must invoke |error_callback|. If neither callback parameter is | 57 // delegates must invoke |error_callback|. If neither callback parameter is |
| 60 // invoked, the request will time out and result in an error. Therefore, | 58 // invoked, the request will time out and result in an error. Therefore, |
| 61 // delegates MUST invoke either |callback| or |error_callback|. | 59 // delegates MUST invoke either |callback| or |error_callback|. |
| 62 virtual void OnCharacteristicReadRequest( | 60 virtual void OnCharacteristicReadRequest( |
| 63 const BluetoothGattService* service, | 61 const BluetoothLocalGattService* service, |
| 64 const BluetoothGattCharacteristic* characteristic, | 62 const BluetoothLocalGattCharacteristic* characteristic, |
| 65 int offset, | 63 int offset, |
| 66 const ValueCallback& callback, | 64 const ValueCallback& callback, |
| 67 const ErrorCallback& error_callback) = 0; | 65 const ErrorCallback& error_callback) = 0; |
| 68 | 66 |
| 69 // Called when a remote device in the central role requests to write the | 67 // Called when a remote device in the central role requests to write the |
| 70 // value of the characteristic |characteristic| starting at offset |offset|. | 68 // value of the characteristic |characteristic| starting at offset |offset|. |
| 71 // This method is only called if the characteristic was specified as | 69 // This method is only called if the characteristic was specified as |
| 72 // writable and any authentication and authorization challenges were | 70 // writable and any authentication and authorization challenges were |
| 73 // satisfied by the remote device. | 71 // satisfied by the remote device. |
| 74 // | 72 // |
| 75 // To respond to the request with success the delegate must invoke | 73 // To respond to the request with success the delegate must invoke |
| 76 // |callback| with the new value of the characteristic. Doing so will | 74 // |callback| with the new value of the characteristic. Doing so will |
| 77 // automatically update the value property of |characteristic|. To respond | 75 // automatically update the value property of |characteristic|. To respond |
| 78 // to the request with failure (e.g. if an invalid offset was given), | 76 // to the request with failure (e.g. if an invalid offset was given), |
| 79 // delegates must invoke |error_callback|. If neither callback parameter is | 77 // delegates must invoke |error_callback|. If neither callback parameter is |
| 80 // invoked, the request will time out and result in an error. Therefore, | 78 // invoked, the request will time out and result in an error. Therefore, |
| 81 // delegates MUST invoke either |callback| or |error_callback|. | 79 // delegates MUST invoke either |callback| or |error_callback|. |
| 82 virtual void OnCharacteristicWriteRequest( | 80 virtual void OnCharacteristicWriteRequest( |
| 83 const BluetoothGattService* service, | 81 const BluetoothLocalGattService* service, |
| 84 const BluetoothGattCharacteristic* characteristic, | 82 const BluetoothLocalGattCharacteristic* characteristic, |
| 85 const std::vector<uint8_t>& value, | 83 const std::vector<uint8_t>& value, |
| 86 int offset, | 84 int offset, |
| 87 const ValueCallback& callback, | 85 const ValueCallback& callback, |
| 88 const ErrorCallback& error_callback) = 0; | 86 const ErrorCallback& error_callback) = 0; |
| 89 | 87 |
| 90 // Called when a remote device in the central role requests to read the | 88 // Called when a remote device in the central role requests to read the |
| 91 // value of the descriptor |descriptor| starting at offset |offset|. | 89 // value of the descriptor |descriptor| starting at offset |offset|. |
| 92 // This method is only called if the characteristic was specified as | 90 // This method is only called if the characteristic was specified as |
| 93 // readable and any authentication and authorization challenges were | 91 // readable and any authentication and authorization challenges were |
| 94 // satisfied by the remote device. | 92 // satisfied by the remote device. |
| 95 // | 93 // |
| 96 // To respond to the request with success and return the requested value, | 94 // To respond to the request with success and return the requested value, |
| 97 // the delegate must invoke |callback| with the value. Doing so will | 95 // the delegate must invoke |callback| with the value. Doing so will |
| 98 // automatically update the value property of |descriptor|. To respond | 96 // automatically update the value property of |descriptor|. To respond |
| 99 // to the request with failure (e.g. if an invalid offset was given), | 97 // to the request with failure (e.g. if an invalid offset was given), |
| 100 // delegates must invoke |error_callback|. If neither callback parameter is | 98 // delegates must invoke |error_callback|. If neither callback parameter is |
| 101 // invoked, the request will time out and result in an error. Therefore, | 99 // invoked, the request will time out and result in an error. Therefore, |
| 102 // delegates MUST invoke either |callback| or |error_callback|. | 100 // delegates MUST invoke either |callback| or |error_callback|. |
| 103 virtual void OnDescriptorReadRequest( | 101 virtual void OnDescriptorReadRequest( |
| 104 const BluetoothGattService* service, | 102 const BluetoothLocalGattService* service, |
| 105 const BluetoothGattDescriptor* descriptor, | 103 const BluetoothLocalGattDescriptor* descriptor, |
| 106 int offset, | 104 int offset, |
| 107 const ValueCallback& callback, | 105 const ValueCallback& callback, |
| 108 const ErrorCallback& error_callback) = 0; | 106 const ErrorCallback& error_callback) = 0; |
| 109 | 107 |
| 110 // Called when a remote device in the central role requests to write the | 108 // Called when a remote device in the central role requests to write the |
| 111 // value of the descriptor |descriptor| starting at offset |offset|. | 109 // value of the descriptor |descriptor| starting at offset |offset|. |
| 112 // This method is only called if the characteristic was specified as | 110 // This method is only called if the characteristic was specified as |
| 113 // writable and any authentication and authorization challenges were | 111 // writable and any authentication and authorization challenges were |
| 114 // satisfied by the remote device. | 112 // satisfied by the remote device. |
| 115 // | 113 // |
| 116 // To respond to the request with success the delegate must invoke | 114 // To respond to the request with success the delegate must invoke |
| 117 // |callback| with the new value of the descriptor. Doing so will | 115 // |callback| with the new value of the descriptor. Doing so will |
| 118 // automatically update the value property of |descriptor|. To respond | 116 // automatically update the value property of |descriptor|. To respond |
| 119 // to the request with failure (e.g. if an invalid offset was given), | 117 // to the request with failure (e.g. if an invalid offset was given), |
| 120 // delegates must invoke |error_callback|. If neither callback parameter is | 118 // delegates must invoke |error_callback|. If neither callback parameter is |
| 121 // invoked, the request will time out and result in an error. Therefore, | 119 // invoked, the request will time out and result in an error. Therefore, |
| 122 // delegates MUST invoke either |callback| or |error_callback|. | 120 // delegates MUST invoke either |callback| or |error_callback|. |
| 123 virtual void OnDescriptorWriteRequest( | 121 virtual void OnDescriptorWriteRequest( |
| 124 const BluetoothGattService* service, | 122 const BluetoothLocalGattService* service, |
| 125 const BluetoothGattDescriptor* descriptor, | 123 const BluetoothLocalGattDescriptor* descriptor, |
| 126 const std::vector<uint8_t>& value, | 124 const std::vector<uint8_t>& value, |
| 127 int offset, | 125 int offset, |
| 128 const ValueCallback& callback, | 126 const ValueCallback& callback, |
| 129 const ErrorCallback& error_callback) = 0; | 127 const ErrorCallback& error_callback) = 0; |
| 130 }; | 128 }; |
| 131 | 129 |
| 132 // Interacting with Characteristics and Descriptors can produce | 130 ~BluetoothLocalGattService() override; |
| 133 // this set of errors. | |
| 134 enum GattErrorCode { | |
| 135 GATT_ERROR_UNKNOWN = 0, | |
| 136 GATT_ERROR_FAILED, | |
| 137 GATT_ERROR_IN_PROGRESS, | |
| 138 GATT_ERROR_INVALID_LENGTH, | |
| 139 GATT_ERROR_NOT_PERMITTED, | |
| 140 GATT_ERROR_NOT_AUTHORIZED, | |
| 141 GATT_ERROR_NOT_PAIRED, | |
| 142 GATT_ERROR_NOT_SUPPORTED | |
| 143 }; | |
| 144 | 131 |
| 145 // The ErrorCallback is used by methods to asynchronously report errors. | 132 // Constructs a BluetoothLocalGattService that can be locally hosted when the |
| 146 using ErrorCallback = base::Callback<void(GattErrorCode error_code)>; | 133 // local adapter is in the peripheral role. The resulting object can then be |
| 147 | 134 // made available by calling the "Register" method. This method constructs a |
| 148 virtual ~BluetoothGattService(); | |
| 149 | |
| 150 // Constructs a BluetoothGattService that can be locally hosted when the local | |
| 151 // adapter is in the peripheral role. The resulting object can then be made | |
| 152 // available by calling the "Register" method. This method constructs a | |
| 153 // service with UUID |uuid|. Whether the constructed service is primary or | 135 // service with UUID |uuid|. Whether the constructed service is primary or |
| 154 // secondary is determined by |is_primary|. |delegate| is used to send certain | 136 // secondary is determined by |is_primary|. |delegate| is used to send certain |
| 155 // peripheral role events. If |delegate| is NULL, then this service will | 137 // peripheral role events. If |delegate| is NULL, then this service will |
| 156 // employ a default behavior when responding to read and write requests based | 138 // employ a default behavior when responding to read and write requests based |
| 157 // on the cached value of its characteristics and descriptors at a given time. | 139 // on the cached value of its characteristics and descriptors at a given time. |
| 158 static BluetoothGattService* Create(const BluetoothUUID& uuid, | 140 // TODO(rkc): Implement included services. |
| 159 bool is_primary, | 141 static BluetoothLocalGattService* Create( |
| 160 Delegate* delegate); | 142 const BluetoothUUID& uuid, |
| 161 | 143 bool is_primary, |
| 162 // Identifier used to uniquely identify a GATT service object. This is | 144 BluetoothLocalGattService* included_service, |
| 163 // different from the service UUID: while multiple services with the same UUID | 145 Delegate* delegate); |
| 164 // can exist on a Bluetooth device, the identifier returned from this method | |
| 165 // is unique among all services on the adapter. The contents of the identifier | |
| 166 // are platform specific. | |
| 167 virtual std::string GetIdentifier() const = 0; | |
| 168 | |
| 169 // The Bluetooth-specific UUID of the service. | |
| 170 virtual BluetoothUUID GetUUID() const = 0; | |
| 171 | |
| 172 // Returns true, if this service hosted locally. If false, then this service | |
| 173 // represents a remote GATT service. | |
| 174 virtual bool IsLocal() const = 0; | |
| 175 | |
| 176 // Indicates whether the type of this service is primary or secondary. A | |
| 177 // primary service describes the primary function of the peripheral that | |
| 178 // hosts it, while a secondary service only makes sense in the presence of a | |
| 179 // primary service. A primary service may include other primary or secondary | |
| 180 // services. | |
| 181 virtual bool IsPrimary() const = 0; | |
| 182 | |
| 183 // Returns the BluetoothDevice that this GATT service was received from, which | |
| 184 // also owns this service. Local services always return NULL. | |
| 185 virtual BluetoothDevice* GetDevice() const = 0; | |
| 186 | |
| 187 // List of characteristics that belong to this service. | |
| 188 virtual std::vector<BluetoothGattCharacteristic*> | |
| 189 GetCharacteristics() const = 0; | |
| 190 | |
| 191 // List of GATT services that are included by this service. | |
| 192 virtual std::vector<BluetoothGattService*> | |
| 193 GetIncludedServices() const = 0; | |
| 194 | |
| 195 // Returns the GATT characteristic with identifier |identifier| if it belongs | |
| 196 // to this GATT service. | |
| 197 virtual BluetoothGattCharacteristic* GetCharacteristic( | |
| 198 const std::string& identifier) const = 0; | |
| 199 | |
| 200 // Adds characteristics and included services to the local attribute hierarchy | |
| 201 // represented by this service. These methods only make sense for local | |
| 202 // services and won't have an effect if this instance represents a remote | |
| 203 // GATT service and will return false. While ownership of added | |
| 204 // characteristics are taken over by the service, ownership of an included | |
| 205 // service is not taken. | |
| 206 virtual bool AddCharacteristic( | |
| 207 BluetoothGattCharacteristic* characteristic) = 0; | |
| 208 virtual bool AddIncludedService(BluetoothGattService* service) = 0; | |
| 209 | 146 |
| 210 // Registers this GATT service. Calling Register will make this service and | 147 // Registers this GATT service. Calling Register will make this service and |
| 211 // all of its associated attributes available on the local adapters GATT | 148 // all of its associated attributes available on the local adapters GATT |
| 212 // database and the service UUID will be advertised to nearby devices if the | 149 // database and the service UUID will be advertised to nearby devices if the |
| 213 // local adapter is discoverable. Call Unregister to make this service no | 150 // local adapter is discoverable. Call Unregister to make this service no |
| 214 // longer available. | 151 // longer available. |
| 215 // | 152 // |
| 216 // These methods only make sense for services that are local and will hence | 153 // These methods only make sense for services that are local and will hence |
| 217 // fail if this instance represents a remote GATT service. |callback| is | 154 // fail if this instance represents a remote GATT service. |callback| is |
| 218 // called to denote success and |error_callback| to denote failure. | 155 // called to denote success and |error_callback| to denote failure. |
| 219 virtual void Register(const base::Closure& callback, | 156 virtual void Register(const base::Closure& callback, |
| 220 const ErrorCallback& error_callback) = 0; | 157 const ErrorCallback& error_callback) = 0; |
| 221 virtual void Unregister(const base::Closure& callback, | 158 virtual void Unregister(const base::Closure& callback, |
| 222 const ErrorCallback& error_callback) = 0; | 159 const ErrorCallback& error_callback) = 0; |
| 223 | 160 |
| 224 protected: | 161 protected: |
| 225 BluetoothGattService(); | 162 BluetoothLocalGattService(); |
| 226 | 163 |
| 227 private: | 164 private: |
| 228 DISALLOW_COPY_AND_ASSIGN(BluetoothGattService); | 165 DISALLOW_COPY_AND_ASSIGN(BluetoothLocalGattService); |
| 229 }; | 166 }; |
| 230 | 167 |
| 231 } // namespace device | 168 } // namespace device |
| 232 | 169 |
| 233 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ | 170 #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |
| OLD | NEW |