blob: 9c50c784656973f14a3f74286b4d9a12497063b7 [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"
8#include "timer.hpp"
9#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>
Patrick Venture043d3232018-08-31 10:10:53 -070015#include <string>
16#include <vector>
Brad Bishop075f7a22017-01-06 09:45:08 -050017
Patrick Ventureab10f162017-05-22 09:44:50 -070018static constexpr auto default_interval = 1000000;
19
Matthew Barth979c8062018-04-17 11:37:15 -050020static constexpr auto sensorID = 0;
21static constexpr auto sensorLabel = 1;
22using SensorIdentifiers = std::tuple<std::string, std::string>;
23
Brad Bishopd499ca62016-12-19 09:24:50 -050024/** @class MainLoop
25 * @brief hwmon-readd main application loop.
26 */
27class MainLoop
28{
Patrick Venture043d3232018-08-31 10:10:53 -070029 public:
30 MainLoop() = delete;
31 MainLoop(const MainLoop&) = delete;
32 MainLoop& operator=(const MainLoop&) = delete;
33 MainLoop(MainLoop&&) = default;
34 MainLoop& operator=(MainLoop&&) = default;
35 ~MainLoop() = default;
Brad Bishopd499ca62016-12-19 09:24:50 -050036
Patrick Venture043d3232018-08-31 10:10:53 -070037 /** @brief Constructor
38 *
39 * @param[in] bus - sdbusplus bus client connection.
40 * @param[in] param - the path parameter provided
41 * @param[in] path - hwmon sysfs instance to manage
42 * @param[in] devPath - physical device sysfs path.
43 * @param[in] prefix - DBus busname prefix.
44 * @param[in] root - DBus sensors namespace root.
45 *
46 * Any DBus objects are created relative to the DBus
47 * sensors namespace root.
48 *
49 * At startup, the application will own a busname with
50 * the format <prefix>.hwmon<n>.
51 */
52 MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
53 const std::string& path, const std::string& devPath,
54 const char* prefix, const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050055
Patrick Venture043d3232018-08-31 10:10:53 -070056 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
57 * event loop.
58 */
59 void run();
Brad Bishopd499ca62016-12-19 09:24:50 -050060
Patrick Venture043d3232018-08-31 10:10:53 -070061 /** @brief Stop polling timer event loop from another thread.
62 *
63 * Typically only used by testcases.
64 */
65 void shutdown() noexcept;
Brad Bishopd499ca62016-12-19 09:24:50 -050066
Patrick Venture043d3232018-08-31 10:10:53 -070067 private:
68 using mapped_type =
69 std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
70 using SensorState = std::map<SensorSet::key_type, mapped_type>;
Brad Bishop3c344d32017-01-05 11:48:39 -050071
Patrick Venture043d3232018-08-31 10:10:53 -070072 /** @brief Read hwmon sysfs entries */
73 void read();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060074
Patrick Venture043d3232018-08-31 10:10:53 -070075 /** @brief Set up D-Bus object state */
76 void init();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060077
Patrick Venture043d3232018-08-31 10:10:53 -070078 /** @brief sdbusplus bus client connection. */
79 sdbusplus::bus::bus _bus;
80 /** @brief sdbusplus freedesktop.ObjectManager storage. */
81 sdbusplus::server::manager::manager _manager;
82 /** @brief the parameter path used. */
83 std::string _pathParam;
84 /** @brief hwmon sysfs class path. */
85 std::string _hwmonRoot;
86 /** @brief hwmon sysfs instance. */
87 std::string _instance;
88 /** @brief physical device sysfs path. */
89 std::string _devPath;
90 /** @brief DBus busname prefix. */
91 const char* _prefix;
92 /** @brief DBus sensors namespace root. */
93 const char* _root;
94 /** @brief DBus object state. */
95 SensorState state;
96 /** @brief Sleep interval in microseconds. */
97 uint64_t _interval = default_interval;
98 /** @brief Hwmon sysfs access. */
99 hwmonio::HwmonIO ioAccess;
100 /** @brief Timer */
101 std::unique_ptr<phosphor::hwmon::Timer> timer;
102 /** @brief the sd_event structure */
103 sd_event* loop = nullptr;
104 /** @brief Store the specifications of sensor objects */
105 std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
106 sensorObjects;
Matthew Barth31d214c2018-03-26 09:54:27 -0500107
Patrick Venture043d3232018-08-31 10:10:53 -0700108 /**
109 * @brief Map of removed sensors
110 */
111 std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors;
Matthew Barth31d214c2018-03-26 09:54:27 -0500112
Patrick Venture043d3232018-08-31 10:10:53 -0700113 /**
114 * @brief Get the ID of the sensor
115 *
116 * @param[in] sensor - Sensor to get the ID of
117 */
118 std::string getID(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500119
Patrick Venture043d3232018-08-31 10:10:53 -0700120 /**
121 * @brief Get the sensor identifiers
122 *
123 * @param[in] sensor - Sensor to get the identifiers of
124 */
125 SensorIdentifiers
126 getIdentifiers(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500127
Patrick Venture043d3232018-08-31 10:10:53 -0700128 /**
129 * @brief Used to create and add sensor objects
130 *
131 * @param[in] sensor - Sensor to create/add object for
132 *
133 * @return - Optional
134 * Object state data on success, nothing on failure
135 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700136 std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700137 getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500138};