xref: /openbmc/phosphor-hwmon/mainloop.hpp (revision e8771fd4)
1 #pragma once
2 
3 #include "average.hpp"
4 #include "hwmonio.hpp"
5 #include "interface.hpp"
6 #include "sensor.hpp"
7 #include "sensorset.hpp"
8 #include "sysfs.hpp"
9 #include "types.hpp"
10 
11 #include <sdbusplus/server.hpp>
12 #include <sdeventplus/clock.hpp>
13 #include <sdeventplus/event.hpp>
14 #include <sdeventplus/utility/timer.hpp>
15 
16 #include <any>
17 #include <future>
18 #include <memory>
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 static constexpr auto default_interval = 1000000;
24 
25 static constexpr auto sensorID = 0;
26 static constexpr auto sensorLabel = 1;
27 static constexpr auto sensorAccuracy = 2;
28 using SensorIdentifiers = std::tuple<std::string, std::string, std::string>;
29 
30 /** @class MainLoop
31  *  @brief hwmon-readd main application loop.
32  */
33 class MainLoop
34 {
35   public:
36     MainLoop() = delete;
37     MainLoop(const MainLoop&) = delete;
38     MainLoop& operator=(const MainLoop&) = delete;
39     MainLoop(MainLoop&&) = delete;
40     MainLoop& operator=(MainLoop&&) = delete;
41     ~MainLoop() = default;
42 
43     /** @brief Constructor
44      *
45      *  @param[in] bus - sdbusplus bus client connection.
46      *  @param[in] param - the path parameter provided
47      *  @param[in] path - hwmon sysfs instance to manage
48      *  @param[in] devPath - physical device sysfs path.
49      *  @param[in] prefix - DBus busname prefix.
50      *  @param[in] root - DBus sensors namespace root.
51      *  @param[in] instanceId - override value to identify instance on d-bus.
52      *
53      *  Any DBus objects are created relative to the DBus
54      *  sensors namespace root.
55      *
56      *  At startup, the application will own a busname with
57      *  the format <prefix>.hwmon<n>.
58      */
59     MainLoop(sdbusplus::bus_t&& bus, const std::string& param,
60              const std::string& path, const std::string& devPath,
61              const char* prefix, const char* root,
62              const std::string& instanceId,
63              const hwmonio::HwmonIOInterface* ioIntf);
64 
65     /** @brief Setup polling timer in a sd event loop and attach to D-Bus
66      *         event loop.
67      */
68     void run();
69 
70     /** @brief Stop polling timer event loop from another thread.
71      *
72      *  Typically only used by testcases.
73      */
74     void shutdown() noexcept;
75 
76     /** @brief Remove sensors slated for removal.
77      */
78     void removeSensors();
79 
80     /** @brief Attempt to add sensors back that had been removed.
81      */
82     void addDroppedSensors();
83 
84   private:
85     using mapped_type =
86         std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
87     using SensorState = std::map<SensorSet::key_type, mapped_type>;
88 
89     /** @brief Read hwmon sysfs entries */
90     void read();
91 
92     /** @brief Set up D-Bus object state */
93     void init();
94 
95     /** @brief sdbusplus bus client connection. */
96     sdbusplus::bus_t _bus;
97     /** @brief sdbusplus freedesktop.ObjectManager storage. */
98     sdbusplus::server::manager_t _manager;
99     /** @brief the parameter path used. */
100     std::string _pathParam;
101     /** @brief hwmon sysfs class path. */
102     std::string _hwmonRoot;
103     /** @brief hwmon sysfs instance. */
104     std::string _instance;
105     /** @brief physical device sysfs path. */
106     std::string _devPath;
107     /** @brief DBus busname prefix. */
108     const char* _prefix;
109     /** @brief DBus sensors namespace root. */
110     const char* _root;
111     /** @brief DBus object state. */
112     SensorState _state;
113     /** @brief DBus instance id specified by command line. */
114     std::string _instanceId;
115     /** @brief Sleep interval in microseconds. */
116     uint64_t _interval = default_interval;
117     /** @brief Hwmon sysfs access. */
118     const hwmonio::HwmonIOInterface* _ioAccess;
119     /** @brief the Event Loop structure */
120     sdeventplus::Event _event;
121     /** @brief Read Timer */
122     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
123     /** @brief Store the specifications of sensor objects */
124     std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
125         _sensorObjects;
126     /** @brief Store the async futures of timed out sensor objects */
127     sensor::TimedoutMap _timedoutMap;
128 
129     /**
130      * @brief Map of removed sensors
131      */
132     std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors;
133 
134     /** @brief Object of class Average, to handle with average related process
135      */
136     Average _average;
137 
138     /**
139      * @brief Get the ID of the sensor
140      *
141      * @param[in] sensor - Sensor to get the ID of
142      */
143     std::string getID(SensorSet::container_t::const_reference sensor);
144 
145     /**
146      * @brief Get the sensor identifiers
147      *
148      * @param[in] sensor - Sensor to get the identifiers of
149      */
150     SensorIdentifiers
151         getIdentifiers(SensorSet::container_t::const_reference sensor);
152 
153     /**
154      * @brief Used to create and add sensor objects
155      *
156      * @param[in] sensor - Sensor to create/add object for
157      *
158      * @return - Optional
159      *     Object state data on success, nothing on failure
160      */
161     std::optional<ObjectStateData>
162         getObject(SensorSet::container_t::const_reference sensor);
163 };
164 
165 /** @brief Given a value and map of interfaces, update values and check
166  * thresholds.
167  */
168 void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value);
169