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.
5 #include "device/bluetooth/bluetooth_adapter_chromeos.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/sys_info.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "chromeos/dbus/bluetooth_adapter_client.h"
18 #include "chromeos/dbus/bluetooth_agent_manager_client.h"
19 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
20 #include "chromeos/dbus/bluetooth_device_client.h"
21 #include "chromeos/dbus/bluetooth_input_client.h"
22 #include "chromeos/dbus/dbus_thread_manager.h"
23 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h"
24 #include "device/bluetooth/bluetooth_advertisement_chromeos.h"
25 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h"
26 #include "device/bluetooth/bluetooth_device.h"
27 #include "device/bluetooth/bluetooth_device_chromeos.h"
28 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
29 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h"
30 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h"
31 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h"
32 #include "device/bluetooth/bluetooth_socket_chromeos.h"
33 #include "device/bluetooth/bluetooth_socket_thread.h"
34 #include "device/bluetooth/bluetooth_uuid.h"
35 #include "third_party/cros_system_api/dbus/service_constants.h"
37 using device::BluetoothAdapter
;
38 using device::BluetoothAudioSink
;
39 using device::BluetoothDevice
;
40 using device::BluetoothDiscoveryFilter
;
41 using device::BluetoothSocket
;
42 using device::BluetoothUUID
;
46 // The agent path is relatively meaningless since BlueZ only permits one to
47 // exist per D-Bus connection, it just has to be unique within Chromium.
48 const char kAgentPath
[] = "/org/chromium/bluetooth_agent";
50 void OnUnregisterAgentError(const std::string
& error_name
,
51 const std::string
& error_message
) {
52 // It's okay if the agent didn't exist, it means we never saw an adapter.
53 if (error_name
== bluetooth_agent_manager::kErrorDoesNotExist
)
56 LOG(WARNING
) << "Failed to unregister pairing agent: "
57 << error_name
<< ": " << error_message
;
65 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapter::CreateAdapter(
66 const InitCallback
& init_callback
) {
67 return chromeos::BluetoothAdapterChromeOS::CreateAdapter();
75 base::WeakPtr
<BluetoothAdapter
> BluetoothAdapterChromeOS::CreateAdapter() {
76 BluetoothAdapterChromeOS
* adapter
= new BluetoothAdapterChromeOS();
77 return adapter
->weak_ptr_factory_
.GetWeakPtr();
80 void BluetoothAdapterChromeOS::Shutdown() {
81 if (dbus_is_shutdown_
)
83 DCHECK(DBusThreadManager::IsInitialized())
84 << "Call BluetoothAdapterFactory::Shutdown() before "
85 "DBusThreadManager::Shutdown().";
88 RemoveAdapter(); // Also deletes devices_.
89 DCHECK(devices_
.empty());
90 // profiles_ should be empty because all BluetoothSockets have been signaled
91 // that this adapter is disappearing.
92 DCHECK(profiles_
.empty());
94 for (auto& it
: profile_queues_
)
96 profile_queues_
.clear();
98 DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveObserver(this);
99 DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this);
100 DBusThreadManager::Get()->GetBluetoothInputClient()->RemoveObserver(this);
102 VLOG(1) << "Unregistering pairing agent";
103 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->UnregisterAgent(
104 dbus::ObjectPath(kAgentPath
), base::Bind(&base::DoNothing
),
105 base::Bind(&OnUnregisterAgentError
));
108 dbus_is_shutdown_
= true;
111 BluetoothAdapterChromeOS::BluetoothAdapterChromeOS()
112 : dbus_is_shutdown_(false),
113 num_discovery_sessions_(0),
114 discovery_request_pending_(false),
115 weak_ptr_factory_(this) {
116 ui_task_runner_
= base::ThreadTaskRunnerHandle::Get();
117 socket_thread_
= device::BluetoothSocketThread::Get();
119 DBusThreadManager::Get()->GetBluetoothAdapterClient()->AddObserver(this);
120 DBusThreadManager::Get()->GetBluetoothDeviceClient()->AddObserver(this);
121 DBusThreadManager::Get()->GetBluetoothInputClient()->AddObserver(this);
123 // Register the pairing agent.
124 dbus::Bus
* system_bus
= DBusThreadManager::Get()->GetSystemBus();
125 agent_
.reset(BluetoothAgentServiceProvider::Create(
126 system_bus
, dbus::ObjectPath(kAgentPath
), this));
127 DCHECK(agent_
.get());
129 std::vector
<dbus::ObjectPath
> object_paths
=
130 DBusThreadManager::Get()->GetBluetoothAdapterClient()->GetAdapters();
132 if (!object_paths
.empty()) {
133 VLOG(1) << object_paths
.size() << " Bluetooth adapter(s) available.";
134 SetAdapter(object_paths
[0]);
138 BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() {
142 std::string
BluetoothAdapterChromeOS::GetAddress() const {
144 return std::string();
146 BluetoothAdapterClient::Properties
* properties
=
147 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
148 GetProperties(object_path_
);
151 return BluetoothDevice::CanonicalizeAddress(properties
->address
.value());
154 std::string
BluetoothAdapterChromeOS::GetName() const {
156 return std::string();
158 BluetoothAdapterClient::Properties
* properties
=
159 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
160 GetProperties(object_path_
);
163 return properties
->alias
.value();
166 void BluetoothAdapterChromeOS::SetName(const std::string
& name
,
167 const base::Closure
& callback
,
168 const ErrorCallback
& error_callback
) {
170 error_callback
.Run();
174 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
175 GetProperties(object_path_
)->alias
.Set(
177 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted
,
178 weak_ptr_factory_
.GetWeakPtr(),
183 bool BluetoothAdapterChromeOS::IsInitialized() const {
187 bool BluetoothAdapterChromeOS::IsPresent() const {
188 return !dbus_is_shutdown_
&& !object_path_
.value().empty();
191 bool BluetoothAdapterChromeOS::IsPowered() const {
195 BluetoothAdapterClient::Properties
* properties
=
196 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
197 GetProperties(object_path_
);
199 return properties
->powered
.value();
202 void BluetoothAdapterChromeOS::SetPowered(
204 const base::Closure
& callback
,
205 const ErrorCallback
& error_callback
) {
207 error_callback
.Run();
211 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
212 GetProperties(object_path_
)->powered
.Set(
214 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted
,
215 weak_ptr_factory_
.GetWeakPtr(),
220 bool BluetoothAdapterChromeOS::IsDiscoverable() const {
224 BluetoothAdapterClient::Properties
* properties
=
225 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
226 GetProperties(object_path_
);
228 return properties
->discoverable
.value();
231 void BluetoothAdapterChromeOS::SetDiscoverable(
233 const base::Closure
& callback
,
234 const ErrorCallback
& error_callback
) {
236 error_callback
.Run();
240 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
241 GetProperties(object_path_
)->discoverable
.Set(
243 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoverable
,
244 weak_ptr_factory_
.GetWeakPtr(),
249 bool BluetoothAdapterChromeOS::IsDiscovering() const {
253 BluetoothAdapterClient::Properties
* properties
=
254 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
255 GetProperties(object_path_
);
257 return properties
->discovering
.value();
260 void BluetoothAdapterChromeOS::CreateRfcommService(
261 const BluetoothUUID
& uuid
,
262 const ServiceOptions
& options
,
263 const CreateServiceCallback
& callback
,
264 const CreateServiceErrorCallback
& error_callback
) {
265 DCHECK(!dbus_is_shutdown_
);
266 VLOG(1) << object_path_
.value() << ": Creating RFCOMM service: "
267 << uuid
.canonical_value();
268 scoped_refptr
<BluetoothSocketChromeOS
> socket
=
269 BluetoothSocketChromeOS::CreateBluetoothSocket(
270 ui_task_runner_
, socket_thread_
);
272 BluetoothSocketChromeOS::kRfcomm
,
275 base::Bind(callback
, socket
),
279 void BluetoothAdapterChromeOS::CreateL2capService(
280 const BluetoothUUID
& uuid
,
281 const ServiceOptions
& options
,
282 const CreateServiceCallback
& callback
,
283 const CreateServiceErrorCallback
& error_callback
) {
284 DCHECK(!dbus_is_shutdown_
);
285 VLOG(1) << object_path_
.value() << ": Creating L2CAP service: "
286 << uuid
.canonical_value();
287 scoped_refptr
<BluetoothSocketChromeOS
> socket
=
288 BluetoothSocketChromeOS::CreateBluetoothSocket(
289 ui_task_runner_
, socket_thread_
);
291 BluetoothSocketChromeOS::kL2cap
,
294 base::Bind(callback
, socket
),
298 void BluetoothAdapterChromeOS::RegisterAudioSink(
299 const BluetoothAudioSink::Options
& options
,
300 const device::BluetoothAdapter::AcquiredCallback
& callback
,
301 const BluetoothAudioSink::ErrorCallback
& error_callback
) {
302 VLOG(1) << "Registering audio sink";
303 if (!this->IsPresent()) {
304 error_callback
.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER
);
307 scoped_refptr
<BluetoothAudioSinkChromeOS
> audio_sink(
308 new BluetoothAudioSinkChromeOS(this));
309 audio_sink
->Register(
310 options
, base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink
,
311 weak_ptr_factory_
.GetWeakPtr(), callback
,
312 error_callback
, audio_sink
),
316 void BluetoothAdapterChromeOS::RegisterAdvertisement(
317 scoped_ptr
<device::BluetoothAdvertisement::Data
> advertisement_data
,
318 const CreateAdvertisementCallback
& callback
,
319 const CreateAdvertisementErrorCallback
& error_callback
) {
320 scoped_refptr
<BluetoothAdvertisementChromeOS
> advertisement(
321 new BluetoothAdvertisementChromeOS(advertisement_data
.Pass(), this));
322 advertisement
->Register(base::Bind(callback
, advertisement
), error_callback
);
325 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal(
326 BluetoothDevice::PairingDelegate
* pairing_delegate
) {
327 // Before removing a pairing delegate make sure that there aren't any devices
328 // currently using it; if there are, clear the pairing context which will
329 // make any responses no-ops.
330 for (DevicesMap::iterator iter
= devices_
.begin();
331 iter
!= devices_
.end(); ++iter
) {
332 BluetoothDeviceChromeOS
* device_chromeos
=
333 static_cast<BluetoothDeviceChromeOS
*>(iter
->second
);
335 BluetoothPairingChromeOS
* pairing
= device_chromeos
->GetPairing();
336 if (pairing
&& pairing
->GetPairingDelegate() == pairing_delegate
)
337 device_chromeos
->EndPairing();
341 void BluetoothAdapterChromeOS::AdapterAdded(
342 const dbus::ObjectPath
& object_path
) {
343 // Set the adapter to the newly added adapter only if no adapter is present.
345 SetAdapter(object_path
);
348 void BluetoothAdapterChromeOS::AdapterRemoved(
349 const dbus::ObjectPath
& object_path
) {
350 if (object_path
== object_path_
)
354 void BluetoothAdapterChromeOS::AdapterPropertyChanged(
355 const dbus::ObjectPath
& object_path
,
356 const std::string
& property_name
) {
357 if (object_path
!= object_path_
)
361 BluetoothAdapterClient::Properties
* properties
=
362 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
363 GetProperties(object_path_
);
365 if (property_name
== properties
->powered
.name())
366 PoweredChanged(properties
->powered
.value());
367 else if (property_name
== properties
->discoverable
.name())
368 DiscoverableChanged(properties
->discoverable
.value());
369 else if (property_name
== properties
->discovering
.name())
370 DiscoveringChanged(properties
->discovering
.value());
373 void BluetoothAdapterChromeOS::DeviceAdded(
374 const dbus::ObjectPath
& object_path
) {
375 DCHECK(DBusThreadManager::Get());
376 BluetoothDeviceClient::Properties
* properties
=
377 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
378 GetProperties(object_path
);
379 if (!properties
|| properties
->adapter
.value() != object_path_
)
383 BluetoothDeviceChromeOS
* device_chromeos
=
384 new BluetoothDeviceChromeOS(this,
388 DCHECK(devices_
.find(device_chromeos
->GetAddress()) == devices_
.end());
390 devices_
[device_chromeos
->GetAddress()] = device_chromeos
;
392 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
393 DeviceAdded(this, device_chromeos
));
396 void BluetoothAdapterChromeOS::DeviceRemoved(
397 const dbus::ObjectPath
& object_path
) {
398 for (DevicesMap::iterator iter
= devices_
.begin();
399 iter
!= devices_
.end(); ++iter
) {
400 BluetoothDeviceChromeOS
* device_chromeos
=
401 static_cast<BluetoothDeviceChromeOS
*>(iter
->second
);
402 if (device_chromeos
->object_path() == object_path
) {
403 devices_
.erase(iter
);
405 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
406 DeviceRemoved(this, device_chromeos
));
407 delete device_chromeos
;
413 void BluetoothAdapterChromeOS::DevicePropertyChanged(
414 const dbus::ObjectPath
& object_path
,
415 const std::string
& property_name
) {
416 BluetoothDeviceChromeOS
* device_chromeos
= GetDeviceWithPath(object_path
);
417 if (!device_chromeos
)
420 BluetoothDeviceClient::Properties
* properties
=
421 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
422 GetProperties(object_path
);
424 if (property_name
== properties
->bluetooth_class
.name() ||
425 property_name
== properties
->address
.name() ||
426 property_name
== properties
->alias
.name() ||
427 property_name
== properties
->paired
.name() ||
428 property_name
== properties
->trusted
.name() ||
429 property_name
== properties
->connected
.name() ||
430 property_name
== properties
->uuids
.name() ||
431 property_name
== properties
->rssi
.name())
432 NotifyDeviceChanged(device_chromeos
);
434 // When a device becomes paired, mark it as trusted so that the user does
435 // not need to approve every incoming connection
436 if (property_name
== properties
->paired
.name() &&
437 properties
->paired
.value() && !properties
->trusted
.value())
438 device_chromeos
->SetTrusted();
440 // UMA connection counting
441 if (property_name
== properties
->connected
.name()) {
442 // PlayStation joystick tries to reconnect after disconnection from USB.
443 // If it is still not trusted, set it, so it becomes available on the
444 // list of known devices.
445 if (properties
->connected
.value() && device_chromeos
->IsTrustable() &&
446 !properties
->trusted
.value())
447 device_chromeos
->SetTrusted();
451 for (DevicesMap::iterator iter
= devices_
.begin();
452 iter
!= devices_
.end(); ++iter
) {
453 if (iter
->second
->IsPaired() && iter
->second
->IsConnected())
457 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count
);
461 void BluetoothAdapterChromeOS::InputPropertyChanged(
462 const dbus::ObjectPath
& object_path
,
463 const std::string
& property_name
) {
464 BluetoothDeviceChromeOS
* device_chromeos
= GetDeviceWithPath(object_path
);
465 if (!device_chromeos
)
468 BluetoothInputClient::Properties
* properties
=
469 DBusThreadManager::Get()->GetBluetoothInputClient()->
470 GetProperties(object_path
);
472 // Properties structure can be removed, which triggers a change in the
473 // BluetoothDevice::IsConnectable() property, as does a change in the
474 // actual reconnect_mode property.
476 property_name
== properties
->reconnect_mode
.name())
477 NotifyDeviceChanged(device_chromeos
);
480 void BluetoothAdapterChromeOS::Released() {
481 VLOG(1) << "Release";
484 DCHECK(agent_
.get());
486 // Called after we unregister the pairing agent, e.g. when changing I/O
487 // capabilities. Nothing much to be done right now.
490 void BluetoothAdapterChromeOS::RequestPinCode(
491 const dbus::ObjectPath
& device_path
,
492 const PinCodeCallback
& callback
) {
494 DCHECK(agent_
.get());
495 VLOG(1) << device_path
.value() << ": RequestPinCode";
497 BluetoothPairingChromeOS
* pairing
= GetPairing(device_path
);
499 callback
.Run(REJECTED
, "");
503 pairing
->RequestPinCode(callback
);
506 void BluetoothAdapterChromeOS::DisplayPinCode(
507 const dbus::ObjectPath
& device_path
,
508 const std::string
& pincode
) {
510 DCHECK(agent_
.get());
511 VLOG(1) << device_path
.value() << ": DisplayPinCode: " << pincode
;
513 BluetoothPairingChromeOS
* pairing
= GetPairing(device_path
);
517 pairing
->DisplayPinCode(pincode
);
520 void BluetoothAdapterChromeOS::RequestPasskey(
521 const dbus::ObjectPath
& device_path
,
522 const PasskeyCallback
& callback
) {
524 DCHECK(agent_
.get());
525 VLOG(1) << device_path
.value() << ": RequestPasskey";
527 BluetoothPairingChromeOS
* pairing
= GetPairing(device_path
);
529 callback
.Run(REJECTED
, 0);
533 pairing
->RequestPasskey(callback
);
536 void BluetoothAdapterChromeOS::DisplayPasskey(
537 const dbus::ObjectPath
& device_path
,
541 DCHECK(agent_
.get());
542 VLOG(1) << device_path
.value() << ": DisplayPasskey: " << passkey
543 << " (" << entered
<< " entered)";
545 BluetoothPairingChromeOS
* pairing
= GetPairing(device_path
);
550 pairing
->DisplayPasskey(passkey
);
552 pairing
->KeysEntered(entered
);
555 void BluetoothAdapterChromeOS::RequestConfirmation(
556 const dbus::ObjectPath
& device_path
,
558 const ConfirmationCallback
& callback
) {
560 DCHECK(agent_
.get());
561 VLOG(1) << device_path
.value() << ": RequestConfirmation: " << passkey
;
563 BluetoothPairingChromeOS
* pairing
= GetPairing(device_path
);
565 callback
.Run(REJECTED
);
569 pairing
->RequestConfirmation(passkey
, callback
);
572 void BluetoothAdapterChromeOS::RequestAuthorization(
573 const dbus::ObjectPath
& device_path
,
574 const ConfirmationCallback
& callback
) {
576 DCHECK(agent_
.get());
577 VLOG(1) << device_path
.value() << ": RequestAuthorization";
579 BluetoothPairingChromeOS
* pairing
= GetPairing(device_path
);
581 callback
.Run(REJECTED
);
585 pairing
->RequestAuthorization(callback
);
588 void BluetoothAdapterChromeOS::AuthorizeService(
589 const dbus::ObjectPath
& device_path
,
590 const std::string
& uuid
,
591 const ConfirmationCallback
& callback
) {
593 DCHECK(agent_
.get());
594 VLOG(1) << device_path
.value() << ": AuthorizeService: " << uuid
;
596 BluetoothDeviceChromeOS
* device_chromeos
= GetDeviceWithPath(device_path
);
597 if (!device_chromeos
) {
598 callback
.Run(CANCELLED
);
602 // We always set paired devices to Trusted, so the only reason that this
603 // method call would ever be called is in the case of a race condition where
604 // our "Set('Trusted', true)" method call is still pending in the Bluetooth
605 // daemon because it's busy handling the incoming connection.
606 if (device_chromeos
->IsPaired()) {
607 callback
.Run(SUCCESS
);
611 // TODO(keybuk): reject service authorizations when not paired, determine
612 // whether this is acceptable long-term.
613 LOG(WARNING
) << "Rejecting service connection from unpaired device "
614 << device_chromeos
->GetAddress() << " for UUID " << uuid
;
615 callback
.Run(REJECTED
);
618 void BluetoothAdapterChromeOS::Cancel() {
620 DCHECK(agent_
.get());
624 void BluetoothAdapterChromeOS::OnRegisterAgent() {
625 VLOG(1) << "Pairing agent registered, requesting to be made default";
627 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->
629 dbus::ObjectPath(kAgentPath
),
630 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent
,
631 weak_ptr_factory_
.GetWeakPtr()),
632 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError
,
633 weak_ptr_factory_
.GetWeakPtr()));
636 void BluetoothAdapterChromeOS::OnRegisterAgentError(
637 const std::string
& error_name
,
638 const std::string
& error_message
) {
639 // Our agent being already registered isn't an error.
640 if (error_name
== bluetooth_agent_manager::kErrorAlreadyExists
)
643 LOG(WARNING
) << ": Failed to register pairing agent: "
644 << error_name
<< ": " << error_message
;
647 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() {
648 VLOG(1) << "Pairing agent now default";
651 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError(
652 const std::string
& error_name
,
653 const std::string
& error_message
) {
654 LOG(WARNING
) << ": Failed to make pairing agent default: "
655 << error_name
<< ": " << error_message
;
658 void BluetoothAdapterChromeOS::OnRegisterAudioSink(
659 const device::BluetoothAdapter::AcquiredCallback
& callback
,
660 const device::BluetoothAudioSink::ErrorCallback
& error_callback
,
661 scoped_refptr
<BluetoothAudioSink
> audio_sink
) {
663 VLOG(1) << "Failed to register audio sink, adapter not present";
664 error_callback
.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER
);
667 DCHECK(audio_sink
.get());
668 callback
.Run(audio_sink
);
671 BluetoothDeviceChromeOS
*
672 BluetoothAdapterChromeOS::GetDeviceWithPath(
673 const dbus::ObjectPath
& object_path
) {
677 for (DevicesMap::iterator iter
= devices_
.begin(); iter
!= devices_
.end();
679 BluetoothDeviceChromeOS
* device_chromeos
=
680 static_cast<BluetoothDeviceChromeOS
*>(iter
->second
);
681 if (device_chromeos
->object_path() == object_path
)
682 return device_chromeos
;
688 BluetoothPairingChromeOS
* BluetoothAdapterChromeOS::GetPairing(
689 const dbus::ObjectPath
& object_path
) {
691 BluetoothDeviceChromeOS
* device_chromeos
= GetDeviceWithPath(object_path
);
692 if (!device_chromeos
) {
693 LOG(WARNING
) << "Pairing Agent request for unknown device: "
694 << object_path
.value();
698 BluetoothPairingChromeOS
* pairing
= device_chromeos
->GetPairing();
702 // The device doesn't have its own pairing context, so this is an incoming
703 // pairing request that should use our best default delegate (if we have one).
704 BluetoothDevice::PairingDelegate
* pairing_delegate
= DefaultPairingDelegate();
705 if (!pairing_delegate
)
708 return device_chromeos
->BeginPairing(pairing_delegate
);
711 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath
& object_path
) {
712 DCHECK(!IsPresent());
713 DCHECK(!dbus_is_shutdown_
);
714 object_path_
= object_path
;
716 VLOG(1) << object_path_
.value() << ": using adapter.";
718 VLOG(1) << "Registering pairing agent";
719 DBusThreadManager::Get()->GetBluetoothAgentManagerClient()->
721 dbus::ObjectPath(kAgentPath
),
722 bluetooth_agent_manager::kKeyboardDisplayCapability
,
723 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent
,
724 weak_ptr_factory_
.GetWeakPtr()),
725 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgentError
,
726 weak_ptr_factory_
.GetWeakPtr()));
728 SetDefaultAdapterName();
730 BluetoothAdapterClient::Properties
* properties
=
731 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
732 GetProperties(object_path_
);
734 PresentChanged(true);
736 if (properties
->powered
.value())
737 PoweredChanged(true);
738 if (properties
->discoverable
.value())
739 DiscoverableChanged(true);
740 if (properties
->discovering
.value())
741 DiscoveringChanged(true);
743 std::vector
<dbus::ObjectPath
> device_paths
=
744 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
745 GetDevicesForAdapter(object_path_
);
747 for (std::vector
<dbus::ObjectPath
>::iterator iter
= device_paths
.begin();
748 iter
!= device_paths
.end(); ++iter
) {
753 void BluetoothAdapterChromeOS::SetDefaultAdapterName() {
755 std::string board
= base::SysInfo::GetLsbReleaseBoard();
757 if (board
.substr(0, 6) == "stumpy") {
759 } else if (board
.substr(0, 4) == "link") {
760 alias
= "Chromebook Pixel";
762 alias
= "Chromebook";
765 SetName(alias
, base::Bind(&base::DoNothing
), base::Bind(&base::DoNothing
));
768 void BluetoothAdapterChromeOS::RemoveAdapter() {
770 VLOG(1) << object_path_
.value() << ": adapter removed.";
772 BluetoothAdapterClient::Properties
* properties
=
773 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
774 GetProperties(object_path_
);
776 object_path_
= dbus::ObjectPath("");
778 if (properties
->powered
.value())
779 PoweredChanged(false);
780 if (properties
->discoverable
.value())
781 DiscoverableChanged(false);
782 if (properties
->discovering
.value())
783 DiscoveringChanged(false);
785 // Copy the devices list here and clear the original so that when we
786 // send DeviceRemoved(), GetDevices() returns no devices.
787 DevicesMap devices
= devices_
;
790 for (DevicesMap::iterator iter
= devices
.begin();
791 iter
!= devices
.end(); ++iter
) {
792 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
793 DeviceRemoved(this, iter
->second
));
797 PresentChanged(false);
800 void BluetoothAdapterChromeOS::PoweredChanged(bool powered
) {
801 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
802 AdapterPoweredChanged(this, powered
));
805 void BluetoothAdapterChromeOS::DiscoverableChanged(bool discoverable
) {
806 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
807 AdapterDiscoverableChanged(this, discoverable
));
810 void BluetoothAdapterChromeOS::DiscoveringChanged(
812 // If the adapter stopped discovery due to a reason other than a request by
813 // us, reset the count to 0.
814 VLOG(1) << "Discovering changed: " << discovering
;
815 if (!discovering
&& !discovery_request_pending_
816 && num_discovery_sessions_
> 0) {
817 VLOG(1) << "Marking sessions as inactive.";
818 num_discovery_sessions_
= 0;
819 MarkDiscoverySessionsAsInactive();
821 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
822 AdapterDiscoveringChanged(this, discovering
));
825 void BluetoothAdapterChromeOS::PresentChanged(bool present
) {
826 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
827 AdapterPresentChanged(this, present
));
830 void BluetoothAdapterChromeOS::NotifyDeviceChanged(
831 BluetoothDeviceChromeOS
* device
) {
832 DCHECK(device
->adapter_
== this);
834 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
, observers_
,
835 DeviceChanged(this, device
));
838 void BluetoothAdapterChromeOS::NotifyGattServiceAdded(
839 BluetoothRemoteGattServiceChromeOS
* service
) {
840 DCHECK_EQ(service
->GetAdapter(), this);
842 static_cast<BluetoothDeviceChromeOS
*>(service
->GetDevice())->adapter_
,
845 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
847 GattServiceAdded(this, service
->GetDevice(), service
));
850 void BluetoothAdapterChromeOS::NotifyGattServiceRemoved(
851 BluetoothRemoteGattServiceChromeOS
* service
) {
852 DCHECK_EQ(service
->GetAdapter(), this);
854 static_cast<BluetoothDeviceChromeOS
*>(service
->GetDevice())->adapter_
,
857 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
859 GattServiceRemoved(this, service
->GetDevice(), service
));
862 void BluetoothAdapterChromeOS::NotifyGattServiceChanged(
863 BluetoothRemoteGattServiceChromeOS
* service
) {
864 DCHECK_EQ(service
->GetAdapter(), this);
866 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
868 GattServiceChanged(this, service
));
871 void BluetoothAdapterChromeOS::NotifyGattDiscoveryComplete(
872 BluetoothRemoteGattServiceChromeOS
* service
) {
873 DCHECK_EQ(service
->GetAdapter(), this);
875 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
877 GattDiscoveryCompleteForService(this, service
));
880 void BluetoothAdapterChromeOS::NotifyGattCharacteristicAdded(
881 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
) {
882 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS
*>(
883 characteristic
->GetService())->GetAdapter(),
886 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
888 GattCharacteristicAdded(this, characteristic
));
891 void BluetoothAdapterChromeOS::NotifyGattCharacteristicRemoved(
892 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
) {
893 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS
*>(
894 characteristic
->GetService())->GetAdapter(),
897 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
899 GattCharacteristicRemoved(this, characteristic
));
902 void BluetoothAdapterChromeOS::NotifyGattDescriptorAdded(
903 BluetoothRemoteGattDescriptorChromeOS
* descriptor
) {
904 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS
*>(
905 descriptor
->GetCharacteristic()->GetService())->GetAdapter(),
908 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
910 GattDescriptorAdded(this, descriptor
));
913 void BluetoothAdapterChromeOS::NotifyGattDescriptorRemoved(
914 BluetoothRemoteGattDescriptorChromeOS
* descriptor
) {
915 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS
*>(
916 descriptor
->GetCharacteristic()->GetService())->GetAdapter(),
919 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
921 GattDescriptorRemoved(this, descriptor
));
924 void BluetoothAdapterChromeOS::NotifyGattCharacteristicValueChanged(
925 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
,
926 const std::vector
<uint8
>& value
) {
927 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS
*>(
928 characteristic
->GetService())->GetAdapter(),
932 BluetoothAdapter::Observer
,
934 GattCharacteristicValueChanged(this, characteristic
, value
));
937 void BluetoothAdapterChromeOS::NotifyGattDescriptorValueChanged(
938 BluetoothRemoteGattDescriptorChromeOS
* descriptor
,
939 const std::vector
<uint8
>& value
) {
940 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS
*>(
941 descriptor
->GetCharacteristic()->GetService())->GetAdapter(),
944 FOR_EACH_OBSERVER(BluetoothAdapter::Observer
,
946 GattDescriptorValueChanged(this, descriptor
, value
));
949 void BluetoothAdapterChromeOS::UseProfile(
950 const BluetoothUUID
& uuid
,
951 const dbus::ObjectPath
& device_path
,
952 const BluetoothProfileManagerClient::Options
& options
,
953 BluetoothProfileServiceProvider::Delegate
* delegate
,
954 const ProfileRegisteredCallback
& success_callback
,
955 const ErrorCompletionCallback
& error_callback
) {
959 VLOG(2) << "Adapter not present, erroring out";
960 error_callback
.Run("Adapter not present");
964 if (profiles_
.find(uuid
) != profiles_
.end()) {
965 // TODO(jamuraa) check that the options are the same and error when they are
967 SetProfileDelegate(uuid
, device_path
, delegate
, success_callback
,
972 if (profile_queues_
.find(uuid
) == profile_queues_
.end()) {
973 BluetoothAdapterProfileChromeOS::Register(
975 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfile
, this, uuid
),
976 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfileError
, this,
979 profile_queues_
[uuid
] = new std::vector
<RegisterProfileCompletionPair
>();
982 profile_queues_
[uuid
]->push_back(std::make_pair(
983 base::Bind(&BluetoothAdapterChromeOS::SetProfileDelegate
, this, uuid
,
984 device_path
, delegate
, success_callback
, error_callback
),
988 void BluetoothAdapterChromeOS::ReleaseProfile(
989 const dbus::ObjectPath
& device_path
,
990 BluetoothAdapterProfileChromeOS
* profile
) {
991 VLOG(2) << "Releasing Profile: " << profile
->uuid().canonical_value()
992 << " from " << device_path
.value();
993 profile
->RemoveDelegate(
994 device_path
, base::Bind(&BluetoothAdapterChromeOS::RemoveProfile
,
995 weak_ptr_factory_
.GetWeakPtr(), profile
->uuid()));
998 void BluetoothAdapterChromeOS::RemoveProfile(const BluetoothUUID
& uuid
) {
999 VLOG(2) << "Remove Profile: " << uuid
.canonical_value();
1001 if (profiles_
.find(uuid
) != profiles_
.end()) {
1002 delete profiles_
[uuid
];
1003 profiles_
.erase(uuid
);
1007 void BluetoothAdapterChromeOS::OnRegisterProfile(
1008 const BluetoothUUID
& uuid
,
1009 scoped_ptr
<BluetoothAdapterProfileChromeOS
> profile
) {
1010 profiles_
[uuid
] = profile
.release();
1012 if (profile_queues_
.find(uuid
) == profile_queues_
.end())
1015 for (auto& it
: *profile_queues_
[uuid
])
1017 delete profile_queues_
[uuid
];
1018 profile_queues_
.erase(uuid
);
1021 void BluetoothAdapterChromeOS::SetProfileDelegate(
1022 const BluetoothUUID
& uuid
,
1023 const dbus::ObjectPath
& device_path
,
1024 BluetoothProfileServiceProvider::Delegate
* delegate
,
1025 const ProfileRegisteredCallback
& success_callback
,
1026 const ErrorCompletionCallback
& error_callback
) {
1027 if (profiles_
.find(uuid
) == profiles_
.end()) {
1028 error_callback
.Run("Cannot find profile!");
1032 if (profiles_
[uuid
]->SetDelegate(device_path
, delegate
)) {
1033 success_callback
.Run(profiles_
[uuid
]);
1037 error_callback
.Run(bluetooth_agent_manager::kErrorAlreadyExists
);
1040 void BluetoothAdapterChromeOS::OnRegisterProfileError(
1041 const BluetoothUUID
& uuid
,
1042 const std::string
& error_name
,
1043 const std::string
& error_message
) {
1044 VLOG(2) << object_path_
.value() << ": Failed to register profile: "
1045 << error_name
<< ": " << error_message
;
1046 if (profile_queues_
.find(uuid
) == profile_queues_
.end())
1049 for (auto& it
: *profile_queues_
[uuid
])
1050 it
.second
.Run(error_message
);
1052 delete profile_queues_
[uuid
];
1053 profile_queues_
.erase(uuid
);
1056 void BluetoothAdapterChromeOS::OnSetDiscoverable(
1057 const base::Closure
& callback
,
1058 const ErrorCallback
& error_callback
,
1061 error_callback
.Run();
1065 // Set the discoverable_timeout property to zero so the adapter remains
1066 // discoverable forever.
1067 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
1068 GetProperties(object_path_
)->discoverable_timeout
.Set(
1070 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted
,
1071 weak_ptr_factory_
.GetWeakPtr(),
1076 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted(
1077 const base::Closure
& callback
,
1078 const ErrorCallback
& error_callback
,
1080 if (IsPresent() && success
)
1083 error_callback
.Run();
1086 void BluetoothAdapterChromeOS::AddDiscoverySession(
1087 BluetoothDiscoveryFilter
* discovery_filter
,
1088 const base::Closure
& callback
,
1089 const ErrorCallback
& error_callback
) {
1091 error_callback
.Run();
1094 VLOG(1) << __func__
;
1095 if (discovery_request_pending_
) {
1096 // The pending request is either to stop a previous session or to start a
1097 // new one. Either way, queue this one.
1098 DCHECK(num_discovery_sessions_
== 1 || num_discovery_sessions_
== 0);
1099 VLOG(1) << "Pending request to start/stop device discovery. Queueing "
1100 << "request to start a new discovery session.";
1101 discovery_request_queue_
.push(
1102 std::make_tuple(discovery_filter
, callback
, error_callback
));
1106 // The adapter is already discovering.
1107 if (num_discovery_sessions_
> 0) {
1108 DCHECK(IsDiscovering());
1109 DCHECK(!discovery_request_pending_
);
1110 num_discovery_sessions_
++;
1111 SetDiscoveryFilter(BluetoothDiscoveryFilter::Merge(
1112 GetMergedDiscoveryFilter().get(), discovery_filter
),
1113 callback
, error_callback
);
1117 // There are no active discovery sessions.
1118 DCHECK_EQ(num_discovery_sessions_
, 0);
1120 if (discovery_filter
) {
1121 discovery_request_pending_
= true;
1123 scoped_ptr
<BluetoothDiscoveryFilter
> df(new BluetoothDiscoveryFilter(
1124 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL
));
1125 df
->CopyFrom(*discovery_filter
);
1128 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter
,
1129 weak_ptr_factory_
.GetWeakPtr(), callback
, error_callback
),
1130 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError
,
1131 weak_ptr_factory_
.GetWeakPtr(), callback
, error_callback
));
1134 current_filter_
.reset();
1136 // This is the first request to start device discovery.
1137 discovery_request_pending_
= true;
1138 DBusThreadManager::Get()->GetBluetoothAdapterClient()->StartDiscovery(
1140 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery
,
1141 weak_ptr_factory_
.GetWeakPtr(), callback
, error_callback
),
1142 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError
,
1143 weak_ptr_factory_
.GetWeakPtr(), callback
, error_callback
));
1146 void BluetoothAdapterChromeOS::RemoveDiscoverySession(
1147 BluetoothDiscoveryFilter
* discovery_filter
,
1148 const base::Closure
& callback
,
1149 const ErrorCallback
& error_callback
) {
1151 error_callback
.Run();
1155 VLOG(1) << __func__
;
1156 // There are active sessions other than the one currently being removed.
1157 if (num_discovery_sessions_
> 1) {
1158 DCHECK(IsDiscovering());
1159 DCHECK(!discovery_request_pending_
);
1160 num_discovery_sessions_
--;
1162 SetDiscoveryFilter(GetMergedDiscoveryFilterMasked(discovery_filter
),
1163 callback
, error_callback
);
1167 // If there is a pending request to BlueZ, then queue this request.
1168 if (discovery_request_pending_
) {
1169 VLOG(1) << "Pending request to start/stop device discovery. Queueing "
1170 << "request to stop discovery session.";
1171 error_callback
.Run();
1175 // There are no active sessions. Return error.
1176 if (num_discovery_sessions_
== 0) {
1177 // TODO(armansito): This should never happen once we have the
1178 // DiscoverySession API. Replace this case with an assert once it's
1179 // the deprecated methods have been removed. (See crbug.com/3445008).
1180 VLOG(1) << "No active discovery sessions. Returning error.";
1181 error_callback
.Run();
1185 // There is exactly one active discovery session. Request BlueZ to stop
1187 DCHECK_EQ(num_discovery_sessions_
, 1);
1188 discovery_request_pending_
= true;
1189 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
1192 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery
,
1193 weak_ptr_factory_
.GetWeakPtr(),
1195 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError
,
1196 weak_ptr_factory_
.GetWeakPtr(),
1200 void BluetoothAdapterChromeOS::SetDiscoveryFilter(
1201 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
,
1202 const base::Closure
& callback
,
1203 const ErrorCallback
& error_callback
) {
1205 error_callback
.Run();
1209 // If old and new filter are equal (null) then don't make request, just call
1211 if (!current_filter_
&& !discovery_filter
.get()) {
1216 // If old and new filter are not null and equal then don't make request, just
1217 // call succes callback
1218 if (current_filter_
&& discovery_filter
&&
1219 current_filter_
->Equals(*discovery_filter
)) {
1224 current_filter_
.reset(discovery_filter
.release());
1226 chromeos::BluetoothAdapterClient::DiscoveryFilter dbus_discovery_filter
;
1228 if (current_filter_
.get()) {
1232 std::set
<device::BluetoothUUID
> uuids
;
1234 if (current_filter_
->GetPathloss(&pathloss
))
1235 dbus_discovery_filter
.pathloss
.reset(new uint16_t(pathloss
));
1237 if (current_filter_
->GetRSSI(&rssi
))
1238 dbus_discovery_filter
.rssi
.reset(new int16_t(rssi
));
1240 transport
= current_filter_
->GetTransport();
1241 if (transport
== BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
) {
1242 dbus_discovery_filter
.transport
.reset(new std::string("le"));
1243 } else if (transport
==
1244 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC
) {
1245 dbus_discovery_filter
.transport
.reset(new std::string("bredr"));
1246 } else if (transport
==
1247 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL
) {
1248 dbus_discovery_filter
.transport
.reset(new std::string("auto"));
1251 current_filter_
->GetUUIDs(uuids
);
1253 dbus_discovery_filter
.uuids
=
1254 scoped_ptr
<std::vector
<std::string
>>(new std::vector
<std::string
>);
1256 for (const auto& it
: uuids
)
1257 dbus_discovery_filter
.uuids
.get()->push_back(it
.value());
1261 DBusThreadManager::Get()->GetBluetoothAdapterClient()->SetDiscoveryFilter(
1262 object_path_
, dbus_discovery_filter
,
1263 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilter
,
1264 weak_ptr_factory_
.GetWeakPtr(), callback
, error_callback
),
1265 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilterError
,
1266 weak_ptr_factory_
.GetWeakPtr(), callback
, error_callback
));
1269 void BluetoothAdapterChromeOS::OnStartDiscovery(
1270 const base::Closure
& callback
,
1271 const ErrorCallback
& error_callback
) {
1272 // Report success on the original request and increment the count.
1273 VLOG(1) << __func__
;
1274 DCHECK(discovery_request_pending_
);
1275 DCHECK_EQ(num_discovery_sessions_
, 0);
1276 discovery_request_pending_
= false;
1277 num_discovery_sessions_
++;
1281 error_callback
.Run();
1283 // Try to add a new discovery session for each queued request.
1284 ProcessQueuedDiscoveryRequests();
1287 void BluetoothAdapterChromeOS::OnStartDiscoveryError(
1288 const base::Closure
& callback
,
1289 const ErrorCallback
& error_callback
,
1290 const std::string
& error_name
,
1291 const std::string
& error_message
) {
1292 LOG(WARNING
) << object_path_
.value() << ": Failed to start discovery: "
1293 << error_name
<< ": " << error_message
;
1295 // Failed to start discovery. This can only happen if the count is at 0.
1296 DCHECK_EQ(num_discovery_sessions_
, 0);
1297 DCHECK(discovery_request_pending_
);
1298 discovery_request_pending_
= false;
1300 // Discovery request may fail if discovery was previously initiated by Chrome,
1301 // but the session were invalidated due to the discovery state unexpectedly
1302 // changing to false and then back to true. In this case, report success.
1303 if (IsPresent() && error_name
== bluetooth_device::kErrorInProgress
&&
1305 VLOG(1) << "Discovery previously initiated. Reporting success.";
1306 num_discovery_sessions_
++;
1309 error_callback
.Run();
1312 // Try to add a new discovery session for each queued request.
1313 ProcessQueuedDiscoveryRequests();
1316 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure
& callback
) {
1317 // Report success on the original request and decrement the count.
1318 VLOG(1) << __func__
;
1319 DCHECK(discovery_request_pending_
);
1320 DCHECK_EQ(num_discovery_sessions_
, 1);
1321 discovery_request_pending_
= false;
1322 num_discovery_sessions_
--;
1325 current_filter_
.reset();
1327 // Try to add a new discovery session for each queued request.
1328 ProcessQueuedDiscoveryRequests();
1331 void BluetoothAdapterChromeOS::OnStopDiscoveryError(
1332 const ErrorCallback
& error_callback
,
1333 const std::string
& error_name
,
1334 const std::string
& error_message
) {
1335 LOG(WARNING
) << object_path_
.value() << ": Failed to stop discovery: "
1336 << error_name
<< ": " << error_message
;
1338 // Failed to stop discovery. This can only happen if the count is at 1.
1339 DCHECK(discovery_request_pending_
);
1340 DCHECK_EQ(num_discovery_sessions_
, 1);
1341 discovery_request_pending_
= false;
1342 error_callback
.Run();
1344 // Try to add a new discovery session for each queued request.
1345 ProcessQueuedDiscoveryRequests();
1348 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter(
1349 const base::Closure
& callback
,
1350 const ErrorCallback
& error_callback
) {
1351 // This is the first request to start device discovery.
1352 DCHECK(discovery_request_pending_
);
1353 DCHECK_EQ(num_discovery_sessions_
, 0);
1355 DBusThreadManager::Get()->GetBluetoothAdapterClient()->StartDiscovery(
1357 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery
,
1358 weak_ptr_factory_
.GetWeakPtr(), callback
, error_callback
),
1359 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError
,
1360 weak_ptr_factory_
.GetWeakPtr(), callback
, error_callback
));
1363 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError(
1364 const base::Closure
& callback
,
1365 const ErrorCallback
& error_callback
) {
1366 LOG(WARNING
) << object_path_
.value()
1367 << ": Failed to pre set discovery filter.";
1369 // Failed to start discovery. This can only happen if the count is at 0.
1370 DCHECK_EQ(num_discovery_sessions_
, 0);
1371 DCHECK(discovery_request_pending_
);
1372 discovery_request_pending_
= false;
1374 error_callback
.Run();
1376 // Try to add a new discovery session for each queued request.
1377 ProcessQueuedDiscoveryRequests();
1380 void BluetoothAdapterChromeOS::OnSetDiscoveryFilter(
1381 const base::Closure
& callback
,
1382 const ErrorCallback
& error_callback
) {
1383 // Report success on the original request and increment the count.
1384 VLOG(1) << __func__
;
1388 error_callback
.Run();
1391 void BluetoothAdapterChromeOS::OnSetDiscoveryFilterError(
1392 const base::Closure
& callback
,
1393 const ErrorCallback
& error_callback
,
1394 const std::string
& error_name
,
1395 const std::string
& error_message
) {
1396 LOG(WARNING
) << object_path_
.value()
1397 << ": Failed to set discovery filter: " << error_name
<< ": "
1400 error_callback
.Run();
1402 // Try to add a new discovery session for each queued request.
1403 ProcessQueuedDiscoveryRequests();
1406 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() {
1407 while (!discovery_request_queue_
.empty()) {
1408 VLOG(1) << "Process queued discovery request.";
1409 DiscoveryParamTuple params
= discovery_request_queue_
.front();
1410 discovery_request_queue_
.pop();
1411 AddDiscoverySession(std::get
<0>(params
), std::get
<1>(params
),
1412 std::get
<2>(params
));
1414 // If the queued request resulted in a pending call, then let it
1415 // asynchonously process the remaining queued requests once the pending
1417 if (discovery_request_pending_
)
1422 } // namespace chromeos