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