xref: /openbmc/phosphor-hwmon/mainloop.hpp (revision 751043e9)
1e55ef3d8SBrad Bishop #pragma once
2e55ef3d8SBrad Bishop 
3d499ca64SBrad Bishop #include <string>
4075f7a2dSBrad Bishop #include <vector>
5075f7a2dSBrad Bishop #include <experimental/any>
69c7b6e06SBrad Bishop #include <sdbusplus/server.hpp>
73c344d3aSBrad Bishop #include "sensorset.hpp"
8*751043e9SBrad Bishop #include "sysfs.hpp"
9075f7a2dSBrad Bishop #include "interface.hpp"
10075f7a2dSBrad Bishop 
11ab10f164SPatrick Venture static constexpr auto default_interval = 1000000;
12ab10f164SPatrick Venture 
13075f7a2dSBrad Bishop using Object = std::map<InterfaceType, std::experimental::any>;
14f7426cffSBrad Bishop using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
15d499ca64SBrad Bishop 
16d499ca64SBrad Bishop /** @class MainLoop
17d499ca64SBrad Bishop  *  @brief hwmon-readd main application loop.
18d499ca64SBrad Bishop  */
19d499ca64SBrad Bishop class MainLoop
20d499ca64SBrad Bishop {
21d499ca64SBrad Bishop     public:
22d499ca64SBrad Bishop         MainLoop() = delete;
23d499ca64SBrad Bishop         MainLoop(const MainLoop&) = delete;
24d499ca64SBrad Bishop         MainLoop& operator=(const MainLoop&) = delete;
25d499ca64SBrad Bishop         MainLoop(MainLoop&&) = default;
26d499ca64SBrad Bishop         MainLoop& operator=(MainLoop&&) = default;
27d499ca64SBrad Bishop         ~MainLoop() = default;
28d499ca64SBrad Bishop 
29d499ca64SBrad Bishop         /** @brief Constructor
30d499ca64SBrad Bishop          *
319c7b6e06SBrad Bishop          *  @param[in] bus - sdbusplus bus client connection.
32d499ca64SBrad Bishop          *  @param[in] path - hwmon sysfs instance to manage
33f3aa9aefSBrad Bishop          *  @param[in] devPath - physical device sysfs path.
34b9e2b07eSBrad Bishop          *  @param[in] prefix - DBus busname prefix.
35b9e2b07eSBrad Bishop          *  @param[in] root - DBus sensors namespace root.
36b9e2b07eSBrad Bishop          *
37b9e2b07eSBrad Bishop          *  Any DBus objects are created relative to the DBus
38b9e2b07eSBrad Bishop          *  sensors namespace root.
39b9e2b07eSBrad Bishop          *
40b9e2b07eSBrad Bishop          *  At startup, the application will own a busname with
41b9e2b07eSBrad Bishop          *  the format <prefix>.hwmon<n>.
42d499ca64SBrad Bishop          */
43b9e2b07eSBrad Bishop         MainLoop(
449c7b6e06SBrad Bishop             sdbusplus::bus::bus&& bus,
45b9e2b07eSBrad Bishop             const std::string& path,
46f3aa9aefSBrad Bishop             const std::string& devPath,
47b9e2b07eSBrad Bishop             const char* prefix,
48b9e2b07eSBrad Bishop             const char* root);
49d499ca64SBrad Bishop 
50d499ca64SBrad Bishop         /** @brief Start polling loop and process dbus traffic. */
51d499ca64SBrad Bishop         void run();
52d499ca64SBrad Bishop 
53d499ca64SBrad Bishop         /** @brief Stop loop from another thread.
54d499ca64SBrad Bishop          *
55d499ca64SBrad Bishop          *  Typically only used by testcases.
56d499ca64SBrad Bishop          */
57d499ca64SBrad Bishop         void shutdown() noexcept;
58d499ca64SBrad Bishop 
59d499ca64SBrad Bishop     private:
60f7426cffSBrad Bishop         using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
613c344d3aSBrad Bishop         using SensorState = std::map<SensorSet::key_type, mapped_type>;
623c344d3aSBrad Bishop 
639c7b6e06SBrad Bishop         /** @brief sdbusplus bus client connection. */
649c7b6e06SBrad Bishop         sdbusplus::bus::bus _bus;
659c7b6e06SBrad Bishop         /** @brief sdbusplus freedesktop.ObjectManager storage. */
669c7b6e06SBrad Bishop         sdbusplus::server::manager::manager _manager;
67d499ca64SBrad Bishop         /** @brief Shutdown requested. */
68d499ca64SBrad Bishop         volatile bool _shutdown;
69b8740fc7SBrad Bishop         /** @brief hwmon sysfs class path. */
70b8740fc7SBrad Bishop         std::string _hwmonRoot;
71b8740fc7SBrad Bishop         /** @brief hwmon sysfs instance. */
72b8740fc7SBrad Bishop         std::string _instance;
73f3aa9aefSBrad Bishop         /** @brief physical device sysfs path. */
74f3aa9aefSBrad Bishop         std::string _devPath;
75b9e2b07eSBrad Bishop         /** @brief DBus busname prefix. */
76b9e2b07eSBrad Bishop         const char* _prefix;
77b9e2b07eSBrad Bishop         /** @brief DBus sensors namespace root. */
78b9e2b07eSBrad Bishop         const char* _root;
793c344d3aSBrad Bishop         /** @brief DBus object state. */
803c344d3aSBrad Bishop         SensorState state;
81ab10f164SPatrick Venture         /** @brief Sleep interval in microseconds. */
82ab10f164SPatrick Venture         uint64_t _interval = default_interval;
83*751043e9SBrad Bishop         /** @brief Hwmon sysfs access. */
84*751043e9SBrad Bishop         sysfs::hwmonio::HwmonIO ioAccess;
85d499ca64SBrad Bishop };
86