blob: b62481d57f98c07e47b7a44ce4e5834aa408f06d [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>
Brad Bishop9c7b6e02016-12-19 12:43:36 -05006#include <sdbusplus/server.hpp>
Brad Bishop3c344d32017-01-05 11:48:39 -05007#include "sensorset.hpp"
Brad Bishop751043e2017-08-29 11:13:46 -04008#include "sysfs.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -05009#include "interface.hpp"
10
Patrick Ventureab10f162017-05-22 09:44:50 -070011static constexpr auto default_interval = 1000000;
12
Brad Bishop075f7a22017-01-06 09:45:08 -050013using Object = std::map<InterfaceType, std::experimental::any>;
Brad Bishopf7426cf2017-01-06 15:36:43 -050014using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
Brad Bishopd499ca62016-12-19 09:24:50 -050015
16/** @class MainLoop
17 * @brief hwmon-readd main application loop.
18 */
19class MainLoop
20{
21 public:
22 MainLoop() = delete;
23 MainLoop(const MainLoop&) = delete;
24 MainLoop& operator=(const MainLoop&) = delete;
25 MainLoop(MainLoop&&) = default;
26 MainLoop& operator=(MainLoop&&) = default;
27 ~MainLoop() = default;
28
29 /** @brief Constructor
30 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050031 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050032 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040033 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050034 * @param[in] prefix - DBus busname prefix.
35 * @param[in] root - DBus sensors namespace root.
36 *
37 * Any DBus objects are created relative to the DBus
38 * sensors namespace root.
39 *
40 * At startup, the application will own a busname with
41 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050042 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050043 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050044 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050045 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040046 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050047 const char* prefix,
48 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050049
50 /** @brief Start polling loop and process dbus traffic. */
51 void run();
52
53 /** @brief Stop loop from another thread.
54 *
55 * Typically only used by testcases.
56 */
57 void shutdown() noexcept;
58
59 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050060 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050061 using SensorState = std::map<SensorSet::key_type, mapped_type>;
62
Brad Bishop9c7b6e02016-12-19 12:43:36 -050063 /** @brief sdbusplus bus client connection. */
64 sdbusplus::bus::bus _bus;
65 /** @brief sdbusplus freedesktop.ObjectManager storage. */
66 sdbusplus::server::manager::manager _manager;
Brad Bishopd499ca62016-12-19 09:24:50 -050067 /** @brief Shutdown requested. */
68 volatile bool _shutdown;
Brad Bishopb8740fc2017-02-24 23:38:37 -050069 /** @brief hwmon sysfs class path. */
70 std::string _hwmonRoot;
71 /** @brief hwmon sysfs instance. */
72 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040073 /** @brief physical device sysfs path. */
74 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050075 /** @brief DBus busname prefix. */
76 const char* _prefix;
77 /** @brief DBus sensors namespace root. */
78 const char* _root;
Brad Bishop3c344d32017-01-05 11:48:39 -050079 /** @brief DBus object state. */
80 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -070081 /** @brief Sleep interval in microseconds. */
82 uint64_t _interval = default_interval;
Brad Bishop751043e2017-08-29 11:13:46 -040083 /** @brief Hwmon sysfs access. */
84 sysfs::hwmonio::HwmonIO ioAccess;
Brad Bishopd499ca62016-12-19 09:24:50 -050085};