| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 const BluetoothUUID remote_service_uuid, | 38 const BluetoothUUID remote_service_uuid, |
| 39 const BluetoothUUID to_peripheral_char_uuid, | 39 const BluetoothUUID to_peripheral_char_uuid, |
| 40 const BluetoothUUID from_peripheral_char_uuid, | 40 const BluetoothUUID from_peripheral_char_uuid, |
| 41 scoped_ptr<BluetoothGattConnection> gatt_connection, | 41 scoped_ptr<BluetoothGattConnection> gatt_connection, |
| 42 int max_number_of_write_attempts) | 42 int max_number_of_write_attempts) |
| 43 : Connection(device), | 43 : Connection(device), |
| 44 adapter_(adapter), | 44 adapter_(adapter), |
| 45 remote_service_({remote_service_uuid, ""}), | 45 remote_service_({remote_service_uuid, ""}), |
| 46 to_peripheral_char_({to_peripheral_char_uuid, ""}), | 46 to_peripheral_char_({to_peripheral_char_uuid, ""}), |
| 47 from_peripheral_char_({from_peripheral_char_uuid, ""}), | 47 from_peripheral_char_({from_peripheral_char_uuid, ""}), |
| 48 connection_(gatt_connection.Pass()), |
| 48 sub_status_(SubStatus::DISCONNECTED), | 49 sub_status_(SubStatus::DISCONNECTED), |
| 49 receiving_bytes_(false), | 50 receiving_bytes_(false), |
| 50 write_remote_characteristic_pending_(false), | 51 write_remote_characteristic_pending_(false), |
| 51 max_number_of_write_attempts_(max_number_of_write_attempts), | 52 max_number_of_write_attempts_(max_number_of_write_attempts), |
| 52 weak_ptr_factory_(this) { | 53 weak_ptr_factory_(this) { |
| 53 DCHECK(adapter_); | 54 DCHECK(adapter_); |
| 54 DCHECK(adapter_->IsInitialized()); | 55 DCHECK(adapter_->IsInitialized()); |
| 55 | 56 |
| 56 start_time_ = base::TimeTicks::Now(); | 57 start_time_ = base::TimeTicks::Now(); |
| 57 adapter_->AddObserver(this); | 58 adapter_->AddObserver(this); |
| 58 | |
| 59 if (gatt_connection && gatt_connection->IsConnected()) | |
| 60 OnGattConnectionCreated(gatt_connection.Pass()); | |
| 61 } | 59 } |
| 62 | 60 |
| 63 BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() { | 61 BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() { |
| 64 Disconnect(); | 62 Disconnect(); |
| 65 if (adapter_) { | 63 if (adapter_) { |
| 66 adapter_->RemoveObserver(this); | 64 adapter_->RemoveObserver(this); |
| 67 adapter_ = NULL; | 65 adapter_ = NULL; |
| 68 } | 66 } |
| 69 } | 67 } |
| 70 | 68 |
| 71 void BluetoothLowEnergyConnection::Connect() { | 69 void BluetoothLowEnergyConnection::Connect() { |
| 70 if (connection_ && connection_->IsConnected()) { |
| 71 OnGattConnectionCreated(connection_.Pass()); |
| 72 return; |
| 73 } |
| 74 |
| 72 BluetoothDevice* remote_device = GetRemoteDevice(); | 75 BluetoothDevice* remote_device = GetRemoteDevice(); |
| 73 if (remote_device) { | 76 if (remote_device) { |
| 74 SetSubStatus(SubStatus::WAITING_GATT_CONNECTION); | 77 SetSubStatus(SubStatus::WAITING_GATT_CONNECTION); |
| 75 remote_device->CreateGattConnection( | 78 remote_device->CreateGattConnection( |
| 76 base::Bind(&BluetoothLowEnergyConnection::OnGattConnectionCreated, | 79 base::Bind(&BluetoothLowEnergyConnection::OnGattConnectionCreated, |
| 77 weak_ptr_factory_.GetWeakPtr()), | 80 weak_ptr_factory_.GetWeakPtr()), |
| 78 base::Bind(&BluetoothLowEnergyConnection::OnCreateGattConnectionError, | 81 base::Bind(&BluetoothLowEnergyConnection::OnCreateGattConnectionError, |
| 79 weak_ptr_factory_.GetWeakPtr())); | 82 weak_ptr_factory_.GetWeakPtr())); |
| 80 } | 83 } |
| 81 } | 84 } |
| 82 | 85 |
| 83 // This actually forgets the remote BLE device. This is safe as long as we only | 86 // This actually forgets the remote BLE device. This is safe as long as we only |
| 84 // connect to BLE devices advertising the SmartLock service (assuming this | 87 // connect to BLE devices advertising the SmartLock service (assuming this |
| 85 // device has no other connection). | 88 // device has no other connection). |
| 86 void BluetoothLowEnergyConnection::Disconnect() { | 89 void BluetoothLowEnergyConnection::Disconnect() { |
| 87 ClearWriteRequestsQueue(); | 90 if (sub_status_ != SubStatus::DISCONNECTED) { |
| 88 StopNotifySession(); | 91 ClearWriteRequestsQueue(); |
| 89 SetSubStatus(SubStatus::DISCONNECTED); | 92 StopNotifySession(); |
| 90 if (connection_) { | 93 SetSubStatus(SubStatus::DISCONNECTED); |
| 91 connection_.reset(); | 94 if (connection_) { |
| 92 BluetoothDevice* device = GetRemoteDevice(); | 95 connection_.reset(); |
| 93 if (device) { | 96 BluetoothDevice* device = GetRemoteDevice(); |
| 94 VLOG(1) << "Forget device " << device->GetAddress(); | 97 if (device) { |
| 95 device->Forget(base::Bind(&base::DoNothing)); | 98 VLOG(1) << "Forget device " << device->GetAddress(); |
| 99 device->Forget(base::Bind(&base::DoNothing)); |
| 100 } |
| 96 } | 101 } |
| 97 } | 102 } |
| 98 } | 103 } |
| 99 | 104 |
| 100 void BluetoothLowEnergyConnection::SetSubStatus(SubStatus new_sub_status) { | 105 void BluetoothLowEnergyConnection::SetSubStatus(SubStatus new_sub_status) { |
| 101 sub_status_ = new_sub_status; | 106 sub_status_ = new_sub_status; |
| 102 | 107 |
| 103 // Sets the status of parent class proximity_auth::Connection accordingly. | 108 // Sets the status of parent class proximity_auth::Connection accordingly. |
| 104 if (new_sub_status == SubStatus::CONNECTED) { | 109 if (new_sub_status == SubStatus::CONNECTED) { |
| 105 SetStatus(CONNECTED); | 110 SetStatus(CONNECTED); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 device::BluetoothDevice::ConnectErrorCode error_code) { | 211 device::BluetoothDevice::ConnectErrorCode error_code) { |
| 207 VLOG(1) << "Error creating GATT connection to " | 212 VLOG(1) << "Error creating GATT connection to " |
| 208 << remote_device().bluetooth_address << "error code: " << error_code; | 213 << remote_device().bluetooth_address << "error code: " << error_code; |
| 209 Disconnect(); | 214 Disconnect(); |
| 210 } | 215 } |
| 211 | 216 |
| 212 void BluetoothLowEnergyConnection::OnGattConnectionCreated( | 217 void BluetoothLowEnergyConnection::OnGattConnectionCreated( |
| 213 scoped_ptr<device::BluetoothGattConnection> gatt_connection) { | 218 scoped_ptr<device::BluetoothGattConnection> gatt_connection) { |
| 214 connection_ = gatt_connection.Pass(); | 219 connection_ = gatt_connection.Pass(); |
| 215 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); | 220 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); |
| 221 characteristic_finder_.reset(CreateCharacteristicsFinder( |
| 222 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFound, |
| 223 weak_ptr_factory_.GetWeakPtr()), |
| 224 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFinderError, |
| 225 weak_ptr_factory_.GetWeakPtr()))); |
| 226 } |
| 216 | 227 |
| 217 characteristic_finder_ = | 228 BluetoothLowEnergyCharacteristicsFinder* |
| 218 make_scoped_ptr(new BluetoothLowEnergyCharacteristicsFinder( | 229 BluetoothLowEnergyConnection::CreateCharacteristicsFinder( |
| 219 adapter_, GetRemoteDevice(), remote_service_, to_peripheral_char_, | 230 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& |
| 220 from_peripheral_char_, | 231 success_callback, |
| 221 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFound, | 232 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& |
| 222 weak_ptr_factory_.GetWeakPtr()), | 233 error_callback) { |
| 223 base::Bind( | 234 return new BluetoothLowEnergyCharacteristicsFinder( |
| 224 &BluetoothLowEnergyConnection::OnCharacteristicsFinderError, | 235 adapter_, GetRemoteDevice(), remote_service_, to_peripheral_char_, |
| 225 weak_ptr_factory_.GetWeakPtr()))); | 236 from_peripheral_char_, success_callback, error_callback); |
| 226 } | 237 } |
| 227 | 238 |
| 228 void BluetoothLowEnergyConnection::OnCharacteristicsFound( | 239 void BluetoothLowEnergyConnection::OnCharacteristicsFound( |
| 229 const RemoteAttribute& service, | 240 const RemoteAttribute& service, |
| 230 const RemoteAttribute& to_peripheral_char, | 241 const RemoteAttribute& to_peripheral_char, |
| 231 const RemoteAttribute& from_peripheral_char) { | 242 const RemoteAttribute& from_peripheral_char) { |
| 232 remote_service_ = service; | 243 remote_service_ = service; |
| 233 to_peripheral_char_ = to_peripheral_char; | 244 to_peripheral_char_ = to_peripheral_char; |
| 234 from_peripheral_char_ = from_peripheral_char; | 245 from_peripheral_char_ = from_peripheral_char; |
| 235 | 246 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 BluetoothGattService::GattErrorCode error) { | 359 BluetoothGattService::GattErrorCode error) { |
| 349 VLOG(1) << "Error " << error << " writing characteristic: " | 360 VLOG(1) << "Error " << error << " writing characteristic: " |
| 350 << to_peripheral_char_.uuid.canonical_value(); | 361 << to_peripheral_char_.uuid.canonical_value(); |
| 351 write_remote_characteristic_pending_ = false; | 362 write_remote_characteristic_pending_ = false; |
| 352 // TODO(sacomoto): Actually pass the current message to the observer. | 363 // TODO(sacomoto): Actually pass the current message to the observer. |
| 353 if (run_did_send_message_callback) | 364 if (run_did_send_message_callback) |
| 354 OnDidSendMessage(FakeWireMessage(""), false); | 365 OnDidSendMessage(FakeWireMessage(""), false); |
| 355 | 366 |
| 356 // Increases the number of failed attempts and retry. | 367 // Increases the number of failed attempts and retry. |
| 357 DCHECK(!write_requests_queue_.empty()); | 368 DCHECK(!write_requests_queue_.empty()); |
| 358 if (write_requests_queue_.front().number_of_failed_attempts++ >= | 369 if (++write_requests_queue_.front().number_of_failed_attempts >= |
| 359 max_number_of_write_attempts_) { | 370 max_number_of_write_attempts_) { |
| 360 Disconnect(); | 371 Disconnect(); |
| 361 return; | 372 return; |
| 362 } | 373 } |
| 363 ProcessNextWriteRequest(); | 374 ProcessNextWriteRequest(); |
| 364 } | 375 } |
| 365 | 376 |
| 366 BluetoothLowEnergyConnection::WriteRequest | 377 BluetoothLowEnergyConnection::WriteRequest |
| 367 BluetoothLowEnergyConnection::BuildWriteRequest( | 378 BluetoothLowEnergyConnection::BuildWriteRequest( |
| 368 const std::vector<uint8>& signal, | 379 const std::vector<uint8>& signal, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 const uint32 value) { | 442 const uint32 value) { |
| 432 std::vector<uint8> bytes(4, 0); | 443 std::vector<uint8> bytes(4, 0); |
| 433 bytes[0] = static_cast<uint8>(value); | 444 bytes[0] = static_cast<uint8>(value); |
| 434 bytes[1] = static_cast<uint8>(value >> 8); | 445 bytes[1] = static_cast<uint8>(value >> 8); |
| 435 bytes[2] = static_cast<uint8>(value >> 16); | 446 bytes[2] = static_cast<uint8>(value >> 16); |
| 436 bytes[3] = static_cast<uint8>(value >> 24); | 447 bytes[3] = static_cast<uint8>(value >> 24); |
| 437 return bytes; | 448 return bytes; |
| 438 } | 449 } |
| 439 | 450 |
| 440 } // namespace proximity_auth | 451 } // namespace proximity_auth |
| OLD | NEW |