| 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_CONNECTION_H_ |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_ | 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <queue> | 11 #include <queue> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.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/fake_wire_message.h" | 20 #include "components/proximity_auth/ble/fake_wire_message.h" |
| 21 #include "components/proximity_auth/ble/remote_attribute.h" | 21 #include "components/proximity_auth/ble/remote_attribute.h" |
| 22 #include "components/proximity_auth/connection.h" | 22 #include "components/proximity_auth/connection.h" |
| 23 #include "device/bluetooth/bluetooth_adapter.h" | 23 #include "device/bluetooth/bluetooth_adapter.h" |
| 24 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
| 25 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | |
| 26 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 25 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 27 #include "device/bluetooth/bluetooth_uuid.h" | 27 #include "device/bluetooth/bluetooth_uuid.h" |
| 28 | 28 |
| 29 namespace base { | 29 namespace base { |
| 30 class TaskRunner; | 30 class TaskRunner; |
| 31 } | 31 } |
| 32 | 32 |
| 33 namespace proximity_auth { | 33 namespace proximity_auth { |
| 34 | 34 |
| 35 class BluetoothThrottler; | 35 class BluetoothThrottler; |
| 36 | 36 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 // proximity_auth::Connection: | 119 // proximity_auth::Connection: |
| 120 void SendMessageImpl(scoped_ptr<WireMessage> message) override; | 120 void SendMessageImpl(scoped_ptr<WireMessage> message) override; |
| 121 | 121 |
| 122 // device::BluetoothAdapter::Observer: | 122 // device::BluetoothAdapter::Observer: |
| 123 void DeviceChanged(device::BluetoothAdapter* adapter, | 123 void DeviceChanged(device::BluetoothAdapter* adapter, |
| 124 device::BluetoothDevice* device) override; | 124 device::BluetoothDevice* device) override; |
| 125 void DeviceRemoved(device::BluetoothAdapter* adapter, | 125 void DeviceRemoved(device::BluetoothAdapter* adapter, |
| 126 device::BluetoothDevice* device) override; | 126 device::BluetoothDevice* device) override; |
| 127 void GattCharacteristicValueChanged( | 127 void GattCharacteristicValueChanged( |
| 128 device::BluetoothAdapter* adapter, | 128 device::BluetoothAdapter* adapter, |
| 129 device::BluetoothGattCharacteristic* characteristic, | 129 device::BluetoothRemoteGattCharacteristic* characteristic, |
| 130 const std::vector<uint8_t>& value) override; | 130 const std::vector<uint8_t>& value) override; |
| 131 | 131 |
| 132 private: | 132 private: |
| 133 // Represents a request to write |value| to a some characteristic. | 133 // Represents a request to write |value| to a some characteristic. |
| 134 // |is_last_write_for_wire_messsage| indicates whether this request | 134 // |is_last_write_for_wire_messsage| indicates whether this request |
| 135 // corresponds to the last write request for some wire message. | 135 // corresponds to the last write request for some wire message. |
| 136 // A WireMessage corresponds to exactly two WriteRequest: the first containing | 136 // A WireMessage corresponds to exactly two WriteRequest: the first containing |
| 137 // a kSendSignal + the size of the WireMessage, and the second containing a | 137 // a kSendSignal + the size of the WireMessage, and the second containing a |
| 138 // SendStatusSignal + the serialized WireMessage. | 138 // SendStatusSignal + the serialized WireMessage. |
| 139 struct WriteRequest { | 139 struct WriteRequest { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 // (OnRemoteCharacteristicWritten()); | 200 // (OnRemoteCharacteristicWritten()); |
| 201 // 4) |request| is not dequeued if it fails | 201 // 4) |request| is not dequeued if it fails |
| 202 // (OnWriteRemoteCharacteristicError()), it remains on the queue and will be | 202 // (OnWriteRemoteCharacteristicError()), it remains on the queue and will be |
| 203 // retried. |request| will remain on the queue until it succeeds or it | 203 // retried. |request| will remain on the queue until it succeeds or it |
| 204 // triggers a Disconnect() call (after |max_number_of_tries_|). | 204 // triggers a Disconnect() call (after |max_number_of_tries_|). |
| 205 void WriteRemoteCharacteristic(WriteRequest request); | 205 void WriteRemoteCharacteristic(WriteRequest request); |
| 206 | 206 |
| 207 // Processes the next request in |write_requests_queue_|. | 207 // Processes the next request in |write_requests_queue_|. |
| 208 void ProcessNextWriteRequest(); | 208 void ProcessNextWriteRequest(); |
| 209 | 209 |
| 210 // Called when the BluetoothGattCharacteristic::RemoteCharacteristicWrite() is | 210 // Called when the |
| 211 // BluetoothRemoteGattCharacteristic::RemoteCharacteristicWrite() is |
| 211 // successfully complete. | 212 // successfully complete. |
| 212 void OnRemoteCharacteristicWritten(bool run_did_send_message_callback); | 213 void OnRemoteCharacteristicWritten(bool run_did_send_message_callback); |
| 213 | 214 |
| 214 // Called when there is an error writing to the remote characteristic | 215 // Called when there is an error writing to the remote characteristic |
| 215 // |to_peripheral_char_|. | 216 // |to_peripheral_char_|. |
| 216 void OnWriteRemoteCharacteristicError( | 217 void OnWriteRemoteCharacteristicError( |
| 217 bool run_did_send_message_callback, | 218 bool run_did_send_message_callback, |
| 218 device::BluetoothGattService::GattErrorCode error); | 219 device::BluetoothRemoteGattService::GattErrorCode error); |
| 219 | 220 |
| 220 // Builds the value to be written on |to_peripheral_char_|. The value | 221 // Builds the value to be written on |to_peripheral_char_|. The value |
| 221 // corresponds to |signal| concatenated with |payload|. | 222 // corresponds to |signal| concatenated with |payload|. |
| 222 WriteRequest BuildWriteRequest(const std::vector<uint8_t>& signal, | 223 WriteRequest BuildWriteRequest(const std::vector<uint8_t>& signal, |
| 223 const std::vector<uint8_t>& bytes, | 224 const std::vector<uint8_t>& bytes, |
| 224 bool is_last_message_for_wire_message); | 225 bool is_last_message_for_wire_message); |
| 225 | 226 |
| 226 // Prints the time elapsed since |Connect()| was called. | 227 // Prints the time elapsed since |Connect()| was called. |
| 227 void PrintTimeElapsed(); | 228 void PrintTimeElapsed(); |
| 228 | 229 |
| 229 // Returns the device corresponding to |remote_device_address_|. | 230 // Returns the device corresponding to |remote_device_address_|. |
| 230 device::BluetoothDevice* GetRemoteDevice(); | 231 device::BluetoothDevice* GetRemoteDevice(); |
| 231 | 232 |
| 232 // Returns the service corresponding to |remote_service_| in the current | 233 // Returns the service corresponding to |remote_service_| in the current |
| 233 // device. | 234 // device. |
| 234 device::BluetoothGattService* GetRemoteService(); | 235 device::BluetoothRemoteGattService* GetRemoteService(); |
| 235 | 236 |
| 236 // Returns the characteristic corresponding to |identifier| in the current | 237 // Returns the characteristic corresponding to |identifier| in the current |
| 237 // service. | 238 // service. |
| 238 device::BluetoothGattCharacteristic* GetGattCharacteristic( | 239 device::BluetoothRemoteGattCharacteristic* GetGattCharacteristic( |
| 239 const std::string& identifier); | 240 const std::string& identifier); |
| 240 | 241 |
| 241 // Convert the first 4 bytes from a byte vector to a uint32_t. | 242 // Convert the first 4 bytes from a byte vector to a uint32_t. |
| 242 uint32_t ToUint32(const std::vector<uint8_t>& bytes); | 243 uint32_t ToUint32(const std::vector<uint8_t>& bytes); |
| 243 | 244 |
| 244 // Convert an uint32_t to a byte vector. | 245 // Convert an uint32_t to a byte vector. |
| 245 const std::vector<uint8_t> ToByteVector(uint32_t value); | 246 const std::vector<uint8_t> ToByteVector(uint32_t value); |
| 246 | 247 |
| 247 // The Bluetooth adapter over which the Bluetooth connection will be made. | 248 // The Bluetooth adapter over which the Bluetooth connection will be made. |
| 248 scoped_refptr<device::BluetoothAdapter> adapter_; | 249 scoped_refptr<device::BluetoothAdapter> adapter_; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 277 // Indicates a receiving operation is in progress. This is set after a | 278 // Indicates a receiving operation is in progress. This is set after a |
| 278 // ControlSignal::kSendSignal was received from the remote device. | 279 // ControlSignal::kSendSignal was received from the remote device. |
| 279 bool receiving_bytes_; | 280 bool receiving_bytes_; |
| 280 | 281 |
| 281 // Total number of bytes expected for the current receive operation. | 282 // Total number of bytes expected for the current receive operation. |
| 282 std::size_t expected_number_of_incoming_bytes_; | 283 std::size_t expected_number_of_incoming_bytes_; |
| 283 | 284 |
| 284 // Bytes already received for the current receive operation. | 285 // Bytes already received for the current receive operation. |
| 285 std::string incoming_bytes_buffer_; | 286 std::string incoming_bytes_buffer_; |
| 286 | 287 |
| 287 // Indicates there is a BluetoothGattCharacteristic::WriteRemoteCharacteristic | 288 // Indicates there is a |
| 289 // BluetoothRemoteGattCharacteristic::WriteRemoteCharacteristic |
| 288 // operation pending. | 290 // operation pending. |
| 289 bool write_remote_characteristic_pending_; | 291 bool write_remote_characteristic_pending_; |
| 290 | 292 |
| 291 std::queue<WriteRequest> write_requests_queue_; | 293 std::queue<WriteRequest> write_requests_queue_; |
| 292 | 294 |
| 293 // Maximum number of tries to send any write request. | 295 // Maximum number of tries to send any write request. |
| 294 int max_number_of_write_attempts_; | 296 int max_number_of_write_attempts_; |
| 295 | 297 |
| 296 // Maximum number of bytes that fit in a single chunk to be written in | 298 // Maximum number of bytes that fit in a single chunk to be written in |
| 297 // |to_peripheral_char_|. Ideally, this should be the maximum value the | 299 // |to_peripheral_char_|. Ideally, this should be the maximum value the |
| 298 // peripheral supports and it should be agreed when the GATT connection is | 300 // peripheral supports and it should be agreed when the GATT connection is |
| 299 // created. Currently, there is no API to find this value. The implementation | 301 // created. Currently, there is no API to find this value. The implementation |
| 300 // uses a hard-coded constant. | 302 // uses a hard-coded constant. |
| 301 int max_chunk_size_; | 303 int max_chunk_size_; |
| 302 | 304 |
| 303 // Stores when the instace was created. | 305 // Stores when the instace was created. |
| 304 base::TimeTicks start_time_; | 306 base::TimeTicks start_time_; |
| 305 | 307 |
| 306 base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_; | 308 base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_; |
| 307 | 309 |
| 308 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection); | 310 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection); |
| 309 }; | 311 }; |
| 310 | 312 |
| 311 } // namespace proximity_auth | 313 } // namespace proximity_auth |
| 312 | 314 |
| 313 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_ | 315 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_ |
| OLD | NEW |