blob: 0581f21cc63366684ef3c38764f1b9d035c44059 [file] [log] [blame]
Ed Tanous8a57ec02020-10-09 12:46:52 -07001#include <Thresholds.hpp>
2#include <VariantVisitors.hpp>
James Feist6714a252018-09-10 15:26:18 -07003#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -07004#include <boost/container/flat_map.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07005#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -07006
7#include <array>
James Feistdc6c55f2018-10-31 12:53:20 -07008#include <cmath>
James Feist6714a252018-09-10 15:26:18 -07009#include <fstream>
10#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070011#include <memory>
12#include <stdexcept>
13#include <string>
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020014#include <tuple>
Patrick Venture96e97db2019-10-31 13:44:38 -070015#include <utility>
16#include <variant>
17#include <vector>
James Feist6714a252018-09-10 15:26:18 -070018
Ed Tanous8a57ec02020-10-09 12:46:52 -070019static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070020namespace thresholds
21{
James Feistd8705872019-02-08 13:26:09 -080022unsigned int toBusValue(const Level& level)
James Feist6714a252018-09-10 15:26:18 -070023{
24 switch (level)
25 {
26 case (Level::WARNING):
27 {
28 return 0;
29 }
30 case (Level::CRITICAL):
31 {
32 return 1;
33 }
34 default:
35 {
36 return -1;
37 }
38 }
39}
40
James Feistd8705872019-02-08 13:26:09 -080041std::string toBusValue(const Direction& direction)
James Feist6714a252018-09-10 15:26:18 -070042{
43 switch (direction)
44 {
45 case (Direction::LOW):
46 {
47 return "less than";
48 }
49 case (Direction::HIGH):
50 {
51 return "greater than";
52 }
53 default:
54 {
55 return "err";
56 }
57 }
58}
59
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070060bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080061 const SensorData& sensorData,
62 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -050063 const std::string* matchLabel, const int* sensorIndex)
James Feist6714a252018-09-10 15:26:18 -070064{
James Feistd8705872019-02-08 13:26:09 -080065 for (const auto& item : sensorData)
James Feist6714a252018-09-10 15:26:18 -070066 {
67 if (item.first.find("Thresholds") == std::string::npos)
68 {
69 continue;
70 }
71 if (matchLabel != nullptr)
72 {
73 auto labelFind = item.second.find("Label");
74 if (labelFind == item.second.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -070075 {
James Feist6714a252018-09-10 15:26:18 -070076 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070077 }
James Feist3eb82622019-02-08 13:10:22 -080078 if (std::visit(VariantToStringVisitor(), labelFind->second) !=
79 *matchLabel)
Ed Tanous8a57ec02020-10-09 12:46:52 -070080 {
James Feist6714a252018-09-10 15:26:18 -070081 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070082 }
James Feist6714a252018-09-10 15:26:18 -070083 }
Matt Spinler5636d522021-03-17 14:52:18 -050084
85 if (sensorIndex != nullptr)
86 {
87 auto indexFind = item.second.find("Index");
88
89 // If we're checking for index 1, a missing Index is OK.
90 if ((indexFind == item.second.end()) && (*sensorIndex != 1))
91 {
92 continue;
93 }
94
95 if ((indexFind != item.second.end()) &&
96 (std::visit(VariantToIntVisitor(), indexFind->second) !=
97 *sensorIndex))
98 {
99 continue;
100 }
101 }
102
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000103 double hysteresis = std::numeric_limits<double>::quiet_NaN();
104 auto hysteresisFind = item.second.find("Hysteresis");
105 if (hysteresisFind != item.second.end())
106 {
107 hysteresis =
108 std::visit(VariantToDoubleVisitor(), hysteresisFind->second);
109 }
110
James Feist6714a252018-09-10 15:26:18 -0700111 auto directionFind = item.second.find("Direction");
112 auto severityFind = item.second.find("Severity");
113 auto valueFind = item.second.find("Value");
114 if (valueFind == item.second.end() ||
115 severityFind == item.second.end() ||
116 directionFind == item.second.end())
117 {
Matt Spinler5636d522021-03-17 14:52:18 -0500118 std::cerr << "Malformed threshold on configuration interface "
119 << item.first << "\n";
James Feist6714a252018-09-10 15:26:18 -0700120 return false;
121 }
122 Level level;
123 Direction direction;
James Feist3eb82622019-02-08 13:10:22 -0800124 if (std::visit(VariantToUnsignedIntVisitor(), severityFind->second) ==
125 0)
James Feist6714a252018-09-10 15:26:18 -0700126 {
127 level = Level::WARNING;
128 }
129 else
130 {
131 level = Level::CRITICAL;
132 }
James Feist3eb82622019-02-08 13:10:22 -0800133 if (std::visit(VariantToStringVisitor(), directionFind->second) ==
134 "less than")
James Feist6714a252018-09-10 15:26:18 -0700135 {
136 direction = Direction::LOW;
137 }
138 else
139 {
140 direction = Direction::HIGH;
141 }
James Feist13f340b2019-03-07 16:36:11 -0800142 double val = std::visit(VariantToDoubleVisitor(), valueFind->second);
James Feist6714a252018-09-10 15:26:18 -0700143
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000144 thresholdVector.emplace_back(level, direction, val, hysteresis);
James Feist6714a252018-09-10 15:26:18 -0700145 }
146 return true;
147}
148
James Feistd8705872019-02-08 13:26:09 -0800149void persistThreshold(const std::string& path, const std::string& baseInterface,
150 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800151 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800152 size_t thresholdCount, const std::string& labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700153{
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500154 for (size_t ii = 0; ii < thresholdCount; ii++)
James Feist6714a252018-09-10 15:26:18 -0700155 {
156 std::string thresholdInterface =
157 baseInterface + ".Thresholds" + std::to_string(ii);
158 conn->async_method_call(
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800159 [&, path, threshold, thresholdInterface, labelMatch](
James Feistd8705872019-02-08 13:26:09 -0800160 const boost::system::error_code& ec,
161 const boost::container::flat_map<std::string, BasicVariantType>&
162 result) {
James Feist6714a252018-09-10 15:26:18 -0700163 if (ec)
164 {
165 return; // threshold not supported
166 }
167
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800168 if (!labelMatch.empty())
169 {
170 auto labelFind = result.find("Label");
171 if (labelFind == result.end())
172 {
173 std::cerr << "No label in threshold configuration\n";
174 return;
175 }
176 std::string label =
177 std::visit(VariantToStringVisitor(), labelFind->second);
178 if (label != labelMatch)
179 {
180 return;
181 }
182 }
183
James Feist6714a252018-09-10 15:26:18 -0700184 auto directionFind = result.find("Direction");
185 auto severityFind = result.find("Severity");
186 auto valueFind = result.find("Value");
187 if (valueFind == result.end() || severityFind == result.end() ||
188 directionFind == result.end())
189 {
190 std::cerr << "Malformed threshold in configuration\n";
191 return;
192 }
James Feist3eb82622019-02-08 13:10:22 -0800193 unsigned int level = std::visit(VariantToUnsignedIntVisitor(),
194 severityFind->second);
James Feist6714a252018-09-10 15:26:18 -0700195
James Feist3eb82622019-02-08 13:10:22 -0800196 std::string dir =
197 std::visit(VariantToStringVisitor(), directionFind->second);
James Feist6714a252018-09-10 15:26:18 -0700198 if ((toBusValue(threshold.level) != level) ||
199 (toBusValue(threshold.direction) != dir))
200 {
201 return; // not the droid we're looking for
202 }
203
James Feist3eb82622019-02-08 13:10:22 -0800204 std::variant<double> value(threshold.value);
James Feist6714a252018-09-10 15:26:18 -0700205 conn->async_method_call(
James Feistd8705872019-02-08 13:26:09 -0800206 [](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700207 if (ec)
208 {
209 std::cerr << "Error setting threshold " << ec
210 << "\n";
211 }
212 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700213 entityManagerName, path, "org.freedesktop.DBus.Properties",
214 "Set", thresholdInterface, "Value", value);
James Feist6714a252018-09-10 15:26:18 -0700215 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700216 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700217 "GetAll", thresholdInterface);
218 }
219}
220
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800221void updateThresholds(Sensor* sensor)
222{
223 if (sensor->thresholds.empty())
224 {
225 return;
226 }
227
228 for (const auto& threshold : sensor->thresholds)
229 {
230 std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
231 std::string property;
232 if (threshold.level == thresholds::Level::CRITICAL)
233 {
234 interface = sensor->thresholdInterfaceCritical;
235 if (threshold.direction == thresholds::Direction::HIGH)
236 {
237 property = "CriticalHigh";
238 }
239 else
240 {
241 property = "CriticalLow";
242 }
243 }
244 else if (threshold.level == thresholds::Level::WARNING)
245 {
246 interface = sensor->thresholdInterfaceWarning;
247 if (threshold.direction == thresholds::Direction::HIGH)
248 {
249 property = "WarningHigh";
250 }
251 else
252 {
253 property = "WarningLow";
254 }
255 }
256 else
257 {
258 continue;
259 }
260 if (!interface)
261 {
262 continue;
263 }
264 interface->set_property(property, threshold.value);
265 }
266}
267
Josh Lehan883fb3a2020-02-27 14:41:39 -0800268// Debugging counters
269static int cHiTrue = 0;
270static int cHiFalse = 0;
271static int cHiMidstate = 0;
272static int cLoTrue = 0;
273static int cLoFalse = 0;
274static int cLoMidstate = 0;
275static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700276static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800277
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700278struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700279{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700280 ChangeParam(Threshold whichThreshold, bool status, double value) :
281 threshold(whichThreshold), asserted(status), assertValue(value)
282 {}
283
284 Threshold threshold;
285 bool asserted;
286 double assertValue;
287};
288
289static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
290{
291 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700292 if (sensor->thresholds.empty())
293 {
James Feist46342ec2019-03-06 14:03:41 -0800294 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700295 }
James Feist46342ec2019-03-06 14:03:41 -0800296
James Feistd8705872019-02-08 13:26:09 -0800297 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700298 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800299 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
300 // if value is noisy while hovering very close to a threshold.
301 // When a threshold is crossed, indicate true immediately,
302 // but require more distance to be crossed the other direction,
303 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800304 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700305 {
James Feist551087a2019-12-09 11:17:12 -0800306 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700307 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700308 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700309 if (++cHiTrue < assertLogCount)
310 {
311 std::cerr << "Sensor " << sensor->name << " high threshold "
312 << threshold.value << " assert: value " << value
313 << " raw data " << sensor->rawValue << "\n";
314 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800315 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000316 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800317 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700318 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800319 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700320 }
Patrick Venture66235d42019-10-11 08:31:27 -0700321 else
James Feist251c7822018-09-12 12:54:15 -0700322 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800323 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700324 }
325 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800326 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700327 {
James Feist551087a2019-12-09 11:17:12 -0800328 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700329 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700330 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700331 if (++cLoTrue < assertLogCount)
332 {
333 std::cerr << "Sensor " << sensor->name << " low threshold "
334 << threshold.value << " assert: value "
335 << sensor->value << " raw data "
336 << sensor->rawValue << "\n";
337 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800338 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000339 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800340 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700341 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800342 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700343 }
Patrick Venture66235d42019-10-11 08:31:27 -0700344 else
James Feist251c7822018-09-12 12:54:15 -0700345 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800346 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700347 }
348 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800349 else
350 {
351 std::cerr << "Error determining threshold direction\n";
352 }
James Feist251c7822018-09-12 12:54:15 -0700353 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800354
Ed Tanous8a17c302021-09-02 15:07:11 -0700355 // Throttle debug output, so that it does not continuously spam
356 ++cDebugThrottle;
357 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800358 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700359 cDebugThrottle = 0;
360 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800361 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800362 std::cerr << "checkThresholds: High T=" << cHiTrue
363 << " F=" << cHiFalse << " M=" << cHiMidstate
364 << ", Low T=" << cLoTrue << " F=" << cLoFalse
365 << " M=" << cLoMidstate << "\n";
366 }
367 }
368
James Feist46342ec2019-03-06 14:03:41 -0800369 return thresholdChanges;
370}
371
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700372void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
373 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800374 double assertValue)
375{
376 struct TimerUsed timerUsed = {};
377 constexpr const size_t waitTime = 5;
378 TimerPair* pair = nullptr;
379
380 for (TimerPair& timer : timers)
381 {
382 if (!timer.first.used)
383 {
384 pair = &timer;
385 break;
386 }
387 }
388 if (pair == nullptr)
389 {
390 pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io));
391 }
392
393 pair->first.used = true;
394 pair->first.level = threshold.level;
395 pair->first.direction = threshold.direction;
396 pair->first.assert = assert;
397 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700398 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800399 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700400 auto sensorPtr = weakSensor.lock();
401 if (!sensorPtr)
402 {
403 return; // owner sensor has been destructed
404 }
405 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800406 pair->first.used = false;
407
408 if (ec == boost::asio::error::operation_aborted)
409 {
410 return; // we're being canceled
411 }
412 if (ec)
413 {
414 std::cerr << "timer error: " << ec.message() << "\n";
415 return;
416 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700417 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800418 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700419 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800420 threshold.direction, assert);
421 }
422 });
423}
424
James Feist46342ec2019-03-06 14:03:41 -0800425bool checkThresholds(Sensor* sensor)
426{
James Feist7b18b1e2019-05-14 13:42:09 -0700427 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700428 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
429 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800430 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700431 assertThresholds(sensor, change.assertValue, change.threshold.level,
432 change.threshold.direction, change.asserted);
433 if (change.threshold.level == thresholds::Level::CRITICAL &&
434 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800435 {
James Feist7b18b1e2019-05-14 13:42:09 -0700436 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800437 }
438 }
439
James Feistdc6c55f2018-10-31 12:53:20 -0700440 return status;
James Feist251c7822018-09-12 12:54:15 -0700441}
442
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700443void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
444 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800445{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700446 auto sensorPtr = weakSensor.lock();
447 if (!sensorPtr)
448 {
449 return; // sensor is destructed, should never be here
450 }
James Feist46342ec2019-03-06 14:03:41 -0800451
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700452 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700453 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
454 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800455 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700456 // When CPU is powered off, some volatges are expected to
457 // go below low thresholds. Filter these events with thresholdTimer.
458 // 1. always delay the assertion of low events to see if they are
459 // caused by power off event.
460 // 2. conditional delay the de-assertion of low events if there is
461 // an existing timer for assertion.
462 // 3. no delays for de-assert of low events if there is an existing
463 // de-assert for low event. This means 2nd de-assert would happen
464 // first and when timer expires for the previous one, no additional
465 // signal will be logged.
466 // 4. no delays for all high events.
467 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800468 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700469 if (change.asserted || thresholdTimer.hasActiveTimer(
470 change.threshold, !change.asserted))
471 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700472 thresholdTimer.startTimer(weakSensor, change.threshold,
473 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700474 continue;
475 }
James Feist46342ec2019-03-06 14:03:41 -0800476 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700477 assertThresholds(sensor, change.assertValue, change.threshold.level,
478 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800479 }
480}
481
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700482void assertThresholds(Sensor* sensor, double assertValue,
483 thresholds::Level level, thresholds::Direction direction,
484 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700485{
486 std::string property;
487 std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
488 if (level == thresholds::Level::WARNING &&
489 direction == thresholds::Direction::HIGH)
490 {
491 property = "WarningAlarmHigh";
492 interface = sensor->thresholdInterfaceWarning;
493 }
494 else if (level == thresholds::Level::WARNING &&
495 direction == thresholds::Direction::LOW)
496 {
497 property = "WarningAlarmLow";
498 interface = sensor->thresholdInterfaceWarning;
499 }
500 else if (level == thresholds::Level::CRITICAL &&
501 direction == thresholds::Direction::HIGH)
502 {
503 property = "CriticalAlarmHigh";
504 interface = sensor->thresholdInterfaceCritical;
505 }
506 else if (level == thresholds::Level::CRITICAL &&
507 direction == thresholds::Direction::LOW)
508 {
509 property = "CriticalAlarmLow";
510 interface = sensor->thresholdInterfaceCritical;
511 }
512 else
513 {
514 std::cerr << "Unknown threshold, level " << level << "direction "
515 << direction << "\n";
516 return;
517 }
518 if (!interface)
519 {
520 std::cout << "trying to set uninitialized interface\n";
521 return;
522 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700523
524 if (interface->set_property<bool, true>(property, assert))
525 {
526 try
527 {
528 // msg.get_path() is interface->get_object_path()
529 sdbusplus::message::message msg =
530 interface->new_signal("ThresholdAsserted");
531
532 msg.append(sensor->name, interface->get_interface_name(), property,
533 assert, assertValue);
534 msg.signal_send();
535 }
536 catch (const sdbusplus::exception::exception& e)
537 {
538 std::cerr
539 << "Failed to send thresholdAsserted signal with assertValue\n";
540 }
541 }
James Feist251c7822018-09-12 12:54:15 -0700542}
543
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700544bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800545 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700546 const std::string& inputPath, const double& scaleFactor,
547 const double& offset)
James Feist6714a252018-09-10 15:26:18 -0700548{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200549 const boost::container::flat_map<
550 std::string, std::vector<std::tuple<const char*, thresholds::Level,
551 thresholds::Direction, double>>>
552 map = {
553 {"average",
554 {
555 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
556 0.0),
557 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
558 0.0),
559 }},
560 {"input",
561 {
562 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
563 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
564 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
565 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
566 offset),
567 }},
568 };
569
570 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700571 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200572 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200573 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700574 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200575 for (const auto& t : map.at(item))
576 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200577 auto& [suffix, level, direction, offset] = t;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200578 auto attrPath =
579 boost::replace_all_copy(inputPath, item, suffix);
580 if (auto val = readFile(attrPath, scaleFactor))
581 {
582 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700583 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200584 {
585 std::cout << "Threshold: " << attrPath << ": " << *val
586 << "\n";
587 }
588 thresholdVector.emplace_back(level, direction, *val);
589 }
590 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700591 }
James Feist6714a252018-09-10 15:26:18 -0700592 }
James Feist6714a252018-09-10 15:26:18 -0700593 return true;
594}
595
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700596bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800597 const std::vector<thresholds::Threshold>& thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700598{
James Feistd8705872019-02-08 13:26:09 -0800599 for (auto& threshold : thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700600 {
601 if (threshold.level == Level::CRITICAL)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700602 {
James Feist6714a252018-09-10 15:26:18 -0700603 return true;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700604 }
James Feist6714a252018-09-10 15:26:18 -0700605 }
606 return false;
607}
608
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700609bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800610 const std::vector<thresholds::Threshold>& thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700611{
James Feistd8705872019-02-08 13:26:09 -0800612 for (auto& threshold : thresholdVector)
James Feist6714a252018-09-10 15:26:18 -0700613 {
614 if (threshold.level == Level::WARNING)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700615 {
James Feist6714a252018-09-10 15:26:18 -0700616 return true;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700617 }
James Feist6714a252018-09-10 15:26:18 -0700618 }
619 return false;
620}
621} // namespace thresholds