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

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

Issue 1113793002: Fix discovery of Smart Lock service in Proximity Auth over Blutooth Low Energy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@eu_4
Patch Set: Add DCHECKs Created 5 years, 7 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/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
25 namespace {
26 void EmptyErrorCallback() {
droger 2015/04/29 14:41:03 Nit: you can use DoNothing() in base/bind_helpers.
msarda 2015/04/29 14:45:11 Done.
27 }
28 } // namespace
29
23 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( 30 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder(
24 const std::string& remote_service_uuid) 31 const std::string& remote_service_uuid)
25 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), 32 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)),
26 connected_(false), 33 connected_(false),
27 weak_ptr_factory_(this) { 34 weak_ptr_factory_(this) {
28 } 35 }
29 36
30 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { 37 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() {
31 if (discovery_session_) { 38 if (discovery_session_) {
32 StopDiscoverySession(); 39 StopDiscoverySession();
(...skipping 19 matching lines...) Expand all
52 weak_ptr_factory_.GetWeakPtr())); 59 weak_ptr_factory_.GetWeakPtr()));
53 } 60 }
54 61
55 void BluetoothLowEnergyConnectionFinder::Find( 62 void BluetoothLowEnergyConnectionFinder::Find(
56 const ConnectionCallback& connection_callback) { 63 const ConnectionCallback& connection_callback) {
57 NOTREACHED(); 64 NOTREACHED();
58 } 65 }
59 66
60 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, 67 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter,
61 BluetoothDevice* device) { 68 BluetoothDevice* device) {
62 if (device) { 69 DCHECK(device);
63 VLOG(1) << "New device found: " << device->GetName(); 70 VLOG(1) << "Device added: " << device->GetAddress();
droger 2015/04/29 14:41:03 Maybe VLOG is fine here, but have you considered u
droger 2015/04/29 14:42:01 I meant DVLOG.
msarda 2015/04/29 14:45:11 We want to use VLOG to be able to debug on Chromeb
64 HandleDeviceAdded(device); 71 HandleDeviceUpdated(device);
72 }
73
74 void BluetoothLowEnergyConnectionFinder::DeviceChanged(
75 BluetoothAdapter* adapter,
76 BluetoothDevice* device) {
77 DCHECK(device);
78 VLOG(1) << "Device changed: " << device->GetAddress();
79 HandleDeviceUpdated(device);
80 }
81
82 void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated(
83 BluetoothDevice* device) {
84 if (connected_)
85 return;
86 const auto& i = pending_connections_.find(device);
87 if (i != pending_connections_.end()) {
88 VLOG(1) << "Pending connection to device " << device->GetAddress();
89 return;
90 }
91 if (HasService(device)) {
92 VLOG(1) << "Connecting to device " << device->GetAddress();
93 pending_connections_.insert(device);
94 CreateConnection(device);
65 } 95 }
66 } 96 }
67 97
98 void BluetoothLowEnergyConnectionFinder::DeviceRemoved(
99 BluetoothAdapter* adapter,
100 BluetoothDevice* device) {
101 if (connected_)
102 return;
103
104 const auto& i = pending_connections_.find(device);
105 if (i != pending_connections_.end()) {
106 VLOG(1) << "Remove pending connection to " << device->GetAddress();
107 pending_connections_.erase(i);
108 }
109 }
110
68 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( 111 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized(
69 scoped_refptr<BluetoothAdapter> adapter) { 112 scoped_refptr<BluetoothAdapter> adapter) {
70 VLOG(1) << "Adapter ready"; 113 VLOG(1) << "Adapter ready";
71 114
72 adapter_ = adapter; 115 adapter_ = adapter;
73 adapter_->AddObserver(this); 116 adapter_->AddObserver(this);
74 117
75 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); 118 // Note: Avoid trying to connect to existing devices as they may be stale.
76 for (auto iter = devices.begin(); iter != devices.end(); iter++) { 119 // The Bluetooth adapter will fire |OnDeviceChanged| notifications for all
77 HandleDeviceAdded((*iter)); 120 // not-stale Bluetooth Low Energy devices that are advertising.
121 if (VLOG_IS_ON(1)) {
122 std::vector<BluetoothDevice*> devices = adapter_->GetDevices();
123 for (auto* device : devices) {
124 VLOG(1) << "Ignoring device " << device->GetAddress()
125 << " present when adapter was initialized.";
126 }
78 } 127 }
79 128
80 StartDiscoverySession(); 129 StartDiscoverySession();
81 } 130 }
82 131
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( 132 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted(
91 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { 133 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
92 VLOG(1) << "Discovery session started"; 134 VLOG(1) << "Discovery session started";
93 discovery_session_ = discovery_session.Pass(); 135 discovery_session_ = discovery_session.Pass();
94 } 136 }
95 137
96 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { 138 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() {
97 VLOG(1) << "Error starting discovery session"; 139 VLOG(1) << "Error starting discovery session";
98 } 140 }
99 141
100 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { 142 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() {
101 DCHECK(adapter_); 143 DCHECK(adapter_);
102 if (discovery_session_ && discovery_session_->IsActive()) { 144 if (discovery_session_ && discovery_session_->IsActive()) {
103 VLOG(1) << "Discovery session already active"; 145 VLOG(1) << "Discovery session already active";
104 return; 146 return;
105 } 147 }
148
106 adapter_->StartDiscoverySession( 149 adapter_->StartDiscoverySession(
107 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, 150 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted,
108 weak_ptr_factory_.GetWeakPtr()), 151 weak_ptr_factory_.GetWeakPtr()),
109 base::Bind( 152 base::Bind(
110 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, 153 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError,
111 weak_ptr_factory_.GetWeakPtr())); 154 weak_ptr_factory_.GetWeakPtr()));
112 } 155 }
113 156
114 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped() { 157 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped() {
115 VLOG(1) << "Discovery session stopped"; 158 VLOG(1) << "Discovery session stopped";
(...skipping 20 matching lines...) Expand all
136 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped, 179 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped,
137 weak_ptr_factory_.GetWeakPtr()), 180 weak_ptr_factory_.GetWeakPtr()),
138 base::Bind( 181 base::Bind(
139 &BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError, 182 &BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError,
140 weak_ptr_factory_.GetWeakPtr())); 183 weak_ptr_factory_.GetWeakPtr()));
141 } 184 }
142 185
143 bool BluetoothLowEnergyConnectionFinder::HasService( 186 bool BluetoothLowEnergyConnectionFinder::HasService(
144 BluetoothDevice* remote_device) { 187 BluetoothDevice* remote_device) {
145 if (remote_device) { 188 if (remote_device) {
189 VLOG(1) << "Device " << remote_device->GetAddress() << " has "
190 << remote_device->GetUUIDs().size() << " services.";
146 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs(); 191 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs();
147 for (auto iter = uuids.begin(); iter != uuids.end(); iter++) { 192 for (const auto& service_uuid : uuids) {
148 if (remote_service_uuid_ == *iter) { 193 if (remote_service_uuid_ == service_uuid) {
149 return true; 194 return true;
150 } 195 }
151 } 196 }
152 } 197 }
153 return false; 198 return false;
154 } 199 }
155 200
156 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( 201 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError(
202 std::string device_address,
157 BluetoothDevice::ConnectErrorCode error_code) { 203 BluetoothDevice::ConnectErrorCode error_code) {
158 VLOG(1) << "Error creating connection: " << error_code; 204 VLOG(1) << "Error creating connection to device " << device_address
205 << " : error code = " << error_code;
159 } 206 }
160 207
161 void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( 208 void BluetoothLowEnergyConnectionFinder::OnConnectionCreated(
162 scoped_ptr<BluetoothGattConnection> connection) { 209 scoped_ptr<BluetoothGattConnection> connection) {
210 if (connected_) {
211 CloseConnection(connection.Pass());
212 return;
213 }
214
163 VLOG(1) << "Connection created"; 215 VLOG(1) << "Connection created";
164 connected_ = true; 216 connected_ = true;
165 StopDiscoverySession(); 217 pending_connections_.clear();
166 if (!connection_callback_.is_null()) { 218 if (!connection_callback_.is_null()) {
167 connection_callback_.Run(connection.Pass()); 219 connection_callback_.Run(connection.Pass());
168 connection_callback_.Reset(); 220 connection_callback_.Reset();
169 } 221 }
222 StopDiscoverySession();
170 } 223 }
171 224
172 void BluetoothLowEnergyConnectionFinder::CreateConnection( 225 void BluetoothLowEnergyConnectionFinder::CreateConnection(
173 device::BluetoothDevice* remote_device) { 226 device::BluetoothDevice* remote_device) {
174 VLOG(1) << "SmartLock service found (" 227 VLOG(1) << "SmartLock service found ("
175 << remote_service_uuid_.canonical_value() << ")\n" 228 << remote_service_uuid_.canonical_value() << ")\n"
176 << "device = " << remote_device->GetAddress() 229 << "device = " << remote_device->GetAddress()
177 << ", name = " << remote_device->GetName(); 230 << ", name = " << remote_device->GetName();
178 remote_device->CreateGattConnection( 231 remote_device->CreateGattConnection(
179 base::Bind(&BluetoothLowEnergyConnectionFinder::OnConnectionCreated, 232 base::Bind(&BluetoothLowEnergyConnectionFinder::OnConnectionCreated,
180 weak_ptr_factory_.GetWeakPtr()), 233 weak_ptr_factory_.GetWeakPtr()),
181 base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError, 234 base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError,
182 weak_ptr_factory_.GetWeakPtr())); 235 weak_ptr_factory_.GetWeakPtr(), remote_device->GetAddress()));
236 }
237
238 void BluetoothLowEnergyConnectionFinder::CloseConnection(
239 scoped_ptr<device::BluetoothGattConnection> connection) {
240 std::string device_address = connection->GetDeviceAddress();
241 connection.reset();
242 BluetoothDevice* device = adapter_->GetDevice(device_address);
243 if (device) {
244 DCHECK(HasService(device));
245 VLOG(1) << "Forget device " << device->GetAddress();
246 device->Forget(base::Bind(&EmptyErrorCallback));
247 }
183 } 248 }
184 249
185 } // namespace proximity_auth 250 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698