bluetooth_gatt_descriptor_client.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_DESCRIPTOR_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_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_service.h"
  14. #include "device/bluetooth/dbus/bluez_dbus_client.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace bluez {
  17. // BluetoothGattDescriptorClient is used to communicate with remote GATT
  18. // characteristic descriptor objects exposed by the Bluetooth daemon.
  19. class DEVICE_BLUETOOTH_EXPORT BluetoothGattDescriptorClient
  20. : public BluezDBusClient {
  21. public:
  22. // Structure of properties associated with GATT descriptors.
  23. struct Properties : public dbus::PropertySet {
  24. // The 128-bit characteristic descriptor UUID. [read-only]
  25. dbus::Property<std::string> uuid;
  26. // Object path of the GATT characteristic the descriptor belongs to.
  27. // [read-only]
  28. dbus::Property<dbus::ObjectPath> characteristic;
  29. // The cached value of the descriptor. This property gets updated only after
  30. // a successful read request. [read-only]
  31. dbus::Property<std::vector<uint8_t>> value;
  32. Properties(dbus::ObjectProxy* object_proxy,
  33. const std::string& interface_name,
  34. const PropertyChangedCallback& callback);
  35. ~Properties() override;
  36. };
  37. // Interface for observing changes from a remote GATT characteristic
  38. // descriptor.
  39. class Observer {
  40. public:
  41. virtual ~Observer() {}
  42. // Called when the GATT descriptor with object path |object_path| is added
  43. // to the system.
  44. virtual void GattDescriptorAdded(const dbus::ObjectPath& object_path) {}
  45. // Called when the GATT descriptor with object path |object_path| is removed
  46. // from the system.
  47. virtual void GattDescriptorRemoved(const dbus::ObjectPath& object_path) {}
  48. // Called when the GATT descriptor with object path |object_path| has a
  49. // change in the value of the property named |property_name|.
  50. virtual void GattDescriptorPropertyChanged(
  51. const dbus::ObjectPath& object_path,
  52. const std::string& property_name) {}
  53. };
  54. // Callbacks used to report the result of asynchronous methods.
  55. using ErrorCallback =
  56. base::OnceCallback<void(const std::string& error_name,
  57. const std::string& error_message)>;
  58. using ValueCallback = base::OnceCallback<void(
  59. absl::optional<device::BluetoothGattService::GattErrorCode> error_code,
  60. const std::vector<uint8_t>& value)>;
  61. BluetoothGattDescriptorClient(const BluetoothGattDescriptorClient&) = delete;
  62. BluetoothGattDescriptorClient& operator=(
  63. const BluetoothGattDescriptorClient&) = delete;
  64. ~BluetoothGattDescriptorClient() override;
  65. // Adds and removes observers for events on all remote GATT descriptors. Check
  66. // the |object_path| parameter of observer methods to determine which GATT
  67. // descriptor is issuing the event.
  68. virtual void AddObserver(Observer* observer) = 0;
  69. virtual void RemoveObserver(Observer* observer) = 0;
  70. // Returns the list of GATT descriptor object paths known to the system.
  71. virtual std::vector<dbus::ObjectPath> GetDescriptors() = 0;
  72. // Obtain the properties for the GATT descriptor with object path
  73. // |object_path|. Values should be copied if needed.
  74. virtual Properties* GetProperties(const dbus::ObjectPath& object_path) = 0;
  75. // Issues a request to read the value of GATT descriptor with object path
  76. // |object_path| and returns the value in |callback| on success. On error,
  77. // invokes |error_callback|.
  78. virtual void ReadValue(const dbus::ObjectPath& object_path,
  79. ValueCallback callback,
  80. ErrorCallback error_callback) = 0;
  81. // Issues a request to write the value of GATT descriptor with object path
  82. // |object_path| with value |value|. Invokes |callback| on success and
  83. // |error_callback| on failure.
  84. virtual void WriteValue(const dbus::ObjectPath& object_path,
  85. const std::vector<uint8_t>& value,
  86. base::OnceClosure callback,
  87. ErrorCallback error_callback) = 0;
  88. // Creates the instance.
  89. static BluetoothGattDescriptorClient* Create();
  90. // Constants used to indicate exceptional error conditions.
  91. static const char kNoResponseError[];
  92. static const char kUnknownDescriptorError[];
  93. protected:
  94. BluetoothGattDescriptorClient();
  95. };
  96. } // namespace bluez
  97. #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_CLIENT_H_