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::UpdateGattServices( |
635 const dbus::ObjectPath& object_path) { | |
636 if (object_path != object_path_) { | |
637 // No need to update map if update is for a different device. | |
638 return; | |
639 } | |
640 | |
641 DCHECK(IsGattServicesDiscoveryComplete()); | |
642 | |
643 const std::vector<dbus::ObjectPath> gatt_services = | |
644 bluez::BluezDBusManager::Get() | |
645 ->GetBluetoothGattServiceClient() | |
646 ->GetServices(); | |
647 for (const auto& service_object_path : gatt_services) { | |
648 // Add all previously unknown GATT services associated with the device. | |
649 BluetoothRemoteGattService* service = | |
650 GetGattService(service_object_path.value()); | |
651 if (service == nullptr) { | |
652 GattServiceAdded(service_object_path); | |
653 } | |
654 | |
655 // Notify of GATT discovery complete if we haven't before. | |
656 service = GetGattService(service_object_path.value()); | |
657 auto notified_pair = discovery_complete_notified_.insert(service); | |
658 if (notified_pair.second) { | |
659 adapter()->NotifyGattDiscoveryComplete(service); | |
660 } | |
661 } | |
662 } | |
663 | |
664 void BluetoothDeviceBlueZ::InitializeGattServicesMap() { | |
664 DCHECK(gatt_services_.empty()); | 665 DCHECK(gatt_services_.empty()); |
665 | 666 |
666 if (!IsGattServicesDiscoveryComplete()) { | 667 if (!IsGattServicesDiscoveryComplete()) { |
vudentz
2016/07/01 11:16:08
InitializeGattServicesMap is only called by the co
ortuno
2016/07/01 15:51:48
What if a device is connected and all it's attribu
| |
667 VLOG(2) << "Gatt services have not been fully resolved for device " | 668 VLOG(2) << "Gatt services have not been fully resolved for device " |
668 << object_path_.value(); | 669 << object_path_.value(); |
669 return; | 670 return; |
670 } | 671 } |
671 | 672 |
672 VLOG(3) << "Initializing the list of GATT services associated with device " | 673 VLOG(3) << "Initializing the list of GATT services associated with device " |
673 << object_path_.value(); | 674 << object_path_.value(); |
674 | 675 |
675 // Add all known GATT services associated with the device. | 676 // Add all known GATT services associated with the device. |
676 const std::vector<dbus::ObjectPath> gatt_services = | 677 const std::vector<dbus::ObjectPath> gatt_services = |
677 bluez::BluezDBusManager::Get() | 678 bluez::BluezDBusManager::Get() |
678 ->GetBluetoothGattServiceClient() | 679 ->GetBluetoothGattServiceClient() |
679 ->GetServices(); | 680 ->GetServices(); |
680 for (const auto& it : gatt_services) | 681 for (const auto& it : gatt_services) |
681 GattServiceAdded(it); | 682 GattServiceAdded(it); |
682 | 683 |
683 // Notify on the discovery complete for each service which is found in the | 684 // Notify on the discovery complete for each service which is found in the |
684 // first discovery. | 685 // first discovery. |
685 DCHECK(adapter()); | 686 DCHECK(adapter()); |
686 for (const auto& iter : gatt_services_) | 687 for (const auto& iter : gatt_services_) { |
vudentz
2016/07/01 11:16:08
What is the purpose of iterating to gatt_services
ortuno
2016/07/01 15:51:48
I combined the two for loops.
| |
688 auto notified_pair = discovery_complete_notified_.insert(iter.second); | |
689 DCHECK(notified_pair.second); | |
687 adapter()->NotifyGattDiscoveryComplete(iter.second); | 690 adapter()->NotifyGattDiscoveryComplete(iter.second); |
691 } | |
688 } | 692 } |
689 | 693 |
690 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, | 694 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, |
691 int16_t rssi, | 695 int16_t rssi, |
692 int16_t transmit_power, | 696 int16_t transmit_power, |
693 int16_t max_transmit_power) { | 697 int16_t max_transmit_power) { |
694 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); | 698 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); |
695 } | 699 } |
696 | 700 |
697 void BluetoothDeviceBlueZ::OnGetConnInfoError( | 701 void BluetoothDeviceBlueZ::OnGetConnInfoError( |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
869 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, | 873 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, |
870 const std::string& error_name, | 874 const std::string& error_name, |
871 const std::string& error_message) { | 875 const std::string& error_message) { |
872 LOG(WARNING) << object_path_.value() | 876 LOG(WARNING) << object_path_.value() |
873 << ": Failed to remove device: " << error_name << ": " | 877 << ": Failed to remove device: " << error_name << ": " |
874 << error_message; | 878 << error_message; |
875 error_callback.Run(); | 879 error_callback.Run(); |
876 } | 880 } |
877 | 881 |
878 } // namespace bluez | 882 } // namespace bluez |
OLD | NEW |