George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 1 | From 2cea5d289b278db4468b16bf6b64102655811fd0 Mon Sep 17 00:00:00 2001 |
| 2 | From: Eddielu <Eddie.Lu@quantatw.com> |
| 3 | Date: Mon, 27 Jul 2020 20:30:22 +0800 |
| 4 | Subject: [PATCH] Update lev-add-poweron-monitor-feature patch. |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 5 | |
| 6 | --- |
| 7 | Makefile.am | 2 ++ |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 8 | mainloop.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 9 | sensor.cpp | 11 ++++++- |
| 10 | sensor.hpp | 13 ++++++++ |
| 11 | thresholds.hpp | 2 -- |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 12 | 5 files changed, 127 insertions(+), 3 deletions(-) |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 13 | |
| 14 | diff --git a/Makefile.am b/Makefile.am |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 15 | index 706a6cc..c620fa4 100644 |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 16 | --- 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 Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 25 | $(CODE_COVERAGE_LIBS) \ |
| 26 | @@ -55,6 +56,7 @@ libhwmon_la_CXXFLAGS = \ |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 27 | $(SDEVENTPLUS_CFLAGS) \ |
| 28 | $(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \ |
| 29 | $(PHOSPHOR_LOGGING_CFLAGS) \ |
| 30 | + $(PTHREAD_CFLAGS) \ |
| 31 | $(STDPLUS_CFLAGS) \ |
| 32 | $(CODE_COVERAGE_CXXFLAGS) |
| 33 | |
| 34 | diff --git a/mainloop.cpp b/mainloop.cpp |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 35 | index 3e7e0bf..4789a80 100644 |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 36 | --- a/mainloop.cpp |
| 37 | +++ b/mainloop.cpp |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 38 | @@ -41,6 +41,12 @@ |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 39 | #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 Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 51 | @@ -76,6 +82,12 @@ decltype( |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 52 | 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 Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 58 | +static boost::asio::io_service io; |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 59 | +static auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 60 | + |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 61 | void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value) |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 62 | { |
| 63 | for (auto& iface : ifaces) |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 64 | @@ -103,6 +115,84 @@ void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value) |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 65 | } |
| 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 Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 137 | + "xyz.openbmc_project.State.Host", |
| 138 | + "/xyz/openbmc_project/state/host0", |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 139 | + "org.freedesktop.DBus.Properties", "Get", |
| 140 | + "xyz.openbmc_project.State.Host", "CurrentHostState"); |
| 141 | + |
| 142 | + createTimer(); |
| 143 | +} |
| 144 | + |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 145 | + |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 146 | std::string MainLoop::getID(SensorSet::container_t::const_reference sensor) |
| 147 | { |
| 148 | std::string id; |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 149 | @@ -384,6 +474,7 @@ void MainLoop::init() |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 150 | _interval = std::strtoull(interval.c_str(), NULL, 10); |
| 151 | } |
| 152 | } |
| 153 | + setupPowerMatch(_bus); |
| 154 | } |
| 155 | |
| 156 | void MainLoop::read() |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 157 | @@ -428,6 +519,12 @@ void MainLoop::read() |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 158 | |
| 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 Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 170 | @@ -490,6 +587,11 @@ void MainLoop::read() |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | + if(sensor->pwrOnMonitor() && !isPowerOn()) |
| 175 | + { |
| 176 | + statusIface->functional(false); |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 177 | + continue; |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 178 | + } |
| 179 | updateSensorInterfaces(obj, value); |
| 180 | } |
| 181 | catch (const std::system_error& e) |
| 182 | diff --git a/sensor.cpp b/sensor.cpp |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 183 | index 09aeca6..b1cb470 100644 |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 184 | --- a/sensor.cpp |
| 185 | +++ b/sensor.cpp |
George Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 186 | @@ -31,7 +31,7 @@ Sensor::Sensor(const SensorSet::key_type& sensor, |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 187 | 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 Hung | a661c16 | 2020-07-31 18:45:32 +0800 | [diff] [blame] | 195 | @@ -60,6 +60,15 @@ Sensor::Sensor(const SensorSet::key_type& sensor, |
George Hung | a66f2b9 | 2020-05-19 15:52:33 +0800 | [diff] [blame] | 196 | 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) |
| 211 | diff --git a/sensor.hpp b/sensor.hpp |
| 212 | index 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 | /** |
| 242 | diff --git a/thresholds.hpp b/thresholds.hpp |
| 243 | index 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 | -- |
| 256 | 2.7.4 |
| 257 | |