Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 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_REMOTE_GATT_CHARACTERISTIC_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_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 <queue> | |
| 11 #include <set> | |
| 9 #include <string> | 12 #include <string> |
| 10 #include <vector> | 13 #include <vector> |
| 11 | 14 |
| 12 #include "base/callback.h" | 15 #include "base/callback.h" |
| 13 #include "base/callback_forward.h" | 16 #include "base/callback_forward.h" |
| 14 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/weak_ptr.h" | |
| 15 #include "device/bluetooth/bluetooth_export.h" | 19 #include "device/bluetooth/bluetooth_export.h" |
| 16 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 20 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 21 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | |
| 17 #include "device/bluetooth/bluetooth_uuid.h" | 22 #include "device/bluetooth/bluetooth_uuid.h" |
| 18 | 23 |
| 19 namespace device { | 24 namespace device { |
| 20 | 25 |
| 21 class BluetoothGattNotifySession; | 26 class BluetoothGattNotifySession; |
| 22 class BluetoothRemoteGattDescriptor; | 27 class BluetoothRemoteGattDescriptor; |
| 23 class BluetoothRemoteGattService; | 28 class BluetoothRemoteGattService; |
| 24 | 29 |
| 25 // BluetoothRemoteGattCharacteristic represents a remote GATT characteristic. | 30 // BluetoothRemoteGattCharacteristic represents a remote GATT characteristic. |
| 26 // This class is used to represent GATT characteristics that belong to a service | 31 // This class is used to represent GATT characteristics that belong to a service |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 45 NotifySessionCallback; | 50 NotifySessionCallback; |
| 46 | 51 |
| 47 // Returns the value of the characteristic. For remote characteristics, this | 52 // Returns the value of the characteristic. For remote characteristics, this |
| 48 // is the most recently cached value. For local characteristics, this is the | 53 // is the most recently cached value. For local characteristics, this is the |
| 49 // most recently updated value or the value retrieved from the delegate. | 54 // most recently updated value or the value retrieved from the delegate. |
| 50 virtual const std::vector<uint8_t>& GetValue() const = 0; | 55 virtual const std::vector<uint8_t>& GetValue() const = 0; |
| 51 | 56 |
| 52 // Returns a pointer to the GATT service this characteristic belongs to. | 57 // Returns a pointer to the GATT service this characteristic belongs to. |
| 53 virtual BluetoothRemoteGattService* GetService() const = 0; | 58 virtual BluetoothRemoteGattService* GetService() const = 0; |
| 54 | 59 |
| 55 // Returns whether or not this characteristic is currently sending value | |
| 56 // updates in the form of a notification or indication. | |
| 57 virtual bool IsNotifying() const = 0; | |
| 58 | |
| 59 // Returns the list of GATT characteristic descriptors that provide more | 60 // Returns the list of GATT characteristic descriptors that provide more |
| 60 // information about this characteristic. | 61 // information about this characteristic. |
| 61 virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() | 62 virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() |
| 62 const = 0; | 63 const = 0; |
| 63 | 64 |
| 64 // Returns the GATT characteristic descriptor with identifier |identifier| if | 65 // Returns the GATT characteristic descriptor with identifier |identifier| if |
| 65 // it belongs to this GATT characteristic. | 66 // it belongs to this GATT characteristic. |
| 66 virtual BluetoothRemoteGattDescriptor* GetDescriptor( | 67 virtual BluetoothRemoteGattDescriptor* GetDescriptor( |
| 67 const std::string& identifier) const = 0; | 68 const std::string& identifier) const = 0; |
| 68 | 69 |
| 69 // Returns the GATT characteristic descriptors that match |uuid|. There may be | 70 // Returns the GATT characteristic descriptors that match |uuid|. There may be |
| 70 // multiple, as illustrated by Core Bluetooth Specification [V4.2 Vol 3 Part G | 71 // multiple, as illustrated by Core Bluetooth Specification [V4.2 Vol 3 Part G |
| 71 // 3.3.3.5 Characteristic Presentation Format]. | 72 // 3.3.3.5 Characteristic Presentation Format]. |
| 72 std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID( | 73 std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID( |
| 73 const BluetoothUUID& uuid); | 74 const BluetoothUUID& uuid) const; |
| 75 | |
| 76 base::WeakPtr<device::BluetoothRemoteGattCharacteristic> GetWeakPtr(); | |
| 77 | |
| 78 // Returns whether or not this characteristic is currently sending value | |
| 79 // updates in the form of a notification or indication. | |
| 80 virtual bool IsNotifying() const; | |
|
ortuno
2016/07/28 21:59:30
hmm this should really be private. I can't think o
tommyt
2016/08/01 11:39:21
Even though there are no good uses of this functio
ortuno
2016/08/02 01:56:31
I think this makes the API a bit confusing and doe
tommyt
2016/08/08 13:55:08
I've updated the documentation.
| |
| 74 | 81 |
| 75 // Starts a notify session for the remote characteristic, if it supports | 82 // Starts a notify session for the remote characteristic, if it supports |
| 76 // notifications/indications. On success, the characteristic starts sending | 83 // notifications/indications. On success, the characteristic starts sending |
| 77 // value notifications and |callback| is called with a session object whose | 84 // value notifications and |callback| is called with a session object whose |
| 78 // ownership belongs to the caller. |error_callback| is called on errors. | 85 // ownership belongs to the caller. |error_callback| is called on errors. |
|
ortuno
2016/07/28 21:59:30
I think we should document this more. Please expla
tommyt
2016/08/01 11:39:21
Done.
| |
| 79 // | 86 // |
| 80 // Writes to the Client Characteristic Configuration descriptor to enable | 87 // This method handles all logic regarding multiple sessions so that |
| 81 // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G | 88 // specific platform implementations of the remote characteristic class |
| 82 // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be | 89 // do not have to. Rather than overriding this method, it is recommended |
| 83 // present when notifications/indications are supported. If the descriptor is | 90 // to override the SubscribeToNotifications method below. |
| 84 // not present |error_callback| will be run. | |
| 85 virtual void StartNotifySession(const NotifySessionCallback& callback, | 91 virtual void StartNotifySession(const NotifySessionCallback& callback, |
| 86 const ErrorCallback& error_callback) = 0; | 92 const ErrorCallback& error_callback); |
| 93 | |
| 94 // Stops an active notify session for the remote characteristic. On success, | |
| 95 // the characteristic removes this session from the list of active sessions. | |
| 96 // If there are no more active sessions, notifications/indications are | |
| 97 // turned off. | |
| 98 // | |
| 99 // It is recommended for platform implementations of the remote charactiristic | |
| 100 // class to override UnsubscribeFromNotifications below, instead of overriding | |
| 101 // this method. | |
| 102 virtual void StopNotifySession(BluetoothGattNotifySession* session, | |
|
ortuno
2016/07/28 21:59:30
I don't think we want to expose another way of sto
tommyt
2016/08/01 11:39:21
Done.
| |
| 103 const base::Closure& callback); | |
| 87 | 104 |
| 88 // Sends a read request to a remote characteristic to read its value. | 105 // Sends a read request to a remote characteristic to read its value. |
| 89 // |callback| is called to return the read value on success and | 106 // |callback| is called to return the read value on success and |
| 90 // |error_callback| is called for failures. | 107 // |error_callback| is called for failures. |
| 91 virtual void ReadRemoteCharacteristic( | 108 virtual void ReadRemoteCharacteristic( |
| 92 const ValueCallback& callback, | 109 const ValueCallback& callback, |
| 93 const ErrorCallback& error_callback) = 0; | 110 const ErrorCallback& error_callback) = 0; |
| 94 | 111 |
| 95 // Sends a write request to a remote characteristic, to modify the | 112 // Sends a write request to a remote characteristic, to modify the |
| 96 // characteristic's value with the new value |new_value|. |callback| is | 113 // characteristic's value with the new value |new_value|. |callback| is |
| 97 // called to signal success and |error_callback| for failures. This method | 114 // called to signal success and |error_callback| for failures. This method |
| 98 // only applies to remote characteristics and will fail for those that are | 115 // only applies to remote characteristics and will fail for those that are |
| 99 // locally hosted. | 116 // locally hosted. |
| 100 virtual void WriteRemoteCharacteristic( | 117 virtual void WriteRemoteCharacteristic( |
| 101 const std::vector<uint8_t>& new_value, | 118 const std::vector<uint8_t>& new_value, |
| 102 const base::Closure& callback, | 119 const base::Closure& callback, |
| 103 const ErrorCallback& error_callback) = 0; | 120 const ErrorCallback& error_callback) = 0; |
| 104 | 121 |
| 105 protected: | 122 protected: |
| 106 BluetoothRemoteGattCharacteristic(); | 123 BluetoothRemoteGattCharacteristic(); |
| 107 ~BluetoothRemoteGattCharacteristic() override; | 124 ~BluetoothRemoteGattCharacteristic() override; |
| 108 | 125 |
| 126 // Writes to the Client Characteristic Configuration descriptor to enable | |
|
ortuno
2016/07/28 21:59:30
Also could you explain the relationship between th
tommyt
2016/08/01 11:39:21
Done.
| |
| 127 // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G | |
| 128 // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be | |
| 129 // present when notifications/indications are supported. If the descriptor is | |
| 130 // not present |error_callback| will be run. | |
|
ortuno
2016/07/28 21:59:30
We probably want this behavior, failing on no desc
tommyt
2016/08/01 11:39:21
I have moved this behavior to BluetoothRemoteGattC
| |
| 131 virtual void SubscribeToNotifications( | |
| 132 const base::Closure& callback, | |
| 133 const ErrorCallback& error_callback) = 0; | |
| 134 | |
| 135 // Writes to the Client Characteristic Configuration descriptor to disable | |
| 136 // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G | |
| 137 // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be | |
| 138 // present when notifications/indications are supported. If the descriptor is | |
| 139 // not present |error_callback| will be run. | |
| 140 virtual void UnsubscribeFromNotifications( | |
| 141 const base::Closure& callback, | |
| 142 const ErrorCallback& error_callback) = 0; | |
| 143 | |
| 109 private: | 144 private: |
| 145 class NotifySessionCommand { | |
| 146 public: | |
| 147 enum Type { COMMAND_START, COMMAND_STOP }; | |
| 148 | |
| 149 BluetoothRemoteGattCharacteristic* characteristic_; | |
| 150 Type type_; | |
| 151 | |
| 152 NotifySessionCommand(BluetoothRemoteGattCharacteristic* characteristic, | |
| 153 Type type); | |
| 154 virtual ~NotifySessionCommand(); | |
| 155 | |
| 156 virtual void Execute() = 0; | |
| 157 virtual void Cancel() = 0; | |
| 158 virtual void OnSuccess() = 0; | |
| 159 virtual void OnError(BluetoothRemoteGattService::GattErrorCode error) = 0; | |
| 160 }; | |
| 161 | |
| 162 class NotifySessionCommandStart : public NotifySessionCommand { | |
| 163 public: | |
| 164 NotifySessionCommandStart(BluetoothRemoteGattCharacteristic* characteristic, | |
| 165 const NotifySessionCallback& callback, | |
| 166 const ErrorCallback& error_callback); | |
| 167 ~NotifySessionCommandStart() override; | |
| 168 | |
| 169 NotifySessionCallback callback_; | |
| 170 ErrorCallback error_callback_; | |
| 171 | |
| 172 void Execute() override; | |
| 173 void Cancel() override; | |
| 174 void OnSuccess() override; | |
| 175 void OnError(BluetoothRemoteGattService::GattErrorCode error) override; | |
| 176 }; | |
| 177 | |
| 178 class NotifySessionCommandStop : public NotifySessionCommand { | |
| 179 public: | |
| 180 NotifySessionCommandStop(BluetoothRemoteGattCharacteristic* characteristic, | |
| 181 BluetoothGattNotifySession* session, | |
| 182 const base::Closure& callback); | |
| 183 ~NotifySessionCommandStop() override; | |
| 184 | |
| 185 BluetoothGattNotifySession* session_; | |
| 186 base::Closure callback_; | |
| 187 | |
| 188 void Execute() override; | |
| 189 void Cancel() override; | |
| 190 void OnSuccess() override; | |
| 191 void OnError(BluetoothRemoteGattService::GattErrorCode error) override; | |
| 192 }; | |
| 193 | |
| 194 // Pending StartNotifySession / StopNotifySession calls. | |
| 195 std::queue<std::unique_ptr<NotifySessionCommand>> pending_notify_commands_; | |
| 196 | |
| 197 // Set of active notify sessions. | |
| 198 std::set<BluetoothGattNotifySession*> notify_sessions_; | |
| 199 | |
| 200 base::WeakPtrFactory<BluetoothRemoteGattCharacteristic> weak_ptr_factory_; | |
| 201 | |
| 110 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristic); | 202 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristic); |
| 111 }; | 203 }; |
| 112 | 204 |
| 113 } // namespace device | 205 } // namespace device |
| 114 | 206 |
| 115 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ | 207 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ |
| OLD | NEW |