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

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

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

Powered by Google App Engine
This is Rietveld 408576698