blob: fc252ac4218a17f6ec2eb77e8310c49568ce939c [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>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070011#include <sdbusplus/asio/connection.hpp>
12#include <sdbusplus/asio/object_server.hpp>
13#include <sdbusplus/exception.hpp>
14#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070015
16#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070017#include <chrono>
18#include <cstddef>
19#include <cstdint>
James Feist6714a252018-09-10 15:26:18 -070020#include <iostream>
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
Ed Tanous8a57ec02020-10-09 12:46:52 -070029static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070030namespace thresholds
31{
Ed Tanousc8fed202022-01-12 14:24:45 -080032Level findThresholdLevel(uint8_t sev)
James Feist6714a252018-09-10 15:26:18 -070033{
Ed Tanousc8fed202022-01-12 14:24:45 -080034 for (const ThresholdDefinition& prop : thresProp)
James Feist6714a252018-09-10 15:26:18 -070035 {
Ed Tanousc8fed202022-01-12 14:24:45 -080036 if (prop.sevOrder == sev)
James Feist6714a252018-09-10 15:26:18 -070037 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053038 return prop.level;
James Feist6714a252018-09-10 15:26:18 -070039 }
40 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053041 return Level::ERROR;
James Feist6714a252018-09-10 15:26:18 -070042}
43
Ed Tanousc8fed202022-01-12 14:24:45 -080044Direction findThresholdDirection(const std::string& direct)
James Feist6714a252018-09-10 15:26:18 -070045{
Ed Tanousc8fed202022-01-12 14:24:45 -080046 if (direct == "greater than")
James Feist6714a252018-09-10 15:26:18 -070047 {
Ed Tanousc8fed202022-01-12 14:24:45 -080048 return Direction::HIGH;
49 }
50 if (direct == "less than")
51 {
52 return Direction::LOW;
James Feist6714a252018-09-10 15:26:18 -070053 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053054 return Direction::ERROR;
James Feist6714a252018-09-10 15:26:18 -070055}
56
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070057bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080058 const SensorData& sensorData,
59 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -050060 const std::string* matchLabel, const int* sensorIndex)
James Feist6714a252018-09-10 15:26:18 -070061{
Zev Weissf783c692022-08-12 18:21:02 -070062 for (const auto& [intf, cfg] : sensorData)
James Feist6714a252018-09-10 15:26:18 -070063 {
Zev Weissf783c692022-08-12 18:21:02 -070064 if (intf.find("Thresholds") == std::string::npos)
James Feist6714a252018-09-10 15:26:18 -070065 {
66 continue;
67 }
68 if (matchLabel != nullptr)
69 {
Zev Weissf783c692022-08-12 18:21:02 -070070 auto labelFind = cfg.find("Label");
71 if (labelFind == cfg.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -070072 {
James Feist6714a252018-09-10 15:26:18 -070073 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070074 }
James Feist3eb82622019-02-08 13:10:22 -080075 if (std::visit(VariantToStringVisitor(), labelFind->second) !=
76 *matchLabel)
Ed Tanous8a57ec02020-10-09 12:46:52 -070077 {
James Feist6714a252018-09-10 15:26:18 -070078 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070079 }
James Feist6714a252018-09-10 15:26:18 -070080 }
Matt Spinler5636d522021-03-17 14:52:18 -050081
82 if (sensorIndex != nullptr)
83 {
Zev Weissf783c692022-08-12 18:21:02 -070084 auto indexFind = cfg.find("Index");
Matt Spinler5636d522021-03-17 14:52:18 -050085
86 // If we're checking for index 1, a missing Index is OK.
Zev Weissf783c692022-08-12 18:21:02 -070087 if ((indexFind == cfg.end()) && (*sensorIndex != 1))
Matt Spinler5636d522021-03-17 14:52:18 -050088 {
89 continue;
90 }
91
Zev Weissf783c692022-08-12 18:21:02 -070092 if ((indexFind != cfg.end()) &&
Matt Spinler5636d522021-03-17 14:52:18 -050093 (std::visit(VariantToIntVisitor(), indexFind->second) !=
94 *sensorIndex))
95 {
96 continue;
97 }
98 }
99
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000100 double hysteresis = std::numeric_limits<double>::quiet_NaN();
Zev Weissf783c692022-08-12 18:21:02 -0700101 auto hysteresisFind = cfg.find("Hysteresis");
102 if (hysteresisFind != cfg.end())
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000103 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400104 hysteresis =
105 std::visit(VariantToDoubleVisitor(), hysteresisFind->second);
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000106 }
107
Zev Weissf783c692022-08-12 18:21:02 -0700108 auto directionFind = cfg.find("Direction");
109 auto severityFind = cfg.find("Severity");
110 auto valueFind = cfg.find("Value");
111 if (valueFind == cfg.end() || severityFind == cfg.end() ||
112 directionFind == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700113 {
Matt Spinler5636d522021-03-17 14:52:18 -0500114 std::cerr << "Malformed threshold on configuration interface "
Zev Weissf783c692022-08-12 18:21:02 -0700115 << intf << "\n";
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 {
161 std::cerr << "No label in threshold configuration\n";
162 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 {
178 std::cerr << "Malformed threshold in configuration\n";
179 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 {
197 std::cerr
198 << "Error setting threshold " << ec << "\n";
199 }
200 },
201 entityManagerName, path, "org.freedesktop.DBus.Properties",
202 "Set", thresholdInterface, "Value", value);
Patrick Williams597e8422023-10-20 11:19:01 -0500203 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700204 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700205 "GetAll", thresholdInterface);
206 }
207}
208
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800209void updateThresholds(Sensor* sensor)
210{
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800211 for (const auto& threshold : sensor->thresholds)
212 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530213 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
214 sensor->getThresholdInterface(threshold.level);
215
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800216 if (!interface)
217 {
218 continue;
219 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530220
Patrick Williams2aaf7172024-08-16 15:20:40 -0400221 std::string property =
222 Sensor::propertyLevel(threshold.level, threshold.direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530223 if (property.empty())
224 {
225 continue;
226 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800227 interface->set_property(property, threshold.value);
228 }
229}
230
Josh Lehan883fb3a2020-02-27 14:41:39 -0800231// Debugging counters
232static int cHiTrue = 0;
233static int cHiFalse = 0;
234static int cHiMidstate = 0;
235static int cLoTrue = 0;
236static int cLoFalse = 0;
237static int cLoMidstate = 0;
238static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700239static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800240
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700241struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700242{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700243 ChangeParam(Threshold whichThreshold, bool status, double value) :
244 threshold(whichThreshold), asserted(status), assertValue(value)
245 {}
246
247 Threshold threshold;
248 bool asserted;
249 double assertValue;
250};
251
252static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
253{
254 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700255 if (sensor->thresholds.empty())
256 {
James Feist46342ec2019-03-06 14:03:41 -0800257 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700258 }
James Feist46342ec2019-03-06 14:03:41 -0800259
James Feistd8705872019-02-08 13:26:09 -0800260 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700261 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800262 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
263 // if value is noisy while hovering very close to a threshold.
264 // When a threshold is crossed, indicate true immediately,
265 // but require more distance to be crossed the other direction,
266 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800267 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700268 {
James Feist551087a2019-12-09 11:17:12 -0800269 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700270 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700271 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700272 if (++cHiTrue < assertLogCount)
273 {
274 std::cerr << "Sensor " << sensor->name << " high threshold "
275 << threshold.value << " assert: value " << value
276 << " raw data " << sensor->rawValue << "\n";
277 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800278 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000279 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800280 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700281 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800282 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700283 }
Patrick Venture66235d42019-10-11 08:31:27 -0700284 else
James Feist251c7822018-09-12 12:54:15 -0700285 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800286 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700287 }
288 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800289 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700290 {
James Feist551087a2019-12-09 11:17:12 -0800291 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700292 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700293 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700294 if (++cLoTrue < assertLogCount)
295 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400296 std::cerr
297 << "Sensor " << sensor->name << " low threshold "
298 << threshold.value << " assert: value " << sensor->value
299 << " raw data " << sensor->rawValue << "\n";
Zhikui Rend3da1282020-09-11 17:02:01 -0700300 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800301 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000302 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800303 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700304 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800305 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700306 }
Patrick Venture66235d42019-10-11 08:31:27 -0700307 else
James Feist251c7822018-09-12 12:54:15 -0700308 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800309 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700310 }
311 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800312 else
313 {
314 std::cerr << "Error determining threshold direction\n";
315 }
James Feist251c7822018-09-12 12:54:15 -0700316 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800317
Ed Tanous8a17c302021-09-02 15:07:11 -0700318 // Throttle debug output, so that it does not continuously spam
319 ++cDebugThrottle;
320 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800321 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700322 cDebugThrottle = 0;
323 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800324 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800325 std::cerr << "checkThresholds: High T=" << cHiTrue
326 << " F=" << cHiFalse << " M=" << cHiMidstate
327 << ", Low T=" << cLoTrue << " F=" << cLoFalse
328 << " M=" << cLoMidstate << "\n";
329 }
330 }
331
James Feist46342ec2019-03-06 14:03:41 -0800332 return thresholdChanges;
333}
334
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700335void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
336 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800337 double assertValue)
338{
339 struct TimerUsed timerUsed = {};
340 constexpr const size_t waitTime = 5;
341 TimerPair* pair = nullptr;
342
343 for (TimerPair& timer : timers)
344 {
345 if (!timer.first.used)
346 {
347 pair = &timer;
348 break;
349 }
350 }
351 if (pair == nullptr)
352 {
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700353 pair = &timers.emplace_back(timerUsed, boost::asio::steady_timer(io));
Jeff Lind9cd7042020-11-20 15:49:28 +0800354 }
355
356 pair->first.used = true;
357 pair->first.level = threshold.level;
358 pair->first.direction = threshold.direction;
359 pair->first.assert = assert;
Ed Tanous83db50c2023-03-01 10:20:24 -0800360 pair->second.expires_after(std::chrono::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700361 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800362 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700363 auto sensorPtr = weakSensor.lock();
364 if (!sensorPtr)
365 {
366 return; // owner sensor has been destructed
367 }
368 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800369 pair->first.used = false;
370
371 if (ec == boost::asio::error::operation_aborted)
372 {
373 return; // we're being canceled
374 }
375 if (ec)
376 {
377 std::cerr << "timer error: " << ec.message() << "\n";
378 return;
379 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700380 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800381 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700382 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800383 threshold.direction, assert);
384 }
385 });
386}
387
James Feist46342ec2019-03-06 14:03:41 -0800388bool checkThresholds(Sensor* sensor)
389{
James Feist7b18b1e2019-05-14 13:42:09 -0700390 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700391 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
392 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800393 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700394 assertThresholds(sensor, change.assertValue, change.threshold.level,
395 change.threshold.direction, change.asserted);
396 if (change.threshold.level == thresholds::Level::CRITICAL &&
397 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800398 {
James Feist7b18b1e2019-05-14 13:42:09 -0700399 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800400 }
401 }
402
James Feistdc6c55f2018-10-31 12:53:20 -0700403 return status;
James Feist251c7822018-09-12 12:54:15 -0700404}
405
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700406void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
407 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800408{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700409 auto sensorPtr = weakSensor.lock();
410 if (!sensorPtr)
411 {
412 return; // sensor is destructed, should never be here
413 }
James Feist46342ec2019-03-06 14:03:41 -0800414
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700415 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700416 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
417 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800418 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700419 // When CPU is powered off, some volatges are expected to
420 // go below low thresholds. Filter these events with thresholdTimer.
421 // 1. always delay the assertion of low events to see if they are
422 // caused by power off event.
423 // 2. conditional delay the de-assertion of low events if there is
424 // an existing timer for assertion.
425 // 3. no delays for de-assert of low events if there is an existing
426 // de-assert for low event. This means 2nd de-assert would happen
427 // first and when timer expires for the previous one, no additional
428 // signal will be logged.
429 // 4. no delays for all high events.
430 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800431 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700432 if (change.asserted || thresholdTimer.hasActiveTimer(
433 change.threshold, !change.asserted))
434 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700435 thresholdTimer.startTimer(weakSensor, change.threshold,
436 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700437 continue;
438 }
James Feist46342ec2019-03-06 14:03:41 -0800439 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700440 assertThresholds(sensor, change.assertValue, change.threshold.level,
441 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800442 }
443}
444
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700445void assertThresholds(Sensor* sensor, double assertValue,
446 thresholds::Level level, thresholds::Direction direction,
447 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700448{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530449 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
450 sensor->getThresholdInterface(level);
451
James Feist251c7822018-09-12 12:54:15 -0700452 if (!interface)
453 {
454 std::cout << "trying to set uninitialized interface\n";
455 return;
456 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700457
Ed Tanous2049bd22022-07-09 07:20:26 -0700458 std::string property = Sensor::propertyAlarm(level, direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530459 if (property.empty())
460 {
461 std::cout << "Alarm property is empty \n";
462 return;
463 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700464 if (interface->set_property<bool, true>(property, assert))
465 {
466 try
467 {
468 // msg.get_path() is interface->get_object_path()
Patrick Williams92f8f512022-07-22 19:26:55 -0500469 sdbusplus::message_t msg =
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700470 interface->new_signal("ThresholdAsserted");
471
472 msg.append(sensor->name, interface->get_interface_name(), property,
473 assert, assertValue);
474 msg.signal_send();
475 }
Patrick Williams92f8f512022-07-22 19:26:55 -0500476 catch (const sdbusplus::exception_t& e)
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700477 {
478 std::cerr
479 << "Failed to send thresholdAsserted signal with assertValue\n";
480 }
481 }
James Feist251c7822018-09-12 12:54:15 -0700482}
483
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700484bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800485 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700486 const std::string& inputPath, const double& scaleFactor,
Chris Sidesa3279232023-04-24 16:08:13 -0500487 const double& offset, const double& hysteresis)
James Feist6714a252018-09-10 15:26:18 -0700488{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200489 const boost::container::flat_map<
490 std::string, std::vector<std::tuple<const char*, thresholds::Level,
491 thresholds::Direction, double>>>
492 map = {
493 {"average",
494 {
495 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
496 0.0),
497 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
498 0.0),
499 }},
500 {"input",
501 {
502 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
503 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
504 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
505 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
506 offset),
507 }},
508 };
509
510 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700511 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200512 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200513 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700514 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200515 for (const auto& t : map.at(item))
516 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700517 const auto& [suffix, level, direction, offset] = t;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400518 auto attrPath =
519 boost::replace_all_copy(inputPath, item, suffix);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200520 if (auto val = readFile(attrPath, scaleFactor))
521 {
522 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700523 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200524 {
525 std::cout << "Threshold: " << attrPath << ": " << *val
526 << "\n";
527 }
Chris Sidesa3279232023-04-24 16:08:13 -0500528 thresholdVector.emplace_back(level, direction, *val,
529 hysteresis);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200530 }
531 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700532 }
James Feist6714a252018-09-10 15:26:18 -0700533 }
James Feist6714a252018-09-10 15:26:18 -0700534 return true;
535}
536
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530537std::string getInterface(const Level thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700538{
Ed Tanousc8fed202022-01-12 14:24:45 -0800539 for (const ThresholdDefinition& thresh : thresProp)
James Feist6714a252018-09-10 15:26:18 -0700540 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800541 if (thresh.level == thresholdLevel)
542 {
543 return std::string("xyz.openbmc_project.Sensor.Threshold.") +
544 thresh.levelName;
545 }
James Feist6714a252018-09-10 15:26:18 -0700546 }
Ed Tanousc8fed202022-01-12 14:24:45 -0800547 return "";
James Feist6714a252018-09-10 15:26:18 -0700548}
549} // namespace thresholds