Mark //testing/perf target testonly.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_chromeos.cc
blob285b1dc1331c8d8e33c8a9fa299deb2198b319df
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 bool BluetoothDeviceChromeOS::ExpectingPinCode() const {
270 return pairing_.get() && pairing_->ExpectingPinCode();
273 bool BluetoothDeviceChromeOS::ExpectingPasskey() const {
274 return pairing_.get() && pairing_->ExpectingPasskey();
277 bool BluetoothDeviceChromeOS::ExpectingConfirmation() const {
278 return pairing_.get() && pairing_->ExpectingConfirmation();
281 void BluetoothDeviceChromeOS::GetConnectionInfo(
282 const ConnectionInfoCallback& callback) {
283 // DBus method call should gracefully return an error if the device is not
284 // currently connected.
285 DBusThreadManager::Get()->GetBluetoothDeviceClient()->GetConnInfo(
286 object_path_, base::Bind(&BluetoothDeviceChromeOS::OnGetConnInfo,
287 weak_ptr_factory_.GetWeakPtr(), callback),
288 base::Bind(&BluetoothDeviceChromeOS::OnGetConnInfoError,
289 weak_ptr_factory_.GetWeakPtr(), callback));
292 void BluetoothDeviceChromeOS::Connect(
293 BluetoothDevice::PairingDelegate* pairing_delegate,
294 const base::Closure& callback,
295 const ConnectErrorCallback& error_callback) {
296 if (num_connecting_calls_++ == 0)
297 adapter_->NotifyDeviceChanged(this);
299 VLOG(1) << object_path_.value() << ": Connecting, " << num_connecting_calls_
300 << " in progress";
302 if (IsPaired() || !pairing_delegate || !IsPairable()) {
303 // No need to pair, or unable to, skip straight to connection.
304 ConnectInternal(false, callback, error_callback);
305 } else {
306 // Initiate high-security connection with pairing.
307 BeginPairing(pairing_delegate);
309 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
310 Pair(object_path_,
311 base::Bind(&BluetoothDeviceChromeOS::OnPair,
312 weak_ptr_factory_.GetWeakPtr(),
313 callback, error_callback),
314 base::Bind(&BluetoothDeviceChromeOS::OnPairError,
315 weak_ptr_factory_.GetWeakPtr(),
316 error_callback));
320 void BluetoothDeviceChromeOS::SetPinCode(const std::string& pincode) {
321 if (!pairing_.get())
322 return;
324 pairing_->SetPinCode(pincode);
327 void BluetoothDeviceChromeOS::SetPasskey(uint32 passkey) {
328 if (!pairing_.get())
329 return;
331 pairing_->SetPasskey(passkey);
334 void BluetoothDeviceChromeOS::ConfirmPairing() {
335 if (!pairing_.get())
336 return;
338 pairing_->ConfirmPairing();
341 void BluetoothDeviceChromeOS::RejectPairing() {
342 if (!pairing_.get())
343 return;
345 pairing_->RejectPairing();
348 void BluetoothDeviceChromeOS::CancelPairing() {
349 bool canceled = false;
351 // If there is a callback in progress that we can reply to then use that
352 // to cancel the current pairing request.
353 if (pairing_.get() && pairing_->CancelPairing())
354 canceled = true;
356 // If not we have to send an explicit CancelPairing() to the device instead.
357 if (!canceled) {
358 VLOG(1) << object_path_.value() << ": No pairing context or callback. "
359 << "Sending explicit cancel";
360 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
361 CancelPairing(
362 object_path_,
363 base::Bind(&base::DoNothing),
364 base::Bind(&BluetoothDeviceChromeOS::OnCancelPairingError,
365 weak_ptr_factory_.GetWeakPtr()));
368 // Since there is no callback to this method it's possible that the pairing
369 // delegate is going to be freed before things complete (indeed it's
370 // documented that this is the method you should call while freeing the
371 // pairing delegate), so clear our the context holding on to it.
372 EndPairing();
375 void BluetoothDeviceChromeOS::Disconnect(const base::Closure& callback,
376 const ErrorCallback& error_callback) {
377 VLOG(1) << object_path_.value() << ": Disconnecting";
378 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
379 Disconnect(
380 object_path_,
381 base::Bind(&BluetoothDeviceChromeOS::OnDisconnect,
382 weak_ptr_factory_.GetWeakPtr(),
383 callback),
384 base::Bind(&BluetoothDeviceChromeOS::OnDisconnectError,
385 weak_ptr_factory_.GetWeakPtr(),
386 error_callback));
389 void BluetoothDeviceChromeOS::Forget(const ErrorCallback& error_callback) {
390 VLOG(1) << object_path_.value() << ": Removing device";
391 DBusThreadManager::Get()->GetBluetoothAdapterClient()->
392 RemoveDevice(
393 adapter_->object_path(),
394 object_path_,
395 base::Bind(&base::DoNothing),
396 base::Bind(&BluetoothDeviceChromeOS::OnForgetError,
397 weak_ptr_factory_.GetWeakPtr(),
398 error_callback));
401 void BluetoothDeviceChromeOS::ConnectToService(
402 const BluetoothUUID& uuid,
403 const ConnectToServiceCallback& callback,
404 const ConnectToServiceErrorCallback& error_callback) {
405 VLOG(1) << object_path_.value() << ": Connecting to service: "
406 << uuid.canonical_value();
407 scoped_refptr<BluetoothSocketChromeOS> socket =
408 BluetoothSocketChromeOS::CreateBluetoothSocket(
409 ui_task_runner_, socket_thread_);
410 socket->Connect(this, uuid, BluetoothSocketChromeOS::SECURITY_LEVEL_MEDIUM,
411 base::Bind(callback, socket), error_callback);
414 void BluetoothDeviceChromeOS::ConnectToServiceInsecurely(
415 const BluetoothUUID& uuid,
416 const ConnectToServiceCallback& callback,
417 const ConnectToServiceErrorCallback& error_callback) {
418 VLOG(1) << object_path_.value() << ": Connecting insecurely to service: "
419 << uuid.canonical_value();
420 scoped_refptr<BluetoothSocketChromeOS> socket =
421 BluetoothSocketChromeOS::CreateBluetoothSocket(
422 ui_task_runner_, socket_thread_);
423 socket->Connect(this, uuid, BluetoothSocketChromeOS::SECURITY_LEVEL_LOW,
424 base::Bind(callback, socket), error_callback);
427 void BluetoothDeviceChromeOS::CreateGattConnection(
428 const GattConnectionCallback& callback,
429 const ConnectErrorCallback& error_callback) {
430 // TODO(armansito): Until there is a way to create a reference counted GATT
431 // connection in bluetoothd, simply do a regular connect.
432 Connect(NULL,
433 base::Bind(&BluetoothDeviceChromeOS::OnCreateGattConnection,
434 weak_ptr_factory_.GetWeakPtr(),
435 callback),
436 error_callback);
439 BluetoothPairingChromeOS* BluetoothDeviceChromeOS::BeginPairing(
440 BluetoothDevice::PairingDelegate* pairing_delegate) {
441 pairing_.reset(new BluetoothPairingChromeOS(this, pairing_delegate));
442 return pairing_.get();
445 void BluetoothDeviceChromeOS::EndPairing() {
446 pairing_.reset();
449 BluetoothPairingChromeOS* BluetoothDeviceChromeOS::GetPairing() const {
450 return pairing_.get();
453 void BluetoothDeviceChromeOS::GattServiceAdded(
454 const dbus::ObjectPath& object_path) {
455 if (GetGattService(object_path.value())) {
456 VLOG(1) << "Remote GATT service already exists: " << object_path.value();
457 return;
460 BluetoothGattServiceClient::Properties* properties =
461 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->
462 GetProperties(object_path);
463 DCHECK(properties);
464 if (properties->device.value() != object_path_) {
465 VLOG(2) << "Remote GATT service does not belong to this device.";
466 return;
469 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress();
471 BluetoothRemoteGattServiceChromeOS* service =
472 new BluetoothRemoteGattServiceChromeOS(adapter_, this, object_path);
474 gatt_services_[service->GetIdentifier()] = service;
475 DCHECK(service->object_path() == object_path);
476 DCHECK(service->GetUUID().IsValid());
478 DCHECK(adapter_);
479 adapter_->NotifyGattServiceAdded(service);
482 void BluetoothDeviceChromeOS::GattServiceRemoved(
483 const dbus::ObjectPath& object_path) {
484 GattServiceMap::iterator iter = gatt_services_.find(object_path.value());
485 if (iter == gatt_services_.end()) {
486 VLOG(3) << "Unknown GATT service removed: " << object_path.value();
487 return;
490 VLOG(1) << "Removing remote GATT service from device: " << GetAddress();
492 BluetoothRemoteGattServiceChromeOS* service =
493 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second);
494 DCHECK(service->object_path() == object_path);
495 gatt_services_.erase(iter);
497 DCHECK(adapter_);
498 adapter_->NotifyGattServiceRemoved(service);
500 delete service;
503 void BluetoothDeviceChromeOS::OnGetConnInfo(
504 const ConnectionInfoCallback& callback,
505 int16 rssi,
506 int16 transmit_power,
507 int16 max_transmit_power) {
508 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power));
511 void BluetoothDeviceChromeOS::OnGetConnInfoError(
512 const ConnectionInfoCallback& callback,
513 const std::string& error_name,
514 const std::string& error_message) {
515 LOG(WARNING) << object_path_.value()
516 << ": Failed to get connection info: " << error_name << ": "
517 << error_message;
518 callback.Run(ConnectionInfo());
521 void BluetoothDeviceChromeOS::ConnectInternal(
522 bool after_pairing,
523 const base::Closure& callback,
524 const ConnectErrorCallback& error_callback) {
525 VLOG(1) << object_path_.value() << ": Connecting";
526 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
527 Connect(
528 object_path_,
529 base::Bind(&BluetoothDeviceChromeOS::OnConnect,
530 weak_ptr_factory_.GetWeakPtr(),
531 after_pairing,
532 callback),
533 base::Bind(&BluetoothDeviceChromeOS::OnConnectError,
534 weak_ptr_factory_.GetWeakPtr(),
535 after_pairing,
536 error_callback));
539 void BluetoothDeviceChromeOS::OnConnect(bool after_pairing,
540 const base::Closure& callback) {
541 if (--num_connecting_calls_ == 0)
542 adapter_->NotifyDeviceChanged(this);
544 DCHECK(num_connecting_calls_ >= 0);
545 VLOG(1) << object_path_.value() << ": Connected, " << num_connecting_calls_
546 << " still in progress";
548 SetTrusted();
550 if (after_pairing)
551 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult",
552 UMA_PAIRING_RESULT_SUCCESS,
553 UMA_PAIRING_RESULT_COUNT);
555 callback.Run();
558 void BluetoothDeviceChromeOS::OnCreateGattConnection(
559 const GattConnectionCallback& callback) {
560 scoped_ptr<device::BluetoothGattConnection> conn(
561 new BluetoothGattConnectionChromeOS(
562 adapter_, GetAddress(), object_path_));
563 callback.Run(conn.Pass());
566 void BluetoothDeviceChromeOS::OnConnectError(
567 bool after_pairing,
568 const ConnectErrorCallback& error_callback,
569 const std::string& error_name,
570 const std::string& error_message) {
571 if (--num_connecting_calls_ == 0)
572 adapter_->NotifyDeviceChanged(this);
574 DCHECK(num_connecting_calls_ >= 0);
575 LOG(WARNING) << object_path_.value() << ": Failed to connect device: "
576 << error_name << ": " << error_message;
577 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_
578 << " still in progress";
580 // Determine the error code from error_name.
581 ConnectErrorCode error_code = ERROR_UNKNOWN;
582 if (error_name == bluetooth_device::kErrorFailed) {
583 error_code = ERROR_FAILED;
584 } else if (error_name == bluetooth_device::kErrorInProgress) {
585 error_code = ERROR_INPROGRESS;
586 } else if (error_name == bluetooth_device::kErrorNotSupported) {
587 error_code = ERROR_UNSUPPORTED_DEVICE;
590 if (after_pairing)
591 RecordPairingResult(error_code);
592 error_callback.Run(error_code);
595 void BluetoothDeviceChromeOS::OnPair(
596 const base::Closure& callback,
597 const ConnectErrorCallback& error_callback) {
598 VLOG(1) << object_path_.value() << ": Paired";
600 EndPairing();
602 ConnectInternal(true, callback, error_callback);
605 void BluetoothDeviceChromeOS::OnPairError(
606 const ConnectErrorCallback& error_callback,
607 const std::string& error_name,
608 const std::string& error_message) {
609 if (--num_connecting_calls_ == 0)
610 adapter_->NotifyDeviceChanged(this);
612 DCHECK(num_connecting_calls_ >= 0);
613 LOG(WARNING) << object_path_.value() << ": Failed to pair device: "
614 << error_name << ": " << error_message;
615 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_
616 << " still in progress";
618 EndPairing();
620 // Determine the error code from error_name.
621 ConnectErrorCode error_code = ERROR_UNKNOWN;
622 if (error_name == bluetooth_device::kErrorConnectionAttemptFailed) {
623 error_code = ERROR_FAILED;
624 } else if (error_name == bluetooth_device::kErrorFailed) {
625 error_code = ERROR_FAILED;
626 } else if (error_name == bluetooth_device::kErrorAuthenticationFailed) {
627 error_code = ERROR_AUTH_FAILED;
628 } else if (error_name == bluetooth_device::kErrorAuthenticationCanceled) {
629 error_code = ERROR_AUTH_CANCELED;
630 } else if (error_name == bluetooth_device::kErrorAuthenticationRejected) {
631 error_code = ERROR_AUTH_REJECTED;
632 } else if (error_name == bluetooth_device::kErrorAuthenticationTimeout) {
633 error_code = ERROR_AUTH_TIMEOUT;
636 RecordPairingResult(error_code);
637 error_callback.Run(error_code);
640 void BluetoothDeviceChromeOS::OnCancelPairingError(
641 const std::string& error_name,
642 const std::string& error_message) {
643 LOG(WARNING) << object_path_.value() << ": Failed to cancel pairing: "
644 << error_name << ": " << error_message;
647 void BluetoothDeviceChromeOS::SetTrusted() {
648 // Unconditionally send the property change, rather than checking the value
649 // first; there's no harm in doing this and it solves any race conditions
650 // with the property becoming true or false and this call happening before
651 // we get the D-Bus signal about the earlier change.
652 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
653 GetProperties(object_path_)->trusted.Set(
654 true,
655 base::Bind(&BluetoothDeviceChromeOS::OnSetTrusted,
656 weak_ptr_factory_.GetWeakPtr()));
659 void BluetoothDeviceChromeOS::OnSetTrusted(bool success) {
660 LOG_IF(WARNING, !success) << object_path_.value()
661 << ": Failed to set device as trusted";
664 void BluetoothDeviceChromeOS::OnDisconnect(const base::Closure& callback) {
665 VLOG(1) << object_path_.value() << ": Disconnected";
666 callback.Run();
669 void BluetoothDeviceChromeOS::OnDisconnectError(
670 const ErrorCallback& error_callback,
671 const std::string& error_name,
672 const std::string& error_message) {
673 LOG(WARNING) << object_path_.value() << ": Failed to disconnect device: "
674 << error_name << ": " << error_message;
675 error_callback.Run();
678 void BluetoothDeviceChromeOS::OnForgetError(
679 const ErrorCallback& error_callback,
680 const std::string& error_name,
681 const std::string& error_message) {
682 LOG(WARNING) << object_path_.value() << ": Failed to remove device: "
683 << error_name << ": " << error_message;
684 error_callback.Run();
687 } // namespace chromeos