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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 scoped_refptr<device::BluetoothSocketThread> socket_thread) | 149 scoped_refptr<device::BluetoothSocketThread> socket_thread) |
150 : BluetoothDevice(adapter), | 150 : BluetoothDevice(adapter), |
151 object_path_(object_path), | 151 object_path_(object_path), |
152 num_connecting_calls_(0), | 152 num_connecting_calls_(0), |
153 connection_monitor_started_(false), | 153 connection_monitor_started_(false), |
154 ui_task_runner_(ui_task_runner), | 154 ui_task_runner_(ui_task_runner), |
155 socket_thread_(socket_thread), | 155 socket_thread_(socket_thread), |
156 weak_ptr_factory_(this) { | 156 weak_ptr_factory_(this) { |
157 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver( | 157 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver( |
158 this); | 158 this); |
159 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); | |
160 | 159 |
161 InitializeGattServiceMap(); | 160 InitializeGattServicesMap(); |
162 } | 161 } |
163 | 162 |
164 BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() { | 163 BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() { |
165 bluez::BluezDBusManager::Get() | 164 bluez::BluezDBusManager::Get() |
166 ->GetBluetoothGattServiceClient() | 165 ->GetBluetoothGattServiceClient() |
167 ->RemoveObserver(this); | 166 ->RemoveObserver(this); |
168 | 167 |
169 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver( | |
170 this); | |
171 // 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 |
172 // send GattServiceRemoved(), GetGattServices() returns no services. | 169 // send GattServiceRemoved(), GetGattServices() returns no services. |
173 GattServiceMap gatt_services_swapped; | 170 GattServiceMap gatt_services_swapped; |
174 gatt_services_swapped.swap(gatt_services_); | 171 gatt_services_swapped.swap(gatt_services_); |
175 for (const auto& iter : gatt_services_swapped) { | 172 for (const auto& iter : gatt_services_swapped) { |
176 DCHECK(adapter()); | 173 DCHECK(adapter()); |
177 adapter()->NotifyGattServiceRemoved( | 174 adapter()->NotifyGattServiceRemoved( |
178 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second)); | 175 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second)); |
179 } | 176 } |
180 } | 177 } |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 } | 568 } |
572 | 569 |
573 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const { | 570 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const { |
574 return pairing_.get(); | 571 return pairing_.get(); |
575 } | 572 } |
576 | 573 |
577 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const { | 574 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const { |
578 return static_cast<BluetoothAdapterBlueZ*>(adapter_); | 575 return static_cast<BluetoothAdapterBlueZ*>(adapter_); |
579 } | 576 } |
580 | 577 |
581 void BluetoothDeviceBlueZ::DevicePropertyChanged( | |
582 const dbus::ObjectPath& object_path, | |
583 const std::string& property_name) { | |
584 if (object_path != object_path_) | |
585 return; | |
586 | |
587 bluez::BluetoothDeviceClient::Properties* properties = | |
588 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
589 object_path); | |
590 DCHECK(properties); | |
591 | |
592 if (property_name == properties->services_resolved.name() && | |
593 properties->services_resolved.value()) { | |
594 VLOG(3) << "All services were discovered for device: " | |
595 << object_path.value(); | |
596 | |
597 for (const auto iter : newly_discovered_gatt_services_) { | |
598 adapter()->NotifyGattDiscoveryComplete( | |
599 static_cast<BluetoothRemoteGattService*>(iter)); | |
600 } | |
601 newly_discovered_gatt_services_.clear(); | |
602 } | |
603 } | |
604 | |
605 void BluetoothDeviceBlueZ::GattServiceAdded( | 578 void BluetoothDeviceBlueZ::GattServiceAdded( |
606 const dbus::ObjectPath& object_path) { | 579 const dbus::ObjectPath& object_path) { |
607 if (GetGattService(object_path.value())) { | 580 if (GetGattService(object_path.value())) { |
608 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); | 581 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); |
609 return; | 582 return; |
610 } | 583 } |
611 | 584 |
612 bluez::BluetoothGattServiceClient::Properties* properties = | 585 bluez::BluetoothGattServiceClient::Properties* properties = |
613 bluez::BluezDBusManager::Get() | 586 bluez::BluezDBusManager::Get() |
614 ->GetBluetoothGattServiceClient() | 587 ->GetBluetoothGattServiceClient() |
615 ->GetProperties(object_path); | 588 ->GetProperties(object_path); |
616 DCHECK(properties); | 589 DCHECK(properties); |
617 if (properties->device.value() != object_path_) { | 590 if (properties->device.value() != object_path_) { |
618 VLOG(2) << "Remote GATT service does not belong to this device."; | 591 VLOG(2) << "Remote GATT service does not belong to this device."; |
619 return; | 592 return; |
620 } | 593 } |
621 | 594 |
622 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); | 595 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); |
623 | 596 |
624 BluetoothRemoteGattServiceBlueZ* service = | 597 BluetoothRemoteGattServiceBlueZ* service = |
625 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path); | 598 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path); |
626 | 599 |
627 newly_discovered_gatt_services_.push_back( | |
628 static_cast<BluetoothRemoteGattServiceBlueZ*>(service)); | |
629 | |
630 gatt_services_.set(service->GetIdentifier(), | 600 gatt_services_.set(service->GetIdentifier(), |
631 std::unique_ptr<BluetoothRemoteGattService>(service)); | 601 std::unique_ptr<BluetoothRemoteGattService>(service)); |
632 DCHECK(service->object_path() == object_path); | 602 DCHECK(service->object_path() == object_path); |
633 DCHECK(service->GetUUID().IsValid()); | 603 DCHECK(service->GetUUID().IsValid()); |
634 | 604 |
635 DCHECK(adapter()); | 605 DCHECK(adapter()); |
636 adapter()->NotifyGattServiceAdded(service); | 606 adapter()->NotifyGattServiceAdded(service); |
637 } | 607 } |
638 | 608 |
639 void BluetoothDeviceBlueZ::GattServiceRemoved( | 609 void BluetoothDeviceBlueZ::GattServiceRemoved( |
(...skipping 10 matching lines...) Expand all Loading... |
650 | 620 |
651 VLOG(1) << "Removing remote GATT service with UUID: '" | 621 VLOG(1) << "Removing remote GATT service with UUID: '" |
652 << service->GetUUID().canonical_value() | 622 << service->GetUUID().canonical_value() |
653 << "' from device: " << GetAddress(); | 623 << "' from device: " << GetAddress(); |
654 | 624 |
655 DCHECK(service->object_path() == object_path); | 625 DCHECK(service->object_path() == object_path); |
656 std::unique_ptr<BluetoothRemoteGattService> scoped_service = | 626 std::unique_ptr<BluetoothRemoteGattService> scoped_service = |
657 gatt_services_.take_and_erase(iter->first); | 627 gatt_services_.take_and_erase(iter->first); |
658 | 628 |
659 DCHECK(adapter()); | 629 DCHECK(adapter()); |
| 630 discovery_complete_notified_.erase(service); |
660 adapter()->NotifyGattServiceRemoved(service); | 631 adapter()->NotifyGattServiceRemoved(service); |
661 } | 632 } |
662 | 633 |
663 void BluetoothDeviceBlueZ::InitializeGattServiceMap() { | 634 void BluetoothDeviceBlueZ::InitializeGattServicesMap() { |
664 DCHECK(gatt_services_.empty()); | 635 DCHECK(gatt_services_.empty()); |
665 | 636 |
666 if (!IsGattServicesDiscoveryComplete()) { | 637 if (!IsGattServicesDiscoveryComplete()) { |
667 VLOG(2) << "Gatt services have not been fully resolved for device " | 638 VLOG(2) << "Gatt services have not been fully resolved for device " |
668 << object_path_.value(); | 639 << object_path_.value(); |
669 return; | 640 return; |
670 } | 641 } |
671 | 642 |
672 VLOG(3) << "Initializing the list of GATT services associated with device " | 643 VLOG(3) << "Initializing the list of GATT services associated with device " |
673 << object_path_.value(); | 644 << object_path_.value(); |
674 | 645 |
675 // Add all known GATT services associated with the device. | 646 // Add all known GATT services associated with the device. |
| 647 const std::vector<dbus::ObjectPath> services_paths = |
| 648 bluez::BluezDBusManager::Get() |
| 649 ->GetBluetoothGattServiceClient() |
| 650 ->GetServices(); |
| 651 |
| 652 for (const auto& service_path : services_paths) { |
| 653 GattServiceAdded(service_path); |
| 654 |
| 655 // Notify on the discovery complete for each service which is found in the |
| 656 // first discovery. |
| 657 BluetoothRemoteGattService* service = GetGattService(service_path.value()); |
| 658 DCHECK(service); |
| 659 auto notified_pair = discovery_complete_notified_.insert(service); |
| 660 DCHECK(notified_pair.second); |
| 661 adapter()->NotifyGattDiscoveryComplete(service); |
| 662 } |
| 663 } |
| 664 |
| 665 void BluetoothDeviceBlueZ::UpdateGattServices( |
| 666 const dbus::ObjectPath& object_path) { |
| 667 if (object_path != object_path_) { |
| 668 // No need to update map if update is for a different device. |
| 669 return; |
| 670 } |
| 671 |
| 672 DCHECK(IsGattServicesDiscoveryComplete()); |
| 673 |
676 const std::vector<dbus::ObjectPath> gatt_services = | 674 const std::vector<dbus::ObjectPath> gatt_services = |
677 bluez::BluezDBusManager::Get() | 675 bluez::BluezDBusManager::Get() |
678 ->GetBluetoothGattServiceClient() | 676 ->GetBluetoothGattServiceClient() |
679 ->GetServices(); | 677 ->GetServices(); |
680 for (const auto& it : gatt_services) | 678 for (const auto& service_object_path : gatt_services) { |
681 GattServiceAdded(it); | 679 // Add all previously unknown GATT services associated with the device. |
| 680 BluetoothRemoteGattService* service = |
| 681 GetGattService(service_object_path.value()); |
| 682 if (service == nullptr) { |
| 683 GattServiceAdded(service_object_path); |
| 684 } |
682 | 685 |
683 // Notify on the discovery complete for each service which is found in the | 686 // Notify of GATT discovery complete if we haven't before. |
684 // first discovery. | 687 service = GetGattService(service_object_path.value()); |
685 DCHECK(adapter()); | 688 auto notified_pair = discovery_complete_notified_.insert(service); |
686 for (const auto& iter : gatt_services_) | 689 if (notified_pair.second) { |
687 adapter()->NotifyGattDiscoveryComplete(iter.second); | 690 adapter()->NotifyGattDiscoveryComplete(service); |
| 691 } |
| 692 } |
688 } | 693 } |
689 | 694 |
690 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, | 695 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, |
691 int16_t rssi, | 696 int16_t rssi, |
692 int16_t transmit_power, | 697 int16_t transmit_power, |
693 int16_t max_transmit_power) { | 698 int16_t max_transmit_power) { |
694 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); | 699 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); |
695 } | 700 } |
696 | 701 |
697 void BluetoothDeviceBlueZ::OnGetConnInfoError( | 702 void BluetoothDeviceBlueZ::OnGetConnInfoError( |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
869 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, | 874 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, |
870 const std::string& error_name, | 875 const std::string& error_name, |
871 const std::string& error_message) { | 876 const std::string& error_message) { |
872 LOG(WARNING) << object_path_.value() | 877 LOG(WARNING) << object_path_.value() |
873 << ": Failed to remove device: " << error_name << ": " | 878 << ": Failed to remove device: " << error_name << ": " |
874 << error_message; | 879 << error_message; |
875 error_callback.Run(); | 880 error_callback.Run(); |
876 } | 881 } |
877 | 882 |
878 } // namespace bluez | 883 } // namespace bluez |
OLD | NEW |