blob: d420c28916c18470aee6b99ee464e42c9973cefe [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001#pragma once
2
Brad Bishopd499ca62016-12-19 09:24:50 -05003#include <string>
Brad Bishop075f7a22017-01-06 09:45:08 -05004#include <vector>
5#include <experimental/any>
Matthew Barthd238e232018-04-17 12:01:50 -05006#include <experimental/optional>
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -06007#include <memory>
Brad Bishop9c7b6e02016-12-19 12:43:36 -05008#include <sdbusplus/server.hpp>
Patrick Venture75e56c62018-04-20 18:10:15 -07009#include "hwmonio.hpp"
Brad Bishop3c344d32017-01-05 11:48:39 -050010#include "sensorset.hpp"
Brad Bishop751043e2017-08-29 11:13:46 -040011#include "sysfs.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050012#include "interface.hpp"
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060013#include "timer.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050014
Patrick Ventureab10f162017-05-22 09:44:50 -070015static constexpr auto default_interval = 1000000;
16
Brad Bishop075f7a22017-01-06 09:45:08 -050017using Object = std::map<InterfaceType, std::experimental::any>;
Brad Bishopf7426cf2017-01-06 15:36:43 -050018using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
Matthew Barthd4beecf2018-04-03 15:50:22 -050019using RetryIO = std::tuple<size_t, std::chrono::milliseconds>;
Matthew Barthd238e232018-04-17 12:01:50 -050020using ObjectStateData = std::pair<std::string, ObjectInfo>;
Brad Bishopd499ca62016-12-19 09:24:50 -050021
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
Matthew Barthd238e232018-04-17 12:01:50 -050026namespace optional_ns = std::experimental;
27
Brad Bishopd499ca62016-12-19 09:24:50 -050028/** @class MainLoop
29 * @brief hwmon-readd main application loop.
30 */
31class MainLoop
32{
33 public:
34 MainLoop() = delete;
35 MainLoop(const MainLoop&) = delete;
36 MainLoop& operator=(const MainLoop&) = delete;
37 MainLoop(MainLoop&&) = default;
38 MainLoop& operator=(MainLoop&&) = default;
39 ~MainLoop() = default;
40
41 /** @brief Constructor
42 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050043 * @param[in] bus - sdbusplus bus client connection.
Patrick Venturec897d8b2018-04-23 19:01:56 -070044 * @param[in] param - the path parameter provided
Brad Bishopd499ca62016-12-19 09:24:50 -050045 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040046 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050047 * @param[in] prefix - DBus busname prefix.
48 * @param[in] root - DBus sensors namespace root.
49 *
50 * Any DBus objects are created relative to the DBus
51 * sensors namespace root.
52 *
53 * At startup, the application will own a busname with
54 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050055 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050056 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050057 sdbusplus::bus::bus&& bus,
Patrick Venturec897d8b2018-04-23 19:01:56 -070058 const std::string& param,
Brad Bishopb9e2b072016-12-19 13:47:10 -050059 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040060 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050061 const char* prefix,
62 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050063
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060064 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
65 * event loop.
66 */
Brad Bishopd499ca62016-12-19 09:24:50 -050067 void run();
68
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060069 /** @brief Stop polling timer event loop from another thread.
Brad Bishopd499ca62016-12-19 09:24:50 -050070 *
71 * Typically only used by testcases.
72 */
73 void shutdown() noexcept;
74
75 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050076 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050077 using SensorState = std::map<SensorSet::key_type, mapped_type>;
78
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060079 /** @brief Read hwmon sysfs entries */
80 void read();
81
82 /** @brief Set up D-Bus object state */
83 void init();
84
Brad Bishop9c7b6e02016-12-19 12:43:36 -050085 /** @brief sdbusplus bus client connection. */
86 sdbusplus::bus::bus _bus;
87 /** @brief sdbusplus freedesktop.ObjectManager storage. */
88 sdbusplus::server::manager::manager _manager;
Patrick Venturec897d8b2018-04-23 19:01:56 -070089 /** @brief the parameter path used. */
90 std::string _pathParam;
Brad Bishopb8740fc2017-02-24 23:38:37 -050091 /** @brief hwmon sysfs class path. */
92 std::string _hwmonRoot;
93 /** @brief hwmon sysfs instance. */
94 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040095 /** @brief physical device sysfs path. */
96 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050097 /** @brief DBus busname prefix. */
98 const char* _prefix;
99 /** @brief DBus sensors namespace root. */
100 const char* _root;
Brad Bishop3c344d32017-01-05 11:48:39 -0500101 /** @brief DBus object state. */
102 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -0700103 /** @brief Sleep interval in microseconds. */
104 uint64_t _interval = default_interval;
Brad Bishop751043e2017-08-29 11:13:46 -0400105 /** @brief Hwmon sysfs access. */
Patrick Venture75e56c62018-04-20 18:10:15 -0700106 hwmonio::HwmonIO ioAccess;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600107 /** @brief Timer */
108 std::unique_ptr<phosphor::hwmon::Timer> timer;
109 /** @brief the sd_event structure */
110 sd_event* loop = nullptr;
Matthew Barth31d214c2018-03-26 09:54:27 -0500111
112 /**
113 * @brief Map of removed sensors
114 */
115 std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors;
116
117 /**
Matthew Barth979c8062018-04-17 11:37:15 -0500118 * @brief Get the ID of the sensor
119 *
120 * @param[in] sensor - Sensor to get the ID of
121 */
122 std::string getID(SensorSet::container_t::const_reference sensor);
123
124 /**
125 * @brief Get the sensor identifiers
126 *
127 * @param[in] sensor - Sensor to get the identifiers of
128 */
129 SensorIdentifiers getIdentifiers(
130 SensorSet::container_t::const_reference sensor);
131
132 /**
Matthew Barth31d214c2018-03-26 09:54:27 -0500133 * @brief Used to create and add sensor objects
134 *
135 * @param[in] sensor - Sensor to create/add object for
Matthew Barthd238e232018-04-17 12:01:50 -0500136 *
137 * @return - Optional
138 * Object state data on success, nothing on failure
Matthew Barth31d214c2018-03-26 09:54:27 -0500139 */
Matthew Barthd238e232018-04-17 12:01:50 -0500140 optional_ns::optional<ObjectStateData> getObject(
141 SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500142};