Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 3 | #include "average.hpp" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 4 | #include "hwmonio.hpp" |
| 5 | #include "interface.hpp" |
| 6 | #include "sensor.hpp" |
| 7 | #include "sensorset.hpp" |
| 8 | #include "sysfs.hpp" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 9 | #include "types.hpp" |
| 10 | |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 11 | #include <any> |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame^] | 12 | #include <future> |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 13 | #include <memory> |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 14 | #include <optional> |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 15 | #include <sdbusplus/server.hpp> |
William A. Kennington III | 0dd0c4d | 2018-10-18 19:20:01 -0700 | [diff] [blame] | 16 | #include <sdeventplus/clock.hpp> |
William A. Kennington III | 0fe4cb3 | 2018-10-18 19:19:58 -0700 | [diff] [blame] | 17 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 0dd0c4d | 2018-10-18 19:20:01 -0700 | [diff] [blame] | 18 | #include <sdeventplus/utility/timer.hpp> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 21 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 22 | static constexpr auto default_interval = 1000000; |
| 23 | |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 24 | static constexpr auto sensorID = 0; |
| 25 | static constexpr auto sensorLabel = 1; |
| 26 | using SensorIdentifiers = std::tuple<std::string, std::string>; |
| 27 | |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 28 | /** @class MainLoop |
| 29 | * @brief hwmon-readd main application loop. |
| 30 | */ |
| 31 | class MainLoop |
| 32 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 33 | public: |
| 34 | MainLoop() = delete; |
| 35 | MainLoop(const MainLoop&) = delete; |
| 36 | MainLoop& operator=(const MainLoop&) = delete; |
William A. Kennington III | 0dd0c4d | 2018-10-18 19:20:01 -0700 | [diff] [blame] | 37 | MainLoop(MainLoop&&) = delete; |
| 38 | MainLoop& operator=(MainLoop&&) = delete; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 39 | ~MainLoop() = default; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 40 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 41 | /** @brief Constructor |
| 42 | * |
| 43 | * @param[in] bus - sdbusplus bus client connection. |
| 44 | * @param[in] param - the path parameter provided |
| 45 | * @param[in] path - hwmon sysfs instance to manage |
| 46 | * @param[in] devPath - physical device sysfs path. |
| 47 | * @param[in] prefix - DBus busname prefix. |
| 48 | * @param[in] root - DBus sensors namespace root. |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 49 | * @param[in] instanceId - override value to identify instance on d-bus. |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 50 | * |
| 51 | * Any DBus objects are created relative to the DBus |
| 52 | * sensors namespace root. |
| 53 | * |
| 54 | * At startup, the application will own a busname with |
| 55 | * the format <prefix>.hwmon<n>. |
| 56 | */ |
| 57 | MainLoop(sdbusplus::bus::bus&& bus, const std::string& param, |
| 58 | const std::string& path, const std::string& devPath, |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 59 | const char* prefix, const char* root, |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 60 | const std::string& instanceId, |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 61 | const hwmonio::HwmonIOInterface* ioIntf); |
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 Setup polling timer in a sd event loop and attach to D-Bus |
| 64 | * event loop. |
| 65 | */ |
| 66 | void run(); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 67 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 68 | /** @brief Stop polling timer event loop from another thread. |
| 69 | * |
| 70 | * Typically only used by testcases. |
| 71 | */ |
| 72 | void shutdown() noexcept; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 73 | |
Patrick Venture | 0cd4f69 | 2019-06-21 13:39:40 -0700 | [diff] [blame] | 74 | /** @brief Remove sensors slated for removal. |
| 75 | */ |
| 76 | void removeSensors(); |
| 77 | |
| 78 | /** @brief Attempt to add sensors back that had been removed. |
| 79 | */ |
| 80 | void addDroppedSensors(); |
| 81 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 82 | private: |
| 83 | using mapped_type = |
| 84 | std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>; |
| 85 | using SensorState = std::map<SensorSet::key_type, mapped_type>; |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 86 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 87 | /** @brief Read hwmon sysfs entries */ |
| 88 | void read(); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 89 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 90 | /** @brief Set up D-Bus object state */ |
| 91 | void init(); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 92 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 93 | /** @brief sdbusplus bus client connection. */ |
| 94 | sdbusplus::bus::bus _bus; |
| 95 | /** @brief sdbusplus freedesktop.ObjectManager storage. */ |
| 96 | sdbusplus::server::manager::manager _manager; |
| 97 | /** @brief the parameter path used. */ |
| 98 | std::string _pathParam; |
| 99 | /** @brief hwmon sysfs class path. */ |
| 100 | std::string _hwmonRoot; |
| 101 | /** @brief hwmon sysfs instance. */ |
| 102 | std::string _instance; |
| 103 | /** @brief physical device sysfs path. */ |
| 104 | std::string _devPath; |
| 105 | /** @brief DBus busname prefix. */ |
| 106 | const char* _prefix; |
| 107 | /** @brief DBus sensors namespace root. */ |
| 108 | const char* _root; |
| 109 | /** @brief DBus object state. */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 110 | SensorState _state; |
Anton D. Kachalov | d46d081 | 2021-02-03 23:25:37 +0100 | [diff] [blame] | 111 | /** @brief DBus instance id specified by command line. */ |
| 112 | std::string _instanceId; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 113 | /** @brief Sleep interval in microseconds. */ |
| 114 | uint64_t _interval = default_interval; |
| 115 | /** @brief Hwmon sysfs access. */ |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 116 | const hwmonio::HwmonIOInterface* _ioAccess; |
William A. Kennington III | 0fe4cb3 | 2018-10-18 19:19:58 -0700 | [diff] [blame] | 117 | /** @brief the Event Loop structure */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 118 | sdeventplus::Event _event; |
William A. Kennington III | 0dd0c4d | 2018-10-18 19:20:01 -0700 | [diff] [blame] | 119 | /** @brief Read Timer */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 120 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 121 | /** @brief Store the specifications of sensor objects */ |
| 122 | std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>> |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 123 | _sensorObjects; |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame^] | 124 | /** @brief Store the async futures of timed out sensor objects */ |
| 125 | sensor::TimedoutMap _timedoutMap; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 126 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 127 | /** |
| 128 | * @brief Map of removed sensors |
| 129 | */ |
Patrick Venture | 52b4061 | 2018-12-19 13:36:41 -0800 | [diff] [blame] | 130 | std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 131 | |
Carol Wang | 9bbe602 | 2019-08-01 17:31:30 +0800 | [diff] [blame] | 132 | /** @brief Object of class Average, to handle with average related process |
| 133 | */ |
| 134 | Average _average; |
| 135 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 136 | /** |
| 137 | * @brief Get the ID of the sensor |
| 138 | * |
| 139 | * @param[in] sensor - Sensor to get the ID of |
| 140 | */ |
| 141 | std::string getID(SensorSet::container_t::const_reference sensor); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 142 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 143 | /** |
| 144 | * @brief Get the sensor identifiers |
| 145 | * |
| 146 | * @param[in] sensor - Sensor to get the identifiers of |
| 147 | */ |
| 148 | SensorIdentifiers |
| 149 | getIdentifiers(SensorSet::container_t::const_reference sensor); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 150 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 151 | /** |
| 152 | * @brief Used to create and add sensor objects |
| 153 | * |
| 154 | * @param[in] sensor - Sensor to create/add object for |
| 155 | * |
| 156 | * @return - Optional |
| 157 | * Object state data on success, nothing on failure |
| 158 | */ |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 159 | std::optional<ObjectStateData> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 160 | getObject(SensorSet::container_t::const_reference sensor); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 161 | }; |
Patrick Venture | feb744a | 2019-06-26 19:07:48 -0700 | [diff] [blame] | 162 | |
| 163 | /** @brief Given a value and map of interfaces, update values and check |
| 164 | * thresholds. |
| 165 | */ |
Matt Spinler | ecac0ae | 2020-07-08 13:09:08 -0500 | [diff] [blame] | 166 | void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value); |