blob: d1fe9b058c37a998caa8422aa4fe164b6bdd2a63 [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>;
Brad Bishopd499ca62016-12-19 09:24:50 -050017
18/** @class MainLoop
19 * @brief hwmon-readd main application loop.
20 */
21class 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 Bishop9c7b6e02016-12-19 12:43:36 -050033 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050034 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040035 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050036 * @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 Bishopd499ca62016-12-19 09:24:50 -050044 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050045 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050046 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050047 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040048 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050049 const char* prefix,
50 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050051
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060052 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
53 * event loop.
54 */
Brad Bishopd499ca62016-12-19 09:24:50 -050055 void run();
56
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060057 /** @brief Stop polling timer event loop from another thread.
Brad Bishopd499ca62016-12-19 09:24:50 -050058 *
59 * Typically only used by testcases.
60 */
61 void shutdown() noexcept;
62
63 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050064 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050065 using SensorState = std::map<SensorSet::key_type, mapped_type>;
66
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060067 /** @brief Read hwmon sysfs entries */
68 void read();
69
70 /** @brief Set up D-Bus object state */
71 void init();
72
Brad Bishop9c7b6e02016-12-19 12:43:36 -050073 /** @brief sdbusplus bus client connection. */
74 sdbusplus::bus::bus _bus;
75 /** @brief sdbusplus freedesktop.ObjectManager storage. */
76 sdbusplus::server::manager::manager _manager;
Brad Bishopb8740fc2017-02-24 23:38:37 -050077 /** @brief hwmon sysfs class path. */
78 std::string _hwmonRoot;
79 /** @brief hwmon sysfs instance. */
80 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040081 /** @brief physical device sysfs path. */
82 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050083 /** @brief DBus busname prefix. */
84 const char* _prefix;
85 /** @brief DBus sensors namespace root. */
86 const char* _root;
Matthew Bartha23babd2018-03-16 10:03:27 -050087 /** @brief hwmon instance is for an OCC. */
88 bool _isOCC = false;
Brad Bishop3c344d32017-01-05 11:48:39 -050089 /** @brief DBus object state. */
90 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -070091 /** @brief Sleep interval in microseconds. */
92 uint64_t _interval = default_interval;
Brad Bishop751043e2017-08-29 11:13:46 -040093 /** @brief Hwmon sysfs access. */
94 sysfs::hwmonio::HwmonIO ioAccess;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060095 /** @brief Timer */
96 std::unique_ptr<phosphor::hwmon::Timer> timer;
97 /** @brief the sd_event structure */
98 sd_event* loop = nullptr;
Matthew Barth31d214c2018-03-26 09:54:27 -050099
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 Bishopd499ca62016-12-19 09:24:50 -0500111};