Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 3 | #include "hwmonio.hpp" |
| 4 | #include "interface.hpp" |
| 5 | #include "sensor.hpp" |
| 6 | #include "sensorset.hpp" |
| 7 | #include "sysfs.hpp" |
| 8 | #include "timer.hpp" |
| 9 | #include "types.hpp" |
| 10 | |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame^] | 11 | #include <any> |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 12 | #include <memory> |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame^] | 13 | #include <optional> |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 14 | #include <sdbusplus/server.hpp> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 17 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 18 | static constexpr auto default_interval = 1000000; |
| 19 | |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 20 | static constexpr auto sensorID = 0; |
| 21 | static constexpr auto sensorLabel = 1; |
| 22 | using SensorIdentifiers = std::tuple<std::string, std::string>; |
| 23 | |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 24 | /** @class MainLoop |
| 25 | * @brief hwmon-readd main application loop. |
| 26 | */ |
| 27 | class MainLoop |
| 28 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 29 | public: |
| 30 | MainLoop() = delete; |
| 31 | MainLoop(const MainLoop&) = delete; |
| 32 | MainLoop& operator=(const MainLoop&) = delete; |
| 33 | MainLoop(MainLoop&&) = default; |
| 34 | MainLoop& operator=(MainLoop&&) = default; |
| 35 | ~MainLoop() = default; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 36 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 37 | /** @brief Constructor |
| 38 | * |
| 39 | * @param[in] bus - sdbusplus bus client connection. |
| 40 | * @param[in] param - the path parameter provided |
| 41 | * @param[in] path - hwmon sysfs instance to manage |
| 42 | * @param[in] devPath - physical device sysfs path. |
| 43 | * @param[in] prefix - DBus busname prefix. |
| 44 | * @param[in] root - DBus sensors namespace root. |
| 45 | * |
| 46 | * Any DBus objects are created relative to the DBus |
| 47 | * sensors namespace root. |
| 48 | * |
| 49 | * At startup, the application will own a busname with |
| 50 | * the format <prefix>.hwmon<n>. |
| 51 | */ |
| 52 | MainLoop(sdbusplus::bus::bus&& bus, const std::string& param, |
| 53 | const std::string& path, const std::string& devPath, |
| 54 | const char* prefix, const char* root); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 55 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 56 | /** @brief Setup polling timer in a sd event loop and attach to D-Bus |
| 57 | * event loop. |
| 58 | */ |
| 59 | void run(); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 60 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 61 | /** @brief Stop polling timer event loop from another thread. |
| 62 | * |
| 63 | * Typically only used by testcases. |
| 64 | */ |
| 65 | void shutdown() noexcept; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 66 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 67 | private: |
| 68 | using mapped_type = |
| 69 | std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>; |
| 70 | using SensorState = std::map<SensorSet::key_type, mapped_type>; |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 71 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 72 | /** @brief Read hwmon sysfs entries */ |
| 73 | void read(); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 74 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 75 | /** @brief Set up D-Bus object state */ |
| 76 | void init(); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 77 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 78 | /** @brief sdbusplus bus client connection. */ |
| 79 | sdbusplus::bus::bus _bus; |
| 80 | /** @brief sdbusplus freedesktop.ObjectManager storage. */ |
| 81 | sdbusplus::server::manager::manager _manager; |
| 82 | /** @brief the parameter path used. */ |
| 83 | std::string _pathParam; |
| 84 | /** @brief hwmon sysfs class path. */ |
| 85 | std::string _hwmonRoot; |
| 86 | /** @brief hwmon sysfs instance. */ |
| 87 | std::string _instance; |
| 88 | /** @brief physical device sysfs path. */ |
| 89 | std::string _devPath; |
| 90 | /** @brief DBus busname prefix. */ |
| 91 | const char* _prefix; |
| 92 | /** @brief DBus sensors namespace root. */ |
| 93 | const char* _root; |
| 94 | /** @brief DBus object state. */ |
| 95 | SensorState state; |
| 96 | /** @brief Sleep interval in microseconds. */ |
| 97 | uint64_t _interval = default_interval; |
| 98 | /** @brief Hwmon sysfs access. */ |
| 99 | hwmonio::HwmonIO ioAccess; |
| 100 | /** @brief Timer */ |
| 101 | std::unique_ptr<phosphor::hwmon::Timer> timer; |
| 102 | /** @brief the sd_event structure */ |
| 103 | sd_event* loop = nullptr; |
| 104 | /** @brief Store the specifications of sensor objects */ |
| 105 | std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>> |
| 106 | sensorObjects; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 107 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 108 | /** |
| 109 | * @brief Map of removed sensors |
| 110 | */ |
| 111 | std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 112 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 113 | /** |
| 114 | * @brief Get the ID of the sensor |
| 115 | * |
| 116 | * @param[in] sensor - Sensor to get the ID of |
| 117 | */ |
| 118 | std::string getID(SensorSet::container_t::const_reference sensor); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 119 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 120 | /** |
| 121 | * @brief Get the sensor identifiers |
| 122 | * |
| 123 | * @param[in] sensor - Sensor to get the identifiers of |
| 124 | */ |
| 125 | SensorIdentifiers |
| 126 | getIdentifiers(SensorSet::container_t::const_reference sensor); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 127 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 128 | /** |
| 129 | * @brief Used to create and add sensor objects |
| 130 | * |
| 131 | * @param[in] sensor - Sensor to create/add object for |
| 132 | * |
| 133 | * @return - Optional |
| 134 | * Object state data on success, nothing on failure |
| 135 | */ |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame^] | 136 | std::optional<ObjectStateData> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 137 | getObject(SensorSet::container_t::const_reference sensor); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 138 | }; |