1 #pragma once 2 3 #include <string> 4 #include <vector> 5 #include <experimental/any> 6 #include <experimental/optional> 7 #include <memory> 8 #include <sdbusplus/server.hpp> 9 #include "hwmonio.hpp" 10 #include "sensorset.hpp" 11 #include "sysfs.hpp" 12 #include "interface.hpp" 13 #include "timer.hpp" 14 15 static constexpr auto default_interval = 1000000; 16 17 using Object = std::map<InterfaceType, std::experimental::any>; 18 using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>; 19 using RetryIO = std::tuple<size_t, std::chrono::milliseconds>; 20 using ObjectStateData = std::pair<std::string, ObjectInfo>; 21 22 static constexpr auto sensorID = 0; 23 static constexpr auto sensorLabel = 1; 24 using SensorIdentifiers = std::tuple<std::string, std::string>; 25 26 namespace optional_ns = std::experimental; 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&&) = default; 38 MainLoop& operator=(MainLoop&&) = default; 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 * 50 * Any DBus objects are created relative to the DBus 51 * sensors namespace root. 52 * 53 * At startup, the application will own a busname with 54 * the format <prefix>.hwmon<n>. 55 */ 56 MainLoop( 57 sdbusplus::bus::bus&& bus, 58 const std::string& param, 59 const std::string& path, 60 const std::string& devPath, 61 const char* prefix, 62 const char* root); 63 64 /** @brief Setup polling timer in a sd event loop and attach to D-Bus 65 * event loop. 66 */ 67 void run(); 68 69 /** @brief Stop polling timer event loop from another thread. 70 * 71 * Typically only used by testcases. 72 */ 73 void shutdown() noexcept; 74 75 private: 76 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>; 77 using SensorState = std::map<SensorSet::key_type, mapped_type>; 78 79 /** @brief Read hwmon sysfs entries */ 80 void read(); 81 82 /** @brief Set up D-Bus object state */ 83 void init(); 84 85 /** @brief sdbusplus bus client connection. */ 86 sdbusplus::bus::bus _bus; 87 /** @brief sdbusplus freedesktop.ObjectManager storage. */ 88 sdbusplus::server::manager::manager _manager; 89 /** @brief the parameter path used. */ 90 std::string _pathParam; 91 /** @brief hwmon sysfs class path. */ 92 std::string _hwmonRoot; 93 /** @brief hwmon sysfs instance. */ 94 std::string _instance; 95 /** @brief physical device sysfs path. */ 96 std::string _devPath; 97 /** @brief DBus busname prefix. */ 98 const char* _prefix; 99 /** @brief DBus sensors namespace root. */ 100 const char* _root; 101 /** @brief DBus object state. */ 102 SensorState state; 103 /** @brief Sleep interval in microseconds. */ 104 uint64_t _interval = default_interval; 105 /** @brief Hwmon sysfs access. */ 106 hwmonio::HwmonIO ioAccess; 107 /** @brief Timer */ 108 std::unique_ptr<phosphor::hwmon::Timer> timer; 109 /** @brief the sd_event structure */ 110 sd_event* loop = nullptr; 111 112 /** 113 * @brief Map of removed sensors 114 */ 115 std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors; 116 117 /** 118 * @brief Get the ID of the sensor 119 * 120 * @param[in] sensor - Sensor to get the ID of 121 */ 122 std::string getID(SensorSet::container_t::const_reference sensor); 123 124 /** 125 * @brief Get the sensor identifiers 126 * 127 * @param[in] sensor - Sensor to get the identifiers of 128 */ 129 SensorIdentifiers getIdentifiers( 130 SensorSet::container_t::const_reference sensor); 131 132 /** 133 * @brief Used to create and add sensor objects 134 * 135 * @param[in] sensor - Sensor to create/add object for 136 * 137 * @return - Optional 138 * Object state data on success, nothing on failure 139 */ 140 optional_ns::optional<ObjectStateData> getObject( 141 SensorSet::container_t::const_reference sensor); 142 }; 143