blob: 6c5b8e0b2a2f6cc4acccdbba28934bfe4959bfee [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>
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060012#include <memory>
William A. Kennington III4cbdfef2018-10-18 19:19:51 -070013#include <optional>
Brad Bishop9c7b6e02016-12-19 12:43:36 -050014#include <sdbusplus/server.hpp>
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070015#include <sdeventplus/clock.hpp>
William A. Kennington III0fe4cb32018-10-18 19:19:58 -070016#include <sdeventplus/event.hpp>
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070017#include <sdeventplus/utility/timer.hpp>
Patrick Venture043d3232018-08-31 10:10:53 -070018#include <string>
19#include <vector>
Brad Bishop075f7a22017-01-06 09:45:08 -050020
Patrick Ventureab10f162017-05-22 09:44:50 -070021static constexpr auto default_interval = 1000000;
22
Matthew Barth979c8062018-04-17 11:37:15 -050023static constexpr auto sensorID = 0;
24static constexpr auto sensorLabel = 1;
25using SensorIdentifiers = std::tuple<std::string, std::string>;
26
Brad Bishopd499ca62016-12-19 09:24:50 -050027/** @class MainLoop
28 * @brief hwmon-readd main application loop.
29 */
30class MainLoop
31{
Patrick Venture043d3232018-08-31 10:10:53 -070032 public:
33 MainLoop() = delete;
34 MainLoop(const MainLoop&) = delete;
35 MainLoop& operator=(const MainLoop&) = delete;
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -070036 MainLoop(MainLoop&&) = delete;
37 MainLoop& operator=(MainLoop&&) = delete;
Patrick Venture043d3232018-08-31 10:10:53 -070038 ~MainLoop() = default;
Brad Bishopd499ca62016-12-19 09:24:50 -050039
Patrick Venture043d3232018-08-31 10:10:53 -070040 /** @brief Constructor
41 *
42 * @param[in] bus - sdbusplus bus client connection.
43 * @param[in] param - the path parameter provided
44 * @param[in] path - hwmon sysfs instance to manage
45 * @param[in] devPath - physical device sysfs path.
46 * @param[in] prefix - DBus busname prefix.
47 * @param[in] root - DBus sensors namespace root.
48 *
49 * Any DBus objects are created relative to the DBus
50 * sensors namespace root.
51 *
52 * At startup, the application will own a busname with
53 * the format <prefix>.hwmon<n>.
54 */
55 MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
56 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -070057 const char* prefix, const char* root,
58 const hwmonio::HwmonIOInterface* ioIntf);
Brad Bishopd499ca62016-12-19 09:24:50 -050059
Patrick Venture043d3232018-08-31 10:10:53 -070060 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
61 * event loop.
62 */
63 void run();
Brad Bishopd499ca62016-12-19 09:24:50 -050064
Patrick Venture043d3232018-08-31 10:10:53 -070065 /** @brief Stop polling timer event loop from another thread.
66 *
67 * Typically only used by testcases.
68 */
69 void shutdown() noexcept;
Brad Bishopd499ca62016-12-19 09:24:50 -050070
Patrick Venture0cd4f692019-06-21 13:39:40 -070071 /** @brief Remove sensors slated for removal.
72 */
73 void removeSensors();
74
75 /** @brief Attempt to add sensors back that had been removed.
76 */
77 void addDroppedSensors();
78
Patrick Venture043d3232018-08-31 10:10:53 -070079 private:
80 using mapped_type =
81 std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
82 using SensorState = std::map<SensorSet::key_type, mapped_type>;
Brad Bishop3c344d32017-01-05 11:48:39 -050083
Patrick Venture043d3232018-08-31 10:10:53 -070084 /** @brief Read hwmon sysfs entries */
85 void read();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060086
Patrick Venture043d3232018-08-31 10:10:53 -070087 /** @brief Set up D-Bus object state */
88 void init();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060089
Patrick Venture043d3232018-08-31 10:10:53 -070090 /** @brief sdbusplus bus client connection. */
91 sdbusplus::bus::bus _bus;
92 /** @brief sdbusplus freedesktop.ObjectManager storage. */
93 sdbusplus::server::manager::manager _manager;
94 /** @brief the parameter path used. */
95 std::string _pathParam;
96 /** @brief hwmon sysfs class path. */
97 std::string _hwmonRoot;
98 /** @brief hwmon sysfs instance. */
99 std::string _instance;
100 /** @brief physical device sysfs path. */
101 std::string _devPath;
102 /** @brief DBus busname prefix. */
103 const char* _prefix;
104 /** @brief DBus sensors namespace root. */
105 const char* _root;
106 /** @brief DBus object state. */
Patrick Venture52b40612018-12-19 13:36:41 -0800107 SensorState _state;
Patrick Venture043d3232018-08-31 10:10:53 -0700108 /** @brief Sleep interval in microseconds. */
109 uint64_t _interval = default_interval;
110 /** @brief Hwmon sysfs access. */
Patrick Venture16805a62019-06-21 14:19:33 -0700111 const hwmonio::HwmonIOInterface* _ioAccess;
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700112 /** @brief the Event Loop structure */
Patrick Venture52b40612018-12-19 13:36:41 -0800113 sdeventplus::Event _event;
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -0700114 /** @brief Read Timer */
Patrick Venture52b40612018-12-19 13:36:41 -0800115 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
Patrick Venture043d3232018-08-31 10:10:53 -0700116 /** @brief Store the specifications of sensor objects */
117 std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
Patrick Venture52b40612018-12-19 13:36:41 -0800118 _sensorObjects;
Matthew Barth31d214c2018-03-26 09:54:27 -0500119
Patrick Venture043d3232018-08-31 10:10:53 -0700120 /**
121 * @brief Map of removed sensors
122 */
Patrick Venture52b40612018-12-19 13:36:41 -0800123 std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors;
Matthew Barth31d214c2018-03-26 09:54:27 -0500124
Carol Wang9bbe6022019-08-01 17:31:30 +0800125 /** @brief Object of class Average, to handle with average related process
126 */
127 Average _average;
128
Patrick Venture043d3232018-08-31 10:10:53 -0700129 /**
130 * @brief Get the ID of the sensor
131 *
132 * @param[in] sensor - Sensor to get the ID of
133 */
134 std::string getID(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500135
Patrick Venture043d3232018-08-31 10:10:53 -0700136 /**
137 * @brief Get the sensor identifiers
138 *
139 * @param[in] sensor - Sensor to get the identifiers of
140 */
141 SensorIdentifiers
142 getIdentifiers(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500143
Patrick Venture043d3232018-08-31 10:10:53 -0700144 /**
145 * @brief Used to create and add sensor objects
146 *
147 * @param[in] sensor - Sensor to create/add object for
148 *
149 * @return - Optional
150 * Object state data on success, nothing on failure
151 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700152 std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700153 getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500154};
Patrick Venturefeb744a2019-06-26 19:07:48 -0700155
156/** @brief Given a value and map of interfaces, update values and check
157 * thresholds.
158 */
159void updateSensorInterfaces(InterfaceMap& ifaces, int64_t value);