1 #pragma once 2 3 #include "average.hpp" 4 #include "hwmonio.hpp" 5 #include "interface.hpp" 6 #include "sensor.hpp" 7 #include "sensorset.hpp" 8 #include "sysfs.hpp" 9 #include "types.hpp" 10 11 #include <any> 12 #include <future> 13 #include <memory> 14 #include <optional> 15 #include <sdbusplus/server.hpp> 16 #include <sdeventplus/clock.hpp> 17 #include <sdeventplus/event.hpp> 18 #include <sdeventplus/utility/timer.hpp> 19 #include <string> 20 #include <vector> 21 22 static constexpr auto default_interval = 1000000; 23 24 static constexpr auto sensorID = 0; 25 static constexpr auto sensorLabel = 1; 26 using SensorIdentifiers = std::tuple<std::string, std::string>; 27 28 /** @class MainLoop 29 * @brief hwmon-readd main application loop. 30 */ 31 class MainLoop 32 { 33 public: 34 MainLoop() = delete; 35 MainLoop(const MainLoop&) = delete; 36 MainLoop& operator=(const MainLoop&) = delete; 37 MainLoop(MainLoop&&) = delete; 38 MainLoop& operator=(MainLoop&&) = delete; 39 ~MainLoop() = default; 40 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. 49 * @param[in] instanceId - override value to identify instance on d-bus. 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_t&& bus, const std::string& param, 58 const std::string& path, const std::string& devPath, 59 const char* prefix, const char* root, 60 const std::string& instanceId, 61 const hwmonio::HwmonIOInterface* ioIntf); 62 63 /** @brief Setup polling timer in a sd event loop and attach to D-Bus 64 * event loop. 65 */ 66 void run(); 67 68 /** @brief Stop polling timer event loop from another thread. 69 * 70 * Typically only used by testcases. 71 */ 72 void shutdown() noexcept; 73 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 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>; 86 87 /** @brief Read hwmon sysfs entries */ 88 void read(); 89 90 /** @brief Set up D-Bus object state */ 91 void init(); 92 93 /** @brief sdbusplus bus client connection. */ 94 sdbusplus::bus_t _bus; 95 /** @brief sdbusplus freedesktop.ObjectManager storage. */ 96 sdbusplus::server::manager_t _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. */ 110 SensorState _state; 111 /** @brief DBus instance id specified by command line. */ 112 std::string _instanceId; 113 /** @brief Sleep interval in microseconds. */ 114 uint64_t _interval = default_interval; 115 /** @brief Hwmon sysfs access. */ 116 const hwmonio::HwmonIOInterface* _ioAccess; 117 /** @brief the Event Loop structure */ 118 sdeventplus::Event _event; 119 /** @brief Read Timer */ 120 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer; 121 /** @brief Store the specifications of sensor objects */ 122 std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>> 123 _sensorObjects; 124 /** @brief Store the async futures of timed out sensor objects */ 125 sensor::TimedoutMap _timedoutMap; 126 127 /** 128 * @brief Map of removed sensors 129 */ 130 std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors; 131 132 /** @brief Object of class Average, to handle with average related process 133 */ 134 Average _average; 135 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); 142 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); 150 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 */ 159 std::optional<ObjectStateData> 160 getObject(SensorSet::container_t::const_reference sensor); 161 }; 162 163 /** @brief Given a value and map of interfaces, update values and check 164 * thresholds. 165 */ 166 void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value); 167