bluetooth_gatt_manager_client.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_manager_client.h"
  5. #include "base/bind.h"
  6. #include "base/check.h"
  7. #include "base/logging.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/strings/strcat.h"
  11. #include "dbus/bus.h"
  12. #include "dbus/message.h"
  13. #include "dbus/object_manager.h"
  14. #include "dbus/object_proxy.h"
  15. #include "third_party/cros_system_api/dbus/service_constants.h"
  16. namespace bluez {
  17. const char BluetoothGattManagerClient::kNoResponseError[] =
  18. "org.chromium.Error.NoResponse";
  19. const char BluetoothGattManagerClient::kUnknownGattManager[] =
  20. "org.chromium.Error.UnknownGattManager";
  21. namespace {
  22. const char kNoGattManagerMessage[] = "No GATT Manager found: ";
  23. } // namespace
  24. // The BluetoothGattManagerClient implementation used in production.
  25. class BluetoothGattManagerClientImpl : public BluetoothGattManagerClient {
  26. public:
  27. BluetoothGattManagerClientImpl() : object_manager_(nullptr) {}
  28. BluetoothGattManagerClientImpl(const BluetoothGattManagerClientImpl&) =
  29. delete;
  30. BluetoothGattManagerClientImpl& operator=(
  31. const BluetoothGattManagerClientImpl&) = delete;
  32. ~BluetoothGattManagerClientImpl() override = default;
  33. // BluetoothGattManagerClient override.
  34. void RegisterApplication(const dbus::ObjectPath& adapter_object_path,
  35. const dbus::ObjectPath& application_path,
  36. const Options& options,
  37. base::OnceClosure callback,
  38. ErrorCallback error_callback) override {
  39. dbus::MethodCall method_call(
  40. bluetooth_gatt_manager::kBluetoothGattManagerInterface,
  41. bluetooth_gatt_manager::kRegisterApplication);
  42. dbus::MessageWriter writer(&method_call);
  43. writer.AppendObjectPath(application_path);
  44. // The parameters of the Options dictionary are undefined but the method
  45. // signature still requires a value dictionary. Pass an empty dictionary
  46. // and fill in the contents later if and when we add any options.
  47. dbus::MessageWriter array_writer(NULL);
  48. writer.OpenArray("{sv}", &array_writer);
  49. writer.CloseContainer(&array_writer);
  50. DCHECK(object_manager_);
  51. dbus::ObjectProxy* object_proxy =
  52. object_manager_->GetObjectProxy(adapter_object_path);
  53. if (!object_proxy) {
  54. RespondWhenNoProxyAvailable(adapter_object_path,
  55. std::move(error_callback));
  56. return;
  57. }
  58. object_proxy->CallMethodWithErrorCallback(
  59. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  60. base::BindOnce(&BluetoothGattManagerClientImpl::OnSuccess,
  61. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  62. base::BindOnce(&BluetoothGattManagerClientImpl::OnError,
  63. weak_ptr_factory_.GetWeakPtr(),
  64. std::move(error_callback)));
  65. }
  66. // BluetoothGattManagerClient override.
  67. void UnregisterApplication(const dbus::ObjectPath& adapter_object_path,
  68. const dbus::ObjectPath& application_path,
  69. base::OnceClosure callback,
  70. ErrorCallback error_callback) override {
  71. dbus::MethodCall method_call(
  72. bluetooth_gatt_manager::kBluetoothGattManagerInterface,
  73. bluetooth_gatt_manager::kUnregisterApplication);
  74. dbus::MessageWriter writer(&method_call);
  75. writer.AppendObjectPath(application_path);
  76. DCHECK(object_manager_);
  77. dbus::ObjectProxy* object_proxy =
  78. object_manager_->GetObjectProxy(adapter_object_path);
  79. if (!object_proxy) {
  80. RespondWhenNoProxyAvailable(adapter_object_path,
  81. std::move(error_callback));
  82. return;
  83. }
  84. object_proxy->CallMethodWithErrorCallback(
  85. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  86. base::BindOnce(&BluetoothGattManagerClientImpl::OnSuccess,
  87. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  88. base::BindOnce(&BluetoothGattManagerClientImpl::OnError,
  89. weak_ptr_factory_.GetWeakPtr(),
  90. std::move(error_callback)));
  91. }
  92. protected:
  93. // bluez::DBusClient override.
  94. void Init(dbus::Bus* bus,
  95. const std::string& bluetooth_service_name) override {
  96. DCHECK(bus);
  97. DCHECK(bus);
  98. object_manager_ = bus->GetObjectManager(
  99. bluetooth_service_name,
  100. dbus::ObjectPath(
  101. bluetooth_object_manager::kBluetoothObjectManagerServicePath));
  102. }
  103. private:
  104. // Called when a response for a successful method call is received.
  105. void OnSuccess(base::OnceClosure callback, dbus::Response* response) {
  106. DCHECK(response);
  107. std::move(callback).Run();
  108. }
  109. // Called when a response for a failed method call is received.
  110. void OnError(ErrorCallback error_callback, dbus::ErrorResponse* response) {
  111. // Error response has optional error message argument.
  112. std::string error_name;
  113. std::string error_message;
  114. if (response) {
  115. dbus::MessageReader reader(response);
  116. error_name = response->GetErrorName();
  117. reader.PopString(&error_message);
  118. } else {
  119. error_name = kNoResponseError;
  120. }
  121. std::move(error_callback).Run(error_name, error_message);
  122. }
  123. void RespondWhenNoProxyAvailable(const dbus::ObjectPath& application_path,
  124. ErrorCallback error_callback) {
  125. LOG(WARNING) << "No ObjectProxy found for " << application_path.value();
  126. std::move(error_callback)
  127. .Run(kUnknownGattManager,
  128. base::StrCat({kNoGattManagerMessage, application_path.value()}));
  129. }
  130. // The proxy to the bluez object manager.
  131. raw_ptr<dbus::ObjectManager> object_manager_;
  132. // Weak pointer factory for generating 'this' pointers that might live longer
  133. // than we do.
  134. // Note: This should remain the last member so it'll be destroyed and
  135. // invalidate its weak pointers before any other members are destroyed.
  136. base::WeakPtrFactory<BluetoothGattManagerClientImpl> weak_ptr_factory_{this};
  137. };
  138. BluetoothGattManagerClient::BluetoothGattManagerClient() = default;
  139. BluetoothGattManagerClient::~BluetoothGattManagerClient() = default;
  140. // static
  141. BluetoothGattManagerClient* BluetoothGattManagerClient::Create() {
  142. return new BluetoothGattManagerClientImpl();
  143. }
  144. } // namespace bluez