blob: d4ef2c443b4338fa62b4b1f6f6c9f714e644ee1d [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 Bishopb9e2b072016-12-19 13:47:10 -050032 * @param[in] prefix - DBus busname prefix.
33 * @param[in] root - DBus sensors namespace root.
34 *
35 * Any DBus objects are created relative to the DBus
36 * sensors namespace root.
37 *
38 * At startup, the application will own a busname with
39 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050040 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050041 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050042 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050043 const std::string& path,
44 const char* prefix,
45 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050046
47 /** @brief Start polling loop and process dbus traffic. */
48 void run();
49
50 /** @brief Stop loop from another thread.
51 *
52 * Typically only used by testcases.
53 */
54 void shutdown() noexcept;
55
56 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050057 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050058 using SensorState = std::map<SensorSet::key_type, mapped_type>;
59
Brad Bishop9c7b6e02016-12-19 12:43:36 -050060 /** @brief sdbusplus bus client connection. */
61 sdbusplus::bus::bus _bus;
62 /** @brief sdbusplus freedesktop.ObjectManager storage. */
63 sdbusplus::server::manager::manager _manager;
Brad Bishopd499ca62016-12-19 09:24:50 -050064 /** @brief Shutdown requested. */
65 volatile bool _shutdown;
Brad Bishopb8740fc2017-02-24 23:38:37 -050066 /** @brief hwmon sysfs class path. */
67 std::string _hwmonRoot;
68 /** @brief hwmon sysfs instance. */
69 std::string _instance;
Brad Bishopb9e2b072016-12-19 13:47:10 -050070 /** @brief DBus busname prefix. */
71 const char* _prefix;
72 /** @brief DBus sensors namespace root. */
73 const char* _root;
Brad Bishop3c344d32017-01-05 11:48:39 -050074 /** @brief DBus object state. */
75 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -070076 /** @brief Sleep interval in microseconds. */
77 uint64_t _interval = default_interval;
Brad Bishopd499ca62016-12-19 09:24:50 -050078};