bluetooth_gatt_characteristic_service_provider_impl.h 6.9 KB

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