fake_sensor_device.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2020 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 CHROMEOS_COMPONENTS_SENSORS_FAKE_SENSOR_DEVICE_H_
  5. #define CHROMEOS_COMPONENTS_SENSORS_FAKE_SENSOR_DEVICE_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/component_export.h"
  10. #include "base/sequence_checker.h"
  11. #include "chromeos/components/sensors/mojom/sensor.mojom.h"
  12. #include "mojo/public/cpp/bindings/receiver_set.h"
  13. #include "mojo/public/cpp/bindings/remote.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace chromeos {
  16. namespace sensors {
  17. class FakeSensorDevice final : public mojom::SensorDevice {
  18. public:
  19. struct ChannelData {
  20. ChannelData();
  21. ChannelData(const ChannelData&);
  22. ChannelData& operator=(const ChannelData&);
  23. ~ChannelData();
  24. std::string id;
  25. std::map<std::string, std::string> attrs;
  26. int64_t sample_data;
  27. };
  28. explicit FakeSensorDevice(const std::vector<ChannelData>& channels);
  29. FakeSensorDevice(const FakeSensorDevice&) = delete;
  30. FakeSensorDevice& operator=(const FakeSensorDevice&) = delete;
  31. ~FakeSensorDevice() override;
  32. mojo::ReceiverId AddReceiver(
  33. mojo::PendingReceiver<mojom::SensorDevice> pending_receiver);
  34. void RemoveReceiver(mojo::ReceiverId id);
  35. void RemoveReceiverWithReason(mojo::ReceiverId id,
  36. mojom::SensorDeviceDisconnectReason reason,
  37. const std::string& description);
  38. void ClearReceivers();
  39. void ClearReceiversWithReason(mojom::SensorDeviceDisconnectReason reason,
  40. const std::string& description);
  41. bool HasReceivers() const;
  42. size_t SizeOfReceivers() const;
  43. void SetAttribute(const std::string& attr_name,
  44. const std::string& attr_value);
  45. void ResetObserverRemote(mojo::ReceiverId id);
  46. void ResetObserverRemoteWithReason(mojo::ReceiverId id,
  47. mojom::SensorDeviceDisconnectReason reason,
  48. const std::string& description);
  49. // Unlike SetChannelsEnabled() below, SetChannelsEnabledWithId() is used
  50. // without a mojo pipe, instead, with the mojo::ReceiverId from AddReceiver().
  51. // It lets the tests manually enable or disable channels to simulate some
  52. // unexpected behaviors of iioservice, such as channels being unavailable
  53. // suddenly.
  54. void SetChannelsEnabledWithId(mojo::ReceiverId id,
  55. const std::vector<int32_t>& iio_chn_indices,
  56. bool en);
  57. // Implementation of mojom::SensorDevice.
  58. void SetTimeout(uint32_t timeout) override {}
  59. void GetAttributes(const std::vector<std::string>& attr_names,
  60. GetAttributesCallback callback) override;
  61. void SetFrequency(double frequency, SetFrequencyCallback callback) override;
  62. void StartReadingSamples(
  63. mojo::PendingRemote<mojom::SensorDeviceSamplesObserver> observer)
  64. override;
  65. void StopReadingSamples() override;
  66. void GetAllChannelIds(GetAllChannelIdsCallback callback) override;
  67. void SetChannelsEnabled(const std::vector<int32_t>& iio_chn_indices,
  68. bool en,
  69. SetChannelsEnabledCallback callback) override;
  70. void GetChannelsEnabled(const std::vector<int32_t>& iio_chn_indices,
  71. GetChannelsEnabledCallback callback) override;
  72. void GetChannelsAttributes(const std::vector<int32_t>& iio_chn_indices,
  73. const std::string& attr_name,
  74. GetChannelsAttributesCallback callback) override;
  75. private:
  76. struct ClientData {
  77. ClientData();
  78. ~ClientData();
  79. absl::optional<double> frequency;
  80. std::vector<bool> channels_enabled;
  81. mojo::Remote<mojom::SensorDeviceSamplesObserver> observer;
  82. };
  83. void SendSampleIfReady(ClientData& client);
  84. std::map<std::string, std::string> attributes_;
  85. const std::vector<ChannelData> channels_;
  86. mojo::ReceiverSet<mojom::SensorDevice> receiver_set_;
  87. // First is the client's id from |receiver_set_|, second is the client's
  88. // states and observer remote.
  89. std::map<mojo::ReceiverId, ClientData> clients_;
  90. SEQUENCE_CHECKER(sequence_checker_);
  91. };
  92. } // namespace sensors
  93. } // namespace chromeos
  94. #endif // CHROMEOS_COMPONENTS_SENSORS_FAKE_SENSOR_DEVICE_H_