blob: f46fd4688f5c0e104437db379b1951784a3b3cf7 [file] [log] [blame]
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10301#include "Thresholds.hpp"
2
Ed Tanouseacbfdd2024-04-04 12:00:24 -07003#include "Utils.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10304#include "VariantVisitors.hpp"
5#include "sensor.hpp"
6
James Feist6714a252018-09-10 15:26:18 -07007#include <boost/algorithm/string/replace.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -07008#include <boost/asio/error.hpp>
9#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070010#include <boost/container/flat_map.hpp>
George Liu61984352025-02-24 14:47:34 +080011#include <phosphor-logging/lg2.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070012#include <sdbusplus/asio/connection.hpp>
13#include <sdbusplus/asio/object_server.hpp>
14#include <sdbusplus/exception.hpp>
15#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070016
17#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070018#include <chrono>
19#include <cstddef>
20#include <cstdint>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070021#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <string>
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020024#include <tuple>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <utility>
26#include <variant>
27#include <vector>
James Feist6714a252018-09-10 15:26:18 -070028
James Feist6714a252018-09-10 15:26:18 -070029namespace thresholds
30{
Ed Tanousc8fed202022-01-12 14:24:45 -080031Level findThresholdLevel(uint8_t sev)
James Feist6714a252018-09-10 15:26:18 -070032{
Ed Tanousc8fed202022-01-12 14:24:45 -080033 for (const ThresholdDefinition& prop : thresProp)
James Feist6714a252018-09-10 15:26:18 -070034 {
Ed Tanousc8fed202022-01-12 14:24:45 -080035 if (prop.sevOrder == sev)
James Feist6714a252018-09-10 15:26:18 -070036 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053037 return prop.level;
James Feist6714a252018-09-10 15:26:18 -070038 }
39 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053040 return Level::ERROR;
James Feist6714a252018-09-10 15:26:18 -070041}
42
Ed Tanousc8fed202022-01-12 14:24:45 -080043Direction findThresholdDirection(const std::string& direct)
James Feist6714a252018-09-10 15:26:18 -070044{
Ed Tanousc8fed202022-01-12 14:24:45 -080045 if (direct == "greater than")
James Feist6714a252018-09-10 15:26:18 -070046 {
Ed Tanousc8fed202022-01-12 14:24:45 -080047 return Direction::HIGH;
48 }
49 if (direct == "less than")
50 {
51 return Direction::LOW;
James Feist6714a252018-09-10 15:26:18 -070052 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053053 return Direction::ERROR;
James Feist6714a252018-09-10 15:26:18 -070054}
55
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070056bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080057 const SensorData& sensorData,
58 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -050059 const std::string* matchLabel, const int* sensorIndex)
James Feist6714a252018-09-10 15:26:18 -070060{
Zev Weissf783c692022-08-12 18:21:02 -070061 for (const auto& [intf, cfg] : sensorData)
James Feist6714a252018-09-10 15:26:18 -070062 {
Zev Weissf783c692022-08-12 18:21:02 -070063 if (intf.find("Thresholds") == std::string::npos)
James Feist6714a252018-09-10 15:26:18 -070064 {
65 continue;
66 }
67 if (matchLabel != nullptr)
68 {
Zev Weissf783c692022-08-12 18:21:02 -070069 auto labelFind = cfg.find("Label");
70 if (labelFind == cfg.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -070071 {
James Feist6714a252018-09-10 15:26:18 -070072 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070073 }
James Feist3eb82622019-02-08 13:10:22 -080074 if (std::visit(VariantToStringVisitor(), labelFind->second) !=
75 *matchLabel)
Ed Tanous8a57ec02020-10-09 12:46:52 -070076 {
James Feist6714a252018-09-10 15:26:18 -070077 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070078 }
James Feist6714a252018-09-10 15:26:18 -070079 }
Matt Spinler5636d522021-03-17 14:52:18 -050080
81 if (sensorIndex != nullptr)
82 {
Zev Weissf783c692022-08-12 18:21:02 -070083 auto indexFind = cfg.find("Index");
Matt Spinler5636d522021-03-17 14:52:18 -050084
85 // If we're checking for index 1, a missing Index is OK.
Zev Weissf783c692022-08-12 18:21:02 -070086 if ((indexFind == cfg.end()) && (*sensorIndex != 1))
Matt Spinler5636d522021-03-17 14:52:18 -050087 {
88 continue;
89 }
90
Zev Weissf783c692022-08-12 18:21:02 -070091 if ((indexFind != cfg.end()) &&
Matt Spinler5636d522021-03-17 14:52:18 -050092 (std::visit(VariantToIntVisitor(), indexFind->second) !=
93 *sensorIndex))
94 {
95 continue;
96 }
97 }
98
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100099 double hysteresis = std::numeric_limits<double>::quiet_NaN();
Zev Weissf783c692022-08-12 18:21:02 -0700100 auto hysteresisFind = cfg.find("Hysteresis");
101 if (hysteresisFind != cfg.end())
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000102 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400103 hysteresis =
104 std::visit(VariantToDoubleVisitor(), hysteresisFind->second);
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000105 }
106
Zev Weissf783c692022-08-12 18:21:02 -0700107 auto directionFind = cfg.find("Direction");
108 auto severityFind = cfg.find("Severity");
109 auto valueFind = cfg.find("Value");
110 if (valueFind == cfg.end() || severityFind == cfg.end() ||
111 directionFind == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700112 {
George Liu61984352025-02-24 14:47:34 +0800113 lg2::error(
114 "Malformed threshold on configuration interface: '{INTERFACE}'",
115 "INTERFACE", intf);
James Feist6714a252018-09-10 15:26:18 -0700116 return false;
117 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400118 unsigned int severity =
119 std::visit(VariantToUnsignedIntVisitor(), severityFind->second);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530120
Patrick Williams2aaf7172024-08-16 15:20:40 -0400121 std::string directions =
122 std::visit(VariantToStringVisitor(), directionFind->second);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530123
Ed Tanousc8fed202022-01-12 14:24:45 -0800124 Level level = findThresholdLevel(severity);
125 Direction direction = findThresholdDirection(directions);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530126
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +0530127 if ((level == Level::ERROR) || (direction == Direction::ERROR))
James Feist6714a252018-09-10 15:26:18 -0700128 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530129 continue;
James Feist6714a252018-09-10 15:26:18 -0700130 }
James Feist13f340b2019-03-07 16:36:11 -0800131 double val = std::visit(VariantToDoubleVisitor(), valueFind->second);
James Feist6714a252018-09-10 15:26:18 -0700132
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000133 thresholdVector.emplace_back(level, direction, val, hysteresis);
James Feist6714a252018-09-10 15:26:18 -0700134 }
135 return true;
136}
137
James Feistd8705872019-02-08 13:26:09 -0800138void persistThreshold(const std::string& path, const std::string& baseInterface,
139 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800140 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800141 size_t thresholdCount, const std::string& labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700142{
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500143 for (size_t ii = 0; ii < thresholdCount; ii++)
James Feist6714a252018-09-10 15:26:18 -0700144 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400145 std::string thresholdInterface =
146 baseInterface + ".Thresholds" + std::to_string(ii);
James Feist6714a252018-09-10 15:26:18 -0700147 conn->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700148 [&, path, threshold, thresholdInterface,
149 labelMatch](const boost::system::error_code& ec,
150 const SensorBaseConfigMap& result) {
Ed Tanousbb679322022-05-16 16:10:00 -0700151 if (ec)
152 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400153 return; // threshold not supported
Ed Tanousbb679322022-05-16 16:10:00 -0700154 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400155
156 if (!labelMatch.empty())
157 {
158 auto labelFind = result.find("Label");
159 if (labelFind == result.end())
160 {
George Liu61984352025-02-24 14:47:34 +0800161 lg2::error("No label in threshold configuration");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400162 return;
163 }
164 std::string label =
165 std::visit(VariantToStringVisitor(), labelFind->second);
166 if (label != labelMatch)
167 {
168 return;
169 }
170 }
171
172 auto directionFind = result.find("Direction");
173 auto severityFind = result.find("Severity");
174 auto valueFind = result.find("Value");
175 if (valueFind == result.end() || severityFind == result.end() ||
176 directionFind == result.end())
177 {
George Liu61984352025-02-24 14:47:34 +0800178 lg2::error("Malformed threshold in configuration");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400179 return;
180 }
181 unsigned int severity = std::visit(
182 VariantToUnsignedIntVisitor(), severityFind->second);
183
184 std::string dir =
185 std::visit(VariantToStringVisitor(), directionFind->second);
186 if ((findThresholdLevel(severity) != threshold.level) ||
187 (findThresholdDirection(dir) != threshold.direction))
188 {
189 return; // not the droid we're looking for
190 }
191
192 std::variant<double> value(threshold.value);
193 conn->async_method_call(
194 [](const boost::system::error_code& ec) {
195 if (ec)
196 {
George Liu61984352025-02-24 14:47:34 +0800197 lg2::error(
198 "Error setting threshold: '{ERROR_MESSAGE}'",
199 "ERROR_MESSAGE", ec.message());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400200 }
201 },
202 entityManagerName, path, "org.freedesktop.DBus.Properties",
203 "Set", thresholdInterface, "Value", value);
Patrick Williams597e8422023-10-20 11:19:01 -0500204 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700205 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700206 "GetAll", thresholdInterface);
207 }
208}
209
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800210void updateThresholds(Sensor* sensor)
211{
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800212 for (const auto& threshold : sensor->thresholds)
213 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530214 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
215 sensor->getThresholdInterface(threshold.level);
216
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800217 if (!interface)
218 {
219 continue;
220 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530221
Patrick Williams2aaf7172024-08-16 15:20:40 -0400222 std::string property =
223 Sensor::propertyLevel(threshold.level, threshold.direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530224 if (property.empty())
225 {
226 continue;
227 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800228 interface->set_property(property, threshold.value);
229 }
230}
231
Josh Lehan883fb3a2020-02-27 14:41:39 -0800232// Debugging counters
233static int cHiTrue = 0;
234static int cHiFalse = 0;
235static int cHiMidstate = 0;
236static int cLoTrue = 0;
237static int cLoFalse = 0;
238static int cLoMidstate = 0;
239static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700240static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800241
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700242struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700243{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700244 ChangeParam(Threshold whichThreshold, bool status, double value) :
245 threshold(whichThreshold), asserted(status), assertValue(value)
246 {}
247
248 Threshold threshold;
249 bool asserted;
250 double assertValue;
251};
252
253static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
254{
255 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700256 if (sensor->thresholds.empty())
257 {
James Feist46342ec2019-03-06 14:03:41 -0800258 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700259 }
James Feist46342ec2019-03-06 14:03:41 -0800260
James Feistd8705872019-02-08 13:26:09 -0800261 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700262 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800263 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
264 // if value is noisy while hovering very close to a threshold.
265 // When a threshold is crossed, indicate true immediately,
266 // but require more distance to be crossed the other direction,
267 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800268 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700269 {
James Feist551087a2019-12-09 11:17:12 -0800270 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700271 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700272 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700273 if (++cHiTrue < assertLogCount)
274 {
George Liu61984352025-02-24 14:47:34 +0800275 lg2::info(
276 "Sensor name: {NAME}, high threshold: {THRESHOLD}, "
277 "assert value: {VALUE}, raw data: {RAW_DATA}",
278 "NAME", sensor->name, "THRESHOLD", threshold.value,
279 "VALUE", value, "RAW_DATA", sensor->rawValue);
Zhikui Rend3da1282020-09-11 17:02:01 -0700280 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800281 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000282 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800283 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700284 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800285 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700286 }
Patrick Venture66235d42019-10-11 08:31:27 -0700287 else
James Feist251c7822018-09-12 12:54:15 -0700288 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800289 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700290 }
291 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800292 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700293 {
James Feist551087a2019-12-09 11:17:12 -0800294 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700295 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700296 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700297 if (++cLoTrue < assertLogCount)
298 {
George Liu61984352025-02-24 14:47:34 +0800299 lg2::info(
300 "Sensor name: {NAME}, low threshold: {THRESHOLD}, "
301 "assert value: {VALUE}, raw data: {RAW_DATA}",
302 "NAME", sensor->name, "THRESHOLD", threshold.value,
303 "VALUE", value, "RAW_DATA", sensor->rawValue);
Zhikui Rend3da1282020-09-11 17:02:01 -0700304 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800305 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000306 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800307 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700308 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800309 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700310 }
Patrick Venture66235d42019-10-11 08:31:27 -0700311 else
James Feist251c7822018-09-12 12:54:15 -0700312 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800313 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700314 }
315 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800316 else
317 {
George Liu61984352025-02-24 14:47:34 +0800318 lg2::error("Error determining threshold direction");
Josh Lehan883fb3a2020-02-27 14:41:39 -0800319 }
James Feist251c7822018-09-12 12:54:15 -0700320 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800321
Ed Tanous8a17c302021-09-02 15:07:11 -0700322 // Throttle debug output, so that it does not continuously spam
323 ++cDebugThrottle;
324 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800325 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700326 cDebugThrottle = 0;
Alexander Hansen89be6142025-09-18 15:36:16 +0200327 lg2::debug("checkThresholds: High T= {HIGH_TRUE}, F= {HIGH_FALSE},"
328 " M= {HIGH_MIDSTATE}, Low T= {LOW_TRUE}, F= {LOW_FALSE},"
329 " M= {LOW_MIDSTATE}",
330 "HIGH_TRUE", cHiTrue, "HIGH_FALSE", cHiFalse,
331 "HIGH_MIDSTATE", cHiMidstate, "LOW_TRUE", cLoTrue,
332 "LOW_FALSE", cLoFalse, "LOW_MIDSTATE", cLoMidstate);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800333 }
334
James Feist46342ec2019-03-06 14:03:41 -0800335 return thresholdChanges;
336}
337
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700338void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
339 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800340 double assertValue)
341{
342 struct TimerUsed timerUsed = {};
343 constexpr const size_t waitTime = 5;
344 TimerPair* pair = nullptr;
345
346 for (TimerPair& timer : timers)
347 {
348 if (!timer.first.used)
349 {
350 pair = &timer;
351 break;
352 }
353 }
354 if (pair == nullptr)
355 {
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700356 pair = &timers.emplace_back(timerUsed, boost::asio::steady_timer(io));
Jeff Lind9cd7042020-11-20 15:49:28 +0800357 }
358
359 pair->first.used = true;
360 pair->first.level = threshold.level;
361 pair->first.direction = threshold.direction;
362 pair->first.assert = assert;
Ed Tanous83db50c2023-03-01 10:20:24 -0800363 pair->second.expires_after(std::chrono::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700364 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800365 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700366 auto sensorPtr = weakSensor.lock();
367 if (!sensorPtr)
368 {
369 return; // owner sensor has been destructed
370 }
371 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800372 pair->first.used = false;
373
374 if (ec == boost::asio::error::operation_aborted)
375 {
376 return; // we're being canceled
377 }
378 if (ec)
379 {
George Liu61984352025-02-24 14:47:34 +0800380 lg2::error("timer error: '{ERROR_MESSAGE}'", "ERROR_MESSAGE",
381 ec.message());
Jeff Lind9cd7042020-11-20 15:49:28 +0800382 return;
383 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700384 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800385 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700386 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800387 threshold.direction, assert);
388 }
389 });
390}
391
James Feist46342ec2019-03-06 14:03:41 -0800392bool checkThresholds(Sensor* sensor)
393{
James Feist7b18b1e2019-05-14 13:42:09 -0700394 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700395 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
396 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800397 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700398 assertThresholds(sensor, change.assertValue, change.threshold.level,
399 change.threshold.direction, change.asserted);
400 if (change.threshold.level == thresholds::Level::CRITICAL &&
401 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800402 {
James Feist7b18b1e2019-05-14 13:42:09 -0700403 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800404 }
405 }
406
James Feistdc6c55f2018-10-31 12:53:20 -0700407 return status;
James Feist251c7822018-09-12 12:54:15 -0700408}
409
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700410void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
411 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800412{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700413 auto sensorPtr = weakSensor.lock();
414 if (!sensorPtr)
415 {
416 return; // sensor is destructed, should never be here
417 }
James Feist46342ec2019-03-06 14:03:41 -0800418
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700419 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700420 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
421 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800422 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700423 // When CPU is powered off, some volatges are expected to
424 // go below low thresholds. Filter these events with thresholdTimer.
425 // 1. always delay the assertion of low events to see if they are
426 // caused by power off event.
427 // 2. conditional delay the de-assertion of low events if there is
428 // an existing timer for assertion.
429 // 3. no delays for de-assert of low events if there is an existing
430 // de-assert for low event. This means 2nd de-assert would happen
431 // first and when timer expires for the previous one, no additional
432 // signal will be logged.
433 // 4. no delays for all high events.
434 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800435 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700436 if (change.asserted || thresholdTimer.hasActiveTimer(
437 change.threshold, !change.asserted))
438 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700439 thresholdTimer.startTimer(weakSensor, change.threshold,
440 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700441 continue;
442 }
James Feist46342ec2019-03-06 14:03:41 -0800443 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700444 assertThresholds(sensor, change.assertValue, change.threshold.level,
445 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800446 }
447}
448
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700449void assertThresholds(Sensor* sensor, double assertValue,
450 thresholds::Level level, thresholds::Direction direction,
451 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700452{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530453 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
454 sensor->getThresholdInterface(level);
455
James Feist251c7822018-09-12 12:54:15 -0700456 if (!interface)
457 {
George Liu61984352025-02-24 14:47:34 +0800458 lg2::info("trying to set uninitialized interface");
James Feist251c7822018-09-12 12:54:15 -0700459 return;
460 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700461
Ed Tanous2049bd22022-07-09 07:20:26 -0700462 std::string property = Sensor::propertyAlarm(level, direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530463 if (property.empty())
464 {
George Liu61984352025-02-24 14:47:34 +0800465 lg2::info("Alarm property is empty");
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530466 return;
467 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700468 if (interface->set_property<bool, true>(property, assert))
469 {
470 try
471 {
472 // msg.get_path() is interface->get_object_path()
Patrick Williams92f8f512022-07-22 19:26:55 -0500473 sdbusplus::message_t msg =
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700474 interface->new_signal("ThresholdAsserted");
475
476 msg.append(sensor->name, interface->get_interface_name(), property,
477 assert, assertValue);
478 msg.signal_send();
479 }
Patrick Williams92f8f512022-07-22 19:26:55 -0500480 catch (const sdbusplus::exception_t& e)
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700481 {
George Liu61984352025-02-24 14:47:34 +0800482 lg2::error(
483 "Failed to send thresholdAsserted signal with assertValue");
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700484 }
485 }
James Feist251c7822018-09-12 12:54:15 -0700486}
487
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700488bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800489 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700490 const std::string& inputPath, const double& scaleFactor,
Chris Sidesa3279232023-04-24 16:08:13 -0500491 const double& offset, const double& hysteresis)
James Feist6714a252018-09-10 15:26:18 -0700492{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200493 const boost::container::flat_map<
494 std::string, std::vector<std::tuple<const char*, thresholds::Level,
495 thresholds::Direction, double>>>
496 map = {
497 {"average",
498 {
499 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
500 0.0),
501 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
502 0.0),
503 }},
504 {"input",
505 {
506 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
507 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
508 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
509 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
510 offset),
511 }},
512 };
513
514 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700515 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200516 auto& [type, nr, item] = *fileParts;
Patrick Williams80c68052025-05-13 23:31:49 -0400517 if (map.contains(item))
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700518 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200519 for (const auto& t : map.at(item))
520 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700521 const auto& [suffix, level, direction, offset] = t;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400522 auto attrPath =
523 boost::replace_all_copy(inputPath, item, suffix);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200524 if (auto val = readFile(attrPath, scaleFactor))
525 {
526 *val += offset;
Alexander Hansen89be6142025-09-18 15:36:16 +0200527 lg2::debug("Threshold: '{PATH}': '{VALUE}'", "PATH",
528 attrPath, "VALUE", *val);
Chris Sidesa3279232023-04-24 16:08:13 -0500529 thresholdVector.emplace_back(level, direction, *val,
530 hysteresis);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200531 }
532 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700533 }
James Feist6714a252018-09-10 15:26:18 -0700534 }
James Feist6714a252018-09-10 15:26:18 -0700535 return true;
536}
537
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530538std::string getInterface(const Level thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700539{
Ed Tanousc8fed202022-01-12 14:24:45 -0800540 for (const ThresholdDefinition& thresh : thresProp)
James Feist6714a252018-09-10 15:26:18 -0700541 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800542 if (thresh.level == thresholdLevel)
543 {
544 return std::string("xyz.openbmc_project.Sensor.Threshold.") +
545 thresh.levelName;
546 }
James Feist6714a252018-09-10 15:26:18 -0700547 }
Ed Tanousc8fed202022-01-12 14:24:45 -0800548 return "";
James Feist6714a252018-09-10 15:26:18 -0700549}
550} // namespace thresholds