blob: f50905a196fb444bf9c7ae276c88f00433e97492 [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{
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053022Level findThresholdLevel(uint8_t sev, const std::string& direct)
James Feist6714a252018-09-10 15:26:18 -070023{
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053024 for (Sensor::ThresholdProperty prop : Sensor::thresProp)
James Feist6714a252018-09-10 15:26:18 -070025 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053026 if ((prop.sevOrder == sev) && (prop.dirOrder == direct))
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
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053034Direction findThresholdDirection(uint8_t sev, const std::string& direct)
James Feist6714a252018-09-10 15:26:18 -070035{
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053036 for (Sensor::ThresholdProperty prop : Sensor::thresProp)
James Feist6714a252018-09-10 15:26:18 -070037 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053038 if ((prop.sevOrder == sev) && (prop.dirOrder == direct))
James Feist6714a252018-09-10 15:26:18 -070039 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053040 return prop.direction;
James Feist6714a252018-09-10 15:26:18 -070041 }
42 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053043 return Direction::ERROR;
James Feist6714a252018-09-10 15:26:18 -070044}
45
Jayashree Dhanapal56678082022-01-04 17:27:20 +053046bool findOrder(Level lev, Direction dir)
47{
48 for (Sensor::ThresholdProperty prop : Sensor::thresProp)
49 {
50 if ((prop.level == lev) && (prop.direction == dir))
51 {
52 return true;
53 }
54 }
55 return false;
56}
57
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070058bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080059 const SensorData& sensorData,
60 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -050061 const std::string* matchLabel, const int* sensorIndex)
James Feist6714a252018-09-10 15:26:18 -070062{
James Feistd8705872019-02-08 13:26:09 -080063 for (const auto& item : sensorData)
James Feist6714a252018-09-10 15:26:18 -070064 {
65 if (item.first.find("Thresholds") == std::string::npos)
66 {
67 continue;
68 }
69 if (matchLabel != nullptr)
70 {
71 auto labelFind = item.second.find("Label");
72 if (labelFind == item.second.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -070073 {
James Feist6714a252018-09-10 15:26:18 -070074 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070075 }
James Feist3eb82622019-02-08 13:10:22 -080076 if (std::visit(VariantToStringVisitor(), labelFind->second) !=
77 *matchLabel)
Ed Tanous8a57ec02020-10-09 12:46:52 -070078 {
James Feist6714a252018-09-10 15:26:18 -070079 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070080 }
James Feist6714a252018-09-10 15:26:18 -070081 }
Matt Spinler5636d522021-03-17 14:52:18 -050082
83 if (sensorIndex != nullptr)
84 {
85 auto indexFind = item.second.find("Index");
86
87 // If we're checking for index 1, a missing Index is OK.
88 if ((indexFind == item.second.end()) && (*sensorIndex != 1))
89 {
90 continue;
91 }
92
93 if ((indexFind != item.second.end()) &&
94 (std::visit(VariantToIntVisitor(), indexFind->second) !=
95 *sensorIndex))
96 {
97 continue;
98 }
99 }
100
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000101 double hysteresis = std::numeric_limits<double>::quiet_NaN();
102 auto hysteresisFind = item.second.find("Hysteresis");
103 if (hysteresisFind != item.second.end())
104 {
105 hysteresis =
106 std::visit(VariantToDoubleVisitor(), hysteresisFind->second);
107 }
108
James Feist6714a252018-09-10 15:26:18 -0700109 auto directionFind = item.second.find("Direction");
110 auto severityFind = item.second.find("Severity");
111 auto valueFind = item.second.find("Value");
112 if (valueFind == item.second.end() ||
113 severityFind == item.second.end() ||
114 directionFind == item.second.end())
115 {
Matt Spinler5636d522021-03-17 14:52:18 -0500116 std::cerr << "Malformed threshold on configuration interface "
117 << item.first << "\n";
James Feist6714a252018-09-10 15:26:18 -0700118 return false;
119 }
Rashmica Guptad2208a92022-01-15 13:04:22 +1100120 unsigned int severity =
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530121 std::visit(VariantToUnsignedIntVisitor(), severityFind->second);
122
123 std::string directions =
124 std::visit(VariantToStringVisitor(), directionFind->second);
125
126 Level level = findThresholdLevel(severity, directions);
127 Direction direction = findThresholdDirection(severity, directions);
128
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +0530129 if ((level == Level::ERROR) || (direction == Direction::ERROR))
James Feist6714a252018-09-10 15:26:18 -0700130 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530131 continue;
James Feist6714a252018-09-10 15:26:18 -0700132 }
James Feist13f340b2019-03-07 16:36:11 -0800133 double val = std::visit(VariantToDoubleVisitor(), valueFind->second);
James Feist6714a252018-09-10 15:26:18 -0700134
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000135 thresholdVector.emplace_back(level, direction, val, hysteresis);
James Feist6714a252018-09-10 15:26:18 -0700136 }
137 return true;
138}
139
James Feistd8705872019-02-08 13:26:09 -0800140void persistThreshold(const std::string& path, const std::string& baseInterface,
141 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800142 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800143 size_t thresholdCount, const std::string& labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700144{
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500145 for (size_t ii = 0; ii < thresholdCount; ii++)
James Feist6714a252018-09-10 15:26:18 -0700146 {
147 std::string thresholdInterface =
148 baseInterface + ".Thresholds" + std::to_string(ii);
149 conn->async_method_call(
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800150 [&, path, threshold, thresholdInterface, labelMatch](
James Feistd8705872019-02-08 13:26:09 -0800151 const boost::system::error_code& ec,
152 const boost::container::flat_map<std::string, BasicVariantType>&
153 result) {
James Feist6714a252018-09-10 15:26:18 -0700154 if (ec)
155 {
156 return; // threshold not supported
157 }
158
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800159 if (!labelMatch.empty())
160 {
161 auto labelFind = result.find("Label");
162 if (labelFind == result.end())
163 {
164 std::cerr << "No label in threshold configuration\n";
165 return;
166 }
167 std::string label =
168 std::visit(VariantToStringVisitor(), labelFind->second);
169 if (label != labelMatch)
170 {
171 return;
172 }
173 }
174
James Feist6714a252018-09-10 15:26:18 -0700175 auto directionFind = result.find("Direction");
176 auto severityFind = result.find("Severity");
177 auto valueFind = result.find("Value");
178 if (valueFind == result.end() || severityFind == result.end() ||
179 directionFind == result.end())
180 {
181 std::cerr << "Malformed threshold in configuration\n";
182 return;
183 }
Rashmica Guptad2208a92022-01-15 13:04:22 +1100184 unsigned int severity = std::visit(
185 VariantToUnsignedIntVisitor(), severityFind->second);
James Feist6714a252018-09-10 15:26:18 -0700186
James Feist3eb82622019-02-08 13:10:22 -0800187 std::string dir =
188 std::visit(VariantToStringVisitor(), directionFind->second);
Rashmica Guptad2208a92022-01-15 13:04:22 +1100189 if (((findThresholdLevel(severity, dir)) != threshold.level) ||
190 ((findThresholdDirection(severity, dir)) !=
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530191 threshold.direction))
James Feist6714a252018-09-10 15:26:18 -0700192 {
193 return; // not the droid we're looking for
194 }
195
James Feist3eb82622019-02-08 13:10:22 -0800196 std::variant<double> value(threshold.value);
James Feist6714a252018-09-10 15:26:18 -0700197 conn->async_method_call(
James Feistd8705872019-02-08 13:26:09 -0800198 [](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700199 if (ec)
200 {
201 std::cerr << "Error setting threshold " << ec
202 << "\n";
203 }
204 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700205 entityManagerName, path, "org.freedesktop.DBus.Properties",
206 "Set", thresholdInterface, "Value", value);
James Feist6714a252018-09-10 15:26:18 -0700207 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700208 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700209 "GetAll", thresholdInterface);
210 }
211}
212
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800213void updateThresholds(Sensor* sensor)
214{
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800215 for (const auto& threshold : sensor->thresholds)
216 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530217 if (!findOrder(threshold.level, threshold.direction))
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800218 {
219 continue;
220 }
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530221 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
222 sensor->getThresholdInterface(threshold.level);
223
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800224 if (!interface)
225 {
226 continue;
227 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530228
229 std::string property =
230 sensor->propertyLevel(threshold.level, threshold.direction);
231 if (property.empty())
232 {
233 continue;
234 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800235 interface->set_property(property, threshold.value);
236 }
237}
238
Josh Lehan883fb3a2020-02-27 14:41:39 -0800239// Debugging counters
240static int cHiTrue = 0;
241static int cHiFalse = 0;
242static int cHiMidstate = 0;
243static int cLoTrue = 0;
244static int cLoFalse = 0;
245static int cLoMidstate = 0;
246static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700247static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800248
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700249struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700250{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700251 ChangeParam(Threshold whichThreshold, bool status, double value) :
252 threshold(whichThreshold), asserted(status), assertValue(value)
253 {}
254
255 Threshold threshold;
256 bool asserted;
257 double assertValue;
258};
259
260static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
261{
262 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700263 if (sensor->thresholds.empty())
264 {
James Feist46342ec2019-03-06 14:03:41 -0800265 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700266 }
James Feist46342ec2019-03-06 14:03:41 -0800267
James Feistd8705872019-02-08 13:26:09 -0800268 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700269 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800270 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
271 // if value is noisy while hovering very close to a threshold.
272 // When a threshold is crossed, indicate true immediately,
273 // but require more distance to be crossed the other direction,
274 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800275 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700276 {
James Feist551087a2019-12-09 11:17:12 -0800277 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700278 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700279 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700280 if (++cHiTrue < assertLogCount)
281 {
282 std::cerr << "Sensor " << sensor->name << " high threshold "
283 << threshold.value << " assert: value " << value
284 << " raw data " << sensor->rawValue << "\n";
285 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800286 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000287 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800288 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700289 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800290 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700291 }
Patrick Venture66235d42019-10-11 08:31:27 -0700292 else
James Feist251c7822018-09-12 12:54:15 -0700293 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800294 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700295 }
296 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800297 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700298 {
James Feist551087a2019-12-09 11:17:12 -0800299 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700300 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700301 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700302 if (++cLoTrue < assertLogCount)
303 {
304 std::cerr << "Sensor " << sensor->name << " low threshold "
305 << threshold.value << " assert: value "
306 << sensor->value << " raw data "
307 << sensor->rawValue << "\n";
308 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800309 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000310 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800311 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700312 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800313 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700314 }
Patrick Venture66235d42019-10-11 08:31:27 -0700315 else
James Feist251c7822018-09-12 12:54:15 -0700316 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800317 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700318 }
319 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800320 else
321 {
322 std::cerr << "Error determining threshold direction\n";
323 }
James Feist251c7822018-09-12 12:54:15 -0700324 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800325
Ed Tanous8a17c302021-09-02 15:07:11 -0700326 // Throttle debug output, so that it does not continuously spam
327 ++cDebugThrottle;
328 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800329 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700330 cDebugThrottle = 0;
331 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800332 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800333 std::cerr << "checkThresholds: High T=" << cHiTrue
334 << " F=" << cHiFalse << " M=" << cHiMidstate
335 << ", Low T=" << cLoTrue << " F=" << cLoFalse
336 << " M=" << cLoMidstate << "\n";
337 }
338 }
339
James Feist46342ec2019-03-06 14:03:41 -0800340 return thresholdChanges;
341}
342
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700343void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
344 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800345 double assertValue)
346{
347 struct TimerUsed timerUsed = {};
348 constexpr const size_t waitTime = 5;
349 TimerPair* pair = nullptr;
350
351 for (TimerPair& timer : timers)
352 {
353 if (!timer.first.used)
354 {
355 pair = &timer;
356 break;
357 }
358 }
359 if (pair == nullptr)
360 {
361 pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io));
362 }
363
364 pair->first.used = true;
365 pair->first.level = threshold.level;
366 pair->first.direction = threshold.direction;
367 pair->first.assert = assert;
368 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700369 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800370 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700371 auto sensorPtr = weakSensor.lock();
372 if (!sensorPtr)
373 {
374 return; // owner sensor has been destructed
375 }
376 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800377 pair->first.used = false;
378
379 if (ec == boost::asio::error::operation_aborted)
380 {
381 return; // we're being canceled
382 }
383 if (ec)
384 {
385 std::cerr << "timer error: " << ec.message() << "\n";
386 return;
387 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700388 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800389 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700390 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800391 threshold.direction, assert);
392 }
393 });
394}
395
James Feist46342ec2019-03-06 14:03:41 -0800396bool checkThresholds(Sensor* sensor)
397{
James Feist7b18b1e2019-05-14 13:42:09 -0700398 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700399 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
400 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800401 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700402 assertThresholds(sensor, change.assertValue, change.threshold.level,
403 change.threshold.direction, change.asserted);
404 if (change.threshold.level == thresholds::Level::CRITICAL &&
405 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800406 {
James Feist7b18b1e2019-05-14 13:42:09 -0700407 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800408 }
409 }
410
James Feistdc6c55f2018-10-31 12:53:20 -0700411 return status;
James Feist251c7822018-09-12 12:54:15 -0700412}
413
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700414void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
415 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800416{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700417 auto sensorPtr = weakSensor.lock();
418 if (!sensorPtr)
419 {
420 return; // sensor is destructed, should never be here
421 }
James Feist46342ec2019-03-06 14:03:41 -0800422
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700423 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700424 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
425 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800426 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700427 // When CPU is powered off, some volatges are expected to
428 // go below low thresholds. Filter these events with thresholdTimer.
429 // 1. always delay the assertion of low events to see if they are
430 // caused by power off event.
431 // 2. conditional delay the de-assertion of low events if there is
432 // an existing timer for assertion.
433 // 3. no delays for de-assert of low events if there is an existing
434 // de-assert for low event. This means 2nd de-assert would happen
435 // first and when timer expires for the previous one, no additional
436 // signal will be logged.
437 // 4. no delays for all high events.
438 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800439 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700440 if (change.asserted || thresholdTimer.hasActiveTimer(
441 change.threshold, !change.asserted))
442 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700443 thresholdTimer.startTimer(weakSensor, change.threshold,
444 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700445 continue;
446 }
James Feist46342ec2019-03-06 14:03:41 -0800447 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700448 assertThresholds(sensor, change.assertValue, change.threshold.level,
449 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800450 }
451}
452
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700453void assertThresholds(Sensor* sensor, double assertValue,
454 thresholds::Level level, thresholds::Direction direction,
455 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700456{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530457 if (!findOrder(level, direction))
James Feist251c7822018-09-12 12:54:15 -0700458 {
James Feist251c7822018-09-12 12:54:15 -0700459 return;
460 }
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530461
462 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
463 sensor->getThresholdInterface(level);
464
James Feist251c7822018-09-12 12:54:15 -0700465 if (!interface)
466 {
467 std::cout << "trying to set uninitialized interface\n";
468 return;
469 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700470
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530471 std::string property = sensor->propertyAlarm(level, direction);
472 if (property.empty())
473 {
474 std::cout << "Alarm property is empty \n";
475 return;
476 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700477 if (interface->set_property<bool, true>(property, assert))
478 {
479 try
480 {
481 // msg.get_path() is interface->get_object_path()
482 sdbusplus::message::message msg =
483 interface->new_signal("ThresholdAsserted");
484
485 msg.append(sensor->name, interface->get_interface_name(), property,
486 assert, assertValue);
487 msg.signal_send();
488 }
489 catch (const sdbusplus::exception::exception& e)
490 {
491 std::cerr
492 << "Failed to send thresholdAsserted signal with assertValue\n";
493 }
494 }
James Feist251c7822018-09-12 12:54:15 -0700495}
496
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700497bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800498 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700499 const std::string& inputPath, const double& scaleFactor,
500 const double& offset)
James Feist6714a252018-09-10 15:26:18 -0700501{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200502 const boost::container::flat_map<
503 std::string, std::vector<std::tuple<const char*, thresholds::Level,
504 thresholds::Direction, double>>>
505 map = {
506 {"average",
507 {
508 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
509 0.0),
510 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
511 0.0),
512 }},
513 {"input",
514 {
515 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
516 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
517 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
518 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
519 offset),
520 }},
521 };
522
523 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700524 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200525 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200526 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700527 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200528 for (const auto& t : map.at(item))
529 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200530 auto& [suffix, level, direction, offset] = t;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200531 auto attrPath =
532 boost::replace_all_copy(inputPath, item, suffix);
533 if (auto val = readFile(attrPath, scaleFactor))
534 {
535 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700536 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200537 {
538 std::cout << "Threshold: " << attrPath << ": " << *val
539 << "\n";
540 }
541 thresholdVector.emplace_back(level, direction, *val);
542 }
543 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700544 }
James Feist6714a252018-09-10 15:26:18 -0700545 }
James Feist6714a252018-09-10 15:26:18 -0700546 return true;
547}
548
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530549std::string getInterface(const Level thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700550{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530551 std::string level;
552 switch (thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700553 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530554 case Level::WARNING:
555 level = "Warning";
556 break;
557 case Level::CRITICAL:
558 level = "Critical";
559 break;
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +0530560 case Level::SOFTSHUTDOWN:
Rashmica Gupta62fd8072022-01-20 10:11:44 +1100561 level = "SoftShutdown";
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +0530562 break;
563 case Level::HARDSHUTDOWN:
564 level = "HardShutdown";
565 break;
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530566 case Level::ERROR:
567 level = "Error";
568 break;
James Feist6714a252018-09-10 15:26:18 -0700569 }
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530570 std::string interface = "xyz.openbmc_project.Sensor.Threshold." + level;
571 return interface;
James Feist6714a252018-09-10 15:26:18 -0700572}
573} // namespace thresholds