Chromium Code Reviews| 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 |
| 175 // Copy the GATT services list here and clear the original so that when we | 168 // Copy the GATT services list here and clear the original so that when we |
| 176 // send GattServiceRemoved(), GetGattServices() returns no services. | 169 // send GattServiceRemoved(), GetGattServices() returns no services. |
| 177 GattServiceMap gatt_services_swapped; | 170 GattServiceMap gatt_services_swapped; |
| 178 gatt_services_swapped.swap(gatt_services_); | 171 gatt_services_swapped.swap(gatt_services_); |
| 179 for (const auto& iter : gatt_services_swapped) { | 172 for (const auto& iter : gatt_services_swapped) { |
| 180 DCHECK(adapter_); | 173 DCHECK(adapter_); |
| 181 adapter()->NotifyGattServiceRemoved( | 174 adapter_->NotifyGattServiceRemoved( |
| 182 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second)); | 175 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second)); |
| 183 } | 176 } |
| 184 } | 177 } |
| 185 | 178 |
| 186 uint32_t BluetoothDeviceBlueZ::GetBluetoothClass() const { | 179 uint32_t BluetoothDeviceBlueZ::GetBluetoothClass() const { |
| 187 bluez::BluetoothDeviceClient::Properties* properties = | 180 bluez::BluetoothDeviceClient::Properties* properties = |
| 188 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | 181 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( |
| 189 object_path_); | 182 object_path_); |
| 190 DCHECK(properties); | 183 DCHECK(properties); |
| 191 | 184 |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 546 } | 539 } |
| 547 | 540 |
| 548 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const { | 541 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const { |
| 549 return pairing_.get(); | 542 return pairing_.get(); |
| 550 } | 543 } |
| 551 | 544 |
| 552 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const { | 545 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const { |
| 553 return static_cast<BluetoothAdapterBlueZ*>(adapter_); | 546 return static_cast<BluetoothAdapterBlueZ*>(adapter_); |
| 554 } | 547 } |
| 555 | 548 |
| 549 void BluetoothDeviceBlueZ::DevicePropertyChanged( | |
| 550 const dbus::ObjectPath& object_path, | |
| 551 const std::string& property_name) { | |
| 552 if (object_path != object_path_) | |
| 553 return; | |
| 554 | |
| 555 bluez::BluetoothDeviceClient::Properties* properties = | |
| 556 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
| 557 object_path); | |
| 558 DCHECK(properties); | |
| 559 | |
| 560 if (property_name == properties->services_resolved.name() && | |
| 561 properties->services_resolved.value()) { | |
| 562 VLOG(3) << "All services were discovered for device: " | |
| 563 << object_path.value(); | |
| 564 | |
| 565 for (const auto iter : newly_discovered_gatt_services_) { | |
| 566 adapter()->NotifyGattDiscoveryComplete( | |
| 567 static_cast<BluetoothRemoteGattService*>(iter)); | |
| 568 } | |
| 569 newly_discovered_gatt_services_.clear(); | |
| 570 } | |
| 571 } | |
| 572 | |
| 556 void BluetoothDeviceBlueZ::GattServiceAdded( | 573 void BluetoothDeviceBlueZ::GattServiceAdded( |
| 557 const dbus::ObjectPath& object_path) { | 574 const dbus::ObjectPath& object_path) { |
| 558 if (GetGattService(object_path.value())) { | 575 if (GetGattService(object_path.value())) { |
| 559 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); | 576 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); |
| 560 return; | 577 return; |
| 561 } | 578 } |
| 562 | 579 |
| 563 bluez::BluetoothGattServiceClient::Properties* properties = | 580 bluez::BluetoothGattServiceClient::Properties* properties = |
| 564 bluez::BluezDBusManager::Get() | 581 bluez::BluezDBusManager::Get() |
| 565 ->GetBluetoothGattServiceClient() | 582 ->GetBluetoothGattServiceClient() |
| 566 ->GetProperties(object_path); | 583 ->GetProperties(object_path); |
| 567 DCHECK(properties); | 584 DCHECK(properties); |
| 568 if (properties->device.value() != object_path_) { | 585 if (properties->device.value() != object_path_) { |
| 569 VLOG(2) << "Remote GATT service does not belong to this device."; | 586 VLOG(2) << "Remote GATT service does not belong to this device."; |
| 570 return; | 587 return; |
| 571 } | 588 } |
| 572 | 589 |
| 573 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); | 590 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); |
| 574 | 591 |
| 575 BluetoothRemoteGattServiceBlueZ* service = | 592 BluetoothRemoteGattServiceBlueZ* service = |
| 576 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path); | 593 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path); |
| 577 | 594 |
| 595 newly_discovered_gatt_services_.push_back( | |
| 596 static_cast<BluetoothRemoteGattServiceBlueZ*>(service)); | |
| 597 | |
| 578 gatt_services_.set(service->GetIdentifier(), | 598 gatt_services_.set(service->GetIdentifier(), |
| 579 std::unique_ptr<BluetoothRemoteGattService>(service)); | 599 std::unique_ptr<BluetoothRemoteGattService>(service)); |
| 580 DCHECK(service->object_path() == object_path); | 600 DCHECK(service->object_path() == object_path); |
| 581 DCHECK(service->GetUUID().IsValid()); | 601 DCHECK(service->GetUUID().IsValid()); |
| 582 | 602 |
| 583 DCHECK(adapter_); | 603 DCHECK(adapter()); |
|
rkc
2016/05/16 16:25:22
Nit: Use either adapter_ or adapter() at all place
Miao
2016/05/17 00:22:46
Done.
| |
| 584 adapter()->NotifyGattServiceAdded(service); | 604 adapter()->NotifyGattServiceAdded(service); |
| 585 } | 605 } |
| 586 | 606 |
| 587 void BluetoothDeviceBlueZ::GattServiceRemoved( | 607 void BluetoothDeviceBlueZ::GattServiceRemoved( |
| 588 const dbus::ObjectPath& object_path) { | 608 const dbus::ObjectPath& object_path) { |
| 589 GattServiceMap::const_iterator iter = | 609 GattServiceMap::const_iterator iter = |
| 590 gatt_services_.find(object_path.value()); | 610 gatt_services_.find(object_path.value()); |
| 591 if (iter == gatt_services_.end()) { | 611 if (iter == gatt_services_.end()) { |
| 592 VLOG(3) << "Unknown GATT service removed: " << object_path.value(); | 612 VLOG(3) << "Unknown GATT service removed: " << object_path.value(); |
| 593 return; | 613 return; |
| 594 } | 614 } |
| 595 | 615 |
| 596 BluetoothRemoteGattServiceBlueZ* service = | 616 BluetoothRemoteGattServiceBlueZ* service = |
| 597 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter->second); | 617 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter->second); |
| 598 | 618 |
| 599 VLOG(1) << "Removing remote GATT service with UUID: '" | 619 VLOG(1) << "Removing remote GATT service with UUID: '" |
| 600 << service->GetUUID().canonical_value() | 620 << service->GetUUID().canonical_value() |
| 601 << "' from device: " << GetAddress(); | 621 << "' from device: " << GetAddress(); |
| 602 | 622 |
| 603 DCHECK(service->object_path() == object_path); | 623 DCHECK(service->object_path() == object_path); |
| 604 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 624 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
| 605 gatt_services_.take_and_erase(iter->first); | 625 gatt_services_.take_and_erase(iter->first); |
| 606 | 626 |
| 607 DCHECK(adapter_); | 627 DCHECK(adapter_); |
| 608 adapter()->NotifyGattServiceRemoved(service); | 628 adapter()->NotifyGattServiceRemoved(service); |
| 609 } | 629 } |
| 610 | 630 |
| 631 void BluetoothDeviceBlueZ::InitializeGattServiceMap() { | |
| 632 if (!gatt_services_.empty()) { | |
|
scheib
2016/05/16 20:22:26
If InitializeGattServiceMap is only called from co
Miao
2016/05/17 18:08:27
Done. Exactly. This function addresses the case wh
| |
| 633 VLOG(2) << "List of Gatt services has already been initialized for device" | |
| 634 << object_path_.value(); | |
| 635 return; | |
| 636 } | |
| 637 | |
| 638 if (!IsGattServicesDiscoveryComplete()) { | |
| 639 VLOG(2) << "Gatt services have not been fully resolved for device " | |
| 640 << object_path_.value(); | |
| 641 return; | |
| 642 } | |
| 643 | |
| 644 VLOG(1) << "Initialize the list of GATT services associated with device " | |
|
rkc
2016/05/16 16:25:22
Normal operation, use VLOG(3).
Also,
nit: s/Initia
Miao
2016/05/17 00:22:46
Done.
| |
| 645 << object_path_.value(); | |
| 646 | |
| 647 // Add all known GATT services associated with the device. | |
| 648 const std::vector<dbus::ObjectPath> gatt_services = | |
| 649 bluez::BluezDBusManager::Get() | |
| 650 ->GetBluetoothGattServiceClient() | |
| 651 ->GetServices(); | |
| 652 for (const auto& it : gatt_services) | |
| 653 GattServiceAdded(it); | |
| 654 | |
| 655 // Notify on the discovery complete for each service which is found in the | |
| 656 // first discovery. | |
| 657 DCHECK(adapter()); | |
| 658 for (const auto& iter : gatt_services_) | |
| 659 adapter()->NotifyGattDiscoveryComplete(iter.second); | |
| 660 } | |
| 661 | |
| 611 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, | 662 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, |
| 612 int16_t rssi, | 663 int16_t rssi, |
| 613 int16_t transmit_power, | 664 int16_t transmit_power, |
| 614 int16_t max_transmit_power) { | 665 int16_t max_transmit_power) { |
| 615 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); | 666 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); |
| 616 } | 667 } |
| 617 | 668 |
| 618 void BluetoothDeviceBlueZ::OnGetConnInfoError( | 669 void BluetoothDeviceBlueZ::OnGetConnInfoError( |
| 619 const ConnectionInfoCallback& callback, | 670 const ConnectionInfoCallback& callback, |
| 620 const std::string& error_name, | 671 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, | 841 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, |
| 791 const std::string& error_name, | 842 const std::string& error_name, |
| 792 const std::string& error_message) { | 843 const std::string& error_message) { |
| 793 LOG(WARNING) << object_path_.value() | 844 LOG(WARNING) << object_path_.value() |
| 794 << ": Failed to remove device: " << error_name << ": " | 845 << ": Failed to remove device: " << error_name << ": " |
| 795 << error_message; | 846 << error_message; |
| 796 error_callback.Run(); | 847 error_callback.Run(); |
| 797 } | 848 } |
| 798 | 849 |
| 799 } // namespace bluez | 850 } // namespace bluez |
| OLD | NEW |