bluetooth_gatt_descriptor_service_provider_impl.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2016 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_SERVICE_PROVIDER_IMPL_H_
  5. #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_SERVICE_PROVIDER_IMPL_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/threading/platform_thread.h"
  13. #include "dbus/bus.h"
  14. #include "dbus/exported_object.h"
  15. #include "dbus/message.h"
  16. #include "dbus/object_path.h"
  17. #include "device/bluetooth/dbus/bluetooth_gatt_attribute_value_delegate.h"
  18. #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_service_provider.h"
  19. namespace bluez {
  20. // The BluetoothGattDescriptorServiceProvider implementation used in production.
  21. class DEVICE_BLUETOOTH_EXPORT BluetoothGattDescriptorServiceProviderImpl
  22. : public BluetoothGattDescriptorServiceProvider {
  23. public:
  24. // Use nullptr for |bus| to create for testing.
  25. BluetoothGattDescriptorServiceProviderImpl(
  26. dbus::Bus* bus,
  27. const dbus::ObjectPath& object_path,
  28. std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate,
  29. const std::string& uuid,
  30. const std::vector<std::string>& flags,
  31. const dbus::ObjectPath& characteristic_path);
  32. BluetoothGattDescriptorServiceProviderImpl(
  33. const BluetoothGattDescriptorServiceProviderImpl&) = delete;
  34. BluetoothGattDescriptorServiceProviderImpl& operator=(
  35. const BluetoothGattDescriptorServiceProviderImpl&) = delete;
  36. ~BluetoothGattDescriptorServiceProviderImpl() override;
  37. // BluetoothGattDescriptorServiceProvider override.
  38. void SendValueChanged(const std::vector<uint8_t>& value) override;
  39. private:
  40. // Returns true if the current thread is on the origin thread.
  41. bool OnOriginThread();
  42. // Called by dbus:: when the Bluetooth daemon fetches a single property of
  43. // the descriptor.
  44. void Get(dbus::MethodCall* method_call,
  45. dbus::ExportedObject::ResponseSender response_sender);
  46. // Called by dbus:: when the Bluetooth daemon sets a single property of the
  47. // descriptor.
  48. void Set(dbus::MethodCall* method_call,
  49. dbus::ExportedObject::ResponseSender response_sender);
  50. // Called by dbus:: when the Bluetooth daemon fetches all properties of the
  51. // descriptor.
  52. void GetAll(dbus::MethodCall* method_call,
  53. dbus::ExportedObject::ResponseSender response_sender);
  54. // Called by BlueZ when a remote central is requesting to read the value of
  55. // this descriptor.
  56. void ReadValue(dbus::MethodCall* method_call,
  57. dbus::ExportedObject::ResponseSender response_sender);
  58. // Called by BlueZ when a remote central is requesting to write the value of
  59. // this descriptor.
  60. void WriteValue(dbus::MethodCall* method_call,
  61. dbus::ExportedObject::ResponseSender response_sender);
  62. // Called by dbus:: when a method is exported.
  63. void OnExported(const std::string& interface_name,
  64. const std::string& method_name,
  65. bool success);
  66. // Writes the descriptor's properties into the provided writer.
  67. void WriteProperties(dbus::MessageWriter* writer) override;
  68. // Called by the Delegate in response to a method to call to read the value
  69. // of this descriptor.
  70. void OnReadValue(
  71. dbus::MethodCall* method_call,
  72. dbus::ExportedObject::ResponseSender response_sender,
  73. absl::optional<device::BluetoothGattService::GattErrorCode> error_code,
  74. const std::vector<uint8_t>& value);
  75. // Called by the Delegate in response to a method to call to write the value
  76. // of this descriptor.
  77. void OnWriteValue(dbus::MethodCall* method_call,
  78. dbus::ExportedObject::ResponseSender response_sender);
  79. // Called by the Delegate in response to a failed method call to set
  80. // the descriptor value.
  81. void OnWriteFailure(dbus::MethodCall* method_call,
  82. dbus::ExportedObject::ResponseSender response_sender);
  83. const dbus::ObjectPath& object_path() const override;
  84. // Origin thread (i.e. the UI thread in production).
  85. base::PlatformThreadId origin_thread_id_;
  86. // 128-bit descriptor UUID of this object.
  87. std::string uuid_;
  88. // Permissions for this descriptor.
  89. std::vector<std::string> flags_;
  90. // D-Bus bus object is exported on, not owned by this object and must
  91. // outlive it.
  92. raw_ptr<dbus::Bus> bus_;
  93. // Incoming methods to get and set the "Value" property are passed on to the
  94. // delegate and callbacks passed to generate a reply. |delegate_| is generally
  95. // the object that owns this one and must outlive it.
  96. std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate_;
  97. // D-Bus object path of object we are exporting, kept so we can unregister
  98. // again in our destructor.
  99. dbus::ObjectPath object_path_;
  100. // Object path of the GATT characteristic that the exported descriptor belongs
  101. // to.
  102. dbus::ObjectPath characteristic_path_;
  103. // D-Bus object we are exporting, owned by this object.
  104. scoped_refptr<dbus::ExportedObject> exported_object_;
  105. // Weak pointer factory for generating 'this' pointers that might live longer
  106. // than we do.
  107. // Note: This should remain the last member so it'll be destroyed and
  108. // invalidate its weak pointers before any other members are destroyed.
  109. base::WeakPtrFactory<BluetoothGattDescriptorServiceProviderImpl>
  110. weak_ptr_factory_{this};
  111. };
  112. } // namespace bluez
  113. #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_SERVICE_PROVIDER_IMPL_H_