blob: 87f6ed0b5ec2e0f2cc3cdc03db1db98fdc7fdff8 [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001#pragma once
2
Patrick Venture043d3232018-08-31 10:10:53 -07003#include "hwmonio.hpp"
4#include "interface.hpp"
5#include "sensor.hpp"
6#include "sensorset.hpp"
7#include "sysfs.hpp"
8#include "timer.hpp"
9#include "types.hpp"
10
William A. Kennington III4cbdfef2018-10-18 19:19:51 -070011#include <any>
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060012#include <memory>
William A. Kennington III4cbdfef2018-10-18 19:19:51 -070013#include <optional>
Brad Bishop9c7b6e02016-12-19 12:43:36 -050014#include <sdbusplus/server.hpp>
William A. Kennington III0fe4cb32018-10-18 19:19:58 -070015#include <sdeventplus/event.hpp>
Patrick Venture043d3232018-08-31 10:10:53 -070016#include <string>
17#include <vector>
Brad Bishop075f7a22017-01-06 09:45:08 -050018
Patrick Ventureab10f162017-05-22 09:44:50 -070019static constexpr auto default_interval = 1000000;
20
Matthew Barth979c8062018-04-17 11:37:15 -050021static constexpr auto sensorID = 0;
22static constexpr auto sensorLabel = 1;
23using SensorIdentifiers = std::tuple<std::string, std::string>;
24
Brad Bishopd499ca62016-12-19 09:24:50 -050025/** @class MainLoop
26 * @brief hwmon-readd main application loop.
27 */
28class MainLoop
29{
Patrick Venture043d3232018-08-31 10:10:53 -070030 public:
31 MainLoop() = delete;
32 MainLoop(const MainLoop&) = delete;
33 MainLoop& operator=(const MainLoop&) = delete;
34 MainLoop(MainLoop&&) = default;
35 MainLoop& operator=(MainLoop&&) = default;
36 ~MainLoop() = default;
Brad Bishopd499ca62016-12-19 09:24:50 -050037
Patrick Venture043d3232018-08-31 10:10:53 -070038 /** @brief Constructor
39 *
40 * @param[in] bus - sdbusplus bus client connection.
41 * @param[in] param - the path parameter provided
42 * @param[in] path - hwmon sysfs instance to manage
43 * @param[in] devPath - physical device sysfs path.
44 * @param[in] prefix - DBus busname prefix.
45 * @param[in] root - DBus sensors namespace root.
46 *
47 * Any DBus objects are created relative to the DBus
48 * sensors namespace root.
49 *
50 * At startup, the application will own a busname with
51 * the format <prefix>.hwmon<n>.
52 */
53 MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
54 const std::string& path, const std::string& devPath,
55 const char* prefix, const char* root);
Brad Bishopd499ca62016-12-19 09:24:50 -050056
Patrick Venture043d3232018-08-31 10:10:53 -070057 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
58 * event loop.
59 */
60 void run();
Brad Bishopd499ca62016-12-19 09:24:50 -050061
Patrick Venture043d3232018-08-31 10:10:53 -070062 /** @brief Stop polling timer event loop from another thread.
63 *
64 * Typically only used by testcases.
65 */
66 void shutdown() noexcept;
Brad Bishopd499ca62016-12-19 09:24:50 -050067
Patrick Venture043d3232018-08-31 10:10:53 -070068 private:
69 using mapped_type =
70 std::tuple<SensorSet::mapped_type, std::string, ObjectInfo>;
71 using SensorState = std::map<SensorSet::key_type, mapped_type>;
Brad Bishop3c344d32017-01-05 11:48:39 -050072
Patrick Venture043d3232018-08-31 10:10:53 -070073 /** @brief Read hwmon sysfs entries */
74 void read();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060075
Patrick Venture043d3232018-08-31 10:10:53 -070076 /** @brief Set up D-Bus object state */
77 void init();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -060078
Patrick Venture043d3232018-08-31 10:10:53 -070079 /** @brief sdbusplus bus client connection. */
80 sdbusplus::bus::bus _bus;
81 /** @brief sdbusplus freedesktop.ObjectManager storage. */
82 sdbusplus::server::manager::manager _manager;
83 /** @brief the parameter path used. */
84 std::string _pathParam;
85 /** @brief hwmon sysfs class path. */
86 std::string _hwmonRoot;
87 /** @brief hwmon sysfs instance. */
88 std::string _instance;
89 /** @brief physical device sysfs path. */
90 std::string _devPath;
91 /** @brief DBus busname prefix. */
92 const char* _prefix;
93 /** @brief DBus sensors namespace root. */
94 const char* _root;
95 /** @brief DBus object state. */
96 SensorState state;
97 /** @brief Sleep interval in microseconds. */
98 uint64_t _interval = default_interval;
99 /** @brief Hwmon sysfs access. */
100 hwmonio::HwmonIO ioAccess;
101 /** @brief Timer */
102 std::unique_ptr<phosphor::hwmon::Timer> timer;
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700103 /** @brief the Event Loop structure */
104 sdeventplus::Event event;
Patrick Venture043d3232018-08-31 10:10:53 -0700105 /** @brief Store the specifications of sensor objects */
106 std::map<SensorSet::key_type, std::unique_ptr<sensor::Sensor>>
107 sensorObjects;
Matthew Barth31d214c2018-03-26 09:54:27 -0500108
Patrick Venture043d3232018-08-31 10:10:53 -0700109 /**
110 * @brief Map of removed sensors
111 */
112 std::map<SensorSet::key_type, SensorSet::mapped_type> rmSensors;
Matthew Barth31d214c2018-03-26 09:54:27 -0500113
Patrick Venture043d3232018-08-31 10:10:53 -0700114 /**
115 * @brief Get the ID of the sensor
116 *
117 * @param[in] sensor - Sensor to get the ID of
118 */
119 std::string getID(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500120
Patrick Venture043d3232018-08-31 10:10:53 -0700121 /**
122 * @brief Get the sensor identifiers
123 *
124 * @param[in] sensor - Sensor to get the identifiers of
125 */
126 SensorIdentifiers
127 getIdentifiers(SensorSet::container_t::const_reference sensor);
Matthew Barth979c8062018-04-17 11:37:15 -0500128
Patrick Venture043d3232018-08-31 10:10:53 -0700129 /**
130 * @brief Used to create and add sensor objects
131 *
132 * @param[in] sensor - Sensor to create/add object for
133 *
134 * @return - Optional
135 * Object state data on success, nothing on failure
136 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700137 std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700138 getObject(SensorSet::container_t::const_reference sensor);
Brad Bishopd499ca62016-12-19 09:24:50 -0500139};