blob: cff512359a1c2f2784963a2d4648c99d86ffbe14 [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 Bishopd499ca62016-12-19 09:24:50 -050011
12/** @class MainLoop
13 * @brief hwmon-readd main application loop.
14 */
15class MainLoop
16{
17 public:
18 MainLoop() = delete;
19 MainLoop(const MainLoop&) = delete;
20 MainLoop& operator=(const MainLoop&) = delete;
21 MainLoop(MainLoop&&) = default;
22 MainLoop& operator=(MainLoop&&) = default;
23 ~MainLoop() = default;
24
25 /** @brief Constructor
26 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050027 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050028 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopb9e2b072016-12-19 13:47:10 -050029 * @param[in] prefix - DBus busname prefix.
30 * @param[in] root - DBus sensors namespace root.
31 *
32 * Any DBus objects are created relative to the DBus
33 * sensors namespace root.
34 *
35 * At startup, the application will own a busname with
36 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050037 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050038 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050039 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050040 const std::string& path,
41 const char* prefix,
42 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050043
44 /** @brief Start polling loop and process dbus traffic. */
45 void run();
46
47 /** @brief Stop loop from another thread.
48 *
49 * Typically only used by testcases.
50 */
51 void shutdown() noexcept;
52
53 private:
Brad Bishop075f7a22017-01-06 09:45:08 -050054 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, Object>;
Brad Bishop3c344d32017-01-05 11:48:39 -050055 using SensorState = std::map<SensorSet::key_type, mapped_type>;
56
Brad Bishop9c7b6e02016-12-19 12:43:36 -050057 /** @brief sdbusplus bus client connection. */
58 sdbusplus::bus::bus _bus;
59 /** @brief sdbusplus freedesktop.ObjectManager storage. */
60 sdbusplus::server::manager::manager _manager;
Brad Bishopd499ca62016-12-19 09:24:50 -050061 /** @brief Shutdown requested. */
62 volatile bool _shutdown;
63 /** @brief Path to hwmon sysfs instance. */
64 std::string _path;
Brad Bishopb9e2b072016-12-19 13:47:10 -050065 /** @brief DBus busname prefix. */
66 const char* _prefix;
67 /** @brief DBus sensors namespace root. */
68 const char* _root;
Brad Bishop3c344d32017-01-05 11:48:39 -050069 /** @brief DBus object state. */
70 SensorState state;
Brad Bishopd499ca62016-12-19 09:24:50 -050071};