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 "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | |
| 14 #include "components/proximity_auth/connection.h" | |
| 13 #include "device/bluetooth/bluetooth_adapter_factory.h" | 15 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 14 #include "device/bluetooth/bluetooth_device.h" | 16 #include "device/bluetooth/bluetooth_device.h" |
| 15 #include "device/bluetooth/bluetooth_discovery_session.h" | 17 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 16 #include "device/bluetooth/bluetooth_uuid.h" | 18 #include "device/bluetooth/bluetooth_uuid.h" |
| 17 | 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, |
| 29 const std::string& to_peripheral_char_uuid, | |
| 30 const std::string& from_peripheral_char_uuid) | |
| 27 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), | 31 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), |
| 32 to_peripheral_char_uuid_(to_peripheral_char_uuid), | |
| 33 from_peripheral_char_uuid_(from_peripheral_char_uuid), | |
| 28 connected_(false), | 34 connected_(false), |
| 29 weak_ptr_factory_(this) { | 35 weak_ptr_factory_(this) { |
| 30 } | 36 } |
| 31 | 37 |
| 32 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { | 38 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { |
| 33 if (discovery_session_) { | 39 if (discovery_session_) { |
| 34 StopDiscoverySession(); | 40 StopDiscoverySession(); |
| 35 } | 41 } |
| 42 | |
| 43 if (connection_) { | |
| 44 connection_->RemoveObserver(this); | |
| 45 connection_.reset(); | |
| 46 } | |
| 47 | |
| 36 if (adapter_) { | 48 if (adapter_) { |
| 37 adapter_->RemoveObserver(this); | 49 adapter_->RemoveObserver(this); |
| 38 adapter_ = NULL; | 50 adapter_ = NULL; |
| 39 } | 51 } |
| 40 } | 52 } |
| 41 | 53 |
| 42 void BluetoothLowEnergyConnectionFinder::Find( | 54 void BluetoothLowEnergyConnectionFinder::Find( |
| 43 const BluetoothDevice::GattConnectionCallback& connection_callback) { | 55 const ConnectionCallback& connection_callback) { |
| 44 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 56 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 45 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; | 57 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; |
| 46 return; | 58 return; |
| 47 } | 59 } |
| 48 VLOG(1) << "Finding connection"; | 60 VLOG(1) << "Finding connection"; |
| 49 | 61 |
| 50 connection_callback_ = connection_callback; | 62 connection_callback_ = connection_callback; |
| 51 | 63 |
| 52 device::BluetoothAdapterFactory::GetAdapter( | 64 device::BluetoothAdapterFactory::GetAdapter( |
| 53 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, | 65 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, |
| 54 weak_ptr_factory_.GetWeakPtr())); | 66 weak_ptr_factory_.GetWeakPtr())); |
| 55 } | 67 } |
| 56 | 68 |
| 57 void BluetoothLowEnergyConnectionFinder::Find( | |
| 58 const ConnectionCallback& connection_callback) { | |
| 59 NOTREACHED(); | |
| 60 } | |
| 61 | |
| 62 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, | 69 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, |
| 63 BluetoothDevice* device) { | 70 BluetoothDevice* device) { |
| 64 DCHECK(device); | 71 DCHECK(device); |
| 65 VLOG(1) << "Device added: " << device->GetAddress(); | 72 VLOG(1) << "Device added: " << device->GetAddress(); |
| 66 HandleDeviceUpdated(device); | 73 HandleDeviceUpdated(device); |
| 67 } | 74 } |
| 68 | 75 |
| 69 void BluetoothLowEnergyConnectionFinder::DeviceChanged( | 76 void BluetoothLowEnergyConnectionFinder::DeviceChanged( |
| 70 BluetoothAdapter* adapter, | 77 BluetoothAdapter* adapter, |
| 71 BluetoothDevice* device) { | 78 BluetoothDevice* device) { |
| 72 DCHECK(device); | 79 DCHECK(device); |
| 73 VLOG(1) << "Device changed: " << device->GetAddress(); | 80 VLOG(1) << "Device changed: " << device->GetAddress(); |
| 74 HandleDeviceUpdated(device); | 81 HandleDeviceUpdated(device); |
| 75 } | 82 } |
| 76 | 83 |
| 77 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( | 84 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( |
| 78 BluetoothDevice* device) { | 85 BluetoothDevice* device) { |
| 79 if (connected_) | 86 if (connected_) |
| 80 return; | 87 return; |
| 81 const auto& i = pending_connections_.find(device); | 88 const auto& i = pending_connections_.find(device); |
| 82 if (i != pending_connections_.end()) { | 89 if (i != pending_connections_.end()) { |
| 83 VLOG(1) << "Pending connection to device " << device->GetAddress(); | 90 VLOG(1) << "Pending connection to device " << device->GetAddress(); |
| 84 return; | 91 return; |
| 85 } | 92 } |
| 86 if (HasService(device)) { | 93 if (HasService(device)) { |
| 87 VLOG(1) << "Connecting to device " << device->GetAddress(); | 94 VLOG(1) << "Connecting to device " << device->GetAddress(); |
| 88 pending_connections_.insert(device); | 95 pending_connections_.insert(device); |
| 89 CreateConnection(device); | 96 CreateGattConnection(device); |
| 90 } | 97 } |
| 91 } | 98 } |
| 92 | 99 |
| 93 void BluetoothLowEnergyConnectionFinder::DeviceRemoved( | 100 void BluetoothLowEnergyConnectionFinder::DeviceRemoved( |
| 94 BluetoothAdapter* adapter, | 101 BluetoothAdapter* adapter, |
| 95 BluetoothDevice* device) { | 102 BluetoothDevice* device) { |
| 96 if (connected_) | 103 if (connected_) |
| 97 return; | 104 return; |
| 98 | 105 |
| 99 const auto& i = pending_connections_.find(device); | 106 const auto& i = pending_connections_.find(device); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs(); | 193 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs(); |
| 187 for (const auto& service_uuid : uuids) { | 194 for (const auto& service_uuid : uuids) { |
| 188 if (remote_service_uuid_ == service_uuid) { | 195 if (remote_service_uuid_ == service_uuid) { |
| 189 return true; | 196 return true; |
| 190 } | 197 } |
| 191 } | 198 } |
| 192 } | 199 } |
| 193 return false; | 200 return false; |
| 194 } | 201 } |
| 195 | 202 |
| 196 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( | 203 void BluetoothLowEnergyConnectionFinder::OnCreateGattConnectionError( |
| 197 std::string device_address, | 204 std::string device_address, |
| 198 BluetoothDevice::ConnectErrorCode error_code) { | 205 BluetoothDevice::ConnectErrorCode error_code) { |
| 199 VLOG(1) << "Error creating connection to device " << device_address | 206 VLOG(1) << "Error creating connection to device " << device_address |
| 200 << " : error code = " << error_code; | 207 << " : error code = " << error_code; |
| 201 } | 208 } |
| 202 | 209 |
| 203 void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( | 210 void BluetoothLowEnergyConnectionFinder::OnGattConnectionCreated( |
| 204 scoped_ptr<BluetoothGattConnection> connection) { | 211 scoped_ptr<BluetoothGattConnection> gatt_connection) { |
| 205 if (connected_) { | 212 if (connected_) { |
| 206 CloseConnection(connection.Pass()); | 213 CloseGattConnection(gatt_connection.Pass()); |
| 207 return; | 214 return; |
| 208 } | 215 } |
| 209 | 216 |
| 210 VLOG(1) << "Connection created"; | 217 VLOG(1) << "Connection created"; |
| 211 connected_ = true; | 218 connected_ = true; |
| 212 pending_connections_.clear(); | 219 pending_connections_.clear(); |
| 213 if (!connection_callback_.is_null()) { | 220 |
| 214 connection_callback_.Run(connection.Pass()); | 221 connection_ = CreateConnection(gatt_connection.Pass()); |
| 215 connection_callback_.Reset(); | 222 connection_->AddObserver(this); |
| 216 } | 223 |
| 217 StopDiscoverySession(); | 224 StopDiscoverySession(); |
| 218 } | 225 } |
| 219 | 226 |
| 220 void BluetoothLowEnergyConnectionFinder::CreateConnection( | 227 void BluetoothLowEnergyConnectionFinder::CreateGattConnection( |
| 221 device::BluetoothDevice* remote_device) { | 228 device::BluetoothDevice* remote_device) { |
| 222 VLOG(1) << "SmartLock service found (" | 229 VLOG(1) << "SmartLock service found (" |
| 223 << remote_service_uuid_.canonical_value() << ")\n" | 230 << remote_service_uuid_.canonical_value() << ")\n" |
| 224 << "device = " << remote_device->GetAddress() | 231 << "device = " << remote_device->GetAddress() |
| 225 << ", name = " << remote_device->GetName(); | 232 << ", name = " << remote_device->GetName(); |
| 226 remote_device->CreateGattConnection( | 233 remote_device->CreateGattConnection( |
| 227 base::Bind(&BluetoothLowEnergyConnectionFinder::OnConnectionCreated, | 234 base::Bind(&BluetoothLowEnergyConnectionFinder::OnGattConnectionCreated, |
| 228 weak_ptr_factory_.GetWeakPtr()), | 235 weak_ptr_factory_.GetWeakPtr()), |
| 229 base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError, | 236 base::Bind( |
| 230 weak_ptr_factory_.GetWeakPtr(), remote_device->GetAddress())); | 237 &BluetoothLowEnergyConnectionFinder::OnCreateGattConnectionError, |
| 238 weak_ptr_factory_.GetWeakPtr(), remote_device->GetAddress())); | |
| 231 } | 239 } |
| 232 | 240 |
| 233 void BluetoothLowEnergyConnectionFinder::CloseConnection( | 241 void BluetoothLowEnergyConnectionFinder::CloseGattConnection( |
| 234 scoped_ptr<device::BluetoothGattConnection> connection) { | 242 scoped_ptr<device::BluetoothGattConnection> gatt_connection) { |
| 235 std::string device_address = connection->GetDeviceAddress(); | 243 DCHECK(gatt_connection); |
| 236 connection.reset(); | 244 std::string device_address = gatt_connection->GetDeviceAddress(); |
| 245 gatt_connection.reset(); | |
| 237 BluetoothDevice* device = adapter_->GetDevice(device_address); | 246 BluetoothDevice* device = adapter_->GetDevice(device_address); |
| 238 if (device) { | 247 if (device) { |
| 239 DCHECK(HasService(device)); | 248 DCHECK(HasService(device)); |
| 240 VLOG(1) << "Forget device " << device->GetAddress(); | 249 VLOG(1) << "Forget device " << device->GetAddress(); |
| 241 device->Forget(base::Bind(&base::DoNothing)); | 250 device->Forget(base::Bind(&base::DoNothing)); |
| 242 } | 251 } |
| 243 } | 252 } |
| 244 | 253 |
| 254 scoped_ptr<Connection> BluetoothLowEnergyConnectionFinder::CreateConnection( | |
| 255 scoped_ptr<BluetoothGattConnection> gatt_connection) { | |
| 256 RemoteDevice remote_device; | |
| 257 remote_device.bluetooth_address = gatt_connection->GetDeviceAddress(); | |
| 258 | |
| 259 return scoped_ptr<BluetoothLowEnergyConnection>( | |
|
Tim Song
2015/05/11 08:02:40
nit: it's a bit shorter to use make_scoped_ptr()
sacomoto
2015/05/11 12:04:04
Done.
| |
| 260 new BluetoothLowEnergyConnection( | |
| 261 remote_device, adapter_, remote_service_uuid_, | |
| 262 to_peripheral_char_uuid_, from_peripheral_char_uuid_, | |
| 263 gatt_connection.Pass())); | |
| 264 } | |
| 265 | |
| 266 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( | |
| 267 Connection* connection, | |
| 268 Connection::Status old_status, | |
| 269 Connection::Status new_status) { | |
| 270 DCHECK_EQ(connection, connection_.get()); | |
| 271 | |
| 272 if (!connection_callback_.is_null() && connection_->IsConnected()) { | |
| 273 connection_->RemoveObserver(this); | |
| 274 connection_callback_.Run(connection_.Pass()); | |
| 275 connection_callback_.Reset(); | |
| 276 } | |
| 277 } | |
| 278 | |
| 245 } // namespace proximity_auth | 279 } // namespace proximity_auth |
| OLD | NEW |