Chromium Code Reviews| 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_WEAVE_CLIENT_CONNECTI ON_H_ | 5 #ifndef COMPONENTS_CRYPTAUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CLIENT_CONNECTION_H_ |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CLIENT_CONNECTI ON_H_ | 6 #define COMPONENTS_CRYPTAUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CLIENT_CONNECTION_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <queue> | 13 #include <queue> |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" | 20 #include "components/cryptauth/ble/bluetooth_low_energy_characteristics_finder.h " |
| 21 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_genera tor.h" | 21 #include "components/cryptauth/ble/bluetooth_low_energy_weave_packet_generator.h " |
| 22 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_packet_receiv er.h" | 22 #include "components/cryptauth/ble/bluetooth_low_energy_weave_packet_receiver.h" |
| 23 #include "components/proximity_auth/ble/fake_wire_message.h" | 23 #include "components/cryptauth/ble/fake_wire_message.h" |
| 24 #include "components/proximity_auth/ble/remote_attribute.h" | 24 #include "components/cryptauth/ble/remote_attribute.h" |
| 25 #include "components/proximity_auth/bluetooth_throttler.h" | |
| 25 #include "components/proximity_auth/connection.h" | 26 #include "components/proximity_auth/connection.h" |
| 26 #include "device/bluetooth/bluetooth_adapter.h" | 27 #include "device/bluetooth/bluetooth_adapter.h" |
| 27 #include "device/bluetooth/bluetooth_device.h" | 28 #include "device/bluetooth/bluetooth_device.h" |
| 28 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | 29 #include "device/bluetooth/bluetooth_gatt_notify_session.h" |
| 29 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 30 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 30 #include "device/bluetooth/bluetooth_uuid.h" | 31 #include "device/bluetooth/bluetooth_uuid.h" |
| 31 | 32 |
| 32 namespace base { | 33 namespace base { |
| 33 class TaskRunner; | 34 class TaskRunner; |
| 34 } | 35 } |
| 35 | 36 |
| 36 namespace proximity_auth { | 37 namespace cryptauth { |
| 37 class BluetoothThrottler; | |
| 38 | 38 |
| 39 namespace weave { | 39 namespace weave { |
| 40 // Creates GATT connection on top of the BLE connection and act as a Client. | 40 // Creates GATT connection on top of the BLE connection and act as a Client. |
| 41 // uWeave communication follows the flow: | 41 // uWeave communication follows the flow: |
| 42 // Client | Server | 42 // Client | Server |
| 43 // ---------------------------------|-------------------------------- | 43 // ---------------------------------|-------------------------------- |
| 44 // send connection request | | 44 // send connection request | |
| 45 // | receive connection request | 45 // | receive connection request |
| 46 // | send connection response | 46 // | send connection response |
| 47 // receive connection response | | 47 // receive connection response | |
| 48 // opt: send data | opt: send data | 48 // opt: send data | opt: send data |
| 49 // receive data | receive data | 49 // receive data | receive data |
| 50 // opt: close connection | opt: close connection | 50 // opt: close connection | opt: close connection |
| 51 class BluetoothLowEnergyWeaveClientConnection | 51 class BluetoothLowEnergyWeaveClientConnection |
| 52 : public Connection, | 52 : public proximity_auth::Connection, |
|
Kyle Horimoto
2016/12/08 21:52:40
//components/cryptauth should not depend on //comp
Ryan Hansberry
2016/12/09 01:18:14
Done.
| |
| 53 public device::BluetoothAdapter::Observer { | 53 public device::BluetoothAdapter::Observer { |
| 54 public: | 54 public: |
| 55 class Factory { | 55 class Factory { |
| 56 public: | 56 public: |
| 57 static std::unique_ptr<BluetoothLowEnergyWeaveClientConnection> | 57 static std::unique_ptr<BluetoothLowEnergyWeaveClientConnection> |
| 58 NewInstance(); | 58 NewInstance(); |
| 59 | 59 |
| 60 // Exposed for testing. | 60 // Exposed for testing. |
| 61 static void SetInstanceForTesting(Factory* factory); | 61 static void SetInstanceForTesting(Factory* factory); |
| 62 | 62 |
| 63 protected: | 63 protected: |
| 64 // Exposed for testing. | 64 // Exposed for testing. |
| 65 virtual std::unique_ptr<BluetoothLowEnergyWeaveClientConnection> | 65 virtual std::unique_ptr<BluetoothLowEnergyWeaveClientConnection> |
| 66 BuildInstance(); | 66 BuildInstance(); |
| 67 | 67 |
| 68 private: | 68 private: |
| 69 static Factory* factory_instance_; | 69 static Factory* factory_instance_; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 // The sub-state of a proximity_auth::BluetoothLowEnergyWeaveClientConnection | 72 // The sub-state of a cryptauth::BluetoothLowEnergyWeaveClientConnection |
| 73 // extends the IN_PROGRESS state of proximity_auth::Connection::Status. | 73 // extends the IN_PROGRESS state of proximity_auth::Connection::Status. |
| 74 enum SubStatus { | 74 enum SubStatus { |
| 75 DISCONNECTED, | 75 DISCONNECTED, |
| 76 WAITING_GATT_CONNECTION, | 76 WAITING_GATT_CONNECTION, |
| 77 WAITING_CHARACTERISTICS, | 77 WAITING_CHARACTERISTICS, |
| 78 CHARACTERISTICS_FOUND, | 78 CHARACTERISTICS_FOUND, |
| 79 WAITING_NOTIFY_SESSION, | 79 WAITING_NOTIFY_SESSION, |
| 80 NOTIFY_SESSION_READY, | 80 NOTIFY_SESSION_READY, |
| 81 WAITING_CONNECTION_RESPONSE, | 81 WAITING_CONNECTION_RESPONSE, |
| 82 CONNECTED, | 82 CONNECTED, |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 // Constructs a Bluetooth low energy connection to the service with | 85 // Constructs a Bluetooth low energy connection to the service with |
| 86 // |remote_service_| on the |remote_device|. The |adapter| must be already | 86 // |remote_service_| on the |remote_device|. The |adapter| must be already |
| 87 // initialized and ready. The GATT connection may alreaady be established and | 87 // initialized and ready. The GATT connection may alreaady be established and |
| 88 // pass through |gatt_connection|. A subsequent call to Connect() must be | 88 // pass through |gatt_connection|. A subsequent call to Connect() must be |
| 89 // made. | 89 // made. |
| 90 BluetoothLowEnergyWeaveClientConnection( | 90 BluetoothLowEnergyWeaveClientConnection( |
| 91 const cryptauth::RemoteDevice& remote_device, | 91 const cryptauth::RemoteDevice& remote_device, |
| 92 scoped_refptr<device::BluetoothAdapter> adapter, | 92 scoped_refptr<device::BluetoothAdapter> adapter, |
| 93 const device::BluetoothUUID remote_service_uuid, | 93 const device::BluetoothUUID remote_service_uuid, |
| 94 BluetoothThrottler* bluetooth_throttler, | 94 proximity_auth::BluetoothThrottler* bluetooth_throttler, |
| 95 int max_number_of_write_attempts); | 95 int max_number_of_write_attempts); |
| 96 | 96 |
| 97 ~BluetoothLowEnergyWeaveClientConnection() override; | 97 ~BluetoothLowEnergyWeaveClientConnection() override; |
| 98 | 98 |
| 99 // proximity_auth::Connection: | 99 // namespace cryptauth::Connection: |
| 100 void Connect() override; | 100 void Connect() override; |
| 101 void Disconnect() override; | 101 void Disconnect() override; |
| 102 std::string GetDeviceAddress() override; | 102 std::string GetDeviceAddress() override; |
| 103 | 103 |
| 104 protected: | 104 protected: |
| 105 // Exposed for testing. | 105 // Exposed for testing. |
| 106 void DestroyConnection(); | 106 void DestroyConnection(); |
| 107 | 107 |
| 108 // Exposed for testing. | 108 // Exposed for testing. |
| 109 SubStatus sub_status() { return sub_status_; } | 109 SubStatus sub_status() { return sub_status_; } |
| 110 | 110 |
| 111 // Sets |task_runner_| for testing. | 111 // Sets |task_runner_| for testing. |
| 112 void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner); | 112 void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner); |
| 113 | 113 |
| 114 // Virtual for testing. | 114 // Virtual for testing. |
| 115 virtual BluetoothLowEnergyCharacteristicsFinder* CreateCharacteristicsFinder( | 115 virtual BluetoothLowEnergyCharacteristicsFinder* CreateCharacteristicsFinder( |
| 116 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | 116 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& |
| 117 success_callback, | 117 success_callback, |
| 118 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& | 118 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& |
| 119 error_callback); | 119 error_callback); |
| 120 | 120 |
| 121 // proximity_auth::Connection: | 121 // namespace cryptauth::Connection: |
| 122 void SendMessageImpl(std::unique_ptr<WireMessage> message) override; | 122 void SendMessageImpl( |
| 123 std::unique_ptr<proximity_auth::WireMessage> message) override; | |
| 123 | 124 |
| 124 // device::BluetoothAdapter::Observer: | 125 // device::BluetoothAdapter::Observer: |
| 125 void DeviceChanged(device::BluetoothAdapter* adapter, | 126 void DeviceChanged(device::BluetoothAdapter* adapter, |
| 126 device::BluetoothDevice* device) override; | 127 device::BluetoothDevice* device) override; |
| 127 void DeviceRemoved(device::BluetoothAdapter* adapter, | 128 void DeviceRemoved(device::BluetoothAdapter* adapter, |
| 128 device::BluetoothDevice* device) override; | 129 device::BluetoothDevice* device) override; |
| 129 void GattCharacteristicValueChanged( | 130 void GattCharacteristicValueChanged( |
| 130 device::BluetoothAdapter* adapter, | 131 device::BluetoothAdapter* adapter, |
| 131 device::BluetoothRemoteGattCharacteristic* characteristic, | 132 device::BluetoothRemoteGattCharacteristic* characteristic, |
| 132 const Packet& value) override; | 133 const Packet& value) override; |
| 133 | 134 |
| 134 private: | 135 private: |
| 135 enum WriteRequestType { | 136 enum WriteRequestType { |
| 136 REGULAR, | 137 REGULAR, |
| 137 MESSAGE_COMPLETE, | 138 MESSAGE_COMPLETE, |
| 138 CONNECTION_REQUEST, | 139 CONNECTION_REQUEST, |
| 139 CONNECTION_CLOSE | 140 CONNECTION_CLOSE |
| 140 }; | 141 }; |
| 141 | 142 |
| 142 // Represents a request to write |value| to a some characteristic. | 143 // Represents a request to write |value| to a some characteristic. |
| 143 // |is_last_write_for_wire_messsage| indicates whether this request | 144 // |is_last_write_for_wire_messsage| indicates whether this request |
| 144 // corresponds to the last write request for some wire message. | 145 // corresponds to the last write request for some wire message. |
| 145 struct WriteRequest { | 146 struct WriteRequest { |
| 146 WriteRequest(const Packet& val, | 147 WriteRequest(const Packet& val, |
| 147 WriteRequestType request_type, | 148 WriteRequestType request_type, |
| 148 std::shared_ptr<WireMessage> message); | 149 std::shared_ptr<proximity_auth::WireMessage> message); |
| 149 WriteRequest(const Packet& val, WriteRequestType request_type); | 150 WriteRequest(const Packet& val, WriteRequestType request_type); |
| 150 WriteRequest(const WriteRequest& other); | 151 WriteRequest(const WriteRequest& other); |
| 151 ~WriteRequest(); | 152 ~WriteRequest(); |
| 152 | 153 |
| 153 Packet value; | 154 Packet value; |
| 154 WriteRequestType request_type; | 155 WriteRequestType request_type; |
| 155 std::shared_ptr<WireMessage> message; | 156 std::shared_ptr<proximity_auth::WireMessage> message; |
| 156 int number_of_failed_attempts; | 157 int number_of_failed_attempts; |
| 157 }; | 158 }; |
| 158 | 159 |
| 159 void SetSubStatus(SubStatus status); | 160 void SetSubStatus(SubStatus status); |
| 160 | 161 |
| 161 // Creates the GATT connection with |remote_device|. | 162 // Creates the GATT connection with |remote_device|. |
| 162 void CreateGattConnection(); | 163 void CreateGattConnection(); |
| 163 | 164 |
| 164 // Called when a GATT connection is created. | 165 // Called when a GATT connection is created. |
| 165 void OnGattConnectionCreated( | 166 void OnGattConnectionCreated( |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> packet_receiver_; | 264 std::unique_ptr<BluetoothLowEnergyWeavePacketReceiver> packet_receiver_; |
| 264 | 265 |
| 265 // Characteristic used to send data to the remote device. | 266 // Characteristic used to send data to the remote device. |
| 266 RemoteAttribute tx_characteristic_; | 267 RemoteAttribute tx_characteristic_; |
| 267 | 268 |
| 268 // Characteristic used to receive data from the remote device. | 269 // Characteristic used to receive data from the remote device. |
| 269 RemoteAttribute rx_characteristic_; | 270 RemoteAttribute rx_characteristic_; |
| 270 | 271 |
| 271 // Throttles repeated connection attempts to the same device. This is a | 272 // Throttles repeated connection attempts to the same device. This is a |
| 272 // workaround for crbug.com/508919. Not owned, must outlive this instance. | 273 // workaround for crbug.com/508919. Not owned, must outlive this instance. |
| 273 BluetoothThrottler* bluetooth_throttler_; | 274 proximity_auth::BluetoothThrottler* bluetooth_throttler_; |
| 274 | 275 |
| 275 scoped_refptr<base::TaskRunner> task_runner_; | 276 scoped_refptr<base::TaskRunner> task_runner_; |
| 276 | 277 |
| 277 // The GATT connection with the remote device. | 278 // The GATT connection with the remote device. |
| 278 std::unique_ptr<device::BluetoothGattConnection> gatt_connection_; | 279 std::unique_ptr<device::BluetoothGattConnection> gatt_connection_; |
| 279 | 280 |
| 280 // The characteristics finder for remote device. | 281 // The characteristics finder for remote device. |
| 281 std::unique_ptr<BluetoothLowEnergyCharacteristicsFinder> | 282 std::unique_ptr<BluetoothLowEnergyCharacteristicsFinder> |
| 282 characteristic_finder_; | 283 characteristic_finder_; |
| 283 | 284 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 304 base::TimeTicks start_time_; | 305 base::TimeTicks start_time_; |
| 305 | 306 |
| 306 base::WeakPtrFactory<BluetoothLowEnergyWeaveClientConnection> | 307 base::WeakPtrFactory<BluetoothLowEnergyWeaveClientConnection> |
| 307 weak_ptr_factory_; | 308 weak_ptr_factory_; |
| 308 | 309 |
| 309 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyWeaveClientConnection); | 310 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyWeaveClientConnection); |
| 310 }; | 311 }; |
| 311 | 312 |
| 312 } // namespace weave | 313 } // namespace weave |
| 313 | 314 |
| 314 } // namespace proximity_auth | 315 } // namespace cryptauth |
| 315 | 316 |
| 316 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CLIENT_CONNE CTION_H_ | 317 #endif // COMPONENTS_CRYPTAUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_CLIENT_CONNECTION _H_ |
| OLD | NEW |