| 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_SERVICE_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | |
| 9 #include <string> | 8 #include <string> |
| 10 #include <vector> | |
| 11 | 9 |
| 12 #include "base/callback.h" | |
| 13 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| 14 #include "base/macros.h" | 11 #include "base/macros.h" |
| 15 #include "device/bluetooth/bluetooth_export.h" | 12 #include "device/bluetooth/bluetooth_export.h" |
| 16 #include "device/bluetooth/bluetooth_uuid.h" | |
| 17 | 13 |
| 18 namespace device { | 14 namespace device { |
| 19 | 15 |
| 20 class BluetoothDevice; | |
| 21 class BluetoothGattCharacteristic; | |
| 22 class BluetoothGattDescriptor; | |
| 23 | |
| 24 // BluetoothGattService represents a local or remote GATT service. A GATT | 16 // BluetoothGattService represents a local or remote GATT service. A GATT |
| 25 // service is hosted by a peripheral and represents a collection of data in | 17 // 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 | 18 // the form of GATT characteristics and a set of included GATT services if this |
| 27 // service is what is called "a primary service". | 19 // service is what is called "a primary service". |
| 28 // | |
| 29 // Instances of the BluetoothGattService class are used for two functions: | |
| 30 // 1. To represent GATT attribute hierarchies that have been received from a | |
| 31 // remote Bluetooth GATT peripheral. Such BluetoothGattService instances | |
| 32 // are constructed and owned by a BluetoothDevice. | |
| 33 // | |
| 34 // 2. To represent a locally hosted GATT attribute hierarchy when the local | |
| 35 // adapter is used in the "peripheral" role. Such instances are meant to be | |
| 36 // constructed directly and registered. Once registered, a GATT attribute | |
| 37 // hierarchy will be visible to remote devices in the "central" role. | |
| 38 class DEVICE_BLUETOOTH_EXPORT BluetoothGattService { | 20 class DEVICE_BLUETOOTH_EXPORT BluetoothGattService { |
| 39 public: | 21 public: |
| 40 // 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 | |
| 42 // requests that are issued from remote clients. | |
| 43 class Delegate { | |
| 44 public: | |
| 45 // Callbacks used for communicating GATT request responses. | |
| 46 typedef base::Callback<void(const std::vector<uint8_t>)> ValueCallback; | |
| 47 typedef base::Closure ErrorCallback; | |
| 48 | |
| 49 // Called when a remote device in the central role requests to read the | |
| 50 // value of the characteristic |characteristic| starting at offset |offset|. | |
| 51 // This method is only called if the characteristic was specified as | |
| 52 // readable and any authentication and authorization challenges were | |
| 53 // satisfied by the remote device. | |
| 54 // | |
| 55 // To respond to the request with success and return the requested value, | |
| 56 // the delegate must invoke |callback| with the value. Doing so will | |
| 57 // automatically update the value property of |characteristic|. To respond | |
| 58 // to the request with failure (e.g. if an invalid offset was given), | |
| 59 // delegates must invoke |error_callback|. If neither callback parameter is | |
| 60 // invoked, the request will time out and result in an error. Therefore, | |
| 61 // delegates MUST invoke either |callback| or |error_callback|. | |
| 62 virtual void OnCharacteristicReadRequest( | |
| 63 const BluetoothGattService* service, | |
| 64 const BluetoothGattCharacteristic* characteristic, | |
| 65 int offset, | |
| 66 const ValueCallback& callback, | |
| 67 const ErrorCallback& error_callback) = 0; | |
| 68 | |
| 69 // Called when a remote device in the central role requests to write the | |
| 70 // value of the characteristic |characteristic| starting at offset |offset|. | |
| 71 // This method is only called if the characteristic was specified as | |
| 72 // writable and any authentication and authorization challenges were | |
| 73 // satisfied by the remote device. | |
| 74 // | |
| 75 // To respond to the request with success the delegate must invoke | |
| 76 // |callback| with the new value of the characteristic. Doing so will | |
| 77 // automatically update the value property of |characteristic|. To respond | |
| 78 // to the request with failure (e.g. if an invalid offset was given), | |
| 79 // delegates must invoke |error_callback|. If neither callback parameter is | |
| 80 // invoked, the request will time out and result in an error. Therefore, | |
| 81 // delegates MUST invoke either |callback| or |error_callback|. | |
| 82 virtual void OnCharacteristicWriteRequest( | |
| 83 const BluetoothGattService* service, | |
| 84 const BluetoothGattCharacteristic* characteristic, | |
| 85 const std::vector<uint8_t>& value, | |
| 86 int offset, | |
| 87 const ValueCallback& callback, | |
| 88 const ErrorCallback& error_callback) = 0; | |
| 89 | |
| 90 // Called when a remote device in the central role requests to read the | |
| 91 // value of the descriptor |descriptor| starting at offset |offset|. | |
| 92 // This method is only called if the characteristic was specified as | |
| 93 // readable and any authentication and authorization challenges were | |
| 94 // satisfied by the remote device. | |
| 95 // | |
| 96 // To respond to the request with success and return the requested value, | |
| 97 // the delegate must invoke |callback| with the value. Doing so will | |
| 98 // automatically update the value property of |descriptor|. To respond | |
| 99 // to the request with failure (e.g. if an invalid offset was given), | |
| 100 // delegates must invoke |error_callback|. If neither callback parameter is | |
| 101 // invoked, the request will time out and result in an error. Therefore, | |
| 102 // delegates MUST invoke either |callback| or |error_callback|. | |
| 103 virtual void OnDescriptorReadRequest( | |
| 104 const BluetoothGattService* service, | |
| 105 const BluetoothGattDescriptor* descriptor, | |
| 106 int offset, | |
| 107 const ValueCallback& callback, | |
| 108 const ErrorCallback& error_callback) = 0; | |
| 109 | |
| 110 // Called when a remote device in the central role requests to write the | |
| 111 // value of the descriptor |descriptor| starting at offset |offset|. | |
| 112 // This method is only called if the characteristic was specified as | |
| 113 // writable and any authentication and authorization challenges were | |
| 114 // satisfied by the remote device. | |
| 115 // | |
| 116 // To respond to the request with success the delegate must invoke | |
| 117 // |callback| with the new value of the descriptor. Doing so will | |
| 118 // automatically update the value property of |descriptor|. To respond | |
| 119 // to the request with failure (e.g. if an invalid offset was given), | |
| 120 // delegates must invoke |error_callback|. If neither callback parameter is | |
| 121 // invoked, the request will time out and result in an error. Therefore, | |
| 122 // delegates MUST invoke either |callback| or |error_callback|. | |
| 123 virtual void OnDescriptorWriteRequest( | |
| 124 const BluetoothGattService* service, | |
| 125 const BluetoothGattDescriptor* descriptor, | |
| 126 const std::vector<uint8_t>& value, | |
| 127 int offset, | |
| 128 const ValueCallback& callback, | |
| 129 const ErrorCallback& error_callback) = 0; | |
| 130 }; | |
| 131 | |
| 132 // Interacting with Characteristics and Descriptors can produce | 22 // Interacting with Characteristics and Descriptors can produce |
| 133 // this set of errors. | 23 // this set of errors. |
| 134 enum GattErrorCode { | 24 enum GattErrorCode { |
| 135 GATT_ERROR_UNKNOWN = 0, | 25 GATT_ERROR_UNKNOWN = 0, |
| 136 GATT_ERROR_FAILED, | 26 GATT_ERROR_FAILED, |
| 137 GATT_ERROR_IN_PROGRESS, | 27 GATT_ERROR_IN_PROGRESS, |
| 138 GATT_ERROR_INVALID_LENGTH, | 28 GATT_ERROR_INVALID_LENGTH, |
| 139 GATT_ERROR_NOT_PERMITTED, | 29 GATT_ERROR_NOT_PERMITTED, |
| 140 GATT_ERROR_NOT_AUTHORIZED, | 30 GATT_ERROR_NOT_AUTHORIZED, |
| 141 GATT_ERROR_NOT_PAIRED, | 31 GATT_ERROR_NOT_PAIRED, |
| 142 GATT_ERROR_NOT_SUPPORTED | 32 GATT_ERROR_NOT_SUPPORTED |
| 143 }; | 33 }; |
| 144 | 34 |
| 145 // The ErrorCallback is used by methods to asynchronously report errors. | 35 // The ErrorCallback is used by methods to asynchronously report errors. |
| 146 using ErrorCallback = base::Callback<void(GattErrorCode error_code)>; | 36 using ErrorCallback = base::Callback<void(GattErrorCode error_code)>; |
| 147 | 37 |
| 148 virtual ~BluetoothGattService(); | 38 virtual ~BluetoothGattService(); |
| 149 | 39 |
| 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 | |
| 154 // secondary is determined by |is_primary|. |delegate| is used to send certain | |
| 155 // peripheral role events. If |delegate| is NULL, then this service will | |
| 156 // 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. | |
| 158 static BluetoothGattService* Create(const BluetoothUUID& uuid, | |
| 159 bool is_primary, | |
| 160 Delegate* delegate); | |
| 161 | |
| 162 // Identifier used to uniquely identify a GATT service object. This is | 40 // Identifier used to uniquely identify a GATT service object. This is |
| 163 // different from the service UUID: while multiple services with the same UUID | 41 // different from the service UUID: while multiple services with the same UUID |
| 164 // can exist on a Bluetooth device, the identifier returned from this method | 42 // 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 | 43 // is unique among all services on the adapter. The contents of the identifier |
| 166 // are platform specific. | 44 // are platform specific. |
| 167 virtual std::string GetIdentifier() const = 0; | 45 virtual std::string GetIdentifier() const = 0; |
| 168 | 46 |
| 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 | |
| 210 // Registers this GATT service. Calling Register will make this service and | |
| 211 // 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 | |
| 213 // local adapter is discoverable. Call Unregister to make this service no | |
| 214 // longer available. | |
| 215 // | |
| 216 // 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 | |
| 218 // called to denote success and |error_callback| to denote failure. | |
| 219 virtual void Register(const base::Closure& callback, | |
| 220 const ErrorCallback& error_callback) = 0; | |
| 221 virtual void Unregister(const base::Closure& callback, | |
| 222 const ErrorCallback& error_callback) = 0; | |
| 223 | |
| 224 protected: | 47 protected: |
| 225 BluetoothGattService(); | 48 BluetoothGattService(); |
| 226 | 49 |
| 227 private: | 50 private: |
| 228 DISALLOW_COPY_AND_ASSIGN(BluetoothGattService); | 51 DISALLOW_COPY_AND_ASSIGN(BluetoothGattService); |
| 229 }; | 52 }; |
| 230 | 53 |
| 231 } // namespace device | 54 } // namespace device |
| 232 | 55 |
| 233 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ | 56 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ |
| OLD | NEW |