Update V8 to version 4.7.21.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_chromeos.cc
blob30b5379714ff9b96aa44208d455d94c9d2723acf
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_device_chromeos.h"
7 #include <stdio.h>
9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "chromeos/dbus/bluetooth_adapter_client.h"
15 #include "chromeos/dbus/bluetooth_device_client.h"
16 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
17 #include "chromeos/dbus/bluetooth_input_client.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "dbus/bus.h"
20 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
21 #include "device/bluetooth/bluetooth_gatt_connection_chromeos.h"
22 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
23 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h"
24 #include "device/bluetooth/bluetooth_socket.h"
25 #include "device/bluetooth/bluetooth_socket_chromeos.h"
26 #include "device/bluetooth/bluetooth_socket_thread.h"
27 #include "device/bluetooth/bluetooth_uuid.h"
28 #include "third_party/cros_system_api/dbus/service_constants.h"
30 using device::BluetoothDevice;
31 using device::BluetoothSocket;
32 using device::BluetoothUUID;
34 namespace {
36 // Histogram enumerations for pairing results.
37 enum UMAPairingResult {
38 UMA_PAIRING_RESULT_SUCCESS,
39 UMA_PAIRING_RESULT_INPROGRESS,
40 UMA_PAIRING_RESULT_FAILED,
41 UMA_PAIRING_RESULT_AUTH_FAILED,
42 UMA_PAIRING_RESULT_AUTH_CANCELED,
43 UMA_PAIRING_RESULT_AUTH_REJECTED,
44 UMA_PAIRING_RESULT_AUTH_TIMEOUT,
45 UMA_PAIRING_RESULT_UNSUPPORTED_DEVICE,
46 UMA_PAIRING_RESULT_UNKNOWN_ERROR,
47 // NOTE: Add new pairing results immediately above this line. Make sure to
48 // update the enum list in tools/histogram/histograms.xml accordinly.
49 UMA_PAIRING_RESULT_COUNT
52 void ParseModalias(const dbus::ObjectPath& object_path,
53 BluetoothDevice::VendorIDSource* vendor_id_source,
54 uint16* vendor_id,
55 uint16* product_id,
56 uint16* device_id) {
57 chromeos::BluetoothDeviceClient::Properties* properties =
58 chromeos::DBusThreadManager::Get()->GetBluetoothDeviceClient()->
59 GetProperties(object_path);
60 DCHECK(properties);
62 std::string modalias = properties->modalias.value();
63 BluetoothDevice::VendorIDSource source_value;
64 int vendor_value, product_value, device_value;
66 if (sscanf(modalias.c_str(), "bluetooth:v%04xp%04xd%04x",
67 &vendor_value, &product_value, &device_value) == 3) {
68 source_value = BluetoothDevice::VENDOR_ID_BLUETOOTH;
69 } else if (sscanf(modalias.c_str(), "usb:v%04xp%04xd%04x",
70 &vendor_value, &product_value, &device_value) == 3) {
71 source_value = BluetoothDevice::VENDOR_ID_USB;
72 } else {
73 return;
76 if (vendor_id_source != NULL)
77 *vendor_id_source = source_value;
78 if (vendor_id != NULL)
79 *vendor_id = vendor_value;
80 if (product_id != NULL)
81 *product_id = product_value;
82 if (device_id != NULL)
83 *device_id = device_value;
86 void RecordPairingResult(BluetoothDevice::ConnectErrorCode error_code) {
87 UMAPairingResult pairing_result;
88 switch (error_code) {
89 case BluetoothDevice::ERROR_INPROGRESS:
90 pairing_result = UMA_PAIRING_RESULT_INPROGRESS;
91 break;
92 case BluetoothDevice::ERROR_FAILED:
93 pairing_result = UMA_PAIRING_RESULT_FAILED;
94 break;
95 case BluetoothDevice::ERROR_AUTH_FAILED:
96 pairing_result = UMA_PAIRING_RESULT_AUTH_FAILED;
97 break;
98 case BluetoothDevice::ERROR_AUTH_CANCELED:
99 pairing_result = UMA_PAIRING_RESULT_AUTH_CANCELED;
100 break;
101 case BluetoothDevice::ERROR_AUTH_REJECTED:
102 pairing_result = UMA_PAIRING_RESULT_AUTH_REJECTED;
103 break;
104 case BluetoothDevice::ERROR_AUTH_TIMEOUT:
105 pairing_result = UMA_PAIRING_RESULT_AUTH_TIMEOUT;
106 break;
107 case BluetoothDevice::ERROR_UNSUPPORTED_DEVICE:
108 pairing_result = UMA_PAIRING_RESULT_UNSUPPORTED_DEVICE;
109 break;
110 default:
111 pairing_result = UMA_PAIRING_RESULT_UNKNOWN_ERROR;
114 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult",
115 pairing_result,
116 UMA_PAIRING_RESULT_COUNT);
119 } // namespace
121 namespace chromeos {
123 BluetoothDeviceChromeOS::BluetoothDeviceChromeOS(
124 BluetoothAdapterChromeOS* adapter,
125 const dbus::ObjectPath& object_path,
126 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
127 scoped_refptr<device::BluetoothSocketThread> socket_thread)
128 : adapter_(adapter),
129 object_path_(object_path),
130 num_connecting_calls_(0),
131 connection_monitor_started_(false),
132 ui_task_runner_(ui_task_runner),
133 socket_thread_(socket_thread),
134 weak_ptr_factory_(this) {
135 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->AddObserver(this);
137 // Add all known GATT services.
138 const std::vector<dbus::ObjectPath> gatt_services =
139 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->GetServices();
140 for (std::vector<dbus::ObjectPath>::const_iterator it = gatt_services.begin();
141 it != gatt_services.end(); ++it) {
142 GattServiceAdded(*it);
146 BluetoothDeviceChromeOS::~BluetoothDeviceChromeOS() {
147 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->
148 RemoveObserver(this);
150 // Copy the GATT services list here and clear the original so that when we
151 // send GattServiceRemoved(), GetGattServices() returns no services.
152 GattServiceMap gatt_services = gatt_services_;
153 gatt_services_.clear();
154 for (GattServiceMap::iterator iter = gatt_services.begin();
155 iter != gatt_services.end(); ++iter) {
156 DCHECK(adapter_);
157 adapter_->NotifyGattServiceRemoved(
158 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second));
159 delete iter->second;
163 uint32 BluetoothDeviceChromeOS::GetBluetoothClass() const {
164 BluetoothDeviceClient::Properties* properties =
165 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
166 GetProperties(object_path_);
167 DCHECK(properties);
169 return properties->bluetooth_class.value();
172 std::string BluetoothDeviceChromeOS::GetDeviceName() const {
173 BluetoothDeviceClient::Properties* properties =
174 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
175 GetProperties(object_path_);
176 DCHECK(properties);
178 return properties->alias.value();
181 std::string BluetoothDeviceChromeOS::GetAddress() const {
182 BluetoothDeviceClient::Properties* properties =
183 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
184 GetProperties(object_path_);
185 DCHECK(properties);
187 return CanonicalizeAddress(properties->address.value());
190 BluetoothDevice::VendorIDSource
191 BluetoothDeviceChromeOS::GetVendorIDSource() const {
192 VendorIDSource vendor_id_source = VENDOR_ID_UNKNOWN;
193 ParseModalias(object_path_, &vendor_id_source, NULL, NULL, NULL);
194 return vendor_id_source;
197 uint16 BluetoothDeviceChromeOS::GetVendorID() const {
198 uint16 vendor_id = 0;
199 ParseModalias(object_path_, NULL, &vendor_id, NULL, NULL);
200 return vendor_id;
203 uint16 BluetoothDeviceChromeOS::GetProductID() const {
204 uint16 product_id = 0;
205 ParseModalias(object_path_, NULL, NULL, &product_id, NULL);
206 return product_id;
209 uint16 BluetoothDeviceChromeOS::GetDeviceID() const {
210 uint16 device_id = 0;
211 ParseModalias(object_path_, NULL, NULL, NULL, &device_id);
212 return device_id;
215 bool BluetoothDeviceChromeOS::IsPaired() const {
216 BluetoothDeviceClient::Properties* properties =
217 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
218 GetProperties(object_path_);
219 DCHECK(properties);
221 // Trusted devices are devices that don't support pairing but that the
222 // user has explicitly connected; it makes no sense for UI purposes to
223 // treat them differently from each other.
224 return properties->paired.value() || properties->trusted.value();
227 bool BluetoothDeviceChromeOS::IsConnected() const {
228 BluetoothDeviceClient::Properties* properties =
229 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
230 GetProperties(object_path_);
231 DCHECK(properties);
233 return properties->connected.value();
236 bool BluetoothDeviceChromeOS::IsConnectable() const {
237 BluetoothInputClient::Properties* input_properties =
238 DBusThreadManager::Get()->GetBluetoothInputClient()->
239 GetProperties(object_path_);
240 // GetProperties returns NULL when the device does not implement the given
241 // interface. Non HID devices are normally connectable.
242 if (!input_properties)
243 return true;
245 return input_properties->reconnect_mode.value() != "device";
248 bool BluetoothDeviceChromeOS::IsConnecting() const {
249 return num_connecting_calls_ > 0;
252 BluetoothDeviceChromeOS::UUIDList BluetoothDeviceChromeOS::GetUUIDs() const {
253 BluetoothDeviceClient::Properties* properties =
254 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
255 GetProperties(object_path_);
256 DCHECK(properties);
258 std::vector<device::BluetoothUUID> uuids;
259 const std::vector<std::string> &dbus_uuids = properties->uuids.value();
260 for (std::vector<std::string>::const_iterator iter = dbus_uuids.begin();
261 iter != dbus_uuids.end(); ++iter) {
262 device::BluetoothUUID uuid(*iter);
263 DCHECK(uuid.IsValid());
264 uuids.push_back(uuid);
266 return uuids;
269 int16 BluetoothDeviceChromeOS::GetInquiryRSSI() const {
270 BluetoothDeviceClient::Properties* properties =
271 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
272 GetProperties(object_path_);
273 DCHECK(properties);
275 if (!properties->rssi.is_valid())
276 return kUnknownPower;
278 return properties->rssi.value();
281 int16 BluetoothDeviceChromeOS::GetInquiryTxPower() const {
282 BluetoothDeviceClient::Properties* properties =
283 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
284 GetProperties(object_path_);
285 DCHECK(properties);
287 if (!properties->tx_power.is_valid())
288 return kUnknownPower;
290 return properties->tx_power.value();
293 bool BluetoothDeviceChromeOS::ExpectingPinCode() const {
294 return pairing_.get() && pairing_->ExpectingPinCode();
297 bool BluetoothDeviceChromeOS::ExpectingPasskey() const {
298 return pairing_.get() && pairing_->ExpectingPasskey();
301 bool BluetoothDeviceChromeOS::ExpectingConfirmation() const {
302 return pairing_.get() && pairing_->ExpectingConfirmation();
305 void BluetoothDeviceChromeOS::GetConnectionInfo(
306 const ConnectionInfoCallback& callback) {
307 // DBus method call should gracefully return an error if the device is not
308 // currently connected.
309 DBusThreadManager::Get()->GetBluetoothDeviceClient()->GetConnInfo(
310 object_path_, base::Bind(&BluetoothDeviceChromeOS::OnGetConnInfo,
311 weak_ptr_factory_.GetWeakPtr(), callback),
312 base::Bind(&BluetoothDeviceChromeOS::OnGetConnInfoError,
313 weak_ptr_factory_.GetWeakPtr(), callback));
316 void BluetoothDeviceChromeOS::Connect(
317 BluetoothDevice::PairingDelegate* pairing_delegate,
318 const base::Closure& callback,
319 const ConnectErrorCallback& error_callback) {
320 if (num_connecting_calls_++ == 0)
321 adapter_->NotifyDeviceChanged(this);
323 VLOG(1) << object_path_.value() << ": Connecting, " << num_connecting_calls_
324 << " in progress";
326 if (IsPaired() || !pairing_delegate || !IsPairable()) {
327 // No need to pair, or unable to, skip straight to connection.
328 ConnectInternal(false, callback, error_callback);
329 } else {
330 // Initiate high-security connection with pairing.
331 BeginPairing(pairing_delegate);
333 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
334 Pair(object_path_,
335 base::Bind(&BluetoothDeviceChromeOS::OnPair,
336 weak_ptr_factory_.GetWeakPtr(),
337 callback, error_callback),
338 base::Bind(&BluetoothDeviceChromeOS::OnPairError,
339 weak_ptr_factory_.GetWeakPtr(),
340 error_callback));
344 void BluetoothDeviceChromeOS::SetPinCode(const std::string& pincode) {
345 if (!pairing_.get())
346 return;
348 pairing_->SetPinCode(pincode);
351 void BluetoothDeviceChromeOS::SetPasskey(uint32 passkey) {
352 if (!pairing_.get())
353 return;
355 pairing_->SetPasskey(passkey);
358 void BluetoothDeviceChromeOS::ConfirmPairing() {
359 if (!pairing_.get())
360 return;
362 pairing_->ConfirmPairing();
365 void BluetoothDeviceChromeOS::RejectPairing() {
366 if (!pairing_.get())
367 return;
369 pairing_->RejectPairing();
372 void BluetoothDeviceChromeOS::CancelPairing() {
373 bool canceled = false;
375 // If there is a callback in progress that we can reply to then use that
376 // to cancel the current pairing request.
377 if (pairing_.get() && pairing_->CancelPairing())
378 canceled = true;
380 // If not we have to send an explicit CancelPairing() to the device instead.
381 if (!canceled) {
382 VLOG(1) << object_path_.value() << ": No pairing context or callback. "
383 << "Sending explicit cancel";
384 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
385 CancelPairing(
386 object_path_,
387 base::Bind(&base::DoNothing),
388 base::Bind(&BluetoothDeviceChromeOS::OnCancelPairingError,
389 weak_ptr_factory_.GetWeakPtr()));
392 // Since there is no callback to this method it's possible that the pairing
393 // delegate is going to be freed before things complete (indeed it's
394 // documented that this is the method you should call while freeing the
395 // pairing delegate), so clear our the context holding on to it.
396 EndPairing();
399 void BluetoothDeviceChromeOS::Disconnect(const base::Closure& callback,
400 const ErrorCallback& error_callback) {
401 VLOG(1) << object_path_.value() << ": Disconnecting";
402 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
403 Disconnect(
404 object_path_,
405 base::Bind(&BluetoothDeviceChromeOS::OnDisconnect,
406 weak_ptr_factory_.GetWeakPtr(),
407 callback),
408 base::Bind(&BluetoothDeviceChromeOS::OnDisconnectError,
409 weak_ptr_factory_.GetWeakPtr(),
410 error_callback));
413 void BluetoothDeviceChromeOS::Forget(const ErrorCallback& error_callback) {
414 VLOG(1) << object_path_.value() << ": Removing device";
415 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
416 RemoveDevice(
417 adapter_->object_path(),
418 object_path_,
419 base::Bind(&base::DoNothing),
420 base::Bind(&BluetoothDeviceChromeOS::OnForgetError,
421 weak_ptr_factory_.GetWeakPtr(),
422 error_callback));
425 void BluetoothDeviceChromeOS::ConnectToService(
426 const BluetoothUUID& uuid,
427 const ConnectToServiceCallback& callback,
428 const ConnectToServiceErrorCallback& error_callback) {
429 VLOG(1) << object_path_.value() << ": Connecting to service: "
430 << uuid.canonical_value();
431 scoped_refptr<BluetoothSocketChromeOS> socket =
432 BluetoothSocketChromeOS::CreateBluetoothSocket(
433 ui_task_runner_, socket_thread_);
434 socket->Connect(this, uuid, BluetoothSocketChromeOS::SECURITY_LEVEL_MEDIUM,
435 base::Bind(callback, socket), error_callback);
438 void BluetoothDeviceChromeOS::ConnectToServiceInsecurely(
439 const BluetoothUUID& uuid,
440 const ConnectToServiceCallback& callback,
441 const ConnectToServiceErrorCallback& error_callback) {
442 VLOG(1) << object_path_.value() << ": Connecting insecurely to service: "
443 << uuid.canonical_value();
444 scoped_refptr<BluetoothSocketChromeOS> socket =
445 BluetoothSocketChromeOS::CreateBluetoothSocket(
446 ui_task_runner_, socket_thread_);
447 socket->Connect(this, uuid, BluetoothSocketChromeOS::SECURITY_LEVEL_LOW,
448 base::Bind(callback, socket), error_callback);
451 void BluetoothDeviceChromeOS::CreateGattConnection(
452 const GattConnectionCallback& callback,
453 const ConnectErrorCallback& error_callback) {
454 // TODO(sacomoto): Workaround to retrieve the connection for already connected
455 // devices. Currently, BluetoothGattConnection::Disconnect doesn't do
456 // anything, the unique underlying physical GATT connection is kept. This
457 // should be removed once the correct behavour is implemented and the GATT
458 // connections are reference counted (see todo below).
459 if (IsConnected()) {
460 OnCreateGattConnection(callback);
461 return;
464 // TODO(armansito): Until there is a way to create a reference counted GATT
465 // connection in bluetoothd, simply do a regular connect.
466 Connect(NULL,
467 base::Bind(&BluetoothDeviceChromeOS::OnCreateGattConnection,
468 weak_ptr_factory_.GetWeakPtr(),
469 callback),
470 error_callback);
473 BluetoothPairingChromeOS* BluetoothDeviceChromeOS::BeginPairing(
474 BluetoothDevice::PairingDelegate* pairing_delegate) {
475 pairing_.reset(new BluetoothPairingChromeOS(this, pairing_delegate));
476 return pairing_.get();
479 void BluetoothDeviceChromeOS::EndPairing() {
480 pairing_.reset();
483 BluetoothPairingChromeOS* BluetoothDeviceChromeOS::GetPairing() const {
484 return pairing_.get();
487 void BluetoothDeviceChromeOS::GattServiceAdded(
488 const dbus::ObjectPath& object_path) {
489 if (GetGattService(object_path.value())) {
490 VLOG(1) << "Remote GATT service already exists: " << object_path.value();
491 return;
494 BluetoothGattServiceClient::Properties* properties =
495 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->
496 GetProperties(object_path);
497 DCHECK(properties);
498 if (properties->device.value() != object_path_) {
499 VLOG(2) << "Remote GATT service does not belong to this device.";
500 return;
503 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress();
505 BluetoothRemoteGattServiceChromeOS* service =
506 new BluetoothRemoteGattServiceChromeOS(adapter_, this, object_path);
508 gatt_services_[service->GetIdentifier()] = service;
509 DCHECK(service->object_path() == object_path);
510 DCHECK(service->GetUUID().IsValid());
512 DCHECK(adapter_);
513 adapter_->NotifyGattServiceAdded(service);
516 void BluetoothDeviceChromeOS::GattServiceRemoved(
517 const dbus::ObjectPath& object_path) {
518 GattServiceMap::iterator iter = gatt_services_.find(object_path.value());
519 if (iter == gatt_services_.end()) {
520 VLOG(3) << "Unknown GATT service removed: " << object_path.value();
521 return;
524 VLOG(1) << "Removing remote GATT service from device: " << GetAddress();
526 BluetoothRemoteGattServiceChromeOS* service =
527 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second);
528 DCHECK(service->object_path() == object_path);
529 gatt_services_.erase(iter);
531 DCHECK(adapter_);
532 adapter_->NotifyGattServiceRemoved(service);
534 delete service;
537 void BluetoothDeviceChromeOS::OnGetConnInfo(
538 const ConnectionInfoCallback& callback,
539 int16 rssi,
540 int16 transmit_power,
541 int16 max_transmit_power) {
542 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power));
545 void BluetoothDeviceChromeOS::OnGetConnInfoError(
546 const ConnectionInfoCallback& callback,
547 const std::string& error_name,
548 const std::string& error_message) {
549 LOG(WARNING) << object_path_.value()
550 << ": Failed to get connection info: " << error_name << ": "
551 << error_message;
552 callback.Run(ConnectionInfo());
555 void BluetoothDeviceChromeOS::ConnectInternal(
556 bool after_pairing,
557 const base::Closure& callback,
558 const ConnectErrorCallback& error_callback) {
559 VLOG(1) << object_path_.value() << ": Connecting";
560 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
561 Connect(
562 object_path_,
563 base::Bind(&BluetoothDeviceChromeOS::OnConnect,
564 weak_ptr_factory_.GetWeakPtr(),
565 after_pairing,
566 callback),
567 base::Bind(&BluetoothDeviceChromeOS::OnConnectError,
568 weak_ptr_factory_.GetWeakPtr(),
569 after_pairing,
570 error_callback));
573 void BluetoothDeviceChromeOS::OnConnect(bool after_pairing,
574 const base::Closure& callback) {
575 if (--num_connecting_calls_ == 0)
576 adapter_->NotifyDeviceChanged(this);
578 DCHECK(num_connecting_calls_ >= 0);
579 VLOG(1) << object_path_.value() << ": Connected, " << num_connecting_calls_
580 << " still in progress";
582 SetTrusted();
584 if (after_pairing)
585 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult",
586 UMA_PAIRING_RESULT_SUCCESS,
587 UMA_PAIRING_RESULT_COUNT);
589 callback.Run();
592 void BluetoothDeviceChromeOS::OnCreateGattConnection(
593 const GattConnectionCallback& callback) {
594 scoped_ptr<device::BluetoothGattConnection> conn(
595 new BluetoothGattConnectionChromeOS(
596 adapter_, GetAddress(), object_path_));
597 callback.Run(conn.Pass());
600 void BluetoothDeviceChromeOS::OnConnectError(
601 bool after_pairing,
602 const ConnectErrorCallback& error_callback,
603 const std::string& error_name,
604 const std::string& error_message) {
605 if (--num_connecting_calls_ == 0)
606 adapter_->NotifyDeviceChanged(this);
608 DCHECK(num_connecting_calls_ >= 0);
609 LOG(WARNING) << object_path_.value() << ": Failed to connect device: "
610 << error_name << ": " << error_message;
611 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_
612 << " still in progress";
614 // Determine the error code from error_name.
615 ConnectErrorCode error_code = ERROR_UNKNOWN;
616 if (error_name == bluetooth_device::kErrorFailed) {
617 error_code = ERROR_FAILED;
618 } else if (error_name == bluetooth_device::kErrorInProgress) {
619 error_code = ERROR_INPROGRESS;
620 } else if (error_name == bluetooth_device::kErrorNotSupported) {
621 error_code = ERROR_UNSUPPORTED_DEVICE;
624 if (after_pairing)
625 RecordPairingResult(error_code);
626 error_callback.Run(error_code);
629 void BluetoothDeviceChromeOS::OnPair(
630 const base::Closure& callback,
631 const ConnectErrorCallback& error_callback) {
632 VLOG(1) << object_path_.value() << ": Paired";
634 EndPairing();
636 ConnectInternal(true, callback, error_callback);
639 void BluetoothDeviceChromeOS::OnPairError(
640 const ConnectErrorCallback& error_callback,
641 const std::string& error_name,
642 const std::string& error_message) {
643 if (--num_connecting_calls_ == 0)
644 adapter_->NotifyDeviceChanged(this);
646 DCHECK(num_connecting_calls_ >= 0);
647 LOG(WARNING) << object_path_.value() << ": Failed to pair device: "
648 << error_name << ": " << error_message;
649 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_
650 << " still in progress";
652 EndPairing();
654 // Determine the error code from error_name.
655 ConnectErrorCode error_code = ERROR_UNKNOWN;
656 if (error_name == bluetooth_device::kErrorConnectionAttemptFailed) {
657 error_code = ERROR_FAILED;
658 } else if (error_name == bluetooth_device::kErrorFailed) {
659 error_code = ERROR_FAILED;
660 } else if (error_name == bluetooth_device::kErrorAuthenticationFailed) {
661 error_code = ERROR_AUTH_FAILED;
662 } else if (error_name == bluetooth_device::kErrorAuthenticationCanceled) {
663 error_code = ERROR_AUTH_CANCELED;
664 } else if (error_name == bluetooth_device::kErrorAuthenticationRejected) {
665 error_code = ERROR_AUTH_REJECTED;
666 } else if (error_name == bluetooth_device::kErrorAuthenticationTimeout) {
667 error_code = ERROR_AUTH_TIMEOUT;
670 RecordPairingResult(error_code);
671 error_callback.Run(error_code);
674 void BluetoothDeviceChromeOS::OnCancelPairingError(
675 const std::string& error_name,
676 const std::string& error_message) {
677 LOG(WARNING) << object_path_.value() << ": Failed to cancel pairing: "
678 << error_name << ": " << error_message;
681 void BluetoothDeviceChromeOS::SetTrusted() {
682 // Unconditionally send the property change, rather than checking the value
683 // first; there's no harm in doing this and it solves any race conditions
684 // with the property becoming true or false and this call happening before
685 // we get the D-Bus signal about the earlier change.
686 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
687 GetProperties(object_path_)->trusted.Set(
688 true,
689 base::Bind(&BluetoothDeviceChromeOS::OnSetTrusted,
690 weak_ptr_factory_.GetWeakPtr()));
693 void BluetoothDeviceChromeOS::OnSetTrusted(bool success) {
694 LOG_IF(WARNING, !success) << object_path_.value()
695 << ": Failed to set device as trusted";
698 void BluetoothDeviceChromeOS::OnDisconnect(const base::Closure& callback) {
699 VLOG(1) << object_path_.value() << ": Disconnected";
700 callback.Run();
703 void BluetoothDeviceChromeOS::OnDisconnectError(
704 const ErrorCallback& error_callback,
705 const std::string& error_name,
706 const std::string& error_message) {
707 LOG(WARNING) << object_path_.value() << ": Failed to disconnect device: "
708 << error_name << ": " << error_message;
709 error_callback.Run();
712 void BluetoothDeviceChromeOS::OnForgetError(
713 const ErrorCallback& error_callback,
714 const std::string& error_name,
715 const std::string& error_message) {
716 LOG(WARNING) << object_path_.value() << ": Failed to remove device: "
717 << error_name << ": " << error_message;
718 error_callback.Run();
721 } // namespace chromeos