blob: 468223b32db02763d898a43a6deda1e61d961dba [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001#pragma once
2
Carol Wang9bbe6022019-08-01 17:31:30 +08003#include "average.hpp"
Patrick Venture043d3232018-08-31 10:10:53 -07004#include "hwmonio.hpp"
5#include "interface.hpp"
6#include "sensor.hpp"
7#include "sensorset.hpp"
8#include "sysfs.hpp"
Patrick Venture043d3232018-08-31 10:10:53 -07009#include "types.hpp"
10
William A. Kennington III4cbdfef2018-10-18 19:19:51 -070011#include <any>
Brandon Kim6d50c3e2019-08-09 15:38:53 -070012#include <future>
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060013#include <memory>
William A. Kennington III4cbdfef2018-10-18 19:19:51 -070014#include <optional>
Brad Bishop9c7b6e02016-12-19 12:43:36 -050015#include <sdbusplus/server.hpp>
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070016#include <sdeventplus/clock.hpp>
William A. Kennington III0fe4cb32018-10-18 19:19:58 -070017#include <sdeventplus/event.hpp>
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070018#include <sdeventplus/utility/timer.hpp>
Patrick Venture043d3232018-08-31 10:10:53 -070019#include <string>
20#include <vector>
Brad Bishop075f7a22017-01-06 09:45:08 -050021
Patrick Ventureab10f162017-05-22 09:44:50 -070022static constexpr auto default_interval = 1000000;
23
Matthew Barth979c8062018-04-17 11:37:15 -050024static constexpr auto sensorID = 0;
25static constexpr auto sensorLabel = 1;
George Liuc9d61612022-10-12 14:31:39 +080026static constexpr auto sensorAccuracy = 2;
27using SensorIdentifiers = std::tuple<std::string, std::string, std::string>;
Matthew Barth979c8062018-04-17 11:37:15 -050028
Brad Bishopd499ca62016-12-19 09:24:50 -050029/** @class MainLoop
30 * @brief hwmon-readd main application loop.
31 */
32class MainLoop
33{
Patrick Venture043d3232018-08-31 10:10:53 -070034 public:
35 MainLoop() = delete;
36 MainLoop(const MainLoop&) = delete;
37 MainLoop& operator=(const MainLoop&) = delete;
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070038 MainLoop(MainLoop&&) = delete;
39 MainLoop& operator=(MainLoop&&) = delete;
Patrick Venture043d3232018-08-31 10:10:53 -070040 ~MainLoop() = default;
Brad Bishopd499ca62016-12-19 09:24:50 -050041
Patrick Venture043d3232018-08-31 10:10:53 -070042 /** @brief Constructor
43 *
44 * @param[in] bus - sdbusplus bus client connection.
45 * @param[in] param - the path parameter provided
46 * @param[in] path - hwmon sysfs instance to manage
47 * @param[in] devPath - physical device sysfs path.
48 * @param[in] prefix - DBus busname prefix.
49 * @param[in] root - DBus sensors namespace root.
Anton D. Kachalovd46d0812021-02-03 23:25:37 +010050 * @param[in] instanceId - override value to identify instance on d-bus.
Patrick Venture043d3232018-08-31 10:10:53 -070051 *
52 * Any DBus objects are created relative to the DBus
53 * sensors namespace root.
54 *
55 * At startup, the application will own a busname with
56 * the format <prefix>.hwmon<n>.
57 */
Patrick Williamsad6043f2022-07-22 19:26:56 -050058 MainLoop(sdbusplus::bus_t&& bus, const std::string& param,
Patrick Venture043d3232018-08-31 10:10:53 -070059 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -070060 const char* prefix, const char* root,
Anton D. Kachalovd46d0812021-02-03 23:25:37 +010061 const std::string& instanceId,
Patrick Venture16805a62019-06-21 14:19:33 -070062 const hwmonio::HwmonIOInterface* ioIntf);
Brad Bishopd499ca62016-12-19 09:24:50 -050063
Patrick Venture043d3232018-08-31 10:10:53 -070064 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
65 * event loop.
66 */
67 void run();
Brad Bishopd499ca62016-12-19 09:24:50 -050068
Patrick Venture043d3232018-08-31 10:10:53 -070069 /** @brief Stop polling timer event loop from another thread.
70 *
71 * Typically only used by testcases.
72 */
73 void shutdown() noexcept;
Brad Bishopd499ca62016-12-19 09:24:50 -050074
Patrick Venture0cd4f692019-06-21 13:39:40 -070075 /** @brief Remove sensors slated for removal.
76 */
77 void removeSensors();
78
79 /** @brief Attempt to add sensors back that had been removed.
80 */
81 void addDroppedSensors();
82
Patrick Venture043d3232018-08-31 10:10:53 -070083 private:
84 using mapped_type =
85 std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
86 using SensorState = std::map<SensorSet::key_type, mapped_type>;
Brad Bishop3c344d32017-01-05 11:48:39 -050087
Patrick Venture043d3232018-08-31 10:10:53 -070088 /** @brief Read hwmon sysfs entries */
89 void read();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060090
Patrick Venture043d3232018-08-31 10:10:53 -070091 /** @brief Set up D-Bus object state */
92 void init();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060093
Patrick Venture043d3232018-08-31 10:10:53 -070094 /** @brief sdbusplus bus client connection. */
Patrick Williamsad6043f2022-07-22 19:26:56 -050095 sdbusplus::bus_t _bus;
Patrick Venture043d3232018-08-31 10:10:53 -070096 /** @brief sdbusplus freedesktop.ObjectManager storage. */
Patrick Williamsad6043f2022-07-22 19:26:56 -050097 sdbusplus::server::manager_t _manager;
Patrick Venture043d3232018-08-31 10:10:53 -070098 /** @brief the parameter path used. */
99 std::string _pathParam;
100 /** @brief hwmon sysfs class path. */
101 std::string _hwmonRoot;
102 /** @brief hwmon sysfs instance. */
103 std::string _instance;
104 /** @brief physical device sysfs path. */
105 std::string _devPath;
106 /** @brief DBus busname prefix. */
107 const char* _prefix;
108 /** @brief DBus sensors namespace root. */
109 const char* _root;
110 /** @brief DBus object state. */
Patrick Venture52b40612018-12-19 13:36:41 -0800111 SensorState _state;
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100112 /** @brief DBus instance id specified by command line. */
113 std::string _instanceId;
Patrick Venture043d3232018-08-31 10:10:53 -0700114 /** @brief Sleep interval in microseconds. */
115 uint64_t _interval = default_interval;
116 /** @brief Hwmon sysfs access. */
Patrick Venture16805a62019-06-21 14:19:33 -0700117 const hwmonio::HwmonIOInterface* _ioAccess;
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700118 /** @brief the Event Loop structure */
Patrick Venture52b40612018-12-19 13:36:41 -0800119 sdeventplus::Event _event;
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -0700120 /** @brief Read Timer */
Patrick Venture52b40612018-12-19 13:36:41 -0800121 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
Patrick Venture043d3232018-08-31 10:10:53 -0700122 /** @brief Store the specifications of sensor objects */
123 std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
Patrick Venture52b40612018-12-19 13:36:41 -0800124 _sensorObjects;
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700125 /** @brief Store the async futures of timed out sensor objects */
126 sensor::TimedoutMap _timedoutMap;
Matthew Barth31d214c2018-03-26 09:54:27 -0500127
Patrick Venture043d3232018-08-31 10:10:53 -0700128 /**
129 * @brief Map of removed sensors
130 */
Patrick Venture52b40612018-12-19 13:36:41 -0800131 std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors;
Matthew Barth31d214c2018-03-26 09:54:27 -0500132
Carol Wang9bbe6022019-08-01 17:31:30 +0800133 /** @brief Object of class Average, to handle with average related process
134 */
135 Average _average;
136
Patrick Venture043d3232018-08-31 10:10:53 -0700137 /**
138 * @brief Get the ID of the sensor
139 *
140 * @param[in] sensor - Sensor to get the ID of
141 */
142 std::string getID(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500143
Patrick Venture043d3232018-08-31 10:10:53 -0700144 /**
145 * @brief Get the sensor identifiers
146 *
147 * @param[in] sensor - Sensor to get the identifiers of
148 */
149 SensorIdentifiers
150 getIdentifiers(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500151
Patrick Venture043d3232018-08-31 10:10:53 -0700152 /**
153 * @brief Used to create and add sensor objects
154 *
155 * @param[in] sensor - Sensor to create/add object for
156 *
157 * @return - Optional
158 * Object state data on success, nothing on failure
159 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700160 std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700161 getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500162};
Patrick Venturefeb744a2019-06-26 19:07:48 -0700163
164/** @brief Given a value and map of interfaces, update values and check
165 * thresholds.
166 */
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500167void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value);