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/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 max_number_of_write_attempts_(max_number_of_write_attempts), | 69 max_number_of_write_attempts_(max_number_of_write_attempts), |
70 max_chunk_size_(kMaxChunkSize), | 70 max_chunk_size_(kMaxChunkSize), |
71 weak_ptr_factory_(this) { | 71 weak_ptr_factory_(this) { |
72 DCHECK(adapter_); | 72 DCHECK(adapter_); |
73 DCHECK(adapter_->IsInitialized()); | 73 DCHECK(adapter_->IsInitialized()); |
74 | 74 |
75 adapter_->AddObserver(this); | 75 adapter_->AddObserver(this); |
76 } | 76 } |
77 | 77 |
78 BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() { | 78 BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() { |
79 PA_LOG(INFO) << "Connection destroyed."; | |
80 Disconnect(); | 79 Disconnect(); |
81 if (adapter_) { | 80 if (adapter_) { |
82 adapter_->RemoveObserver(this); | 81 adapter_->RemoveObserver(this); |
83 adapter_ = NULL; | 82 adapter_ = NULL; |
84 } | 83 } |
85 } | 84 } |
86 | 85 |
87 void BluetoothLowEnergyConnection::Connect() { | 86 void BluetoothLowEnergyConnection::Connect() { |
88 DCHECK(sub_status() == SubStatus::DISCONNECTED); | 87 DCHECK(sub_status() == SubStatus::DISCONNECTED); |
89 | 88 |
(...skipping 30 matching lines...) Expand all Loading... |
120 remote_device->CreateGattConnection( | 119 remote_device->CreateGattConnection( |
121 base::Bind(&BluetoothLowEnergyConnection::OnGattConnectionCreated, | 120 base::Bind(&BluetoothLowEnergyConnection::OnGattConnectionCreated, |
122 weak_ptr_factory_.GetWeakPtr()), | 121 weak_ptr_factory_.GetWeakPtr()), |
123 base::Bind(&BluetoothLowEnergyConnection::OnCreateGattConnectionError, | 122 base::Bind(&BluetoothLowEnergyConnection::OnCreateGattConnectionError, |
124 weak_ptr_factory_.GetWeakPtr())); | 123 weak_ptr_factory_.GetWeakPtr())); |
125 } | 124 } |
126 } | 125 } |
127 | 126 |
128 void BluetoothLowEnergyConnection::Disconnect() { | 127 void BluetoothLowEnergyConnection::Disconnect() { |
129 if (sub_status() != SubStatus::DISCONNECTED) { | 128 if (sub_status() != SubStatus::DISCONNECTED) { |
130 ClearWriteRequestsQueue(); | 129 weak_ptr_factory_.InvalidateWeakPtrs(); |
131 StopNotifySession(); | 130 StopNotifySession(); |
132 characteristic_finder_.reset(); | 131 characteristic_finder_.reset(); |
133 if (gatt_connection_) { | 132 if (gatt_connection_) { |
134 PA_LOG(INFO) << "Disconnect from device " | 133 PA_LOG(INFO) << "Disconnect from device " |
135 << gatt_connection_->GetDeviceAddress(); | 134 << gatt_connection_->GetDeviceAddress(); |
136 | 135 |
137 // Destroying BluetoothGattConnection also disconnects it. | 136 // Destroying BluetoothGattConnection also disconnects it. |
138 gatt_connection_.reset(); | 137 gatt_connection_.reset(); |
139 } | 138 } |
140 | 139 |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 } | 310 } |
312 | 311 |
313 BluetoothLowEnergyConnection::WriteRequest::WriteRequest( | 312 BluetoothLowEnergyConnection::WriteRequest::WriteRequest( |
314 const std::vector<uint8>& val, | 313 const std::vector<uint8>& val, |
315 bool flag) | 314 bool flag) |
316 : value(val), | 315 : value(val), |
317 is_last_write_for_wire_message(flag), | 316 is_last_write_for_wire_message(flag), |
318 number_of_failed_attempts(0) { | 317 number_of_failed_attempts(0) { |
319 } | 318 } |
320 | 319 |
321 BluetoothLowEnergyConnection::WriteRequest::~WriteRequest() { | 320 BluetoothLowEnergyConnection::WriteRequest::~WriteRequest() {} |
322 } | |
323 | 321 |
324 void BluetoothLowEnergyConnection::CompleteConnection() { | 322 void BluetoothLowEnergyConnection::CompleteConnection() { |
325 PA_LOG(INFO) << "Connection completed. Time elapsed: " | 323 PA_LOG(INFO) << "Connection completed. Time elapsed: " |
326 << base::TimeTicks::Now() - start_time_; | 324 << base::TimeTicks::Now() - start_time_; |
327 SetSubStatus(SubStatus::CONNECTED); | 325 SetSubStatus(SubStatus::CONNECTED); |
328 } | 326 } |
329 | 327 |
330 void BluetoothLowEnergyConnection::OnCreateGattConnectionError( | 328 void BluetoothLowEnergyConnection::OnCreateGattConnectionError( |
331 device::BluetoothDevice::ConnectErrorCode error_code) { | 329 device::BluetoothDevice::ConnectErrorCode error_code) { |
332 DCHECK(sub_status_ == SubStatus::WAITING_GATT_CONNECTION); | 330 DCHECK(sub_status_ == SubStatus::WAITING_GATT_CONNECTION); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 BluetoothLowEnergyConnection::WriteRequest | 526 BluetoothLowEnergyConnection::WriteRequest |
529 BluetoothLowEnergyConnection::BuildWriteRequest( | 527 BluetoothLowEnergyConnection::BuildWriteRequest( |
530 const std::vector<uint8>& signal, | 528 const std::vector<uint8>& signal, |
531 const std::vector<uint8>& bytes, | 529 const std::vector<uint8>& bytes, |
532 bool is_last_write_for_wire_message) { | 530 bool is_last_write_for_wire_message) { |
533 std::vector<uint8> value(signal.begin(), signal.end()); | 531 std::vector<uint8> value(signal.begin(), signal.end()); |
534 value.insert(value.end(), bytes.begin(), bytes.end()); | 532 value.insert(value.end(), bytes.begin(), bytes.end()); |
535 return WriteRequest(value, is_last_write_for_wire_message); | 533 return WriteRequest(value, is_last_write_for_wire_message); |
536 } | 534 } |
537 | 535 |
538 void BluetoothLowEnergyConnection::ClearWriteRequestsQueue() { | |
539 while (!write_requests_queue_.empty()) | |
540 write_requests_queue_.pop(); | |
541 } | |
542 | |
543 void BluetoothLowEnergyConnection::PrintTimeElapsed() { | 536 void BluetoothLowEnergyConnection::PrintTimeElapsed() { |
544 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; | 537 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; |
545 } | 538 } |
546 | 539 |
547 std::string BluetoothLowEnergyConnection::GetDeviceAddress() { | 540 std::string BluetoothLowEnergyConnection::GetDeviceAddress() { |
548 // When the remote device is connected we should rely on the address given by | 541 // When the remote device is connected we should rely on the address given by |
549 // |gatt_connection_|. As the device address may change if the device is | 542 // |gatt_connection_|. As the device address may change if the device is |
550 // paired. The address in |gatt_connection_| is automatically updated in this | 543 // paired. The address in |gatt_connection_| is automatically updated in this |
551 // case. | 544 // case. |
552 return gatt_connection_ ? gatt_connection_->GetDeviceAddress() | 545 return gatt_connection_ ? gatt_connection_->GetDeviceAddress() |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 const uint32 value) { | 604 const uint32 value) { |
612 std::vector<uint8> bytes(4, 0); | 605 std::vector<uint8> bytes(4, 0); |
613 bytes[0] = static_cast<uint8>(value); | 606 bytes[0] = static_cast<uint8>(value); |
614 bytes[1] = static_cast<uint8>(value >> 8); | 607 bytes[1] = static_cast<uint8>(value >> 8); |
615 bytes[2] = static_cast<uint8>(value >> 16); | 608 bytes[2] = static_cast<uint8>(value >> 16); |
616 bytes[3] = static_cast<uint8>(value >> 24); | 609 bytes[3] = static_cast<uint8>(value >> 24); |
617 return bytes; | 610 return bytes; |
618 } | 611 } |
619 | 612 |
620 } // namespace proximity_auth | 613 } // namespace proximity_auth |
OLD | NEW |