OLD | NEW |
1 // Copyright 2015 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 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_connection.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | |
16 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" | |
17 #include "components/proximity_auth/ble/fake_wire_message.h" | |
18 #include "components/proximity_auth/bluetooth_throttler.h" | 13 #include "components/proximity_auth/bluetooth_throttler.h" |
19 #include "components/proximity_auth/connection_finder.h" | 14 #include "components/proximity_auth/connection_finder.h" |
20 #include "components/proximity_auth/logging/logging.h" | 15 #include "components/proximity_auth/logging/logging.h" |
21 #include "components/proximity_auth/wire_message.h" | 16 #include "components/proximity_auth/wire_message.h" |
22 #include "device/bluetooth/bluetooth_adapter.h" | |
23 #include "device/bluetooth/bluetooth_device.h" | |
24 #include "device/bluetooth/bluetooth_gatt_connection.h" | 17 #include "device/bluetooth/bluetooth_gatt_connection.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" | |
28 | 18 |
29 using device::BluetoothAdapter; | 19 using device::BluetoothAdapter; |
30 using device::BluetoothDevice; | 20 using device::BluetoothDevice; |
31 using device::BluetoothGattConnection; | 21 using device::BluetoothGattConnection; |
32 using device::BluetoothRemoteGattService; | 22 using device::BluetoothRemoteGattService; |
33 using device::BluetoothRemoteGattCharacteristic; | 23 using device::BluetoothRemoteGattCharacteristic; |
34 using device::BluetoothGattNotifySession; | 24 using device::BluetoothGattNotifySession; |
35 using device::BluetoothUUID; | 25 using device::BluetoothUUID; |
36 | 26 |
37 namespace proximity_auth { | |
38 namespace { | 27 namespace { |
39 | 28 |
40 // The UUID of the characteristic used to send data to the peripheral. | 29 typedef proximity_auth::BluetoothLowEnergyWeavePacketReceiver::State |
41 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; | 30 ReceiverState; |
42 | 31 |
43 // The UUID of the characteristic used to receive data from the peripheral. | 32 // TODO(jingxy): figure out the right UUID to use |
44 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; | 33 // The UUID of the TX characteristic used to send data to the server. |
| 34 const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; |
45 | 35 |
46 // Deprecated signal send as the first byte in send byte operations. | 36 // TODO(jingxy): figure out the right UUID to use |
47 const int kFirstByteZero = 0; | 37 // The UUID of the RX characteristic used to receiver data from the server. |
| 38 const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; |
48 | 39 |
49 // The maximum number of bytes written in a remote characteristic with a single | |
50 // write request. This is not the connection MTU, we are assuming that the | |
51 // remote device allows for writes larger than MTU. | |
52 const int kMaxChunkSize = 500; | |
53 } // namespace | 40 } // namespace |
54 | 41 |
55 BluetoothLowEnergyConnection::BluetoothLowEnergyConnection( | 42 namespace proximity_auth { |
| 43 |
| 44 // TODO(jingxuy): where/how should I use the factor for the packer receiver |
| 45 // and packet generator. |
| 46 BluetoothLowEnergyWeaveConnection::BluetoothLowEnergyWeaveConnection( |
56 const RemoteDevice& device, | 47 const RemoteDevice& device, |
57 scoped_refptr<device::BluetoothAdapter> adapter, | 48 scoped_refptr<device::BluetoothAdapter> adapter, |
58 const BluetoothUUID remote_service_uuid, | 49 const BluetoothUUID remote_service_uuid, |
59 BluetoothThrottler* bluetooth_throttler, | 50 BluetoothThrottler* bluetooth_throttler, |
60 int max_number_of_write_attempts) | 51 int max_number_of_write_attempts) |
61 : Connection(device), | 52 : Connection(device), |
62 adapter_(adapter), | 53 adapter_(adapter), |
63 remote_service_({remote_service_uuid, ""}), | 54 remote_service_({remote_service_uuid, ""}), |
64 to_peripheral_char_({BluetoothUUID(kToPeripheralCharUUID), ""}), | 55 packet_generator_( |
65 from_peripheral_char_({BluetoothUUID(kFromPeripheralCharUUID), ""}), | 56 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance()), |
| 57 packet_receiver_( |
| 58 BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance( |
| 59 BluetoothLowEnergyWeavePacketReceiver::ReceiverType::CLIENT)), |
| 60 tx_characteristic_({BluetoothUUID(kTXCharacteristicUUID), ""}), |
| 61 rx_characteristic_({BluetoothUUID(kRXCharacteristicUUID), ""}), |
66 bluetooth_throttler_(bluetooth_throttler), | 62 bluetooth_throttler_(bluetooth_throttler), |
67 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 63 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
68 sub_status_(SubStatus::DISCONNECTED), | 64 sub_status_(SubStatus::DISCONNECTED), |
69 receiving_bytes_(false), | |
70 write_remote_characteristic_pending_(false), | 65 write_remote_characteristic_pending_(false), |
71 max_number_of_write_attempts_(max_number_of_write_attempts), | 66 max_number_of_write_attempts_(max_number_of_write_attempts), |
72 max_chunk_size_(kMaxChunkSize), | |
73 weak_ptr_factory_(this) { | 67 weak_ptr_factory_(this) { |
74 DCHECK(adapter_); | 68 DCHECK(adapter_); |
75 DCHECK(adapter_->IsInitialized()); | 69 DCHECK(adapter_->IsInitialized()); |
76 | 70 |
77 adapter_->AddObserver(this); | 71 adapter_->AddObserver(this); |
78 } | 72 } |
79 | 73 |
80 BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() { | 74 BluetoothLowEnergyWeaveConnection::~BluetoothLowEnergyWeaveConnection() { |
81 Disconnect(); | 75 Disconnect(); |
82 if (adapter_) { | 76 if (adapter_) { |
83 adapter_->RemoveObserver(this); | 77 adapter_->RemoveObserver(this); |
84 adapter_ = NULL; | 78 adapter_ = NULL; |
85 } | 79 } |
86 } | 80 } |
87 | 81 |
88 void BluetoothLowEnergyConnection::Connect() { | 82 void BluetoothLowEnergyWeaveConnection::Connect() { |
89 DCHECK(sub_status() == SubStatus::DISCONNECTED); | 83 DCHECK(sub_status() == SubStatus::DISCONNECTED); |
90 | 84 |
91 SetSubStatus(SubStatus::WAITING_GATT_CONNECTION); | 85 SetSubStatus(SubStatus::WAITING_GATT_CONNECTION); |
92 base::TimeDelta throttler_delay = bluetooth_throttler_->GetDelay(); | 86 base::TimeDelta throttler_delay = bluetooth_throttler_->GetDelay(); |
93 PA_LOG(INFO) << "Connecting in " << throttler_delay; | 87 PA_LOG(INFO) << "Connecting in " << throttler_delay; |
94 | 88 |
95 start_time_ = base::TimeTicks::Now(); | 89 start_time_ = base::TimeTicks::Now(); |
96 | 90 |
97 // If necessary, wait to create a new GATT connection. | 91 // If necessary, wait to create a new GATT connection. |
98 // | 92 // |
99 // Avoid creating a new GATT connection immediately after a given device was | 93 // Avoid creating a new GATT connection immediately after a given device was |
100 // disconnected. This is a workaround for crbug.com/508919. | 94 // disconnected. This is a workaround for crbug.com/508919. |
101 if (!throttler_delay.is_zero()) { | 95 if (!throttler_delay.is_zero()) { |
102 task_runner_->PostDelayedTask( | 96 task_runner_->PostDelayedTask( |
103 FROM_HERE, | 97 FROM_HERE, |
104 base::Bind(&BluetoothLowEnergyConnection::CreateGattConnection, | 98 base::Bind(&BluetoothLowEnergyWeaveConnection::CreateGattConnection, |
105 weak_ptr_factory_.GetWeakPtr()), | 99 weak_ptr_factory_.GetWeakPtr()), |
106 throttler_delay); | 100 throttler_delay); |
107 return; | 101 return; |
108 } | 102 } |
109 | 103 |
110 CreateGattConnection(); | 104 CreateGattConnection(); |
111 } | 105 } |
112 | 106 |
113 void BluetoothLowEnergyConnection::CreateGattConnection() { | 107 void BluetoothLowEnergyWeaveConnection::CreateGattConnection() { |
114 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); | 108 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); |
115 | 109 |
116 BluetoothDevice* remote_device = GetRemoteDevice(); | 110 BluetoothDevice* remote_device = GetRemoteDevice(); |
117 if (remote_device) { | 111 if (remote_device) { |
118 PA_LOG(INFO) << "Creating GATT connection with " | 112 PA_LOG(INFO) << "Creating GATT connection with " |
119 << remote_device->GetAddress(); | 113 << remote_device->GetAddress(); |
120 | 114 |
121 remote_device->CreateGattConnection( | 115 remote_device->CreateGattConnection( |
122 base::Bind(&BluetoothLowEnergyConnection::OnGattConnectionCreated, | 116 base::Bind(&BluetoothLowEnergyWeaveConnection::OnGattConnectionCreated, |
123 weak_ptr_factory_.GetWeakPtr()), | 117 weak_ptr_factory_.GetWeakPtr()), |
124 base::Bind(&BluetoothLowEnergyConnection::OnCreateGattConnectionError, | 118 base::Bind( |
125 weak_ptr_factory_.GetWeakPtr())); | 119 &BluetoothLowEnergyWeaveConnection::OnCreateGattConnectionError, |
| 120 weak_ptr_factory_.GetWeakPtr())); |
126 } | 121 } |
127 } | 122 } |
128 | 123 |
129 void BluetoothLowEnergyConnection::Disconnect() { | 124 void BluetoothLowEnergyWeaveConnection::Disconnect() { |
130 if (sub_status() != SubStatus::DISCONNECTED) { | 125 if (sub_status() != SubStatus::DISCONNECTED) { |
131 weak_ptr_factory_.InvalidateWeakPtrs(); | 126 weak_ptr_factory_.InvalidateWeakPtrs(); |
132 StopNotifySession(); | 127 StopNotifySession(); |
133 characteristic_finder_.reset(); | 128 characteristic_finder_.reset(); |
134 if (gatt_connection_) { | 129 if (gatt_connection_) { |
135 PA_LOG(INFO) << "Disconnect from device " | 130 PA_LOG(INFO) << "Disconnect from device " |
136 << gatt_connection_->GetDeviceAddress(); | 131 << gatt_connection_->GetDeviceAddress(); |
137 | 132 |
138 // Destroying BluetoothGattConnection also disconnects it. | 133 // Destroying BluetoothGattConnection also disconnects it. |
139 gatt_connection_.reset(); | 134 gatt_connection_.reset(); |
140 } | 135 } |
141 | 136 |
142 // Only transition to the DISCONNECTED state after perfoming all necessary | 137 // Only transition to the DISCONNECTED state after perfoming all necessary |
143 // operations. Otherwise, it'll trigger observers that can pontentially | 138 // operations. Otherwise, it'll trigger observers that can pontentially |
144 // destroy the current instance (causing a crash). | 139 // destroy the current instance (causing a crash). |
145 SetSubStatus(SubStatus::DISCONNECTED); | 140 SetSubStatus(SubStatus::DISCONNECTED); |
146 } | 141 } |
147 } | 142 } |
148 | 143 |
149 void BluetoothLowEnergyConnection::SetSubStatus(SubStatus new_sub_status) { | 144 void BluetoothLowEnergyWeaveConnection::SetSubStatus(SubStatus new_sub_status) { |
150 sub_status_ = new_sub_status; | 145 sub_status_ = new_sub_status; |
151 | 146 |
152 // Sets the status of parent class proximity_auth::Connection accordingly. | 147 // Sets the status of parent class proximity_auth::Connection accordingly. |
153 if (new_sub_status == SubStatus::CONNECTED) { | 148 if (new_sub_status == SubStatus::CONNECTED) { |
154 SetStatus(CONNECTED); | 149 SetStatus(Status::CONNECTED); |
155 } else if (new_sub_status == SubStatus::DISCONNECTED) { | 150 } else if (new_sub_status == SubStatus::DISCONNECTED) { |
156 SetStatus(DISCONNECTED); | 151 SetStatus(Status::DISCONNECTED); |
157 } else { | 152 } else { |
158 SetStatus(IN_PROGRESS); | 153 SetStatus(Status::IN_PROGRESS); |
159 } | 154 } |
160 } | 155 } |
161 | 156 |
162 void BluetoothLowEnergyConnection::SetTaskRunnerForTesting( | 157 void BluetoothLowEnergyWeaveConnection::SetTaskRunnerForTesting( |
163 scoped_refptr<base::TaskRunner> task_runner) { | 158 scoped_refptr<base::TaskRunner> task_runner) { |
164 task_runner_ = task_runner; | 159 task_runner_ = task_runner; |
165 } | 160 } |
166 | 161 |
167 void BluetoothLowEnergyConnection::SendMessageImpl( | 162 void BluetoothLowEnergyWeaveConnection::SendMessageImpl( |
168 std::unique_ptr<WireMessage> message) { | 163 std::unique_ptr<WireMessage> message) { |
169 PA_LOG(INFO) << "Sending message " << message->Serialize(); | 164 PA_LOG(INFO) << "Sending message " << message->Serialize(); |
170 std::string serialized_msg = message->Serialize(); | 165 std::string serialized_msg = message->Serialize(); |
171 | 166 |
172 // [First write]: Build a header with the [send signal] + [size of the | 167 std::vector<std::vector<uint8_t>> packets = |
173 // message]. | 168 packet_generator_->EncodeDataMessage(serialized_msg); |
174 WriteRequest write_request = BuildWriteRequest( | |
175 ToByteVector(static_cast<uint32_t>(ControlSignal::kSendSignal)), | |
176 ToByteVector(static_cast<uint32_t>(serialized_msg.size())), false); | |
177 | 169 |
178 // [First write]: Fill the it with a prefix of |serialized_msg| up to | 170 for (uint32_t i = 0; i < packets.size(); ++i) { |
179 // |max_chunk_size_|. | 171 WriteRequest request = WriteRequest(packets[i], i == packets.size() - 1); |
180 size_t first_chunk_size = std::min( | 172 WriteRemoteCharacteristic(request); |
181 max_chunk_size_ - write_request.value.size(), serialized_msg.size()); | |
182 std::vector<uint8_t> bytes(serialized_msg.begin(), | |
183 serialized_msg.begin() + first_chunk_size); | |
184 write_request.value.insert(write_request.value.end(), bytes.begin(), | |
185 bytes.end()); | |
186 | |
187 bool is_last_write_request = first_chunk_size == serialized_msg.size(); | |
188 write_request.is_last_write_for_wire_message = is_last_write_request; | |
189 WriteRemoteCharacteristic(write_request); | |
190 if (is_last_write_request) | |
191 return; | |
192 | |
193 // [Other write requests]: Each chunk has to include a deprecated signal: | |
194 // |kFirstByteZero| as the first byte. | |
195 int chunk_size = max_chunk_size_ - 1; | |
196 std::vector<uint8_t> kFirstByteZeroVector; | |
197 kFirstByteZeroVector.push_back(static_cast<uint8_t>(kFirstByteZero)); | |
198 | |
199 int message_size = static_cast<int>(serialized_msg.size()); | |
200 int start_index = first_chunk_size; | |
201 while (start_index < message_size) { | |
202 int end_index = (start_index + chunk_size) <= message_size | |
203 ? (start_index + chunk_size) | |
204 : message_size; | |
205 bool is_last_write_request = (end_index == message_size); | |
206 write_request = BuildWriteRequest( | |
207 kFirstByteZeroVector, | |
208 std::vector<uint8_t>(serialized_msg.begin() + start_index, | |
209 serialized_msg.begin() + end_index), | |
210 is_last_write_request); | |
211 WriteRemoteCharacteristic(write_request); | |
212 start_index = end_index; | |
213 } | 173 } |
214 } | 174 } |
215 | 175 |
216 // Changes in the GATT connection with the remote device should be observed | 176 // Changes in the GATT connection with the remote device should be observed |
217 // here. If the GATT connection is dropped, we should call Disconnect() anyway, | 177 // here. If the GATT connection is dropped, we should call Disconnect() anyway, |
218 // so the object can notify its observers. | 178 // so the object can notify its observers. |
219 void BluetoothLowEnergyConnection::DeviceChanged(BluetoothAdapter* adapter, | 179 void BluetoothLowEnergyWeaveConnection::DeviceChanged(BluetoothAdapter* adapter, |
220 BluetoothDevice* device) { | 180 BluetoothDevice* device) { |
221 DCHECK(device); | 181 DCHECK(device); |
222 if (sub_status() == SubStatus::DISCONNECTED || | 182 if (sub_status() == SubStatus::DISCONNECTED || |
223 device->GetAddress() != GetDeviceAddress()) | 183 device->GetAddress() != GetDeviceAddress()) |
224 return; | 184 return; |
225 | 185 |
226 if (sub_status() != SubStatus::WAITING_GATT_CONNECTION && | 186 if (sub_status() != SubStatus::WAITING_GATT_CONNECTION && |
227 !device->IsConnected()) { | 187 !device->IsConnected()) { |
228 PA_LOG(INFO) << "GATT connection dropped " << GetDeviceAddress() | 188 PA_LOG(INFO) << "GATT connection dropped " << GetDeviceAddress() |
229 << "\ndevice connected: " << device->IsConnected() | 189 << "\ndevice connected: " << device->IsConnected() |
230 << "\ngatt connection: " | 190 << "\ngatt connection: " |
231 << (gatt_connection_ ? gatt_connection_->IsConnected() | 191 << (gatt_connection_ ? gatt_connection_->IsConnected() |
232 : false); | 192 : false); |
233 Disconnect(); | 193 Disconnect(); |
234 } | 194 } |
235 } | 195 } |
236 | 196 |
237 void BluetoothLowEnergyConnection::DeviceRemoved(BluetoothAdapter* adapter, | 197 void BluetoothLowEnergyWeaveConnection::DeviceRemoved(BluetoothAdapter* adapter, |
238 BluetoothDevice* device) { | 198 BluetoothDevice* device) { |
239 DCHECK(device); | 199 DCHECK(device); |
240 if (sub_status_ == SubStatus::DISCONNECTED || | 200 if (sub_status_ == SubStatus::DISCONNECTED || |
241 device->GetAddress() != GetDeviceAddress()) | 201 device->GetAddress() != GetDeviceAddress()) |
242 return; | 202 return; |
243 | 203 |
244 PA_LOG(INFO) << "Device removed " << GetDeviceAddress(); | 204 PA_LOG(INFO) << "Device removed " << GetDeviceAddress(); |
245 Disconnect(); | 205 Disconnect(); |
246 } | 206 } |
247 | 207 |
248 void BluetoothLowEnergyConnection::GattCharacteristicValueChanged( | 208 void BluetoothLowEnergyWeaveConnection::GattCharacteristicValueChanged( |
249 BluetoothAdapter* adapter, | 209 BluetoothAdapter* adapter, |
250 BluetoothRemoteGattCharacteristic* characteristic, | 210 BluetoothRemoteGattCharacteristic* characteristic, |
251 const std::vector<uint8_t>& value) { | 211 const std::vector<uint8_t>& value) { |
252 DCHECK_EQ(adapter, adapter_.get()); | 212 DCHECK_EQ(adapter, adapter_.get()); |
253 if (sub_status() != SubStatus::WAITING_RESPONSE_SIGNAL && | 213 if (sub_status() != SubStatus::WAITING_RESPONSE_SIGNAL && |
254 sub_status() != SubStatus::CONNECTED) | 214 sub_status() != SubStatus::CONNECTED) |
255 return; | 215 return; |
256 | 216 |
257 PA_LOG(INFO) << "Characteristic value changed: " | 217 PA_LOG(INFO) << "Characteristic value changed: " |
258 << characteristic->GetUUID().canonical_value(); | 218 << characteristic->GetUUID().canonical_value(); |
259 | 219 |
260 if (characteristic->GetIdentifier() == from_peripheral_char_.id) { | 220 if (characteristic->GetIdentifier() == rx_characteristic_.id) { |
261 if (receiving_bytes_) { | 221 ReceiverState state = packet_receiver_->ReceivePacket(value); |
262 // Ignoring the first byte, as it contains a deprecated signal. | 222 PA_LOG(INFO) << "\nReceiver State: " << state; |
263 const std::string bytes(value.begin() + 1, value.end()); | 223 switch (state) { |
264 incoming_bytes_buffer_.append(bytes); | 224 case ReceiverState::DATA_READY: |
265 if (incoming_bytes_buffer_.size() >= expected_number_of_incoming_bytes_) { | 225 OnBytesReceived(packet_receiver_->GetDataMessage()); |
266 OnBytesReceived(incoming_bytes_buffer_); | 226 break; |
267 receiving_bytes_ = false; | 227 case ReceiverState::CONNECTION_CLOSED: |
268 } | 228 // TODO(jingxuy): what to do with the reason for close? |
269 return; | 229 Disconnect(); |
270 } | 230 break; |
271 | 231 case ReceiverState::ERROR: |
272 if (value.size() < 4) { | 232 // TODO(jingxuy): send a control close over the channel |
273 PA_LOG(WARNING) << "Incoming data corrupted, no signal found."; | 233 Disconnect(); |
274 return; | 234 break; |
275 } | 235 case ReceiverState::WAITING: |
276 | 236 packet_generator_->SetMaxPacketSize( |
277 const ControlSignal signal = static_cast<ControlSignal>(ToUint32(value)); | 237 packet_receiver_->GetMaxPacketSize()); |
278 switch (signal) { | |
279 case ControlSignal::kInvitationResponseSignal: | |
280 if (sub_status() == SubStatus::WAITING_RESPONSE_SIGNAL) | 238 if (sub_status() == SubStatus::WAITING_RESPONSE_SIGNAL) |
281 CompleteConnection(); | 239 CompleteConnection(); |
282 break; | 240 break; |
283 case ControlSignal::kInviteToConnectSignal: | 241 case ReceiverState::CONNECTING: |
| 242 case ReceiverState::RECEIVING_DATA: |
| 243 // Normal in between states, so do nothing. |
284 break; | 244 break; |
285 case ControlSignal::kSendSignal: { | 245 default: |
286 if (value.size() < 8) { | 246 NOTREACHED(); |
287 PA_LOG(WARNING) | |
288 << "Incoming data corrupted, expected message size not found."; | |
289 return; | |
290 } | |
291 std::vector<uint8_t> size(value.begin() + 4, value.begin() + 8); | |
292 expected_number_of_incoming_bytes_ = | |
293 static_cast<size_t>(ToUint32(size)); | |
294 receiving_bytes_ = true; | |
295 incoming_bytes_buffer_.clear(); | |
296 | |
297 const std::string bytes(value.begin() + 8, value.end()); | |
298 incoming_bytes_buffer_.append(bytes); | |
299 if (incoming_bytes_buffer_.size() >= | |
300 expected_number_of_incoming_bytes_) { | |
301 OnBytesReceived(incoming_bytes_buffer_); | |
302 receiving_bytes_ = false; | |
303 } | |
304 break; | |
305 } | |
306 case ControlSignal::kDisconnectSignal: | |
307 PA_LOG(INFO) << "Disconnect signal received."; | |
308 Disconnect(); | |
309 break; | |
310 } | 247 } |
311 } | 248 } |
312 } | 249 } |
313 | 250 |
314 BluetoothLowEnergyConnection::WriteRequest::WriteRequest( | 251 BluetoothLowEnergyWeaveConnection::WriteRequest::WriteRequest( |
315 const std::vector<uint8_t>& val, | 252 const std::vector<uint8_t>& val, |
316 bool flag) | 253 bool flag) |
317 : value(val), | 254 : value(val), |
318 is_last_write_for_wire_message(flag), | 255 is_last_write_for_wire_message(flag), |
319 number_of_failed_attempts(0) {} | 256 number_of_failed_attempts(0) {} |
320 | 257 |
321 BluetoothLowEnergyConnection::WriteRequest::WriteRequest( | 258 BluetoothLowEnergyWeaveConnection::WriteRequest::WriteRequest( |
322 const WriteRequest& other) = default; | 259 const WriteRequest& other) = default; |
323 | 260 |
324 BluetoothLowEnergyConnection::WriteRequest::~WriteRequest() {} | 261 BluetoothLowEnergyWeaveConnection::WriteRequest::~WriteRequest() {} |
325 | 262 |
326 void BluetoothLowEnergyConnection::CompleteConnection() { | 263 void BluetoothLowEnergyWeaveConnection::CompleteConnection() { |
327 PA_LOG(INFO) << "Connection completed. Time elapsed: " | 264 PA_LOG(INFO) << "Connection completed. Time elapsed: " |
328 << base::TimeTicks::Now() - start_time_; | 265 << base::TimeTicks::Now() - start_time_; |
329 SetSubStatus(SubStatus::CONNECTED); | 266 SetSubStatus(SubStatus::CONNECTED); |
330 } | 267 } |
331 | 268 |
332 void BluetoothLowEnergyConnection::OnCreateGattConnectionError( | 269 void BluetoothLowEnergyWeaveConnection::OnCreateGattConnectionError( |
333 device::BluetoothDevice::ConnectErrorCode error_code) { | 270 device::BluetoothDevice::ConnectErrorCode error_code) { |
334 DCHECK(sub_status_ == SubStatus::WAITING_GATT_CONNECTION); | 271 DCHECK(sub_status_ == SubStatus::WAITING_GATT_CONNECTION); |
335 PA_LOG(WARNING) << "Error creating GATT connection to " | 272 PA_LOG(WARNING) << "Error creating GATT connection to " |
336 << remote_device().bluetooth_address | 273 << remote_device().bluetooth_address |
337 << "error code: " << error_code; | 274 << "error code: " << error_code; |
338 Disconnect(); | 275 Disconnect(); |
339 } | 276 } |
340 | 277 |
341 void BluetoothLowEnergyConnection::OnGattConnectionCreated( | 278 void BluetoothLowEnergyWeaveConnection::OnGattConnectionCreated( |
342 std::unique_ptr<device::BluetoothGattConnection> gatt_connection) { | 279 std::unique_ptr<device::BluetoothGattConnection> gatt_connection) { |
343 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); | 280 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); |
344 PA_LOG(INFO) << "GATT connection with " << gatt_connection->GetDeviceAddress() | 281 PA_LOG(INFO) << "GATT connection with " << gatt_connection->GetDeviceAddress() |
345 << " created."; | 282 << " created."; |
346 PrintTimeElapsed(); | 283 PrintTimeElapsed(); |
347 | 284 |
348 // Informing |bluetooth_trottler_| a new connection was established. | 285 // Informing |bluetooth_trottler_| a new connection was established. |
349 bluetooth_throttler_->OnConnection(this); | 286 bluetooth_throttler_->OnConnection(this); |
350 | 287 |
351 gatt_connection_ = std::move(gatt_connection); | 288 gatt_connection_ = std::move(gatt_connection); |
352 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); | 289 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); |
353 characteristic_finder_.reset(CreateCharacteristicsFinder( | 290 characteristic_finder_.reset(CreateCharacteristicsFinder( |
354 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFound, | 291 base::Bind(&BluetoothLowEnergyWeaveConnection::OnCharacteristicsFound, |
355 weak_ptr_factory_.GetWeakPtr()), | 292 weak_ptr_factory_.GetWeakPtr()), |
356 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFinderError, | 293 base::Bind( |
357 weak_ptr_factory_.GetWeakPtr()))); | 294 &BluetoothLowEnergyWeaveConnection::OnCharacteristicsFinderError, |
| 295 weak_ptr_factory_.GetWeakPtr()))); |
358 } | 296 } |
359 | 297 |
360 BluetoothLowEnergyCharacteristicsFinder* | 298 BluetoothLowEnergyCharacteristicsFinder* |
361 BluetoothLowEnergyConnection::CreateCharacteristicsFinder( | 299 BluetoothLowEnergyWeaveConnection::CreateCharacteristicsFinder( |
362 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | 300 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& |
363 success_callback, | 301 success_callback, |
364 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& | 302 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& |
365 error_callback) { | 303 error_callback) { |
366 return new BluetoothLowEnergyCharacteristicsFinder( | 304 return new BluetoothLowEnergyCharacteristicsFinder( |
367 adapter_, GetRemoteDevice(), remote_service_, to_peripheral_char_, | 305 adapter_, GetRemoteDevice(), remote_service_, tx_characteristic_, |
368 from_peripheral_char_, success_callback, error_callback); | 306 rx_characteristic_, success_callback, error_callback); |
369 } | 307 } |
370 | 308 |
371 void BluetoothLowEnergyConnection::OnCharacteristicsFound( | 309 void BluetoothLowEnergyWeaveConnection::OnCharacteristicsFound( |
372 const RemoteAttribute& service, | 310 const RemoteAttribute& service, |
373 const RemoteAttribute& to_peripheral_char, | 311 const RemoteAttribute& tx_characteristic, |
374 const RemoteAttribute& from_peripheral_char) { | 312 const RemoteAttribute& rx_characteristic) { |
375 PA_LOG(INFO) << "Remote chacteristics found."; | 313 PA_LOG(INFO) << "Remote chacteristics found."; |
376 PrintTimeElapsed(); | 314 PrintTimeElapsed(); |
377 | 315 |
378 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); | 316 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); |
379 remote_service_ = service; | 317 remote_service_ = service; |
380 to_peripheral_char_ = to_peripheral_char; | 318 tx_characteristic_ = tx_characteristic; |
381 from_peripheral_char_ = from_peripheral_char; | 319 rx_characteristic_ = rx_characteristic; |
382 | 320 |
383 SetSubStatus(SubStatus::CHARACTERISTICS_FOUND); | 321 SetSubStatus(SubStatus::CHARACTERISTICS_FOUND); |
384 StartNotifySession(); | 322 StartNotifySession(); |
385 } | 323 } |
386 | 324 |
387 void BluetoothLowEnergyConnection::OnCharacteristicsFinderError( | 325 void BluetoothLowEnergyWeaveConnection::OnCharacteristicsFinderError( |
388 const RemoteAttribute& to_peripheral_char, | 326 const RemoteAttribute& tx_characteristic, |
389 const RemoteAttribute& from_peripheral_char) { | 327 const RemoteAttribute& rx_characteristic) { |
390 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); | 328 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); |
391 PA_LOG(WARNING) << "Connection error, missing characteristics for SmartLock " | 329 PA_LOG(WARNING) << "Connection error, missing characteristics for SmartLock " |
392 "service.\n" | 330 "service.\n" |
393 << (to_peripheral_char.id.empty() | 331 << (tx_characteristic.id.empty() |
394 ? to_peripheral_char.uuid.canonical_value() | 332 ? tx_characteristic.uuid.canonical_value() |
395 : "") | 333 : "") |
396 << (from_peripheral_char.id.empty() | 334 << (rx_characteristic.id.empty() |
397 ? ", " + from_peripheral_char.uuid.canonical_value() | 335 ? ", " + rx_characteristic.uuid.canonical_value() |
398 : "") << " not found."; | 336 : "") |
| 337 << " not found."; |
399 | 338 |
400 Disconnect(); | 339 Disconnect(); |
401 } | 340 } |
402 | 341 |
403 void BluetoothLowEnergyConnection::StartNotifySession() { | 342 void BluetoothLowEnergyWeaveConnection::StartNotifySession() { |
404 if (sub_status() == SubStatus::CHARACTERISTICS_FOUND) { | 343 if (sub_status() == SubStatus::CHARACTERISTICS_FOUND) { |
405 BluetoothRemoteGattCharacteristic* characteristic = | 344 BluetoothRemoteGattCharacteristic* characteristic = |
406 GetGattCharacteristic(from_peripheral_char_.id); | 345 GetGattCharacteristic(rx_characteristic_.id); |
407 DCHECK(characteristic); | 346 DCHECK(characteristic); |
408 | 347 |
409 // This is a workaround for crbug.com/507325. If |characteristic| is already | 348 // This is a workaround for crbug.com/507325. If |characteristic| is already |
410 // notifying |characteristic->StartNotifySession()| will fail with | 349 // notifying |characteristic->StartNotifySession()| will fail with |
411 // GATT_ERROR_FAILED. | 350 // GATT_ERROR_FAILED. |
412 if (characteristic->IsNotifying()) { | 351 if (characteristic->IsNotifying()) { |
413 PA_LOG(INFO) << characteristic->GetUUID().canonical_value() | 352 PA_LOG(INFO) << characteristic->GetUUID().canonical_value() |
414 << " already notifying."; | 353 << " already notifying."; |
415 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); | 354 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); |
416 SendInviteToConnectSignal(); | 355 SendInviteToConnectSignal(); |
417 return; | 356 return; |
418 } | 357 } |
419 | 358 |
420 SetSubStatus(SubStatus::WAITING_NOTIFY_SESSION); | 359 SetSubStatus(SubStatus::WAITING_NOTIFY_SESSION); |
421 characteristic->StartNotifySession( | 360 characteristic->StartNotifySession( |
422 base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionStarted, | 361 base::Bind(&BluetoothLowEnergyWeaveConnection::OnNotifySessionStarted, |
423 weak_ptr_factory_.GetWeakPtr()), | 362 weak_ptr_factory_.GetWeakPtr()), |
424 base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionError, | 363 base::Bind(&BluetoothLowEnergyWeaveConnection::OnNotifySessionError, |
425 weak_ptr_factory_.GetWeakPtr())); | 364 weak_ptr_factory_.GetWeakPtr())); |
426 } | 365 } |
427 } | 366 } |
428 | 367 |
429 void BluetoothLowEnergyConnection::OnNotifySessionError( | 368 void BluetoothLowEnergyWeaveConnection::OnNotifySessionStarted( |
430 BluetoothRemoteGattService::GattErrorCode error) { | |
431 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); | |
432 PA_LOG(WARNING) << "Error starting notification session: " << error; | |
433 Disconnect(); | |
434 } | |
435 | |
436 void BluetoothLowEnergyConnection::OnNotifySessionStarted( | |
437 std::unique_ptr<BluetoothGattNotifySession> notify_session) { | 369 std::unique_ptr<BluetoothGattNotifySession> notify_session) { |
438 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); | 370 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); |
439 PA_LOG(INFO) << "Notification session started " | 371 PA_LOG(INFO) << "Notification session started " |
440 << notify_session->GetCharacteristicIdentifier(); | 372 << notify_session->GetCharacteristicIdentifier(); |
441 PrintTimeElapsed(); | 373 PrintTimeElapsed(); |
442 | 374 |
443 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); | 375 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); |
444 notify_session_ = std::move(notify_session); | 376 notify_session_ = std::move(notify_session); |
445 | 377 |
446 SendInviteToConnectSignal(); | 378 SendInviteToConnectSignal(); |
447 } | 379 } |
448 | 380 |
449 void BluetoothLowEnergyConnection::StopNotifySession() { | 381 void BluetoothLowEnergyWeaveConnection::OnNotifySessionError( |
| 382 BluetoothRemoteGattService::GattErrorCode error) { |
| 383 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); |
| 384 PA_LOG(WARNING) << "Error starting notification session: " << error; |
| 385 Disconnect(); |
| 386 } |
| 387 |
| 388 void BluetoothLowEnergyWeaveConnection::StopNotifySession() { |
450 if (notify_session_) { | 389 if (notify_session_) { |
451 notify_session_->Stop(base::Bind(&base::DoNothing)); | 390 notify_session_->Stop(base::Bind(&base::DoNothing)); |
452 notify_session_.reset(); | 391 notify_session_.reset(); |
453 } | 392 } |
454 } | 393 } |
455 | 394 |
456 void BluetoothLowEnergyConnection::SendInviteToConnectSignal() { | 395 void BluetoothLowEnergyWeaveConnection::SendInviteToConnectSignal() { |
457 if (sub_status() == SubStatus::NOTIFY_SESSION_READY) { | 396 if (sub_status() == SubStatus::NOTIFY_SESSION_READY) { |
458 PA_LOG(INFO) << "Sending invite to connect signal"; | 397 PA_LOG(INFO) << "Sending invite to connect signal"; |
459 SetSubStatus(SubStatus::WAITING_RESPONSE_SIGNAL); | 398 SetSubStatus(SubStatus::WAITING_RESPONSE_SIGNAL); |
460 | 399 |
461 WriteRequest write_request = BuildWriteRequest( | 400 WriteRequest write_request = |
462 ToByteVector( | 401 WriteRequest(packet_generator_->CreateConnectionRequest(), false); |
463 static_cast<uint32_t>(ControlSignal::kInviteToConnectSignal)), | |
464 std::vector<uint8_t>(), false); | |
465 | 402 |
466 WriteRemoteCharacteristic(write_request); | 403 WriteRemoteCharacteristic(write_request); |
467 } | 404 } |
468 } | 405 } |
469 | 406 |
470 void BluetoothLowEnergyConnection::WriteRemoteCharacteristic( | 407 void BluetoothLowEnergyWeaveConnection::WriteRemoteCharacteristic( |
471 WriteRequest request) { | 408 const WriteRequest& request) { |
472 write_requests_queue_.push(request); | 409 write_requests_queue_.push(request); |
473 ProcessNextWriteRequest(); | 410 ProcessNextWriteRequest(); |
474 } | 411 } |
475 | 412 |
476 void BluetoothLowEnergyConnection::ProcessNextWriteRequest() { | 413 void BluetoothLowEnergyWeaveConnection::ProcessNextWriteRequest() { |
477 BluetoothRemoteGattCharacteristic* characteristic = | 414 BluetoothRemoteGattCharacteristic* characteristic = |
478 GetGattCharacteristic(to_peripheral_char_.id); | 415 GetGattCharacteristic(tx_characteristic_.id); |
479 if (!write_requests_queue_.empty() && !write_remote_characteristic_pending_ && | 416 if (!write_requests_queue_.empty() && !write_remote_characteristic_pending_ && |
480 characteristic) { | 417 characteristic) { |
481 write_remote_characteristic_pending_ = true; | 418 write_remote_characteristic_pending_ = true; |
482 WriteRequest next_request = write_requests_queue_.front(); | 419 WriteRequest next_request = write_requests_queue_.front(); |
483 PA_LOG(INFO) << "Writing characteristic..."; | 420 PA_LOG(INFO) << "Writing characteristic..."; |
484 characteristic->WriteRemoteCharacteristic( | 421 characteristic->WriteRemoteCharacteristic( |
485 next_request.value, | 422 next_request.value, |
486 base::Bind(&BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten, | 423 base::Bind( |
| 424 &BluetoothLowEnergyWeaveConnection::OnRemoteCharacteristicWritten, |
| 425 weak_ptr_factory_.GetWeakPtr(), |
| 426 next_request.is_last_write_for_wire_message), |
| 427 base::Bind(&BluetoothLowEnergyWeaveConnection:: |
| 428 OnWriteRemoteCharacteristicError, |
487 weak_ptr_factory_.GetWeakPtr(), | 429 weak_ptr_factory_.GetWeakPtr(), |
488 next_request.is_last_write_for_wire_message), | 430 next_request.is_last_write_for_wire_message)); |
489 base::Bind( | |
490 &BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError, | |
491 weak_ptr_factory_.GetWeakPtr(), | |
492 next_request.is_last_write_for_wire_message)); | |
493 } | 431 } |
494 } | 432 } |
495 | 433 |
496 void BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten( | 434 void BluetoothLowEnergyWeaveConnection::OnRemoteCharacteristicWritten( |
497 bool run_did_send_message_callback) { | 435 bool run_did_send_message_callback) { |
498 PA_LOG(INFO) << "Characteristic written."; | 436 PA_LOG(INFO) << "Characteristic written."; |
499 write_remote_characteristic_pending_ = false; | 437 write_remote_characteristic_pending_ = false; |
500 // TODO(sacomoto): Actually pass the current message to the observer. | 438 // TODO(sacomoto): Actually pass the current message to the observer. |
501 if (run_did_send_message_callback) | 439 if (run_did_send_message_callback) |
502 OnDidSendMessage(WireMessage(std::string(), std::string()), true); | 440 OnDidSendMessage(WireMessage(std::string(), std::string()), true); |
503 | 441 |
504 // Removes the top of queue (already processed) and process the next request. | 442 // Removes the top of queue (already processed) and process the next request. |
505 DCHECK(!write_requests_queue_.empty()); | 443 DCHECK(!write_requests_queue_.empty()); |
506 write_requests_queue_.pop(); | 444 write_requests_queue_.pop(); |
507 ProcessNextWriteRequest(); | 445 ProcessNextWriteRequest(); |
508 } | 446 } |
509 | 447 |
510 void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError( | 448 void BluetoothLowEnergyWeaveConnection::OnWriteRemoteCharacteristicError( |
511 bool run_did_send_message_callback, | 449 bool run_did_send_message_callback, |
512 BluetoothRemoteGattService::GattErrorCode error) { | 450 BluetoothRemoteGattService::GattErrorCode error) { |
513 PA_LOG(WARNING) << "Error " << error << " writing characteristic: " | 451 PA_LOG(WARNING) << "Error " << error << " writing characteristic: " |
514 << to_peripheral_char_.uuid.canonical_value(); | 452 << tx_characteristic_.uuid.canonical_value(); |
515 write_remote_characteristic_pending_ = false; | 453 write_remote_characteristic_pending_ = false; |
516 // TODO(sacomoto): Actually pass the current message to the observer. | 454 // TODO(sacomoto): Actually pass the current message to the observer. |
517 if (run_did_send_message_callback) | 455 if (run_did_send_message_callback) |
518 OnDidSendMessage(WireMessage(std::string(), std::string()), false); | 456 OnDidSendMessage(WireMessage(std::string(), std::string()), false); |
519 | 457 |
520 // Increases the number of failed attempts and retry. | 458 // Increases the number of failed attempts and retry. |
521 DCHECK(!write_requests_queue_.empty()); | 459 DCHECK(!write_requests_queue_.empty()); |
522 if (++write_requests_queue_.front().number_of_failed_attempts >= | 460 if (++write_requests_queue_.front().number_of_failed_attempts >= |
523 max_number_of_write_attempts_) { | 461 max_number_of_write_attempts_) { |
524 Disconnect(); | 462 Disconnect(); |
525 return; | 463 return; |
526 } | 464 } |
527 ProcessNextWriteRequest(); | 465 ProcessNextWriteRequest(); |
528 } | 466 } |
529 | 467 |
530 BluetoothLowEnergyConnection::WriteRequest | 468 void BluetoothLowEnergyWeaveConnection::PrintTimeElapsed() { |
531 BluetoothLowEnergyConnection::BuildWriteRequest( | |
532 const std::vector<uint8_t>& signal, | |
533 const std::vector<uint8_t>& bytes, | |
534 bool is_last_write_for_wire_message) { | |
535 std::vector<uint8_t> value(signal.begin(), signal.end()); | |
536 value.insert(value.end(), bytes.begin(), bytes.end()); | |
537 return WriteRequest(value, is_last_write_for_wire_message); | |
538 } | |
539 | |
540 void BluetoothLowEnergyConnection::PrintTimeElapsed() { | |
541 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; | 469 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; |
542 } | 470 } |
543 | 471 |
544 std::string BluetoothLowEnergyConnection::GetDeviceAddress() { | 472 std::string BluetoothLowEnergyWeaveConnection::GetDeviceAddress() { |
545 // When the remote device is connected we should rely on the address given by | 473 // When the remote device is connected we should rely on the address given by |
546 // |gatt_connection_|. As the device address may change if the device is | 474 // |gatt_connection_|. As the device address may change if the device is |
547 // paired. The address in |gatt_connection_| is automatically updated in this | 475 // paired. The address in |gatt_connection_| is automatically updated in this |
548 // case. | 476 // case. |
549 return gatt_connection_ ? gatt_connection_->GetDeviceAddress() | 477 return gatt_connection_ ? gatt_connection_->GetDeviceAddress() |
550 : remote_device().bluetooth_address; | 478 : remote_device().bluetooth_address; |
551 } | 479 } |
552 | 480 |
553 BluetoothDevice* BluetoothLowEnergyConnection::GetRemoteDevice() { | 481 BluetoothDevice* BluetoothLowEnergyWeaveConnection::GetRemoteDevice() { |
554 // It's not possible to simply use | 482 // It's not possible to simply use |
555 // |adapter_->GetDevice(GetDeviceAddress())| to find the device with MAC | 483 // |adapter_->GetDevice(GetDeviceAddress())| to find the device with MAC |
556 // address |GetDeviceAddress()|. For paired devices, | 484 // address |GetDeviceAddress()|. For paired devices, |
557 // BluetoothAdapter::GetDevice(XXX) searches for the temporary MAC address | 485 // BluetoothAdapter::GetDevice(XXX) searches for the temporary MAC address |
558 // XXX, whereas |GetDeviceAddress()| is the real MAC address. This is a | 486 // XXX, whereas |GetDeviceAddress()| is the real MAC address. This is a |
559 // bug in the way device::BluetoothAdapter is storing the devices (see | 487 // bug in the way device::BluetoothAdapter is storing the devices (see |
560 // crbug.com/497841). | 488 // crbug.com/497841). |
561 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); | 489 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); |
562 for (const auto& device : devices) { | 490 for (const auto& device : devices) { |
563 if (device->GetAddress() == GetDeviceAddress()) | 491 if (device->GetAddress() == GetDeviceAddress()) |
564 return device; | 492 return device; |
565 } | 493 } |
566 | 494 |
567 return nullptr; | 495 return nullptr; |
568 } | 496 } |
569 | 497 |
570 BluetoothRemoteGattService* BluetoothLowEnergyConnection::GetRemoteService() { | 498 BluetoothRemoteGattService* |
| 499 BluetoothLowEnergyWeaveConnection::GetRemoteService() { |
571 BluetoothDevice* remote_device = GetRemoteDevice(); | 500 BluetoothDevice* remote_device = GetRemoteDevice(); |
572 if (!remote_device) { | 501 if (!remote_device) { |
573 PA_LOG(WARNING) << "Remote device not found."; | 502 PA_LOG(WARNING) << "Remote device not found."; |
574 return NULL; | 503 return NULL; |
575 } | 504 } |
576 if (remote_service_.id.empty()) { | 505 if (remote_service_.id.empty()) { |
577 std::vector<BluetoothRemoteGattService*> services = | 506 std::vector<BluetoothRemoteGattService*> services = |
578 remote_device->GetGattServices(); | 507 remote_device->GetGattServices(); |
579 for (const auto& service : services) | 508 for (const auto& service : services) |
580 if (service->GetUUID() == remote_service_.uuid) { | 509 if (service->GetUUID() == remote_service_.uuid) { |
581 remote_service_.id = service->GetIdentifier(); | 510 remote_service_.id = service->GetIdentifier(); |
582 break; | 511 break; |
583 } | 512 } |
584 } | 513 } |
585 return remote_device->GetGattService(remote_service_.id); | 514 return remote_device->GetGattService(remote_service_.id); |
586 } | 515 } |
587 | 516 |
588 BluetoothRemoteGattCharacteristic* | 517 BluetoothRemoteGattCharacteristic* |
589 BluetoothLowEnergyConnection::GetGattCharacteristic( | 518 BluetoothLowEnergyWeaveConnection::GetGattCharacteristic( |
590 const std::string& gatt_characteristic) { | 519 const std::string& gatt_characteristic) { |
591 BluetoothRemoteGattService* remote_service = GetRemoteService(); | 520 BluetoothRemoteGattService* remote_service = GetRemoteService(); |
592 if (!remote_service) { | 521 if (!remote_service) { |
593 PA_LOG(WARNING) << "Remote service not found."; | 522 PA_LOG(WARNING) << "Remote service not found."; |
594 return NULL; | 523 return NULL; |
595 } | 524 } |
596 return remote_service->GetCharacteristic(gatt_characteristic); | 525 return remote_service->GetCharacteristic(gatt_characteristic); |
597 } | 526 } |
598 | 527 |
599 // TODO(sacomoto): make this robust to byte ordering in both sides of the | 528 // TODO(sacomoto): make this robust to byte ordering in both sides of the |
600 // SmartLock BLE socket. | 529 // SmartLock BLE socket. |
601 uint32_t BluetoothLowEnergyConnection::ToUint32( | 530 uint32_t BluetoothLowEnergyWeaveConnection::ToUint32( |
602 const std::vector<uint8_t>& bytes) { | 531 const std::vector<uint8_t>& bytes) { |
603 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); | 532 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); |
604 } | 533 } |
605 | 534 |
606 // TODO(sacomoto): make this robust to byte ordering in both sides of the | 535 // TODO(sacomoto): make this robust to byte ordering in both sides of the |
607 // SmartLock BLE socket. | 536 // SmartLock BLE socket. |
608 const std::vector<uint8_t> BluetoothLowEnergyConnection::ToByteVector( | 537 const std::vector<uint8_t> BluetoothLowEnergyWeaveConnection::ToByteVector( |
609 const uint32_t value) { | 538 const uint32_t value) { |
610 std::vector<uint8_t> bytes(4, 0); | 539 std::vector<uint8_t> bytes(4, 0); |
611 bytes[0] = static_cast<uint8_t>(value); | 540 bytes[0] = static_cast<uint8_t>(value); |
612 bytes[1] = static_cast<uint8_t>(value >> 8); | 541 bytes[1] = static_cast<uint8_t>(value >> 8); |
613 bytes[2] = static_cast<uint8_t>(value >> 16); | 542 bytes[2] = static_cast<uint8_t>(value >> 16); |
614 bytes[3] = static_cast<uint8_t>(value >> 24); | 543 bytes[3] = static_cast<uint8_t>(value >> 24); |
615 return bytes; | 544 return bytes; |
616 } | 545 } |
617 | 546 |
618 } // namespace proximity_auth | 547 } // namespace proximity_auth |
OLD | NEW |