blob: 610e68ee1111611c27fec20869d80212dd848041 [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{
Ed Tanousc8fed202022-01-12 14:24:45 -080022Level findThresholdLevel(uint8_t sev)
James Feist6714a252018-09-10 15:26:18 -070023{
Ed Tanousc8fed202022-01-12 14:24:45 -080024 for (const ThresholdDefinition& prop : thresProp)
James Feist6714a252018-09-10 15:26:18 -070025 {
Ed Tanousc8fed202022-01-12 14:24:45 -080026 if (prop.sevOrder == sev)
James Feist6714a252018-09-10 15:26:18 -070027 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053028 return prop.level;
James Feist6714a252018-09-10 15:26:18 -070029 }
30 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053031 return Level::ERROR;
James Feist6714a252018-09-10 15:26:18 -070032}
33
Ed Tanousc8fed202022-01-12 14:24:45 -080034Direction findThresholdDirection(const std::string& direct)
James Feist6714a252018-09-10 15:26:18 -070035{
Ed Tanousc8fed202022-01-12 14:24:45 -080036 if (direct == "greater than")
James Feist6714a252018-09-10 15:26:18 -070037 {
Ed Tanousc8fed202022-01-12 14:24:45 -080038 return Direction::HIGH;
39 }
40 if (direct == "less than")
41 {
42 return Direction::LOW;
James Feist6714a252018-09-10 15:26:18 -070043 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053044 return Direction::ERROR;
James Feist6714a252018-09-10 15:26:18 -070045}
46
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070047bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080048 const SensorData& sensorData,
49 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -050050 const std::string* matchLabel, const int* sensorIndex)
James Feist6714a252018-09-10 15:26:18 -070051{
Zev Weissf783c692022-08-12 18:21:02 -070052 for (const auto& [intf, cfg] : sensorData)
James Feist6714a252018-09-10 15:26:18 -070053 {
Zev Weissf783c692022-08-12 18:21:02 -070054 if (intf.find("Thresholds") == std::string::npos)
James Feist6714a252018-09-10 15:26:18 -070055 {
56 continue;
57 }
58 if (matchLabel != nullptr)
59 {
Zev Weissf783c692022-08-12 18:21:02 -070060 auto labelFind = cfg.find("Label");
61 if (labelFind == cfg.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -070062 {
James Feist6714a252018-09-10 15:26:18 -070063 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070064 }
James Feist3eb82622019-02-08 13:10:22 -080065 if (std::visit(VariantToStringVisitor(), labelFind->second) !=
66 *matchLabel)
Ed Tanous8a57ec02020-10-09 12:46:52 -070067 {
James Feist6714a252018-09-10 15:26:18 -070068 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070069 }
James Feist6714a252018-09-10 15:26:18 -070070 }
Matt Spinler5636d522021-03-17 14:52:18 -050071
72 if (sensorIndex != nullptr)
73 {
Zev Weissf783c692022-08-12 18:21:02 -070074 auto indexFind = cfg.find("Index");
Matt Spinler5636d522021-03-17 14:52:18 -050075
76 // If we're checking for index 1, a missing Index is OK.
Zev Weissf783c692022-08-12 18:21:02 -070077 if ((indexFind == cfg.end()) && (*sensorIndex != 1))
Matt Spinler5636d522021-03-17 14:52:18 -050078 {
79 continue;
80 }
81
Zev Weissf783c692022-08-12 18:21:02 -070082 if ((indexFind != cfg.end()) &&
Matt Spinler5636d522021-03-17 14:52:18 -050083 (std::visit(VariantToIntVisitor(), indexFind->second) !=
84 *sensorIndex))
85 {
86 continue;
87 }
88 }
89
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100090 double hysteresis = std::numeric_limits<double>::quiet_NaN();
Zev Weissf783c692022-08-12 18:21:02 -070091 auto hysteresisFind = cfg.find("Hysteresis");
92 if (hysteresisFind != cfg.end())
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100093 {
94 hysteresis =
95 std::visit(VariantToDoubleVisitor(), hysteresisFind->second);
96 }
97
Zev Weissf783c692022-08-12 18:21:02 -070098 auto directionFind = cfg.find("Direction");
99 auto severityFind = cfg.find("Severity");
100 auto valueFind = cfg.find("Value");
101 if (valueFind == cfg.end() || severityFind == cfg.end() ||
102 directionFind == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700103 {
Matt Spinler5636d522021-03-17 14:52:18 -0500104 std::cerr << "Malformed threshold on configuration interface "
Zev Weissf783c692022-08-12 18:21:02 -0700105 << intf << "\n";
James Feist6714a252018-09-10 15:26:18 -0700106 return false;
107 }
Rashmica Guptad2208a92022-01-15 13:04:22 +1100108 unsigned int severity =
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530109 std::visit(VariantToUnsignedIntVisitor(), severityFind->second);
110
111 std::string directions =
112 std::visit(VariantToStringVisitor(), directionFind->second);
113
Ed Tanousc8fed202022-01-12 14:24:45 -0800114 Level level = findThresholdLevel(severity);
115 Direction direction = findThresholdDirection(directions);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530116
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +0530117 if ((level == Level::ERROR) || (direction == Direction::ERROR))
James Feist6714a252018-09-10 15:26:18 -0700118 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530119 continue;
James Feist6714a252018-09-10 15:26:18 -0700120 }
James Feist13f340b2019-03-07 16:36:11 -0800121 double val = std::visit(VariantToDoubleVisitor(), valueFind->second);
James Feist6714a252018-09-10 15:26:18 -0700122
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000123 thresholdVector.emplace_back(level, direction, val, hysteresis);
James Feist6714a252018-09-10 15:26:18 -0700124 }
125 return true;
126}
127
James Feistd8705872019-02-08 13:26:09 -0800128void persistThreshold(const std::string& path, const std::string& baseInterface,
129 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800130 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800131 size_t thresholdCount, const std::string& labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700132{
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500133 for (size_t ii = 0; ii < thresholdCount; ii++)
James Feist6714a252018-09-10 15:26:18 -0700134 {
135 std::string thresholdInterface =
136 baseInterface + ".Thresholds" + std::to_string(ii);
137 conn->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700138 [&, path, threshold, thresholdInterface,
139 labelMatch](const boost::system::error_code& ec,
140 const SensorBaseConfigMap& result) {
Ed Tanousbb679322022-05-16 16:10:00 -0700141 if (ec)
142 {
143 return; // threshold not supported
144 }
James Feist6714a252018-09-10 15:26:18 -0700145
Ed Tanousbb679322022-05-16 16:10:00 -0700146 if (!labelMatch.empty())
147 {
148 auto labelFind = result.find("Label");
149 if (labelFind == result.end())
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800150 {
Ed Tanousbb679322022-05-16 16:10:00 -0700151 std::cerr << "No label in threshold configuration\n";
James Feist6714a252018-09-10 15:26:18 -0700152 return;
153 }
Ed Tanousbb679322022-05-16 16:10:00 -0700154 std::string label =
155 std::visit(VariantToStringVisitor(), labelFind->second);
156 if (label != labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700157 {
Ed Tanousbb679322022-05-16 16:10:00 -0700158 return;
James Feist6714a252018-09-10 15:26:18 -0700159 }
Ed Tanousbb679322022-05-16 16:10:00 -0700160 }
James Feist6714a252018-09-10 15:26:18 -0700161
Ed Tanousbb679322022-05-16 16:10:00 -0700162 auto directionFind = result.find("Direction");
163 auto severityFind = result.find("Severity");
164 auto valueFind = result.find("Value");
165 if (valueFind == result.end() || severityFind == result.end() ||
166 directionFind == result.end())
167 {
168 std::cerr << "Malformed threshold in configuration\n";
169 return;
170 }
171 unsigned int severity =
172 std::visit(VariantToUnsignedIntVisitor(), severityFind->second);
173
174 std::string dir =
175 std::visit(VariantToStringVisitor(), directionFind->second);
176 if ((findThresholdLevel(severity) != threshold.level) ||
177 (findThresholdDirection(dir) != threshold.direction))
178 {
179 return; // not the droid we're looking for
180 }
181
182 std::variant<double> value(threshold.value);
183 conn->async_method_call(
184 [](const boost::system::error_code& ec) {
185 if (ec)
186 {
187 std::cerr << "Error setting threshold " << ec << "\n";
188 }
189 },
190 entityManagerName, path, "org.freedesktop.DBus.Properties",
191 "Set", thresholdInterface, "Value", value);
James Feist6714a252018-09-10 15:26:18 -0700192 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700194 "GetAll", thresholdInterface);
195 }
196}
197
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800198void updateThresholds(Sensor* sensor)
199{
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800200 for (const auto& threshold : sensor->thresholds)
201 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530202 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
203 sensor->getThresholdInterface(threshold.level);
204
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800205 if (!interface)
206 {
207 continue;
208 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530209
210 std::string property =
Ed Tanous2049bd22022-07-09 07:20:26 -0700211 Sensor::propertyLevel(threshold.level, threshold.direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530212 if (property.empty())
213 {
214 continue;
215 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800216 interface->set_property(property, threshold.value);
217 }
218}
219
Josh Lehan883fb3a2020-02-27 14:41:39 -0800220// Debugging counters
221static int cHiTrue = 0;
222static int cHiFalse = 0;
223static int cHiMidstate = 0;
224static int cLoTrue = 0;
225static int cLoFalse = 0;
226static int cLoMidstate = 0;
227static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700228static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800229
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700230struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700231{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700232 ChangeParam(Threshold whichThreshold, bool status, double value) :
233 threshold(whichThreshold), asserted(status), assertValue(value)
234 {}
235
236 Threshold threshold;
237 bool asserted;
238 double assertValue;
239};
240
241static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
242{
243 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700244 if (sensor->thresholds.empty())
245 {
James Feist46342ec2019-03-06 14:03:41 -0800246 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700247 }
James Feist46342ec2019-03-06 14:03:41 -0800248
James Feistd8705872019-02-08 13:26:09 -0800249 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700250 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800251 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
252 // if value is noisy while hovering very close to a threshold.
253 // When a threshold is crossed, indicate true immediately,
254 // but require more distance to be crossed the other direction,
255 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800256 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700257 {
James Feist551087a2019-12-09 11:17:12 -0800258 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700259 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700260 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700261 if (++cHiTrue < assertLogCount)
262 {
263 std::cerr << "Sensor " << sensor->name << " high threshold "
264 << threshold.value << " assert: value " << value
265 << " raw data " << sensor->rawValue << "\n";
266 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800267 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000268 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800269 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700270 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800271 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700272 }
Patrick Venture66235d42019-10-11 08:31:27 -0700273 else
James Feist251c7822018-09-12 12:54:15 -0700274 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800275 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700276 }
277 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800278 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700279 {
James Feist551087a2019-12-09 11:17:12 -0800280 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700281 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700282 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700283 if (++cLoTrue < assertLogCount)
284 {
285 std::cerr << "Sensor " << sensor->name << " low threshold "
286 << threshold.value << " assert: value "
287 << sensor->value << " raw data "
288 << sensor->rawValue << "\n";
289 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800290 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000291 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800292 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700293 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800294 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700295 }
Patrick Venture66235d42019-10-11 08:31:27 -0700296 else
James Feist251c7822018-09-12 12:54:15 -0700297 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800298 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700299 }
300 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800301 else
302 {
303 std::cerr << "Error determining threshold direction\n";
304 }
James Feist251c7822018-09-12 12:54:15 -0700305 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800306
Ed Tanous8a17c302021-09-02 15:07:11 -0700307 // Throttle debug output, so that it does not continuously spam
308 ++cDebugThrottle;
309 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800310 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700311 cDebugThrottle = 0;
312 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800313 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800314 std::cerr << "checkThresholds: High T=" << cHiTrue
315 << " F=" << cHiFalse << " M=" << cHiMidstate
316 << ", Low T=" << cLoTrue << " F=" << cLoFalse
317 << " M=" << cLoMidstate << "\n";
318 }
319 }
320
James Feist46342ec2019-03-06 14:03:41 -0800321 return thresholdChanges;
322}
323
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700324void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
325 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800326 double assertValue)
327{
328 struct TimerUsed timerUsed = {};
329 constexpr const size_t waitTime = 5;
330 TimerPair* pair = nullptr;
331
332 for (TimerPair& timer : timers)
333 {
334 if (!timer.first.used)
335 {
336 pair = &timer;
337 break;
338 }
339 }
340 if (pair == nullptr)
341 {
342 pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io));
343 }
344
345 pair->first.used = true;
346 pair->first.level = threshold.level;
347 pair->first.direction = threshold.direction;
348 pair->first.assert = assert;
349 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700350 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800351 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700352 auto sensorPtr = weakSensor.lock();
353 if (!sensorPtr)
354 {
355 return; // owner sensor has been destructed
356 }
357 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800358 pair->first.used = false;
359
360 if (ec == boost::asio::error::operation_aborted)
361 {
362 return; // we're being canceled
363 }
364 if (ec)
365 {
366 std::cerr << "timer error: " << ec.message() << "\n";
367 return;
368 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700369 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800370 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700371 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800372 threshold.direction, assert);
373 }
374 });
375}
376
James Feist46342ec2019-03-06 14:03:41 -0800377bool checkThresholds(Sensor* sensor)
378{
James Feist7b18b1e2019-05-14 13:42:09 -0700379 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700380 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
381 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800382 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700383 assertThresholds(sensor, change.assertValue, change.threshold.level,
384 change.threshold.direction, change.asserted);
385 if (change.threshold.level == thresholds::Level::CRITICAL &&
386 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800387 {
James Feist7b18b1e2019-05-14 13:42:09 -0700388 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800389 }
390 }
391
James Feistdc6c55f2018-10-31 12:53:20 -0700392 return status;
James Feist251c7822018-09-12 12:54:15 -0700393}
394
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700395void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
396 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800397{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700398 auto sensorPtr = weakSensor.lock();
399 if (!sensorPtr)
400 {
401 return; // sensor is destructed, should never be here
402 }
James Feist46342ec2019-03-06 14:03:41 -0800403
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700404 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700405 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
406 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800407 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700408 // When CPU is powered off, some volatges are expected to
409 // go below low thresholds. Filter these events with thresholdTimer.
410 // 1. always delay the assertion of low events to see if they are
411 // caused by power off event.
412 // 2. conditional delay the de-assertion of low events if there is
413 // an existing timer for assertion.
414 // 3. no delays for de-assert of low events if there is an existing
415 // de-assert for low event. This means 2nd de-assert would happen
416 // first and when timer expires for the previous one, no additional
417 // signal will be logged.
418 // 4. no delays for all high events.
419 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800420 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700421 if (change.asserted || thresholdTimer.hasActiveTimer(
422 change.threshold, !change.asserted))
423 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700424 thresholdTimer.startTimer(weakSensor, change.threshold,
425 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700426 continue;
427 }
James Feist46342ec2019-03-06 14:03:41 -0800428 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700429 assertThresholds(sensor, change.assertValue, change.threshold.level,
430 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800431 }
432}
433
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700434void assertThresholds(Sensor* sensor, double assertValue,
435 thresholds::Level level, thresholds::Direction direction,
436 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700437{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530438 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
439 sensor->getThresholdInterface(level);
440
James Feist251c7822018-09-12 12:54:15 -0700441 if (!interface)
442 {
443 std::cout << "trying to set uninitialized interface\n";
444 return;
445 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700446
Ed Tanous2049bd22022-07-09 07:20:26 -0700447 std::string property = Sensor::propertyAlarm(level, direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530448 if (property.empty())
449 {
450 std::cout << "Alarm property is empty \n";
451 return;
452 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700453 if (interface->set_property<bool, true>(property, assert))
454 {
455 try
456 {
457 // msg.get_path() is interface->get_object_path()
Patrick Williams92f8f512022-07-22 19:26:55 -0500458 sdbusplus::message_t msg =
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700459 interface->new_signal("ThresholdAsserted");
460
461 msg.append(sensor->name, interface->get_interface_name(), property,
462 assert, assertValue);
463 msg.signal_send();
464 }
Patrick Williams92f8f512022-07-22 19:26:55 -0500465 catch (const sdbusplus::exception_t& e)
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700466 {
467 std::cerr
468 << "Failed to send thresholdAsserted signal with assertValue\n";
469 }
470 }
James Feist251c7822018-09-12 12:54:15 -0700471}
472
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700473bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800474 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700475 const std::string& inputPath, const double& scaleFactor,
476 const double& offset)
James Feist6714a252018-09-10 15:26:18 -0700477{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200478 const boost::container::flat_map<
479 std::string, std::vector<std::tuple<const char*, thresholds::Level,
480 thresholds::Direction, double>>>
481 map = {
482 {"average",
483 {
484 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
485 0.0),
486 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
487 0.0),
488 }},
489 {"input",
490 {
491 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
492 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
493 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
494 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
495 offset),
496 }},
497 };
498
499 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700500 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200501 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200502 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700503 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200504 for (const auto& t : map.at(item))
505 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700506 const auto& [suffix, level, direction, offset] = t;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200507 auto attrPath =
508 boost::replace_all_copy(inputPath, item, suffix);
509 if (auto val = readFile(attrPath, scaleFactor))
510 {
511 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700512 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200513 {
514 std::cout << "Threshold: " << attrPath << ": " << *val
515 << "\n";
516 }
517 thresholdVector.emplace_back(level, direction, *val);
518 }
519 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700520 }
James Feist6714a252018-09-10 15:26:18 -0700521 }
James Feist6714a252018-09-10 15:26:18 -0700522 return true;
523}
524
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530525std::string getInterface(const Level thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700526{
Ed Tanousc8fed202022-01-12 14:24:45 -0800527 for (const ThresholdDefinition& thresh : thresProp)
James Feist6714a252018-09-10 15:26:18 -0700528 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800529 if (thresh.level == thresholdLevel)
530 {
531 return std::string("xyz.openbmc_project.Sensor.Threshold.") +
532 thresh.levelName;
533 }
James Feist6714a252018-09-10 15:26:18 -0700534 }
Ed Tanousc8fed202022-01-12 14:24:45 -0800535 return "";
James Feist6714a252018-09-10 15:26:18 -0700536}
537} // namespace thresholds