rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |
| 6 | #define DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/callback.h" |
| 12 | #include "base/callback_forward.h" |
| 13 | #include "base/macros.h" |
rkc | f27a499 | 2016-04-21 18:22:13 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
| 15 | #include "device/bluetooth/bluetooth_adapter.h" |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 16 | #include "device/bluetooth/bluetooth_export.h" |
| 17 | #include "device/bluetooth/bluetooth_gatt_service.h" |
| 18 | #include "device/bluetooth/bluetooth_uuid.h" |
| 19 | |
| 20 | namespace device { |
| 21 | |
| 22 | class BluetoothLocalGattCharacteristic; |
| 23 | class BluetoothLocalGattDescriptor; |
| 24 | |
| 25 | // BluetoothLocalGattService represents a local GATT service. |
| 26 | // |
| 27 | // Instances of the BluetoothLocalGattService class are used to represent a |
| 28 | // locally hosted GATT attribute hierarchy when the local |
| 29 | // adapter is used in the "peripheral" role. Such instances are meant to be |
| 30 | // constructed directly and registered. Once registered, a GATT attribute |
| 31 | // hierarchy will be visible to remote devices in the "central" role. |
rkc | f27a499 | 2016-04-21 18:22:13 | [diff] [blame] | 32 | // BT local GATT services will be owned by the adapter they are created with. |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 33 | // |
| 34 | // Note: We use virtual inheritance on the gatt service since it will be |
| 35 | // inherited by platform specific versions of the gatt service classes also. The |
| 36 | // platform specific local gatt service classes will inherit both this class and |
| 37 | // their gatt service class, hence causing an inheritance diamond. |
| 38 | class DEVICE_BLUETOOTH_EXPORT BluetoothLocalGattService |
| 39 | : public virtual BluetoothGattService { |
| 40 | public: |
| 41 | // The Delegate class is used to send certain events that need to be handled |
| 42 | // when the device is in peripheral mode. The delegate handles read and write |
| 43 | // requests that are issued from remote clients. |
| 44 | class Delegate { |
| 45 | public: |
| 46 | // Callbacks used for communicating GATT request responses. |
rkc | a65ced97 | 2016-04-28 06:56:38 | [diff] [blame] | 47 | typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback; |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 48 | typedef base::Closure ErrorCallback; |
| 49 | |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 50 | // Called when a remote device |device| requests to read the value of the |
| 51 | // characteristic |characteristic| starting at offset |offset|. |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 52 | // This method is only called if the characteristic was specified as |
| 53 | // readable and any authentication and authorization challenges were |
| 54 | // satisfied by the remote device. |
| 55 | // |
| 56 | // To respond to the request with success and return the requested value, |
| 57 | // the delegate must invoke |callback| with the value. Doing so will |
| 58 | // automatically update the value property of |characteristic|. To respond |
| 59 | // to the request with failure (e.g. if an invalid offset was given), |
| 60 | // delegates must invoke |error_callback|. If neither callback parameter is |
| 61 | // invoked, the request will time out and result in an error. Therefore, |
| 62 | // delegates MUST invoke either |callback| or |error_callback|. |
| 63 | virtual void OnCharacteristicReadRequest( |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 64 | const BluetoothDevice* device, |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 65 | const BluetoothLocalGattCharacteristic* characteristic, |
| 66 | int offset, |
| 67 | const ValueCallback& callback, |
| 68 | const ErrorCallback& error_callback) = 0; |
| 69 | |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 70 | // Called when a remote device |device| requests to write the value of the |
| 71 | // characteristic |characteristic| starting at offset |offset|. |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 72 | // This method is only called if the characteristic was specified as |
| 73 | // writable and any authentication and authorization challenges were |
| 74 | // satisfied by the remote device. |
| 75 | // |
| 76 | // To respond to the request with success the delegate must invoke |
rkc | a65ced97 | 2016-04-28 06:56:38 | [diff] [blame] | 77 | // |callback|. To respond to the request with failure delegates must invoke |
| 78 | // |error_callback|. If neither callback parameter is invoked, the request |
| 79 | // will time out and result in an error. Therefore, delegates MUST invoke |
| 80 | // either |callback| or |error_callback|. |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 81 | virtual void OnCharacteristicWriteRequest( |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 82 | const BluetoothDevice* device, |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 83 | const BluetoothLocalGattCharacteristic* characteristic, |
| 84 | const std::vector<uint8_t>& value, |
| 85 | int offset, |
rkc | a65ced97 | 2016-04-28 06:56:38 | [diff] [blame] | 86 | const base::Closure& callback, |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 87 | const ErrorCallback& error_callback) = 0; |
| 88 | |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 89 | // Called when a remote device |device| requests to read the value of the |
| 90 | // descriptor |descriptor| starting at offset |offset|. |
| 91 | // This method is only called if the descriptor was specified as |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 92 | // readable and any authentication and authorization challenges were |
| 93 | // satisfied by the remote device. |
| 94 | // |
| 95 | // To respond to the request with success and return the requested value, |
| 96 | // the delegate must invoke |callback| with the value. Doing so will |
| 97 | // automatically update the value property of |descriptor|. To respond |
| 98 | // to the request with failure (e.g. if an invalid offset was given), |
| 99 | // delegates must invoke |error_callback|. If neither callback parameter is |
| 100 | // invoked, the request will time out and result in an error. Therefore, |
| 101 | // delegates MUST invoke either |callback| or |error_callback|. |
| 102 | virtual void OnDescriptorReadRequest( |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 103 | const BluetoothDevice* device, |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 104 | const BluetoothLocalGattDescriptor* descriptor, |
| 105 | int offset, |
| 106 | const ValueCallback& callback, |
| 107 | const ErrorCallback& error_callback) = 0; |
| 108 | |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 109 | // Called when a remote device |devie| requests to write the value of the |
| 110 | // descriptor |descriptor| starting at offset |offset|. |
| 111 | // This method is only called if the descriptor was specified as |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 112 | // writable and any authentication and authorization challenges were |
| 113 | // satisfied by the remote device. |
| 114 | // |
| 115 | // To respond to the request with success the delegate must invoke |
rkc | a65ced97 | 2016-04-28 06:56:38 | [diff] [blame] | 116 | // |callback|. To respond to the request with failure delegates must invoke |
| 117 | // |error_callback|. If neither callback parameter is invoked, the request |
| 118 | // will time out and result in an error. Therefore, delegates MUST invoke |
| 119 | // either |callback| or |error_callback|. |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 120 | virtual void OnDescriptorWriteRequest( |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 121 | const BluetoothDevice* device, |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 122 | const BluetoothLocalGattDescriptor* descriptor, |
| 123 | const std::vector<uint8_t>& value, |
| 124 | int offset, |
rkc | a65ced97 | 2016-04-28 06:56:38 | [diff] [blame] | 125 | const base::Closure& callback, |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 126 | const ErrorCallback& error_callback) = 0; |
rkc | d1e6dc4 | 2016-05-12 23:12:47 | [diff] [blame] | 127 | |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 128 | // Called when a remote device |device| requests notifications to start for |
rkc | d1e6dc4 | 2016-05-12 23:12:47 | [diff] [blame] | 129 | // |characteristic|. This is only called if the characteristic has |
| 130 | // specified the notify or indicate property. |
| 131 | virtual void OnNotificationsStart( |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 132 | const BluetoothDevice* device, |
rkc | d1e6dc4 | 2016-05-12 23:12:47 | [diff] [blame] | 133 | const BluetoothLocalGattCharacteristic* characteristic) = 0; |
| 134 | |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 135 | // Called when a remote device |device| requests notifications to stop for |
rkc | d1e6dc4 | 2016-05-12 23:12:47 | [diff] [blame] | 136 | // |characteristic|. This is only called if the characteristic has |
| 137 | // specified the notify or indicate property. |
| 138 | virtual void OnNotificationsStop( |
rkc | d4ba1a5 | 2016-05-17 22:42:34 | [diff] [blame] | 139 | const BluetoothDevice* device, |
rkc | d1e6dc4 | 2016-05-12 23:12:47 | [diff] [blame] | 140 | const BluetoothLocalGattCharacteristic* characteristic) = 0; |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 141 | }; |
| 142 | |
rkc | f27a499 | 2016-04-21 18:22:13 | [diff] [blame] | 143 | // Creates a local GATT service to be used with |adapter| (which will own |
| 144 | // the created service object). A service can register or unregister itself |
| 145 | // at any time by calling its Register/Unregister methods. |delegate| |
| 146 | // receives read/write requests for characteristic/descriptor values. It |
| 147 | // needs to outlive this object. |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 148 | // TODO(rkc): Implement included services. |
rkc | f27a499 | 2016-04-21 18:22:13 | [diff] [blame] | 149 | static base::WeakPtr<BluetoothLocalGattService> Create( |
| 150 | BluetoothAdapter* adapter, |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 151 | const BluetoothUUID& uuid, |
| 152 | bool is_primary, |
| 153 | BluetoothLocalGattService* included_service, |
rkc | f27a499 | 2016-04-21 18:22:13 | [diff] [blame] | 154 | BluetoothLocalGattService::Delegate* delegate); |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 155 | |
| 156 | // Registers this GATT service. Calling Register will make this service and |
| 157 | // all of its associated attributes available on the local adapters GATT |
rkc | f27a499 | 2016-04-21 18:22:13 | [diff] [blame] | 158 | // database. Call Unregister to make this service no longer available. |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 159 | virtual void Register(const base::Closure& callback, |
| 160 | const ErrorCallback& error_callback) = 0; |
rkc | f27a499 | 2016-04-21 18:22:13 | [diff] [blame] | 161 | |
| 162 | // Unregisters this GATT service. This will remove the service from the list |
| 163 | // of services exposed by the adapter this service was registered on. |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 164 | virtual void Unregister(const base::Closure& callback, |
| 165 | const ErrorCallback& error_callback) = 0; |
| 166 | |
rkc | 5ca6c3f | 2016-05-10 03:50:57 | [diff] [blame] | 167 | // Returns if this service is currently registered. |
| 168 | virtual bool IsRegistered() = 0; |
| 169 | |
| 170 | // Deletes this service, invaliding the weak pointer returned by create and |
| 171 | // unregistering the service if it was registered. |
| 172 | virtual void Delete() = 0; |
| 173 | |
rkc | 5916b89a | 2016-05-04 07:42:09 | [diff] [blame] | 174 | virtual BluetoothLocalGattCharacteristic* GetCharacteristic( |
| 175 | const std::string& identifier) = 0; |
| 176 | |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 177 | protected: |
| 178 | BluetoothLocalGattService(); |
rkc | f27a499 | 2016-04-21 18:22:13 | [diff] [blame] | 179 | ~BluetoothLocalGattService() override; |
rkc | 12223975 | 2016-04-20 23:59:08 | [diff] [blame] | 180 | |
| 181 | private: |
| 182 | DISALLOW_COPY_AND_ASSIGN(BluetoothLocalGattService); |
| 183 | }; |
| 184 | |
| 185 | } // namespace device |
| 186 | |
| 187 | #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |