| 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. | 
| Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame^] | 44 | *  @param[in] param - the path parameter provided | 
| Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 45 | *  @param[in] path - hwmon sysfs instance to manage | 
| Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 46 | *  @param[in] devPath - physical device sysfs path. | 
| Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 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>. | 
| Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 55 | */ | 
| Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 56 | MainLoop( | 
| Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 57 | sdbusplus::bus::bus&& bus, | 
| Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame^] | 58 | const std::string& param, | 
| Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 59 | const std::string& path, | 
| Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 60 | const std::string& devPath, | 
| Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 61 | const char* prefix, | 
|  | 62 | const char* root); | 
| Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 63 |  | 
| Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 64 | /** @brief Setup polling timer in a sd event loop and attach to D-Bus | 
|  | 65 | *         event loop. | 
|  | 66 | */ | 
| Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 67 | void run(); | 
|  | 68 |  | 
| Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 69 | /** @brief Stop polling timer event loop from another thread. | 
| Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 70 | * | 
|  | 71 | *  Typically only used by testcases. | 
|  | 72 | */ | 
|  | 73 | void shutdown() noexcept; | 
|  | 74 |  | 
|  | 75 | private: | 
| Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 76 | using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>; | 
| Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 77 | using SensorState = std::map<SensorSet::key_type, mapped_type>; | 
|  | 78 |  | 
| Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 79 | /** @brief Read hwmon sysfs entries */ | 
|  | 80 | void read(); | 
|  | 81 |  | 
|  | 82 | /** @brief Set up D-Bus object state */ | 
|  | 83 | void init(); | 
|  | 84 |  | 
| Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 85 | /** @brief sdbusplus bus client connection. */ | 
|  | 86 | sdbusplus::bus::bus _bus; | 
|  | 87 | /** @brief sdbusplus freedesktop.ObjectManager storage. */ | 
|  | 88 | sdbusplus::server::manager::manager _manager; | 
| Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame^] | 89 | /** @brief the parameter path used. */ | 
|  | 90 | std::string _pathParam; | 
| Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 91 | /** @brief hwmon sysfs class path. */ | 
|  | 92 | std::string _hwmonRoot; | 
|  | 93 | /** @brief hwmon sysfs instance. */ | 
|  | 94 | std::string _instance; | 
| Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 95 | /** @brief physical device sysfs path. */ | 
|  | 96 | std::string _devPath; | 
| Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 97 | /** @brief DBus busname prefix. */ | 
|  | 98 | const char* _prefix; | 
|  | 99 | /** @brief DBus sensors namespace root. */ | 
|  | 100 | const char* _root; | 
| Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 101 | /** @brief DBus object state. */ | 
|  | 102 | SensorState state; | 
| Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 103 | /** @brief Sleep interval in microseconds. */ | 
|  | 104 | uint64_t _interval = default_interval; | 
| Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 105 | /** @brief Hwmon sysfs access. */ | 
| Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 106 | hwmonio::HwmonIO ioAccess; | 
| Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 107 | /** @brief Timer */ | 
|  | 108 | std::unique_ptr<phosphor::hwmon::Timer> timer; | 
|  | 109 | /** @brief the sd_event structure */ | 
|  | 110 | sd_event* loop = nullptr; | 
| 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 | }; |