blob: d139593a0ca10a9016e994b6cd86d9857b6df56b [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>
Patrick Venture75e56c62018-04-20 18:10:15 -07008#include "hwmonio.hpp"
Brad Bishop3c344d32017-01-05 11:48:39 -05009#include "sensorset.hpp"
Brad Bishop751043e2017-08-29 11:13:46 -040010#include "sysfs.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050011#include "interface.hpp"
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060012#include "timer.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050013
Patrick Ventureab10f162017-05-22 09:44:50 -070014static constexpr auto default_interval = 1000000;
15
Brad Bishop075f7a22017-01-06 09:45:08 -050016using Object = std::map<InterfaceType, std::experimental::any>;
Brad Bishopf7426cf2017-01-06 15:36:43 -050017using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
Matthew Barthd4beecf2018-04-03 15:50:22 -050018using RetryIO = std::tuple<size_t, std::chrono::milliseconds>;
Brad Bishopd499ca62016-12-19 09:24:50 -050019
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{
29 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;
36
37 /** @brief Constructor
38 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050039 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050040 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040041 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050042 * @param[in] prefix - DBus busname prefix.
43 * @param[in] root - DBus sensors namespace root.
44 *
45 * Any DBus objects are created relative to the DBus
46 * sensors namespace root.
47 *
48 * At startup, the application will own a busname with
49 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050050 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050051 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050052 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050053 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040054 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050055 const char* prefix,
56 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050057
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060058 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
59 * event loop.
60 */
Brad Bishopd499ca62016-12-19 09:24:50 -050061 void run();
62
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060063 /** @brief Stop polling timer event loop from another thread.
Brad Bishopd499ca62016-12-19 09:24:50 -050064 *
65 * Typically only used by testcases.
66 */
67 void shutdown() noexcept;
68
69 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050070 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050071 using SensorState = std::map<SensorSet::key_type, mapped_type>;
72
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060073 /** @brief Read hwmon sysfs entries */
74 void read();
75
76 /** @brief Set up D-Bus object state */
77 void init();
78
Brad Bishop9c7b6e02016-12-19 12:43:36 -050079 /** @brief sdbusplus bus client connection. */
80 sdbusplus::bus::bus _bus;
81 /** @brief sdbusplus freedesktop.ObjectManager storage. */
82 sdbusplus::server::manager::manager _manager;
Brad Bishopb8740fc2017-02-24 23:38:37 -050083 /** @brief hwmon sysfs class path. */
84 std::string _hwmonRoot;
85 /** @brief hwmon sysfs instance. */
86 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040087 /** @brief physical device sysfs path. */
88 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050089 /** @brief DBus busname prefix. */
90 const char* _prefix;
91 /** @brief DBus sensors namespace root. */
92 const char* _root;
Matthew Bartha23babd2018-03-16 10:03:27 -050093 /** @brief hwmon instance is for an OCC. */
94 bool _isOCC = false;
Brad Bishop3c344d32017-01-05 11:48:39 -050095 /** @brief DBus object state. */
96 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -070097 /** @brief Sleep interval in microseconds. */
98 uint64_t _interval = default_interval;
Brad Bishop751043e2017-08-29 11:13:46 -040099 /** @brief Hwmon sysfs access. */
Patrick Venture75e56c62018-04-20 18:10:15 -0700100 hwmonio::HwmonIO ioAccess;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600101 /** @brief Timer */
102 std::unique_ptr<phosphor::hwmon::Timer> timer;
103 /** @brief the sd_event structure */
104 sd_event* loop = nullptr;
Matthew Barth31d214c2018-03-26 09:54:27 -0500105
106 /**
107 * @brief Map of removed sensors
108 */
109 std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors;
110
111 /**
Matthew Barth979c8062018-04-17 11:37:15 -0500112 * @brief Get the ID of the sensor
113 *
114 * @param[in] sensor - Sensor to get the ID of
115 */
116 std::string getID(SensorSet::container_t::const_reference sensor);
117
118 /**
119 * @brief Get the sensor identifiers
120 *
121 * @param[in] sensor - Sensor to get the identifiers of
122 */
123 SensorIdentifiers getIdentifiers(
124 SensorSet::container_t::const_reference sensor);
125
126 /**
Matthew Barth31d214c2018-03-26 09:54:27 -0500127 * @brief Used to create and add sensor objects
128 *
129 * @param[in] sensor - Sensor to create/add object for
130 */
131 void getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500132};