| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_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" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/task_runner.h" | 13 #include "base/task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin
der.h" | 16 #include "components/cryptauth/ble/bluetooth_low_energy_characteristics_finder.h
" |
| 17 #include "components/proximity_auth/ble/fake_wire_message.h" | 17 #include "components/cryptauth/ble/fake_wire_message.h" |
| 18 #include "components/proximity_auth/bluetooth_throttler.h" | 18 #include "components/cryptauth/bluetooth_throttler.h" |
| 19 #include "components/cryptauth/connection.h" |
| 20 #include "components/cryptauth/wire_message.h" |
| 19 #include "components/proximity_auth/connection_finder.h" | 21 #include "components/proximity_auth/connection_finder.h" |
| 20 #include "components/proximity_auth/logging/logging.h" | 22 #include "components/proximity_auth/logging/logging.h" |
| 21 #include "components/proximity_auth/wire_message.h" | |
| 22 #include "device/bluetooth/bluetooth_adapter.h" | 23 #include "device/bluetooth/bluetooth_adapter.h" |
| 23 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
| 24 #include "device/bluetooth/bluetooth_gatt_connection.h" | 25 #include "device/bluetooth/bluetooth_gatt_connection.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 using device::BluetoothAdapter; | 30 using device::BluetoothAdapter; |
| 30 using device::BluetoothDevice; | 31 using device::BluetoothDevice; |
| 31 using device::BluetoothGattConnection; | 32 using device::BluetoothGattConnection; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 49 // The maximum number of bytes written in a remote characteristic with a single | 50 // 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 // write request. This is not the connection MTU, we are assuming that the |
| 51 // remote device allows for writes larger than MTU. | 52 // remote device allows for writes larger than MTU. |
| 52 const int kMaxChunkSize = 500; | 53 const int kMaxChunkSize = 500; |
| 53 } // namespace | 54 } // namespace |
| 54 | 55 |
| 55 BluetoothLowEnergyConnection::BluetoothLowEnergyConnection( | 56 BluetoothLowEnergyConnection::BluetoothLowEnergyConnection( |
| 56 const cryptauth::RemoteDevice& device, | 57 const cryptauth::RemoteDevice& device, |
| 57 scoped_refptr<device::BluetoothAdapter> adapter, | 58 scoped_refptr<device::BluetoothAdapter> adapter, |
| 58 const BluetoothUUID remote_service_uuid, | 59 const BluetoothUUID remote_service_uuid, |
| 59 BluetoothThrottler* bluetooth_throttler, | 60 cryptauth::BluetoothThrottler* bluetooth_throttler, |
| 60 int max_number_of_write_attempts) | 61 int max_number_of_write_attempts) |
| 61 : Connection(device), | 62 : cryptauth::Connection(device), |
| 62 adapter_(adapter), | 63 adapter_(adapter), |
| 63 remote_service_({remote_service_uuid, ""}), | 64 remote_service_({remote_service_uuid, ""}), |
| 64 to_peripheral_char_({BluetoothUUID(kToPeripheralCharUUID), ""}), | 65 to_peripheral_char_({BluetoothUUID(kToPeripheralCharUUID), ""}), |
| 65 from_peripheral_char_({BluetoothUUID(kFromPeripheralCharUUID), ""}), | 66 from_peripheral_char_({BluetoothUUID(kFromPeripheralCharUUID), ""}), |
| 66 bluetooth_throttler_(bluetooth_throttler), | 67 bluetooth_throttler_(bluetooth_throttler), |
| 67 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 68 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 68 sub_status_(SubStatus::DISCONNECTED), | 69 sub_status_(SubStatus::DISCONNECTED), |
| 69 receiving_bytes_(false), | 70 receiving_bytes_(false), |
| 70 write_remote_characteristic_pending_(false), | 71 write_remote_characteristic_pending_(false), |
| 71 max_number_of_write_attempts_(max_number_of_write_attempts), | 72 max_number_of_write_attempts_(max_number_of_write_attempts), |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 // Only transition to the DISCONNECTED state after perfoming all necessary | 143 // Only transition to the DISCONNECTED state after perfoming all necessary |
| 143 // operations. Otherwise, it'll trigger observers that can pontentially | 144 // operations. Otherwise, it'll trigger observers that can pontentially |
| 144 // destroy the current instance (causing a crash). | 145 // destroy the current instance (causing a crash). |
| 145 SetSubStatus(SubStatus::DISCONNECTED); | 146 SetSubStatus(SubStatus::DISCONNECTED); |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 149 void BluetoothLowEnergyConnection::SetSubStatus(SubStatus new_sub_status) { | 150 void BluetoothLowEnergyConnection::SetSubStatus(SubStatus new_sub_status) { |
| 150 sub_status_ = new_sub_status; | 151 sub_status_ = new_sub_status; |
| 151 | 152 |
| 152 // Sets the status of parent class proximity_auth::Connection accordingly. | 153 // Sets the status of parent class cryptauth::Connection accordingly. |
| 153 if (new_sub_status == SubStatus::CONNECTED) { | 154 if (new_sub_status == SubStatus::CONNECTED) { |
| 154 SetStatus(CONNECTED); | 155 SetStatus(CONNECTED); |
| 155 } else if (new_sub_status == SubStatus::DISCONNECTED) { | 156 } else if (new_sub_status == SubStatus::DISCONNECTED) { |
| 156 SetStatus(DISCONNECTED); | 157 SetStatus(DISCONNECTED); |
| 157 } else { | 158 } else { |
| 158 SetStatus(IN_PROGRESS); | 159 SetStatus(IN_PROGRESS); |
| 159 } | 160 } |
| 160 } | 161 } |
| 161 | 162 |
| 162 void BluetoothLowEnergyConnection::SetTaskRunnerForTesting( | 163 void BluetoothLowEnergyConnection::SetTaskRunnerForTesting( |
| 163 scoped_refptr<base::TaskRunner> task_runner) { | 164 scoped_refptr<base::TaskRunner> task_runner) { |
| 164 task_runner_ = task_runner; | 165 task_runner_ = task_runner; |
| 165 } | 166 } |
| 166 | 167 |
| 167 void BluetoothLowEnergyConnection::SendMessageImpl( | 168 void BluetoothLowEnergyConnection::SendMessageImpl( |
| 168 std::unique_ptr<WireMessage> message) { | 169 std::unique_ptr<cryptauth::WireMessage> message) { |
| 169 PA_LOG(INFO) << "Sending message " << message->Serialize(); | 170 PA_LOG(INFO) << "Sending message " << message->Serialize(); |
| 170 std::string serialized_msg = message->Serialize(); | 171 std::string serialized_msg = message->Serialize(); |
| 171 | 172 |
| 172 // [First write]: Build a header with the [send signal] + [size of the | 173 // [First write]: Build a header with the [send signal] + [size of the |
| 173 // message]. | 174 // message]. |
| 174 WriteRequest write_request = BuildWriteRequest( | 175 WriteRequest write_request = BuildWriteRequest( |
| 175 ToByteVector(static_cast<uint32_t>(ControlSignal::kSendSignal)), | 176 ToByteVector(static_cast<uint32_t>(ControlSignal::kSendSignal)), |
| 176 ToByteVector(static_cast<uint32_t>(serialized_msg.size())), false); | 177 ToByteVector(static_cast<uint32_t>(serialized_msg.size())), false); |
| 177 | 178 |
| 178 // [First write]: Fill the it with a prefix of |serialized_msg| up to | 179 // [First write]: Fill the it with a prefix of |serialized_msg| up to |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 | 351 |
| 351 gatt_connection_ = std::move(gatt_connection); | 352 gatt_connection_ = std::move(gatt_connection); |
| 352 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); | 353 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); |
| 353 characteristic_finder_.reset(CreateCharacteristicsFinder( | 354 characteristic_finder_.reset(CreateCharacteristicsFinder( |
| 354 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFound, | 355 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFound, |
| 355 weak_ptr_factory_.GetWeakPtr()), | 356 weak_ptr_factory_.GetWeakPtr()), |
| 356 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFinderError, | 357 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFinderError, |
| 357 weak_ptr_factory_.GetWeakPtr()))); | 358 weak_ptr_factory_.GetWeakPtr()))); |
| 358 } | 359 } |
| 359 | 360 |
| 360 BluetoothLowEnergyCharacteristicsFinder* | 361 cryptauth::BluetoothLowEnergyCharacteristicsFinder* |
| 361 BluetoothLowEnergyConnection::CreateCharacteristicsFinder( | 362 BluetoothLowEnergyConnection::CreateCharacteristicsFinder( |
| 362 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | 363 const cryptauth::BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& |
| 363 success_callback, | 364 success_callback, |
| 364 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& | 365 const cryptauth::BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& |
| 365 error_callback) { | 366 error_callback) { |
| 366 return new BluetoothLowEnergyCharacteristicsFinder( | 367 return new cryptauth::BluetoothLowEnergyCharacteristicsFinder( |
| 367 adapter_, GetRemoteDevice(), remote_service_, to_peripheral_char_, | 368 adapter_, GetRemoteDevice(), remote_service_, to_peripheral_char_, |
| 368 from_peripheral_char_, success_callback, error_callback); | 369 from_peripheral_char_, success_callback, error_callback); |
| 369 } | 370 } |
| 370 | 371 |
| 371 void BluetoothLowEnergyConnection::OnCharacteristicsFound( | 372 void BluetoothLowEnergyConnection::OnCharacteristicsFound( |
| 372 const RemoteAttribute& service, | 373 const cryptauth::RemoteAttribute& service, |
| 373 const RemoteAttribute& to_peripheral_char, | 374 const cryptauth::RemoteAttribute& to_peripheral_char, |
| 374 const RemoteAttribute& from_peripheral_char) { | 375 const cryptauth::RemoteAttribute& from_peripheral_char) { |
| 375 PA_LOG(INFO) << "Remote chacteristics found."; | 376 PA_LOG(INFO) << "Remote chacteristics found."; |
| 376 PrintTimeElapsed(); | 377 PrintTimeElapsed(); |
| 377 | 378 |
| 378 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); | 379 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); |
| 379 remote_service_ = service; | 380 remote_service_ = service; |
| 380 to_peripheral_char_ = to_peripheral_char; | 381 to_peripheral_char_ = to_peripheral_char; |
| 381 from_peripheral_char_ = from_peripheral_char; | 382 from_peripheral_char_ = from_peripheral_char; |
| 382 | 383 |
| 383 SetSubStatus(SubStatus::CHARACTERISTICS_FOUND); | 384 SetSubStatus(SubStatus::CHARACTERISTICS_FOUND); |
| 384 StartNotifySession(); | 385 StartNotifySession(); |
| 385 } | 386 } |
| 386 | 387 |
| 387 void BluetoothLowEnergyConnection::OnCharacteristicsFinderError( | 388 void BluetoothLowEnergyConnection::OnCharacteristicsFinderError( |
| 388 const RemoteAttribute& to_peripheral_char, | 389 const cryptauth::RemoteAttribute& to_peripheral_char, |
| 389 const RemoteAttribute& from_peripheral_char) { | 390 const cryptauth::RemoteAttribute& from_peripheral_char) { |
| 390 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); | 391 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); |
| 391 PA_LOG(WARNING) << "Connection error, missing characteristics for SmartLock " | 392 PA_LOG(WARNING) << "Connection error, missing characteristics for SmartLock " |
| 392 "service.\n" | 393 "service.\n" |
| 393 << (to_peripheral_char.id.empty() | 394 << (to_peripheral_char.id.empty() |
| 394 ? to_peripheral_char.uuid.canonical_value() | 395 ? to_peripheral_char.uuid.canonical_value() |
| 395 : "") | 396 : "") |
| 396 << (from_peripheral_char.id.empty() | 397 << (from_peripheral_char.id.empty() |
| 397 ? ", " + from_peripheral_char.uuid.canonical_value() | 398 ? ", " + from_peripheral_char.uuid.canonical_value() |
| 398 : "") << " not found."; | 399 : "") << " not found."; |
| 399 | 400 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 next_request.is_last_write_for_wire_message)); | 493 next_request.is_last_write_for_wire_message)); |
| 493 } | 494 } |
| 494 } | 495 } |
| 495 | 496 |
| 496 void BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten( | 497 void BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten( |
| 497 bool run_did_send_message_callback) { | 498 bool run_did_send_message_callback) { |
| 498 PA_LOG(INFO) << "Characteristic written."; | 499 PA_LOG(INFO) << "Characteristic written."; |
| 499 write_remote_characteristic_pending_ = false; | 500 write_remote_characteristic_pending_ = false; |
| 500 // TODO(sacomoto): Actually pass the current message to the observer. | 501 // TODO(sacomoto): Actually pass the current message to the observer. |
| 501 if (run_did_send_message_callback) | 502 if (run_did_send_message_callback) |
| 502 OnDidSendMessage(WireMessage(std::string(), std::string()), true); | 503 OnDidSendMessage(cryptauth::WireMessage(std::string(), std::string()), |
| 504 true); |
| 503 | 505 |
| 504 // Removes the top of queue (already processed) and process the next request. | 506 // Removes the top of queue (already processed) and process the next request. |
| 505 DCHECK(!write_requests_queue_.empty()); | 507 DCHECK(!write_requests_queue_.empty()); |
| 506 write_requests_queue_.pop(); | 508 write_requests_queue_.pop(); |
| 507 ProcessNextWriteRequest(); | 509 ProcessNextWriteRequest(); |
| 508 } | 510 } |
| 509 | 511 |
| 510 void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError( | 512 void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError( |
| 511 bool run_did_send_message_callback, | 513 bool run_did_send_message_callback, |
| 512 BluetoothRemoteGattService::GattErrorCode error) { | 514 BluetoothRemoteGattService::GattErrorCode error) { |
| 513 PA_LOG(WARNING) << "Error " << error << " writing characteristic: " | 515 PA_LOG(WARNING) << "Error " << error << " writing characteristic: " |
| 514 << to_peripheral_char_.uuid.canonical_value(); | 516 << to_peripheral_char_.uuid.canonical_value(); |
| 515 write_remote_characteristic_pending_ = false; | 517 write_remote_characteristic_pending_ = false; |
| 516 // TODO(sacomoto): Actually pass the current message to the observer. | 518 // TODO(sacomoto): Actually pass the current message to the observer. |
| 517 if (run_did_send_message_callback) | 519 if (run_did_send_message_callback) |
| 518 OnDidSendMessage(WireMessage(std::string(), std::string()), false); | 520 OnDidSendMessage(cryptauth::WireMessage(std::string(), std::string()), |
| 521 false); |
| 519 | 522 |
| 520 // Increases the number of failed attempts and retry. | 523 // Increases the number of failed attempts and retry. |
| 521 DCHECK(!write_requests_queue_.empty()); | 524 DCHECK(!write_requests_queue_.empty()); |
| 522 if (++write_requests_queue_.front().number_of_failed_attempts >= | 525 if (++write_requests_queue_.front().number_of_failed_attempts >= |
| 523 max_number_of_write_attempts_) { | 526 max_number_of_write_attempts_) { |
| 524 Disconnect(); | 527 Disconnect(); |
| 525 return; | 528 return; |
| 526 } | 529 } |
| 527 ProcessNextWriteRequest(); | 530 ProcessNextWriteRequest(); |
| 528 } | 531 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 const uint32_t value) { | 612 const uint32_t value) { |
| 610 std::vector<uint8_t> bytes(4, 0); | 613 std::vector<uint8_t> bytes(4, 0); |
| 611 bytes[0] = static_cast<uint8_t>(value); | 614 bytes[0] = static_cast<uint8_t>(value); |
| 612 bytes[1] = static_cast<uint8_t>(value >> 8); | 615 bytes[1] = static_cast<uint8_t>(value >> 8); |
| 613 bytes[2] = static_cast<uint8_t>(value >> 16); | 616 bytes[2] = static_cast<uint8_t>(value >> 16); |
| 614 bytes[3] = static_cast<uint8_t>(value >> 24); | 617 bytes[3] = static_cast<uint8_t>(value >> 24); |
| 615 return bytes; | 618 return bytes; |
| 616 } | 619 } |
| 617 | 620 |
| 618 } // namespace proximity_auth | 621 } // namespace proximity_auth |
| OLD | NEW |