blob: c279e0b816a9e09e5a717abd5ee1d380b65151df [file] [log] [blame]
George Hunga661c162020-07-31 18:45:32 +08001From 2cea5d289b278db4468b16bf6b64102655811fd0 Mon Sep 17 00:00:00 2001
2From: Eddielu <Eddie.Lu@quantatw.com>
3Date: Mon, 27 Jul 2020 20:30:22 +0800
4Subject: [PATCH] Update lev-add-poweron-monitor-feature patch.
George Hunga66f2b92020-05-19 15:52:33 +08005
6---
7 Makefile.am | 2 ++
George Hunga661c162020-07-31 18:45:32 +08008 mainloop.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
George Hunga66f2b92020-05-19 15:52:33 +08009 sensor.cpp | 11 ++++++-
10 sensor.hpp | 13 ++++++++
11 thresholds.hpp | 2 --
George Hunga661c162020-07-31 18:45:32 +080012 5 files changed, 127 insertions(+), 3 deletions(-)
George Hunga66f2b92020-05-19 15:52:33 +080013
14diff --git a/Makefile.am b/Makefile.am
George Hunga661c162020-07-31 18:45:32 +080015index 706a6cc..c620fa4 100644
George Hunga66f2b92020-05-19 15:52:33 +080016--- a/Makefile.am
17+++ b/Makefile.am
18@@ -46,6 +46,7 @@ libhwmon_la_LIBADD = \
19 $(SDEVENTPLUS_LIBS) \
20 $(PHOSPHOR_DBUS_INTERFACES_LIBS) \
21 $(PHOSPHOR_LOGGING_LIBS) \
22+ $(PTHREAD_LIBS) \
23 $(GPIOPLUS_LIBS) \
24 $(STDPLUS_LIBS) \
George Hunga661c162020-07-31 18:45:32 +080025 $(CODE_COVERAGE_LIBS) \
26@@ -55,6 +56,7 @@ libhwmon_la_CXXFLAGS = \
George Hunga66f2b92020-05-19 15:52:33 +080027 $(SDEVENTPLUS_CFLAGS) \
28 $(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
29 $(PHOSPHOR_LOGGING_CFLAGS) \
30+ $(PTHREAD_CFLAGS) \
31 $(STDPLUS_CFLAGS) \
32 $(CODE_COVERAGE_CXXFLAGS)
33
34diff --git a/mainloop.cpp b/mainloop.cpp
George Hunga661c162020-07-31 18:45:32 +080035index 3e7e0bf..4789a80 100644
George Hunga66f2b92020-05-19 15:52:33 +080036--- a/mainloop.cpp
37+++ b/mainloop.cpp
George Hunga661c162020-07-31 18:45:32 +080038@@ -41,6 +41,12 @@
George Hunga66f2b92020-05-19 15:52:33 +080039 #include <string>
40 #include <unordered_set>
41 #include <xyz/openbmc_project/Sensor/Device/error.hpp>
42+#include <boost/container/flat_map.hpp>
43+#include <boost/algorithm/string/predicate.hpp>
44+#include <sdbusplus/asio/connection.hpp>
45+#include <sdbusplus/asio/object_server.hpp>
46+#include <sdbusplus/message/types.hpp>
47+#include <sdbusplus/timer.hpp>
48
49 using namespace phosphor::logging;
50
George Hunga661c162020-07-31 18:45:32 +080051@@ -76,6 +82,12 @@ decltype(
George Hunga66f2b92020-05-19 15:52:33 +080052 Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
53 &CriticalObject::criticalAlarmHigh;
54
55+static std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
56+static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
57+static bool powerStatusOn = false;
George Hunga661c162020-07-31 18:45:32 +080058+static boost::asio::io_service io;
George Hunga66f2b92020-05-19 15:52:33 +080059+static auto conn = std::make_shared<sdbusplus::asio::connection>(io);
60+
George Hunga661c162020-07-31 18:45:32 +080061 void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
George Hunga66f2b92020-05-19 15:52:33 +080062 {
63 for (auto& iface : ifaces)
George Hunga661c162020-07-31 18:45:32 +080064@@ -103,6 +115,84 @@ void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
George Hunga66f2b92020-05-19 15:52:33 +080065 }
66 }
67
68+void powerStatusSet()
69+{
70+ powerStatusOn = true;
71+ return;
72+}
73+
74+void createTimer()
75+{
76+ if (cacheTimer == nullptr)
77+ {
78+ cacheTimer = std::make_unique<phosphor::Timer>(powerStatusSet);
79+ }
80+}
81+
82+bool isPowerOn(void)
83+{
84+ if (!powerMatch)
85+ {
86+ throw std::runtime_error("Power Match Not Created");
87+ }
88+ return powerStatusOn;
89+}
90+
91+void setupPowerMatch(sdbusplus::bus::bus& bus)
92+{
93+ if (powerMatch)
94+ {
95+ return;
96+ }
97+
98+ powerMatch = std::make_unique<sdbusplus::bus::match::match>(
99+ bus,
100+ "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
101+ "openbmc_project/state/"
102+ "host0',arg0='xyz.openbmc_project.State.Host'",
103+ [](sdbusplus::message::message& message) {
104+ std::string objectName;
105+ boost::container::flat_map<std::string, std::variant<std::string>>
106+ values;
107+ message.read(objectName, values);
108+ auto findState = values.find("CurrentHostState");
109+ if (findState != values.end())
110+ {
111+ bool on = boost::ends_with(
112+ std::get<std::string>(findState->second), "Running");
113+ if (!on)
114+ {
115+ cacheTimer->stop();
116+ powerStatusOn = false;
117+ return;
118+ }
119+ cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
120+ std::chrono::seconds(10)));
121+ }
122+ else {
123+ powerStatusOn = false;
124+ }
125+ });
126+
127+ conn->async_method_call(
128+ [](boost::system::error_code ec,
129+ const std::variant<std::string>& state) {
130+ if (ec)
131+ {
132+ return;
133+ }
134+ powerStatusOn =
135+ boost::ends_with(std::get<std::string>(state), "Running");
136+ },
George Hunga661c162020-07-31 18:45:32 +0800137+ "xyz.openbmc_project.State.Host",
138+ "/xyz/openbmc_project/state/host0",
George Hunga66f2b92020-05-19 15:52:33 +0800139+ "org.freedesktop.DBus.Properties", "Get",
140+ "xyz.openbmc_project.State.Host", "CurrentHostState");
141+
142+ createTimer();
143+}
144+
George Hunga661c162020-07-31 18:45:32 +0800145+
George Hunga66f2b92020-05-19 15:52:33 +0800146 std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
147 {
148 std::string id;
George Hunga661c162020-07-31 18:45:32 +0800149@@ -384,6 +474,7 @@ void MainLoop::init()
George Hunga66f2b92020-05-19 15:52:33 +0800150 _interval = std::strtoull(interval.c_str(), NULL, 10);
151 }
152 }
153+ setupPowerMatch(_bus);
154 }
155
156 void MainLoop::read()
George Hunga661c162020-07-31 18:45:32 +0800157@@ -428,6 +519,12 @@ void MainLoop::read()
George Hunga66f2b92020-05-19 15:52:33 +0800158
159 try
160 {
161+ if(sensor->pwrOnMonitor() && !isPowerOn())
162+ {
163+ statusIface->functional(false);
164+ continue;
165+ }
166+
167 if (sensor->hasFaultFile())
168 {
169 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
George Hunga661c162020-07-31 18:45:32 +0800170@@ -490,6 +587,11 @@ void MainLoop::read()
George Hunga66f2b92020-05-19 15:52:33 +0800171 }
172 }
173
174+ if(sensor->pwrOnMonitor() && !isPowerOn())
175+ {
176+ statusIface->functional(false);
George Hunga661c162020-07-31 18:45:32 +0800177+ continue;
George Hunga66f2b92020-05-19 15:52:33 +0800178+ }
179 updateSensorInterfaces(obj, value);
180 }
181 catch (const std::system_error& e)
182diff --git a/sensor.cpp b/sensor.cpp
George Hunga661c162020-07-31 18:45:32 +0800183index 09aeca6..b1cb470 100644
George Hunga66f2b92020-05-19 15:52:33 +0800184--- a/sensor.cpp
185+++ b/sensor.cpp
George Hunga661c162020-07-31 18:45:32 +0800186@@ -31,7 +31,7 @@ Sensor::Sensor(const SensorSet::key_type& sensor,
George Hunga66f2b92020-05-19 15:52:33 +0800187 const hwmonio::HwmonIOInterface* ioAccess,
188 const std::string& devPath) :
189 _sensor(sensor),
190- _ioAccess(ioAccess), _devPath(devPath), _scale(0), _hasFaultFile(false)
191+ _ioAccess(ioAccess), _devPath(devPath), _scale(0), _hasFaultFile(false), _pwrOnMonitor(false)
192 {
193 auto chip = env::getEnv("GPIOCHIP", sensor);
194 auto access = env::getEnv("GPIO", sensor);
George Hunga661c162020-07-31 18:45:32 +0800195@@ -60,6 +60,15 @@ Sensor::Sensor(const SensorSet::key_type& sensor,
George Hunga66f2b92020-05-19 15:52:33 +0800196 auto senRmRCs = env::getEnv("REMOVERCS", sensor);
197 // Add sensor removal return codes defined per sensor
198 addRemoveRCs(senRmRCs);
199+
200+ auto pwrOnMon = env::getEnv("PWRONMON", sensor);
201+ if (!pwrOnMon.empty())
202+ {
203+ if (pwrOnMon == "ON")
204+ {
205+ _pwrOnMonitor = true;
206+ }
207+ }
208 }
209
210 void Sensor::addRemoveRCs(const std::string& rcList)
211diff --git a/sensor.hpp b/sensor.hpp
212index 4b2d281..369a252 100644
213--- a/sensor.hpp
214+++ b/sensor.hpp
215@@ -135,6 +135,16 @@ class Sensor
216 return _hasFaultFile;
217 }
218
219+ /**
220+ * @brief Get whether the sensor only need to be monitored in power on state or not.
221+ *
222+ * @return - Boolean on whether the sensor only need to be monitored in power on state
223+ */
224+ inline bool pwrOnMonitor(void) const
225+ {
226+ return _pwrOnMonitor;
227+ }
228+
229 private:
230 /** @brief Sensor object's identifiers */
231 SensorSet::key_type _sensor;
232@@ -156,6 +166,9 @@ class Sensor
233
234 /** @brief Tracks whether the sensor has a fault file or not. */
235 bool _hasFaultFile;
236+
237+ /** @brief Whether the sensor only need to be monitored in power on state or not. */
238+ bool _pwrOnMonitor;
239 };
240
241 /**
242diff --git a/thresholds.hpp b/thresholds.hpp
243index 4d2fcff..972a469 100644
244--- a/thresholds.hpp
245+++ b/thresholds.hpp
246@@ -101,8 +101,6 @@ auto addThreshold(const std::string& sensorType, const std::string& sensorID,
247 auto hi = stod(tHi) * std::pow(10, scale);
248 (*iface.*Thresholds<T>::setLo)(lo);
249 (*iface.*Thresholds<T>::setHi)(hi);
250- (*iface.*Thresholds<T>::alarmLo)(value <= lo);
251- (*iface.*Thresholds<T>::alarmHi)(value >= hi);
252 auto type = Thresholds<T>::type;
253 obj[type] = iface;
254 }
255--
2562.7.4
257