blob: f48e5904136dcc128fcfb92800edabd078a70669 [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
10using Object = std::map<InterfaceType, std::experimental::any>;
Brad Bishopf7426cf2017-01-06 15:36:43 -050011using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
Brad Bishopd499ca62016-12-19 09:24:50 -050012
13/** @class MainLoop
14 * @brief hwmon-readd main application loop.
15 */
16class MainLoop
17{
18 public:
19 MainLoop() = delete;
20 MainLoop(const MainLoop&) = delete;
21 MainLoop& operator=(const MainLoop&) = delete;
22 MainLoop(MainLoop&&) = default;
23 MainLoop& operator=(MainLoop&&) = default;
24 ~MainLoop() = default;
25
26 /** @brief Constructor
27 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050028 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050029 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopb9e2b072016-12-19 13:47:10 -050030 * @param[in] prefix - DBus busname prefix.
31 * @param[in] root - DBus sensors namespace root.
32 *
33 * Any DBus objects are created relative to the DBus
34 * sensors namespace root.
35 *
36 * At startup, the application will own a busname with
37 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050038 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050039 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050040 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050041 const std::string& path,
42 const char* prefix,
43 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050044
45 /** @brief Start polling loop and process dbus traffic. */
46 void run();
47
48 /** @brief Stop loop from another thread.
49 *
50 * Typically only used by testcases.
51 */
52 void shutdown() noexcept;
53
54 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050055 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050056 using SensorState = std::map<SensorSet::key_type, mapped_type>;
57
Brad Bishop9c7b6e02016-12-19 12:43:36 -050058 /** @brief sdbusplus bus client connection. */
59 sdbusplus::bus::bus _bus;
60 /** @brief sdbusplus freedesktop.ObjectManager storage. */
61 sdbusplus::server::manager::manager _manager;
Brad Bishopd499ca62016-12-19 09:24:50 -050062 /** @brief Shutdown requested. */
63 volatile bool _shutdown;
Brad Bishopb8740fc2017-02-24 23:38:37 -050064 /** @brief hwmon sysfs class path. */
65 std::string _hwmonRoot;
66 /** @brief hwmon sysfs instance. */
67 std::string _instance;
Brad Bishopb9e2b072016-12-19 13:47:10 -050068 /** @brief DBus busname prefix. */
69 const char* _prefix;
70 /** @brief DBus sensors namespace root. */
71 const char* _root;
Brad Bishop3c344d32017-01-05 11:48:39 -050072 /** @brief DBus object state. */
73 SensorState state;
Brad Bishopd499ca62016-12-19 09:24:50 -050074};