#pragma once #include "average.hpp" #include "hwmonio.hpp" #include "interface.hpp" #include "sensor.hpp" #include "sensorset.hpp" #include "sysfs.hpp" #include "types.hpp" #include #include #include #include #include #include #include #include #include #include static constexpr auto default_interval = 1000000; static constexpr auto sensorID = 0; static constexpr auto sensorLabel = 1; static constexpr auto sensorAccuracy = 2; static constexpr auto sensorPriority = 3; using SensorIdentifiers = std::tuple; /** @class MainLoop * @brief hwmon-readd main application loop. */ class MainLoop { public: MainLoop() = delete; MainLoop(const MainLoop&) = delete; MainLoop& operator=(const MainLoop&) = delete; MainLoop(MainLoop&&) = delete; MainLoop& operator=(MainLoop&&) = delete; ~MainLoop() = default; /** @brief Constructor * * @param[in] bus - sdbusplus bus client connection. * @param[in] param - the path parameter provided * @param[in] path - hwmon sysfs instance to manage * @param[in] devPath - physical device sysfs path. * @param[in] prefix - DBus busname prefix. * @param[in] root - DBus sensors namespace root. * @param[in] instanceId - override value to identify instance on d-bus. * * Any DBus objects are created relative to the DBus * sensors namespace root. * * At startup, the application will own a busname with * the format .hwmon. */ MainLoop(sdbusplus::bus_t&& bus, const std::string& param, const std::string& path, const std::string& devPath, const char* prefix, const char* root, const std::string& instanceId, const hwmonio::HwmonIOInterface* ioIntf); /** @brief Setup polling timer in a sd event loop and attach to D-Bus * event loop. */ void run(); /** @brief Stop polling timer event loop from another thread. * * Typically only used by testcases. */ void shutdown() noexcept; /** @brief Remove sensors slated for removal. */ void removeSensors(); /** @brief Attempt to add sensors back that had been removed. */ void addDroppedSensors(); private: using mapped_type = std::tuple; using SensorState = std::map; /** @brief Read hwmon sysfs entries */ void read(); /** @brief Set up D-Bus object state */ void init(); /** @brief sdbusplus bus client connection. */ sdbusplus::bus_t _bus; /** @brief sdbusplus freedesktop.ObjectManager storage. */ sdbusplus::server::manager_t _manager; /** @brief the parameter path used. */ std::string _pathParam; /** @brief hwmon sysfs class path. */ std::string _hwmonRoot; /** @brief hwmon sysfs instance. */ std::string _instance; /** @brief physical device sysfs path. */ std::string _devPath; /** @brief DBus busname prefix. */ const char* _prefix; /** @brief DBus sensors namespace root. */ const char* _root; /** @brief DBus object state. */ SensorState _state; /** @brief DBus instance id specified by command line. */ std::string _instanceId; /** @brief Sleep interval in microseconds. */ uint64_t _interval = default_interval; /** @brief Hwmon sysfs access. */ const hwmonio::HwmonIOInterface* _ioAccess; /** @brief the Event Loop structure */ sdeventplus::Event _event; /** @brief Read Timer */ sdeventplus::utility::Timer _timer; /** @brief Store the specifications of sensor objects */ std::map> _sensorObjects; /** @brief Store the async futures of timed out sensor objects */ sensor::TimedoutMap _timedoutMap; /** * @brief Map of removed sensors */ std::map _rmSensors; /** @brief Object of class Average, to handle with average related process */ Average _average; /** * @brief Get the ID of the sensor * * @param[in] sensor - Sensor to get the ID of */ std::string getID(SensorSet::container_t::const_reference sensor); /** * @brief Get the sensor identifiers * * @param[in] sensor - Sensor to get the identifiers of */ SensorIdentifiers getIdentifiers(SensorSet::container_t::const_reference sensor); /** * @brief Used to create and add sensor objects * * @param[in] sensor - Sensor to create/add object for * * @return - Optional * Object state data on success, nothing on failure */ std::optional getObject(SensorSet::container_t::const_reference sensor); }; /** @brief Given a value and map of interfaces, update values and check * thresholds. */ void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value);