bluetooth_gatt_descriptor_client.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_descriptor_client.h"
  5. #include <stddef.h>
  6. #include "base/bind.h"
  7. #include "base/logging.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/observer_list.h"
  11. #include "base/values.h"
  12. #include "dbus/bus.h"
  13. #include "dbus/object_manager.h"
  14. #include "dbus/values_util.h"
  15. #include "third_party/cros_system_api/dbus/service_constants.h"
  16. namespace bluez {
  17. namespace {
  18. // TODO(armansito): Move this constant to cros_system_api.
  19. const char kValueProperty[] = "Value";
  20. } // namespace
  21. // static
  22. const char BluetoothGattDescriptorClient::kNoResponseError[] =
  23. "org.chromium.Error.NoResponse";
  24. // static
  25. const char BluetoothGattDescriptorClient::kUnknownDescriptorError[] =
  26. "org.chromium.Error.UnknownDescriptor";
  27. BluetoothGattDescriptorClient::Properties::Properties(
  28. dbus::ObjectProxy* object_proxy,
  29. const std::string& interface_name,
  30. const PropertyChangedCallback& callback)
  31. : dbus::PropertySet(object_proxy, interface_name, callback) {
  32. RegisterProperty(bluetooth_gatt_descriptor::kUUIDProperty, &uuid);
  33. RegisterProperty(bluetooth_gatt_descriptor::kCharacteristicProperty,
  34. &characteristic);
  35. RegisterProperty(kValueProperty, &value);
  36. }
  37. BluetoothGattDescriptorClient::Properties::~Properties() = default;
  38. // The BluetoothGattDescriptorClient implementation used in production.
  39. class BluetoothGattDescriptorClientImpl
  40. : public BluetoothGattDescriptorClient,
  41. public dbus::ObjectManager::Interface {
  42. public:
  43. BluetoothGattDescriptorClientImpl() : object_manager_(nullptr) {}
  44. BluetoothGattDescriptorClientImpl(const BluetoothGattDescriptorClientImpl&) =
  45. delete;
  46. BluetoothGattDescriptorClientImpl& operator=(
  47. const BluetoothGattDescriptorClientImpl&) = delete;
  48. ~BluetoothGattDescriptorClientImpl() override {
  49. object_manager_->UnregisterInterface(
  50. bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface);
  51. }
  52. // BluetoothGattDescriptorClientImpl override.
  53. void AddObserver(BluetoothGattDescriptorClient::Observer* observer) override {
  54. DCHECK(observer);
  55. observers_.AddObserver(observer);
  56. }
  57. // BluetoothGattDescriptorClientImpl override.
  58. void RemoveObserver(
  59. BluetoothGattDescriptorClient::Observer* observer) override {
  60. DCHECK(observer);
  61. observers_.RemoveObserver(observer);
  62. }
  63. // BluetoothGattDescriptorClientImpl override.
  64. std::vector<dbus::ObjectPath> GetDescriptors() override {
  65. DCHECK(object_manager_);
  66. return object_manager_->GetObjectsWithInterface(
  67. bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface);
  68. }
  69. // BluetoothGattDescriptorClientImpl override.
  70. Properties* GetProperties(const dbus::ObjectPath& object_path) override {
  71. DCHECK(object_manager_);
  72. return static_cast<Properties*>(object_manager_->GetProperties(
  73. object_path,
  74. bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface));
  75. }
  76. // BluetoothGattDescriptorClientImpl override.
  77. void ReadValue(const dbus::ObjectPath& object_path,
  78. ValueCallback callback,
  79. ErrorCallback error_callback) override {
  80. dbus::ObjectProxy* object_proxy =
  81. object_manager_->GetObjectProxy(object_path);
  82. if (!object_proxy) {
  83. std::move(error_callback).Run(kUnknownDescriptorError, "");
  84. return;
  85. }
  86. dbus::MethodCall method_call(
  87. bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface,
  88. bluetooth_gatt_descriptor::kReadValue);
  89. // Append empty option dict
  90. dbus::MessageWriter writer(&method_call);
  91. dbus::AppendValueData(&writer, base::Value::Dict());
  92. object_proxy->CallMethodWithErrorCallback(
  93. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  94. base::BindOnce(&BluetoothGattDescriptorClientImpl::OnValueSuccess,
  95. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  96. base::BindOnce(&BluetoothGattDescriptorClientImpl::OnError,
  97. weak_ptr_factory_.GetWeakPtr(),
  98. std::move(error_callback)));
  99. }
  100. // BluetoothGattDescriptorClientImpl override.
  101. void WriteValue(const dbus::ObjectPath& object_path,
  102. const std::vector<uint8_t>& value,
  103. base::OnceClosure callback,
  104. ErrorCallback error_callback) override {
  105. dbus::ObjectProxy* object_proxy =
  106. object_manager_->GetObjectProxy(object_path);
  107. if (!object_proxy) {
  108. std::move(error_callback).Run(kUnknownDescriptorError, "");
  109. return;
  110. }
  111. dbus::MethodCall method_call(
  112. bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface,
  113. bluetooth_gatt_descriptor::kWriteValue);
  114. dbus::MessageWriter writer(&method_call);
  115. writer.AppendArrayOfBytes(value.data(), value.size());
  116. // Append empty option dict
  117. dbus::AppendValueData(&writer, base::Value::Dict());
  118. object_proxy->CallMethodWithErrorCallback(
  119. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  120. base::BindOnce(&BluetoothGattDescriptorClientImpl::OnSuccess,
  121. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  122. base::BindOnce(&BluetoothGattDescriptorClientImpl::OnError,
  123. weak_ptr_factory_.GetWeakPtr(),
  124. std::move(error_callback)));
  125. }
  126. // dbus::ObjectManager::Interface override.
  127. dbus::PropertySet* CreateProperties(
  128. dbus::ObjectProxy* object_proxy,
  129. const dbus::ObjectPath& object_path,
  130. const std::string& interface_name) override {
  131. return new Properties(
  132. object_proxy, interface_name,
  133. base::BindRepeating(
  134. &BluetoothGattDescriptorClientImpl::OnPropertyChanged,
  135. weak_ptr_factory_.GetWeakPtr(), object_path));
  136. }
  137. // dbus::ObjectManager::Interface override.
  138. void ObjectAdded(const dbus::ObjectPath& object_path,
  139. const std::string& interface_name) override {
  140. DVLOG(2) << "Remote GATT descriptor added: " << object_path.value();
  141. for (auto& observer : observers_)
  142. observer.GattDescriptorAdded(object_path);
  143. }
  144. // dbus::ObjectManager::Interface override.
  145. void ObjectRemoved(const dbus::ObjectPath& object_path,
  146. const std::string& interface_name) override {
  147. DVLOG(2) << "Remote GATT descriptor removed: " << object_path.value();
  148. for (auto& observer : observers_)
  149. observer.GattDescriptorRemoved(object_path);
  150. }
  151. protected:
  152. // bluez::DBusClient override.
  153. void Init(dbus::Bus* bus,
  154. const std::string& bluetooth_service_name) override {
  155. object_manager_ = bus->GetObjectManager(
  156. bluetooth_service_name,
  157. dbus::ObjectPath(
  158. bluetooth_object_manager::kBluetoothObjectManagerServicePath));
  159. object_manager_->RegisterInterface(
  160. bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface, this);
  161. }
  162. private:
  163. // Called by dbus::PropertySet when a property value is changed, either by
  164. // result of a signal or response to a GetAll() or Get() call. Informs
  165. // observers.
  166. virtual void OnPropertyChanged(const dbus::ObjectPath& object_path,
  167. const std::string& property_name) {
  168. DVLOG(2) << "Remote GATT descriptor property changed: "
  169. << object_path.value() << ": " << property_name;
  170. for (auto& observer : observers_)
  171. observer.GattDescriptorPropertyChanged(object_path, property_name);
  172. }
  173. // Called when a response for a successful method call is received.
  174. void OnSuccess(base::OnceClosure callback, dbus::Response* response) {
  175. DCHECK(response);
  176. std::move(callback).Run();
  177. }
  178. // Called when a descriptor value response for a successful method call is
  179. // received.
  180. void OnValueSuccess(ValueCallback callback, dbus::Response* response) {
  181. DCHECK(response);
  182. dbus::MessageReader reader(response);
  183. const uint8_t* bytes = NULL;
  184. size_t length = 0;
  185. if (!reader.PopArrayOfBytes(&bytes, &length))
  186. DVLOG(2) << "Error reading array of bytes in ValueCallback";
  187. std::vector<uint8_t> value;
  188. if (bytes)
  189. value.assign(bytes, bytes + length);
  190. std::move(callback).Run(/*error_code=*/absl::nullopt, value);
  191. }
  192. // Called when a response for a failed method call is received.
  193. void OnError(ErrorCallback error_callback, dbus::ErrorResponse* response) {
  194. // Error response has optional error message argument.
  195. std::string error_name;
  196. std::string error_message;
  197. if (response) {
  198. dbus::MessageReader reader(response);
  199. error_name = response->GetErrorName();
  200. reader.PopString(&error_message);
  201. } else {
  202. error_name = kNoResponseError;
  203. error_message = "";
  204. }
  205. std::move(error_callback).Run(error_name, error_message);
  206. }
  207. raw_ptr<dbus::ObjectManager> object_manager_;
  208. // List of observers interested in event notifications from us.
  209. base::ObserverList<BluetoothGattDescriptorClient::Observer>::Unchecked
  210. observers_;
  211. // Weak pointer factory for generating 'this' pointers that might live longer
  212. // than we do.
  213. // Note: This should remain the last member so it'll be destroyed and
  214. // invalidate its weak pointers before any other members are destroyed.
  215. base::WeakPtrFactory<BluetoothGattDescriptorClientImpl> weak_ptr_factory_{
  216. this};
  217. };
  218. BluetoothGattDescriptorClient::BluetoothGattDescriptorClient() = default;
  219. BluetoothGattDescriptorClient::~BluetoothGattDescriptorClient() = default;
  220. // static
  221. BluetoothGattDescriptorClient* BluetoothGattDescriptorClient::Create() {
  222. return new BluetoothGattDescriptorClientImpl();
  223. }
  224. } // namespace bluez