fake_bluetooth_gatt_characteristic_client.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
  5. #define DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "dbus/object_path.h"
  14. #include "dbus/property.h"
  15. #include "device/bluetooth/bluetooth_export.h"
  16. #include "device/bluetooth/bluetooth_gatt_characteristic.h"
  17. #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_client.h"
  18. namespace bluez {
  19. // FakeBluetoothGattCharacteristicClient simulates the behavior of the
  20. // Bluetooth Daemon GATT characteristic objects and is used in test cases in
  21. // place of a mock and on the Linux desktop.
  22. class DEVICE_BLUETOOTH_EXPORT FakeBluetoothGattCharacteristicClient
  23. : public BluetoothGattCharacteristicClient {
  24. public:
  25. struct Properties : public BluetoothGattCharacteristicClient::Properties {
  26. explicit Properties(const PropertyChangedCallback& callback);
  27. ~Properties() override;
  28. // dbus::PropertySet override
  29. void Get(dbus::PropertyBase* property,
  30. dbus::PropertySet::GetCallback callback) override;
  31. void GetAll() override;
  32. void Set(dbus::PropertyBase* property,
  33. dbus::PropertySet::SetCallback callback) override;
  34. };
  35. FakeBluetoothGattCharacteristicClient();
  36. FakeBluetoothGattCharacteristicClient(
  37. const FakeBluetoothGattCharacteristicClient&) = delete;
  38. FakeBluetoothGattCharacteristicClient& operator=(
  39. const FakeBluetoothGattCharacteristicClient&) = delete;
  40. ~FakeBluetoothGattCharacteristicClient() override;
  41. // DBusClient override.
  42. void Init(dbus::Bus* bus, const std::string& bluetooth_service_name) override;
  43. // BluetoothGattCharacteristicClient overrides.
  44. void AddObserver(Observer* observer) override;
  45. void RemoveObserver(Observer* observer) override;
  46. std::vector<dbus::ObjectPath> GetCharacteristics() override;
  47. Properties* GetProperties(const dbus::ObjectPath& object_path) override;
  48. void ReadValue(const dbus::ObjectPath& object_path,
  49. ValueCallback callback,
  50. ErrorCallback error_callback) override;
  51. void WriteValue(const dbus::ObjectPath& object_path,
  52. const std::vector<uint8_t>& value,
  53. base::StringPiece type_option,
  54. base::OnceClosure callback,
  55. ErrorCallback error_callback) override;
  56. void PrepareWriteValue(const dbus::ObjectPath& object_path,
  57. const std::vector<uint8_t>& value,
  58. base::OnceClosure callback,
  59. ErrorCallback error_callback) override;
  60. #if BUILDFLAG(IS_CHROMEOS)
  61. void StartNotify(
  62. const dbus::ObjectPath& object_path,
  63. device::BluetoothGattCharacteristic::NotificationType notification_type,
  64. base::OnceClosure callback,
  65. ErrorCallback error_callback) override;
  66. #else
  67. void StartNotify(const dbus::ObjectPath& object_path,
  68. base::OnceClosure callback,
  69. ErrorCallback error_callback) override;
  70. #endif // BUILDFLAG(IS_CHROMEOS)
  71. void StopNotify(const dbus::ObjectPath& object_path,
  72. base::OnceClosure callback,
  73. ErrorCallback error_callback) override;
  74. // Makes the group of characteristics belonging to a particular GATT based
  75. // profile available under the GATT service with object path |service_path|.
  76. // Characteristic paths are hierarchical to service paths.
  77. void ExposeHeartRateCharacteristics(const dbus::ObjectPath& service_path);
  78. void HideHeartRateCharacteristics();
  79. // Returns whether or not the heart rate characteristics are visible and
  80. // performs the appropriate assertions.
  81. bool IsHeartRateVisible() const;
  82. // Makes this characteristic client really slow.
  83. // So slow, that it is guaranteed that |requests| requests will
  84. // come in while the client is doing the previous request.
  85. // Setting |requests| to zero will cause all delayed actions to
  86. // complete immediately.
  87. void SetExtraProcessing(size_t requests);
  88. size_t GetExtraProcessing() const;
  89. // Sets whether the client is authorized or not.
  90. // Defaults to authorized.
  91. void SetAuthorized(bool authorized) { authorized_ = authorized; }
  92. // Get the current Authorization state.
  93. bool IsAuthorized() const { return authorized_; }
  94. // Whether the client is Authenticated
  95. // Defaults to authenticated.
  96. void SetAuthenticated(bool authenticated) { authenticated_ = authenticated; }
  97. // Get the current Authenticated state.
  98. bool IsAuthenticated() const { return authenticated_; }
  99. // Returns the current object paths of exposed characteristics. If the
  100. // characteristic is not visible, returns an invalid empty path.
  101. dbus::ObjectPath GetHeartRateMeasurementPath() const;
  102. dbus::ObjectPath GetBodySensorLocationPath() const;
  103. dbus::ObjectPath GetHeartRateControlPointPath() const;
  104. // Object path components and UUIDs of GATT characteristics.
  105. // Heart Rate Service:
  106. static const char kHeartRateMeasurementPathComponent[];
  107. static const char kHeartRateMeasurementUUID[];
  108. static const char kBodySensorLocationPathComponent[];
  109. static const char kBodySensorLocationUUID[];
  110. static const char kHeartRateControlPointPathComponent[];
  111. static const char kHeartRateControlPointUUID[];
  112. private:
  113. // Property callback passed when we create Properties structures.
  114. void OnPropertyChanged(const dbus::ObjectPath& object_path,
  115. const std::string& property_name);
  116. // Notifies observers.
  117. void NotifyCharacteristicAdded(const dbus::ObjectPath& object_path);
  118. void NotifyCharacteristicRemoved(const dbus::ObjectPath& object_path);
  119. // Schedules a heart rate measurement value change, if the heart rate
  120. // characteristics are visible.
  121. void ScheduleHeartRateMeasurementValueChange();
  122. // Returns a random Heart Rate Measurement value. All the fields of the value
  123. // are populated according to the the fake behavior. The measurement value
  124. // is a random value within a reasonable range.
  125. std::vector<uint8_t> GetHeartRateMeasurementValue();
  126. // Callback that executes a delayed ReadValue action by updating the
  127. // appropriate "Value" property and invoking the ValueCallback.
  128. void DelayedReadValueCallback(const dbus::ObjectPath& object_path,
  129. ValueCallback callback,
  130. const std::vector<uint8_t>& value);
  131. // If true, characteristics of the Heart Rate Service are visible. Use
  132. // IsHeartRateVisible() to check the value.
  133. bool heart_rate_visible_;
  134. // If true, the client is authorized to read and write.
  135. bool authorized_;
  136. // If true, the client is authenticated.
  137. bool authenticated_;
  138. // Total calories burned, used for the Heart Rate Measurement characteristic.
  139. uint16_t calories_burned_;
  140. // Static properties returned for simulated characteristics for the Heart
  141. // Rate Service. These pointers are not NULL only if the characteristics are
  142. // actually exposed.
  143. std::unique_ptr<Properties> heart_rate_measurement_properties_;
  144. std::unique_ptr<Properties> body_sensor_location_properties_;
  145. std::unique_ptr<Properties> heart_rate_control_point_properties_;
  146. // Object paths of the exposed characteristics. If a characteristic is not
  147. // exposed, these will be empty.
  148. std::string heart_rate_measurement_path_;
  149. std::string heart_rate_measurement_ccc_desc_path_;
  150. std::string body_sensor_location_path_;
  151. std::string heart_rate_control_point_path_;
  152. // Number of extra requests that need to come in simulating slowness.
  153. size_t extra_requests_;
  154. // Current countdowns for extra requests for various actions.
  155. struct DelayedCallback {
  156. public:
  157. DelayedCallback(base::OnceClosure callback, size_t delay);
  158. ~DelayedCallback();
  159. base::OnceClosure callback_;
  160. size_t delay_;
  161. };
  162. // Map of delayed callbacks.
  163. std::map<std::string, DelayedCallback*> action_extra_requests_;
  164. // List of observers interested in event notifications from us.
  165. base::ObserverList<Observer>::Unchecked observers_;
  166. // Weak pointer factory for generating 'this' pointers that might live longer
  167. // than we do.
  168. // Note: This should remain the last member so it'll be destroyed and
  169. // invalidate its weak pointers before any other members are destroyed.
  170. base::WeakPtrFactory<FakeBluetoothGattCharacteristicClient> weak_ptr_factory_{
  171. this};
  172. };
  173. } // namespace bluez
  174. #endif // DEVICE_BLUETOOTH_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_