OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluetooth_device_chromeos.h" | 5 #include "device/bluetooth/bluetooth_device_chromeos.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 118 |
119 } // namespace | 119 } // namespace |
120 | 120 |
121 namespace chromeos { | 121 namespace chromeos { |
122 | 122 |
123 BluetoothDeviceChromeOS::BluetoothDeviceChromeOS( | 123 BluetoothDeviceChromeOS::BluetoothDeviceChromeOS( |
124 BluetoothAdapterChromeOS* adapter, | 124 BluetoothAdapterChromeOS* adapter, |
125 const dbus::ObjectPath& object_path, | 125 const dbus::ObjectPath& object_path, |
126 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 126 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
127 scoped_refptr<device::BluetoothSocketThread> socket_thread) | 127 scoped_refptr<device::BluetoothSocketThread> socket_thread) |
128 : adapter_(adapter), | 128 : BluetoothDevice(adapter), |
129 object_path_(object_path), | 129 object_path_(object_path), |
130 num_connecting_calls_(0), | 130 num_connecting_calls_(0), |
131 connection_monitor_started_(false), | 131 connection_monitor_started_(false), |
132 ui_task_runner_(ui_task_runner), | 132 ui_task_runner_(ui_task_runner), |
133 socket_thread_(socket_thread), | 133 socket_thread_(socket_thread), |
134 weak_ptr_factory_(this) { | 134 weak_ptr_factory_(this) { |
135 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->AddObserver(this); | 135 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->AddObserver(this); |
136 | 136 |
137 // Add all known GATT services. | 137 // Add all known GATT services. |
138 const std::vector<dbus::ObjectPath> gatt_services = | 138 const std::vector<dbus::ObjectPath> gatt_services = |
139 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->GetServices(); | 139 DBusThreadManager::Get()->GetBluetoothGattServiceClient()->GetServices(); |
140 for (std::vector<dbus::ObjectPath>::const_iterator it = gatt_services.begin(); | 140 for (std::vector<dbus::ObjectPath>::const_iterator it = gatt_services.begin(); |
141 it != gatt_services.end(); ++it) { | 141 it != gatt_services.end(); ++it) { |
142 GattServiceAdded(*it); | 142 GattServiceAdded(*it); |
143 } | 143 } |
144 } | 144 } |
145 | 145 |
146 BluetoothDeviceChromeOS::~BluetoothDeviceChromeOS() { | 146 BluetoothDeviceChromeOS::~BluetoothDeviceChromeOS() { |
147 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> | 147 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> |
148 RemoveObserver(this); | 148 RemoveObserver(this); |
149 | 149 |
150 // Copy the GATT services list here and clear the original so that when we | 150 // Copy the GATT services list here and clear the original so that when we |
151 // send GattServiceRemoved(), GetGattServices() returns no services. | 151 // send GattServiceRemoved(), GetGattServices() returns no services. |
152 GattServiceMap gatt_services = gatt_services_; | 152 GattServiceMap gatt_services = gatt_services_; |
153 gatt_services_.clear(); | 153 gatt_services_.clear(); |
154 for (GattServiceMap::iterator iter = gatt_services.begin(); | 154 for (GattServiceMap::iterator iter = gatt_services.begin(); |
155 iter != gatt_services.end(); ++iter) { | 155 iter != gatt_services.end(); ++iter) { |
156 DCHECK(adapter_); | 156 DCHECK(adapter_); |
157 adapter_->NotifyGattServiceRemoved( | 157 adapter()->NotifyGattServiceRemoved( |
158 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second)); | 158 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second)); |
159 delete iter->second; | 159 delete iter->second; |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
163 uint32 BluetoothDeviceChromeOS::GetBluetoothClass() const { | 163 uint32 BluetoothDeviceChromeOS::GetBluetoothClass() const { |
164 BluetoothDeviceClient::Properties* properties = | 164 BluetoothDeviceClient::Properties* properties = |
165 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 165 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
166 GetProperties(object_path_); | 166 GetProperties(object_path_); |
167 DCHECK(properties); | 167 DCHECK(properties); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 weak_ptr_factory_.GetWeakPtr(), callback), | 311 weak_ptr_factory_.GetWeakPtr(), callback), |
312 base::Bind(&BluetoothDeviceChromeOS::OnGetConnInfoError, | 312 base::Bind(&BluetoothDeviceChromeOS::OnGetConnInfoError, |
313 weak_ptr_factory_.GetWeakPtr(), callback)); | 313 weak_ptr_factory_.GetWeakPtr(), callback)); |
314 } | 314 } |
315 | 315 |
316 void BluetoothDeviceChromeOS::Connect( | 316 void BluetoothDeviceChromeOS::Connect( |
317 BluetoothDevice::PairingDelegate* pairing_delegate, | 317 BluetoothDevice::PairingDelegate* pairing_delegate, |
318 const base::Closure& callback, | 318 const base::Closure& callback, |
319 const ConnectErrorCallback& error_callback) { | 319 const ConnectErrorCallback& error_callback) { |
320 if (num_connecting_calls_++ == 0) | 320 if (num_connecting_calls_++ == 0) |
321 adapter_->NotifyDeviceChanged(this); | 321 adapter()->NotifyDeviceChanged(this); |
322 | 322 |
323 VLOG(1) << object_path_.value() << ": Connecting, " << num_connecting_calls_ | 323 VLOG(1) << object_path_.value() << ": Connecting, " << num_connecting_calls_ |
324 << " in progress"; | 324 << " in progress"; |
325 | 325 |
326 if (IsPaired() || !pairing_delegate || !IsPairable()) { | 326 if (IsPaired() || !pairing_delegate || !IsPairable()) { |
327 // No need to pair, or unable to, skip straight to connection. | 327 // No need to pair, or unable to, skip straight to connection. |
328 ConnectInternal(false, callback, error_callback); | 328 ConnectInternal(false, callback, error_callback); |
329 } else { | 329 } else { |
330 // Initiate high-security connection with pairing. | 330 // Initiate high-security connection with pairing. |
331 BeginPairing(pairing_delegate); | 331 BeginPairing(pairing_delegate); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 base::Bind(&BluetoothDeviceChromeOS::OnDisconnect, | 405 base::Bind(&BluetoothDeviceChromeOS::OnDisconnect, |
406 weak_ptr_factory_.GetWeakPtr(), | 406 weak_ptr_factory_.GetWeakPtr(), |
407 callback), | 407 callback), |
408 base::Bind(&BluetoothDeviceChromeOS::OnDisconnectError, | 408 base::Bind(&BluetoothDeviceChromeOS::OnDisconnectError, |
409 weak_ptr_factory_.GetWeakPtr(), | 409 weak_ptr_factory_.GetWeakPtr(), |
410 error_callback)); | 410 error_callback)); |
411 } | 411 } |
412 | 412 |
413 void BluetoothDeviceChromeOS::Forget(const ErrorCallback& error_callback) { | 413 void BluetoothDeviceChromeOS::Forget(const ErrorCallback& error_callback) { |
414 VLOG(1) << object_path_.value() << ": Removing device"; | 414 VLOG(1) << object_path_.value() << ": Removing device"; |
415 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 415 DBusThreadManager::Get()->GetBluetoothAdapterClient()->RemoveDevice( |
416 RemoveDevice( | 416 adapter()->object_path(), object_path_, base::Bind(&base::DoNothing), |
417 adapter_->object_path(), | 417 base::Bind(&BluetoothDeviceChromeOS::OnForgetError, |
418 object_path_, | 418 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
419 base::Bind(&base::DoNothing), | |
420 base::Bind(&BluetoothDeviceChromeOS::OnForgetError, | |
421 weak_ptr_factory_.GetWeakPtr(), | |
422 error_callback)); | |
423 } | 419 } |
424 | 420 |
425 void BluetoothDeviceChromeOS::ConnectToService( | 421 void BluetoothDeviceChromeOS::ConnectToService( |
426 const BluetoothUUID& uuid, | 422 const BluetoothUUID& uuid, |
427 const ConnectToServiceCallback& callback, | 423 const ConnectToServiceCallback& callback, |
428 const ConnectToServiceErrorCallback& error_callback) { | 424 const ConnectToServiceErrorCallback& error_callback) { |
429 VLOG(1) << object_path_.value() << ": Connecting to service: " | 425 VLOG(1) << object_path_.value() << ": Connecting to service: " |
430 << uuid.canonical_value(); | 426 << uuid.canonical_value(); |
431 scoped_refptr<BluetoothSocketChromeOS> socket = | 427 scoped_refptr<BluetoothSocketChromeOS> socket = |
432 BluetoothSocketChromeOS::CreateBluetoothSocket( | 428 BluetoothSocketChromeOS::CreateBluetoothSocket( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 } | 473 } |
478 | 474 |
479 void BluetoothDeviceChromeOS::EndPairing() { | 475 void BluetoothDeviceChromeOS::EndPairing() { |
480 pairing_.reset(); | 476 pairing_.reset(); |
481 } | 477 } |
482 | 478 |
483 BluetoothPairingChromeOS* BluetoothDeviceChromeOS::GetPairing() const { | 479 BluetoothPairingChromeOS* BluetoothDeviceChromeOS::GetPairing() const { |
484 return pairing_.get(); | 480 return pairing_.get(); |
485 } | 481 } |
486 | 482 |
| 483 BluetoothAdapterChromeOS* BluetoothDeviceChromeOS::adapter() const { |
| 484 return static_cast<BluetoothAdapterChromeOS*>(adapter_); |
| 485 } |
| 486 |
487 void BluetoothDeviceChromeOS::GattServiceAdded( | 487 void BluetoothDeviceChromeOS::GattServiceAdded( |
488 const dbus::ObjectPath& object_path) { | 488 const dbus::ObjectPath& object_path) { |
489 if (GetGattService(object_path.value())) { | 489 if (GetGattService(object_path.value())) { |
490 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); | 490 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); |
491 return; | 491 return; |
492 } | 492 } |
493 | 493 |
494 BluetoothGattServiceClient::Properties* properties = | 494 BluetoothGattServiceClient::Properties* properties = |
495 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> | 495 DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> |
496 GetProperties(object_path); | 496 GetProperties(object_path); |
497 DCHECK(properties); | 497 DCHECK(properties); |
498 if (properties->device.value() != object_path_) { | 498 if (properties->device.value() != object_path_) { |
499 VLOG(2) << "Remote GATT service does not belong to this device."; | 499 VLOG(2) << "Remote GATT service does not belong to this device."; |
500 return; | 500 return; |
501 } | 501 } |
502 | 502 |
503 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); | 503 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); |
504 | 504 |
505 BluetoothRemoteGattServiceChromeOS* service = | 505 BluetoothRemoteGattServiceChromeOS* service = |
506 new BluetoothRemoteGattServiceChromeOS(adapter_, this, object_path); | 506 new BluetoothRemoteGattServiceChromeOS(adapter(), this, object_path); |
507 | 507 |
508 gatt_services_[service->GetIdentifier()] = service; | 508 gatt_services_[service->GetIdentifier()] = service; |
509 DCHECK(service->object_path() == object_path); | 509 DCHECK(service->object_path() == object_path); |
510 DCHECK(service->GetUUID().IsValid()); | 510 DCHECK(service->GetUUID().IsValid()); |
511 | 511 |
512 DCHECK(adapter_); | 512 DCHECK(adapter_); |
513 adapter_->NotifyGattServiceAdded(service); | 513 adapter()->NotifyGattServiceAdded(service); |
514 } | 514 } |
515 | 515 |
516 void BluetoothDeviceChromeOS::GattServiceRemoved( | 516 void BluetoothDeviceChromeOS::GattServiceRemoved( |
517 const dbus::ObjectPath& object_path) { | 517 const dbus::ObjectPath& object_path) { |
518 GattServiceMap::iterator iter = gatt_services_.find(object_path.value()); | 518 GattServiceMap::iterator iter = gatt_services_.find(object_path.value()); |
519 if (iter == gatt_services_.end()) { | 519 if (iter == gatt_services_.end()) { |
520 VLOG(3) << "Unknown GATT service removed: " << object_path.value(); | 520 VLOG(3) << "Unknown GATT service removed: " << object_path.value(); |
521 return; | 521 return; |
522 } | 522 } |
523 | 523 |
524 VLOG(1) << "Removing remote GATT service from device: " << GetAddress(); | 524 VLOG(1) << "Removing remote GATT service from device: " << GetAddress(); |
525 | 525 |
526 BluetoothRemoteGattServiceChromeOS* service = | 526 BluetoothRemoteGattServiceChromeOS* service = |
527 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second); | 527 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second); |
528 DCHECK(service->object_path() == object_path); | 528 DCHECK(service->object_path() == object_path); |
529 gatt_services_.erase(iter); | 529 gatt_services_.erase(iter); |
530 | 530 |
531 DCHECK(adapter_); | 531 DCHECK(adapter_); |
532 adapter_->NotifyGattServiceRemoved(service); | 532 adapter()->NotifyGattServiceRemoved(service); |
533 | 533 |
534 delete service; | 534 delete service; |
535 } | 535 } |
536 | 536 |
537 void BluetoothDeviceChromeOS::OnGetConnInfo( | 537 void BluetoothDeviceChromeOS::OnGetConnInfo( |
538 const ConnectionInfoCallback& callback, | 538 const ConnectionInfoCallback& callback, |
539 int16 rssi, | 539 int16 rssi, |
540 int16 transmit_power, | 540 int16 transmit_power, |
541 int16 max_transmit_power) { | 541 int16 max_transmit_power) { |
542 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); | 542 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); |
(...skipping 23 matching lines...) Expand all Loading... |
566 callback), | 566 callback), |
567 base::Bind(&BluetoothDeviceChromeOS::OnConnectError, | 567 base::Bind(&BluetoothDeviceChromeOS::OnConnectError, |
568 weak_ptr_factory_.GetWeakPtr(), | 568 weak_ptr_factory_.GetWeakPtr(), |
569 after_pairing, | 569 after_pairing, |
570 error_callback)); | 570 error_callback)); |
571 } | 571 } |
572 | 572 |
573 void BluetoothDeviceChromeOS::OnConnect(bool after_pairing, | 573 void BluetoothDeviceChromeOS::OnConnect(bool after_pairing, |
574 const base::Closure& callback) { | 574 const base::Closure& callback) { |
575 if (--num_connecting_calls_ == 0) | 575 if (--num_connecting_calls_ == 0) |
576 adapter_->NotifyDeviceChanged(this); | 576 adapter()->NotifyDeviceChanged(this); |
577 | 577 |
578 DCHECK(num_connecting_calls_ >= 0); | 578 DCHECK(num_connecting_calls_ >= 0); |
579 VLOG(1) << object_path_.value() << ": Connected, " << num_connecting_calls_ | 579 VLOG(1) << object_path_.value() << ": Connected, " << num_connecting_calls_ |
580 << " still in progress"; | 580 << " still in progress"; |
581 | 581 |
582 SetTrusted(); | 582 SetTrusted(); |
583 | 583 |
584 if (after_pairing) | 584 if (after_pairing) |
585 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult", | 585 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult", |
586 UMA_PAIRING_RESULT_SUCCESS, | 586 UMA_PAIRING_RESULT_SUCCESS, |
587 UMA_PAIRING_RESULT_COUNT); | 587 UMA_PAIRING_RESULT_COUNT); |
588 | 588 |
589 callback.Run(); | 589 callback.Run(); |
590 } | 590 } |
591 | 591 |
592 void BluetoothDeviceChromeOS::OnCreateGattConnection( | 592 void BluetoothDeviceChromeOS::OnCreateGattConnection( |
593 const GattConnectionCallback& callback) { | 593 const GattConnectionCallback& callback) { |
594 scoped_ptr<device::BluetoothGattConnection> conn( | 594 scoped_ptr<device::BluetoothGattConnection> conn( |
595 new BluetoothGattConnectionChromeOS( | 595 new BluetoothGattConnectionChromeOS( |
596 adapter_, GetAddress(), object_path_)); | 596 adapter_, GetAddress(), object_path_)); |
597 callback.Run(conn.Pass()); | 597 callback.Run(conn.Pass()); |
598 } | 598 } |
599 | 599 |
600 void BluetoothDeviceChromeOS::OnConnectError( | 600 void BluetoothDeviceChromeOS::OnConnectError( |
601 bool after_pairing, | 601 bool after_pairing, |
602 const ConnectErrorCallback& error_callback, | 602 const ConnectErrorCallback& error_callback, |
603 const std::string& error_name, | 603 const std::string& error_name, |
604 const std::string& error_message) { | 604 const std::string& error_message) { |
605 if (--num_connecting_calls_ == 0) | 605 if (--num_connecting_calls_ == 0) |
606 adapter_->NotifyDeviceChanged(this); | 606 adapter()->NotifyDeviceChanged(this); |
607 | 607 |
608 DCHECK(num_connecting_calls_ >= 0); | 608 DCHECK(num_connecting_calls_ >= 0); |
609 LOG(WARNING) << object_path_.value() << ": Failed to connect device: " | 609 LOG(WARNING) << object_path_.value() << ": Failed to connect device: " |
610 << error_name << ": " << error_message; | 610 << error_name << ": " << error_message; |
611 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ | 611 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ |
612 << " still in progress"; | 612 << " still in progress"; |
613 | 613 |
614 // Determine the error code from error_name. | 614 // Determine the error code from error_name. |
615 ConnectErrorCode error_code = ERROR_UNKNOWN; | 615 ConnectErrorCode error_code = ERROR_UNKNOWN; |
616 if (error_name == bluetooth_device::kErrorFailed) { | 616 if (error_name == bluetooth_device::kErrorFailed) { |
(...skipping 17 matching lines...) Expand all Loading... |
634 EndPairing(); | 634 EndPairing(); |
635 | 635 |
636 ConnectInternal(true, callback, error_callback); | 636 ConnectInternal(true, callback, error_callback); |
637 } | 637 } |
638 | 638 |
639 void BluetoothDeviceChromeOS::OnPairError( | 639 void BluetoothDeviceChromeOS::OnPairError( |
640 const ConnectErrorCallback& error_callback, | 640 const ConnectErrorCallback& error_callback, |
641 const std::string& error_name, | 641 const std::string& error_name, |
642 const std::string& error_message) { | 642 const std::string& error_message) { |
643 if (--num_connecting_calls_ == 0) | 643 if (--num_connecting_calls_ == 0) |
644 adapter_->NotifyDeviceChanged(this); | 644 adapter()->NotifyDeviceChanged(this); |
645 | 645 |
646 DCHECK(num_connecting_calls_ >= 0); | 646 DCHECK(num_connecting_calls_ >= 0); |
647 LOG(WARNING) << object_path_.value() << ": Failed to pair device: " | 647 LOG(WARNING) << object_path_.value() << ": Failed to pair device: " |
648 << error_name << ": " << error_message; | 648 << error_name << ": " << error_message; |
649 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ | 649 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ |
650 << " still in progress"; | 650 << " still in progress"; |
651 | 651 |
652 EndPairing(); | 652 EndPairing(); |
653 | 653 |
654 // Determine the error code from error_name. | 654 // Determine the error code from error_name. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 void BluetoothDeviceChromeOS::OnForgetError( | 712 void BluetoothDeviceChromeOS::OnForgetError( |
713 const ErrorCallback& error_callback, | 713 const ErrorCallback& error_callback, |
714 const std::string& error_name, | 714 const std::string& error_name, |
715 const std::string& error_message) { | 715 const std::string& error_message) { |
716 LOG(WARNING) << object_path_.value() << ": Failed to remove device: " | 716 LOG(WARNING) << object_path_.value() << ": Failed to remove device: " |
717 << error_name << ": " << error_message; | 717 << error_name << ": " << error_message; |
718 error_callback.Run(); | 718 error_callback.Run(); |
719 } | 719 } |
720 | 720 |
721 } // namespace chromeos | 721 } // namespace chromeos |
OLD | NEW |