Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: components/proximity_auth/bluetooth_low_energy_connection_finder.cc

Issue 2841743003: [EasyUnlock] Update BluetoothLowEnergyConnectionFinder to look for EIDs. (Closed)
Patch Set: [EasyUnlock] Update BluetoothLowEnergyConnectionFinder to look for EIDs. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 std::unique_ptr<cryptauth::RemoteBeaconSeedFetcher> beacon_seed_fetcher,
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 std::move(beacon_seed_fetcher),
47 base::MakeUnique<cryptauth::BackgroundEidGenerator>(),
48 bluetooth_throttler) {}
49
50 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder(
51 const cryptauth::RemoteDevice remote_device,
52 std::unique_ptr<cryptauth::RemoteBeaconSeedFetcher> beacon_seed_fetcher,
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_seed_fetcher_(std::move(beacon_seed_fetcher)),
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 }
66 70
67 if (adapter_) { 71 if (adapter_) {
68 adapter_->RemoveObserver(this); 72 adapter_->RemoveObserver(this);
69 adapter_ = NULL; 73 adapter_ = NULL;
70 } 74 }
71 } 75 }
72 76
73 void BluetoothLowEnergyConnectionFinder::Find( 77 void BluetoothLowEnergyConnectionFinder::Find(
74 const cryptauth::ConnectionFinder::ConnectionCallback& 78 const cryptauth::ConnectionFinder::ConnectionCallback&
75 connection_callback) { 79 connection_callback) {
76 if (!device::BluetoothAdapterFactory::IsBluetoothSupported()) { 80 if (!device::BluetoothAdapterFactory::IsBluetoothSupported()) {
77 PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting."; 81 PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting.";
78 return; 82 return;
79 } 83 }
80 PA_LOG(INFO) << "Finding connection"; 84 PA_LOG(INFO) << "Finding connection";
81 85
82 connection_callback_ = connection_callback; 86 connection_callback_ = connection_callback;
83 87
88 std::vector<cryptauth::BeaconSeed> beacon_seeds;
89 if (!beacon_seed_fetcher_->FetchSeedsForDevice(remote_device_,
90 &beacon_seeds)) {
91 PA_LOG(ERROR) << "Unable to fetch beacon seeds for " << remote_device_.name;
92 return;
sacomoto 2017/04/25 09:45:00 I think this is a bit awkward. This method is a no
Tim Song 2017/04/25 20:37:47 I changed the interface to accept the BeaconSeeds
93 }
94 nearest_eids_ = eid_generator_->GenerateNearestEids(beacon_seeds);
sacomoto 2017/04/25 09:45:00 Shouldn't we recompute this periodically? What if
Tim Song 2017/04/25 20:37:47 This should be very unlikely as the Chromebook wil
95
84 device::BluetoothAdapterFactory::GetAdapter( 96 device::BluetoothAdapterFactory::GetAdapter(
85 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, 97 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized,
86 weak_ptr_factory_.GetWeakPtr())); 98 weak_ptr_factory_.GetWeakPtr()));
87 } 99 }
88 100
89 // It's not necessary to observe |AdapterPresentChanged| too. When |adapter_| is 101 // It's not necessary to observe |AdapterPresentChanged| too. When |adapter_| is
90 // present, but not powered, it's not possible to scan for new devices. 102 // present, but not powered, it's not possible to scan for new devices.
91 void BluetoothLowEnergyConnectionFinder::AdapterPoweredChanged( 103 void BluetoothLowEnergyConnectionFinder::AdapterPoweredChanged(
92 BluetoothAdapter* adapter, 104 BluetoothAdapter* adapter,
93 bool powered) { 105 bool powered) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 144
133 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( 145 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated(
134 BluetoothDevice* device) { 146 BluetoothDevice* device) {
135 // Ensuring only one call to |CreateConnection()| is made. A new |connection_| 147 // 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 148 // can be created only when the previous one disconnects, triggering a call to
137 // |OnConnectionStatusChanged|. 149 // |OnConnectionStatusChanged|.
138 if (connection_) 150 if (connection_)
139 return; 151 return;
140 152
141 if (IsRightDevice(device)) { 153 if (IsRightDevice(device)) {
142 PA_LOG(INFO) << "Connecting to device " << device->GetAddress() 154 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()); 155 connection_ = CreateConnection(device->GetAddress());
147 connection_->AddObserver(this); 156 connection_->AddObserver(this);
148 connection_->Connect(); 157 connection_->Connect();
149 158
150 StopDiscoverySession(); 159 StopDiscoverySession();
151 } 160 }
152 } 161 }
153 162
154 bool BluetoothLowEnergyConnectionFinder::IsRightDevice( 163 bool BluetoothLowEnergyConnectionFinder::IsRightDevice(
155 BluetoothDevice* device) { 164 BluetoothDevice* device) {
156 if (!device) 165 if (!device)
157 return false; 166 return false;
158 167
159 // TODO(sacomoto): Remove it when ProximityAuthBleSystem is not needed 168 device::BluetoothUUID advertisement_uuid(kAdvertisementUUID);
160 // anymore. 169 const std::vector<uint8_t>* service_data =
161 if (device_whitelist_) 170 device->GetServiceDataForUUID(advertisement_uuid);
162 return device->IsPaired() && 171 if (!service_data)
163 (HasService(device) || 172 return false;
164 device_whitelist_->HasDeviceWithAddress(device->GetAddress()));
165 173
166 // The device should be paired when looking for BLE devices by bluetooth 174 std::string service_data_string(service_data->begin(), service_data->end());
167 // address. 175 for (const std::string& eid : nearest_eids_) {
168 if (finder_strategy_ == FIND_PAIRED_DEVICE) 176 if (eid == service_data_string) {
169 return device->IsPaired() && 177 PA_LOG(INFO) << "Found a matching EID: " << eid;
170 device->GetAddress() == remote_device_.bluetooth_address; 178 return true;
171 return HasService(device); 179 }
172 }
173
174 bool BluetoothLowEnergyConnectionFinder::HasService(
175 BluetoothDevice* remote_device) {
176 if (!remote_device) {
177 return false;
178 } 180 }
179 181 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 } 182 }
186 183
187 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( 184 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized(
188 scoped_refptr<BluetoothAdapter> adapter) { 185 scoped_refptr<BluetoothAdapter> adapter) {
189 PA_LOG(INFO) << "Adapter ready"; 186 PA_LOG(INFO) << "Adapter ready";
190
191 adapter_ = adapter; 187 adapter_ = adapter;
192 adapter_->AddObserver(this); 188 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(); 189 StartDiscoverySession();
210 } 190 }
211 191
212 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( 192 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted(
213 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { 193 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) {
214 PA_LOG(INFO) << "Discovery session started"; 194 PA_LOG(INFO) << "Discovery session started";
215 discovery_session_ = std::move(discovery_session); 195 discovery_session_ = std::move(discovery_session);
216 } 196 }
217 197
218 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { 198 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() {
219 PA_LOG(WARNING) << "Error starting discovery session"; 199 PA_LOG(WARNING) << "Error starting discovery session, restarting in "
200 << kRestartDiscoveryOnErrorDelaySeconds << " seconds.";
201 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
202 FROM_HERE,
203 base::Bind(
204 &BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync,
205 weak_ptr_factory_.GetWeakPtr()),
206 base::TimeDelta::FromSeconds(kRestartDiscoveryOnErrorDelaySeconds));
220 } 207 }
221 208
222 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { 209 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() {
223 DCHECK(adapter_); 210 DCHECK(adapter_);
224 if (discovery_session_ && discovery_session_->IsActive()) { 211 if (discovery_session_ && discovery_session_->IsActive()) {
225 PA_LOG(INFO) << "Discovery session already active"; 212 PA_LOG(INFO) << "Discovery session already active";
226 return; 213 return;
227 } 214 }
228 215
229 // Discover only low energy (LE) devices with strong enough signal. 216 // Discover only low energy (LE) devices.
230 std::unique_ptr<BluetoothDiscoveryFilter> filter( 217 std::unique_ptr<BluetoothDiscoveryFilter> filter(
231 new BluetoothDiscoveryFilter(device::BLUETOOTH_TRANSPORT_LE)); 218 new BluetoothDiscoveryFilter(device::BLUETOOTH_TRANSPORT_LE));
232 filter->SetRSSI(kMinDiscoveryRSSI);
233 219
234 adapter_->StartDiscoverySessionWithFilter( 220 adapter_->StartDiscoverySessionWithFilter(
235 std::move(filter), 221 std::move(filter),
236 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, 222 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted,
237 weak_ptr_factory_.GetWeakPtr()), 223 weak_ptr_factory_.GetWeakPtr()),
238 base::Bind( 224 base::Bind(
239 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, 225 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError,
240 weak_ptr_factory_.GetWeakPtr())); 226 weak_ptr_factory_.GetWeakPtr()));
241 } 227 }
242 228
243 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { 229 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() {
244 PA_LOG(INFO) << "Stopping discovery session"; 230 PA_LOG(INFO) << "Stopping discovery session";
245 // Destroying the discovery session also stops it. 231 // Destroying the discovery session also stops it.
246 discovery_session_.reset(); 232 discovery_session_.reset();
247 } 233 }
248 234
249 std::unique_ptr<cryptauth::Connection> 235 std::unique_ptr<cryptauth::Connection>
250 BluetoothLowEnergyConnectionFinder::CreateConnection( 236 BluetoothLowEnergyConnectionFinder::CreateConnection(
251 const std::string& device_address) { 237 const std::string& device_address) {
252 DCHECK(remote_device_.bluetooth_address.empty() || 238 return cryptauth::weave::BluetoothLowEnergyWeaveClientConnection::Factory::
253 remote_device_.bluetooth_address == device_address); 239 NewInstance(remote_device_, device_address, adapter_,
254 remote_device_.bluetooth_address = device_address; 240 device::BluetoothUUID(kBLEGattServiceUUID),
255 return base::MakeUnique<BluetoothLowEnergyConnection>( 241 bluetooth_throttler_);
256 remote_device_, adapter_, remote_service_uuid_, bluetooth_throttler_,
257 max_number_of_tries_);
258 } 242 }
259 243
260 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( 244 void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged(
261 cryptauth::Connection* connection, 245 cryptauth::Connection* connection,
262 cryptauth::Connection::Status old_status, 246 cryptauth::Connection::Status old_status,
263 cryptauth::Connection::Status new_status) { 247 cryptauth::Connection::Status new_status) {
264 DCHECK_EQ(connection, connection_.get()); 248 DCHECK_EQ(connection, connection_.get());
265 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> " 249 PA_LOG(INFO) << "OnConnectionStatusChanged: " << old_status << " -> "
266 << new_status; 250 << new_status;
267 251
(...skipping 20 matching lines...) Expand all
288 } 272 }
289 } 273 }
290 274
291 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() { 275 void BluetoothLowEnergyConnectionFinder::RestartDiscoverySessionAsync() {
292 PA_LOG(INFO) << "Restarting discovery session."; 276 PA_LOG(INFO) << "Restarting discovery session.";
293 connection_.reset(); 277 connection_.reset();
294 if (!discovery_session_ || !discovery_session_->IsActive()) 278 if (!discovery_session_ || !discovery_session_->IsActive())
295 StartDiscoverySession(); 279 StartDiscoverySession();
296 } 280 }
297 281
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() { 282 void BluetoothLowEnergyConnectionFinder::InvokeCallbackAsync() {
316 connection_callback_.Run(std::move(connection_)); 283 connection_callback_.Run(std::move(connection_));
317 } 284 }
318 285
319 } // namespace proximity_auth 286 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698