blob: 7a64d1e19a09d7a47ea6b3431cfdc7f2c9405fc2 [file] [log] [blame]
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10301#include "Thresholds.hpp"
2
3#include "VariantVisitors.hpp"
4#include "sensor.hpp"
5
James Feist6714a252018-09-10 15:26:18 -07006#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -07007#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -07008
9#include <array>
James Feistdc6c55f2018-10-31 12:53:20 -070010#include <cmath>
James Feist6714a252018-09-10 15:26:18 -070011#include <fstream>
12#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070013#include <memory>
14#include <stdexcept>
15#include <string>
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020016#include <tuple>
Patrick Venture96e97db2019-10-31 13:44:38 -070017#include <utility>
18#include <variant>
19#include <vector>
James Feist6714a252018-09-10 15:26:18 -070020
Ed Tanous8a57ec02020-10-09 12:46:52 -070021static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070022namespace thresholds
23{
Ed Tanousc8fed202022-01-12 14:24:45 -080024Level findThresholdLevel(uint8_t sev)
James Feist6714a252018-09-10 15:26:18 -070025{
Ed Tanousc8fed202022-01-12 14:24:45 -080026 for (const ThresholdDefinition& prop : thresProp)
James Feist6714a252018-09-10 15:26:18 -070027 {
Ed Tanousc8fed202022-01-12 14:24:45 -080028 if (prop.sevOrder == sev)
James Feist6714a252018-09-10 15:26:18 -070029 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053030 return prop.level;
James Feist6714a252018-09-10 15:26:18 -070031 }
32 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053033 return Level::ERROR;
James Feist6714a252018-09-10 15:26:18 -070034}
35
Ed Tanousc8fed202022-01-12 14:24:45 -080036Direction findThresholdDirection(const std::string& direct)
James Feist6714a252018-09-10 15:26:18 -070037{
Ed Tanousc8fed202022-01-12 14:24:45 -080038 if (direct == "greater than")
James Feist6714a252018-09-10 15:26:18 -070039 {
Ed Tanousc8fed202022-01-12 14:24:45 -080040 return Direction::HIGH;
41 }
42 if (direct == "less than")
43 {
44 return Direction::LOW;
James Feist6714a252018-09-10 15:26:18 -070045 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053046 return Direction::ERROR;
James Feist6714a252018-09-10 15:26:18 -070047}
48
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070049bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080050 const SensorData& sensorData,
51 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -050052 const std::string* matchLabel, const int* sensorIndex)
James Feist6714a252018-09-10 15:26:18 -070053{
Zev Weissf783c692022-08-12 18:21:02 -070054 for (const auto& [intf, cfg] : sensorData)
James Feist6714a252018-09-10 15:26:18 -070055 {
Zev Weissf783c692022-08-12 18:21:02 -070056 if (intf.find("Thresholds") == std::string::npos)
James Feist6714a252018-09-10 15:26:18 -070057 {
58 continue;
59 }
60 if (matchLabel != nullptr)
61 {
Zev Weissf783c692022-08-12 18:21:02 -070062 auto labelFind = cfg.find("Label");
63 if (labelFind == cfg.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -070064 {
James Feist6714a252018-09-10 15:26:18 -070065 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070066 }
James Feist3eb82622019-02-08 13:10:22 -080067 if (std::visit(VariantToStringVisitor(), labelFind->second) !=
68 *matchLabel)
Ed Tanous8a57ec02020-10-09 12:46:52 -070069 {
James Feist6714a252018-09-10 15:26:18 -070070 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070071 }
James Feist6714a252018-09-10 15:26:18 -070072 }
Matt Spinler5636d522021-03-17 14:52:18 -050073
74 if (sensorIndex != nullptr)
75 {
Zev Weissf783c692022-08-12 18:21:02 -070076 auto indexFind = cfg.find("Index");
Matt Spinler5636d522021-03-17 14:52:18 -050077
78 // If we're checking for index 1, a missing Index is OK.
Zev Weissf783c692022-08-12 18:21:02 -070079 if ((indexFind == cfg.end()) && (*sensorIndex != 1))
Matt Spinler5636d522021-03-17 14:52:18 -050080 {
81 continue;
82 }
83
Zev Weissf783c692022-08-12 18:21:02 -070084 if ((indexFind != cfg.end()) &&
Matt Spinler5636d522021-03-17 14:52:18 -050085 (std::visit(VariantToIntVisitor(), indexFind->second) !=
86 *sensorIndex))
87 {
88 continue;
89 }
90 }
91
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100092 double hysteresis = std::numeric_limits<double>::quiet_NaN();
Zev Weissf783c692022-08-12 18:21:02 -070093 auto hysteresisFind = cfg.find("Hysteresis");
94 if (hysteresisFind != cfg.end())
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100095 {
Patrick Williams779c96a2023-05-10 07:50:42 -050096 hysteresis = std::visit(VariantToDoubleVisitor(),
97 hysteresisFind->second);
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100098 }
99
Zev Weissf783c692022-08-12 18:21:02 -0700100 auto directionFind = cfg.find("Direction");
101 auto severityFind = cfg.find("Severity");
102 auto valueFind = cfg.find("Value");
103 if (valueFind == cfg.end() || severityFind == cfg.end() ||
104 directionFind == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700105 {
Matt Spinler5636d522021-03-17 14:52:18 -0500106 std::cerr << "Malformed threshold on configuration interface "
Zev Weissf783c692022-08-12 18:21:02 -0700107 << intf << "\n";
James Feist6714a252018-09-10 15:26:18 -0700108 return false;
109 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500110 unsigned int severity = std::visit(VariantToUnsignedIntVisitor(),
111 severityFind->second);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530112
Patrick Williams779c96a2023-05-10 07:50:42 -0500113 std::string directions = std::visit(VariantToStringVisitor(),
114 directionFind->second);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530115
Ed Tanousc8fed202022-01-12 14:24:45 -0800116 Level level = findThresholdLevel(severity);
117 Direction direction = findThresholdDirection(directions);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530118
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +0530119 if ((level == Level::ERROR) || (direction == Direction::ERROR))
James Feist6714a252018-09-10 15:26:18 -0700120 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530121 continue;
James Feist6714a252018-09-10 15:26:18 -0700122 }
James Feist13f340b2019-03-07 16:36:11 -0800123 double val = std::visit(VariantToDoubleVisitor(), valueFind->second);
James Feist6714a252018-09-10 15:26:18 -0700124
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000125 thresholdVector.emplace_back(level, direction, val, hysteresis);
James Feist6714a252018-09-10 15:26:18 -0700126 }
127 return true;
128}
129
James Feistd8705872019-02-08 13:26:09 -0800130void persistThreshold(const std::string& path, const std::string& baseInterface,
131 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800132 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800133 size_t thresholdCount, const std::string& labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700134{
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500135 for (size_t ii = 0; ii < thresholdCount; ii++)
James Feist6714a252018-09-10 15:26:18 -0700136 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500137 std::string thresholdInterface = baseInterface + ".Thresholds" +
138 std::to_string(ii);
James Feist6714a252018-09-10 15:26:18 -0700139 conn->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700140 [&, path, threshold, thresholdInterface,
141 labelMatch](const boost::system::error_code& ec,
142 const SensorBaseConfigMap& result) {
Ed Tanousbb679322022-05-16 16:10:00 -0700143 if (ec)
144 {
145 return; // threshold not supported
146 }
James Feist6714a252018-09-10 15:26:18 -0700147
Ed Tanousbb679322022-05-16 16:10:00 -0700148 if (!labelMatch.empty())
149 {
150 auto labelFind = result.find("Label");
151 if (labelFind == result.end())
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800152 {
Ed Tanousbb679322022-05-16 16:10:00 -0700153 std::cerr << "No label in threshold configuration\n";
James Feist6714a252018-09-10 15:26:18 -0700154 return;
155 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500156 std::string label = std::visit(VariantToStringVisitor(),
157 labelFind->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700158 if (label != labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700159 {
Ed Tanousbb679322022-05-16 16:10:00 -0700160 return;
James Feist6714a252018-09-10 15:26:18 -0700161 }
Ed Tanousbb679322022-05-16 16:10:00 -0700162 }
James Feist6714a252018-09-10 15:26:18 -0700163
Ed Tanousbb679322022-05-16 16:10:00 -0700164 auto directionFind = result.find("Direction");
165 auto severityFind = result.find("Severity");
166 auto valueFind = result.find("Value");
167 if (valueFind == result.end() || severityFind == result.end() ||
168 directionFind == result.end())
169 {
170 std::cerr << "Malformed threshold in configuration\n";
171 return;
172 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500173 unsigned int severity = std::visit(VariantToUnsignedIntVisitor(),
174 severityFind->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700175
Patrick Williams779c96a2023-05-10 07:50:42 -0500176 std::string dir = std::visit(VariantToStringVisitor(),
177 directionFind->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700178 if ((findThresholdLevel(severity) != threshold.level) ||
179 (findThresholdDirection(dir) != threshold.direction))
180 {
181 return; // not the droid we're looking for
182 }
183
184 std::variant<double> value(threshold.value);
185 conn->async_method_call(
186 [](const boost::system::error_code& ec) {
187 if (ec)
188 {
189 std::cerr << "Error setting threshold " << ec << "\n";
190 }
Patrick Williams597e8422023-10-20 11:19:01 -0500191 },
Ed Tanousbb679322022-05-16 16:10:00 -0700192 entityManagerName, path, "org.freedesktop.DBus.Properties",
193 "Set", thresholdInterface, "Value", value);
Patrick Williams597e8422023-10-20 11:19:01 -0500194 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700196 "GetAll", thresholdInterface);
197 }
198}
199
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800200void updateThresholds(Sensor* sensor)
201{
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800202 for (const auto& threshold : sensor->thresholds)
203 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530204 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
205 sensor->getThresholdInterface(threshold.level);
206
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800207 if (!interface)
208 {
209 continue;
210 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530211
Patrick Williams779c96a2023-05-10 07:50:42 -0500212 std::string property = Sensor::propertyLevel(threshold.level,
213 threshold.direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530214 if (property.empty())
215 {
216 continue;
217 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800218 interface->set_property(property, threshold.value);
219 }
220}
221
Josh Lehan883fb3a2020-02-27 14:41:39 -0800222// Debugging counters
223static int cHiTrue = 0;
224static int cHiFalse = 0;
225static int cHiMidstate = 0;
226static int cLoTrue = 0;
227static int cLoFalse = 0;
228static int cLoMidstate = 0;
229static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700230static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800231
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700232struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700233{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700234 ChangeParam(Threshold whichThreshold, bool status, double value) :
235 threshold(whichThreshold), asserted(status), assertValue(value)
236 {}
237
238 Threshold threshold;
239 bool asserted;
240 double assertValue;
241};
242
243static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
244{
245 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700246 if (sensor->thresholds.empty())
247 {
James Feist46342ec2019-03-06 14:03:41 -0800248 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700249 }
James Feist46342ec2019-03-06 14:03:41 -0800250
James Feistd8705872019-02-08 13:26:09 -0800251 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700252 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800253 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
254 // if value is noisy while hovering very close to a threshold.
255 // When a threshold is crossed, indicate true immediately,
256 // but require more distance to be crossed the other direction,
257 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800258 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700259 {
James Feist551087a2019-12-09 11:17:12 -0800260 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700261 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700262 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700263 if (++cHiTrue < assertLogCount)
264 {
265 std::cerr << "Sensor " << sensor->name << " high threshold "
266 << threshold.value << " assert: value " << value
267 << " raw data " << sensor->rawValue << "\n";
268 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800269 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000270 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800271 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700272 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800273 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700274 }
Patrick Venture66235d42019-10-11 08:31:27 -0700275 else
James Feist251c7822018-09-12 12:54:15 -0700276 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800277 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700278 }
279 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800280 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700281 {
James Feist551087a2019-12-09 11:17:12 -0800282 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700283 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700284 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700285 if (++cLoTrue < assertLogCount)
286 {
287 std::cerr << "Sensor " << sensor->name << " low threshold "
288 << threshold.value << " assert: value "
289 << sensor->value << " raw data "
290 << sensor->rawValue << "\n";
291 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800292 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000293 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800294 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700295 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800296 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700297 }
Patrick Venture66235d42019-10-11 08:31:27 -0700298 else
James Feist251c7822018-09-12 12:54:15 -0700299 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800300 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700301 }
302 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800303 else
304 {
305 std::cerr << "Error determining threshold direction\n";
306 }
James Feist251c7822018-09-12 12:54:15 -0700307 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800308
Ed Tanous8a17c302021-09-02 15:07:11 -0700309 // Throttle debug output, so that it does not continuously spam
310 ++cDebugThrottle;
311 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800312 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700313 cDebugThrottle = 0;
314 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800315 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800316 std::cerr << "checkThresholds: High T=" << cHiTrue
317 << " F=" << cHiFalse << " M=" << cHiMidstate
318 << ", Low T=" << cLoTrue << " F=" << cLoFalse
319 << " M=" << cLoMidstate << "\n";
320 }
321 }
322
James Feist46342ec2019-03-06 14:03:41 -0800323 return thresholdChanges;
324}
325
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700326void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
327 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800328 double assertValue)
329{
330 struct TimerUsed timerUsed = {};
331 constexpr const size_t waitTime = 5;
332 TimerPair* pair = nullptr;
333
334 for (TimerPair& timer : timers)
335 {
336 if (!timer.first.used)
337 {
338 pair = &timer;
339 break;
340 }
341 }
342 if (pair == nullptr)
343 {
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700344 pair = &timers.emplace_back(timerUsed, boost::asio::steady_timer(io));
Jeff Lind9cd7042020-11-20 15:49:28 +0800345 }
346
347 pair->first.used = true;
348 pair->first.level = threshold.level;
349 pair->first.direction = threshold.direction;
350 pair->first.assert = assert;
Ed Tanous83db50c2023-03-01 10:20:24 -0800351 pair->second.expires_after(std::chrono::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700352 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800353 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700354 auto sensorPtr = weakSensor.lock();
355 if (!sensorPtr)
356 {
357 return; // owner sensor has been destructed
358 }
359 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800360 pair->first.used = false;
361
362 if (ec == boost::asio::error::operation_aborted)
363 {
364 return; // we're being canceled
365 }
366 if (ec)
367 {
368 std::cerr << "timer error: " << ec.message() << "\n";
369 return;
370 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700371 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800372 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700373 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800374 threshold.direction, assert);
375 }
376 });
377}
378
James Feist46342ec2019-03-06 14:03:41 -0800379bool checkThresholds(Sensor* sensor)
380{
James Feist7b18b1e2019-05-14 13:42:09 -0700381 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700382 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
383 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800384 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700385 assertThresholds(sensor, change.assertValue, change.threshold.level,
386 change.threshold.direction, change.asserted);
387 if (change.threshold.level == thresholds::Level::CRITICAL &&
388 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800389 {
James Feist7b18b1e2019-05-14 13:42:09 -0700390 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800391 }
392 }
393
James Feistdc6c55f2018-10-31 12:53:20 -0700394 return status;
James Feist251c7822018-09-12 12:54:15 -0700395}
396
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700397void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
398 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800399{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700400 auto sensorPtr = weakSensor.lock();
401 if (!sensorPtr)
402 {
403 return; // sensor is destructed, should never be here
404 }
James Feist46342ec2019-03-06 14:03:41 -0800405
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700406 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700407 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
408 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800409 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700410 // When CPU is powered off, some volatges are expected to
411 // go below low thresholds. Filter these events with thresholdTimer.
412 // 1. always delay the assertion of low events to see if they are
413 // caused by power off event.
414 // 2. conditional delay the de-assertion of low events if there is
415 // an existing timer for assertion.
416 // 3. no delays for de-assert of low events if there is an existing
417 // de-assert for low event. This means 2nd de-assert would happen
418 // first and when timer expires for the previous one, no additional
419 // signal will be logged.
420 // 4. no delays for all high events.
421 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800422 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700423 if (change.asserted || thresholdTimer.hasActiveTimer(
424 change.threshold, !change.asserted))
425 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700426 thresholdTimer.startTimer(weakSensor, change.threshold,
427 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700428 continue;
429 }
James Feist46342ec2019-03-06 14:03:41 -0800430 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700431 assertThresholds(sensor, change.assertValue, change.threshold.level,
432 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800433 }
434}
435
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700436void assertThresholds(Sensor* sensor, double assertValue,
437 thresholds::Level level, thresholds::Direction direction,
438 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700439{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530440 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
441 sensor->getThresholdInterface(level);
442
James Feist251c7822018-09-12 12:54:15 -0700443 if (!interface)
444 {
445 std::cout << "trying to set uninitialized interface\n";
446 return;
447 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700448
Ed Tanous2049bd22022-07-09 07:20:26 -0700449 std::string property = Sensor::propertyAlarm(level, direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530450 if (property.empty())
451 {
452 std::cout << "Alarm property is empty \n";
453 return;
454 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700455 if (interface->set_property<bool, true>(property, assert))
456 {
457 try
458 {
459 // msg.get_path() is interface->get_object_path()
Patrick Williams92f8f512022-07-22 19:26:55 -0500460 sdbusplus::message_t msg =
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700461 interface->new_signal("ThresholdAsserted");
462
463 msg.append(sensor->name, interface->get_interface_name(), property,
464 assert, assertValue);
465 msg.signal_send();
466 }
Patrick Williams92f8f512022-07-22 19:26:55 -0500467 catch (const sdbusplus::exception_t& e)
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700468 {
469 std::cerr
470 << "Failed to send thresholdAsserted signal with assertValue\n";
471 }
472 }
James Feist251c7822018-09-12 12:54:15 -0700473}
474
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700475bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800476 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700477 const std::string& inputPath, const double& scaleFactor,
Chris Sidesa3279232023-04-24 16:08:13 -0500478 const double& offset, const double& hysteresis)
James Feist6714a252018-09-10 15:26:18 -0700479{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200480 const boost::container::flat_map<
481 std::string, std::vector<std::tuple<const char*, thresholds::Level,
482 thresholds::Direction, double>>>
483 map = {
484 {"average",
485 {
486 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
487 0.0),
488 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
489 0.0),
490 }},
491 {"input",
492 {
493 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
494 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
495 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
496 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
497 offset),
498 }},
499 };
500
501 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700502 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200503 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200504 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700505 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200506 for (const auto& t : map.at(item))
507 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700508 const auto& [suffix, level, direction, offset] = t;
Patrick Williams779c96a2023-05-10 07:50:42 -0500509 auto attrPath = boost::replace_all_copy(inputPath, item,
510 suffix);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200511 if (auto val = readFile(attrPath, scaleFactor))
512 {
513 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700514 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200515 {
516 std::cout << "Threshold: " << attrPath << ": " << *val
517 << "\n";
518 }
Chris Sidesa3279232023-04-24 16:08:13 -0500519 thresholdVector.emplace_back(level, direction, *val,
520 hysteresis);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200521 }
522 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700523 }
James Feist6714a252018-09-10 15:26:18 -0700524 }
James Feist6714a252018-09-10 15:26:18 -0700525 return true;
526}
527
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530528std::string getInterface(const Level thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700529{
Ed Tanousc8fed202022-01-12 14:24:45 -0800530 for (const ThresholdDefinition& thresh : thresProp)
James Feist6714a252018-09-10 15:26:18 -0700531 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800532 if (thresh.level == thresholdLevel)
533 {
534 return std::string("xyz.openbmc_project.Sensor.Threshold.") +
535 thresh.levelName;
536 }
James Feist6714a252018-09-10 15:26:18 -0700537 }
Ed Tanousc8fed202022-01-12 14:24:45 -0800538 return "";
James Feist6714a252018-09-10 15:26:18 -0700539}
540} // namespace thresholds