blob: 4f7293d05dbbaf0a3ea6cb96e637356965f6384a [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,
56 const char* prefix, const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050057
Patrick Venture043d3232018-08-31 10:10:53 -070058 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
59 * event loop.
60 */
61 void run();
Brad Bishopd499ca62016-12-19 09:24:50 -050062
Patrick Venture043d3232018-08-31 10:10:53 -070063 /** @brief Stop polling timer event loop from another thread.
64 *
65 * Typically only used by testcases.
66 */
67 void shutdown() noexcept;
Brad Bishopd499ca62016-12-19 09:24:50 -050068
Patrick Venture043d3232018-08-31 10:10:53 -070069 private:
70 using mapped_type =
71 std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
72 using SensorState = std::map<SensorSet::key_type, mapped_type>;
Brad Bishop3c344d32017-01-05 11:48:39 -050073
Patrick Venture043d3232018-08-31 10:10:53 -070074 /** @brief Read hwmon sysfs entries */
75 void read();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060076
Patrick Venture043d3232018-08-31 10:10:53 -070077 /** @brief Set up D-Bus object state */
78 void init();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060079
Patrick Venture043d3232018-08-31 10:10:53 -070080 /** @brief sdbusplus bus client connection. */
81 sdbusplus::bus::bus _bus;
82 /** @brief sdbusplus freedesktop.ObjectManager storage. */
83 sdbusplus::server::manager::manager _manager;
84 /** @brief the parameter path used. */
85 std::string _pathParam;
86 /** @brief hwmon sysfs class path. */
87 std::string _hwmonRoot;
88 /** @brief hwmon sysfs instance. */
89 std::string _instance;
90 /** @brief physical device sysfs path. */
91 std::string _devPath;
92 /** @brief DBus busname prefix. */
93 const char* _prefix;
94 /** @brief DBus sensors namespace root. */
95 const char* _root;
96 /** @brief DBus object state. */
Patrick Venture52b40612018-12-19 13:36:41 -080097 SensorState _state;
Patrick Venture043d3232018-08-31 10:10:53 -070098 /** @brief Sleep interval in microseconds. */
99 uint64_t _interval = default_interval;
100 /** @brief Hwmon sysfs access. */
Patrick Venture52b40612018-12-19 13:36:41 -0800101 hwmonio::HwmonIO _ioAccess;
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700102 /** @brief the Event Loop structure */
Patrick Venture52b40612018-12-19 13:36:41 -0800103 sdeventplus::Event _event;
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -0700104 /** @brief Read Timer */
Patrick Venture52b40612018-12-19 13:36:41 -0800105 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
Patrick Venture043d3232018-08-31 10:10:53 -0700106 /** @brief Store the specifications of sensor objects */
107 std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
Patrick Venture52b40612018-12-19 13:36:41 -0800108 _sensorObjects;
Matthew Barth31d214c2018-03-26 09:54:27 -0500109
Patrick Venture043d3232018-08-31 10:10:53 -0700110 /**
111 * @brief Map of removed sensors
112 */
Patrick Venture52b40612018-12-19 13:36:41 -0800113 std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors;
Matthew Barth31d214c2018-03-26 09:54:27 -0500114
Patrick Venture043d3232018-08-31 10:10:53 -0700115 /**
116 * @brief Get the ID of the sensor
117 *
118 * @param[in] sensor - Sensor to get the ID of
119 */
120 std::string getID(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500121
Patrick Venture043d3232018-08-31 10:10:53 -0700122 /**
123 * @brief Get the sensor identifiers
124 *
125 * @param[in] sensor - Sensor to get the identifiers of
126 */
127 SensorIdentifiers
128 getIdentifiers(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500129
Patrick Venture043d3232018-08-31 10:10:53 -0700130 /**
131 * @brief Used to create and add sensor objects
132 *
133 * @param[in] sensor - Sensor to create/add object for
134 *
135 * @return - Optional
136 * Object state data on success, nothing on failure
137 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700138 std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700139 getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500140};