| 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/bluez/bluetooth_device_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_device_bluez.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 scoped_refptr<device::BluetoothSocketThread> socket_thread) | 148 scoped_refptr<device::BluetoothSocketThread> socket_thread) |
| 149 : BluetoothDevice(adapter), | 149 : BluetoothDevice(adapter), |
| 150 object_path_(object_path), | 150 object_path_(object_path), |
| 151 num_connecting_calls_(0), | 151 num_connecting_calls_(0), |
| 152 connection_monitor_started_(false), | 152 connection_monitor_started_(false), |
| 153 ui_task_runner_(ui_task_runner), | 153 ui_task_runner_(ui_task_runner), |
| 154 socket_thread_(socket_thread), | 154 socket_thread_(socket_thread), |
| 155 weak_ptr_factory_(this) { | 155 weak_ptr_factory_(this) { |
| 156 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver( | 156 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver( |
| 157 this); | 157 this); |
| 158 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); |
| 158 | 159 |
| 159 // Add all known GATT services. | 160 InitializeGattServiceMap(); |
| 160 const std::vector<dbus::ObjectPath> gatt_services = | |
| 161 bluez::BluezDBusManager::Get() | |
| 162 ->GetBluetoothGattServiceClient() | |
| 163 ->GetServices(); | |
| 164 for (std::vector<dbus::ObjectPath>::const_iterator it = gatt_services.begin(); | |
| 165 it != gatt_services.end(); ++it) { | |
| 166 GattServiceAdded(*it); | |
| 167 } | |
| 168 } | 161 } |
| 169 | 162 |
| 170 BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() { | 163 BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() { |
| 171 bluez::BluezDBusManager::Get() | 164 bluez::BluezDBusManager::Get() |
| 172 ->GetBluetoothGattServiceClient() | 165 ->GetBluetoothGattServiceClient() |
| 173 ->RemoveObserver(this); | 166 ->RemoveObserver(this); |
| 174 | 167 |
| 168 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver( |
| 169 this); |
| 175 // Copy the GATT services list here and clear the original so that when we | 170 // Copy the GATT services list here and clear the original so that when we |
| 176 // send GattServiceRemoved(), GetGattServices() returns no services. | 171 // send GattServiceRemoved(), GetGattServices() returns no services. |
| 177 GattServiceMap gatt_services_swapped; | 172 GattServiceMap gatt_services_swapped; |
| 178 gatt_services_swapped.swap(gatt_services_); | 173 gatt_services_swapped.swap(gatt_services_); |
| 179 for (const auto& iter : gatt_services_swapped) { | 174 for (const auto& iter : gatt_services_swapped) { |
| 180 DCHECK(adapter_); | 175 DCHECK(adapter()); |
| 181 adapter()->NotifyGattServiceRemoved( | 176 adapter()->NotifyGattServiceRemoved( |
| 182 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second)); | 177 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second)); |
| 183 } | 178 } |
| 184 } | 179 } |
| 185 | 180 |
| 186 uint32_t BluetoothDeviceBlueZ::GetBluetoothClass() const { | 181 uint32_t BluetoothDeviceBlueZ::GetBluetoothClass() const { |
| 187 bluez::BluetoothDeviceClient::Properties* properties = | 182 bluez::BluetoothDeviceClient::Properties* properties = |
| 188 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 183 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
| 189 object_path_); | 184 object_path_); |
| 190 DCHECK(properties); | 185 DCHECK(properties); |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 } | 541 } |
| 547 | 542 |
| 548 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const { | 543 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const { |
| 549 return pairing_.get(); | 544 return pairing_.get(); |
| 550 } | 545 } |
| 551 | 546 |
| 552 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const { | 547 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const { |
| 553 return static_cast<BluetoothAdapterBlueZ*>(adapter_); | 548 return static_cast<BluetoothAdapterBlueZ*>(adapter_); |
| 554 } | 549 } |
| 555 | 550 |
| 551 void BluetoothDeviceBlueZ::DevicePropertyChanged( |
| 552 const dbus::ObjectPath& object_path, |
| 553 const std::string& property_name) { |
| 554 if (object_path != object_path_) |
| 555 return; |
| 556 |
| 557 bluez::BluetoothDeviceClient::Properties* properties = |
| 558 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
| 559 object_path); |
| 560 DCHECK(properties); |
| 561 |
| 562 if (property_name == properties->services_resolved.name() && |
| 563 properties->services_resolved.value()) { |
| 564 VLOG(3) << "All services were discovered for device: " |
| 565 << object_path.value(); |
| 566 |
| 567 for (const auto iter : newly_discovered_gatt_services_) { |
| 568 adapter()->NotifyGattDiscoveryComplete( |
| 569 static_cast<BluetoothRemoteGattService*>(iter)); |
| 570 } |
| 571 newly_discovered_gatt_services_.clear(); |
| 572 } |
| 573 } |
| 574 |
| 556 void BluetoothDeviceBlueZ::GattServiceAdded( | 575 void BluetoothDeviceBlueZ::GattServiceAdded( |
| 557 const dbus::ObjectPath& object_path) { | 576 const dbus::ObjectPath& object_path) { |
| 558 if (GetGattService(object_path.value())) { | 577 if (GetGattService(object_path.value())) { |
| 559 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); | 578 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); |
| 560 return; | 579 return; |
| 561 } | 580 } |
| 562 | 581 |
| 563 bluez::BluetoothGattServiceClient::Properties* properties = | 582 bluez::BluetoothGattServiceClient::Properties* properties = |
| 564 bluez::BluezDBusManager::Get() | 583 bluez::BluezDBusManager::Get() |
| 565 ->GetBluetoothGattServiceClient() | 584 ->GetBluetoothGattServiceClient() |
| 566 ->GetProperties(object_path); | 585 ->GetProperties(object_path); |
| 567 DCHECK(properties); | 586 DCHECK(properties); |
| 568 if (properties->device.value() != object_path_) { | 587 if (properties->device.value() != object_path_) { |
| 569 VLOG(2) << "Remote GATT service does not belong to this device."; | 588 VLOG(2) << "Remote GATT service does not belong to this device."; |
| 570 return; | 589 return; |
| 571 } | 590 } |
| 572 | 591 |
| 573 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); | 592 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); |
| 574 | 593 |
| 575 BluetoothRemoteGattServiceBlueZ* service = | 594 BluetoothRemoteGattServiceBlueZ* service = |
| 576 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path); | 595 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path); |
| 577 | 596 |
| 597 newly_discovered_gatt_services_.push_back( |
| 598 static_cast<BluetoothRemoteGattServiceBlueZ*>(service)); |
| 599 |
| 578 gatt_services_.set(service->GetIdentifier(), | 600 gatt_services_.set(service->GetIdentifier(), |
| 579 std::unique_ptr<BluetoothRemoteGattService>(service)); | 601 std::unique_ptr<BluetoothRemoteGattService>(service)); |
| 580 DCHECK(service->object_path() == object_path); | 602 DCHECK(service->object_path() == object_path); |
| 581 DCHECK(service->GetUUID().IsValid()); | 603 DCHECK(service->GetUUID().IsValid()); |
| 582 | 604 |
| 583 DCHECK(adapter_); | 605 DCHECK(adapter()); |
| 584 adapter()->NotifyGattServiceAdded(service); | 606 adapter()->NotifyGattServiceAdded(service); |
| 585 } | 607 } |
| 586 | 608 |
| 587 void BluetoothDeviceBlueZ::GattServiceRemoved( | 609 void BluetoothDeviceBlueZ::GattServiceRemoved( |
| 588 const dbus::ObjectPath& object_path) { | 610 const dbus::ObjectPath& object_path) { |
| 589 GattServiceMap::const_iterator iter = | 611 GattServiceMap::const_iterator iter = |
| 590 gatt_services_.find(object_path.value()); | 612 gatt_services_.find(object_path.value()); |
| 591 if (iter == gatt_services_.end()) { | 613 if (iter == gatt_services_.end()) { |
| 592 VLOG(3) << "Unknown GATT service removed: " << object_path.value(); | 614 VLOG(3) << "Unknown GATT service removed: " << object_path.value(); |
| 593 return; | 615 return; |
| 594 } | 616 } |
| 595 | 617 |
| 596 BluetoothRemoteGattServiceBlueZ* service = | 618 BluetoothRemoteGattServiceBlueZ* service = |
| 597 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter->second); | 619 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter->second); |
| 598 | 620 |
| 599 VLOG(1) << "Removing remote GATT service with UUID: '" | 621 VLOG(1) << "Removing remote GATT service with UUID: '" |
| 600 << service->GetUUID().canonical_value() | 622 << service->GetUUID().canonical_value() |
| 601 << "' from device: " << GetAddress(); | 623 << "' from device: " << GetAddress(); |
| 602 | 624 |
| 603 DCHECK(service->object_path() == object_path); | 625 DCHECK(service->object_path() == object_path); |
| 604 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 626 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
| 605 gatt_services_.take_and_erase(iter->first); | 627 gatt_services_.take_and_erase(iter->first); |
| 606 | 628 |
| 607 DCHECK(adapter_); | 629 DCHECK(adapter()); |
| 608 adapter()->NotifyGattServiceRemoved(service); | 630 adapter()->NotifyGattServiceRemoved(service); |
| 609 } | 631 } |
| 610 | 632 |
| 633 void BluetoothDeviceBlueZ::InitializeGattServiceMap() { |
| 634 DCHECK(gatt_services_.empty()); |
| 635 |
| 636 if (!IsGattServicesDiscoveryComplete()) { |
| 637 VLOG(2) << "Gatt services have not been fully resolved for device " |
| 638 << object_path_.value(); |
| 639 return; |
| 640 } |
| 641 |
| 642 VLOG(3) << "Initializing the list of GATT services associated with device " |
| 643 << object_path_.value(); |
| 644 |
| 645 // Add all known GATT services associated with the device. |
| 646 const std::vector<dbus::ObjectPath> gatt_services = |
| 647 bluez::BluezDBusManager::Get() |
| 648 ->GetBluetoothGattServiceClient() |
| 649 ->GetServices(); |
| 650 for (const auto& it : gatt_services) |
| 651 GattServiceAdded(it); |
| 652 |
| 653 // Notify on the discovery complete for each service which is found in the |
| 654 // first discovery. |
| 655 DCHECK(adapter()); |
| 656 for (const auto& iter : gatt_services_) |
| 657 adapter()->NotifyGattDiscoveryComplete(iter.second); |
| 658 } |
| 659 |
| 611 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, | 660 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, |
| 612 int16_t rssi, | 661 int16_t rssi, |
| 613 int16_t transmit_power, | 662 int16_t transmit_power, |
| 614 int16_t max_transmit_power) { | 663 int16_t max_transmit_power) { |
| 615 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); | 664 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); |
| 616 } | 665 } |
| 617 | 666 |
| 618 void BluetoothDeviceBlueZ::OnGetConnInfoError( | 667 void BluetoothDeviceBlueZ::OnGetConnInfoError( |
| 619 const ConnectionInfoCallback& callback, | 668 const ConnectionInfoCallback& callback, |
| 620 const std::string& error_name, | 669 const std::string& error_name, |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, | 839 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, |
| 791 const std::string& error_name, | 840 const std::string& error_name, |
| 792 const std::string& error_message) { | 841 const std::string& error_message) { |
| 793 LOG(WARNING) << object_path_.value() | 842 LOG(WARNING) << object_path_.value() |
| 794 << ": Failed to remove device: " << error_name << ": " | 843 << ": Failed to remove device: " << error_name << ": " |
| 795 << error_message; | 844 << error_message; |
| 796 error_callback.Run(); | 845 error_callback.Run(); |
| 797 } | 846 } |
| 798 | 847 |
| 799 } // namespace bluez | 848 } // namespace bluez |
| OLD | NEW |