1 #pragma once 2 3 #include "hwmonio.hpp" 4 #include "interface.hpp" 5 #include "sensor.hpp" 6 #include "sensorset.hpp" 7 #include "sysfs.hpp" 8 #include "types.hpp" 9 10 #include <any> 11 #include <memory> 12 #include <optional> 13 #include <sdbusplus/server.hpp> 14 #include <sdeventplus/clock.hpp> 15 #include <sdeventplus/event.hpp> 16 #include <sdeventplus/utility/timer.hpp> 17 #include <string> 18 #include <vector> 19 20 static constexpr auto default_interval = 1000000; 21 22 static constexpr auto sensorID = 0; 23 static constexpr auto sensorLabel = 1; 24 using SensorIdentifiers = std::tuple<std::string, std::string>; 25 26 /** @class MainLoop 27 * @brief hwmon-readd main application loop. 28 */ 29 class MainLoop 30 { 31 public: 32 MainLoop() = delete; 33 MainLoop(const MainLoop&) = delete; 34 MainLoop& operator=(const MainLoop&) = delete; 35 MainLoop(MainLoop&&) = delete; 36 MainLoop& operator=(MainLoop&&) = delete; 37 ~MainLoop() = default; 38 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, 57 const hwmonio::HwmonIOInterface* ioIntf); 58 59 /** @brief Setup polling timer in a sd event loop and attach to D-Bus 60 * event loop. 61 */ 62 void run(); 63 64 /** @brief Stop polling timer event loop from another thread. 65 * 66 * Typically only used by testcases. 67 */ 68 void shutdown() noexcept; 69 70 /** @brief Remove sensors slated for removal. 71 */ 72 void removeSensors(); 73 74 /** @brief Attempt to add sensors back that had been removed. 75 */ 76 void addDroppedSensors(); 77 78 private: 79 using mapped_type = 80 std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>; 81 using SensorState = std::map<SensorSet::key_type, mapped_type>; 82 83 /** @brief Read hwmon sysfs entries */ 84 void read(); 85 86 /** @brief Set up D-Bus object state */ 87 void init(); 88 89 /** @brief sdbusplus bus client connection. */ 90 sdbusplus::bus::bus _bus; 91 /** @brief sdbusplus freedesktop.ObjectManager storage. */ 92 sdbusplus::server::manager::manager _manager; 93 /** @brief the parameter path used. */ 94 std::string _pathParam; 95 /** @brief hwmon sysfs class path. */ 96 std::string _hwmonRoot; 97 /** @brief hwmon sysfs instance. */ 98 std::string _instance; 99 /** @brief physical device sysfs path. */ 100 std::string _devPath; 101 /** @brief DBus busname prefix. */ 102 const char* _prefix; 103 /** @brief DBus sensors namespace root. */ 104 const char* _root; 105 /** @brief DBus object state. */ 106 SensorState _state; 107 /** @brief Sleep interval in microseconds. */ 108 uint64_t _interval = default_interval; 109 /** @brief Hwmon sysfs access. */ 110 const hwmonio::HwmonIOInterface* _ioAccess; 111 /** @brief the Event Loop structure */ 112 sdeventplus::Event _event; 113 /** @brief Read Timer */ 114 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer; 115 /** @brief Store the specifications of sensor objects */ 116 std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>> 117 _sensorObjects; 118 119 /** 120 * @brief Map of removed sensors 121 */ 122 std::map<SensorSet::key_type, SensorSet::mapped_type> _rmSensors; 123 124 /** 125 * @brief Get the ID of the sensor 126 * 127 * @param[in] sensor - Sensor to get the ID of 128 */ 129 std::string getID(SensorSet::container_t::const_reference sensor); 130 131 /** 132 * @brief Get the sensor identifiers 133 * 134 * @param[in] sensor - Sensor to get the identifiers of 135 */ 136 SensorIdentifiers 137 getIdentifiers(SensorSet::container_t::const_reference sensor); 138 139 /** 140 * @brief Used to create and add sensor objects 141 * 142 * @param[in] sensor - Sensor to create/add object for 143 * 144 * @return - Optional 145 * Object state data on success, nothing on failure 146 */ 147 std::optional<ObjectStateData> 148 getObject(SensorSet::container_t::const_reference sensor); 149 }; 150