bluetooth_gatt_characteristic_client.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_characteristic_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/bluetooth/dbus-constants.h"
  16. #include "third_party/cros_system_api/dbus/service_constants.h"
  17. namespace bluez {
  18. // static
  19. const char BluetoothGattCharacteristicClient::kNoResponseError[] =
  20. "org.chromium.Error.NoResponse";
  21. // static
  22. const char BluetoothGattCharacteristicClient::kUnknownCharacteristicError[] =
  23. "org.chromium.Error.UnknownCharacteristic";
  24. BluetoothGattCharacteristicClient::Properties::Properties(
  25. dbus::ObjectProxy* object_proxy,
  26. const std::string& interface_name,
  27. const PropertyChangedCallback& callback)
  28. : dbus::PropertySet(object_proxy, interface_name, callback) {
  29. RegisterProperty(bluetooth_gatt_characteristic::kUUIDProperty, &uuid);
  30. RegisterProperty(bluetooth_gatt_characteristic::kServiceProperty, &service);
  31. RegisterProperty(bluetooth_gatt_characteristic::kValueProperty, &value);
  32. RegisterProperty(bluetooth_gatt_characteristic::kNotifyingProperty,
  33. &notifying);
  34. RegisterProperty(bluetooth_gatt_characteristic::kFlagsProperty, &flags);
  35. }
  36. BluetoothGattCharacteristicClient::Properties::~Properties() = default;
  37. // The BluetoothGattCharacteristicClient implementation used in production.
  38. class BluetoothGattCharacteristicClientImpl
  39. : public BluetoothGattCharacteristicClient,
  40. public dbus::ObjectManager::Interface {
  41. public:
  42. BluetoothGattCharacteristicClientImpl() : object_manager_(nullptr) {}
  43. BluetoothGattCharacteristicClientImpl(
  44. const BluetoothGattCharacteristicClientImpl&) = delete;
  45. BluetoothGattCharacteristicClientImpl& operator=(
  46. const BluetoothGattCharacteristicClientImpl&) = delete;
  47. ~BluetoothGattCharacteristicClientImpl() override {
  48. object_manager_->UnregisterInterface(
  49. bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface);
  50. }
  51. // BluetoothGattCharacteristicClient override.
  52. void AddObserver(
  53. BluetoothGattCharacteristicClient::Observer* observer) override {
  54. DCHECK(observer);
  55. observers_.AddObserver(observer);
  56. }
  57. // BluetoothGattCharacteristicClient override.
  58. void RemoveObserver(
  59. BluetoothGattCharacteristicClient::Observer* observer) override {
  60. DCHECK(observer);
  61. observers_.RemoveObserver(observer);
  62. }
  63. // BluetoothGattCharacteristicClient override.
  64. std::vector<dbus::ObjectPath> GetCharacteristics() override {
  65. DCHECK(object_manager_);
  66. return object_manager_->GetObjectsWithInterface(
  67. bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface);
  68. }
  69. // BluetoothGattCharacteristicClient 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_characteristic::kBluetoothGattCharacteristicInterface));
  75. }
  76. // BluetoothGattCharacteristicClient 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(kUnknownCharacteristicError, "");
  84. return;
  85. }
  86. dbus::MethodCall method_call(
  87. bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
  88. bluetooth_gatt_characteristic::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(&BluetoothGattCharacteristicClientImpl::OnValueSuccess,
  95. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  96. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnError,
  97. weak_ptr_factory_.GetWeakPtr(),
  98. std::move(error_callback)));
  99. }
  100. // BluetoothGattCharacteristicClient override.
  101. void WriteValue(const dbus::ObjectPath& object_path,
  102. const std::vector<uint8_t>& value,
  103. base::StringPiece type_option,
  104. base::OnceClosure callback,
  105. ErrorCallback error_callback) override {
  106. dbus::ObjectProxy* object_proxy =
  107. object_manager_->GetObjectProxy(object_path);
  108. if (!object_proxy) {
  109. std::move(error_callback).Run(kUnknownCharacteristicError, "");
  110. return;
  111. }
  112. dbus::MethodCall method_call(
  113. bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
  114. bluetooth_gatt_characteristic::kWriteValue);
  115. dbus::MessageWriter writer(&method_call);
  116. writer.AppendArrayOfBytes(value.data(), value.size());
  117. // Append option dict
  118. base::Value::Dict dict;
  119. if (!type_option.empty()) {
  120. // NB: the "type" option was added in BlueZ 5.51. Older versions of BlueZ
  121. // will ignore this option.
  122. dict.Set(bluetooth_gatt_characteristic::kOptionType, type_option);
  123. }
  124. dbus::AppendValueData(&writer, dict);
  125. object_proxy->CallMethodWithErrorCallback(
  126. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  127. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnSuccess,
  128. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  129. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnError,
  130. weak_ptr_factory_.GetWeakPtr(),
  131. std::move(error_callback)));
  132. }
  133. void PrepareWriteValue(const dbus::ObjectPath& object_path,
  134. const std::vector<uint8_t>& value,
  135. base::OnceClosure callback,
  136. ErrorCallback error_callback) override {
  137. dbus::ObjectProxy* object_proxy =
  138. object_manager_->GetObjectProxy(object_path);
  139. if (!object_proxy) {
  140. std::move(error_callback).Run(kUnknownCharacteristicError, "");
  141. return;
  142. }
  143. dbus::MethodCall method_call(
  144. bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
  145. bluetooth_gatt_characteristic::kPrepareWriteValue);
  146. dbus::MessageWriter writer(&method_call);
  147. writer.AppendArrayOfBytes(value.data(), value.size());
  148. dbus::AppendValueData(&writer, base::Value::Dict());
  149. object_proxy->CallMethodWithErrorCallback(
  150. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  151. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnSuccess,
  152. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  153. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnError,
  154. weak_ptr_factory_.GetWeakPtr(),
  155. std::move(error_callback)));
  156. }
  157. // BluetoothGattCharacteristicClient override.
  158. void StartNotify(
  159. const dbus::ObjectPath& object_path,
  160. #if BUILDFLAG(IS_CHROMEOS)
  161. device::BluetoothGattCharacteristic::NotificationType notification_type,
  162. #endif // BUILDFLAG(IS_CHROMEOS)
  163. base::OnceClosure callback,
  164. ErrorCallback error_callback) override {
  165. dbus::ObjectProxy* object_proxy =
  166. object_manager_->GetObjectProxy(object_path);
  167. if (!object_proxy) {
  168. std::move(error_callback).Run(kUnknownCharacteristicError, "");
  169. return;
  170. }
  171. dbus::MethodCall method_call(
  172. bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
  173. bluetooth_gatt_characteristic::kStartNotify);
  174. #if BUILDFLAG(IS_CHROMEOS)
  175. dbus::MessageWriter writer(&method_call);
  176. writer.AppendByte(static_cast<uint8_t>(notification_type));
  177. #endif // BUILDFLAG(IS_CHROMEOS)
  178. object_proxy->CallMethodWithErrorCallback(
  179. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  180. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnSuccess,
  181. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  182. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnError,
  183. weak_ptr_factory_.GetWeakPtr(),
  184. std::move(error_callback)));
  185. }
  186. // BluetoothGattCharacteristicClient override.
  187. void StopNotify(const dbus::ObjectPath& object_path,
  188. base::OnceClosure callback,
  189. ErrorCallback error_callback) override {
  190. dbus::ObjectProxy* object_proxy =
  191. object_manager_->GetObjectProxy(object_path);
  192. if (!object_proxy) {
  193. std::move(error_callback).Run(kUnknownCharacteristicError, "");
  194. return;
  195. }
  196. dbus::MethodCall method_call(
  197. bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
  198. bluetooth_gatt_characteristic::kStopNotify);
  199. object_proxy->CallMethodWithErrorCallback(
  200. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  201. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnSuccess,
  202. weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
  203. base::BindOnce(&BluetoothGattCharacteristicClientImpl::OnError,
  204. weak_ptr_factory_.GetWeakPtr(),
  205. std::move(error_callback)));
  206. }
  207. // dbus::ObjectManager::Interface override.
  208. dbus::PropertySet* CreateProperties(
  209. dbus::ObjectProxy* object_proxy,
  210. const dbus::ObjectPath& object_path,
  211. const std::string& interface_name) override {
  212. return new Properties(
  213. object_proxy, interface_name,
  214. base::BindRepeating(
  215. &BluetoothGattCharacteristicClientImpl::OnPropertyChanged,
  216. weak_ptr_factory_.GetWeakPtr(), object_path));
  217. }
  218. // dbus::ObjectManager::Interface override.
  219. void ObjectAdded(const dbus::ObjectPath& object_path,
  220. const std::string& interface_name) override {
  221. DVLOG(2) << "Remote GATT characteristic added: " << object_path.value();
  222. for (auto& observer : observers_)
  223. observer.GattCharacteristicAdded(object_path);
  224. }
  225. // dbus::ObjectManager::Interface override.
  226. void ObjectRemoved(const dbus::ObjectPath& object_path,
  227. const std::string& interface_name) override {
  228. DVLOG(2) << "Remote GATT characteristic removed: " << object_path.value();
  229. for (auto& observer : observers_)
  230. observer.GattCharacteristicRemoved(object_path);
  231. }
  232. protected:
  233. // bluez::DBusClient override.
  234. void Init(dbus::Bus* bus,
  235. const std::string& bluetooth_service_name) override {
  236. object_manager_ = bus->GetObjectManager(
  237. bluetooth_service_name,
  238. dbus::ObjectPath(
  239. bluetooth_object_manager::kBluetoothObjectManagerServicePath));
  240. object_manager_->RegisterInterface(
  241. bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
  242. this);
  243. }
  244. private:
  245. // Called by dbus::PropertySet when a property value is changed, either by
  246. // result of a signal or response to a GetAll() or Get() call. Informs
  247. // observers.
  248. virtual void OnPropertyChanged(const dbus::ObjectPath& object_path,
  249. const std::string& property_name) {
  250. DVLOG(2) << "Remote GATT characteristic property changed: "
  251. << object_path.value() << ": " << property_name;
  252. for (auto& observer : observers_)
  253. observer.GattCharacteristicPropertyChanged(object_path, property_name);
  254. }
  255. // Called when a response for successful method call is received.
  256. void OnSuccess(base::OnceClosure callback, dbus::Response* response) {
  257. DCHECK(response);
  258. std::move(callback).Run();
  259. }
  260. // Called when a characteristic value response for a successful method call
  261. // is received.
  262. void OnValueSuccess(ValueCallback callback, dbus::Response* response) {
  263. DCHECK(response);
  264. dbus::MessageReader reader(response);
  265. const uint8_t* bytes = NULL;
  266. size_t length = 0;
  267. if (!reader.PopArrayOfBytes(&bytes, &length))
  268. DVLOG(2) << "Error reading array of bytes in ValueCallback";
  269. std::vector<uint8_t> value;
  270. if (bytes)
  271. value.assign(bytes, bytes + length);
  272. std::move(callback).Run(/*error_code=*/absl::nullopt, value);
  273. }
  274. // Called when a response for a failed method call is received.
  275. void OnError(ErrorCallback error_callback, dbus::ErrorResponse* response) {
  276. // Error response has optional error message argument.
  277. std::string error_name;
  278. std::string error_message;
  279. if (response) {
  280. dbus::MessageReader reader(response);
  281. error_name = response->GetErrorName();
  282. reader.PopString(&error_message);
  283. } else {
  284. error_name = kNoResponseError;
  285. error_message = "";
  286. }
  287. std::move(error_callback).Run(error_name, error_message);
  288. }
  289. raw_ptr<dbus::ObjectManager> object_manager_;
  290. // List of observers interested in event notifications from us.
  291. base::ObserverList<BluetoothGattCharacteristicClient::Observer>::Unchecked
  292. observers_;
  293. // Weak pointer factory for generating 'this' pointers that might live longer
  294. // than we do.
  295. // Note: This should remain the last member so it'll be destroyed and
  296. // invalidate its weak pointers before any other members are destroyed.
  297. base::WeakPtrFactory<BluetoothGattCharacteristicClientImpl> weak_ptr_factory_{
  298. this};
  299. };
  300. BluetoothGattCharacteristicClient::BluetoothGattCharacteristicClient() =
  301. default;
  302. BluetoothGattCharacteristicClient::~BluetoothGattCharacteristicClient() =
  303. default;
  304. // static
  305. BluetoothGattCharacteristicClient* BluetoothGattCharacteristicClient::Create() {
  306. return new BluetoothGattCharacteristicClientImpl();
  307. }
  308. } // namespace bluez