Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_client_connec tion.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/task_runner.h" | |
| 12 #include "base/threading/thread_task_runner_handle.h" | |
| 13 #include "components/proximity_auth/bluetooth_throttler.h" | |
| 14 #include "components/proximity_auth/connection_finder.h" | |
| 15 #include "components/proximity_auth/logging/logging.h" | |
| 16 #include "components/proximity_auth/wire_message.h" | |
| 17 #include "device/bluetooth/bluetooth_gatt_connection.h" | |
| 18 | |
| 19 using device::BluetoothAdapter; | |
| 20 using device::BluetoothDevice; | |
| 21 using device::BluetoothGattConnection; | |
| 22 using device::BluetoothRemoteGattService; | |
| 23 using device::BluetoothRemoteGattCharacteristic; | |
| 24 using device::BluetoothGattNotifySession; | |
| 25 using device::BluetoothUUID; | |
| 26 | |
| 27 namespace proximity_auth { | |
| 28 namespace weave { | |
| 29 namespace { | |
| 30 | |
| 31 typedef BluetoothLowEnergyWeavePacketReceiver::State ReceiverState; | |
| 32 | |
| 33 const int kDefaultMessageCounter = 0; | |
| 34 | |
| 35 // The UUID of the TX characteristic used to transmit data to the server. | |
| 36 const char kTXCharacteristicUUID[] = "00000100-0004-1000-8000-001A11000101"; | |
| 37 | |
| 38 // The UUID of the RX characteristic used to receive data from the server. | |
| 39 const char kRXCharacteristicUUID[] = "00000100-0004-1000-8000-001A11000102"; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 BluetoothLowEnergyWeaveClientConnection:: | |
| 44 BluetoothLowEnergyWeaveClientConnection( | |
| 45 const RemoteDevice& device, | |
| 46 scoped_refptr<device::BluetoothAdapter> adapter, | |
| 47 const BluetoothUUID remote_service_uuid, | |
| 48 BluetoothThrottler* bluetooth_throttler, | |
| 49 int max_number_of_write_attempts) | |
| 50 : Connection(device), | |
| 51 adapter_(adapter), | |
| 52 remote_service_({remote_service_uuid, ""}), | |
| 53 packet_generator_( | |
| 54 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance()), | |
| 55 packet_receiver_( | |
| 56 BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance( | |
| 57 BluetoothLowEnergyWeavePacketReceiver::ReceiverType::CLIENT)), | |
| 58 tx_characteristic_({BluetoothUUID(kTXCharacteristicUUID), ""}), | |
| 59 rx_characteristic_({BluetoothUUID(kRXCharacteristicUUID), ""}), | |
| 60 bluetooth_throttler_(bluetooth_throttler), | |
| 61 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 62 sub_status_(SubStatus::DISCONNECTED), | |
| 63 write_remote_characteristic_pending_(false), | |
| 64 message_counter_(kDefaultMessageCounter + 1), | |
| 65 max_number_of_write_attempts_(max_number_of_write_attempts), | |
| 66 weak_ptr_factory_(this) { | |
| 67 DCHECK(adapter_); | |
| 68 DCHECK(adapter_->IsInitialized()); | |
| 69 | |
| 70 map_to_message_[kDefaultMessageCounter] = | |
|
Kyle Horimoto
2016/07/12 01:03:12
Comment about why this is needed (non-data packets
jingxuy
2016/07/15 18:24:59
It's actually not necessary
| |
| 71 std::unique_ptr<WireMessage>(nullptr); | |
|
Kyle Horimoto
2016/07/12 01:03:14
You should probably use an empty string instead of
jingxuy
2016/07/15 18:24:59
This line is deleted
| |
| 72 adapter_->AddObserver(this); | |
| 73 } | |
| 74 | |
| 75 BluetoothLowEnergyWeaveClientConnection:: | |
| 76 ~BluetoothLowEnergyWeaveClientConnection() { | |
| 77 // Since the destructor can be called at anytime, it would be unwise to send a | |
| 78 // connection since we might not be connected at all. | |
| 79 DestroyConnection(); | |
| 80 | |
| 81 if (adapter_) { | |
| 82 adapter_->RemoveObserver(this); | |
| 83 adapter_ = NULL; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 void BluetoothLowEnergyWeaveClientConnection::Connect() { | |
| 88 DCHECK(sub_status() == SubStatus::DISCONNECTED); | |
| 89 | |
| 90 SetSubStatus(SubStatus::WAITING_GATT_CONNECTION); | |
| 91 base::TimeDelta throttler_delay = bluetooth_throttler_->GetDelay(); | |
| 92 PA_LOG(INFO) << "Connecting in " << throttler_delay; | |
| 93 | |
| 94 start_time_ = base::TimeTicks::Now(); | |
| 95 | |
| 96 // If necessary, wait to create a new GATT connection. | |
| 97 // | |
| 98 // Avoid creating a new GATT connection immediately after a given device was | |
| 99 // disconnected. This is a workaround for crbug.com/508919. | |
| 100 if (!throttler_delay.is_zero()) { | |
| 101 task_runner_->PostDelayedTask( | |
| 102 FROM_HERE, | |
| 103 base::Bind( | |
| 104 &BluetoothLowEnergyWeaveClientConnection::CreateGattConnection, | |
| 105 weak_ptr_factory_.GetWeakPtr()), | |
| 106 throttler_delay); | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 CreateGattConnection(); | |
| 111 } | |
| 112 | |
| 113 void BluetoothLowEnergyWeaveClientConnection::CreateGattConnection() { | |
| 114 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); | |
| 115 | |
| 116 BluetoothDevice* remote_device = GetRemoteDevice(); | |
| 117 if (remote_device) { | |
| 118 PA_LOG(INFO) << "Creating GATT connection with " | |
| 119 << remote_device->GetAddress(); | |
| 120 | |
| 121 remote_device->CreateGattConnection( | |
| 122 base::Bind( | |
| 123 &BluetoothLowEnergyWeaveClientConnection::OnGattConnectionCreated, | |
| 124 weak_ptr_factory_.GetWeakPtr()), | |
| 125 base::Bind(&BluetoothLowEnergyWeaveClientConnection:: | |
| 126 OnCreateGattConnectionError, | |
| 127 weak_ptr_factory_.GetWeakPtr())); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 void BluetoothLowEnergyWeaveClientConnection::Disconnect() { | |
| 132 if (sub_status_ == SubStatus::CONNECTED) { | |
| 133 // Friendly disconnect by sending a connection close and then destroy the | |
| 134 // the connection once the connection close has been sent. | |
| 135 WriteRequest request(packet_generator_->CreateConnectionClose( | |
| 136 ReasonForClose::CLOSE_WITHOUT_ERROR), | |
| 137 WriteRequestType::CONNECTION_CLOSE); | |
| 138 WriteRemoteCharacteristic(request); | |
| 139 } else { | |
| 140 DestroyConnection(); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void BluetoothLowEnergyWeaveClientConnection::DestroyConnection() { | |
| 145 if (sub_status() != SubStatus::DISCONNECTED) { | |
| 146 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 147 StopNotifySession(); | |
| 148 characteristic_finder_.reset(); | |
| 149 if (gatt_connection_) { | |
| 150 PA_LOG(INFO) << "Disconnect from device " | |
| 151 << gatt_connection_->GetDeviceAddress(); | |
| 152 | |
| 153 // Destroying BluetoothGattConnection also disconnects it. | |
| 154 gatt_connection_.reset(); | |
| 155 } | |
| 156 | |
| 157 // Only transition to the DISCONNECTED state after perfoming all necessary | |
| 158 // operations. Otherwise, it'll trigger observers that can pontentially | |
| 159 // destroy the current instance (causing a crash). | |
| 160 SetSubStatus(SubStatus::DISCONNECTED); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 void BluetoothLowEnergyWeaveClientConnection::SetSubStatus( | |
| 165 SubStatus new_sub_status) { | |
| 166 sub_status_ = new_sub_status; | |
| 167 | |
| 168 // Sets the status of parent class proximity_auth::Connection accordingly. | |
| 169 if (new_sub_status == SubStatus::CONNECTED) { | |
| 170 SetStatus(Status::CONNECTED); | |
| 171 } else if (new_sub_status == SubStatus::DISCONNECTED) { | |
| 172 SetStatus(Status::DISCONNECTED); | |
| 173 } else { | |
| 174 SetStatus(Status::IN_PROGRESS); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 void BluetoothLowEnergyWeaveClientConnection::SetTaskRunnerForTesting( | |
| 179 scoped_refptr<base::TaskRunner> task_runner) { | |
| 180 task_runner_ = task_runner; | |
| 181 } | |
| 182 | |
| 183 void BluetoothLowEnergyWeaveClientConnection::SendMessageImpl( | |
| 184 std::unique_ptr<WireMessage> message) { | |
| 185 PA_LOG(INFO) << "Sending message " << message->Serialize(); | |
| 186 std::string serialized_msg = message->Serialize(); | |
| 187 | |
| 188 map_to_message_.insert(std::pair<int, std::unique_ptr<WireMessage>>( | |
| 189 message_counter_, std::move(message))); | |
|
Kyle Horimoto
2016/07/12 01:03:12
You also need to remove things from the map when t
Kyle Horimoto
2016/07/12 01:03:12
This won't work correctly once the WireMessage is
jingxuy
2016/07/15 18:24:59
Done.
jingxuy
2016/07/15 18:24:59
The wire_message is passed in as unique_ptr which
Kyle Horimoto
2016/07/15 20:20:43
I didn't mean that the client who passed in the un
Kyle Horimoto
2016/07/15 20:20:43
Please expose map_to_message_ as a protected varia
jingxuy
2016/07/15 21:30:06
There could be only one object that owns the point
jingxuy
2016/07/15 21:30:06
TODO
Kyle Horimoto
2016/07/15 21:54:54
Thanks for the link! Didn't know this before. Can
jingxuy
2016/07/16 00:14:06
Done.
jingxuy
2016/07/16 00:14:06
Done.
| |
| 190 | |
| 191 int message_counter = message_counter_; | |
|
Kyle Horimoto
2016/07/12 01:03:12
current_message_counter
Also, move these 2 lines
jingxuy
2016/07/15 18:24:59
Done.
| |
| 192 message_counter_++; | |
| 193 | |
| 194 std::vector<Packet> packets = | |
| 195 packet_generator_->EncodeDataMessage(serialized_msg); | |
| 196 | |
| 197 for (uint32_t i = 0; i < packets.size(); ++i) { | |
| 198 WriteRequestType request_type = (i == packets.size() - 1) | |
| 199 ? WriteRequestType::MESSAGE_COMPLETE | |
| 200 : WriteRequestType::REGULAR; | |
| 201 WriteRequest request = | |
| 202 WriteRequest(packets[i], request_type, message_counter); | |
| 203 WriteRemoteCharacteristic(request); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // Changes in the GATT connection with the remote device should be observed | |
| 208 // here. If the GATT connection is dropped, we should call DestroyConnection() | |
| 209 // anyway, so the object can notify its observers. | |
| 210 void BluetoothLowEnergyWeaveClientConnection::DeviceChanged( | |
| 211 BluetoothAdapter* adapter, | |
| 212 BluetoothDevice* device) { | |
| 213 DCHECK(device); | |
| 214 if (sub_status() == SubStatus::DISCONNECTED || | |
| 215 device->GetAddress() != GetDeviceAddress()) | |
| 216 return; | |
| 217 | |
| 218 if (sub_status() != SubStatus::WAITING_GATT_CONNECTION && | |
| 219 !device->IsConnected()) { | |
| 220 PA_LOG(INFO) << "GATT connection dropped " << GetDeviceAddress() | |
| 221 << "\ndevice connected: " << device->IsConnected() | |
| 222 << "\ngatt connection: " | |
| 223 << (gatt_connection_ ? gatt_connection_->IsConnected() | |
| 224 : false); | |
| 225 DestroyConnection(); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 void BluetoothLowEnergyWeaveClientConnection::DeviceRemoved( | |
| 230 BluetoothAdapter* adapter, | |
| 231 BluetoothDevice* device) { | |
| 232 DCHECK(device); | |
| 233 if (sub_status_ == SubStatus::DISCONNECTED || | |
| 234 device->GetAddress() != GetDeviceAddress()) | |
| 235 return; | |
| 236 | |
| 237 PA_LOG(INFO) << "Device removed " << GetDeviceAddress(); | |
| 238 DestroyConnection(); | |
| 239 } | |
| 240 | |
| 241 void BluetoothLowEnergyWeaveClientConnection::GattCharacteristicValueChanged( | |
| 242 BluetoothAdapter* adapter, | |
| 243 BluetoothRemoteGattCharacteristic* characteristic, | |
| 244 const Packet& value) { | |
| 245 DCHECK_EQ(adapter, adapter_.get()); | |
| 246 if (sub_status() != SubStatus::WAITING_CONNECTION_RESPONSE && | |
| 247 sub_status() != SubStatus::CONNECTED) | |
| 248 return; | |
| 249 | |
| 250 PA_LOG(INFO) << "Characteristic value changed: " | |
| 251 << characteristic->GetUUID().canonical_value(); | |
| 252 | |
| 253 if (characteristic->GetIdentifier() == rx_characteristic_.id) { | |
| 254 ReceiverState state = packet_receiver_->ReceivePacket(value); | |
| 255 | |
| 256 PA_LOG(INFO) << "\nReceiver State: " << state; | |
| 257 switch (state) { | |
| 258 case ReceiverState::DATA_READY: | |
| 259 OnBytesReceived(packet_receiver_->GetDataMessage()); | |
| 260 break; | |
| 261 case ReceiverState::CONNECTION_CLOSED: | |
| 262 PA_LOG(ERROR) << "Connection closed due to: " << GetReasonForClose(); | |
| 263 DestroyConnection(); | |
| 264 break; | |
| 265 case ReceiverState::ERROR_DETECTED: | |
| 266 OnPacketReceiverError(); | |
| 267 break; | |
| 268 case ReceiverState::WAITING: | |
| 269 // Receiver state should have changed from CONNECTING to WAITING if | |
| 270 // a proper connection response had been received. | |
| 271 // The max packet size selected from the connection response will be | |
| 272 // used to generate future data packets. | |
| 273 packet_generator_->SetMaxPacketSize( | |
| 274 packet_receiver_->GetMaxPacketSize()); | |
| 275 DCHECK(sub_status() == SubStatus::WAITING_CONNECTION_RESPONSE); | |
| 276 CompleteConnection(); | |
| 277 break; | |
| 278 case ReceiverState::RECEIVING_DATA: | |
| 279 // Normal in between states, so do nothing. | |
| 280 break; | |
| 281 default: | |
| 282 NOTREACHED(); | |
| 283 } | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 BluetoothLowEnergyWeaveClientConnection::WriteRequest::WriteRequest( | |
| 288 const Packet& val, | |
| 289 WriteRequestType request_type, | |
| 290 int message_counter) | |
| 291 : value(val), | |
| 292 request_type(request_type), | |
| 293 message_counter(message_counter), | |
| 294 number_of_failed_attempts(0) {} | |
| 295 | |
| 296 BluetoothLowEnergyWeaveClientConnection::WriteRequest::WriteRequest( | |
| 297 const Packet& val, | |
| 298 WriteRequestType request_type) | |
| 299 : value(val), | |
| 300 request_type(request_type), | |
| 301 message_counter(kDefaultMessageCounter), | |
| 302 number_of_failed_attempts(0) {} | |
| 303 | |
| 304 BluetoothLowEnergyWeaveClientConnection::WriteRequest::WriteRequest( | |
| 305 const WriteRequest& other) = default; | |
| 306 | |
| 307 BluetoothLowEnergyWeaveClientConnection::WriteRequest::~WriteRequest() {} | |
| 308 | |
| 309 void BluetoothLowEnergyWeaveClientConnection::CompleteConnection() { | |
| 310 PA_LOG(INFO) << "Connection completed. Time elapsed: " | |
| 311 << base::TimeTicks::Now() - start_time_; | |
| 312 SetSubStatus(SubStatus::CONNECTED); | |
| 313 } | |
| 314 | |
| 315 void BluetoothLowEnergyWeaveClientConnection::OnCreateGattConnectionError( | |
| 316 device::BluetoothDevice::ConnectErrorCode error_code) { | |
| 317 DCHECK(sub_status_ == SubStatus::WAITING_GATT_CONNECTION); | |
| 318 PA_LOG(WARNING) << "Error creating GATT connection to " | |
| 319 << remote_device().bluetooth_address | |
| 320 << "error code: " << error_code; | |
| 321 DestroyConnection(); | |
| 322 } | |
| 323 | |
| 324 void BluetoothLowEnergyWeaveClientConnection::OnGattConnectionCreated( | |
| 325 std::unique_ptr<device::BluetoothGattConnection> gatt_connection) { | |
| 326 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); | |
| 327 PA_LOG(INFO) << "GATT connection with " << gatt_connection->GetDeviceAddress() | |
| 328 << " created."; | |
| 329 PrintTimeElapsed(); | |
| 330 | |
| 331 // Informing |bluetooth_trottler_| a new connection was established. | |
| 332 bluetooth_throttler_->OnConnection(this); | |
| 333 | |
| 334 gatt_connection_ = std::move(gatt_connection); | |
| 335 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); | |
| 336 characteristic_finder_.reset(CreateCharacteristicsFinder( | |
| 337 base::Bind( | |
| 338 &BluetoothLowEnergyWeaveClientConnection::OnCharacteristicsFound, | |
| 339 weak_ptr_factory_.GetWeakPtr()), | |
| 340 base::Bind(&BluetoothLowEnergyWeaveClientConnection:: | |
| 341 OnCharacteristicsFinderError, | |
| 342 weak_ptr_factory_.GetWeakPtr()))); | |
| 343 } | |
| 344 | |
| 345 BluetoothLowEnergyCharacteristicsFinder* | |
| 346 BluetoothLowEnergyWeaveClientConnection::CreateCharacteristicsFinder( | |
| 347 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | |
| 348 success_callback, | |
| 349 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& | |
| 350 error_callback) { | |
| 351 return new BluetoothLowEnergyCharacteristicsFinder( | |
| 352 adapter_, GetRemoteDevice(), remote_service_, tx_characteristic_, | |
| 353 rx_characteristic_, success_callback, error_callback); | |
| 354 } | |
| 355 | |
| 356 void BluetoothLowEnergyWeaveClientConnection::OnCharacteristicsFound( | |
| 357 const RemoteAttribute& service, | |
| 358 const RemoteAttribute& tx_characteristic, | |
| 359 const RemoteAttribute& rx_characteristic) { | |
| 360 PA_LOG(INFO) << "Remote chacteristics found."; | |
| 361 PrintTimeElapsed(); | |
| 362 | |
| 363 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); | |
| 364 remote_service_ = service; | |
| 365 tx_characteristic_ = tx_characteristic; | |
| 366 rx_characteristic_ = rx_characteristic; | |
| 367 | |
| 368 SetSubStatus(SubStatus::CHARACTERISTICS_FOUND); | |
| 369 StartNotifySession(); | |
| 370 } | |
| 371 | |
| 372 void BluetoothLowEnergyWeaveClientConnection::OnCharacteristicsFinderError( | |
| 373 const RemoteAttribute& tx_characteristic, | |
| 374 const RemoteAttribute& rx_characteristic) { | |
| 375 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); | |
| 376 PA_LOG(WARNING) << "Connection error, missing characteristics for SmartLock " | |
| 377 "service.\n" | |
| 378 << (tx_characteristic.id.empty() | |
| 379 ? tx_characteristic.uuid.canonical_value() | |
| 380 : "") | |
| 381 << (rx_characteristic.id.empty() | |
| 382 ? ", " + rx_characteristic.uuid.canonical_value() | |
| 383 : "") | |
| 384 << " not found."; | |
| 385 | |
| 386 DestroyConnection(); | |
| 387 } | |
| 388 | |
| 389 void BluetoothLowEnergyWeaveClientConnection::StartNotifySession() { | |
| 390 if (sub_status() == SubStatus::CHARACTERISTICS_FOUND) { | |
| 391 BluetoothRemoteGattCharacteristic* characteristic = | |
| 392 GetGattCharacteristic(rx_characteristic_.id); | |
| 393 DCHECK(characteristic); | |
| 394 | |
| 395 // This is a workaround for crbug.com/507325. If |characteristic| is already | |
| 396 // notifying |characteristic->StartNotifySession()| will fail with | |
| 397 // GATT_ERROR_FAILED. | |
| 398 if (characteristic->IsNotifying()) { | |
| 399 PA_LOG(INFO) << characteristic->GetUUID().canonical_value() | |
| 400 << " already notifying."; | |
| 401 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); | |
| 402 SendConnectionRequest(); | |
| 403 return; | |
| 404 } | |
| 405 | |
| 406 SetSubStatus(SubStatus::WAITING_NOTIFY_SESSION); | |
| 407 characteristic->StartNotifySession( | |
| 408 base::Bind( | |
| 409 &BluetoothLowEnergyWeaveClientConnection::OnNotifySessionStarted, | |
| 410 weak_ptr_factory_.GetWeakPtr()), | |
| 411 base::Bind( | |
| 412 &BluetoothLowEnergyWeaveClientConnection::OnNotifySessionError, | |
| 413 weak_ptr_factory_.GetWeakPtr())); | |
| 414 } | |
| 415 } | |
| 416 | |
| 417 void BluetoothLowEnergyWeaveClientConnection::OnNotifySessionStarted( | |
| 418 std::unique_ptr<BluetoothGattNotifySession> notify_session) { | |
| 419 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); | |
| 420 PA_LOG(INFO) << "Notification session started " | |
| 421 << notify_session->GetCharacteristicIdentifier(); | |
| 422 PrintTimeElapsed(); | |
| 423 | |
| 424 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); | |
| 425 notify_session_ = std::move(notify_session); | |
| 426 | |
| 427 SendConnectionRequest(); | |
| 428 } | |
| 429 | |
| 430 void BluetoothLowEnergyWeaveClientConnection::OnNotifySessionError( | |
| 431 BluetoothRemoteGattService::GattErrorCode error) { | |
| 432 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); | |
| 433 PA_LOG(WARNING) << "Error starting notification session: " << error; | |
| 434 DestroyConnection(); | |
| 435 } | |
| 436 | |
| 437 void BluetoothLowEnergyWeaveClientConnection::StopNotifySession() { | |
| 438 if (notify_session_) { | |
| 439 notify_session_->Stop(base::Bind(&base::DoNothing)); | |
| 440 notify_session_.reset(); | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 void BluetoothLowEnergyWeaveClientConnection::SendConnectionRequest() { | |
| 445 if (sub_status() == SubStatus::NOTIFY_SESSION_READY) { | |
| 446 PA_LOG(INFO) << "Sending connection request to the server"; | |
| 447 SetSubStatus(SubStatus::WAITING_CONNECTION_RESPONSE); | |
| 448 | |
| 449 WriteRequest write_request = | |
| 450 WriteRequest(packet_generator_->CreateConnectionRequest(), | |
| 451 WriteRequestType::REGULAR); | |
| 452 | |
| 453 WriteRemoteCharacteristic(write_request); | |
| 454 } | |
| 455 } | |
| 456 | |
| 457 void BluetoothLowEnergyWeaveClientConnection::WriteRemoteCharacteristic( | |
| 458 const WriteRequest& request) { | |
| 459 write_requests_queue_.push(request); | |
| 460 ProcessNextWriteRequest(); | |
| 461 } | |
| 462 | |
| 463 void BluetoothLowEnergyWeaveClientConnection::ProcessNextWriteRequest() { | |
| 464 BluetoothRemoteGattCharacteristic* characteristic = | |
| 465 GetGattCharacteristic(tx_characteristic_.id); | |
| 466 if (!write_requests_queue_.empty() && !write_remote_characteristic_pending_ && | |
| 467 characteristic) { | |
| 468 write_remote_characteristic_pending_ = true; | |
| 469 WriteRequest next_request = write_requests_queue_.front(); | |
| 470 | |
| 471 PA_LOG(INFO) << "Writing characteristic..."; | |
| 472 characteristic->WriteRemoteCharacteristic( | |
| 473 next_request.value, | |
| 474 base::Bind(&BluetoothLowEnergyWeaveClientConnection:: | |
| 475 OnRemoteCharacteristicWritten, | |
| 476 weak_ptr_factory_.GetWeakPtr(), next_request.request_type, | |
|
Kyle Horimoto
2016/07/12 01:03:13
nit: Put next_request.request_type on its own line
jingxuy
2016/07/15 18:24:59
this is git cl format's doing, undoing it will fai
| |
| 477 next_request.message_counter), | |
| 478 base::Bind(&BluetoothLowEnergyWeaveClientConnection:: | |
| 479 OnWriteRemoteCharacteristicError, | |
| 480 weak_ptr_factory_.GetWeakPtr(), next_request.request_type, | |
| 481 next_request.message_counter)); | |
| 482 } | |
| 483 } | |
| 484 | |
| 485 void BluetoothLowEnergyWeaveClientConnection::OnRemoteCharacteristicWritten( | |
| 486 WriteRequestType request_type, | |
| 487 int message_counter) { | |
| 488 PA_LOG(INFO) << "Characteristic written."; | |
| 489 | |
| 490 switch (request_type) { | |
| 491 case WriteRequestType::REGULAR: | |
| 492 break; | |
| 493 case WriteRequestType::MESSAGE_COMPLETE: | |
| 494 OnDidSendMessage(*map_to_message_[message_counter], true); | |
|
Kyle Horimoto
2016/07/12 01:03:13
Please change your tests to assert that the correc
jingxuy
2016/07/15 18:24:59
I tried to do this but it seems like I'm encounter
Kyle Horimoto
2016/07/15 20:20:43
I'm not sure what you mean, but I'm not talking ab
jingxuy
2016/07/15 21:30:06
The way the current tests are written, you can sav
Kyle Horimoto
2016/07/15 21:54:54
Yep, let's create an observer - thanks!
jingxuy
2016/07/16 00:14:06
Done.
| |
| 495 break; | |
| 496 case WriteRequestType::CONNECTION_CLOSE: | |
| 497 DestroyConnection(); | |
| 498 break; | |
| 499 default: | |
| 500 NOTREACHED(); | |
| 501 } | |
| 502 | |
| 503 // Removes the top of queue (already processed) and process the next request. | |
| 504 DCHECK(!write_requests_queue_.empty()); | |
| 505 write_requests_queue_.pop(); | |
| 506 write_remote_characteristic_pending_ = false; | |
| 507 ProcessNextWriteRequest(); | |
| 508 } | |
| 509 | |
| 510 void BluetoothLowEnergyWeaveClientConnection::OnWriteRemoteCharacteristicError( | |
| 511 WriteRequestType request_type, | |
| 512 int message_counter, | |
| 513 BluetoothRemoteGattService::GattErrorCode error) { | |
| 514 PA_LOG(WARNING) << "Error " << error << " writing characteristic: " | |
| 515 << tx_characteristic_.uuid.canonical_value(); | |
| 516 | |
| 517 switch (request_type) { | |
| 518 case WriteRequestType::REGULAR: | |
| 519 break; | |
| 520 case WriteRequestType::MESSAGE_COMPLETE: | |
| 521 OnDidSendMessage(*map_to_message_[message_counter], false); | |
| 522 break; | |
| 523 case WriteRequestType::CONNECTION_CLOSE: | |
| 524 break; | |
| 525 default: | |
| 526 NOTREACHED(); | |
| 527 } | |
| 528 | |
| 529 write_remote_characteristic_pending_ = false; | |
| 530 | |
| 531 // Increases the number of failed attempts and retry. | |
| 532 DCHECK(!write_requests_queue_.empty()); | |
| 533 if (++write_requests_queue_.front().number_of_failed_attempts >= | |
| 534 max_number_of_write_attempts_) { | |
| 535 // If the previous write failed that many times, probably can't write a | |
| 536 // connection close either, so just destroy the connection. | |
| 537 DestroyConnection(); | |
| 538 } else { | |
| 539 ProcessNextWriteRequest(); | |
| 540 } | |
| 541 } | |
| 542 | |
| 543 void BluetoothLowEnergyWeaveClientConnection::OnPacketReceiverError() { | |
| 544 PA_LOG(ERROR) << "Received erroneous packet. Closing connection."; | |
| 545 | |
| 546 WriteRequest request(packet_generator_->CreateConnectionClose( | |
| 547 packet_receiver_->GetReasonToClose()), | |
| 548 WriteRequestType::CONNECTION_CLOSE); | |
| 549 | |
| 550 // Skip all other writes that's not in progress. | |
| 551 | |
| 552 // C++ queue does not have a clear method. | |
| 553 // According to stackoverflow | |
| 554 // "http://stackoverflow.com/questions/709146/how-do-i-clear-the-stdqueue- | |
| 555 // efficiently" | |
| 556 std::queue<WriteRequest> empty; | |
| 557 std::swap(write_requests_queue_, empty); | |
| 558 | |
| 559 if (write_remote_characteristic_pending_) { | |
| 560 // Add the in progress write back to the queue. | |
| 561 write_requests_queue_.push(empty.front()); | |
| 562 } | |
| 563 | |
| 564 WriteRemoteCharacteristic(request); | |
| 565 } | |
| 566 | |
| 567 void BluetoothLowEnergyWeaveClientConnection::PrintTimeElapsed() { | |
| 568 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; | |
| 569 } | |
| 570 | |
| 571 std::string BluetoothLowEnergyWeaveClientConnection::GetDeviceAddress() { | |
| 572 // When the remote device is connected we should rely on the address given by | |
| 573 // |gatt_connection_|. As the device address may change if the device is | |
| 574 // paired. The address in |gatt_connection_| is automatically updated in this | |
| 575 // case. | |
| 576 return gatt_connection_ ? gatt_connection_->GetDeviceAddress() | |
| 577 : remote_device().bluetooth_address; | |
| 578 } | |
| 579 | |
| 580 BluetoothDevice* BluetoothLowEnergyWeaveClientConnection::GetRemoteDevice() { | |
| 581 // It's not possible to simply use | |
| 582 // |adapter_->GetDevice(GetDeviceAddress())| to find the device with MAC | |
| 583 // address |GetDeviceAddress()|. For paired devices, | |
| 584 // BluetoothAdapter::GetDevice(XXX) searches for the temporary MAC address | |
| 585 // XXX, whereas |GetDeviceAddress()| is the real MAC address. This is a | |
| 586 // bug in the way device::BluetoothAdapter is storing the devices (see | |
| 587 // crbug.com/497841). | |
| 588 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); | |
| 589 for (const auto& device : devices) { | |
| 590 if (device->GetAddress() == GetDeviceAddress()) | |
| 591 return device; | |
| 592 } | |
| 593 | |
| 594 return nullptr; | |
| 595 } | |
| 596 | |
| 597 BluetoothRemoteGattService* | |
| 598 BluetoothLowEnergyWeaveClientConnection::GetRemoteService() { | |
| 599 BluetoothDevice* remote_device = GetRemoteDevice(); | |
| 600 if (!remote_device) { | |
| 601 PA_LOG(WARNING) << "Remote device not found."; | |
| 602 return NULL; | |
| 603 } | |
| 604 if (remote_service_.id.empty()) { | |
| 605 std::vector<BluetoothRemoteGattService*> services = | |
| 606 remote_device->GetGattServices(); | |
| 607 for (const auto& service : services) | |
| 608 if (service->GetUUID() == remote_service_.uuid) { | |
| 609 remote_service_.id = service->GetIdentifier(); | |
| 610 break; | |
| 611 } | |
| 612 } | |
| 613 return remote_device->GetGattService(remote_service_.id); | |
| 614 } | |
| 615 | |
| 616 BluetoothRemoteGattCharacteristic* | |
| 617 BluetoothLowEnergyWeaveClientConnection::GetGattCharacteristic( | |
| 618 const std::string& gatt_characteristic) { | |
| 619 BluetoothRemoteGattService* remote_service = GetRemoteService(); | |
| 620 if (!remote_service) { | |
| 621 PA_LOG(WARNING) << "Remote service not found."; | |
| 622 return NULL; | |
| 623 } | |
| 624 return remote_service->GetCharacteristic(gatt_characteristic); | |
| 625 } | |
| 626 | |
| 627 std::string BluetoothLowEnergyWeaveClientConnection::GetReasonForClose() { | |
| 628 switch (packet_receiver_->GetReasonForClose()) { | |
| 629 case ReasonForClose::CLOSE_WITHOUT_ERROR: | |
| 630 return "CLOSE_WITHOUT_ERROR"; | |
| 631 case ReasonForClose::UNKNOWN_ERROR: | |
| 632 return "UNKNOWN_ERROR"; | |
| 633 case ReasonForClose::NO_COMMON_VERSION_SUPPORTED: | |
| 634 return "NO_COMMON_VERSION_SUPPORTED"; | |
| 635 case ReasonForClose::RECEIVED_PACKET_OUT_OF_SEQUENCE: | |
| 636 return "RECEIVED_PACKET_OUT_OF_SEQUENCE"; | |
| 637 case ReasonForClose::APPLICATION_ERROR: | |
| 638 return "APPLICATION_ERROR"; | |
| 639 default: | |
| 640 NOTREACHED(); | |
| 641 } | |
| 642 } | |
| 643 | |
| 644 } // namespace weave | |
| 645 | |
| 646 } // namespace proximity_auth | |
| OLD | NEW |