xref: /openbmc/telemetry/src/sensor.hpp (revision 7e098e93ef0974739459d296f99ddfab54722c23)
1 #pragma once
2 
3 #include "interfaces/sensor.hpp"
4 #include "interfaces/sensor_listener.hpp"
5 #include "types/milliseconds.hpp"
6 #include "utils/unique_call.hpp"
7 
8 #include <boost/asio/high_resolution_timer.hpp>
9 #include <sdbusplus/asio/connection.hpp>
10 
11 #include <memory>
12 
13 class Sensor final :
14     public interfaces::Sensor,
15     public std::enable_shared_from_this<Sensor>
16 {
17     using ValueVariant = std::variant<std::monostate, double>;
18 
19   public:
20     Sensor(interfaces::Sensor::Id sensorId, boost::asio::io_context& ioc,
21            const std::shared_ptr<sdbusplus::asio::connection>& bus);
22 
23     Sensor(const Sensor&) = delete;
24     Sensor& operator=(const Sensor&) = delete;
25 
26     static Id makeId(std::string_view service, std::string_view path);
27 
28     Id id() const override;
29     void registerForUpdates(
30         const std::weak_ptr<interfaces::SensorListener>& weakListener) override;
31     void unregisterFromUpdates(
32         const std::weak_ptr<interfaces::SensorListener>& weakListener) override;
33 
34   private:
35     static std::optional<double> readValue(const ValueVariant& v);
36     static void signalProc(const std::weak_ptr<Sensor>& weakSelf,
37                            sdbusplus::message::message&);
38 
39     void async_read();
40     void async_read(std::shared_ptr<utils::UniqueCall::Lock>);
41     void makeSignalMonitor();
42     void updateValue(double);
43 
44     interfaces::Sensor::Id sensorId;
45     boost::asio::io_context& ioc;
46     std::shared_ptr<sdbusplus::asio::connection> bus;
47     Milliseconds timerInterval = Milliseconds(0);
48     std::optional<boost::asio::high_resolution_timer> timer;
49 
50     utils::UniqueCall uniqueCall;
51     std::vector<std::weak_ptr<interfaces::SensorListener>> listeners;
52     uint64_t timestamp = 0;
53     std::optional<double> value;
54     std::unique_ptr<sdbusplus::bus::match::match> signalMonitor;
55 };
56