blob: 3fbc059cedb7a654c06308265593f27f3ccb5dfe [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>
Matthew Barthd238e232018-04-17 12:01:50 -05006#include <experimental/optional>
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -06007#include <memory>
Brad Bishop9c7b6e02016-12-19 12:43:36 -05008#include <sdbusplus/server.hpp>
Patrick Venture75e56c62018-04-20 18:10:15 -07009#include "hwmonio.hpp"
Brad Bishop3c344d32017-01-05 11:48:39 -050010#include "sensorset.hpp"
Brad Bishop751043e2017-08-29 11:13:46 -040011#include "sysfs.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050012#include "interface.hpp"
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060013#include "timer.hpp"
Brad Bishop075f7a22017-01-06 09:45:08 -050014
Patrick Ventureab10f162017-05-22 09:44:50 -070015static constexpr auto default_interval = 1000000;
16
Brad Bishop075f7a22017-01-06 09:45:08 -050017using Object = std::map<InterfaceType, std::experimental::any>;
Brad Bishopf7426cf2017-01-06 15:36:43 -050018using ObjectInfo = std::tuple<sdbusplus::bus::bus*, std::string, Object>;
Matthew Barthd4beecf2018-04-03 15:50:22 -050019using RetryIO = std::tuple<size_t, std::chrono::milliseconds>;
Matthew Barthd238e232018-04-17 12:01:50 -050020using ObjectStateData = std::pair<std::string, ObjectInfo>;
Brad Bishopd499ca62016-12-19 09:24:50 -050021
Matthew Barth979c8062018-04-17 11:37:15 -050022static constexpr auto sensorID = 0;
23static constexpr auto sensorLabel = 1;
24using SensorIdentifiers = std::tuple<std::string, std::string>;
25
Matthew Barthd238e232018-04-17 12:01:50 -050026namespace optional_ns = std::experimental;
27
Brad Bishopd499ca62016-12-19 09:24:50 -050028/** @class MainLoop
29 * @brief hwmon-readd main application loop.
30 */
31class 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 Bishop9c7b6e02016-12-19 12:43:36 -050043 * @param[in] bus - sdbusplus bus client connection.
Brad Bishopd499ca62016-12-19 09:24:50 -050044 * @param[in] path - hwmon sysfs instance to manage
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040045 * @param[in] devPath - physical device sysfs path.
Brad Bishopb9e2b072016-12-19 13:47:10 -050046 * @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 Bishopd499ca62016-12-19 09:24:50 -050054 */
Brad Bishopb9e2b072016-12-19 13:47:10 -050055 MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -050056 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -050057 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040058 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -050059 const char* prefix,
60 const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050061
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060062 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
63 * event loop.
64 */
Brad Bishopd499ca62016-12-19 09:24:50 -050065 void run();
66
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060067 /** @brief Stop polling timer event loop from another thread.
Brad Bishopd499ca62016-12-19 09:24:50 -050068 *
69 * Typically only used by testcases.
70 */
71 void shutdown() noexcept;
72
73 private:
Brad Bishopf7426cf2017-01-06 15:36:43 -050074 using mapped_type = std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
Brad Bishop3c344d32017-01-05 11:48:39 -050075 using SensorState = std::map<SensorSet::key_type, mapped_type>;
76
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060077 /** @brief Read hwmon sysfs entries */
78 void read();
79
80 /** @brief Set up D-Bus object state */
81 void init();
82
Brad Bishop9c7b6e02016-12-19 12:43:36 -050083 /** @brief sdbusplus bus client connection. */
84 sdbusplus::bus::bus _bus;
85 /** @brief sdbusplus freedesktop.ObjectManager storage. */
86 sdbusplus::server::manager::manager _manager;
Brad Bishopb8740fc2017-02-24 23:38:37 -050087 /** @brief hwmon sysfs class path. */
88 std::string _hwmonRoot;
89 /** @brief hwmon sysfs instance. */
90 std::string _instance;
Brad Bishopf3aa9ae2017-08-25 09:56:02 -040091 /** @brief physical device sysfs path. */
92 std::string _devPath;
Brad Bishopb9e2b072016-12-19 13:47:10 -050093 /** @brief DBus busname prefix. */
94 const char* _prefix;
95 /** @brief DBus sensors namespace root. */
96 const char* _root;
Matthew Bartha23babd2018-03-16 10:03:27 -050097 /** @brief hwmon instance is for an OCC. */
98 bool _isOCC = false;
Brad Bishop3c344d32017-01-05 11:48:39 -050099 /** @brief DBus object state. */
100 SensorState state;
Patrick Ventureab10f162017-05-22 09:44:50 -0700101 /** @brief Sleep interval in microseconds. */
102 uint64_t _interval = default_interval;
Brad Bishop751043e2017-08-29 11:13:46 -0400103 /** @brief Hwmon sysfs access. */
Patrick Venture75e56c62018-04-20 18:10:15 -0700104 hwmonio::HwmonIO ioAccess;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600105 /** @brief Timer */
106 std::unique_ptr<phosphor::hwmon::Timer> timer;
107 /** @brief the sd_event structure */
108 sd_event* loop = nullptr;
Matthew Barth31d214c2018-03-26 09:54:27 -0500109
110 /**
111 * @brief Map of removed sensors
112 */
113 std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors;
114
115 /**
Matthew Barth979c8062018-04-17 11:37:15 -0500116 * @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 Barth31d214c2018-03-26 09:54:27 -0500131 * @brief Used to create and add sensor objects
132 *
133 * @param[in] sensor - Sensor to create/add object for
Matthew Barthd238e232018-04-17 12:01:50 -0500134 *
135 * @return - Optional
136 * Object state data on success, nothing on failure
Matthew Barth31d214c2018-03-26 09:54:27 -0500137 */
Matthew Barthd238e232018-04-17 12:01:50 -0500138 optional_ns::optional<ObjectStateData> getObject(
139 SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500140};