Chromium Code Reviews| 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_finder.h " | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h " |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_factory.h" | 13 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 14 #include "device/bluetooth/bluetooth_device.h" | 14 #include "device/bluetooth/bluetooth_device.h" |
| 15 #include "device/bluetooth/bluetooth_discovery_session.h" | 15 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 16 #include "device/bluetooth/bluetooth_uuid.h" | 16 #include "device/bluetooth/bluetooth_uuid.h" |
| 17 | 17 |
| 18 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | |
|
msarda
2015/05/05 11:56:14
Sort headers.
sacomoto
2015/05/06 13:47:59
Done.
| |
| 19 | |
| 18 using device::BluetoothAdapter; | 20 using device::BluetoothAdapter; |
| 19 using device::BluetoothDevice; | 21 using device::BluetoothDevice; |
| 20 using device::BluetoothGattConnection; | 22 using device::BluetoothGattConnection; |
| 21 using device::BluetoothDiscoveryFilter; | 23 using device::BluetoothDiscoveryFilter; |
| 22 | 24 |
| 23 namespace proximity_auth { | 25 namespace proximity_auth { |
| 24 | 26 |
| 25 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( | 27 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( |
| 26 const std::string& remote_service_uuid) | 28 const std::string& remote_service_uuid) |
| 27 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), | 29 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), |
| 28 connected_(false), | 30 connected_(false), |
| 29 weak_ptr_factory_(this) { | 31 weak_ptr_factory_(this) { |
| 30 } | 32 } |
| 31 | 33 |
| 32 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { | 34 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { |
| 33 if (discovery_session_) { | 35 if (discovery_session_) { |
| 34 StopDiscoverySession(); | 36 StopDiscoverySession(); |
| 35 } | 37 } |
| 36 if (adapter_) { | 38 if (adapter_) { |
| 37 adapter_->RemoveObserver(this); | 39 adapter_->RemoveObserver(this); |
| 38 adapter_ = NULL; | 40 adapter_ = NULL; |
| 39 } | 41 } |
| 40 } | 42 } |
| 41 | 43 |
| 42 void BluetoothLowEnergyConnectionFinder::Find( | 44 void BluetoothLowEnergyConnectionFinder::Find( |
| 43 const BluetoothDevice::GattConnectionCallback& connection_callback) { | 45 const ConnectionCallback& connection_callback) { |
| 44 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 46 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 45 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; | 47 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; |
| 46 return; | 48 return; |
| 47 } | 49 } |
| 48 VLOG(1) << "Finding connection"; | 50 VLOG(1) << "Finding connection"; |
| 49 | 51 |
| 50 connection_callback_ = connection_callback; | 52 connection_callback_ = connection_callback; |
| 51 | 53 |
| 52 device::BluetoothAdapterFactory::GetAdapter( | 54 device::BluetoothAdapterFactory::GetAdapter( |
| 53 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, | 55 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, |
| 54 weak_ptr_factory_.GetWeakPtr())); | 56 weak_ptr_factory_.GetWeakPtr())); |
| 55 } | 57 } |
| 56 | 58 |
| 57 void BluetoothLowEnergyConnectionFinder::Find( | |
| 58 const ConnectionCallback& connection_callback) { | |
| 59 NOTREACHED(); | |
| 60 } | |
| 61 | |
| 62 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, | 59 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, |
| 63 BluetoothDevice* device) { | 60 BluetoothDevice* device) { |
| 64 DCHECK(device); | 61 DCHECK(device); |
| 65 VLOG(1) << "Device added: " << device->GetAddress(); | 62 VLOG(1) << "Device added: " << device->GetAddress(); |
| 66 HandleDeviceUpdated(device); | 63 HandleDeviceUpdated(device); |
| 67 } | 64 } |
| 68 | 65 |
| 69 void BluetoothLowEnergyConnectionFinder::DeviceChanged( | 66 void BluetoothLowEnergyConnectionFinder::DeviceChanged( |
| 70 BluetoothAdapter* adapter, | 67 BluetoothAdapter* adapter, |
| 71 BluetoothDevice* device) { | 68 BluetoothDevice* device) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 } | 191 } |
| 195 | 192 |
| 196 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( | 193 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( |
| 197 std::string device_address, | 194 std::string device_address, |
| 198 BluetoothDevice::ConnectErrorCode error_code) { | 195 BluetoothDevice::ConnectErrorCode error_code) { |
| 199 VLOG(1) << "Error creating connection to device " << device_address | 196 VLOG(1) << "Error creating connection to device " << device_address |
| 200 << " : error code = " << error_code; | 197 << " : error code = " << error_code; |
| 201 } | 198 } |
| 202 | 199 |
| 203 void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( | 200 void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( |
| 204 scoped_ptr<BluetoothGattConnection> connection) { | 201 scoped_ptr<BluetoothGattConnection> gatt_connection) { |
| 205 if (connected_) { | 202 if (connected_) { |
| 206 CloseConnection(connection.Pass()); | 203 CloseConnection(gatt_connection.Pass()); |
| 207 return; | 204 return; |
| 208 } | 205 } |
| 209 | 206 |
| 210 VLOG(1) << "Connection created"; | 207 VLOG(1) << "Connection created"; |
| 211 connected_ = true; | 208 connected_ = true; |
| 212 pending_connections_.clear(); | 209 pending_connections_.clear(); |
| 213 if (!connection_callback_.is_null()) { | 210 if (!connection_callback_.is_null()) { |
| 211 RemoteDevice remote_device; | |
| 212 remote_device.bluetooth_address = gatt_connection->GetDeviceAddress(); | |
| 213 std::string remote_device_address = gatt_connection->GetDeviceAddress(); | |
|
msarda
2015/05/05 11:56:14
Remove remote_device_address as it is not used.
sacomoto
2015/05/06 13:47:59
Done.
| |
| 214 scoped_ptr<BluetoothLowEnergyConnection> connection = | |
| 215 scoped_ptr<BluetoothLowEnergyConnection>( | |
| 216 new BluetoothLowEnergyConnection(remote_device, adapter_, | |
| 217 remote_service_uuid_, | |
| 218 gatt_connection.Pass())); | |
| 219 | |
| 214 connection_callback_.Run(connection.Pass()); | 220 connection_callback_.Run(connection.Pass()); |
| 215 connection_callback_.Reset(); | 221 connection_callback_.Reset(); |
| 216 } | 222 } |
| 217 StopDiscoverySession(); | 223 StopDiscoverySession(); |
| 218 } | 224 } |
| 219 | 225 |
| 220 void BluetoothLowEnergyConnectionFinder::CreateConnection( | 226 void BluetoothLowEnergyConnectionFinder::CreateConnection( |
| 221 device::BluetoothDevice* remote_device) { | 227 device::BluetoothDevice* remote_device) { |
| 222 VLOG(1) << "SmartLock service found (" | 228 VLOG(1) << "SmartLock service found (" |
| 223 << remote_service_uuid_.canonical_value() << ")\n" | 229 << remote_service_uuid_.canonical_value() << ")\n" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 236 connection.reset(); | 242 connection.reset(); |
| 237 BluetoothDevice* device = adapter_->GetDevice(device_address); | 243 BluetoothDevice* device = adapter_->GetDevice(device_address); |
| 238 if (device) { | 244 if (device) { |
| 239 DCHECK(HasService(device)); | 245 DCHECK(HasService(device)); |
| 240 VLOG(1) << "Forget device " << device->GetAddress(); | 246 VLOG(1) << "Forget device " << device->GetAddress(); |
| 241 device->Forget(base::Bind(&base::DoNothing)); | 247 device->Forget(base::Bind(&base::DoNothing)); |
| 242 } | 248 } |
| 243 } | 249 } |
| 244 | 250 |
| 245 } // namespace proximity_auth | 251 } // namespace proximity_auth |
| OLD | NEW |