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