Chromium Code Reviews| Index: device/bluetooth/bluetooth_remote_gatt_characteristic.h |
| diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic.h b/device/bluetooth/bluetooth_remote_gatt_characteristic.h |
| index d8f9ff6dbdb8d0d9d0340d6bc37d60558e6fa8ab..ec1b70e051c4240753e386a3050ec70a903600ae 100644 |
| --- a/device/bluetooth/bluetooth_remote_gatt_characteristic.h |
| +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic.h |
| @@ -6,14 +6,19 @@ |
| #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ |
| #include <stdint.h> |
| + |
| +#include <queue> |
| +#include <set> |
| #include <string> |
| #include <vector> |
| #include "base/callback.h" |
| #include "base/callback_forward.h" |
| #include "base/macros.h" |
| +#include "base/memory/weak_ptr.h" |
| #include "device/bluetooth/bluetooth_export.h" |
| #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| +#include "device/bluetooth/bluetooth_remote_gatt_service.h" |
| #include "device/bluetooth/bluetooth_uuid.h" |
| namespace device { |
| @@ -52,10 +57,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic |
| // Returns a pointer to the GATT service this characteristic belongs to. |
| virtual BluetoothRemoteGattService* GetService() const = 0; |
| - // Returns whether or not this characteristic is currently sending value |
| - // updates in the form of a notification or indication. |
| - virtual bool IsNotifying() const = 0; |
| - |
| // Returns the list of GATT characteristic descriptors that provide more |
| // information about this characteristic. |
| virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() |
| @@ -70,20 +71,42 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic |
| // multiple, as illustrated by Core Bluetooth Specification [V4.2 Vol 3 Part G |
| // 3.3.3.5 Characteristic Presentation Format]. |
| std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID( |
| - const BluetoothUUID& uuid); |
| + const BluetoothUUID& uuid) const; |
| + |
| + base::WeakPtr<device::BluetoothRemoteGattCharacteristic> GetWeakPtr(); |
| + |
| + // Returns whether or not this characteristic is currently sending value |
| + // updates in the form of a notification or indication. |
| + virtual bool IsNotifying() const; |
| // Starts a notify session for the remote characteristic, if it supports |
| // notifications/indications. On success, the characteristic starts sending |
| // value notifications and |callback| is called with a session object whose |
| // ownership belongs to the caller. |error_callback| is called on errors. |
| // |
| - // Writes to the Client Characteristic Configuration descriptor to enable |
| - // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G |
| - // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be |
| - // present when notifications/indications are supported. If the descriptor is |
| - // not present |error_callback| will be run. |
| + // This method handles all logic regarding multiple sessions so that |
| + // specific platform implementations of the remote characteristic class |
| + // do not have to. Rather than overriding this method, it is recommended |
| + // to override the SubscribeToNotifications method below. |
| + // |
| + // The code in SubscribeToNotifications writes to the Client Characteristic |
| + // Configuration descriptor to enable notifications/indications. Core |
| + // Bluetooth Specification [V4.2 Vol 3 Part G Section 3.3.1.1. Characteristic |
| + // Properties] requires this descriptor to be present when |
| + // notifications/indications are supported. If the descriptor is not present |
| + // |error_callback| will be run. |
| + // |
| + // Writing a non-zero value to the remote characteristic's Client |
| + // Characteristic Configuration descriptor, causes the remote characteristic |
| + // to start sending us notifications whenever the characteristic's value |
| + // changes. These notifications are routed up from the platform into the |
|
ortuno
2016/08/02 01:56:32
This layer shouldn't know anything about javascrip
tommyt
2016/08/05 12:22:28
Done.
|
| + // oncharacteristicvaluechanged event handler in the |
| + // BluetoothRemoteGATTCharacteristic object in javascript. |
| + // |
| + // To stop the flow of notifications, simply call the Stop method on the |
| + // BluetoothGattNotifySession object that you received in |callback|. |
| virtual void StartNotifySession(const NotifySessionCallback& callback, |
| - const ErrorCallback& error_callback) = 0; |
| + const ErrorCallback& error_callback); |
| // Sends a read request to a remote characteristic to read its value. |
| // |callback| is called to return the read value on success and |
| @@ -106,7 +129,100 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic |
| BluetoothRemoteGattCharacteristic(); |
| ~BluetoothRemoteGattCharacteristic() override; |
| + // Writes to the Client Characteristic Configuration descriptor to enable |
| + // notifications/indications. This method is meant to be called from |
| + // StartNotifySession and should contain only the code necessary to start |
| + // listening to characteristic notifications on a particular platform. |
| + virtual void SubscribeToNotifications( |
| + BluetoothRemoteGattDescriptor* ccc_descriptor, |
| + const base::Closure& callback, |
| + const ErrorCallback& error_callback) = 0; |
| + |
| + // Writes to the Client Characteristic Configuration descriptor to disable |
| + // notifications/indications. This method is meant to be called from |
| + // StopNotifySession and should contain only the code necessary to stop |
| + // listening to characteristic notifications on a particular platform. |
| + virtual void UnsubscribeFromNotifications( |
| + BluetoothRemoteGattDescriptor* ccc_descriptor, |
| + const base::Closure& callback, |
| + const ErrorCallback& error_callback) = 0; |
| + |
| private: |
| + friend class BluetoothGattNotifySession; |
| + |
| + class NotifySessionCommand { |
| + public: |
| + enum Type { COMMAND_START, COMMAND_STOP }; |
| + |
| + BluetoothRemoteGattCharacteristic* characteristic_; |
| + Type type_; |
| + |
| + NotifySessionCommand(BluetoothRemoteGattCharacteristic* characteristic, |
| + Type type); |
| + virtual ~NotifySessionCommand(); |
| + |
| + virtual void Execute() = 0; |
| + virtual void Cancel() = 0; |
| + virtual void OnSuccess() = 0; |
| + virtual void OnError(BluetoothRemoteGattService::GattErrorCode error) = 0; |
| + }; |
| + |
| + class NotifySessionCommandStart : public NotifySessionCommand { |
| + public: |
| + NotifySessionCommandStart(BluetoothRemoteGattCharacteristic* characteristic, |
| + const NotifySessionCallback& callback, |
| + const ErrorCallback& error_callback); |
| + ~NotifySessionCommandStart() override; |
| + |
| + NotifySessionCallback callback_; |
| + ErrorCallback error_callback_; |
| + |
| + void Execute() override; |
| + void Cancel() override; |
| + void OnSuccess() override; |
| + void OnError(BluetoothRemoteGattService::GattErrorCode error) override; |
| + }; |
| + |
| + class NotifySessionCommandStop : public NotifySessionCommand { |
| + public: |
| + NotifySessionCommandStop(BluetoothRemoteGattCharacteristic* characteristic, |
| + BluetoothGattNotifySession* session, |
| + const base::Closure& callback); |
| + ~NotifySessionCommandStop() override; |
| + |
| + BluetoothGattNotifySession* session_; |
| + base::Closure callback_; |
| + |
| + void Execute() override; |
| + void Cancel() override; |
| + void OnSuccess() override; |
| + void OnError(BluetoothRemoteGattService::GattErrorCode error) override; |
| + }; |
| + |
| + // Stops an active notify session for the remote characteristic. On success, |
| + // the characteristic removes this session from the list of active sessions. |
| + // If there are no more active sessions, notifications/indications are |
| + // turned off. |
| + // |
| + // This method is, and should only be, called from |
| + // BluetoothGattNotifySession::Stop(). |
| + // |
| + // The code in UnsubscribeFromNotifications writes to the Client |
| + // Characteristic Configuration descriptor to disable |
| + // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G |
| + // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be |
| + // present when notifications/indications are supported. |
| + virtual void StopNotifySession(BluetoothGattNotifySession* session, |
| + const base::Closure& callback); |
| + |
| + // Pending StartNotifySession / StopNotifySession calls. |
| + std::queue<std::unique_ptr<NotifySessionCommand>> pending_notify_commands_; |
| + |
| + // Set of active notify sessions. |
| + std::set<BluetoothGattNotifySession*> notify_sessions_; |
| + |
| + base::WeakPtrFactory<BluetoothRemoteGattCharacteristic> weak_ptr_factory_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristic); |
| }; |