OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_ | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CONNECTION_H_ |
6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_ | 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CONNECTION_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <queue> | 12 #include <queue> |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" | 19 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" |
20 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_genera tor.h" | |
21 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiv er.h" | |
20 #include "components/proximity_auth/ble/fake_wire_message.h" | 22 #include "components/proximity_auth/ble/fake_wire_message.h" |
21 #include "components/proximity_auth/ble/remote_attribute.h" | 23 #include "components/proximity_auth/ble/remote_attribute.h" |
22 #include "components/proximity_auth/connection.h" | 24 #include "components/proximity_auth/connection.h" |
23 #include "device/bluetooth/bluetooth_adapter.h" | 25 #include "device/bluetooth/bluetooth_adapter.h" |
24 #include "device/bluetooth/bluetooth_device.h" | 26 #include "device/bluetooth/bluetooth_device.h" |
25 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 27 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 28 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
27 #include "device/bluetooth/bluetooth_uuid.h" | 29 #include "device/bluetooth/bluetooth_uuid.h" |
28 | 30 |
29 namespace base { | 31 namespace base { |
30 class TaskRunner; | 32 class TaskRunner; |
31 } | 33 } |
32 | 34 |
33 namespace proximity_auth { | 35 namespace proximity_auth { |
34 | 36 |
35 class BluetoothThrottler; | 37 class BluetoothThrottler; |
36 | 38 |
37 // Represents a connection with a remote device over Bluetooth low energy. The | 39 class BluetoothLowEnergyWeaveConnection |
Kyle Horimoto
2016/06/24 00:14:09
Please add an in-depth description to this class.
jingxuy
2016/06/25 00:23:27
Done.
| |
38 // connection is a persistent bidirectional channel for sending and receiving | 40 : public Connection, |
39 // wire messages. The remote device is the peripheral mode and the service | 41 public device::BluetoothAdapter::Observer { |
40 // contains two characteristics: one to send data and another to receive it. | |
41 // | |
42 // The connection flow is described below. | |
43 // | |
44 // Discover Reader and Writer Characteristics | |
45 // | | |
46 // | | |
47 // | | |
48 // Start Notify Session | |
49 // | | |
50 // | | |
51 // | | |
52 // Write kInviteToConnectSignal to Writer Characteristic | |
53 // | | |
54 // | | |
55 // | | |
56 // Read kInvitationResponseSignal from Reader Characteristic | |
57 // | | |
58 // | | |
59 // | | |
60 // Proximity Auth Connection Established | |
61 class BluetoothLowEnergyConnection : public Connection, | |
62 public device::BluetoothAdapter::Observer { | |
63 public: | 42 public: |
64 // Signals sent to the remote device to indicate connection related events. | 43 // Signals sent to the remote device to indicate connection related events. |
65 enum class ControlSignal : uint32_t { | 44 enum class ControlSignal : uint32_t { |
66 kInviteToConnectSignal = 0, | 45 kInviteToConnectSignal = 0, |
67 kInvitationResponseSignal = 1, | 46 kInvitationResponseSignal = 1, |
68 kSendSignal = 2, | 47 kSendSignal = 2, |
69 kDisconnectSignal = 3, | 48 kDisconnectSignal = 3, |
70 }; | 49 }; |
71 | 50 |
72 // The sub-state of a proximity_auth::BluetoothLowEnergyConnection class | 51 // The sub-state of a proximity_auth::BluetoothLowEnergyWeaveConnection class |
73 // extends the IN_PROGRESS state of proximity_auth::Connection::Status. | 52 // extends the IN_PROGRESS state of proximity_auth::Connection::Status. |
74 enum class SubStatus { | 53 enum SubStatus { |
75 DISCONNECTED, | 54 DISCONNECTED, |
76 WAITING_GATT_CONNECTION, | 55 WAITING_GATT_CONNECTION, |
77 WAITING_CHARACTERISTICS, | 56 WAITING_CHARACTERISTICS, |
78 CHARACTERISTICS_FOUND, | 57 CHARACTERISTICS_FOUND, |
79 WAITING_NOTIFY_SESSION, | 58 WAITING_NOTIFY_SESSION, |
80 NOTIFY_SESSION_READY, | 59 NOTIFY_SESSION_READY, |
81 WAITING_RESPONSE_SIGNAL, | 60 WAITING_RESPONSE_SIGNAL, |
82 CONNECTED, | 61 CONNECTED, |
83 }; | 62 }; |
84 | 63 |
85 // Constructs a Bluetooth low energy connection to the service with | 64 // Constructs a Bluetooth low energy connection to the service with |
86 // |remote_service_| on the |remote_device|. The |adapter| must be already | 65 // |remote_service_| on the |remote_device|. The |adapter| must be already |
87 // initaalized and ready. The GATT connection may alreaady be established and | 66 // initaalized and ready. The GATT connection may alreaady be established and |
88 // pass through |gatt_connection|. A subsequent call to Connect() must be | 67 // pass through |gatt_connection|. A subsequent call to Connect() must be |
89 // made. | 68 // made. |
90 BluetoothLowEnergyConnection( | 69 BluetoothLowEnergyWeaveConnection( |
91 const RemoteDevice& remote_device, | 70 const RemoteDevice& remote_device, |
92 scoped_refptr<device::BluetoothAdapter> adapter, | 71 scoped_refptr<device::BluetoothAdapter> adapter, |
93 const device::BluetoothUUID remote_service_uuid, | 72 const device::BluetoothUUID remote_service_uuid, |
94 BluetoothThrottler* bluetooth_throttler, | 73 BluetoothThrottler* bluetooth_throttler, |
95 int max_number_of_write_attempts); | 74 int max_number_of_write_attempts); |
96 | 75 |
97 ~BluetoothLowEnergyConnection() override; | 76 ~BluetoothLowEnergyWeaveConnection() override; |
98 | 77 |
99 // proximity_auth::Connection: | 78 // proximity_auth::Connection: |
100 void Connect() override; | 79 void Connect() override; |
101 void Disconnect() override; | 80 void Disconnect() override; |
102 std::string GetDeviceAddress() override; | 81 std::string GetDeviceAddress() override; |
103 | 82 |
104 protected: | 83 protected: |
105 // Exposed for testing. | 84 // Exposed for testing. |
106 void SetSubStatus(SubStatus status); | 85 void SetSubStatus(SubStatus status); |
107 SubStatus sub_status() { return sub_status_; } | 86 SubStatus sub_status() { return sub_status_; } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 void CreateGattConnection(); | 129 void CreateGattConnection(); |
151 | 130 |
152 // Called when a GATT connection is created. | 131 // Called when a GATT connection is created. |
153 void OnGattConnectionCreated( | 132 void OnGattConnectionCreated( |
154 std::unique_ptr<device::BluetoothGattConnection> gatt_connection); | 133 std::unique_ptr<device::BluetoothGattConnection> gatt_connection); |
155 | 134 |
156 // Callback called when there is an error creating the GATT connection. | 135 // Callback called when there is an error creating the GATT connection. |
157 void OnCreateGattConnectionError( | 136 void OnCreateGattConnectionError( |
158 device::BluetoothDevice::ConnectErrorCode error_code); | 137 device::BluetoothDevice::ConnectErrorCode error_code); |
159 | 138 |
160 // Callback called when |to_peripheral_char_| and |from_peripheral_char_| were | 139 // Callback called when |tx_characteristic_| and |rx_characteristic_| were |
161 // found. | 140 // found. |
162 void OnCharacteristicsFound(const RemoteAttribute& service, | 141 void OnCharacteristicsFound(const RemoteAttribute& service, |
163 const RemoteAttribute& to_peripheral_char, | 142 const RemoteAttribute& to_peripheral_char, |
164 const RemoteAttribute& from_peripheral_char); | 143 const RemoteAttribute& from_peripheral_char); |
165 | 144 |
166 // Callback called there was an error finding the characteristics. | 145 // Callback called there was an error finding the characteristics. |
167 void OnCharacteristicsFinderError( | 146 void OnCharacteristicsFinderError( |
168 const RemoteAttribute& to_peripheral_char, | 147 const RemoteAttribute& to_peripheral_char, |
169 const RemoteAttribute& from_peripheral_char); | 148 const RemoteAttribute& from_peripheral_char); |
170 | 149 |
171 // Starts a notify session for |from_peripheral_char_| when ready | 150 // Starts a notify session for |rx_characteristic_| when ready |
172 // (SubStatus::CHARACTERISTICS_FOUND). | 151 // (SubStatus::CHARACTERISTICS_FOUND). |
173 void StartNotifySession(); | 152 void StartNotifySession(); |
174 | 153 |
175 // Called when a notification session is successfully started for | 154 // Called when a notification session is successfully started for |
176 // |from_peripheral_char_| characteristic. | 155 // |rx_characteristic_| characteristic. |
177 void OnNotifySessionStarted( | 156 void OnNotifySessionStarted( |
178 std::unique_ptr<device::BluetoothGattNotifySession> notify_session); | 157 std::unique_ptr<device::BluetoothGattNotifySession> notify_session); |
179 | 158 |
180 // Called when there is an error starting a notification session for | 159 // Called when there is an error starting a notification session for |
181 // |from_peripheral_char_| characteristic. | 160 // |rx_characteristic_| characteristic. |
182 void OnNotifySessionError(device::BluetoothGattService::GattErrorCode); | 161 void OnNotifySessionError(device::BluetoothGattService::GattErrorCode); |
183 | 162 |
184 // Stops |notify_session_|. | 163 // Stops |notify_session_|. |
185 void StopNotifySession(); | 164 void StopNotifySession(); |
186 | 165 |
187 // Sends an invite to connect signal to the peripheral if when ready | 166 // Sends an invite to connect signal to the peripheral if when ready |
188 // (SubStatus::NOTIFY_SESSION_READY). | 167 // (SubStatus::NOTIFY_SESSION_READY). |
189 void SendInviteToConnectSignal(); | 168 void SendInviteToConnectSignal(); |
190 | 169 |
191 // Completes and updates the status accordingly. | 170 // Completes and updates the status accordingly. |
192 void CompleteConnection(); | 171 void CompleteConnection(); |
193 | 172 |
194 // This is the only entry point for WriteRequests, which are processed | 173 // This is the only entry point for WriteRequests, which are processed |
195 // accordingly the following flow: | 174 // accordingly the following flow: |
196 // 1) |request| is enqueued; | 175 // 1) |request| is enqueued; |
197 // 2) |request| will be processed by ProcessNextWriteRequest() when there is | 176 // 2) |request| will be processed by ProcessNextWriteRequest() when there is |
198 // no pending write request; | 177 // no pending write request; |
199 // 3) |request| will be dequeued when it's successfully processed | 178 // 3) |request| will be dequeued when it's successfully processed |
200 // (OnRemoteCharacteristicWritten()); | 179 // (OnRemoteCharacteristicWritten()); |
201 // 4) |request| is not dequeued if it fails | 180 // 4) |request| is not dequeued if it fails |
202 // (OnWriteRemoteCharacteristicError()), it remains on the queue and will be | 181 // (OnWriteRemoteCharacteristicError()), it remains on the queue and will be |
203 // retried. |request| will remain on the queue until it succeeds or it | 182 // retried. |request| will remain on the queue until it succeeds or it |
204 // triggers a Disconnect() call (after |max_number_of_tries_|). | 183 // triggers a Disconnect() call (after |max_number_of_tries_|). |
205 void WriteRemoteCharacteristic(WriteRequest request); | 184 void WriteRemoteCharacteristic(const WriteRequest& request); |
206 | 185 |
207 // Processes the next request in |write_requests_queue_|. | 186 // Processes the next request in |write_requests_queue_|. |
208 void ProcessNextWriteRequest(); | 187 void ProcessNextWriteRequest(); |
209 | 188 |
210 // Called when the | 189 // Called when the |
211 // BluetoothRemoteGattCharacteristic::RemoteCharacteristicWrite() is | 190 // BluetoothRemoteGattCharacteristic::RemoteCharacteristicWrite() is |
212 // successfully complete. | 191 // successfully complete. |
213 void OnRemoteCharacteristicWritten(bool run_did_send_message_callback); | 192 void OnRemoteCharacteristicWritten(bool run_did_send_message_callback); |
214 | 193 |
215 // Called when there is an error writing to the remote characteristic | 194 // Called when there is an error writing to the remote characteristic |
216 // |to_peripheral_char_|. | 195 // |tx_characteristic_|. |
217 void OnWriteRemoteCharacteristicError( | 196 void OnWriteRemoteCharacteristicError( |
218 bool run_did_send_message_callback, | 197 bool run_did_send_message_callback, |
219 device::BluetoothRemoteGattService::GattErrorCode error); | 198 device::BluetoothRemoteGattService::GattErrorCode error); |
220 | 199 |
221 // Builds the value to be written on |to_peripheral_char_|. The value | |
222 // corresponds to |signal| concatenated with |payload|. | |
223 WriteRequest BuildWriteRequest(const std::vector<uint8_t>& signal, | |
224 const std::vector<uint8_t>& bytes, | |
225 bool is_last_message_for_wire_message); | |
226 | |
227 // Prints the time elapsed since |Connect()| was called. | 200 // Prints the time elapsed since |Connect()| was called. |
228 void PrintTimeElapsed(); | 201 void PrintTimeElapsed(); |
229 | 202 |
230 // Returns the device corresponding to |remote_device_address_|. | 203 // Returns the device corresponding to |remote_device_address_|. |
231 device::BluetoothDevice* GetRemoteDevice(); | 204 device::BluetoothDevice* GetRemoteDevice(); |
232 | 205 |
233 // Returns the service corresponding to |remote_service_| in the current | 206 // Returns the service corresponding to |remote_service_| in the current |
234 // device. | 207 // device. |
235 device::BluetoothRemoteGattService* GetRemoteService(); | 208 device::BluetoothRemoteGattService* GetRemoteService(); |
236 | 209 |
237 // Returns the characteristic corresponding to |identifier| in the current | 210 // Returns the characteristic corresponding to |identifier| in the current |
238 // service. | 211 // service. |
239 device::BluetoothRemoteGattCharacteristic* GetGattCharacteristic( | 212 device::BluetoothRemoteGattCharacteristic* GetGattCharacteristic( |
240 const std::string& identifier); | 213 const std::string& identifier); |
241 | 214 |
242 // Convert the first 4 bytes from a byte vector to a uint32_t. | 215 // Convert the first 4 bytes from a byte vector to a uint32_t. |
243 uint32_t ToUint32(const std::vector<uint8_t>& bytes); | 216 uint32_t ToUint32(const std::vector<uint8_t>& bytes); |
244 | 217 |
245 // Convert an uint32_t to a byte vector. | 218 // Convert an uint32_t to a byte vector. |
246 const std::vector<uint8_t> ToByteVector(uint32_t value); | 219 const std::vector<uint8_t> ToByteVector(uint32_t value); |
247 | 220 |
248 // The Bluetooth adapter over which the Bluetooth connection will be made. | 221 // The Bluetooth adapter over which the Bluetooth connection will be made. |
249 scoped_refptr<device::BluetoothAdapter> adapter_; | 222 scoped_refptr<device::BluetoothAdapter> adapter_; |
250 | 223 |
251 // Remote service the |gatt_connection_| was established with. | 224 // Remote service the |gatt_connection_| was established with. |
252 RemoteAttribute remote_service_; | 225 RemoteAttribute remote_service_; |
253 | 226 |
227 // uWeave packet generator. | |
228 std::unique_ptr<BluetoothLowEnergyWeavePacketGenerator> packet_generator_; | |
229 | |
230 // uWeave packet receiver. | |
231 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> packet_receiver_; | |
232 | |
254 // Characteristic used to send data to the remote device. | 233 // Characteristic used to send data to the remote device. |
255 RemoteAttribute to_peripheral_char_; | 234 RemoteAttribute tx_characteristic_; |
256 | 235 |
257 // Characteristic used to receive data from the remote device. | 236 // Characteristic used to receive data from the remote device. |
258 RemoteAttribute from_peripheral_char_; | 237 RemoteAttribute rx_characteristic_; |
259 | 238 |
260 // Throttles repeated connection attempts to the same device. This is a | 239 // Throttles repeated connection attempts to the same device. This is a |
261 // workaround for crbug.com/508919. Not owned, must outlive this instance. | 240 // workaround for crbug.com/508919. Not owned, must outlive this instance. |
262 BluetoothThrottler* bluetooth_throttler_; | 241 BluetoothThrottler* bluetooth_throttler_; |
263 | 242 |
264 scoped_refptr<base::TaskRunner> task_runner_; | 243 scoped_refptr<base::TaskRunner> task_runner_; |
265 | 244 |
266 // The GATT connection with the remote device. | 245 // The GATT connection with the remote device. |
267 std::unique_ptr<device::BluetoothGattConnection> gatt_connection_; | 246 std::unique_ptr<device::BluetoothGattConnection> gatt_connection_; |
268 | 247 |
269 // The characteristics finder for remote device. | 248 // The characteristics finder for remote device. |
270 std::unique_ptr<BluetoothLowEnergyCharacteristicsFinder> | 249 std::unique_ptr<BluetoothLowEnergyCharacteristicsFinder> |
271 characteristic_finder_; | 250 characteristic_finder_; |
272 | 251 |
273 // The notify session for |from_peripheral_char|. | 252 // The notify session for |from_peripheral_char|. |
274 std::unique_ptr<device::BluetoothGattNotifySession> notify_session_; | 253 std::unique_ptr<device::BluetoothGattNotifySession> notify_session_; |
275 | 254 |
276 // Internal connection status | 255 // Internal connection status |
277 SubStatus sub_status_; | 256 SubStatus sub_status_; |
278 | 257 |
279 // Indicates a receiving operation is in progress. This is set after a | |
280 // ControlSignal::kSendSignal was received from the remote device. | |
281 bool receiving_bytes_; | |
282 | |
283 // Total number of bytes expected for the current receive operation. | |
284 std::size_t expected_number_of_incoming_bytes_; | |
285 | |
286 // Bytes already received for the current receive operation. | 258 // Bytes already received for the current receive operation. |
287 std::string incoming_bytes_buffer_; | 259 std::string incoming_bytes_buffer_; |
288 | 260 |
289 // Indicates there is a | 261 // Indicates there is a |
290 // BluetoothRemoteGattCharacteristic::WriteRemoteCharacteristic | 262 // BluetoothRemoteGattCharacteristic::WriteRemoteCharacteristic |
291 // operation pending. | 263 // operation pending. |
292 bool write_remote_characteristic_pending_; | 264 bool write_remote_characteristic_pending_; |
293 | 265 |
294 std::queue<WriteRequest> write_requests_queue_; | 266 std::queue<WriteRequest> write_requests_queue_; |
295 | 267 |
296 // Maximum number of tries to send any write request. | 268 // Maximum number of tries to send any write request. |
297 int max_number_of_write_attempts_; | 269 int max_number_of_write_attempts_; |
298 | 270 |
299 // Maximum number of bytes that fit in a single chunk to be written in | |
300 // |to_peripheral_char_|. Ideally, this should be the maximum value the | |
301 // peripheral supports and it should be agreed when the GATT connection is | |
302 // created. Currently, there is no API to find this value. The implementation | |
303 // uses a hard-coded constant. | |
304 int max_chunk_size_; | |
305 | |
306 // Stores when the instace was created. | 271 // Stores when the instace was created. |
307 base::TimeTicks start_time_; | 272 base::TimeTicks start_time_; |
308 | 273 |
309 base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_; | 274 base::WeakPtrFactory<BluetoothLowEnergyWeaveConnection> weak_ptr_factory_; |
310 | 275 |
311 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection); | 276 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyWeaveConnection); |
312 }; | 277 }; |
313 | 278 |
314 } // namespace proximity_auth | 279 } // namespace proximity_auth |
315 | 280 |
316 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_ | 281 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CONNECTION_H _ |
OLD | NEW |