bluetooth_gatt_characteristic_client.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "dbus/object_path.h"
  11. #include "dbus/property.h"
  12. #include "device/bluetooth/bluetooth_export.h"
  13. #include "device/bluetooth/bluetooth_gatt_characteristic.h"
  14. #include "device/bluetooth/dbus/bluez_dbus_client.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace bluez {
  17. // BluetoothGattCharacteristicClient is used to communicate with remote GATT
  18. // characteristic objects exposed by the Bluetooth daemon.
  19. class DEVICE_BLUETOOTH_EXPORT BluetoothGattCharacteristicClient
  20. : public BluezDBusClient {
  21. public:
  22. // Structure of properties associated with GATT characteristics.
  23. struct Properties : public dbus::PropertySet {
  24. // The 128-bit characteristic UUID. [read-only]
  25. dbus::Property<std::string> uuid;
  26. // Object path of the GATT service the characteristic belongs to.
  27. // [read-only]
  28. dbus::Property<dbus::ObjectPath> service;
  29. // The cached value of the characteristic. This property gets updated only
  30. // after a successful read request and when a notification or indication is
  31. // received. [read-only]
  32. dbus::Property<std::vector<uint8_t>> value;
  33. // Whether or not this characteristic is currently sending ValueUpdated
  34. // signals. [read-only]
  35. dbus::Property<bool> notifying;
  36. // List of flags representing the GATT "Characteristic Properties bit field"
  37. // and properties read from the GATT "Characteristic Extended Properties"
  38. // descriptor bit field. [read-only, optional]
  39. dbus::Property<std::vector<std::string>> flags;
  40. Properties(dbus::ObjectProxy* object_proxy,
  41. const std::string& interface_name,
  42. const PropertyChangedCallback& callback);
  43. ~Properties() override;
  44. };
  45. // Interface for observing changes from a remote GATT characteristic.
  46. class Observer {
  47. public:
  48. virtual ~Observer() {}
  49. // Called when the GATT characteristic with object path |object_path| is
  50. // added to the system.
  51. virtual void GattCharacteristicAdded(const dbus::ObjectPath& object_path) {}
  52. // Called when the GATT characteristic with object path |object_path| is
  53. // removed from the system.
  54. virtual void GattCharacteristicRemoved(
  55. const dbus::ObjectPath& object_path) {}
  56. // Called when the GATT characteristic with object path |object_path| has a
  57. // change in the value of the property named |property_name|.
  58. virtual void GattCharacteristicPropertyChanged(
  59. const dbus::ObjectPath& object_path,
  60. const std::string& property_name) {}
  61. };
  62. // Callbacks used to report the result of asynchronous methods.
  63. using ErrorCallback =
  64. base::OnceCallback<void(const std::string& error_name,
  65. const std::string& error_message)>;
  66. using ValueCallback = base::OnceCallback<void(
  67. absl::optional<device::BluetoothGattService::GattErrorCode> error_code,
  68. const std::vector<uint8_t>& value)>;
  69. BluetoothGattCharacteristicClient(const BluetoothGattCharacteristicClient&) =
  70. delete;
  71. BluetoothGattCharacteristicClient& operator=(
  72. const BluetoothGattCharacteristicClient&) = delete;
  73. ~BluetoothGattCharacteristicClient() override;
  74. // Adds and removes observers for events on all remote GATT characteristics.
  75. // Check the |object_path| parameter of observer methods to determine which
  76. // GATT characteristic is issuing the event.
  77. virtual void AddObserver(Observer* observer) = 0;
  78. virtual void RemoveObserver(Observer* observer) = 0;
  79. // Returns the list of GATT characteristic object paths known to the system.
  80. virtual std::vector<dbus::ObjectPath> GetCharacteristics() = 0;
  81. // Obtain the properties for the GATT characteristic with object path
  82. // |object_path|. Values should be copied if needed.
  83. virtual Properties* GetProperties(const dbus::ObjectPath& object_path) = 0;
  84. // Issues a request to read the value of GATT characteristic with object path
  85. // |object_path| and returns the value in |callback| on success. On error,
  86. // invokes |error_callback|.
  87. virtual void ReadValue(const dbus::ObjectPath& object_path,
  88. ValueCallback callback,
  89. ErrorCallback error_callback) = 0;
  90. // Issues a request to write the value of GATT characteristic with object path
  91. // |object_path| with value |value| and |type_option|. |type_option| is
  92. // bluetooth_gatt_characteristic::kTypeRequest or kTypeCommand, or "" to omit
  93. // the option. Invokes |callback| on success and |error_callback| on failure.
  94. virtual void WriteValue(const dbus::ObjectPath& object_path,
  95. const std::vector<uint8_t>& value,
  96. base::StringPiece type_option,
  97. base::OnceClosure callback,
  98. ErrorCallback error_callback) = 0;
  99. // Issues a request to prepare write the value of GATT characteristic with
  100. // object path |object_path| with value |value|.
  101. // Invokes |callback| on success and |error_callback| on failure.
  102. virtual void PrepareWriteValue(const dbus::ObjectPath& object_path,
  103. const std::vector<uint8_t>& value,
  104. base::OnceClosure callback,
  105. ErrorCallback error_callback) = 0;
  106. // Starts a notification session from this characteristic with object path
  107. // |object_path| if it supports value notifications or indications. Invokes
  108. // |callback| on success and |error_callback| on failure.
  109. #if BUILDFLAG(IS_CHROMEOS)
  110. virtual void StartNotify(
  111. const dbus::ObjectPath& object_path,
  112. device::BluetoothGattCharacteristic::NotificationType notification_type,
  113. base::OnceClosure callback,
  114. ErrorCallback error_callback) = 0;
  115. #else
  116. virtual void StartNotify(const dbus::ObjectPath& object_path,
  117. base::OnceClosure callback,
  118. ErrorCallback error_callback) = 0;
  119. #endif // BUILDFLAG(IS_CHROMEOS)
  120. // Cancels any previous StartNotify transaction for characteristic with
  121. // object path |object_path|. Invokes |callback| on success and
  122. // |error_callback| on failure.
  123. virtual void StopNotify(const dbus::ObjectPath& object_path,
  124. base::OnceClosure callback,
  125. ErrorCallback error_callback) = 0;
  126. // Creates the instance.
  127. static BluetoothGattCharacteristicClient* Create();
  128. // Constants used to indicate exceptional error conditions.
  129. static const char kNoResponseError[];
  130. static const char kUnknownCharacteristicError[];
  131. protected:
  132. BluetoothGattCharacteristicClient();
  133. };
  134. } // namespace bluez
  135. #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_