Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 3 | #include "hwmonio.hpp" |
| 4 | #include "interface.hpp" |
| 5 | #include "sensor.hpp" |
| 6 | #include "sensorset.hpp" |
| 7 | #include "sysfs.hpp" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 8 | #include "types.hpp" |
| 9 | |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 10 | #include <any> |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 11 | #include <memory> |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 12 | #include <optional> |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 13 | #include <sdbusplus/server.hpp> |
William A. Kennington III | 0dd0c4d | 2018-10-18 19:20:01 -0700 | [diff] [blame] | 14 | #include <sdeventplus/clock.hpp> |
William A. Kennington III | 0fe4cb3 | 2018-10-18 19:19:58 -0700 | [diff] [blame] | 15 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 0dd0c4d | 2018-10-18 19:20:01 -0700 | [diff] [blame] | 16 | #include <sdeventplus/utility/timer.hpp> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 19 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 20 | static constexpr auto default_interval = 1000000; |
| 21 | |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 22 | static constexpr auto sensorID = 0; |
| 23 | static constexpr auto sensorLabel = 1; |
| 24 | using SensorIdentifiers = std::tuple<std::string, std::string>; |
| 25 | |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 26 | /** @class MainLoop |
| 27 | * @brief hwmon-readd main application loop. |
| 28 | */ |
| 29 | class MainLoop |
| 30 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 31 | public: |
| 32 | MainLoop() = delete; |
| 33 | MainLoop(const MainLoop&) = delete; |
| 34 | MainLoop& operator=(const MainLoop&) = delete; |
William A. Kennington III | 0dd0c4d | 2018-10-18 19:20:01 -0700 | [diff] [blame] | 35 | MainLoop(MainLoop&&) = delete; |
| 36 | MainLoop& operator=(MainLoop&&) = delete; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 37 | ~MainLoop() = default; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 38 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 39 | /** @brief Constructor |
| 40 | * |
| 41 | * @param[in] bus - sdbusplus bus client connection. |
| 42 | * @param[in] param - the path parameter provided |
| 43 | * @param[in] path - hwmon sysfs instance to manage |
| 44 | * @param[in] devPath - physical device sysfs path. |
| 45 | * @param[in] prefix - DBus busname prefix. |
| 46 | * @param[in] root - DBus sensors namespace root. |
| 47 | * |
| 48 | * Any DBus objects are created relative to the DBus |
| 49 | * sensors namespace root. |
| 50 | * |
| 51 | * At startup, the application will own a busname with |
| 52 | * the format <prefix>.hwmon<n>. |
| 53 | */ |
| 54 | MainLoop(sdbusplus::bus::bus&& bus, const std::string& param, |
| 55 | const std::string& path, const std::string& devPath, |
| 56 | const char* prefix, const char* root); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 57 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 58 | /** @brief Setup polling timer in a sd event loop and attach to D-Bus |
| 59 | * event loop. |
| 60 | */ |
| 61 | void run(); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 62 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 63 | /** @brief Stop polling timer event loop from another thread. |
| 64 | * |
| 65 | * Typically only used by testcases. |
| 66 | */ |
| 67 | void shutdown() noexcept; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 68 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 69 | private: |
| 70 | using mapped_type = |
| 71 | std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>; |
| 72 | using SensorState = std::map<SensorSet::key_type, mapped_type>; |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 73 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 74 | /** @brief Read hwmon sysfs entries */ |
| 75 | void read(); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 76 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 77 | /** @brief Set up D-Bus object state */ |
| 78 | void init(); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 79 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 80 | /** @brief sdbusplus bus client connection. */ |
| 81 | sdbusplus::bus::bus _bus; |
| 82 | /** @brief sdbusplus freedesktop.ObjectManager storage. */ |
| 83 | sdbusplus::server::manager::manager _manager; |
| 84 | /** @brief the parameter path used. */ |
| 85 | std::string _pathParam; |
| 86 | /** @brief hwmon sysfs class path. */ |
| 87 | std::string _hwmonRoot; |
| 88 | /** @brief hwmon sysfs instance. */ |
| 89 | std::string _instance; |
| 90 | /** @brief physical device sysfs path. */ |
| 91 | std::string _devPath; |
| 92 | /** @brief DBus busname prefix. */ |
| 93 | const char* _prefix; |
| 94 | /** @brief DBus sensors namespace root. */ |
| 95 | const char* _root; |
| 96 | /** @brief DBus object state. */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 97 | SensorState _state; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 98 | /** @brief Sleep interval in microseconds. */ |
| 99 | uint64_t _interval = default_interval; |
| 100 | /** @brief Hwmon sysfs access. */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 101 | hwmonio::HwmonIO _ioAccess; |
William A. Kennington III | 0fe4cb3 | 2018-10-18 19:19:58 -0700 | [diff] [blame] | 102 | /** @brief the Event Loop structure */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 103 | sdeventplus::Event _event; |
William A. Kennington III | 0dd0c4d | 2018-10-18 19:20:01 -0700 | [diff] [blame] | 104 | /** @brief Read Timer */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 105 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 106 | /** @brief Store the specifications of sensor objects */ |
| 107 | std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>> |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 108 | _sensorObjects; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 109 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 110 | /** |
| 111 | * @brief Map of removed sensors |
| 112 | */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 113 | std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 114 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 115 | /** |
| 116 | * @brief Get the ID of the sensor |
| 117 | * |
| 118 | * @param[in] sensor - Sensor to get the ID of |
| 119 | */ |
| 120 | std::string getID(SensorSet::container_t::const_reference sensor); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 121 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 122 | /** |
| 123 | * @brief Get the sensor identifiers |
| 124 | * |
| 125 | * @param[in] sensor - Sensor to get the identifiers of |
| 126 | */ |
| 127 | SensorIdentifiers |
| 128 | getIdentifiers(SensorSet::container_t::const_reference sensor); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 129 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 130 | /** |
| 131 | * @brief Used to create and add sensor objects |
| 132 | * |
| 133 | * @param[in] sensor - Sensor to create/add object for |
| 134 | * |
| 135 | * @return - Optional |
| 136 | * Object state data on success, nothing on failure |
| 137 | */ |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 138 | std::optional<ObjectStateData> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 139 | getObject(SensorSet::container_t::const_reference sensor); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 140 | }; |