Implemented sensor class
Sensor class was introduced, it monitors
xyz.openbmc_project.Sensor.Value, for change and notifies all
listeners.
Tested:
- Unit tested with service stub that provides dbus interface
xyz.openbmc_project.Sensor.Value
- All changes are delivered to listeners
- All other unit tests are passing
Change-Id: I8c9d58cc986c1fe2a4d2386815d559814016efa6
Signed-off-by: Krzysztof Grobelny <krzysztof.grobelny@intel.com>
diff --git a/src/sensor.hpp b/src/sensor.hpp
new file mode 100644
index 0000000..1374c54
--- /dev/null
+++ b/src/sensor.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include "interfaces/sensor.hpp"
+#include "interfaces/sensor_listener.hpp"
+#include "utils/unique_call.hpp"
+
+#include <boost/asio/high_resolution_timer.hpp>
+#include <sdbusplus/asio/connection.hpp>
+
+#include <memory>
+
+class Sensor final :
+ public interfaces::Sensor,
+ public std::enable_shared_from_this<Sensor>
+{
+ using ValueVariant = std::variant<std::monostate, double>;
+
+ public:
+ Sensor(interfaces::Sensor::Id sensorId, boost::asio::io_context& ioc,
+ const std::shared_ptr<sdbusplus::asio::connection>& bus);
+
+ Sensor(const Sensor&) = delete;
+ Sensor& operator=(const Sensor&) = delete;
+
+ static Id makeId(std::string_view service, std::string_view path);
+
+ Id id() const override;
+ void registerForUpdates(
+ const std::weak_ptr<interfaces::SensorListener>& weakListener) override;
+
+ private:
+ static std::optional<double> readValue(const ValueVariant& v);
+ static void signalProc(const std::weak_ptr<Sensor>& weakSelf,
+ sdbusplus::message::message&);
+
+ void async_read();
+ void async_read(std::shared_ptr<utils::UniqueCall::Lock>);
+ void makeSignalMonitor();
+ void updateValue(double);
+
+ interfaces::Sensor::Id sensorId;
+ boost::asio::io_context& ioc;
+ std::shared_ptr<sdbusplus::asio::connection> bus;
+ std::chrono::milliseconds timerInterval = std::chrono::milliseconds(0);
+ std::optional<boost::asio::high_resolution_timer> timer;
+
+ utils::UniqueCall uniqueCall;
+ std::vector<std::weak_ptr<interfaces::SensorListener>> listeners;
+ uint64_t timestamp = 0;
+ std::optional<double> value;
+ std::unique_ptr<sdbusplus::bus::match::match> signalMonitor;
+};