blob: 9a515803e11b2be002f52ce88e9ceb7938cfcabb [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001#pragma once
2
Brad Bishopd499ca62016-12-19 09:24:50 -05003#include <string>
Brad Bishop075f7a22017-01-06 09:45:08 -05004#include <vector>
5#include <experimental/any>
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -06006#include <memory>
Brad Bishop9c7b6e02016-12-19 12:43:36 -05007#include <sdbusplus/server.hpp>
Brad Bishop3c344d32017-01-05 11:48:39 -05008#include "sensorset.hpp"
Brad Bishop751043e2017-08-29 11:13:46 -04009#include "sysfs.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050010#include "interface.hpp"
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060011#include "timer.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050012
Patrick Ventureab10f162017-05-22 09:44:50 -070013static constexpr auto default_interval = 1000000;
14
Brad Bishop075f7a22017-01-06 09:45:08 -050015using Object = std::map<InterfaceType, std::experimental::any>;
Brad Bishopf7426cf2017-01-06 15:36:43 -050016using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
Matthew Barthd4beecf2018-04-03 15:50:22 -050017using RetryIO = std::tuple<size_t, std::chrono::milliseconds>;
Brad Bishopd499ca62016-12-19 09:24:50 -050018
19/** @class MainLoop
20 * @brief hwmon-readd main application loop.
21 */
22class MainLoop
23{
24 public:
25 MainLoop() = delete;
26 MainLoop(const MainLoop&) = delete;
27 MainLoop& operator=(const MainLoop&) = delete;
28 MainLoop(MainLoop&&) = default;
29 MainLoop& operator=(MainLoop&&) = default;
30 ~MainLoop() = default;
31
32 /** @brief Constructor
33 *
Brad Bishop9c7b6e02016-12-19 12:43:36 -050034 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050035 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040036 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050037 * @param[in] prefix - DBus busname prefix.
38 * @param[in] root - DBus sensors namespace root.
39 *
40 * Any DBus objects are created relative to the DBus
41 * sensors namespace root.
42 *
43 * At startup, the application will own a busname with
44 * the format <prefix>.hwmon<n>.
Brad Bishopd499ca62016-12-19 09:24:50 -050045 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050046 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050047 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050048 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040049 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050050 const char* prefix,
51 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050052
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060053 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
54 * event loop.
55 */
Brad Bishopd499ca62016-12-19 09:24:50 -050056 void run();
57
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060058 /** @brief Stop polling timer event loop from another thread.
Brad Bishopd499ca62016-12-19 09:24:50 -050059 *
60 * Typically only used by testcases.
61 */
62 void shutdown() noexcept;
63
64 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050065 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050066 using SensorState = std::map<SensorSet::key_type, mapped_type>;
67
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060068 /** @brief Read hwmon sysfs entries */
69 void read();
70
71 /** @brief Set up D-Bus object state */
72 void init();
73
Brad Bishop9c7b6e02016-12-19 12:43:36 -050074 /** @brief sdbusplus bus client connection. */
75 sdbusplus::bus::bus _bus;
76 /** @brief sdbusplus freedesktop.ObjectManager storage. */
77 sdbusplus::server::manager::manager _manager;
Brad Bishopb8740fc2017-02-24 23:38:37 -050078 /** @brief hwmon sysfs class path. */
79 std::string _hwmonRoot;
80 /** @brief hwmon sysfs instance. */
81 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040082 /** @brief physical device sysfs path. */
83 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050084 /** @brief DBus busname prefix. */
85 const char* _prefix;
86 /** @brief DBus sensors namespace root. */
87 const char* _root;
Matthew Bartha23babd2018-03-16 10:03:27 -050088 /** @brief hwmon instance is for an OCC. */
89 bool _isOCC = false;
Brad Bishop3c344d32017-01-05 11:48:39 -050090 /** @brief DBus object state. */
91 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -070092 /** @brief Sleep interval in microseconds. */
93 uint64_t _interval = default_interval;
Brad Bishop751043e2017-08-29 11:13:46 -040094 /** @brief Hwmon sysfs access. */
95 sysfs::hwmonio::HwmonIO ioAccess;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060096 /** @brief Timer */
97 std::unique_ptr<phosphor::hwmon::Timer> timer;
98 /** @brief the sd_event structure */
99 sd_event* loop = nullptr;
Matthew Barth31d214c2018-03-26 09:54:27 -0500100
101 /**
102 * @brief Map of removed sensors
103 */
104 std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors;
105
106 /**
107 * @brief Used to create and add sensor objects
108 *
109 * @param[in] sensor - Sensor to create/add object for
110 */
111 void getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500112};