| 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/bluetooth_low_energy_connection_finder.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "components/cryptauth/ble/bluetooth_low_energy_weave_client_connection.
h" |
| 18 #include "components/cryptauth/bluetooth_throttler.h" | 19 #include "components/cryptauth/bluetooth_throttler.h" |
| 19 #include "components/cryptauth/connection.h" | 20 #include "components/cryptauth/connection.h" |
| 20 #include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | |
| 21 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | |
| 22 #include "components/proximity_auth/logging/logging.h" | 21 #include "components/proximity_auth/logging/logging.h" |
| 23 #include "device/bluetooth/bluetooth_adapter_factory.h" | 22 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 24 #include "device/bluetooth/bluetooth_common.h" | 23 #include "device/bluetooth/bluetooth_common.h" |
| 25 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
| 26 #include "device/bluetooth/bluetooth_discovery_session.h" | 25 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 27 #include "device/bluetooth/bluetooth_uuid.h" | 26 #include "device/bluetooth/bluetooth_uuid.h" |
| 28 | 27 |
| 29 using device::BluetoothAdapter; | 28 using device::BluetoothAdapter; |
| 30 using device::BluetoothDevice; | 29 using device::BluetoothDevice; |
| 31 using device::BluetoothGattConnection; | 30 using device::BluetoothGattConnection; |
| 32 using device::BluetoothDiscoveryFilter; | 31 using device::BluetoothDiscoveryFilter; |
| 33 | 32 |
| 34 namespace proximity_auth { | 33 namespace proximity_auth { |
| 35 namespace { | 34 namespace { |
| 36 const int kMinDiscoveryRSSI = -90; | 35 const char kAdvertisementUUID[] = "0000fe50-0000-1000-8000-00805f9b34fb"; |
| 36 const char kBLEGattServiceUUID[] = "b3b7e28e-a000-3e17-bd86-6e97b9e28c11"; |
| 37 const int kRestartDiscoveryOnErrorDelaySeconds = 2; |
| 37 } // namespace | 38 } // namespace |
| 38 | 39 |
| 39 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( | 40 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( |
| 40 const cryptauth::RemoteDevice remote_device, | 41 const cryptauth::RemoteDevice remote_device, |
| 41 const std::string& remote_service_uuid, | 42 const std::vector<cryptauth::BeaconSeed>& beacon_seeds, |
| 42 FinderStrategy finder_strategy, | 43 cryptauth::BluetoothThrottler* bluetooth_throttler) |
| 43 const BluetoothLowEnergyDeviceWhitelist* device_whitelist, | 44 : BluetoothLowEnergyConnectionFinder( |
| 44 cryptauth::BluetoothThrottler* bluetooth_throttler, | 45 remote_device, |
| 45 int max_number_of_tries) | 46 beacon_seeds, |
| 47 base::MakeUnique<cryptauth::BackgroundEidGenerator>(), |
| 48 bluetooth_throttler) {} |
| 49 |
| 50 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( |
| 51 const cryptauth::RemoteDevice remote_device, |
| 52 const std::vector<cryptauth::BeaconSeed>& beacon_seeds, |
| 53 std::unique_ptr<cryptauth::BackgroundEidGenerator> eid_generator, |
| 54 cryptauth::BluetoothThrottler* bluetooth_throttler) |
| 46 : remote_device_(remote_device), | 55 : remote_device_(remote_device), |
| 47 remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), | 56 beacon_seeds_(beacon_seeds), |
| 48 finder_strategy_(finder_strategy), | 57 eid_generator_(std::move(eid_generator)), |
| 49 device_whitelist_(device_whitelist), | |
| 50 bluetooth_throttler_(bluetooth_throttler), | 58 bluetooth_throttler_(bluetooth_throttler), |
| 51 max_number_of_tries_(max_number_of_tries), | 59 weak_ptr_factory_(this) {} |
| 52 weak_ptr_factory_(this) { | |
| 53 DCHECK(finder_strategy_ == FIND_ANY_DEVICE || | |
| 54 !remote_device.bluetooth_address.empty()); | |
| 55 } | |
| 56 | 60 |
| 57 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { | 61 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { |
| 58 if (discovery_session_) { | 62 if (discovery_session_) { |
| 59 StopDiscoverySession(); | 63 StopDiscoverySession(); |
| 60 } | 64 } |
| 61 | 65 |
| 62 if (connection_) { | 66 if (connection_) { |
| 63 connection_->RemoveObserver(this); | 67 connection_->RemoveObserver(this); |
| 64 connection_.reset(); | 68 connection_.reset(); |
| 65 } | 69 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 136 |
| 133 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( | 137 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( |
| 134 BluetoothDevice* device) { | 138 BluetoothDevice* device) { |
| 135 // Ensuring only one call to |CreateConnection()| is made. A new |connection_| | 139 // Ensuring only one call to |CreateConnection()| is made. A new |connection_| |
| 136 // can be created only when the previous one disconnects, triggering a call to | 140 // can be created only when the previous one disconnects, triggering a call to |
| 137 // |OnConnectionStatusChanged|. | 141 // |OnConnectionStatusChanged|. |
| 138 if (connection_) | 142 if (connection_) |
| 139 return; | 143 return; |
| 140 | 144 |
| 141 if (IsRightDevice(device)) { | 145 if (IsRightDevice(device)) { |
| 142 PA_LOG(INFO) << "Connecting to device " << device->GetAddress() | 146 PA_LOG(INFO) << "Connecting to device " << device->GetAddress(); |
| 143 << " with service (" << HasService(device) | |
| 144 << ") and is paired (" << device->IsPaired(); | |
| 145 | |
| 146 connection_ = CreateConnection(device->GetAddress()); | 147 connection_ = CreateConnection(device->GetAddress()); |
| 147 connection_->AddObserver(this); | 148 connection_->AddObserver(this); |
| 148 connection_->Connect(); | 149 connection_->Connect(); |
| 149 | 150 |
| 150 StopDiscoverySession(); | 151 StopDiscoverySession(); |
| 151 } | 152 } |
| 152 } | 153 } |
| 153 | 154 |
| 154 bool BluetoothLowEnergyConnectionFinder::IsRightDevice( | 155 bool BluetoothLowEnergyConnectionFinder::IsRightDevice( |
| 155 BluetoothDevice* device) { | 156 BluetoothDevice* device) { |
| 156 if (!device) | 157 if (!device) |
| 157 return false; | 158 return false; |
| 158 | 159 |
| 159 // TODO(sacomoto): Remove it when ProximityAuthBleSystem is not needed | 160 device::BluetoothUUID advertisement_uuid(kAdvertisementUUID); |
| 160 // anymore. | 161 const std::vector<uint8_t>* service_data = |
| 161 if (device_whitelist_) | 162 device->GetServiceDataForUUID(advertisement_uuid); |
| 162 return device->IsPaired() && | 163 if (!service_data) |
| 163 (HasService(device) || | 164 return false; |
| 164 device_whitelist_->HasDeviceWithAddress(device->GetAddress())); | |
| 165 | 165 |
| 166 // The device should be paired when looking for BLE devices by bluetooth | 166 std::string service_data_string(service_data->begin(), service_data->end()); |
| 167 // address. | 167 std::vector<std::string> nearest_eids = |
| 168 if (finder_strategy_ == FIND_PAIRED_DEVICE) | 168 eid_generator_->GenerateNearestEids(beacon_seeds_); |
| 169 return device->IsPaired() && | 169 for (const std::string& eid : nearest_eids) { |
| 170 device->GetAddress() == remote_device_.bluetooth_address; | 170 if (eid == service_data_string) { |
| 171 return HasService(device); | 171 PA_LOG(INFO) << "Found a matching EID: " << eid; |
| 172 } | 172 return true; |
| 173 | 173 } |
| 174 bool BluetoothLowEnergyConnectionFinder::HasService( | |
| 175 BluetoothDevice* remote_device) { | |
| 176 if (!remote_device) { | |
| 177 return false; | |
| 178 } | 174 } |
| 179 | 175 return false; |
| 180 BluetoothDevice::UUIDSet uuids = remote_device->GetUUIDs(); | |
| 181 | |
| 182 PA_LOG(INFO) << "Device " << remote_device->GetAddress() << " has " | |
| 183 << uuids.size() << " services."; | |
| 184 return base::ContainsKey(uuids, remote_service_uuid_); | |
| 185 } | 176 } |
| 186 | 177 |
| 187 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( | 178 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( |
| 188 scoped_refptr<BluetoothAdapter> adapter) { | 179 scoped_refptr<BluetoothAdapter> adapter) { |
| 189 PA_LOG(INFO) << "Adapter ready"; | 180 PA_LOG(INFO) << "Adapter ready"; |
| 190 | |
| 191 adapter_ = adapter; | 181 adapter_ = adapter; |
| 192 adapter_->AddObserver(this); | 182 adapter_->AddObserver(this); |
| 193 | |
| 194 // This is important for debugging. To eliminate the case where the device was | |
| 195 // removed (forgotten) by the user, or BlueZ didn't load the device correctly. | |
| 196 if (finder_strategy_ == FIND_PAIRED_DEVICE) { | |
| 197 PA_LOG(INFO) << "Looking for paired device: " | |
| 198 << remote_device_.bluetooth_address; | |
| 199 for (const auto* device : adapter_->GetDevices()) { | |
| 200 if (device->IsPaired()) | |
| 201 PA_LOG(INFO) << device->GetAddress() << " is paired"; | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 // Note: It's possible to connect to the paired directly, so when using | |
| 206 // FIND_PAIRED_DEVICE strategy this is not necessary. However, the discovery | |
| 207 // doesn't add a lot of latency, and the makes the code path for both | |
| 208 // strategies more similar. | |
| 209 StartDiscoverySession(); | 183 StartDiscoverySession(); |
| 210 } | 184 } |
| 211 | 185 |
| 212 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( | 186 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( |
| 213 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { | 187 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 214 PA_LOG(INFO) << "Discovery session started"; | 188 PA_LOG(INFO) << "Discovery session started"; |
| 215 discovery_session_ = std::move(discovery_session); | 189 discovery_session_ = std::move(discovery_session); |
| 216 } | 190 } |
| 217 | 191 |
| 218 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { | 192 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { |
| 219 PA_LOG(WARNING) << "Error starting discovery session"; | 193 PA_LOG(WARNING) << "Error starting discovery session, restarting in " |
| 194 << kRestartDiscoveryOnErrorDelaySeconds << " seconds."; |
| 195 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 196 FROM_HERE, |
| 197 base::Bind( |
| 198 &BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync, |
| 199 weak_ptr_factory_.GetWeakPtr()), |
| 200 base::TimeDelta::FromSeconds(kRestartDiscoveryOnErrorDelaySeconds)); |
| 220 } | 201 } |
| 221 | 202 |
| 222 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { | 203 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { |
| 223 DCHECK(adapter_); | 204 DCHECK(adapter_); |
| 224 if (discovery_session_ && discovery_session_->IsActive()) { | 205 if (discovery_session_ && discovery_session_->IsActive()) { |
| 225 PA_LOG(INFO) << "Discovery session already active"; | 206 PA_LOG(INFO) << "Discovery session already active"; |
| 226 return; | 207 return; |
| 227 } | 208 } |
| 228 | 209 |
| 229 // Discover only low energy (LE) devices with strong enough signal. | 210 // Discover only low energy (LE) devices. |
| 230 std::unique_ptr<BluetoothDiscoveryFilter> filter( | 211 std::unique_ptr<BluetoothDiscoveryFilter> filter( |
| 231 new BluetoothDiscoveryFilter(device::BLUETOOTH_TRANSPORT_LE)); | 212 new BluetoothDiscoveryFilter(device::BLUETOOTH_TRANSPORT_LE)); |
| 232 filter->SetRSSI(kMinDiscoveryRSSI); | |
| 233 | 213 |
| 234 adapter_->StartDiscoverySessionWithFilter( | 214 adapter_->StartDiscoverySessionWithFilter( |
| 235 std::move(filter), | 215 std::move(filter), |
| 236 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, | 216 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, |
| 237 weak_ptr_factory_.GetWeakPtr()), | 217 weak_ptr_factory_.GetWeakPtr()), |
| 238 base::Bind( | 218 base::Bind( |
| 239 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, | 219 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, |
| 240 weak_ptr_factory_.GetWeakPtr())); | 220 weak_ptr_factory_.GetWeakPtr())); |
| 241 } | 221 } |
| 242 | 222 |
| 243 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { | 223 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { |
| 244 PA_LOG(INFO) << "Stopping discovery session"; | 224 PA_LOG(INFO) << "Stopping discovery session"; |
| 245 // Destroying the discovery session also stops it. | 225 // Destroying the discovery session also stops it. |
| 246 discovery_session_.reset(); | 226 discovery_session_.reset(); |
| 247 } | 227 } |
| 248 | 228 |
| 249 std::unique_ptr<cryptauth::Connection> | 229 std::unique_ptr<cryptauth::Connection> |
| 250 BluetoothLowEnergyConnectionFinder::CreateConnection( | 230 BluetoothLowEnergyConnectionFinder::CreateConnection( |
| 251 const std::string& device_address) { | 231 const std::string& device_address) { |
| 252 DCHECK(remote_device_.bluetooth_address.empty() || | 232 return cryptauth::weave::BluetoothLowEnergyWeaveClientConnection::Factory:: |
| 253 remote_device_.bluetooth_address == device_address); | 233 NewInstance(remote_device_, device_address, adapter_, |
| 254 remote_device_.bluetooth_address = device_address; | 234 device::BluetoothUUID(kBLEGattServiceUUID), |
| 255 return base::MakeUnique<BluetoothLowEnergyConnection>( | 235 bluetooth_throttler_); |
| 256 remote_device_, adapter_, remote_service_uuid_, bluetooth_throttler_, | |
| 257 max_number_of_tries_); | |
| 258 } | 236 } |
| 259 | 237 |
| 260 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( | 238 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( |
| 261 cryptauth::Connection* connection, | 239 cryptauth::Connection* connection, |
| 262 cryptauth::Connection::Status old_status, | 240 cryptauth::Connection::Status old_status, |
| 263 cryptauth::Connection::Status new_status) { | 241 cryptauth::Connection::Status new_status) { |
| 264 DCHECK_EQ(connection, connection_.get()); | 242 DCHECK_EQ(connection, connection_.get()); |
| 265 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " | 243 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " |
| 266 << new_status; | 244 << new_status; |
| 267 | 245 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 288 } | 266 } |
| 289 } | 267 } |
| 290 | 268 |
| 291 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() { | 269 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() { |
| 292 PA_LOG(INFO) << "Restarting discovery session."; | 270 PA_LOG(INFO) << "Restarting discovery session."; |
| 293 connection_.reset(); | 271 connection_.reset(); |
| 294 if (!discovery_session_ || !discovery_session_->IsActive()) | 272 if (!discovery_session_ || !discovery_session_->IsActive()) |
| 295 StartDiscoverySession(); | 273 StartDiscoverySession(); |
| 296 } | 274 } |
| 297 | 275 |
| 298 BluetoothDevice* BluetoothLowEnergyConnectionFinder::GetDevice( | |
| 299 const std::string& device_address) { | |
| 300 // It's not possible to simply use | |
| 301 // |adapter_->GetDevice(GetRemoteDeviceAddress())| to find the device with MAC | |
| 302 // address |GetRemoteDeviceAddress()|. For paired devices, | |
| 303 // BluetoothAdapter::GetDevice(XXX) searches for the temporary MAC address | |
| 304 // XXX, whereas |remote_device_.bluetooth_address| is the real MAC address. | |
| 305 // This is a bug in the way device::BluetoothAdapter is storing the devices | |
| 306 // (see crbug.com/497841). | |
| 307 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); | |
| 308 for (auto* device : devices) { | |
| 309 if (device->GetAddress() == device_address) | |
| 310 return device; | |
| 311 } | |
| 312 return nullptr; | |
| 313 } | |
| 314 | |
| 315 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { | 276 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() { |
| 316 connection_callback_.Run(std::move(connection_)); | 277 connection_callback_.Run(std::move(connection_)); |
| 317 } | 278 } |
| 318 | 279 |
| 319 } // namespace proximity_auth | 280 } // namespace proximity_auth |
| OLD | NEW |