blob: 90f1efa997e11fb3e2b5f4ea7baab3dbb97feb11 [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 Bishop075f7a22017-01-06 09:45:08 -05008#include "interface.hpp"
9
Patrick Ventureab10f162017-05-22 09:44:50 -070010static constexpr auto default_interval = 1000000;
11
Brad Bishop075f7a22017-01-06 09:45:08 -050012using Object = std::map<InterfaceType, std::experimental::any>;
Brad Bishopf7426cf2017-01-06 15:36:43 -050013using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
Brad Bishopd499ca62016-12-19 09:24:50 -050014
15/** @class MainLoop
16 * @brief hwmon-readd main application loop.
17 */
18class MainLoop
19{
20 public:
21 MainLoop() = delete;
22 MainLoop(const MainLoop&) = delete;
23 MainLoop& operator=(const MainLoop&) = delete;
24 MainLoop(MainLoop&&) = default;
25 MainLoop& operator=(MainLoop&&) = default;
26 ~MainLoop() = default;
27
28 /** @brief Constructor
29 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050030 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050031 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040032 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050033 * @param[in] prefix - DBus busname prefix.
34 * @param[in] root - DBus sensors namespace root.
35 *
36 * Any DBus objects are created relative to the DBus
37 * sensors namespace root.
38 *
39 * At startup, the application will own a busname with
40 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050041 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050042 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050043 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050044 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040045 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050046 const char* prefix,
47 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050048
49 /** @brief Start polling loop and process dbus traffic. */
50 void run();
51
52 /** @brief Stop loop from another thread.
53 *
54 * Typically only used by testcases.
55 */
56 void shutdown() noexcept;
57
58 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050059 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050060 using SensorState = std::map<SensorSet::key_type, mapped_type>;
61
Brad Bishop9c7b6e02016-12-19 12:43:36 -050062 /** @brief sdbusplus bus client connection. */
63 sdbusplus::bus::bus _bus;
64 /** @brief sdbusplus freedesktop.ObjectManager storage. */
65 sdbusplus::server::manager::manager _manager;
Brad Bishopd499ca62016-12-19 09:24:50 -050066 /** @brief Shutdown requested. */
67 volatile bool _shutdown;
Brad Bishopb8740fc2017-02-24 23:38:37 -050068 /** @brief hwmon sysfs class path. */
69 std::string _hwmonRoot;
70 /** @brief hwmon sysfs instance. */
71 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040072 /** @brief physical device sysfs path. */
73 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050074 /** @brief DBus busname prefix. */
75 const char* _prefix;
76 /** @brief DBus sensors namespace root. */
77 const char* _root;
Brad Bishop3c344d32017-01-05 11:48:39 -050078 /** @brief DBus object state. */
79 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -070080 /** @brief Sleep interval in microseconds. */
81 uint64_t _interval = default_interval;
Brad Bishopd499ca62016-12-19 09:24:50 -050082};