xref: /openbmc/phosphor-hwmon/mainloop.hpp (revision f7426cff)
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <experimental/any>
6 #include <sdbusplus/server.hpp>
7 #include "sensorset.hpp"
8 #include "interface.hpp"
9 
10 using Object = std::map<InterfaceType, std::experimental::any>;
11 using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
12 
13 /** @class MainLoop
14  *  @brief hwmon-readd main application loop.
15  */
16 class 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          *
28          *  @param[in] bus - sdbusplus bus client connection.
29          *  @param[in] path - hwmon sysfs instance to manage
30          *  @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>.
38          */
39         MainLoop(
40             sdbusplus::bus::bus&& bus,
41             const std::string& path,
42             const char* prefix,
43             const char* root);
44 
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:
55         using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
56         using SensorState = std::map<SensorSet::key_type, mapped_type>;
57 
58         /** @brief sdbusplus bus client connection. */
59         sdbusplus::bus::bus _bus;
60         /** @brief sdbusplus freedesktop.ObjectManager storage. */
61         sdbusplus::server::manager::manager _manager;
62         /** @brief Shutdown requested. */
63         volatile bool _shutdown;
64         /** @brief Path to hwmon sysfs instance. */
65         std::string _path;
66         /** @brief DBus busname prefix. */
67         const char* _prefix;
68         /** @brief DBus sensors namespace root. */
69         const char* _root;
70         /** @brief DBus object state. */
71         SensorState state;
72 };
73