OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | |
12 #include "base/metrics/histogram.h" | |
13 #include "base/sequenced_task_runner.h" | |
14 #include "base/single_thread_task_runner.h" | |
15 #include "base/thread_task_runner_handle.h" | |
16 #include "chromeos/system/devicetype.h" | |
17 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | |
18 #include "device/bluetooth/bluetooth_advertisement_chromeos.h" | |
19 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h" | |
20 #include "device/bluetooth/bluetooth_device.h" | |
21 #include "device/bluetooth/bluetooth_device_chromeos.h" | |
22 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | |
23 #include "device/bluetooth/bluetooth_pairing_chromeos.h" | |
24 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" | |
25 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" | |
26 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" | |
27 #include "device/bluetooth/bluetooth_socket_chromeos.h" | |
28 #include "device/bluetooth/bluetooth_socket_thread.h" | |
29 #include "device/bluetooth/bluetooth_uuid.h" | |
30 #include "device/bluetooth/dbus/bluetooth_adapter_client.h" | |
31 #include "device/bluetooth/dbus/bluetooth_agent_manager_client.h" | |
32 #include "device/bluetooth/dbus/bluetooth_agent_service_provider.h" | |
33 #include "device/bluetooth/dbus/bluetooth_device_client.h" | |
34 #include "device/bluetooth/dbus/bluetooth_input_client.h" | |
35 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
36 #include "third_party/cros_system_api/dbus/service_constants.h" | |
37 | |
38 using device::BluetoothAdapter; | |
39 using device::BluetoothAudioSink; | |
40 using device::BluetoothDevice; | |
41 using device::BluetoothDiscoveryFilter; | |
42 using device::BluetoothSocket; | |
43 using device::BluetoothUUID; | |
44 using device::UMABluetoothDiscoverySessionOutcome; | |
45 | |
46 namespace { | |
47 | |
48 // The agent path is relatively meaningless since BlueZ only permits one to | |
49 // exist per D-Bus connection, it just has to be unique within Chromium. | |
50 const char kAgentPath[] = "/org/chromium/bluetooth_agent"; | |
51 | |
52 void OnUnregisterAgentError(const std::string& error_name, | |
53 const std::string& error_message) { | |
54 // It's okay if the agent didn't exist, it means we never saw an adapter. | |
55 if (error_name == bluetooth_agent_manager::kErrorDoesNotExist) | |
56 return; | |
57 | |
58 LOG(WARNING) << "Failed to unregister pairing agent: " | |
59 << error_name << ": " << error_message; | |
60 } | |
61 | |
62 UMABluetoothDiscoverySessionOutcome TranslateDiscoveryErrorToUMA( | |
63 const std::string& error_name) { | |
64 if (error_name == bluez::BluetoothAdapterClient::kUnknownAdapterError) { | |
65 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_UNKNOWN_ADAPTER; | |
66 } else if (error_name == bluez::BluetoothAdapterClient::kNoResponseError) { | |
67 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_NO_RESPONSE; | |
68 } else if (error_name == bluetooth_device::kErrorInProgress) { | |
69 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_IN_PROGRESS; | |
70 } else if (error_name == bluetooth_device::kErrorNotReady) { | |
71 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_NOT_READY; | |
72 } else if (error_name == bluetooth_device::kErrorFailed) { | |
73 return UMABluetoothDiscoverySessionOutcome::FAILED; | |
74 } else { | |
75 LOG(WARNING) << "Can't histogram DBus error " << error_name; | |
76 return UMABluetoothDiscoverySessionOutcome::UNKNOWN; | |
77 } | |
78 } | |
79 | |
80 } // namespace | |
81 | |
82 namespace device { | |
83 | |
84 // static | |
85 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( | |
86 const InitCallback& init_callback) { | |
87 return chromeos::BluetoothAdapterChromeOS::CreateAdapter(); | |
88 } | |
89 | |
90 } // namespace device | |
91 | |
92 namespace chromeos { | |
93 | |
94 // static | |
95 base::WeakPtr<BluetoothAdapter> BluetoothAdapterChromeOS::CreateAdapter() { | |
96 BluetoothAdapterChromeOS* adapter = new BluetoothAdapterChromeOS(); | |
97 return adapter->weak_ptr_factory_.GetWeakPtr(); | |
98 } | |
99 | |
100 void BluetoothAdapterChromeOS::Shutdown() { | |
101 if (dbus_is_shutdown_) | |
102 return; | |
103 DCHECK(bluez::BluezDBusManager::IsInitialized()) | |
104 << "Call BluetoothAdapterFactory::Shutdown() before " | |
105 "BluezDBusManager::Shutdown()."; | |
106 | |
107 if (IsPresent()) | |
108 RemoveAdapter(); // Also deletes devices_. | |
109 DCHECK(devices_.empty()); | |
110 // profiles_ is empty because all BluetoothSockets have been notified | |
111 // that this adapter is disappearing. | |
112 DCHECK(profiles_.empty()); | |
113 | |
114 for (auto& it : profile_queues_) | |
115 delete it.second; | |
116 profile_queues_.clear(); | |
117 | |
118 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->RemoveObserver( | |
119 this); | |
120 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver( | |
121 this); | |
122 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->RemoveObserver( | |
123 this); | |
124 | |
125 VLOG(1) << "Unregistering pairing agent"; | |
126 bluez::BluezDBusManager::Get() | |
127 ->GetBluetoothAgentManagerClient() | |
128 ->UnregisterAgent(dbus::ObjectPath(kAgentPath), | |
129 base::Bind(&base::DoNothing), | |
130 base::Bind(&OnUnregisterAgentError)); | |
131 | |
132 agent_.reset(); | |
133 dbus_is_shutdown_ = true; | |
134 } | |
135 | |
136 BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() | |
137 : dbus_is_shutdown_(false), | |
138 num_discovery_sessions_(0), | |
139 discovery_request_pending_(false), | |
140 weak_ptr_factory_(this) { | |
141 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
142 socket_thread_ = device::BluetoothSocketThread::Get(); | |
143 | |
144 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->AddObserver( | |
145 this); | |
146 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); | |
147 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->AddObserver(this); | |
148 | |
149 // Register the pairing agent. | |
150 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus(); | |
151 agent_.reset(bluez::BluetoothAgentServiceProvider::Create( | |
152 system_bus, dbus::ObjectPath(kAgentPath), this)); | |
153 DCHECK(agent_.get()); | |
154 | |
155 std::vector<dbus::ObjectPath> object_paths = bluez::BluezDBusManager::Get() | |
156 ->GetBluetoothAdapterClient() | |
157 ->GetAdapters(); | |
158 | |
159 if (!object_paths.empty()) { | |
160 VLOG(1) << object_paths.size() << " Bluetooth adapter(s) available."; | |
161 SetAdapter(object_paths[0]); | |
162 } | |
163 } | |
164 | |
165 BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() { | |
166 Shutdown(); | |
167 } | |
168 | |
169 std::string BluetoothAdapterChromeOS::GetAddress() const { | |
170 if (!IsPresent()) | |
171 return std::string(); | |
172 | |
173 bluez::BluetoothAdapterClient::Properties* properties = | |
174 bluez::BluezDBusManager::Get() | |
175 ->GetBluetoothAdapterClient() | |
176 ->GetProperties(object_path_); | |
177 DCHECK(properties); | |
178 | |
179 return BluetoothDevice::CanonicalizeAddress(properties->address.value()); | |
180 } | |
181 | |
182 std::string BluetoothAdapterChromeOS::GetName() const { | |
183 if (!IsPresent()) | |
184 return std::string(); | |
185 | |
186 bluez::BluetoothAdapterClient::Properties* properties = | |
187 bluez::BluezDBusManager::Get() | |
188 ->GetBluetoothAdapterClient() | |
189 ->GetProperties(object_path_); | |
190 DCHECK(properties); | |
191 | |
192 return properties->alias.value(); | |
193 } | |
194 | |
195 void BluetoothAdapterChromeOS::SetName(const std::string& name, | |
196 const base::Closure& callback, | |
197 const ErrorCallback& error_callback) { | |
198 if (!IsPresent()) { | |
199 error_callback.Run(); | |
200 return; | |
201 } | |
202 | |
203 bluez::BluezDBusManager::Get() | |
204 ->GetBluetoothAdapterClient() | |
205 ->GetProperties(object_path_) | |
206 ->alias.Set( | |
207 name, | |
208 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, | |
209 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
210 } | |
211 | |
212 bool BluetoothAdapterChromeOS::IsInitialized() const { | |
213 return true; | |
214 } | |
215 | |
216 bool BluetoothAdapterChromeOS::IsPresent() const { | |
217 return !dbus_is_shutdown_ && !object_path_.value().empty(); | |
218 } | |
219 | |
220 bool BluetoothAdapterChromeOS::IsPowered() const { | |
221 if (!IsPresent()) | |
222 return false; | |
223 | |
224 bluez::BluetoothAdapterClient::Properties* properties = | |
225 bluez::BluezDBusManager::Get() | |
226 ->GetBluetoothAdapterClient() | |
227 ->GetProperties(object_path_); | |
228 | |
229 return properties->powered.value(); | |
230 } | |
231 | |
232 void BluetoothAdapterChromeOS::SetPowered( | |
233 bool powered, | |
234 const base::Closure& callback, | |
235 const ErrorCallback& error_callback) { | |
236 if (!IsPresent()) { | |
237 error_callback.Run(); | |
238 return; | |
239 } | |
240 | |
241 bluez::BluezDBusManager::Get() | |
242 ->GetBluetoothAdapterClient() | |
243 ->GetProperties(object_path_) | |
244 ->powered.Set( | |
245 powered, | |
246 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, | |
247 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
248 } | |
249 | |
250 bool BluetoothAdapterChromeOS::IsDiscoverable() const { | |
251 if (!IsPresent()) | |
252 return false; | |
253 | |
254 bluez::BluetoothAdapterClient::Properties* properties = | |
255 bluez::BluezDBusManager::Get() | |
256 ->GetBluetoothAdapterClient() | |
257 ->GetProperties(object_path_); | |
258 | |
259 return properties->discoverable.value(); | |
260 } | |
261 | |
262 void BluetoothAdapterChromeOS::SetDiscoverable( | |
263 bool discoverable, | |
264 const base::Closure& callback, | |
265 const ErrorCallback& error_callback) { | |
266 if (!IsPresent()) { | |
267 error_callback.Run(); | |
268 return; | |
269 } | |
270 | |
271 bluez::BluezDBusManager::Get() | |
272 ->GetBluetoothAdapterClient() | |
273 ->GetProperties(object_path_) | |
274 ->discoverable.Set( | |
275 discoverable, | |
276 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoverable, | |
277 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
278 } | |
279 | |
280 bool BluetoothAdapterChromeOS::IsDiscovering() const { | |
281 if (!IsPresent()) | |
282 return false; | |
283 | |
284 bluez::BluetoothAdapterClient::Properties* properties = | |
285 bluez::BluezDBusManager::Get() | |
286 ->GetBluetoothAdapterClient() | |
287 ->GetProperties(object_path_); | |
288 | |
289 return properties->discovering.value(); | |
290 } | |
291 | |
292 void BluetoothAdapterChromeOS::CreateRfcommService( | |
293 const BluetoothUUID& uuid, | |
294 const ServiceOptions& options, | |
295 const CreateServiceCallback& callback, | |
296 const CreateServiceErrorCallback& error_callback) { | |
297 DCHECK(!dbus_is_shutdown_); | |
298 VLOG(1) << object_path_.value() << ": Creating RFCOMM service: " | |
299 << uuid.canonical_value(); | |
300 scoped_refptr<BluetoothSocketChromeOS> socket = | |
301 BluetoothSocketChromeOS::CreateBluetoothSocket( | |
302 ui_task_runner_, socket_thread_); | |
303 socket->Listen(this, | |
304 BluetoothSocketChromeOS::kRfcomm, | |
305 uuid, | |
306 options, | |
307 base::Bind(callback, socket), | |
308 error_callback); | |
309 } | |
310 | |
311 void BluetoothAdapterChromeOS::CreateL2capService( | |
312 const BluetoothUUID& uuid, | |
313 const ServiceOptions& options, | |
314 const CreateServiceCallback& callback, | |
315 const CreateServiceErrorCallback& error_callback) { | |
316 DCHECK(!dbus_is_shutdown_); | |
317 VLOG(1) << object_path_.value() << ": Creating L2CAP service: " | |
318 << uuid.canonical_value(); | |
319 scoped_refptr<BluetoothSocketChromeOS> socket = | |
320 BluetoothSocketChromeOS::CreateBluetoothSocket( | |
321 ui_task_runner_, socket_thread_); | |
322 socket->Listen(this, | |
323 BluetoothSocketChromeOS::kL2cap, | |
324 uuid, | |
325 options, | |
326 base::Bind(callback, socket), | |
327 error_callback); | |
328 } | |
329 | |
330 void BluetoothAdapterChromeOS::RegisterAudioSink( | |
331 const BluetoothAudioSink::Options& options, | |
332 const device::BluetoothAdapter::AcquiredCallback& callback, | |
333 const BluetoothAudioSink::ErrorCallback& error_callback) { | |
334 VLOG(1) << "Registering audio sink"; | |
335 if (!this->IsPresent()) { | |
336 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); | |
337 return; | |
338 } | |
339 scoped_refptr<BluetoothAudioSinkChromeOS> audio_sink( | |
340 new BluetoothAudioSinkChromeOS(this)); | |
341 audio_sink->Register( | |
342 options, base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink, | |
343 weak_ptr_factory_.GetWeakPtr(), callback, | |
344 error_callback, audio_sink), | |
345 error_callback); | |
346 } | |
347 | |
348 void BluetoothAdapterChromeOS::RegisterAdvertisement( | |
349 scoped_ptr<device::BluetoothAdvertisement::Data> advertisement_data, | |
350 const CreateAdvertisementCallback& callback, | |
351 const CreateAdvertisementErrorCallback& error_callback) { | |
352 scoped_refptr<BluetoothAdvertisementChromeOS> advertisement( | |
353 new BluetoothAdvertisementChromeOS(advertisement_data.Pass(), this)); | |
354 advertisement->Register(base::Bind(callback, advertisement), error_callback); | |
355 } | |
356 | |
357 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( | |
358 BluetoothDevice::PairingDelegate* pairing_delegate) { | |
359 // Check if any device is using the pairing delegate. | |
360 // If so, clear the pairing context which will make any responses no-ops. | |
361 for (DevicesMap::const_iterator iter = devices_.begin(); | |
362 iter != devices_.end(); ++iter) { | |
363 BluetoothDeviceChromeOS* device_chromeos = | |
364 static_cast<BluetoothDeviceChromeOS*>(iter->second); | |
365 | |
366 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); | |
367 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) | |
368 device_chromeos->EndPairing(); | |
369 } | |
370 } | |
371 | |
372 void BluetoothAdapterChromeOS::AdapterAdded( | |
373 const dbus::ObjectPath& object_path) { | |
374 // Set the adapter to the newly added adapter only if no adapter is present. | |
375 if (!IsPresent()) | |
376 SetAdapter(object_path); | |
377 } | |
378 | |
379 void BluetoothAdapterChromeOS::AdapterRemoved( | |
380 const dbus::ObjectPath& object_path) { | |
381 if (object_path == object_path_) | |
382 RemoveAdapter(); | |
383 } | |
384 | |
385 void BluetoothAdapterChromeOS::AdapterPropertyChanged( | |
386 const dbus::ObjectPath& object_path, | |
387 const std::string& property_name) { | |
388 if (object_path != object_path_) | |
389 return; | |
390 DCHECK(IsPresent()); | |
391 | |
392 bluez::BluetoothAdapterClient::Properties* properties = | |
393 bluez::BluezDBusManager::Get() | |
394 ->GetBluetoothAdapterClient() | |
395 ->GetProperties(object_path_); | |
396 | |
397 if (property_name == properties->powered.name()) { | |
398 PoweredChanged(properties->powered.value()); | |
399 } else if (property_name == properties->discoverable.name()) { | |
400 DiscoverableChanged(properties->discoverable.value()); | |
401 } else if (property_name == properties->discovering.name()) { | |
402 DiscoveringChanged(properties->discovering.value()); | |
403 } | |
404 } | |
405 | |
406 void BluetoothAdapterChromeOS::DeviceAdded( | |
407 const dbus::ObjectPath& object_path) { | |
408 DCHECK(bluez::BluezDBusManager::Get()); | |
409 bluez::BluetoothDeviceClient::Properties* properties = | |
410 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
411 object_path); | |
412 if (!properties || properties->adapter.value() != object_path_) | |
413 return; | |
414 DCHECK(IsPresent()); | |
415 | |
416 BluetoothDeviceChromeOS* device_chromeos = | |
417 new BluetoothDeviceChromeOS(this, | |
418 object_path, | |
419 ui_task_runner_, | |
420 socket_thread_); | |
421 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); | |
422 | |
423 devices_.set(device_chromeos->GetAddress(), | |
424 scoped_ptr<BluetoothDevice>(device_chromeos)); | |
425 | |
426 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
427 DeviceAdded(this, device_chromeos)); | |
428 } | |
429 | |
430 void BluetoothAdapterChromeOS::DeviceRemoved( | |
431 const dbus::ObjectPath& object_path) { | |
432 for (DevicesMap::const_iterator iter = devices_.begin(); | |
433 iter != devices_.end(); ++iter) { | |
434 BluetoothDeviceChromeOS* device_chromeos = | |
435 static_cast<BluetoothDeviceChromeOS*>(iter->second); | |
436 if (device_chromeos->object_path() == object_path) { | |
437 scoped_ptr<BluetoothDevice> scoped_device = | |
438 devices_.take_and_erase(iter->first); | |
439 | |
440 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
441 DeviceRemoved(this, device_chromeos)); | |
442 return; | |
443 } | |
444 } | |
445 } | |
446 | |
447 void BluetoothAdapterChromeOS::DevicePropertyChanged( | |
448 const dbus::ObjectPath& object_path, | |
449 const std::string& property_name) { | |
450 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | |
451 if (!device_chromeos) | |
452 return; | |
453 | |
454 bluez::BluetoothDeviceClient::Properties* properties = | |
455 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
456 object_path); | |
457 | |
458 if (property_name == properties->address.name()) { | |
459 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); | |
460 ++iter) { | |
461 if (iter->second->GetAddress() == device_chromeos->GetAddress()) { | |
462 std::string old_address = iter->first; | |
463 VLOG(1) << "Device changed address, old: " << old_address | |
464 << " new: " << device_chromeos->GetAddress(); | |
465 scoped_ptr<BluetoothDevice> scoped_device = | |
466 devices_.take_and_erase(iter); | |
467 ignore_result(scoped_device.release()); | |
468 | |
469 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); | |
470 devices_.set(device_chromeos->GetAddress(), | |
471 scoped_ptr<BluetoothDevice>(device_chromeos)); | |
472 NotifyDeviceAddressChanged(device_chromeos, old_address); | |
473 break; | |
474 } | |
475 } | |
476 } | |
477 | |
478 if (property_name == properties->bluetooth_class.name() || | |
479 property_name == properties->address.name() || | |
480 property_name == properties->alias.name() || | |
481 property_name == properties->paired.name() || | |
482 property_name == properties->trusted.name() || | |
483 property_name == properties->connected.name() || | |
484 property_name == properties->uuids.name() || | |
485 property_name == properties->rssi.name() || | |
486 property_name == properties->tx_power.name()) { | |
487 NotifyDeviceChanged(device_chromeos); | |
488 } | |
489 | |
490 if (property_name == properties->gatt_services.name()) { | |
491 NotifyGattServicesDiscovered(device_chromeos); | |
492 } | |
493 | |
494 // When a device becomes paired, mark it as trusted so that the user does | |
495 // not need to approve every incoming connection | |
496 if (property_name == properties->paired.name() && | |
497 properties->paired.value() && !properties->trusted.value()) { | |
498 device_chromeos->SetTrusted(); | |
499 } | |
500 | |
501 // UMA connection counting | |
502 if (property_name == properties->connected.name()) { | |
503 // PlayStation joystick tries to reconnect after disconnection from USB. | |
504 // If it is still not trusted, set it, so it becomes available on the | |
505 // list of known devices. | |
506 if (properties->connected.value() && device_chromeos->IsTrustable() && | |
507 !properties->trusted.value()) | |
508 device_chromeos->SetTrusted(); | |
509 | |
510 int count = 0; | |
511 | |
512 for (DevicesMap::const_iterator iter = devices_.begin(); | |
513 iter != devices_.end(); ++iter) { | |
514 if (iter->second->IsPaired() && iter->second->IsConnected()) | |
515 ++count; | |
516 } | |
517 | |
518 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); | |
519 } | |
520 } | |
521 | |
522 void BluetoothAdapterChromeOS::InputPropertyChanged( | |
523 const dbus::ObjectPath& object_path, | |
524 const std::string& property_name) { | |
525 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | |
526 if (!device_chromeos) | |
527 return; | |
528 | |
529 bluez::BluetoothInputClient::Properties* properties = | |
530 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->GetProperties( | |
531 object_path); | |
532 | |
533 // Properties structure can be removed, which triggers a change in the | |
534 // BluetoothDevice::IsConnectable() property, as does a change in the | |
535 // actual reconnect_mode property. | |
536 if (!properties || property_name == properties->reconnect_mode.name()) { | |
537 NotifyDeviceChanged(device_chromeos); | |
538 } | |
539 } | |
540 | |
541 void BluetoothAdapterChromeOS::Released() { | |
542 VLOG(1) << "Release"; | |
543 if (!IsPresent()) | |
544 return; | |
545 DCHECK(agent_.get()); | |
546 | |
547 // Called after we unregister the pairing agent, e.g. when changing I/O | |
548 // capabilities. Nothing much to be done right now. | |
549 } | |
550 | |
551 void BluetoothAdapterChromeOS::RequestPinCode( | |
552 const dbus::ObjectPath& device_path, | |
553 const PinCodeCallback& callback) { | |
554 DCHECK(IsPresent()); | |
555 DCHECK(agent_.get()); | |
556 VLOG(1) << device_path.value() << ": RequestPinCode"; | |
557 | |
558 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
559 if (!pairing) { | |
560 callback.Run(REJECTED, ""); | |
561 return; | |
562 } | |
563 | |
564 pairing->RequestPinCode(callback); | |
565 } | |
566 | |
567 void BluetoothAdapterChromeOS::DisplayPinCode( | |
568 const dbus::ObjectPath& device_path, | |
569 const std::string& pincode) { | |
570 DCHECK(IsPresent()); | |
571 DCHECK(agent_.get()); | |
572 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; | |
573 | |
574 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
575 if (!pairing) | |
576 return; | |
577 | |
578 pairing->DisplayPinCode(pincode); | |
579 } | |
580 | |
581 void BluetoothAdapterChromeOS::RequestPasskey( | |
582 const dbus::ObjectPath& device_path, | |
583 const PasskeyCallback& callback) { | |
584 DCHECK(IsPresent()); | |
585 DCHECK(agent_.get()); | |
586 VLOG(1) << device_path.value() << ": RequestPasskey"; | |
587 | |
588 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
589 if (!pairing) { | |
590 callback.Run(REJECTED, 0); | |
591 return; | |
592 } | |
593 | |
594 pairing->RequestPasskey(callback); | |
595 } | |
596 | |
597 void BluetoothAdapterChromeOS::DisplayPasskey( | |
598 const dbus::ObjectPath& device_path, | |
599 uint32 passkey, | |
600 uint16 entered) { | |
601 DCHECK(IsPresent()); | |
602 DCHECK(agent_.get()); | |
603 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey | |
604 << " (" << entered << " entered)"; | |
605 | |
606 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
607 if (!pairing) | |
608 return; | |
609 | |
610 if (entered == 0) | |
611 pairing->DisplayPasskey(passkey); | |
612 | |
613 pairing->KeysEntered(entered); | |
614 } | |
615 | |
616 void BluetoothAdapterChromeOS::RequestConfirmation( | |
617 const dbus::ObjectPath& device_path, | |
618 uint32 passkey, | |
619 const ConfirmationCallback& callback) { | |
620 DCHECK(IsPresent()); | |
621 DCHECK(agent_.get()); | |
622 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey; | |
623 | |
624 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
625 if (!pairing) { | |
626 callback.Run(REJECTED); | |
627 return; | |
628 } | |
629 | |
630 pairing->RequestConfirmation(passkey, callback); | |
631 } | |
632 | |
633 void BluetoothAdapterChromeOS::RequestAuthorization( | |
634 const dbus::ObjectPath& device_path, | |
635 const ConfirmationCallback& callback) { | |
636 DCHECK(IsPresent()); | |
637 DCHECK(agent_.get()); | |
638 VLOG(1) << device_path.value() << ": RequestAuthorization"; | |
639 | |
640 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
641 if (!pairing) { | |
642 callback.Run(REJECTED); | |
643 return; | |
644 } | |
645 | |
646 pairing->RequestAuthorization(callback); | |
647 } | |
648 | |
649 void BluetoothAdapterChromeOS::AuthorizeService( | |
650 const dbus::ObjectPath& device_path, | |
651 const std::string& uuid, | |
652 const ConfirmationCallback& callback) { | |
653 DCHECK(IsPresent()); | |
654 DCHECK(agent_.get()); | |
655 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; | |
656 | |
657 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(device_path); | |
658 if (!device_chromeos) { | |
659 callback.Run(CANCELLED); | |
660 return; | |
661 } | |
662 | |
663 // We always set paired devices to Trusted, so the only reason that this | |
664 // method call would ever be called is in the case of a race condition where | |
665 // our "Set('Trusted', true)" method call is still pending in the Bluetooth | |
666 // daemon because it's busy handling the incoming connection. | |
667 if (device_chromeos->IsPaired()) { | |
668 callback.Run(SUCCESS); | |
669 return; | |
670 } | |
671 | |
672 // TODO(keybuk): reject service authorizations when not paired, determine | |
673 // whether this is acceptable long-term. | |
674 LOG(WARNING) << "Rejecting service connection from unpaired device " | |
675 << device_chromeos->GetAddress() << " for UUID " << uuid; | |
676 callback.Run(REJECTED); | |
677 } | |
678 | |
679 void BluetoothAdapterChromeOS::Cancel() { | |
680 DCHECK(IsPresent()); | |
681 DCHECK(agent_.get()); | |
682 VLOG(1) << "Cancel"; | |
683 } | |
684 | |
685 void BluetoothAdapterChromeOS::OnRegisterAgent() { | |
686 VLOG(1) << "Pairing agent registered, requesting to be made default"; | |
687 | |
688 bluez::BluezDBusManager::Get() | |
689 ->GetBluetoothAgentManagerClient() | |
690 ->RequestDefaultAgent( | |
691 dbus::ObjectPath(kAgentPath), | |
692 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, | |
693 weak_ptr_factory_.GetWeakPtr()), | |
694 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, | |
695 weak_ptr_factory_.GetWeakPtr())); | |
696 } | |
697 | |
698 void BluetoothAdapterChromeOS::OnRegisterAgentError( | |
699 const std::string& error_name, | |
700 const std::string& error_message) { | |
701 // Our agent being already registered isn't an error. | |
702 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) | |
703 return; | |
704 | |
705 LOG(WARNING) << ": Failed to register pairing agent: " | |
706 << error_name << ": " << error_message; | |
707 } | |
708 | |
709 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { | |
710 VLOG(1) << "Pairing agent now default"; | |
711 } | |
712 | |
713 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( | |
714 const std::string& error_name, | |
715 const std::string& error_message) { | |
716 LOG(WARNING) << ": Failed to make pairing agent default: " | |
717 << error_name << ": " << error_message; | |
718 } | |
719 | |
720 void BluetoothAdapterChromeOS::OnRegisterAudioSink( | |
721 const device::BluetoothAdapter::AcquiredCallback& callback, | |
722 const device::BluetoothAudioSink::ErrorCallback& error_callback, | |
723 scoped_refptr<BluetoothAudioSink> audio_sink) { | |
724 if (!IsPresent()) { | |
725 VLOG(1) << "Failed to register audio sink, adapter not present"; | |
726 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); | |
727 return; | |
728 } | |
729 DCHECK(audio_sink.get()); | |
730 callback.Run(audio_sink); | |
731 } | |
732 | |
733 BluetoothDeviceChromeOS* | |
734 BluetoothAdapterChromeOS::GetDeviceWithPath( | |
735 const dbus::ObjectPath& object_path) { | |
736 if (!IsPresent()) | |
737 return nullptr; | |
738 | |
739 for (DevicesMap::const_iterator iter = devices_.begin(); | |
740 iter != devices_.end(); ++iter) { | |
741 BluetoothDeviceChromeOS* device_chromeos = | |
742 static_cast<BluetoothDeviceChromeOS*>(iter->second); | |
743 if (device_chromeos->object_path() == object_path) | |
744 return device_chromeos; | |
745 } | |
746 | |
747 return nullptr; | |
748 } | |
749 | |
750 BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( | |
751 const dbus::ObjectPath& object_path) { | |
752 DCHECK(IsPresent()); | |
753 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | |
754 if (!device_chromeos) { | |
755 LOG(WARNING) << "Pairing Agent request for unknown device: " | |
756 << object_path.value(); | |
757 return nullptr; | |
758 } | |
759 | |
760 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); | |
761 if (pairing) | |
762 return pairing; | |
763 | |
764 // The device doesn't have its own pairing context, so this is an incoming | |
765 // pairing request that should use our best default delegate (if we have one). | |
766 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate(); | |
767 if (!pairing_delegate) | |
768 return nullptr; | |
769 | |
770 return device_chromeos->BeginPairing(pairing_delegate); | |
771 } | |
772 | |
773 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { | |
774 DCHECK(!IsPresent()); | |
775 DCHECK(!dbus_is_shutdown_); | |
776 object_path_ = object_path; | |
777 | |
778 VLOG(1) << object_path_.value() << ": using adapter."; | |
779 | |
780 VLOG(1) << "Registering pairing agent"; | |
781 bluez::BluezDBusManager::Get() | |
782 ->GetBluetoothAgentManagerClient() | |
783 ->RegisterAgent( | |
784 dbus::ObjectPath(kAgentPath), | |
785 bluetooth_agent_manager::kKeyboardDisplayCapability, | |
786 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent, | |
787 weak_ptr_factory_.GetWeakPtr()), | |
788 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgentError, | |
789 weak_ptr_factory_.GetWeakPtr())); | |
790 | |
791 SetDefaultAdapterName(); | |
792 | |
793 bluez::BluetoothAdapterClient::Properties* properties = | |
794 bluez::BluezDBusManager::Get() | |
795 ->GetBluetoothAdapterClient() | |
796 ->GetProperties(object_path_); | |
797 | |
798 PresentChanged(true); | |
799 | |
800 if (properties->powered.value()) | |
801 PoweredChanged(true); | |
802 if (properties->discoverable.value()) | |
803 DiscoverableChanged(true); | |
804 if (properties->discovering.value()) | |
805 DiscoveringChanged(true); | |
806 | |
807 std::vector<dbus::ObjectPath> device_paths = | |
808 bluez::BluezDBusManager::Get() | |
809 ->GetBluetoothDeviceClient() | |
810 ->GetDevicesForAdapter(object_path_); | |
811 | |
812 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin(); | |
813 iter != device_paths.end(); ++iter) { | |
814 DeviceAdded(*iter); | |
815 } | |
816 } | |
817 | |
818 void BluetoothAdapterChromeOS::SetDefaultAdapterName() { | |
819 DCHECK(IsPresent()); | |
820 | |
821 std::string alias; | |
822 switch (chromeos::GetDeviceType()) { | |
823 case DeviceType::kChromebase: | |
824 alias = "Chromebase"; | |
825 break; | |
826 case DeviceType::kChromebit: | |
827 alias = "Chromebit"; | |
828 break; | |
829 case DeviceType::kChromebook: | |
830 alias = "Chromebook"; | |
831 break; | |
832 case DeviceType::kChromebox: | |
833 alias = "Chromebox"; | |
834 break; | |
835 case DeviceType::kUnknown: | |
836 alias = "Chromebook"; | |
837 break; | |
838 } | |
839 | |
840 SetName(alias, base::Bind(&base::DoNothing), base::Bind(&base::DoNothing)); | |
841 } | |
842 | |
843 void BluetoothAdapterChromeOS::RemoveAdapter() { | |
844 DCHECK(IsPresent()); | |
845 VLOG(1) << object_path_.value() << ": adapter removed."; | |
846 | |
847 bluez::BluetoothAdapterClient::Properties* properties = | |
848 bluez::BluezDBusManager::Get() | |
849 ->GetBluetoothAdapterClient() | |
850 ->GetProperties(object_path_); | |
851 | |
852 object_path_ = dbus::ObjectPath(""); | |
853 | |
854 if (properties->powered.value()) | |
855 PoweredChanged(false); | |
856 if (properties->discoverable.value()) | |
857 DiscoverableChanged(false); | |
858 if (properties->discovering.value()) | |
859 DiscoveringChanged(false); | |
860 | |
861 // Move all elements of the original devices list to a new list here, | |
862 // leaving the original list empty so that when we send DeviceRemoved(), | |
863 // GetDevices() returns no devices. | |
864 DevicesMap devices_swapped; | |
865 devices_swapped.swap(devices_); | |
866 | |
867 for (auto& iter : devices_swapped) { | |
868 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
869 DeviceRemoved(this, iter.second)); | |
870 } | |
871 | |
872 PresentChanged(false); | |
873 } | |
874 | |
875 void BluetoothAdapterChromeOS::PoweredChanged(bool powered) { | |
876 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
877 AdapterPoweredChanged(this, powered)); | |
878 } | |
879 | |
880 void BluetoothAdapterChromeOS::DiscoverableChanged(bool discoverable) { | |
881 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
882 AdapterDiscoverableChanged(this, discoverable)); | |
883 } | |
884 | |
885 void BluetoothAdapterChromeOS::DiscoveringChanged( | |
886 bool discovering) { | |
887 // If the adapter stopped discovery due to a reason other than a request by | |
888 // us, reset the count to 0. | |
889 VLOG(1) << "Discovering changed: " << discovering; | |
890 if (!discovering && !discovery_request_pending_ && | |
891 num_discovery_sessions_ > 0) { | |
892 VLOG(1) << "Marking sessions as inactive."; | |
893 num_discovery_sessions_ = 0; | |
894 MarkDiscoverySessionsAsInactive(); | |
895 } | |
896 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
897 AdapterDiscoveringChanged(this, discovering)); | |
898 } | |
899 | |
900 void BluetoothAdapterChromeOS::PresentChanged(bool present) { | |
901 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
902 AdapterPresentChanged(this, present)); | |
903 } | |
904 | |
905 void BluetoothAdapterChromeOS::NotifyDeviceChanged( | |
906 BluetoothDeviceChromeOS* device) { | |
907 DCHECK(device); | |
908 DCHECK(device->adapter_ == this); | |
909 | |
910 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
911 DeviceChanged(this, device)); | |
912 } | |
913 | |
914 void BluetoothAdapterChromeOS::NotifyDeviceAddressChanged( | |
915 BluetoothDeviceChromeOS* device, | |
916 const std::string& old_address) { | |
917 DCHECK(device->adapter_ == this); | |
918 | |
919 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
920 DeviceAddressChanged(this, device, old_address)); | |
921 } | |
922 | |
923 void BluetoothAdapterChromeOS::NotifyGattServiceAdded( | |
924 BluetoothRemoteGattServiceChromeOS* service) { | |
925 DCHECK_EQ(service->GetAdapter(), this); | |
926 DCHECK_EQ( | |
927 static_cast<BluetoothDeviceChromeOS*>(service->GetDevice())->adapter_, | |
928 this); | |
929 | |
930 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
931 observers_, | |
932 GattServiceAdded(this, service->GetDevice(), service)); | |
933 } | |
934 | |
935 void BluetoothAdapterChromeOS::NotifyGattServiceRemoved( | |
936 BluetoothRemoteGattServiceChromeOS* service) { | |
937 DCHECK_EQ(service->GetAdapter(), this); | |
938 DCHECK_EQ( | |
939 static_cast<BluetoothDeviceChromeOS*>(service->GetDevice())->adapter_, | |
940 this); | |
941 | |
942 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
943 observers_, | |
944 GattServiceRemoved(this, service->GetDevice(), service)); | |
945 } | |
946 | |
947 void BluetoothAdapterChromeOS::NotifyGattServiceChanged( | |
948 BluetoothRemoteGattServiceChromeOS* service) { | |
949 DCHECK_EQ(service->GetAdapter(), this); | |
950 | |
951 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
952 observers_, | |
953 GattServiceChanged(this, service)); | |
954 } | |
955 | |
956 void BluetoothAdapterChromeOS::NotifyGattServicesDiscovered( | |
957 BluetoothDeviceChromeOS* device) { | |
958 DCHECK(device->adapter_ == this); | |
959 | |
960 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
961 GattServicesDiscovered(this, device)); | |
962 } | |
963 | |
964 void BluetoothAdapterChromeOS::NotifyGattDiscoveryComplete( | |
965 BluetoothRemoteGattServiceChromeOS* service) { | |
966 DCHECK_EQ(service->GetAdapter(), this); | |
967 | |
968 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
969 observers_, | |
970 GattDiscoveryCompleteForService(this, service)); | |
971 } | |
972 | |
973 void BluetoothAdapterChromeOS::NotifyGattCharacteristicAdded( | |
974 BluetoothRemoteGattCharacteristicChromeOS* characteristic) { | |
975 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
976 characteristic->GetService())->GetAdapter(), | |
977 this); | |
978 | |
979 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
980 observers_, | |
981 GattCharacteristicAdded(this, characteristic)); | |
982 } | |
983 | |
984 void BluetoothAdapterChromeOS::NotifyGattCharacteristicRemoved( | |
985 BluetoothRemoteGattCharacteristicChromeOS* characteristic) { | |
986 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
987 characteristic->GetService())->GetAdapter(), | |
988 this); | |
989 | |
990 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
991 observers_, | |
992 GattCharacteristicRemoved(this, characteristic)); | |
993 } | |
994 | |
995 void BluetoothAdapterChromeOS::NotifyGattDescriptorAdded( | |
996 BluetoothRemoteGattDescriptorChromeOS* descriptor) { | |
997 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
998 descriptor->GetCharacteristic()->GetService())->GetAdapter(), | |
999 this); | |
1000 | |
1001 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
1002 observers_, | |
1003 GattDescriptorAdded(this, descriptor)); | |
1004 } | |
1005 | |
1006 void BluetoothAdapterChromeOS::NotifyGattDescriptorRemoved( | |
1007 BluetoothRemoteGattDescriptorChromeOS* descriptor) { | |
1008 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
1009 descriptor->GetCharacteristic()->GetService())->GetAdapter(), | |
1010 this); | |
1011 | |
1012 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
1013 observers_, | |
1014 GattDescriptorRemoved(this, descriptor)); | |
1015 } | |
1016 | |
1017 void BluetoothAdapterChromeOS::NotifyGattCharacteristicValueChanged( | |
1018 BluetoothRemoteGattCharacteristicChromeOS* characteristic, | |
1019 const std::vector<uint8>& value) { | |
1020 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
1021 characteristic->GetService())->GetAdapter(), | |
1022 this); | |
1023 | |
1024 FOR_EACH_OBSERVER( | |
1025 BluetoothAdapter::Observer, | |
1026 observers_, | |
1027 GattCharacteristicValueChanged(this, characteristic, value)); | |
1028 } | |
1029 | |
1030 void BluetoothAdapterChromeOS::NotifyGattDescriptorValueChanged( | |
1031 BluetoothRemoteGattDescriptorChromeOS* descriptor, | |
1032 const std::vector<uint8>& value) { | |
1033 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
1034 descriptor->GetCharacteristic()->GetService())->GetAdapter(), | |
1035 this); | |
1036 | |
1037 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
1038 observers_, | |
1039 GattDescriptorValueChanged(this, descriptor, value)); | |
1040 } | |
1041 | |
1042 void BluetoothAdapterChromeOS::UseProfile( | |
1043 const BluetoothUUID& uuid, | |
1044 const dbus::ObjectPath& device_path, | |
1045 const bluez::BluetoothProfileManagerClient::Options& options, | |
1046 bluez::BluetoothProfileServiceProvider::Delegate* delegate, | |
1047 const ProfileRegisteredCallback& success_callback, | |
1048 const ErrorCompletionCallback& error_callback) { | |
1049 DCHECK(delegate); | |
1050 | |
1051 if (!IsPresent()) { | |
1052 VLOG(2) << "Adapter not present, erroring out"; | |
1053 error_callback.Run("Adapter not present"); | |
1054 return; | |
1055 } | |
1056 | |
1057 if (profiles_.find(uuid) != profiles_.end()) { | |
1058 // TODO(jamuraa) check that the options are the same and error when they are | |
1059 // not. | |
1060 SetProfileDelegate(uuid, device_path, delegate, success_callback, | |
1061 error_callback); | |
1062 return; | |
1063 } | |
1064 | |
1065 if (profile_queues_.find(uuid) == profile_queues_.end()) { | |
1066 BluetoothAdapterProfileChromeOS::Register( | |
1067 uuid, options, | |
1068 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfile, this, uuid), | |
1069 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfileError, this, | |
1070 uuid)); | |
1071 | |
1072 profile_queues_[uuid] = new std::vector<RegisterProfileCompletionPair>(); | |
1073 } | |
1074 | |
1075 profile_queues_[uuid]->push_back(std::make_pair( | |
1076 base::Bind(&BluetoothAdapterChromeOS::SetProfileDelegate, this, uuid, | |
1077 device_path, delegate, success_callback, error_callback), | |
1078 error_callback)); | |
1079 } | |
1080 | |
1081 void BluetoothAdapterChromeOS::ReleaseProfile( | |
1082 const dbus::ObjectPath& device_path, | |
1083 BluetoothAdapterProfileChromeOS* profile) { | |
1084 VLOG(2) << "Releasing Profile: " << profile->uuid().canonical_value() | |
1085 << " from " << device_path.value(); | |
1086 profile->RemoveDelegate( | |
1087 device_path, base::Bind(&BluetoothAdapterChromeOS::RemoveProfile, | |
1088 weak_ptr_factory_.GetWeakPtr(), profile->uuid())); | |
1089 } | |
1090 | |
1091 void BluetoothAdapterChromeOS::RemoveProfile(const BluetoothUUID& uuid) { | |
1092 VLOG(2) << "Remove Profile: " << uuid.canonical_value(); | |
1093 | |
1094 if (profiles_.find(uuid) != profiles_.end()) { | |
1095 delete profiles_[uuid]; | |
1096 profiles_.erase(uuid); | |
1097 } | |
1098 } | |
1099 | |
1100 void BluetoothAdapterChromeOS::OnRegisterProfile( | |
1101 const BluetoothUUID& uuid, | |
1102 scoped_ptr<BluetoothAdapterProfileChromeOS> profile) { | |
1103 profiles_[uuid] = profile.release(); | |
1104 | |
1105 if (profile_queues_.find(uuid) == profile_queues_.end()) | |
1106 return; | |
1107 | |
1108 for (auto& it : *profile_queues_[uuid]) | |
1109 it.first.Run(); | |
1110 delete profile_queues_[uuid]; | |
1111 profile_queues_.erase(uuid); | |
1112 } | |
1113 | |
1114 void BluetoothAdapterChromeOS::SetProfileDelegate( | |
1115 const BluetoothUUID& uuid, | |
1116 const dbus::ObjectPath& device_path, | |
1117 bluez::BluetoothProfileServiceProvider::Delegate* delegate, | |
1118 const ProfileRegisteredCallback& success_callback, | |
1119 const ErrorCompletionCallback& error_callback) { | |
1120 if (profiles_.find(uuid) == profiles_.end()) { | |
1121 error_callback.Run("Cannot find profile!"); | |
1122 return; | |
1123 } | |
1124 | |
1125 if (profiles_[uuid]->SetDelegate(device_path, delegate)) { | |
1126 success_callback.Run(profiles_[uuid]); | |
1127 return; | |
1128 } | |
1129 // Already set | |
1130 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists); | |
1131 } | |
1132 | |
1133 void BluetoothAdapterChromeOS::OnRegisterProfileError( | |
1134 const BluetoothUUID& uuid, | |
1135 const std::string& error_name, | |
1136 const std::string& error_message) { | |
1137 VLOG(2) << object_path_.value() << ": Failed to register profile: " | |
1138 << error_name << ": " << error_message; | |
1139 if (profile_queues_.find(uuid) == profile_queues_.end()) | |
1140 return; | |
1141 | |
1142 for (auto& it : *profile_queues_[uuid]) | |
1143 it.second.Run(error_message); | |
1144 | |
1145 delete profile_queues_[uuid]; | |
1146 profile_queues_.erase(uuid); | |
1147 } | |
1148 | |
1149 void BluetoothAdapterChromeOS::OnSetDiscoverable( | |
1150 const base::Closure& callback, | |
1151 const ErrorCallback& error_callback, | |
1152 bool success) { | |
1153 if (!IsPresent()) { | |
1154 error_callback.Run(); | |
1155 return; | |
1156 } | |
1157 | |
1158 // Set the discoverable_timeout property to zero so the adapter remains | |
1159 // discoverable forever. | |
1160 bluez::BluezDBusManager::Get() | |
1161 ->GetBluetoothAdapterClient() | |
1162 ->GetProperties(object_path_) | |
1163 ->discoverable_timeout.Set( | |
1164 0, | |
1165 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, | |
1166 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1167 } | |
1168 | |
1169 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( | |
1170 const base::Closure& callback, | |
1171 const ErrorCallback& error_callback, | |
1172 bool success) { | |
1173 if (IsPresent() && success) { | |
1174 callback.Run(); | |
1175 } else { | |
1176 error_callback.Run(); | |
1177 } | |
1178 } | |
1179 | |
1180 void BluetoothAdapterChromeOS::AddDiscoverySession( | |
1181 BluetoothDiscoveryFilter* discovery_filter, | |
1182 const base::Closure& callback, | |
1183 const DiscoverySessionErrorCallback& error_callback) { | |
1184 if (!IsPresent()) { | |
1185 error_callback.Run( | |
1186 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT); | |
1187 return; | |
1188 } | |
1189 VLOG(1) << __func__; | |
1190 if (discovery_request_pending_) { | |
1191 // The pending request is either to stop a previous session or to start a | |
1192 // new one. Either way, queue this one. | |
1193 DCHECK(num_discovery_sessions_ == 1 || num_discovery_sessions_ == 0); | |
1194 VLOG(1) << "Pending request to start/stop device discovery. Queueing " | |
1195 << "request to start a new discovery session."; | |
1196 discovery_request_queue_.push( | |
1197 std::make_tuple(discovery_filter, callback, error_callback)); | |
1198 return; | |
1199 } | |
1200 | |
1201 // The adapter is already discovering. | |
1202 if (num_discovery_sessions_ > 0) { | |
1203 DCHECK(IsDiscovering()); | |
1204 DCHECK(!discovery_request_pending_); | |
1205 num_discovery_sessions_++; | |
1206 SetDiscoveryFilter(BluetoothDiscoveryFilter::Merge( | |
1207 GetMergedDiscoveryFilter().get(), discovery_filter), | |
1208 callback, error_callback); | |
1209 return; | |
1210 } | |
1211 | |
1212 // There are no active discovery sessions. | |
1213 DCHECK_EQ(num_discovery_sessions_, 0); | |
1214 | |
1215 if (discovery_filter) { | |
1216 discovery_request_pending_ = true; | |
1217 | |
1218 scoped_ptr<BluetoothDiscoveryFilter> df(new BluetoothDiscoveryFilter( | |
1219 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL)); | |
1220 df->CopyFrom(*discovery_filter); | |
1221 SetDiscoveryFilter( | |
1222 df.Pass(), | |
1223 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter, | |
1224 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
1225 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError, | |
1226 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1227 return; | |
1228 } else { | |
1229 current_filter_.reset(); | |
1230 } | |
1231 | |
1232 // This is the first request to start device discovery. | |
1233 discovery_request_pending_ = true; | |
1234 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery( | |
1235 object_path_, | |
1236 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, | |
1237 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
1238 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | |
1239 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1240 } | |
1241 | |
1242 void BluetoothAdapterChromeOS::RemoveDiscoverySession( | |
1243 BluetoothDiscoveryFilter* discovery_filter, | |
1244 const base::Closure& callback, | |
1245 const DiscoverySessionErrorCallback& error_callback) { | |
1246 if (!IsPresent()) { | |
1247 error_callback.Run( | |
1248 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT); | |
1249 return; | |
1250 } | |
1251 | |
1252 VLOG(1) << __func__; | |
1253 // There are active sessions other than the one currently being removed. | |
1254 if (num_discovery_sessions_ > 1) { | |
1255 DCHECK(IsDiscovering()); | |
1256 DCHECK(!discovery_request_pending_); | |
1257 num_discovery_sessions_--; | |
1258 | |
1259 SetDiscoveryFilter(GetMergedDiscoveryFilterMasked(discovery_filter), | |
1260 callback, error_callback); | |
1261 return; | |
1262 } | |
1263 | |
1264 // If there is a pending request to BlueZ, then queue this request. | |
1265 if (discovery_request_pending_) { | |
1266 VLOG(1) << "Pending request to start/stop device discovery. Queueing " | |
1267 << "request to stop discovery session."; | |
1268 error_callback.Run( | |
1269 UMABluetoothDiscoverySessionOutcome::REMOVE_WITH_PENDING_REQUEST); | |
1270 return; | |
1271 } | |
1272 | |
1273 // There are no active sessions. Return error. | |
1274 if (num_discovery_sessions_ == 0) { | |
1275 // TODO(armansito): This should never happen once we have the | |
1276 // DiscoverySession API. Replace this case with an assert once it's | |
1277 // the deprecated methods have been removed. (See crbug.com/3445008). | |
1278 VLOG(1) << "No active discovery sessions. Returning error."; | |
1279 error_callback.Run( | |
1280 UMABluetoothDiscoverySessionOutcome::ACTIVE_SESSION_NOT_IN_ADAPTER); | |
1281 return; | |
1282 } | |
1283 | |
1284 // There is exactly one active discovery session. Request BlueZ to stop | |
1285 // discovery. | |
1286 DCHECK_EQ(num_discovery_sessions_, 1); | |
1287 discovery_request_pending_ = true; | |
1288 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StopDiscovery( | |
1289 object_path_, base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, | |
1290 weak_ptr_factory_.GetWeakPtr(), callback), | |
1291 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, | |
1292 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
1293 } | |
1294 | |
1295 void BluetoothAdapterChromeOS::SetDiscoveryFilter( | |
1296 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, | |
1297 const base::Closure& callback, | |
1298 const DiscoverySessionErrorCallback& error_callback) { | |
1299 if (!IsPresent()) { | |
1300 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); | |
1301 return; | |
1302 } | |
1303 | |
1304 // If old and new filter are equal (null) then don't make request, just call | |
1305 // succes callback | |
1306 if (!current_filter_ && !discovery_filter.get()) { | |
1307 callback.Run(); | |
1308 return; | |
1309 } | |
1310 | |
1311 // If old and new filter are not null and equal then don't make request, just | |
1312 // call succes callback | |
1313 if (current_filter_ && discovery_filter && | |
1314 current_filter_->Equals(*discovery_filter)) { | |
1315 callback.Run(); | |
1316 return; | |
1317 } | |
1318 | |
1319 current_filter_.reset(discovery_filter.release()); | |
1320 | |
1321 bluez::BluetoothAdapterClient::DiscoveryFilter dbus_discovery_filter; | |
1322 | |
1323 if (current_filter_.get()) { | |
1324 uint16_t pathloss; | |
1325 int16_t rssi; | |
1326 uint8_t transport; | |
1327 std::set<device::BluetoothUUID> uuids; | |
1328 | |
1329 if (current_filter_->GetPathloss(&pathloss)) | |
1330 dbus_discovery_filter.pathloss.reset(new uint16_t(pathloss)); | |
1331 | |
1332 if (current_filter_->GetRSSI(&rssi)) | |
1333 dbus_discovery_filter.rssi.reset(new int16_t(rssi)); | |
1334 | |
1335 transport = current_filter_->GetTransport(); | |
1336 if (transport == BluetoothDiscoveryFilter::Transport::TRANSPORT_LE) { | |
1337 dbus_discovery_filter.transport.reset(new std::string("le")); | |
1338 } else if (transport == | |
1339 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC) { | |
1340 dbus_discovery_filter.transport.reset(new std::string("bredr")); | |
1341 } else if (transport == | |
1342 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL) { | |
1343 dbus_discovery_filter.transport.reset(new std::string("auto")); | |
1344 } | |
1345 | |
1346 current_filter_->GetUUIDs(uuids); | |
1347 if (uuids.size()) { | |
1348 dbus_discovery_filter.uuids = | |
1349 scoped_ptr<std::vector<std::string>>(new std::vector<std::string>); | |
1350 | |
1351 for (const auto& it : uuids) | |
1352 dbus_discovery_filter.uuids.get()->push_back(it.value()); | |
1353 } | |
1354 } | |
1355 | |
1356 bluez::BluezDBusManager::Get() | |
1357 ->GetBluetoothAdapterClient() | |
1358 ->SetDiscoveryFilter( | |
1359 object_path_, dbus_discovery_filter, | |
1360 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilter, | |
1361 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
1362 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilterError, | |
1363 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1364 } | |
1365 | |
1366 void BluetoothAdapterChromeOS::OnStartDiscovery( | |
1367 const base::Closure& callback, | |
1368 const DiscoverySessionErrorCallback& error_callback) { | |
1369 // Report success on the original request and increment the count. | |
1370 VLOG(1) << __func__; | |
1371 DCHECK(discovery_request_pending_); | |
1372 DCHECK_EQ(num_discovery_sessions_, 0); | |
1373 discovery_request_pending_ = false; | |
1374 num_discovery_sessions_++; | |
1375 if (IsPresent()) { | |
1376 callback.Run(); | |
1377 } else { | |
1378 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); | |
1379 } | |
1380 | |
1381 // Try to add a new discovery session for each queued request. | |
1382 ProcessQueuedDiscoveryRequests(); | |
1383 } | |
1384 | |
1385 void BluetoothAdapterChromeOS::OnStartDiscoveryError( | |
1386 const base::Closure& callback, | |
1387 const DiscoverySessionErrorCallback& error_callback, | |
1388 const std::string& error_name, | |
1389 const std::string& error_message) { | |
1390 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " | |
1391 << error_name << ": " << error_message; | |
1392 | |
1393 // Failed to start discovery. This can only happen if the count is at 0. | |
1394 DCHECK_EQ(num_discovery_sessions_, 0); | |
1395 DCHECK(discovery_request_pending_); | |
1396 discovery_request_pending_ = false; | |
1397 | |
1398 // Discovery request may fail if discovery was previously initiated by Chrome, | |
1399 // but the session were invalidated due to the discovery state unexpectedly | |
1400 // changing to false and then back to true. In this case, report success. | |
1401 if (IsPresent() && error_name == bluetooth_device::kErrorInProgress && | |
1402 IsDiscovering()) { | |
1403 VLOG(1) << "Discovery previously initiated. Reporting success."; | |
1404 num_discovery_sessions_++; | |
1405 callback.Run(); | |
1406 } else { | |
1407 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name)); | |
1408 } | |
1409 | |
1410 // Try to add a new discovery session for each queued request. | |
1411 ProcessQueuedDiscoveryRequests(); | |
1412 } | |
1413 | |
1414 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { | |
1415 // Report success on the original request and decrement the count. | |
1416 VLOG(1) << __func__; | |
1417 DCHECK(discovery_request_pending_); | |
1418 DCHECK_EQ(num_discovery_sessions_, 1); | |
1419 discovery_request_pending_ = false; | |
1420 num_discovery_sessions_--; | |
1421 callback.Run(); | |
1422 | |
1423 current_filter_.reset(); | |
1424 | |
1425 // Try to add a new discovery session for each queued request. | |
1426 ProcessQueuedDiscoveryRequests(); | |
1427 } | |
1428 | |
1429 void BluetoothAdapterChromeOS::OnStopDiscoveryError( | |
1430 const DiscoverySessionErrorCallback& error_callback, | |
1431 const std::string& error_name, | |
1432 const std::string& error_message) { | |
1433 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " | |
1434 << error_name << ": " << error_message; | |
1435 | |
1436 // Failed to stop discovery. This can only happen if the count is at 1. | |
1437 DCHECK(discovery_request_pending_); | |
1438 DCHECK_EQ(num_discovery_sessions_, 1); | |
1439 discovery_request_pending_ = false; | |
1440 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name)); | |
1441 | |
1442 // Try to add a new discovery session for each queued request. | |
1443 ProcessQueuedDiscoveryRequests(); | |
1444 } | |
1445 | |
1446 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter( | |
1447 const base::Closure& callback, | |
1448 const DiscoverySessionErrorCallback& error_callback) { | |
1449 // This is the first request to start device discovery. | |
1450 DCHECK(discovery_request_pending_); | |
1451 DCHECK_EQ(num_discovery_sessions_, 0); | |
1452 | |
1453 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery( | |
1454 object_path_, | |
1455 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, | |
1456 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
1457 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | |
1458 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1459 } | |
1460 | |
1461 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError( | |
1462 const base::Closure& callback, | |
1463 const DiscoverySessionErrorCallback& error_callback, | |
1464 UMABluetoothDiscoverySessionOutcome outcome) { | |
1465 LOG(WARNING) << object_path_.value() | |
1466 << ": Failed to pre set discovery filter."; | |
1467 | |
1468 // Failed to start discovery. This can only happen if the count is at 0. | |
1469 DCHECK_EQ(num_discovery_sessions_, 0); | |
1470 DCHECK(discovery_request_pending_); | |
1471 discovery_request_pending_ = false; | |
1472 | |
1473 error_callback.Run(outcome); | |
1474 | |
1475 // Try to add a new discovery session for each queued request. | |
1476 ProcessQueuedDiscoveryRequests(); | |
1477 } | |
1478 | |
1479 void BluetoothAdapterChromeOS::OnSetDiscoveryFilter( | |
1480 const base::Closure& callback, | |
1481 const DiscoverySessionErrorCallback& error_callback) { | |
1482 // Report success on the original request and increment the count. | |
1483 VLOG(1) << __func__; | |
1484 if (IsPresent()) { | |
1485 callback.Run(); | |
1486 } else { | |
1487 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); | |
1488 } | |
1489 } | |
1490 | |
1491 void BluetoothAdapterChromeOS::OnSetDiscoveryFilterError( | |
1492 const base::Closure& callback, | |
1493 const DiscoverySessionErrorCallback& error_callback, | |
1494 const std::string& error_name, | |
1495 const std::string& error_message) { | |
1496 LOG(WARNING) << object_path_.value() | |
1497 << ": Failed to set discovery filter: " << error_name << ": " | |
1498 << error_message; | |
1499 | |
1500 UMABluetoothDiscoverySessionOutcome outcome = | |
1501 TranslateDiscoveryErrorToUMA(error_name); | |
1502 if (outcome == UMABluetoothDiscoverySessionOutcome::FAILED) { | |
1503 // bluez/doc/adapter-api.txt says "Failed" is returned from | |
1504 // SetDiscoveryFilter when the controller doesn't support the requested | |
1505 // transport. | |
1506 outcome = UMABluetoothDiscoverySessionOutcome:: | |
1507 CHROMEOS_DBUS_FAILED_MAYBE_UNSUPPORTED_TRANSPORT; | |
1508 } | |
1509 error_callback.Run(outcome); | |
1510 | |
1511 // Try to add a new discovery session for each queued request. | |
1512 ProcessQueuedDiscoveryRequests(); | |
1513 } | |
1514 | |
1515 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { | |
1516 while (!discovery_request_queue_.empty()) { | |
1517 VLOG(1) << "Process queued discovery request."; | |
1518 DiscoveryParamTuple params = discovery_request_queue_.front(); | |
1519 discovery_request_queue_.pop(); | |
1520 AddDiscoverySession(std::get<0>(params), std::get<1>(params), | |
1521 std::get<2>(params)); | |
1522 | |
1523 // If the queued request resulted in a pending call, then let it | |
1524 // asynchonously process the remaining queued requests once the pending | |
1525 // call returns. | |
1526 if (discovery_request_pending_) | |
1527 return; | |
1528 } | |
1529 } | |
1530 | |
1531 } // namespace chromeos | |
OLD | NEW |