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; | |
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. |
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. | 91 // |
92 // The code in SubscribeToNotifications writes to the Client Characteristic | |
93 // Configuration descriptor to enable notifications/indications. Core | |
94 // Bluetooth Specification [V4.2 Vol 3 Part G Section 3.3.1.1. Characteristic | |
95 // Properties] requires this descriptor to be present when | |
96 // notifications/indications are supported. If the descriptor is not present | |
97 // |error_callback| will be run. | |
98 // | |
99 // Writing a non-zero value to the remote characteristic's Client | |
100 // Characteristic Configuration descriptor, causes the remote characteristic | |
101 // to start sending us notifications whenever the characteristic's value | |
102 // 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.
| |
103 // oncharacteristicvaluechanged event handler in the | |
104 // BluetoothRemoteGATTCharacteristic object in javascript. | |
105 // | |
106 // To stop the flow of notifications, simply call the Stop method on the | |
107 // BluetoothGattNotifySession object that you received in |callback|. | |
85 virtual void StartNotifySession(const NotifySessionCallback& callback, | 108 virtual void StartNotifySession(const NotifySessionCallback& callback, |
86 const ErrorCallback& error_callback) = 0; | 109 const ErrorCallback& error_callback); |
87 | 110 |
88 // Sends a read request to a remote characteristic to read its value. | 111 // Sends a read request to a remote characteristic to read its value. |
89 // |callback| is called to return the read value on success and | 112 // |callback| is called to return the read value on success and |
90 // |error_callback| is called for failures. | 113 // |error_callback| is called for failures. |
91 virtual void ReadRemoteCharacteristic( | 114 virtual void ReadRemoteCharacteristic( |
92 const ValueCallback& callback, | 115 const ValueCallback& callback, |
93 const ErrorCallback& error_callback) = 0; | 116 const ErrorCallback& error_callback) = 0; |
94 | 117 |
95 // Sends a write request to a remote characteristic, to modify the | 118 // Sends a write request to a remote characteristic, to modify the |
96 // characteristic's value with the new value |new_value|. |callback| is | 119 // characteristic's value with the new value |new_value|. |callback| is |
97 // called to signal success and |error_callback| for failures. This method | 120 // called to signal success and |error_callback| for failures. This method |
98 // only applies to remote characteristics and will fail for those that are | 121 // only applies to remote characteristics and will fail for those that are |
99 // locally hosted. | 122 // locally hosted. |
100 virtual void WriteRemoteCharacteristic( | 123 virtual void WriteRemoteCharacteristic( |
101 const std::vector<uint8_t>& new_value, | 124 const std::vector<uint8_t>& new_value, |
102 const base::Closure& callback, | 125 const base::Closure& callback, |
103 const ErrorCallback& error_callback) = 0; | 126 const ErrorCallback& error_callback) = 0; |
104 | 127 |
105 protected: | 128 protected: |
106 BluetoothRemoteGattCharacteristic(); | 129 BluetoothRemoteGattCharacteristic(); |
107 ~BluetoothRemoteGattCharacteristic() override; | 130 ~BluetoothRemoteGattCharacteristic() override; |
108 | 131 |
132 // Writes to the Client Characteristic Configuration descriptor to enable | |
133 // notifications/indications. This method is meant to be called from | |
134 // StartNotifySession and should contain only the code necessary to start | |
135 // listening to characteristic notifications on a particular platform. | |
136 virtual void SubscribeToNotifications( | |
137 BluetoothRemoteGattDescriptor* ccc_descriptor, | |
138 const base::Closure& callback, | |
139 const ErrorCallback& error_callback) = 0; | |
140 | |
141 // Writes to the Client Characteristic Configuration descriptor to disable | |
142 // notifications/indications. This method is meant to be called from | |
143 // StopNotifySession and should contain only the code necessary to stop | |
144 // listening to characteristic notifications on a particular platform. | |
145 virtual void UnsubscribeFromNotifications( | |
146 BluetoothRemoteGattDescriptor* ccc_descriptor, | |
147 const base::Closure& callback, | |
148 const ErrorCallback& error_callback) = 0; | |
149 | |
109 private: | 150 private: |
151 friend class BluetoothGattNotifySession; | |
152 | |
153 class NotifySessionCommand { | |
154 public: | |
155 enum Type { COMMAND_START, COMMAND_STOP }; | |
156 | |
157 BluetoothRemoteGattCharacteristic* characteristic_; | |
158 Type type_; | |
159 | |
160 NotifySessionCommand(BluetoothRemoteGattCharacteristic* characteristic, | |
161 Type type); | |
162 virtual ~NotifySessionCommand(); | |
163 | |
164 virtual void Execute() = 0; | |
165 virtual void Cancel() = 0; | |
166 virtual void OnSuccess() = 0; | |
167 virtual void OnError(BluetoothRemoteGattService::GattErrorCode error) = 0; | |
168 }; | |
169 | |
170 class NotifySessionCommandStart : public NotifySessionCommand { | |
171 public: | |
172 NotifySessionCommandStart(BluetoothRemoteGattCharacteristic* characteristic, | |
173 const NotifySessionCallback& callback, | |
174 const ErrorCallback& error_callback); | |
175 ~NotifySessionCommandStart() override; | |
176 | |
177 NotifySessionCallback callback_; | |
178 ErrorCallback error_callback_; | |
179 | |
180 void Execute() override; | |
181 void Cancel() override; | |
182 void OnSuccess() override; | |
183 void OnError(BluetoothRemoteGattService::GattErrorCode error) override; | |
184 }; | |
185 | |
186 class NotifySessionCommandStop : public NotifySessionCommand { | |
187 public: | |
188 NotifySessionCommandStop(BluetoothRemoteGattCharacteristic* characteristic, | |
189 BluetoothGattNotifySession* session, | |
190 const base::Closure& callback); | |
191 ~NotifySessionCommandStop() override; | |
192 | |
193 BluetoothGattNotifySession* session_; | |
194 base::Closure callback_; | |
195 | |
196 void Execute() override; | |
197 void Cancel() override; | |
198 void OnSuccess() override; | |
199 void OnError(BluetoothRemoteGattService::GattErrorCode error) override; | |
200 }; | |
201 | |
202 // Stops an active notify session for the remote characteristic. On success, | |
203 // the characteristic removes this session from the list of active sessions. | |
204 // If there are no more active sessions, notifications/indications are | |
205 // turned off. | |
206 // | |
207 // This method is, and should only be, called from | |
208 // BluetoothGattNotifySession::Stop(). | |
209 // | |
210 // The code in UnsubscribeFromNotifications writes to the Client | |
211 // Characteristic Configuration descriptor to disable | |
212 // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G | |
213 // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be | |
214 // present when notifications/indications are supported. | |
215 virtual void StopNotifySession(BluetoothGattNotifySession* session, | |
216 const base::Closure& callback); | |
217 | |
218 // Pending StartNotifySession / StopNotifySession calls. | |
219 std::queue<std::unique_ptr<NotifySessionCommand>> pending_notify_commands_; | |
220 | |
221 // Set of active notify sessions. | |
222 std::set<BluetoothGattNotifySession*> notify_sessions_; | |
223 | |
224 base::WeakPtrFactory<BluetoothRemoteGattCharacteristic> weak_ptr_factory_; | |
225 | |
110 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristic); | 226 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristic); |
111 }; | 227 }; |
112 | 228 |
113 } // namespace device | 229 } // namespace device |
114 | 230 |
115 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ | 231 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_ |
OLD | NEW |