blob: 0453d8e98ce1291ad095f2c563facd6426c1fa75 [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>
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -06006#include <memory>
Brad Bishop9c7b6e02016-12-19 12:43:36 -05007#include <sdbusplus/server.hpp>
Brad Bishop3c344d32017-01-05 11:48:39 -05008#include "sensorset.hpp"
Brad Bishop751043e2017-08-29 11:13:46 -04009#include "sysfs.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050010#include "interface.hpp"
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060011#include "timer.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050012
Patrick Ventureab10f162017-05-22 09:44:50 -070013static constexpr auto default_interval = 1000000;
14
Brad Bishop075f7a22017-01-06 09:45:08 -050015using Object = std::map<InterfaceType, std::experimental::any>;
Brad Bishopf7426cf2017-01-06 15:36:43 -050016using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
Matthew Barthd4beecf2018-04-03 15:50:22 -050017using RetryIO = std::tuple<size_t, std::chrono::milliseconds>;
Brad Bishopd499ca62016-12-19 09:24:50 -050018
Matthew Barth979c8062018-04-17 11:37:15 -050019static constexpr auto sensorID = 0;
20static constexpr auto sensorLabel = 1;
21using SensorIdentifiers = std::tuple<std::string, std::string>;
22
Brad Bishopd499ca62016-12-19 09:24:50 -050023/** @class MainLoop
24 * @brief hwmon-readd main application loop.
25 */
26class MainLoop
27{
28 public:
29 MainLoop() = delete;
30 MainLoop(const MainLoop&) = delete;
31 MainLoop& operator=(const MainLoop&) = delete;
32 MainLoop(MainLoop&&) = default;
33 MainLoop& operator=(MainLoop&&) = default;
34 ~MainLoop() = default;
35
36 /** @brief Constructor
37 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050038 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050039 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040040 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050041 * @param[in] prefix - DBus busname prefix.
42 * @param[in] root - DBus sensors namespace root.
43 *
44 * Any DBus objects are created relative to the DBus
45 * sensors namespace root.
46 *
47 * At startup, the application will own a busname with
48 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050049 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050050 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050051 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050052 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040053 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050054 const char* prefix,
55 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050056
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060057 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
58 * event loop.
59 */
Brad Bishopd499ca62016-12-19 09:24:50 -050060 void run();
61
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060062 /** @brief Stop polling timer event loop from another thread.
Brad Bishopd499ca62016-12-19 09:24:50 -050063 *
64 * Typically only used by testcases.
65 */
66 void shutdown() noexcept;
67
68 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050069 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050070 using SensorState = std::map<SensorSet::key_type, mapped_type>;
71
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060072 /** @brief Read hwmon sysfs entries */
73 void read();
74
75 /** @brief Set up D-Bus object state */
76 void init();
77
Brad Bishop9c7b6e02016-12-19 12:43:36 -050078 /** @brief sdbusplus bus client connection. */
79 sdbusplus::bus::bus _bus;
80 /** @brief sdbusplus freedesktop.ObjectManager storage. */
81 sdbusplus::server::manager::manager _manager;
Brad Bishopb8740fc2017-02-24 23:38:37 -050082 /** @brief hwmon sysfs class path. */
83 std::string _hwmonRoot;
84 /** @brief hwmon sysfs instance. */
85 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040086 /** @brief physical device sysfs path. */
87 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050088 /** @brief DBus busname prefix. */
89 const char* _prefix;
90 /** @brief DBus sensors namespace root. */
91 const char* _root;
Matthew Bartha23babd2018-03-16 10:03:27 -050092 /** @brief hwmon instance is for an OCC. */
93 bool _isOCC = false;
Brad Bishop3c344d32017-01-05 11:48:39 -050094 /** @brief DBus object state. */
95 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -070096 /** @brief Sleep interval in microseconds. */
97 uint64_t _interval = default_interval;
Brad Bishop751043e2017-08-29 11:13:46 -040098 /** @brief Hwmon sysfs access. */
99 sysfs::hwmonio::HwmonIO ioAccess;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600100 /** @brief Timer */
101 std::unique_ptr<phosphor::hwmon::Timer> timer;
102 /** @brief the sd_event structure */
103 sd_event* loop = nullptr;
Matthew Barth31d214c2018-03-26 09:54:27 -0500104
105 /**
106 * @brief Map of removed sensors
107 */
108 std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors;
109
110 /**
Matthew Barth979c8062018-04-17 11:37:15 -0500111 * @brief Get the ID of the sensor
112 *
113 * @param[in] sensor - Sensor to get the ID of
114 */
115 std::string getID(SensorSet::container_t::const_reference sensor);
116
117 /**
118 * @brief Get the sensor identifiers
119 *
120 * @param[in] sensor - Sensor to get the identifiers of
121 */
122 SensorIdentifiers getIdentifiers(
123 SensorSet::container_t::const_reference sensor);
124
125 /**
Matthew Barth31d214c2018-03-26 09:54:27 -0500126 * @brief Used to create and add sensor objects
127 *
128 * @param[in] sensor - Sensor to create/add object for
129 */
130 void getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500131};