Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_connection.h

Issue 2561203002: Migrate weave-related classes from proximity_auth/ble to cryptauth/ble. (Closed)
Patch Set: Rebase. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <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/cryptauth/ble/bluetooth_low_energy_characteristics_finder.h "
20 #include "components/proximity_auth/ble/fake_wire_message.h" 20 #include "components/cryptauth/ble/fake_wire_message.h"
21 #include "components/proximity_auth/ble/remote_attribute.h" 21 #include "components/cryptauth/ble/remote_attribute.h"
22 #include "components/proximity_auth/connection.h" 22 #include "components/cryptauth/bluetooth_throttler.h"
23 #include "components/cryptauth/connection.h"
23 #include "device/bluetooth/bluetooth_adapter.h" 24 #include "device/bluetooth/bluetooth_adapter.h"
24 #include "device/bluetooth/bluetooth_device.h" 25 #include "device/bluetooth/bluetooth_device.h"
25 #include "device/bluetooth/bluetooth_gatt_notify_session.h" 26 #include "device/bluetooth/bluetooth_gatt_notify_session.h"
26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" 27 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
27 #include "device/bluetooth/bluetooth_uuid.h" 28 #include "device/bluetooth/bluetooth_uuid.h"
28 29
29 namespace base { 30 namespace base {
30 class TaskRunner; 31 class TaskRunner;
31 } 32 }
32 33
33 namespace proximity_auth { 34 namespace proximity_auth {
34 35
35 class BluetoothThrottler;
36
37 // Represents a connection with a remote device over Bluetooth low energy. The 36 // Represents a connection with a remote device over Bluetooth low energy. The
38 // connection is a persistent bidirectional channel for sending and receiving 37 // connection is a persistent bidirectional channel for sending and receiving
39 // wire messages. The remote device is the peripheral mode and the service 38 // wire messages. The remote device is the peripheral mode and the service
40 // contains two characteristics: one to send data and another to receive it. 39 // contains two characteristics: one to send data and another to receive it.
41 // 40 //
42 // The connection flow is described below. 41 // The connection flow is described below.
43 // 42 //
44 // Discover Reader and Writer Characteristics 43 // Discover Reader and Writer Characteristics
45 // | 44 // |
46 // | 45 // |
47 // | 46 // |
48 // Start Notify Session 47 // Start Notify Session
49 // | 48 // |
50 // | 49 // |
51 // | 50 // |
52 // Write kInviteToConnectSignal to Writer Characteristic 51 // Write kInviteToConnectSignal to Writer Characteristic
53 // | 52 // |
54 // | 53 // |
55 // | 54 // |
56 // Read kInvitationResponseSignal from Reader Characteristic 55 // Read kInvitationResponseSignal from Reader Characteristic
57 // | 56 // |
58 // | 57 // |
59 // | 58 // |
60 // Proximity Auth Connection Established 59 // Proximity Auth Connection Established
61 class BluetoothLowEnergyConnection : public Connection, 60 class BluetoothLowEnergyConnection : public cryptauth::Connection,
62 public device::BluetoothAdapter::Observer { 61 public device::BluetoothAdapter::Observer {
63 public: 62 public:
64 // Signals sent to the remote device to indicate connection related events. 63 // Signals sent to the remote device to indicate connection related events.
65 enum class ControlSignal : uint32_t { 64 enum class ControlSignal : uint32_t {
66 kInviteToConnectSignal = 0, 65 kInviteToConnectSignal = 0,
67 kInvitationResponseSignal = 1, 66 kInvitationResponseSignal = 1,
68 kSendSignal = 2, 67 kSendSignal = 2,
69 kDisconnectSignal = 3, 68 kDisconnectSignal = 3,
70 }; 69 };
71 70
72 // The sub-state of a proximity_auth::BluetoothLowEnergyConnection class 71 // The sub-state of a proximity_auth::BluetoothLowEnergyConnection class
73 // extends the IN_PROGRESS state of proximity_auth::Connection::Status. 72 // extends the IN_PROGRESS state of proximity_auth::Connection::Status.
74 enum class SubStatus { 73 enum class SubStatus {
75 DISCONNECTED, 74 DISCONNECTED,
76 WAITING_GATT_CONNECTION, 75 WAITING_GATT_CONNECTION,
77 WAITING_CHARACTERISTICS, 76 WAITING_CHARACTERISTICS,
78 CHARACTERISTICS_FOUND, 77 CHARACTERISTICS_FOUND,
79 WAITING_NOTIFY_SESSION, 78 WAITING_NOTIFY_SESSION,
80 NOTIFY_SESSION_READY, 79 NOTIFY_SESSION_READY,
81 WAITING_RESPONSE_SIGNAL, 80 WAITING_RESPONSE_SIGNAL,
82 CONNECTED, 81 CONNECTED,
83 }; 82 };
84 83
85 // Constructs a Bluetooth low energy connection to the service with 84 // Constructs a Bluetooth low energy connection to the service with
86 // |remote_service_| on the |remote_device|. The |adapter| must be already 85 // |remote_service_| on the |remote_device|. The |adapter| must be already
87 // initaalized and ready. The GATT connection may alreaady be established and 86 // initaalized and ready. The GATT connection may alreaady be established and
88 // pass through |gatt_connection|. A subsequent call to Connect() must be 87 // pass through |gatt_connection|. A subsequent call to Connect() must be
89 // made. 88 // made.
90 BluetoothLowEnergyConnection(const cryptauth::RemoteDevice& remote_device, 89 BluetoothLowEnergyConnection(
91 scoped_refptr<device::BluetoothAdapter> adapter, 90 const cryptauth::RemoteDevice& remote_device,
92 const device::BluetoothUUID remote_service_uuid, 91 scoped_refptr<device::BluetoothAdapter> adapter,
93 BluetoothThrottler* bluetooth_throttler, 92 const device::BluetoothUUID remote_service_uuid,
94 int max_number_of_write_attempts); 93 cryptauth::BluetoothThrottler* bluetooth_throttler,
94 int max_number_of_write_attempts);
95 95
96 ~BluetoothLowEnergyConnection() override; 96 ~BluetoothLowEnergyConnection() override;
97 97
98 // proximity_auth::Connection: 98 // cryptauth::Connection:
99 void Connect() override; 99 void Connect() override;
100 void Disconnect() override; 100 void Disconnect() override;
101 std::string GetDeviceAddress() override; 101 std::string GetDeviceAddress() override;
102 102
103 protected: 103 protected:
104 // Exposed for testing. 104 // Exposed for testing.
105 void SetSubStatus(SubStatus status); 105 void SetSubStatus(SubStatus status);
106 SubStatus sub_status() { return sub_status_; } 106 SubStatus sub_status() { return sub_status_; }
107 107
108 // Sets |task_runner_| for testing. 108 // Sets |task_runner_| for testing.
109 void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner); 109 void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner);
110 110
111 // Virtual for testing. 111 // Virtual for testing.
112 virtual BluetoothLowEnergyCharacteristicsFinder* CreateCharacteristicsFinder( 112 virtual cryptauth::BluetoothLowEnergyCharacteristicsFinder*
113 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& 113 CreateCharacteristicsFinder(
114 const cryptauth::BluetoothLowEnergyCharacteristicsFinder::SuccessCallback&
114 success_callback, 115 success_callback,
115 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& 116 const cryptauth::BluetoothLowEnergyCharacteristicsFinder::ErrorCallback&
116 error_callback); 117 error_callback);
117 118
118 // proximity_auth::Connection: 119 // cryptauth::Connection:
119 void SendMessageImpl(std::unique_ptr<WireMessage> message) override; 120 void SendMessageImpl(
121 std::unique_ptr<cryptauth::WireMessage> message) override;
120 122
121 // device::BluetoothAdapter::Observer: 123 // device::BluetoothAdapter::Observer:
122 void DeviceChanged(device::BluetoothAdapter* adapter, 124 void DeviceChanged(device::BluetoothAdapter* adapter,
123 device::BluetoothDevice* device) override; 125 device::BluetoothDevice* device) override;
124 void DeviceRemoved(device::BluetoothAdapter* adapter, 126 void DeviceRemoved(device::BluetoothAdapter* adapter,
125 device::BluetoothDevice* device) override; 127 device::BluetoothDevice* device) override;
126 void GattCharacteristicValueChanged( 128 void GattCharacteristicValueChanged(
127 device::BluetoothAdapter* adapter, 129 device::BluetoothAdapter* adapter,
128 device::BluetoothRemoteGattCharacteristic* characteristic, 130 device::BluetoothRemoteGattCharacteristic* characteristic,
129 const std::vector<uint8_t>& value) override; 131 const std::vector<uint8_t>& value) override;
130 132
131 private: 133 private:
132 // Represents a request to write |value| to a some characteristic. 134 // Represents a request to write |value| to a some characteristic.
133 // |is_last_write_for_wire_messsage| indicates whether this request 135 // |is_last_write_for_wire_messsage| indicates whether this request
134 // corresponds to the last write request for some wire message. 136 // corresponds to the last write request for some wire message.
135 // A WireMessage corresponds to exactly two WriteRequest: the first containing 137 // A cryptauth::WireMessage corresponds to exactly two WriteRequest: the first
136 // a kSendSignal + the size of the WireMessage, and the second containing a 138 // containing
137 // SendStatusSignal + the serialized WireMessage. 139 // a kSendSignal + the size of the cryptauth::WireMessage, and the second
140 // containing a
141 // SendStatusSignal + the serialized cryptauth::WireMessage.
138 struct WriteRequest { 142 struct WriteRequest {
139 WriteRequest(const std::vector<uint8_t>& val, bool flag); 143 WriteRequest(const std::vector<uint8_t>& val, bool flag);
140 WriteRequest(const WriteRequest& other); 144 WriteRequest(const WriteRequest& other);
141 ~WriteRequest(); 145 ~WriteRequest();
142 146
143 std::vector<uint8_t> value; 147 std::vector<uint8_t> value;
144 bool is_last_write_for_wire_message; 148 bool is_last_write_for_wire_message;
145 int number_of_failed_attempts; 149 int number_of_failed_attempts;
146 }; 150 };
147 151
148 // Creates the GATT connection with |remote_device|. 152 // Creates the GATT connection with |remote_device|.
149 void CreateGattConnection(); 153 void CreateGattConnection();
150 154
151 // Called when a GATT connection is created. 155 // Called when a GATT connection is created.
152 void OnGattConnectionCreated( 156 void OnGattConnectionCreated(
153 std::unique_ptr<device::BluetoothGattConnection> gatt_connection); 157 std::unique_ptr<device::BluetoothGattConnection> gatt_connection);
154 158
155 // Callback called when there is an error creating the GATT connection. 159 // Callback called when there is an error creating the GATT connection.
156 void OnCreateGattConnectionError( 160 void OnCreateGattConnectionError(
157 device::BluetoothDevice::ConnectErrorCode error_code); 161 device::BluetoothDevice::ConnectErrorCode error_code);
158 162
159 // Callback called when |to_peripheral_char_| and |from_peripheral_char_| were 163 // Callback called when |to_peripheral_char_| and |from_peripheral_char_| were
160 // found. 164 // found.
161 void OnCharacteristicsFound(const RemoteAttribute& service, 165 void OnCharacteristicsFound(
162 const RemoteAttribute& to_peripheral_char, 166 const cryptauth::RemoteAttribute& service,
163 const RemoteAttribute& from_peripheral_char); 167 const cryptauth::RemoteAttribute& to_peripheral_char,
168 const cryptauth::RemoteAttribute& from_peripheral_char);
164 169
165 // Callback called there was an error finding the characteristics. 170 // Callback called there was an error finding the characteristics.
166 void OnCharacteristicsFinderError( 171 void OnCharacteristicsFinderError(
167 const RemoteAttribute& to_peripheral_char, 172 const cryptauth::RemoteAttribute& to_peripheral_char,
168 const RemoteAttribute& from_peripheral_char); 173 const cryptauth::RemoteAttribute& from_peripheral_char);
169 174
170 // Starts a notify session for |from_peripheral_char_| when ready 175 // Starts a notify session for |from_peripheral_char_| when ready
171 // (SubStatus::CHARACTERISTICS_FOUND). 176 // (SubStatus::CHARACTERISTICS_FOUND).
172 void StartNotifySession(); 177 void StartNotifySession();
173 178
174 // Called when a notification session is successfully started for 179 // Called when a notification session is successfully started for
175 // |from_peripheral_char_| characteristic. 180 // |from_peripheral_char_| characteristic.
176 void OnNotifySessionStarted( 181 void OnNotifySessionStarted(
177 std::unique_ptr<device::BluetoothGattNotifySession> notify_session); 182 std::unique_ptr<device::BluetoothGattNotifySession> notify_session);
178 183
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 // Convert the first 4 bytes from a byte vector to a uint32_t. 246 // Convert the first 4 bytes from a byte vector to a uint32_t.
242 uint32_t ToUint32(const std::vector<uint8_t>& bytes); 247 uint32_t ToUint32(const std::vector<uint8_t>& bytes);
243 248
244 // Convert an uint32_t to a byte vector. 249 // Convert an uint32_t to a byte vector.
245 const std::vector<uint8_t> ToByteVector(uint32_t value); 250 const std::vector<uint8_t> ToByteVector(uint32_t value);
246 251
247 // The Bluetooth adapter over which the Bluetooth connection will be made. 252 // The Bluetooth adapter over which the Bluetooth connection will be made.
248 scoped_refptr<device::BluetoothAdapter> adapter_; 253 scoped_refptr<device::BluetoothAdapter> adapter_;
249 254
250 // Remote service the |gatt_connection_| was established with. 255 // Remote service the |gatt_connection_| was established with.
251 RemoteAttribute remote_service_; 256 cryptauth::RemoteAttribute remote_service_;
252 257
253 // Characteristic used to send data to the remote device. 258 // Characteristic used to send data to the remote device.
254 RemoteAttribute to_peripheral_char_; 259 cryptauth::RemoteAttribute to_peripheral_char_;
255 260
256 // Characteristic used to receive data from the remote device. 261 // Characteristic used to receive data from the remote device.
257 RemoteAttribute from_peripheral_char_; 262 cryptauth::RemoteAttribute from_peripheral_char_;
258 263
259 // Throttles repeated connection attempts to the same device. This is a 264 // Throttles repeated connection attempts to the same device. This is a
260 // workaround for crbug.com/508919. Not owned, must outlive this instance. 265 // workaround for crbug.com/508919. Not owned, must outlive this instance.
261 BluetoothThrottler* bluetooth_throttler_; 266 cryptauth::BluetoothThrottler* bluetooth_throttler_;
262 267
263 scoped_refptr<base::TaskRunner> task_runner_; 268 scoped_refptr<base::TaskRunner> task_runner_;
264 269
265 // The GATT connection with the remote device. 270 // The GATT connection with the remote device.
266 std::unique_ptr<device::BluetoothGattConnection> gatt_connection_; 271 std::unique_ptr<device::BluetoothGattConnection> gatt_connection_;
267 272
268 // The characteristics finder for remote device. 273 // The characteristics finder for remote device.
269 std::unique_ptr<BluetoothLowEnergyCharacteristicsFinder> 274 std::unique_ptr<cryptauth::BluetoothLowEnergyCharacteristicsFinder>
270 characteristic_finder_; 275 characteristic_finder_;
271 276
272 // The notify session for |from_peripheral_char|. 277 // The notify session for |from_peripheral_char|.
273 std::unique_ptr<device::BluetoothGattNotifySession> notify_session_; 278 std::unique_ptr<device::BluetoothGattNotifySession> notify_session_;
274 279
275 // Internal connection status 280 // Internal connection status
276 SubStatus sub_status_; 281 SubStatus sub_status_;
277 282
278 // Indicates a receiving operation is in progress. This is set after a 283 // Indicates a receiving operation is in progress. This is set after a
279 // ControlSignal::kSendSignal was received from the remote device. 284 // ControlSignal::kSendSignal was received from the remote device.
(...skipping 26 matching lines...) Expand all
306 base::TimeTicks start_time_; 311 base::TimeTicks start_time_;
307 312
308 base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_; 313 base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_;
309 314
310 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection); 315 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection);
311 }; 316 };
312 317
313 } // namespace proximity_auth 318 } // namespace proximity_auth
314 319
315 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_ 320 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_CONNECTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698