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