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