xref: /openbmc/phosphor-hwmon/mainloop.hpp (revision 47fb49ac)
1e55ef3d8SBrad Bishop #pragma once
2e55ef3d8SBrad Bishop 
39bbe6026SCarol Wang #include "average.hpp"
4043d3230SPatrick Venture #include "hwmonio.hpp"
5043d3230SPatrick Venture #include "interface.hpp"
6043d3230SPatrick Venture #include "sensor.hpp"
7043d3230SPatrick Venture #include "sensorset.hpp"
8043d3230SPatrick Venture #include "sysfs.hpp"
9043d3230SPatrick Venture #include "types.hpp"
10043d3230SPatrick Venture 
119c7b6e06SBrad Bishop #include <sdbusplus/server.hpp>
120dd0c4d7SWilliam A. Kennington III #include <sdeventplus/clock.hpp>
130fe4cb34SWilliam A. Kennington III #include <sdeventplus/event.hpp>
140dd0c4d7SWilliam A. Kennington III #include <sdeventplus/utility/timer.hpp>
15e8771fd4SPatrick Williams 
16e8771fd4SPatrick Williams #include <any>
17e8771fd4SPatrick Williams #include <future>
18e8771fd4SPatrick Williams #include <memory>
19e8771fd4SPatrick Williams #include <optional>
20043d3230SPatrick Venture #include <string>
21043d3230SPatrick Venture #include <vector>
22075f7a2dSBrad Bishop 
23ab10f164SPatrick Venture static constexpr auto default_interval = 1000000;
24ab10f164SPatrick Venture 
25979c806dSMatthew Barth static constexpr auto sensorID = 0;
26979c806dSMatthew Barth static constexpr auto sensorLabel = 1;
27c9d61613SGeorge Liu static constexpr auto sensorAccuracy = 2;
28*47fb49acSLakshmi Yadlapati static constexpr auto sensorPriority = 3;
29*47fb49acSLakshmi Yadlapati using SensorIdentifiers =
30*47fb49acSLakshmi Yadlapati     std::tuple<std::string, std::string, std::string, std::string>;
31979c806dSMatthew Barth 
32d499ca64SBrad Bishop /** @class MainLoop
33d499ca64SBrad Bishop  *  @brief hwmon-readd main application loop.
34d499ca64SBrad Bishop  */
35d499ca64SBrad Bishop class MainLoop
36d499ca64SBrad Bishop {
37d499ca64SBrad Bishop   public:
38d499ca64SBrad Bishop     MainLoop() = delete;
39d499ca64SBrad Bishop     MainLoop(const MainLoop&) = delete;
40d499ca64SBrad Bishop     MainLoop& operator=(const MainLoop&) = delete;
410dd0c4d7SWilliam A. Kennington III     MainLoop(MainLoop&&) = delete;
420dd0c4d7SWilliam A. Kennington III     MainLoop& operator=(MainLoop&&) = delete;
43d499ca64SBrad Bishop     ~MainLoop() = default;
44d499ca64SBrad Bishop 
45d499ca64SBrad Bishop     /** @brief Constructor
46d499ca64SBrad Bishop      *
479c7b6e06SBrad Bishop      *  @param[in] bus - sdbusplus bus client connection.
48c897d8bbSPatrick Venture      *  @param[in] param - the path parameter provided
49d499ca64SBrad Bishop      *  @param[in] path - hwmon sysfs instance to manage
50f3aa9aefSBrad Bishop      *  @param[in] devPath - physical device sysfs path.
51b9e2b07eSBrad Bishop      *  @param[in] prefix - DBus busname prefix.
52b9e2b07eSBrad Bishop      *  @param[in] root - DBus sensors namespace root.
53d46d0818SAnton D. Kachalov      *  @param[in] instanceId - override value to identify instance on d-bus.
54b9e2b07eSBrad Bishop      *
55b9e2b07eSBrad Bishop      *  Any DBus objects are created relative to the DBus
56b9e2b07eSBrad Bishop      *  sensors namespace root.
57b9e2b07eSBrad Bishop      *
58b9e2b07eSBrad Bishop      *  At startup, the application will own a busname with
59b9e2b07eSBrad Bishop      *  the format <prefix>.hwmon<n>.
60d499ca64SBrad Bishop      */
61ad6043f6SPatrick Williams     MainLoop(sdbusplus::bus_t&& bus, const std::string& param,
62043d3230SPatrick Venture              const std::string& path, const std::string& devPath,
6316805a6aSPatrick Venture              const char* prefix, const char* root,
64d46d0818SAnton D. Kachalov              const std::string& instanceId,
6516805a6aSPatrick Venture              const hwmonio::HwmonIOInterface* ioIntf);
66d499ca64SBrad Bishop 
672a51a9c9SDeepak Kodihalli     /** @brief Setup polling timer in a sd event loop and attach to D-Bus
682a51a9c9SDeepak Kodihalli      *         event loop.
692a51a9c9SDeepak Kodihalli      */
70d499ca64SBrad Bishop     void run();
71d499ca64SBrad Bishop 
722a51a9c9SDeepak Kodihalli     /** @brief Stop polling timer event loop from another thread.
73d499ca64SBrad Bishop      *
74d499ca64SBrad Bishop      *  Typically only used by testcases.
75d499ca64SBrad Bishop      */
76d499ca64SBrad Bishop     void shutdown() noexcept;
77d499ca64SBrad Bishop 
780cd4f69fSPatrick Venture     /** @brief Remove sensors slated for removal.
790cd4f69fSPatrick Venture      */
800cd4f69fSPatrick Venture     void removeSensors();
810cd4f69fSPatrick Venture 
820cd4f69fSPatrick Venture     /** @brief Attempt to add sensors back that had been removed.
830cd4f69fSPatrick Venture      */
840cd4f69fSPatrick Venture     void addDroppedSensors();
850cd4f69fSPatrick Venture 
86d499ca64SBrad Bishop   private:
87043d3230SPatrick Venture     using mapped_type =
88043d3230SPatrick Venture         std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
893c344d3aSBrad Bishop     using SensorState = std::map<SensorSet::key_type, mapped_type>;
903c344d3aSBrad Bishop 
912a51a9c9SDeepak Kodihalli     /** @brief Read hwmon sysfs entries */
922a51a9c9SDeepak Kodihalli     void read();
932a51a9c9SDeepak Kodihalli 
942a51a9c9SDeepak Kodihalli     /** @brief Set up D-Bus object state */
952a51a9c9SDeepak Kodihalli     void init();
962a51a9c9SDeepak Kodihalli 
979c7b6e06SBrad Bishop     /** @brief sdbusplus bus client connection. */
98ad6043f6SPatrick Williams     sdbusplus::bus_t _bus;
999c7b6e06SBrad Bishop     /** @brief sdbusplus freedesktop.ObjectManager storage. */
100ad6043f6SPatrick Williams     sdbusplus::server::manager_t _manager;
101c897d8bbSPatrick Venture     /** @brief the parameter path used. */
102c897d8bbSPatrick Venture     std::string _pathParam;
103b8740fc7SBrad Bishop     /** @brief hwmon sysfs class path. */
104b8740fc7SBrad Bishop     std::string _hwmonRoot;
105b8740fc7SBrad Bishop     /** @brief hwmon sysfs instance. */
106b8740fc7SBrad Bishop     std::string _instance;
107f3aa9aefSBrad Bishop     /** @brief physical device sysfs path. */
108f3aa9aefSBrad Bishop     std::string _devPath;
109b9e2b07eSBrad Bishop     /** @brief DBus busname prefix. */
110b9e2b07eSBrad Bishop     const char* _prefix;
111b9e2b07eSBrad Bishop     /** @brief DBus sensors namespace root. */
112b9e2b07eSBrad Bishop     const char* _root;
1133c344d3aSBrad Bishop     /** @brief DBus object state. */
11452b40613SPatrick Venture     SensorState _state;
115d46d0818SAnton D. Kachalov     /** @brief DBus instance id specified by command line. */
116d46d0818SAnton D. Kachalov     std::string _instanceId;
117ab10f164SPatrick Venture     /** @brief Sleep interval in microseconds. */
118ab10f164SPatrick Venture     uint64_t _interval = default_interval;
119751043e9SBrad Bishop     /** @brief Hwmon sysfs access. */
12016805a6aSPatrick Venture     const hwmonio::HwmonIOInterface* _ioAccess;
1210fe4cb34SWilliam A. Kennington III     /** @brief the Event Loop structure */
12252b40613SPatrick Venture     sdeventplus::Event _event;
1230dd0c4d7SWilliam A. Kennington III     /** @brief Read Timer */
12452b40613SPatrick Venture     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
1259c43106cSMatthew Barth     /** @brief Store the specifications of sensor objects */
126043d3230SPatrick Venture     std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
12752b40613SPatrick Venture         _sensorObjects;
1286d50c3e9SBrandon Kim     /** @brief Store the async futures of timed out sensor objects */
1296d50c3e9SBrandon Kim     sensor::TimedoutMap _timedoutMap;
13031d214ccSMatthew Barth 
13131d214ccSMatthew Barth     /**
13231d214ccSMatthew Barth      * @brief Map of removed sensors
13331d214ccSMatthew Barth      */
13452b40613SPatrick Venture     std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors;
13531d214ccSMatthew Barth 
1369bbe6026SCarol Wang     /** @brief Object of class Average, to handle with average related process
1379bbe6026SCarol Wang      */
1389bbe6026SCarol Wang     Average _average;
1399bbe6026SCarol Wang 
14031d214ccSMatthew Barth     /**
141979c806dSMatthew Barth      * @brief Get the ID of the sensor
142979c806dSMatthew Barth      *
143979c806dSMatthew Barth      * @param[in] sensor - Sensor to get the ID of
144979c806dSMatthew Barth      */
145979c806dSMatthew Barth     std::string getID(SensorSet::container_t::const_reference sensor);
146979c806dSMatthew Barth 
147979c806dSMatthew Barth     /**
148979c806dSMatthew Barth      * @brief Get the sensor identifiers
149979c806dSMatthew Barth      *
150979c806dSMatthew Barth      * @param[in] sensor - Sensor to get the identifiers of
151979c806dSMatthew Barth      */
152043d3230SPatrick Venture     SensorIdentifiers
153043d3230SPatrick Venture         getIdentifiers(SensorSet::container_t::const_reference sensor);
154979c806dSMatthew Barth 
155979c806dSMatthew Barth     /**
15631d214ccSMatthew Barth      * @brief Used to create and add sensor objects
15731d214ccSMatthew Barth      *
15831d214ccSMatthew Barth      * @param[in] sensor - Sensor to create/add object for
159d238e232SMatthew Barth      *
160d238e232SMatthew Barth      * @return - Optional
161d238e232SMatthew Barth      *     Object state data on success, nothing on failure
16231d214ccSMatthew Barth      */
1634cbdfef2SWilliam A. Kennington III     std::optional<ObjectStateData>
164043d3230SPatrick Venture         getObject(SensorSet::container_t::const_reference sensor);
165d499ca64SBrad Bishop };
166feb744a7SPatrick Venture 
167feb744a7SPatrick Venture /** @brief Given a value and map of interfaces, update values and check
168feb744a7SPatrick Venture  * thresholds.
169feb744a7SPatrick Venture  */
170ecac0ae2SMatt Spinler void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value);
171