Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(400)

Side by Side Diff: device/bluetooth/bluez/bluetooth_device_bluez.cc

Issue 2105423003: bluetooth: Update the map of GATT services when services resolve (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@my-origin
Patch Set: Address luiz's comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // If GATT Services have already been discovered update the list of Gatt
161 // Services.
162 if (IsGattServicesDiscoveryComplete()) {
ortuno 2016/07/06 21:31:46 luiz: You mentioned that this will always be false
vudentz 2016/07/07 09:45:08 Yep, I was assuming this object to be responsible
163 UpdateGattServices(object_path_);
164 } else {
165 VLOG(2) << "Gatt services have not been fully resolved for device "
166 << object_path_.value();
167 }
162 } 168 }
163 169
164 BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() { 170 BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() {
165 bluez::BluezDBusManager::Get() 171 bluez::BluezDBusManager::Get()
166 ->GetBluetoothGattServiceClient() 172 ->GetBluetoothGattServiceClient()
167 ->RemoveObserver(this); 173 ->RemoveObserver(this);
168 174
169 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(
170 this);
171 // Copy the GATT services list here and clear the original so that when we 175 // Copy the GATT services list here and clear the original so that when we
172 // send GattServiceRemoved(), GetGattServices() returns no services. 176 // send GattServiceRemoved(), GetGattServices() returns no services.
173 GattServiceMap gatt_services_swapped; 177 GattServiceMap gatt_services_swapped;
174 gatt_services_swapped.swap(gatt_services_); 178 gatt_services_swapped.swap(gatt_services_);
175 for (const auto& iter : gatt_services_swapped) { 179 for (const auto& iter : gatt_services_swapped) {
176 DCHECK(adapter()); 180 DCHECK(adapter());
177 adapter()->NotifyGattServiceRemoved( 181 adapter()->NotifyGattServiceRemoved(
178 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second)); 182 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter.second));
179 } 183 }
180 } 184 }
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 } 578 }
575 579
576 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const { 580 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const {
577 return pairing_.get(); 581 return pairing_.get();
578 } 582 }
579 583
580 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const { 584 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const {
581 return static_cast<BluetoothAdapterBlueZ*>(adapter_); 585 return static_cast<BluetoothAdapterBlueZ*>(adapter_);
582 } 586 }
583 587
584 void BluetoothDeviceBlueZ::DevicePropertyChanged(
585 const dbus::ObjectPath& object_path,
586 const std::string& property_name) {
587 if (object_path != object_path_)
588 return;
589
590 bluez::BluetoothDeviceClient::Properties* properties =
591 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
592 object_path);
593 DCHECK(properties);
594
595 if (property_name == properties->services_resolved.name() &&
596 properties->services_resolved.value()) {
597 VLOG(3) << "All services were discovered for device: "
598 << object_path.value();
599
600 for (const auto iter : newly_discovered_gatt_services_) {
601 adapter()->NotifyGattDiscoveryComplete(
602 static_cast<BluetoothRemoteGattService*>(iter));
603 }
604 newly_discovered_gatt_services_.clear();
605 }
606 }
607
608 void BluetoothDeviceBlueZ::GattServiceAdded( 588 void BluetoothDeviceBlueZ::GattServiceAdded(
609 const dbus::ObjectPath& object_path) { 589 const dbus::ObjectPath& object_path) {
610 if (GetGattService(object_path.value())) { 590 if (GetGattService(object_path.value())) {
611 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); 591 VLOG(1) << "Remote GATT service already exists: " << object_path.value();
612 return; 592 return;
613 } 593 }
614 594
615 bluez::BluetoothGattServiceClient::Properties* properties = 595 bluez::BluetoothGattServiceClient::Properties* properties =
616 bluez::BluezDBusManager::Get() 596 bluez::BluezDBusManager::Get()
617 ->GetBluetoothGattServiceClient() 597 ->GetBluetoothGattServiceClient()
618 ->GetProperties(object_path); 598 ->GetProperties(object_path);
619 DCHECK(properties); 599 DCHECK(properties);
620 if (properties->device.value() != object_path_) { 600 if (properties->device.value() != object_path_) {
621 VLOG(2) << "Remote GATT service does not belong to this device."; 601 VLOG(2) << "Remote GATT service does not belong to this device.";
622 return; 602 return;
623 } 603 }
624 604
625 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); 605 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress();
626 606
627 BluetoothRemoteGattServiceBlueZ* service = 607 BluetoothRemoteGattServiceBlueZ* service =
628 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path); 608 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path);
629 609
630 newly_discovered_gatt_services_.push_back(
631 static_cast<BluetoothRemoteGattServiceBlueZ*>(service));
632
633 gatt_services_.set(service->GetIdentifier(), 610 gatt_services_.set(service->GetIdentifier(),
634 std::unique_ptr<BluetoothRemoteGattService>(service)); 611 std::unique_ptr<BluetoothRemoteGattService>(service));
635 DCHECK(service->object_path() == object_path); 612 DCHECK(service->object_path() == object_path);
636 DCHECK(service->GetUUID().IsValid()); 613 DCHECK(service->GetUUID().IsValid());
637 614
638 DCHECK(adapter()); 615 DCHECK(adapter());
639 adapter()->NotifyGattServiceAdded(service); 616 adapter()->NotifyGattServiceAdded(service);
640 } 617 }
641 618
642 void BluetoothDeviceBlueZ::GattServiceRemoved( 619 void BluetoothDeviceBlueZ::GattServiceRemoved(
(...skipping 10 matching lines...) Expand all
653 630
654 VLOG(1) << "Removing remote GATT service with UUID: '" 631 VLOG(1) << "Removing remote GATT service with UUID: '"
655 << service->GetUUID().canonical_value() 632 << service->GetUUID().canonical_value()
656 << "' from device: " << GetAddress(); 633 << "' from device: " << GetAddress();
657 634
658 DCHECK(service->object_path() == object_path); 635 DCHECK(service->object_path() == object_path);
659 std::unique_ptr<BluetoothRemoteGattService> scoped_service = 636 std::unique_ptr<BluetoothRemoteGattService> scoped_service =
660 gatt_services_.take_and_erase(iter->first); 637 gatt_services_.take_and_erase(iter->first);
661 638
662 DCHECK(adapter()); 639 DCHECK(adapter());
640 discovery_complete_notified_.erase(service);
663 adapter()->NotifyGattServiceRemoved(service); 641 adapter()->NotifyGattServiceRemoved(service);
664 } 642 }
665 643
666 void BluetoothDeviceBlueZ::InitializeGattServiceMap() { 644 void BluetoothDeviceBlueZ::UpdateGattServices(
667 DCHECK(gatt_services_.empty()); 645 const dbus::ObjectPath& object_path) {
668 646 if (object_path != object_path_) {
669 if (!IsGattServicesDiscoveryComplete()) { 647 // No need to update map if update is for a different device.
670 VLOG(2) << "Gatt services have not been fully resolved for device "
671 << object_path_.value();
672 return; 648 return;
673 } 649 }
674 650
675 VLOG(3) << "Initializing the list of GATT services associated with device " 651 DCHECK(IsGattServicesDiscoveryComplete());
652
653 VLOG(3) << "Updating the list of GATT services associated with device "
676 << object_path_.value(); 654 << object_path_.value();
677 655
678 // Add all known GATT services associated with the device. 656 const std::vector<dbus::ObjectPath> service_paths =
679 const std::vector<dbus::ObjectPath> gatt_services =
680 bluez::BluezDBusManager::Get() 657 bluez::BluezDBusManager::Get()
681 ->GetBluetoothGattServiceClient() 658 ->GetBluetoothGattServiceClient()
682 ->GetServices(); 659 ->GetServices();
683 for (const auto& it : gatt_services) 660 for (const auto& service_path : service_paths) {
684 GattServiceAdded(it); 661 // Add all previously unknown GATT services associated with the device.
662 GattServiceAdded(service_path);
685 663
686 // Notify on the discovery complete for each service which is found in the 664 // If the service does not belong in this device, there is nothing left to
687 // first discovery. 665 // do.
688 DCHECK(adapter()); 666 BluetoothRemoteGattService* service = GetGattService(service_path.value());
689 for (const auto& iter : gatt_services_) 667 if (service == nullptr) {
690 adapter()->NotifyGattDiscoveryComplete(iter.second); 668 return;
669 }
670
671 // Notify of GATT discovery complete if we haven't before.
672 auto notified_pair = discovery_complete_notified_.insert(service);
673 if (notified_pair.second) {
674 adapter()->NotifyGattDiscoveryComplete(service);
675 }
676 }
691 } 677 }
692 678
693 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback, 679 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback,
694 int16_t rssi, 680 int16_t rssi,
695 int16_t transmit_power, 681 int16_t transmit_power,
696 int16_t max_transmit_power) { 682 int16_t max_transmit_power) {
697 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); 683 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power));
698 } 684 }
699 685
700 void BluetoothDeviceBlueZ::OnGetConnInfoError( 686 void BluetoothDeviceBlueZ::OnGetConnInfoError(
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
887 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback, 873 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback,
888 const std::string& error_name, 874 const std::string& error_name,
889 const std::string& error_message) { 875 const std::string& error_message) {
890 LOG(WARNING) << object_path_.value() 876 LOG(WARNING) << object_path_.value()
891 << ": Failed to remove device: " << error_name << ": " 877 << ": Failed to remove device: " << error_name << ": "
892 << error_message; 878 << error_message;
893 error_callback.Run(); 879 error_callback.Run();
894 } 880 }
895 881
896 } // namespace bluez 882 } // namespace bluez
OLDNEW
« no previous file with comments | « device/bluetooth/bluez/bluetooth_device_bluez.h ('k') | device/bluetooth/bluez/bluetooth_gatt_bluez_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698