Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 3 | #include <string> |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 4 | #include <vector> |
| 5 | #include <experimental/any> |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 6 | #include <sdbusplus/server.hpp> |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 7 | #include "sensorset.hpp" |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 8 | #include "interface.hpp" |
| 9 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame^] | 10 | static constexpr auto default_interval = 1000000; |
| 11 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 12 | using Object = std::map<InterfaceType, std::experimental::any>; |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 13 | using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 14 | |
| 15 | /** @class MainLoop |
| 16 | * @brief hwmon-readd main application loop. |
| 17 | */ |
| 18 | class 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 Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 30 | * @param[in] bus - sdbusplus bus client connection. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 31 | * @param[in] path - hwmon sysfs instance to manage |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 32 | * @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 Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 40 | */ |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 41 | MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 42 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 43 | const std::string& path, |
| 44 | const char* prefix, |
| 45 | const char* root); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 46 | |
| 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 Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 57 | using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>; |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 58 | using SensorState = std::map<SensorSet::key_type, mapped_type>; |
| 59 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 60 | /** @brief sdbusplus bus client connection. */ |
| 61 | sdbusplus::bus::bus _bus; |
| 62 | /** @brief sdbusplus freedesktop.ObjectManager storage. */ |
| 63 | sdbusplus::server::manager::manager _manager; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 64 | /** @brief Shutdown requested. */ |
| 65 | volatile bool _shutdown; |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 66 | /** @brief hwmon sysfs class path. */ |
| 67 | std::string _hwmonRoot; |
| 68 | /** @brief hwmon sysfs instance. */ |
| 69 | std::string _instance; |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 70 | /** @brief DBus busname prefix. */ |
| 71 | const char* _prefix; |
| 72 | /** @brief DBus sensors namespace root. */ |
| 73 | const char* _root; |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 74 | /** @brief DBus object state. */ |
| 75 | SensorState state; |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame^] | 76 | /** @brief Sleep interval in microseconds. */ |
| 77 | uint64_t _interval = default_interval; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 78 | }; |