blob: 5fa82aa7dacfe947a0a8e40bd10539a87bf16ea2 [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) {
James Feist6714a252018-09-10 15:26:18 -0700143 if (ec)
144 {
145 return; // threshold not supported
146 }
147
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800148 if (!labelMatch.empty())
149 {
150 auto labelFind = result.find("Label");
151 if (labelFind == result.end())
152 {
153 std::cerr << "No label in threshold configuration\n";
154 return;
155 }
156 std::string label =
157 std::visit(VariantToStringVisitor(), labelFind->second);
158 if (label != labelMatch)
159 {
160 return;
161 }
162 }
163
James Feist6714a252018-09-10 15:26:18 -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 }
Rashmica Guptad2208a92022-01-15 13:04:22 +1100173 unsigned int severity = std::visit(
174 VariantToUnsignedIntVisitor(), severityFind->second);
James Feist6714a252018-09-10 15:26:18 -0700175
James Feist3eb82622019-02-08 13:10:22 -0800176 std::string dir =
177 std::visit(VariantToStringVisitor(), directionFind->second);
Ed Tanousc8fed202022-01-12 14:24:45 -0800178 if ((findThresholdLevel(severity) != threshold.level) ||
179 (findThresholdDirection(dir) != threshold.direction))
James Feist6714a252018-09-10 15:26:18 -0700180 {
181 return; // not the droid we're looking for
182 }
183
James Feist3eb82622019-02-08 13:10:22 -0800184 std::variant<double> value(threshold.value);
James Feist6714a252018-09-10 15:26:18 -0700185 conn->async_method_call(
James Feistd8705872019-02-08 13:26:09 -0800186 [](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700187 if (ec)
188 {
189 std::cerr << "Error setting threshold " << ec
190 << "\n";
191 }
192 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193 entityManagerName, path, "org.freedesktop.DBus.Properties",
194 "Set", thresholdInterface, "Value", value);
James Feist6714a252018-09-10 15:26:18 -0700195 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700196 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700197 "GetAll", thresholdInterface);
198 }
199}
200
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800201void updateThresholds(Sensor* sensor)
202{
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800203 for (const auto& threshold : sensor->thresholds)
204 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530205 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
206 sensor->getThresholdInterface(threshold.level);
207
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800208 if (!interface)
209 {
210 continue;
211 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530212
213 std::string property =
214 sensor->propertyLevel(threshold.level, threshold.direction);
215 if (property.empty())
216 {
217 continue;
218 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800219 interface->set_property(property, threshold.value);
220 }
221}
222
Josh Lehan883fb3a2020-02-27 14:41:39 -0800223// Debugging counters
224static int cHiTrue = 0;
225static int cHiFalse = 0;
226static int cHiMidstate = 0;
227static int cLoTrue = 0;
228static int cLoFalse = 0;
229static int cLoMidstate = 0;
230static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700231static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800232
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700233struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700234{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700235 ChangeParam(Threshold whichThreshold, bool status, double value) :
236 threshold(whichThreshold), asserted(status), assertValue(value)
237 {}
238
239 Threshold threshold;
240 bool asserted;
241 double assertValue;
242};
243
244static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
245{
246 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700247 if (sensor->thresholds.empty())
248 {
James Feist46342ec2019-03-06 14:03:41 -0800249 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700250 }
James Feist46342ec2019-03-06 14:03:41 -0800251
James Feistd8705872019-02-08 13:26:09 -0800252 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700253 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800254 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
255 // if value is noisy while hovering very close to a threshold.
256 // When a threshold is crossed, indicate true immediately,
257 // but require more distance to be crossed the other direction,
258 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800259 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700260 {
James Feist551087a2019-12-09 11:17:12 -0800261 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700262 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700263 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700264 if (++cHiTrue < assertLogCount)
265 {
266 std::cerr << "Sensor " << sensor->name << " high threshold "
267 << threshold.value << " assert: value " << value
268 << " raw data " << sensor->rawValue << "\n";
269 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800270 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000271 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800272 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700273 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800274 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700275 }
Patrick Venture66235d42019-10-11 08:31:27 -0700276 else
James Feist251c7822018-09-12 12:54:15 -0700277 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800278 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700279 }
280 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800281 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700282 {
James Feist551087a2019-12-09 11:17:12 -0800283 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700284 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700285 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700286 if (++cLoTrue < assertLogCount)
287 {
288 std::cerr << "Sensor " << sensor->name << " low threshold "
289 << threshold.value << " assert: value "
290 << sensor->value << " raw data "
291 << sensor->rawValue << "\n";
292 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800293 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000294 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800295 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700296 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800297 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700298 }
Patrick Venture66235d42019-10-11 08:31:27 -0700299 else
James Feist251c7822018-09-12 12:54:15 -0700300 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800301 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700302 }
303 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800304 else
305 {
306 std::cerr << "Error determining threshold direction\n";
307 }
James Feist251c7822018-09-12 12:54:15 -0700308 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800309
Ed Tanous8a17c302021-09-02 15:07:11 -0700310 // Throttle debug output, so that it does not continuously spam
311 ++cDebugThrottle;
312 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800313 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700314 cDebugThrottle = 0;
315 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800316 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800317 std::cerr << "checkThresholds: High T=" << cHiTrue
318 << " F=" << cHiFalse << " M=" << cHiMidstate
319 << ", Low T=" << cLoTrue << " F=" << cLoFalse
320 << " M=" << cLoMidstate << "\n";
321 }
322 }
323
James Feist46342ec2019-03-06 14:03:41 -0800324 return thresholdChanges;
325}
326
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700327void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
328 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800329 double assertValue)
330{
331 struct TimerUsed timerUsed = {};
332 constexpr const size_t waitTime = 5;
333 TimerPair* pair = nullptr;
334
335 for (TimerPair& timer : timers)
336 {
337 if (!timer.first.used)
338 {
339 pair = &timer;
340 break;
341 }
342 }
343 if (pair == nullptr)
344 {
345 pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io));
346 }
347
348 pair->first.used = true;
349 pair->first.level = threshold.level;
350 pair->first.direction = threshold.direction;
351 pair->first.assert = assert;
352 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700353 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800354 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700355 auto sensorPtr = weakSensor.lock();
356 if (!sensorPtr)
357 {
358 return; // owner sensor has been destructed
359 }
360 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800361 pair->first.used = false;
362
363 if (ec == boost::asio::error::operation_aborted)
364 {
365 return; // we're being canceled
366 }
367 if (ec)
368 {
369 std::cerr << "timer error: " << ec.message() << "\n";
370 return;
371 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700372 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800373 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700374 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800375 threshold.direction, assert);
376 }
377 });
378}
379
James Feist46342ec2019-03-06 14:03:41 -0800380bool checkThresholds(Sensor* sensor)
381{
James Feist7b18b1e2019-05-14 13:42:09 -0700382 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700383 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
384 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800385 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700386 assertThresholds(sensor, change.assertValue, change.threshold.level,
387 change.threshold.direction, change.asserted);
388 if (change.threshold.level == thresholds::Level::CRITICAL &&
389 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800390 {
James Feist7b18b1e2019-05-14 13:42:09 -0700391 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800392 }
393 }
394
James Feistdc6c55f2018-10-31 12:53:20 -0700395 return status;
James Feist251c7822018-09-12 12:54:15 -0700396}
397
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700398void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
399 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800400{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700401 auto sensorPtr = weakSensor.lock();
402 if (!sensorPtr)
403 {
404 return; // sensor is destructed, should never be here
405 }
James Feist46342ec2019-03-06 14:03:41 -0800406
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700407 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700408 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
409 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800410 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700411 // When CPU is powered off, some volatges are expected to
412 // go below low thresholds. Filter these events with thresholdTimer.
413 // 1. always delay the assertion of low events to see if they are
414 // caused by power off event.
415 // 2. conditional delay the de-assertion of low events if there is
416 // an existing timer for assertion.
417 // 3. no delays for de-assert of low events if there is an existing
418 // de-assert for low event. This means 2nd de-assert would happen
419 // first and when timer expires for the previous one, no additional
420 // signal will be logged.
421 // 4. no delays for all high events.
422 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800423 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700424 if (change.asserted || thresholdTimer.hasActiveTimer(
425 change.threshold, !change.asserted))
426 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700427 thresholdTimer.startTimer(weakSensor, change.threshold,
428 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700429 continue;
430 }
James Feist46342ec2019-03-06 14:03:41 -0800431 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700432 assertThresholds(sensor, change.assertValue, change.threshold.level,
433 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800434 }
435}
436
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700437void assertThresholds(Sensor* sensor, double assertValue,
438 thresholds::Level level, thresholds::Direction direction,
439 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700440{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530441 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
442 sensor->getThresholdInterface(level);
443
James Feist251c7822018-09-12 12:54:15 -0700444 if (!interface)
445 {
446 std::cout << "trying to set uninitialized interface\n";
447 return;
448 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700449
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530450 std::string property = sensor->propertyAlarm(level, direction);
451 if (property.empty())
452 {
453 std::cout << "Alarm property is empty \n";
454 return;
455 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700456 if (interface->set_property<bool, true>(property, assert))
457 {
458 try
459 {
460 // msg.get_path() is interface->get_object_path()
461 sdbusplus::message::message msg =
462 interface->new_signal("ThresholdAsserted");
463
464 msg.append(sensor->name, interface->get_interface_name(), property,
465 assert, assertValue);
466 msg.signal_send();
467 }
468 catch (const sdbusplus::exception::exception& e)
469 {
470 std::cerr
471 << "Failed to send thresholdAsserted signal with assertValue\n";
472 }
473 }
James Feist251c7822018-09-12 12:54:15 -0700474}
475
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700476bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800477 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700478 const std::string& inputPath, const double& scaleFactor,
479 const double& offset)
James Feist6714a252018-09-10 15:26:18 -0700480{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200481 const boost::container::flat_map<
482 std::string, std::vector<std::tuple<const char*, thresholds::Level,
483 thresholds::Direction, double>>>
484 map = {
485 {"average",
486 {
487 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
488 0.0),
489 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
490 0.0),
491 }},
492 {"input",
493 {
494 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
495 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
496 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
497 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
498 offset),
499 }},
500 };
501
502 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700503 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200504 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200505 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700506 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200507 for (const auto& t : map.at(item))
508 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200509 auto& [suffix, level, direction, offset] = t;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200510 auto attrPath =
511 boost::replace_all_copy(inputPath, item, suffix);
512 if (auto val = readFile(attrPath, scaleFactor))
513 {
514 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700515 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200516 {
517 std::cout << "Threshold: " << attrPath << ": " << *val
518 << "\n";
519 }
520 thresholdVector.emplace_back(level, direction, *val);
521 }
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