blob: 1690c2547349c284d7108fa054aead5e3795d598 [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{
James Feistd8705872019-02-08 13:26:09 -080052 for (const auto& item : sensorData)
James Feist6714a252018-09-10 15:26:18 -070053 {
54 if (item.first.find("Thresholds") == std::string::npos)
55 {
56 continue;
57 }
58 if (matchLabel != nullptr)
59 {
60 auto labelFind = item.second.find("Label");
61 if (labelFind == item.second.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 {
74 auto indexFind = item.second.find("Index");
75
76 // If we're checking for index 1, a missing Index is OK.
77 if ((indexFind == item.second.end()) && (*sensorIndex != 1))
78 {
79 continue;
80 }
81
82 if ((indexFind != item.second.end()) &&
83 (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();
91 auto hysteresisFind = item.second.find("Hysteresis");
92 if (hysteresisFind != item.second.end())
93 {
94 hysteresis =
95 std::visit(VariantToDoubleVisitor(), hysteresisFind->second);
96 }
97
James Feist6714a252018-09-10 15:26:18 -070098 auto directionFind = item.second.find("Direction");
99 auto severityFind = item.second.find("Severity");
100 auto valueFind = item.second.find("Value");
101 if (valueFind == item.second.end() ||
102 severityFind == item.second.end() ||
103 directionFind == item.second.end())
104 {
Matt Spinler5636d522021-03-17 14:52:18 -0500105 std::cerr << "Malformed threshold on configuration interface "
106 << item.first << "\n";
James Feist6714a252018-09-10 15:26:18 -0700107 return false;
108 }
Rashmica Guptad2208a92022-01-15 13:04:22 +1100109 unsigned int severity =
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530110 std::visit(VariantToUnsignedIntVisitor(), severityFind->second);
111
112 std::string directions =
113 std::visit(VariantToStringVisitor(), directionFind->second);
114
Ed Tanousc8fed202022-01-12 14:24:45 -0800115 Level level = findThresholdLevel(severity);
116 Direction direction = findThresholdDirection(directions);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530117
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +0530118 if ((level == Level::ERROR) || (direction == Direction::ERROR))
James Feist6714a252018-09-10 15:26:18 -0700119 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530120 continue;
James Feist6714a252018-09-10 15:26:18 -0700121 }
James Feist13f340b2019-03-07 16:36:11 -0800122 double val = std::visit(VariantToDoubleVisitor(), valueFind->second);
James Feist6714a252018-09-10 15:26:18 -0700123
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000124 thresholdVector.emplace_back(level, direction, val, hysteresis);
James Feist6714a252018-09-10 15:26:18 -0700125 }
126 return true;
127}
128
James Feistd8705872019-02-08 13:26:09 -0800129void persistThreshold(const std::string& path, const std::string& baseInterface,
130 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800131 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800132 size_t thresholdCount, const std::string& labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700133{
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500134 for (size_t ii = 0; ii < thresholdCount; ii++)
James Feist6714a252018-09-10 15:26:18 -0700135 {
136 std::string thresholdInterface =
137 baseInterface + ".Thresholds" + std::to_string(ii);
138 conn->async_method_call(
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800139 [&, path, threshold, thresholdInterface, labelMatch](
James Feistd8705872019-02-08 13:26:09 -0800140 const boost::system::error_code& ec,
141 const boost::container::flat_map<std::string, BasicVariantType>&
142 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 }
Ed Tanousbb679322022-05-16 16:10:00 -0700156 std::string label =
157 std::visit(VariantToStringVisitor(), labelFind->second);
158 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 }
173 unsigned int severity =
174 std::visit(VariantToUnsignedIntVisitor(), severityFind->second);
175
176 std::string dir =
177 std::visit(VariantToStringVisitor(), directionFind->second);
178 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 }
191 },
192 entityManagerName, path, "org.freedesktop.DBus.Properties",
193 "Set", thresholdInterface, "Value", value);
James Feist6714a252018-09-10 15:26:18 -0700194 },
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
212 std::string property =
213 sensor->propertyLevel(threshold.level, threshold.direction);
214 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 {
344 pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io));
345 }
346
347 pair->first.used = true;
348 pair->first.level = threshold.level;
349 pair->first.direction = threshold.direction;
350 pair->first.assert = assert;
351 pair->second.expires_from_now(boost::posix_time::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
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530449 std::string property = sensor->propertyAlarm(level, direction);
450 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()
460 sdbusplus::message::message msg =
461 interface->new_signal("ThresholdAsserted");
462
463 msg.append(sensor->name, interface->get_interface_name(), property,
464 assert, assertValue);
465 msg.signal_send();
466 }
467 catch (const sdbusplus::exception::exception& e)
468 {
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,
478 const double& offset)
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 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200508 auto& [suffix, level, direction, offset] = t;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200509 auto attrPath =
510 boost::replace_all_copy(inputPath, item, suffix);
511 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 }
519 thresholdVector.emplace_back(level, direction, *val);
520 }
521 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700522 }
James Feist6714a252018-09-10 15:26:18 -0700523 }
James Feist6714a252018-09-10 15:26:18 -0700524 return true;
525}
526
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530527std::string getInterface(const Level thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700528{
Ed Tanousc8fed202022-01-12 14:24:45 -0800529 for (const ThresholdDefinition& thresh : thresProp)
James Feist6714a252018-09-10 15:26:18 -0700530 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800531 if (thresh.level == thresholdLevel)
532 {
533 return std::string("xyz.openbmc_project.Sensor.Threshold.") +
534 thresh.levelName;
535 }
James Feist6714a252018-09-10 15:26:18 -0700536 }
Ed Tanousc8fed202022-01-12 14:24:45 -0800537 return "";
James Feist6714a252018-09-10 15:26:18 -0700538}
539} // namespace thresholds