blob: c3ed161601875dc0f890949939accafc2bb3e334 [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>
Matthew Barth9c431062018-05-07 13:55:29 -05009#include "types.hpp"
Patrick Venture75e56c62018-04-20 18:10:15 -070010#include "hwmonio.hpp"
Brad Bishop3c344d32017-01-05 11:48:39 -050011#include "sensorset.hpp"
Brad Bishop751043e2017-08-29 11:13:46 -040012#include "sysfs.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050013#include "interface.hpp"
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060014#include "timer.hpp"
Matthew Barth9c431062018-05-07 13:55:29 -050015#include "sensor.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050016
Patrick Ventureab10f162017-05-22 09:44:50 -070017static constexpr auto default_interval = 1000000;
18
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
Matthew Barthd238e232018-04-17 12:01:50 -050023namespace optional_ns = std::experimental;
24
Brad Bishopd499ca62016-12-19 09:24:50 -050025/** @class MainLoop
26 * @brief hwmon-readd main application loop.
27 */
28class MainLoop
29{
30 public:
31 MainLoop() = delete;
32 MainLoop(const MainLoop&) = delete;
33 MainLoop& operator=(const MainLoop&) = delete;
34 MainLoop(MainLoop&&) = default;
35 MainLoop& operator=(MainLoop&&) = default;
36 ~MainLoop() = default;
37
38 /** @brief Constructor
39 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050040 * @param[in] bus - sdbusplus bus client connection.
Patrick Venturec897d8b2018-04-23 19:01:56 -070041 * @param[in] param - the path parameter provided
Brad Bishopd499ca62016-12-19 09:24:50 -050042 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040043 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050044 * @param[in] prefix - DBus busname prefix.
45 * @param[in] root - DBus sensors namespace root.
46 *
47 * Any DBus objects are created relative to the DBus
48 * sensors namespace root.
49 *
50 * At startup, the application will own a busname with
51 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050052 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050053 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050054 sdbusplus::bus::bus&& bus,
Patrick Venturec897d8b2018-04-23 19:01:56 -070055 const std::string& param,
Brad Bishopb9e2b072016-12-19 13:47:10 -050056 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040057 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050058 const char* prefix,
59 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050060
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060061 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
62 * event loop.
63 */
Brad Bishopd499ca62016-12-19 09:24:50 -050064 void run();
65
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060066 /** @brief Stop polling timer event loop from another thread.
Brad Bishopd499ca62016-12-19 09:24:50 -050067 *
68 * Typically only used by testcases.
69 */
70 void shutdown() noexcept;
71
72 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050073 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050074 using SensorState = std::map<SensorSet::key_type, mapped_type>;
75
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060076 /** @brief Read hwmon sysfs entries */
77 void read();
78
79 /** @brief Set up D-Bus object state */
80 void init();
81
Brad Bishop9c7b6e02016-12-19 12:43:36 -050082 /** @brief sdbusplus bus client connection. */
83 sdbusplus::bus::bus _bus;
84 /** @brief sdbusplus freedesktop.ObjectManager storage. */
85 sdbusplus::server::manager::manager _manager;
Patrick Venturec897d8b2018-04-23 19:01:56 -070086 /** @brief the parameter path used. */
87 std::string _pathParam;
Brad Bishopb8740fc2017-02-24 23:38:37 -050088 /** @brief hwmon sysfs class path. */
89 std::string _hwmonRoot;
90 /** @brief hwmon sysfs instance. */
91 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040092 /** @brief physical device sysfs path. */
93 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050094 /** @brief DBus busname prefix. */
95 const char* _prefix;
96 /** @brief DBus sensors namespace root. */
97 const char* _root;
Brad Bishop3c344d32017-01-05 11:48:39 -050098 /** @brief DBus object state. */
99 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -0700100 /** @brief Sleep interval in microseconds. */
101 uint64_t _interval = default_interval;
Brad Bishop751043e2017-08-29 11:13:46 -0400102 /** @brief Hwmon sysfs access. */
Patrick Venture75e56c62018-04-20 18:10:15 -0700103 hwmonio::HwmonIO ioAccess;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600104 /** @brief Timer */
105 std::unique_ptr<phosphor::hwmon::Timer> timer;
106 /** @brief the sd_event structure */
107 sd_event* loop = nullptr;
Matthew Barth9c431062018-05-07 13:55:29 -0500108 /** @brief Store the specifications of sensor objects */
109 std::map<SensorSet::key_type,
110 std::unique_ptr<sensor::Sensor>> sensorObjects;
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};