blob: d99ed91802493dea797581990e8a985951012cb0 [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001#pragma once
2
Patrick Venture043d3232018-08-31 10:10:53 -07003#include "hwmonio.hpp"
4#include "interface.hpp"
5#include "sensor.hpp"
6#include "sensorset.hpp"
7#include "sysfs.hpp"
Patrick Venture043d3232018-08-31 10:10:53 -07008#include "types.hpp"
9
William A. Kennington III4cbdfef2018-10-18 19:19:51 -070010#include <any>
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060011#include <memory>
William A. Kennington III4cbdfef2018-10-18 19:19:51 -070012#include <optional>
Brad Bishop9c7b6e02016-12-19 12:43:36 -050013#include <sdbusplus/server.hpp>
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070014#include <sdeventplus/clock.hpp>
William A. Kennington III0fe4cb32018-10-18 19:19:58 -070015#include <sdeventplus/event.hpp>
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070016#include <sdeventplus/utility/timer.hpp>
Patrick Venture043d3232018-08-31 10:10:53 -070017#include <string>
18#include <vector>
Brad Bishop075f7a22017-01-06 09:45:08 -050019
Patrick Ventureab10f162017-05-22 09:44:50 -070020static constexpr auto default_interval = 1000000;
21
Matthew Barth979c8062018-04-17 11:37:15 -050022static constexpr auto sensorID = 0;
23static constexpr auto sensorLabel = 1;
24using SensorIdentifiers = std::tuple<std::string, std::string>;
25
Brad Bishopd499ca62016-12-19 09:24:50 -050026/** @class MainLoop
27 * @brief hwmon-readd main application loop.
28 */
29class MainLoop
30{
Patrick Venture043d3232018-08-31 10:10:53 -070031 public:
32 MainLoop() = delete;
33 MainLoop(const MainLoop&) = delete;
34 MainLoop& operator=(const MainLoop&) = delete;
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070035 MainLoop(MainLoop&&) = delete;
36 MainLoop& operator=(MainLoop&&) = delete;
Patrick Venture043d3232018-08-31 10:10:53 -070037 ~MainLoop() = default;
Brad Bishopd499ca62016-12-19 09:24:50 -050038
Patrick Venture043d3232018-08-31 10:10:53 -070039 /** @brief Constructor
40 *
41 * @param[in] bus - sdbusplus bus client connection.
42 * @param[in] param - the path parameter provided
43 * @param[in] path - hwmon sysfs instance to manage
44 * @param[in] devPath - physical device sysfs path.
45 * @param[in] prefix - DBus busname prefix.
46 * @param[in] root - DBus sensors namespace root.
47 *
48 * Any DBus objects are created relative to the DBus
49 * sensors namespace root.
50 *
51 * At startup, the application will own a busname with
52 * the format <prefix>.hwmon<n>.
53 */
54 MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
55 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -070056 const char* prefix, const char* root,
57 const hwmonio::HwmonIOInterface* ioIntf);
Brad Bishopd499ca62016-12-19 09:24:50 -050058
Patrick Venture043d3232018-08-31 10:10:53 -070059 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
60 * event loop.
61 */
62 void run();
Brad Bishopd499ca62016-12-19 09:24:50 -050063
Patrick Venture043d3232018-08-31 10:10:53 -070064 /** @brief Stop polling timer event loop from another thread.
65 *
66 * Typically only used by testcases.
67 */
68 void shutdown() noexcept;
Brad Bishopd499ca62016-12-19 09:24:50 -050069
Patrick Venture0cd4f692019-06-21 13:39:40 -070070 /** @brief Remove sensors slated for removal.
71 */
72 void removeSensors();
73
74 /** @brief Attempt to add sensors back that had been removed.
75 */
76 void addDroppedSensors();
77
Patrick Venture043d3232018-08-31 10:10:53 -070078 private:
79 using mapped_type =
80 std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
81 using SensorState = std::map<SensorSet::key_type, mapped_type>;
Brad Bishop3c344d32017-01-05 11:48:39 -050082
Patrick Venture043d3232018-08-31 10:10:53 -070083 /** @brief Read hwmon sysfs entries */
84 void read();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060085
Patrick Venture043d3232018-08-31 10:10:53 -070086 /** @brief Set up D-Bus object state */
87 void init();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060088
Patrick Venture043d3232018-08-31 10:10:53 -070089 /** @brief sdbusplus bus client connection. */
90 sdbusplus::bus::bus _bus;
91 /** @brief sdbusplus freedesktop.ObjectManager storage. */
92 sdbusplus::server::manager::manager _manager;
93 /** @brief the parameter path used. */
94 std::string _pathParam;
95 /** @brief hwmon sysfs class path. */
96 std::string _hwmonRoot;
97 /** @brief hwmon sysfs instance. */
98 std::string _instance;
99 /** @brief physical device sysfs path. */
100 std::string _devPath;
101 /** @brief DBus busname prefix. */
102 const char* _prefix;
103 /** @brief DBus sensors namespace root. */
104 const char* _root;
105 /** @brief DBus object state. */
Patrick Venture52b40612018-12-19 13:36:41 -0800106 SensorState _state;
Patrick Venture043d3232018-08-31 10:10:53 -0700107 /** @brief Sleep interval in microseconds. */
108 uint64_t _interval = default_interval;
109 /** @brief Hwmon sysfs access. */
Patrick Venture16805a62019-06-21 14:19:33 -0700110 const hwmonio::HwmonIOInterface* _ioAccess;
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700111 /** @brief the Event Loop structure */
Patrick Venture52b40612018-12-19 13:36:41 -0800112 sdeventplus::Event _event;
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -0700113 /** @brief Read Timer */
Patrick Venture52b40612018-12-19 13:36:41 -0800114 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
Patrick Venture043d3232018-08-31 10:10:53 -0700115 /** @brief Store the specifications of sensor objects */
116 std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
Patrick Venture52b40612018-12-19 13:36:41 -0800117 _sensorObjects;
Matthew Barth31d214c2018-03-26 09:54:27 -0500118
Patrick Venture043d3232018-08-31 10:10:53 -0700119 /**
120 * @brief Map of removed sensors
121 */
Patrick Venture52b40612018-12-19 13:36:41 -0800122 std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors;
Matthew Barth31d214c2018-03-26 09:54:27 -0500123
Patrick Venture043d3232018-08-31 10:10:53 -0700124 /**
125 * @brief Get the ID of the sensor
126 *
127 * @param[in] sensor - Sensor to get the ID of
128 */
129 std::string getID(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500130
Patrick Venture043d3232018-08-31 10:10:53 -0700131 /**
132 * @brief Get the sensor identifiers
133 *
134 * @param[in] sensor - Sensor to get the identifiers of
135 */
136 SensorIdentifiers
137 getIdentifiers(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500138
Patrick Venture043d3232018-08-31 10:10:53 -0700139 /**
140 * @brief Used to create and add sensor objects
141 *
142 * @param[in] sensor - Sensor to create/add object for
143 *
144 * @return - Optional
145 * Object state data on success, nothing on failure
146 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700147 std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700148 getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500149};
Patrick Venturefeb744a2019-06-26 19:07:48 -0700150
151/** @brief Given a value and map of interfaces, update values and check
152 * thresholds.
153 */
154void updateSensorInterfaces(InterfaceMap& ifaces, int64_t value);