| 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 "device/bluetooth/bluetooth_adapter_factory.h" | 13 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 13 #include "device/bluetooth/bluetooth_device.h" | 14 #include "device/bluetooth/bluetooth_device.h" |
| 14 #include "device/bluetooth/bluetooth_discovery_session.h" | 15 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 15 #include "device/bluetooth/bluetooth_uuid.h" | 16 #include "device/bluetooth/bluetooth_uuid.h" |
| 16 | 17 |
| 17 using device::BluetoothAdapter; | 18 using device::BluetoothAdapter; |
| 18 using device::BluetoothDevice; | 19 using device::BluetoothDevice; |
| 19 using device::BluetoothGattConnection; | 20 using device::BluetoothGattConnection; |
| 21 using device::BluetoothDiscoveryFilter; |
| 20 | 22 |
| 21 namespace proximity_auth { | 23 namespace proximity_auth { |
| 22 | 24 |
| 23 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( | 25 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( |
| 24 const std::string& remote_service_uuid) | 26 const std::string& remote_service_uuid) |
| 25 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), | 27 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), |
| 26 connected_(false), | 28 connected_(false), |
| 27 weak_ptr_factory_(this) { | 29 weak_ptr_factory_(this) { |
| 28 } | 30 } |
| 29 | 31 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 52 weak_ptr_factory_.GetWeakPtr())); | 54 weak_ptr_factory_.GetWeakPtr())); |
| 53 } | 55 } |
| 54 | 56 |
| 55 void BluetoothLowEnergyConnectionFinder::Find( | 57 void BluetoothLowEnergyConnectionFinder::Find( |
| 56 const ConnectionCallback& connection_callback) { | 58 const ConnectionCallback& connection_callback) { |
| 57 NOTREACHED(); | 59 NOTREACHED(); |
| 58 } | 60 } |
| 59 | 61 |
| 60 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, | 62 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, |
| 61 BluetoothDevice* device) { | 63 BluetoothDevice* device) { |
| 62 if (device) { | 64 DCHECK(device); |
| 63 VLOG(1) << "New device found: " << device->GetName(); | 65 VLOG(1) << "Device added: " << device->GetAddress(); |
| 64 HandleDeviceAdded(device); | 66 HandleDeviceUpdated(device); |
| 67 } |
| 68 |
| 69 void BluetoothLowEnergyConnectionFinder::DeviceChanged( |
| 70 BluetoothAdapter* adapter, |
| 71 BluetoothDevice* device) { |
| 72 DCHECK(device); |
| 73 VLOG(1) << "Device changed: " << device->GetAddress(); |
| 74 HandleDeviceUpdated(device); |
| 75 } |
| 76 |
| 77 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( |
| 78 BluetoothDevice* device) { |
| 79 if (connected_) |
| 80 return; |
| 81 const auto& i = pending_connections_.find(device); |
| 82 if (i != pending_connections_.end()) { |
| 83 VLOG(1) << "Pending connection to device " << device->GetAddress(); |
| 84 return; |
| 85 } |
| 86 if (HasService(device)) { |
| 87 VLOG(1) << "Connecting to device " << device->GetAddress(); |
| 88 pending_connections_.insert(device); |
| 89 CreateConnection(device); |
| 65 } | 90 } |
| 66 } | 91 } |
| 67 | 92 |
| 93 void BluetoothLowEnergyConnectionFinder::DeviceRemoved( |
| 94 BluetoothAdapter* adapter, |
| 95 BluetoothDevice* device) { |
| 96 if (connected_) |
| 97 return; |
| 98 |
| 99 const auto& i = pending_connections_.find(device); |
| 100 if (i != pending_connections_.end()) { |
| 101 VLOG(1) << "Remove pending connection to " << device->GetAddress(); |
| 102 pending_connections_.erase(i); |
| 103 } |
| 104 } |
| 105 |
| 68 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( | 106 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( |
| 69 scoped_refptr<BluetoothAdapter> adapter) { | 107 scoped_refptr<BluetoothAdapter> adapter) { |
| 70 VLOG(1) << "Adapter ready"; | 108 VLOG(1) << "Adapter ready"; |
| 71 | 109 |
| 72 adapter_ = adapter; | 110 adapter_ = adapter; |
| 73 adapter_->AddObserver(this); | 111 adapter_->AddObserver(this); |
| 74 | 112 |
| 75 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); | 113 // Note: Avoid trying to connect to existing devices as they may be stale. |
| 76 for (auto iter = devices.begin(); iter != devices.end(); iter++) { | 114 // The Bluetooth adapter will fire |OnDeviceChanged| notifications for all |
| 77 HandleDeviceAdded((*iter)); | 115 // not-stale Bluetooth Low Energy devices that are advertising. |
| 116 if (VLOG_IS_ON(1)) { |
| 117 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); |
| 118 for (auto* device : devices) { |
| 119 VLOG(1) << "Ignoring device " << device->GetAddress() |
| 120 << " present when adapter was initialized."; |
| 121 } |
| 78 } | 122 } |
| 79 | 123 |
| 80 StartDiscoverySession(); | 124 StartDiscoverySession(); |
| 81 } | 125 } |
| 82 | 126 |
| 83 void BluetoothLowEnergyConnectionFinder::HandleDeviceAdded( | |
| 84 BluetoothDevice* remote_device) { | |
| 85 if (!connected_ && HasService(remote_device)) { | |
| 86 CreateConnection(remote_device); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( | 127 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( |
| 91 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 128 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 92 VLOG(1) << "Discovery session started"; | 129 VLOG(1) << "Discovery session started"; |
| 93 discovery_session_ = discovery_session.Pass(); | 130 discovery_session_ = discovery_session.Pass(); |
| 94 } | 131 } |
| 95 | 132 |
| 96 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { | 133 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { |
| 97 VLOG(1) << "Error starting discovery session"; | 134 VLOG(1) << "Error starting discovery session"; |
| 98 } | 135 } |
| 99 | 136 |
| 100 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { | 137 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { |
| 101 DCHECK(adapter_); | 138 DCHECK(adapter_); |
| 102 if (discovery_session_ && discovery_session_->IsActive()) { | 139 if (discovery_session_ && discovery_session_->IsActive()) { |
| 103 VLOG(1) << "Discovery session already active"; | 140 VLOG(1) << "Discovery session already active"; |
| 104 return; | 141 return; |
| 105 } | 142 } |
| 143 |
| 106 adapter_->StartDiscoverySession( | 144 adapter_->StartDiscoverySession( |
| 107 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, | 145 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, |
| 108 weak_ptr_factory_.GetWeakPtr()), | 146 weak_ptr_factory_.GetWeakPtr()), |
| 109 base::Bind( | 147 base::Bind( |
| 110 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, | 148 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, |
| 111 weak_ptr_factory_.GetWeakPtr())); | 149 weak_ptr_factory_.GetWeakPtr())); |
| 112 } | 150 } |
| 113 | 151 |
| 114 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped() { | 152 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped() { |
| 115 VLOG(1) << "Discovery session stopped"; | 153 VLOG(1) << "Discovery session stopped"; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 136 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped, | 174 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped, |
| 137 weak_ptr_factory_.GetWeakPtr()), | 175 weak_ptr_factory_.GetWeakPtr()), |
| 138 base::Bind( | 176 base::Bind( |
| 139 &BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError, | 177 &BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError, |
| 140 weak_ptr_factory_.GetWeakPtr())); | 178 weak_ptr_factory_.GetWeakPtr())); |
| 141 } | 179 } |
| 142 | 180 |
| 143 bool BluetoothLowEnergyConnectionFinder::HasService( | 181 bool BluetoothLowEnergyConnectionFinder::HasService( |
| 144 BluetoothDevice* remote_device) { | 182 BluetoothDevice* remote_device) { |
| 145 if (remote_device) { | 183 if (remote_device) { |
| 184 VLOG(1) << "Device " << remote_device->GetAddress() << " has " |
| 185 << remote_device->GetUUIDs().size() << " services."; |
| 146 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs(); | 186 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs(); |
| 147 for (auto iter = uuids.begin(); iter != uuids.end(); iter++) { | 187 for (const auto& service_uuid : uuids) { |
| 148 if (remote_service_uuid_ == *iter) { | 188 if (remote_service_uuid_ == service_uuid) { |
| 149 return true; | 189 return true; |
| 150 } | 190 } |
| 151 } | 191 } |
| 152 } | 192 } |
| 153 return false; | 193 return false; |
| 154 } | 194 } |
| 155 | 195 |
| 156 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( | 196 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( |
| 197 std::string device_address, |
| 157 BluetoothDevice::ConnectErrorCode error_code) { | 198 BluetoothDevice::ConnectErrorCode error_code) { |
| 158 VLOG(1) << "Error creating connection: " << error_code; | 199 VLOG(1) << "Error creating connection to device " << device_address |
| 200 << " : error code = " << error_code; |
| 159 } | 201 } |
| 160 | 202 |
| 161 void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( | 203 void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( |
| 162 scoped_ptr<BluetoothGattConnection> connection) { | 204 scoped_ptr<BluetoothGattConnection> connection) { |
| 205 if (connected_) { |
| 206 CloseConnection(connection.Pass()); |
| 207 return; |
| 208 } |
| 209 |
| 163 VLOG(1) << "Connection created"; | 210 VLOG(1) << "Connection created"; |
| 164 connected_ = true; | 211 connected_ = true; |
| 165 StopDiscoverySession(); | 212 pending_connections_.clear(); |
| 166 if (!connection_callback_.is_null()) { | 213 if (!connection_callback_.is_null()) { |
| 167 connection_callback_.Run(connection.Pass()); | 214 connection_callback_.Run(connection.Pass()); |
| 168 connection_callback_.Reset(); | 215 connection_callback_.Reset(); |
| 169 } | 216 } |
| 217 StopDiscoverySession(); |
| 170 } | 218 } |
| 171 | 219 |
| 172 void BluetoothLowEnergyConnectionFinder::CreateConnection( | 220 void BluetoothLowEnergyConnectionFinder::CreateConnection( |
| 173 device::BluetoothDevice* remote_device) { | 221 device::BluetoothDevice* remote_device) { |
| 174 VLOG(1) << "SmartLock service found (" | 222 VLOG(1) << "SmartLock service found (" |
| 175 << remote_service_uuid_.canonical_value() << ")\n" | 223 << remote_service_uuid_.canonical_value() << ")\n" |
| 176 << "device = " << remote_device->GetAddress() | 224 << "device = " << remote_device->GetAddress() |
| 177 << ", name = " << remote_device->GetName(); | 225 << ", name = " << remote_device->GetName(); |
| 178 remote_device->CreateGattConnection( | 226 remote_device->CreateGattConnection( |
| 179 base::Bind(&BluetoothLowEnergyConnectionFinder::OnConnectionCreated, | 227 base::Bind(&BluetoothLowEnergyConnectionFinder::OnConnectionCreated, |
| 180 weak_ptr_factory_.GetWeakPtr()), | 228 weak_ptr_factory_.GetWeakPtr()), |
| 181 base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError, | 229 base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError, |
| 182 weak_ptr_factory_.GetWeakPtr())); | 230 weak_ptr_factory_.GetWeakPtr(), remote_device->GetAddress())); |
| 231 } |
| 232 |
| 233 void BluetoothLowEnergyConnectionFinder::CloseConnection( |
| 234 scoped_ptr<device::BluetoothGattConnection> connection) { |
| 235 std::string device_address = connection->GetDeviceAddress(); |
| 236 connection.reset(); |
| 237 BluetoothDevice* device = adapter_->GetDevice(device_address); |
| 238 if (device) { |
| 239 DCHECK(HasService(device)); |
| 240 VLOG(1) << "Forget device " << device->GetAddress(); |
| 241 device->Forget(base::Bind(&base::DoNothing)); |
| 242 } |
| 183 } | 243 } |
| 184 | 244 |
| 185 } // namespace proximity_auth | 245 } // namespace proximity_auth |
| OLD | NEW |