bluetooth_gatt_service_client.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "device/bluetooth/dbus/bluetooth_gatt_service_client.h"
  5. #include "base/bind.h"
  6. #include "base/logging.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/observer_list.h"
  10. #include "dbus/bus.h"
  11. #include "dbus/object_manager.h"
  12. #include "third_party/cros_system_api/dbus/service_constants.h"
  13. namespace bluez {
  14. BluetoothGattServiceClient::Properties::Properties(
  15. dbus::ObjectProxy* object_proxy,
  16. const std::string& interface_name,
  17. const PropertyChangedCallback& callback)
  18. : dbus::PropertySet(object_proxy, interface_name, callback) {
  19. RegisterProperty(bluetooth_gatt_service::kUUIDProperty, &uuid);
  20. RegisterProperty(bluetooth_gatt_service::kIncludesProperty, &includes);
  21. RegisterProperty(bluetooth_gatt_service::kDeviceProperty, &device);
  22. RegisterProperty(bluetooth_gatt_service::kPrimaryProperty, &primary);
  23. }
  24. BluetoothGattServiceClient::Properties::~Properties() = default;
  25. // The BluetoothGattServiceClient implementation used in production.
  26. class BluetoothGattServiceClientImpl : public BluetoothGattServiceClient,
  27. public dbus::ObjectManager::Interface {
  28. public:
  29. BluetoothGattServiceClientImpl() : object_manager_(nullptr) {}
  30. BluetoothGattServiceClientImpl(const BluetoothGattServiceClientImpl&) =
  31. delete;
  32. BluetoothGattServiceClientImpl& operator=(
  33. const BluetoothGattServiceClientImpl&) = delete;
  34. ~BluetoothGattServiceClientImpl() override {
  35. object_manager_->UnregisterInterface(
  36. bluetooth_gatt_service::kBluetoothGattServiceInterface);
  37. }
  38. // BluetoothGattServiceClientImpl override.
  39. void AddObserver(BluetoothGattServiceClient::Observer* observer) override {
  40. DCHECK(observer);
  41. observers_.AddObserver(observer);
  42. }
  43. // BluetoothGattServiceClientImpl override.
  44. void RemoveObserver(BluetoothGattServiceClient::Observer* observer) override {
  45. DCHECK(observer);
  46. observers_.RemoveObserver(observer);
  47. }
  48. // BluetoothGattServiceClientImpl override.
  49. std::vector<dbus::ObjectPath> GetServices() override {
  50. DCHECK(object_manager_);
  51. return object_manager_->GetObjectsWithInterface(
  52. bluetooth_gatt_service::kBluetoothGattServiceInterface);
  53. }
  54. // BluetoothGattServiceClientImpl override.
  55. Properties* GetProperties(const dbus::ObjectPath& object_path) override {
  56. DCHECK(object_manager_);
  57. return static_cast<Properties*>(object_manager_->GetProperties(
  58. object_path, bluetooth_gatt_service::kBluetoothGattServiceInterface));
  59. }
  60. // dbus::ObjectManager::Interface override.
  61. dbus::PropertySet* CreateProperties(
  62. dbus::ObjectProxy* object_proxy,
  63. const dbus::ObjectPath& object_path,
  64. const std::string& interface_name) override {
  65. return new Properties(
  66. object_proxy, interface_name,
  67. base::BindRepeating(&BluetoothGattServiceClientImpl::OnPropertyChanged,
  68. weak_ptr_factory_.GetWeakPtr(), object_path));
  69. }
  70. // dbus::ObjectManager::Interface override.
  71. void ObjectAdded(const dbus::ObjectPath& object_path,
  72. const std::string& interface_name) override {
  73. DVLOG(2) << "Remote GATT service added: " << object_path.value();
  74. for (auto& observer : observers_)
  75. observer.GattServiceAdded(object_path);
  76. }
  77. // dbus::ObjectManager::Interface override.
  78. void ObjectRemoved(const dbus::ObjectPath& object_path,
  79. const std::string& interface_name) override {
  80. DVLOG(2) << "Remote GATT service removed: " << object_path.value();
  81. for (auto& observer : observers_)
  82. observer.GattServiceRemoved(object_path);
  83. }
  84. protected:
  85. // bluez::DBusClient override.
  86. void Init(dbus::Bus* bus,
  87. const std::string& bluetooth_service_name) override {
  88. object_manager_ = bus->GetObjectManager(
  89. bluetooth_service_name,
  90. dbus::ObjectPath(
  91. bluetooth_object_manager::kBluetoothObjectManagerServicePath));
  92. object_manager_->RegisterInterface(
  93. bluetooth_gatt_service::kBluetoothGattServiceInterface, this);
  94. }
  95. private:
  96. // Called by dbus::PropertySet when a property value is changed, either by
  97. // result of a signal or response to a GetAll() or Get() call. Informs
  98. // observers.
  99. virtual void OnPropertyChanged(const dbus::ObjectPath& object_path,
  100. const std::string& property_name) {
  101. DVLOG(2) << "Remote GATT service property changed: " << object_path.value()
  102. << ": " << property_name;
  103. for (auto& observer : observers_)
  104. observer.GattServicePropertyChanged(object_path, property_name);
  105. }
  106. raw_ptr<dbus::ObjectManager> object_manager_;
  107. // List of observers interested in event notifications from us.
  108. base::ObserverList<BluetoothGattServiceClient::Observer>::Unchecked
  109. observers_;
  110. // Weak pointer factory for generating 'this' pointers that might live longer
  111. // than we do.
  112. // Note: This should remain the last member so it'll be destroyed and
  113. // invalidate its weak pointers before any other members are destroyed.
  114. base::WeakPtrFactory<BluetoothGattServiceClientImpl> weak_ptr_factory_{this};
  115. };
  116. BluetoothGattServiceClient::BluetoothGattServiceClient() = default;
  117. BluetoothGattServiceClient::~BluetoothGattServiceClient() = default;
  118. // static
  119. BluetoothGattServiceClient* BluetoothGattServiceClient::Create() {
  120. return new BluetoothGattServiceClientImpl();
  121. }
  122. } // namespace bluez