blob: 7805dff7147939359a2833caf607d53b8f2fb5ca [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(
Zev Weissafd15042022-07-18 12:28:40 -0700139 [&, path, threshold, thresholdInterface,
140 labelMatch](const boost::system::error_code& ec,
141 const SensorBaseConfigMap& result) {
Ed Tanousbb679322022-05-16 16:10:00 -0700142 if (ec)
143 {
144 return; // threshold not supported
145 }
James Feist6714a252018-09-10 15:26:18 -0700146
Ed Tanousbb679322022-05-16 16:10:00 -0700147 if (!labelMatch.empty())
148 {
149 auto labelFind = result.find("Label");
150 if (labelFind == result.end())
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800151 {
Ed Tanousbb679322022-05-16 16:10:00 -0700152 std::cerr << "No label in threshold configuration\n";
James Feist6714a252018-09-10 15:26:18 -0700153 return;
154 }
Ed Tanousbb679322022-05-16 16:10:00 -0700155 std::string label =
156 std::visit(VariantToStringVisitor(), labelFind->second);
157 if (label != labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700158 {
Ed Tanousbb679322022-05-16 16:10:00 -0700159 return;
James Feist6714a252018-09-10 15:26:18 -0700160 }
Ed Tanousbb679322022-05-16 16:10:00 -0700161 }
James Feist6714a252018-09-10 15:26:18 -0700162
Ed Tanousbb679322022-05-16 16:10:00 -0700163 auto directionFind = result.find("Direction");
164 auto severityFind = result.find("Severity");
165 auto valueFind = result.find("Value");
166 if (valueFind == result.end() || severityFind == result.end() ||
167 directionFind == result.end())
168 {
169 std::cerr << "Malformed threshold in configuration\n";
170 return;
171 }
172 unsigned int severity =
173 std::visit(VariantToUnsignedIntVisitor(), severityFind->second);
174
175 std::string dir =
176 std::visit(VariantToStringVisitor(), directionFind->second);
177 if ((findThresholdLevel(severity) != threshold.level) ||
178 (findThresholdDirection(dir) != threshold.direction))
179 {
180 return; // not the droid we're looking for
181 }
182
183 std::variant<double> value(threshold.value);
184 conn->async_method_call(
185 [](const boost::system::error_code& ec) {
186 if (ec)
187 {
188 std::cerr << "Error setting threshold " << ec << "\n";
189 }
190 },
191 entityManagerName, path, "org.freedesktop.DBus.Properties",
192 "Set", thresholdInterface, "Value", value);
James Feist6714a252018-09-10 15:26:18 -0700193 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700195 "GetAll", thresholdInterface);
196 }
197}
198
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800199void updateThresholds(Sensor* sensor)
200{
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800201 for (const auto& threshold : sensor->thresholds)
202 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530203 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
204 sensor->getThresholdInterface(threshold.level);
205
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800206 if (!interface)
207 {
208 continue;
209 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530210
211 std::string property =
Ed Tanous2049bd22022-07-09 07:20:26 -0700212 Sensor::propertyLevel(threshold.level, threshold.direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530213 if (property.empty())
214 {
215 continue;
216 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800217 interface->set_property(property, threshold.value);
218 }
219}
220
Josh Lehan883fb3a2020-02-27 14:41:39 -0800221// Debugging counters
222static int cHiTrue = 0;
223static int cHiFalse = 0;
224static int cHiMidstate = 0;
225static int cLoTrue = 0;
226static int cLoFalse = 0;
227static int cLoMidstate = 0;
228static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700229static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800230
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700231struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700232{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700233 ChangeParam(Threshold whichThreshold, bool status, double value) :
234 threshold(whichThreshold), asserted(status), assertValue(value)
235 {}
236
237 Threshold threshold;
238 bool asserted;
239 double assertValue;
240};
241
242static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
243{
244 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700245 if (sensor->thresholds.empty())
246 {
James Feist46342ec2019-03-06 14:03:41 -0800247 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700248 }
James Feist46342ec2019-03-06 14:03:41 -0800249
James Feistd8705872019-02-08 13:26:09 -0800250 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700251 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800252 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
253 // if value is noisy while hovering very close to a threshold.
254 // When a threshold is crossed, indicate true immediately,
255 // but require more distance to be crossed the other direction,
256 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800257 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700258 {
James Feist551087a2019-12-09 11:17:12 -0800259 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700260 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700261 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700262 if (++cHiTrue < assertLogCount)
263 {
264 std::cerr << "Sensor " << sensor->name << " high threshold "
265 << threshold.value << " assert: value " << value
266 << " raw data " << sensor->rawValue << "\n";
267 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800268 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000269 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800270 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700271 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800272 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700273 }
Patrick Venture66235d42019-10-11 08:31:27 -0700274 else
James Feist251c7822018-09-12 12:54:15 -0700275 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800276 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700277 }
278 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800279 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700280 {
James Feist551087a2019-12-09 11:17:12 -0800281 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700282 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700283 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700284 if (++cLoTrue < assertLogCount)
285 {
286 std::cerr << "Sensor " << sensor->name << " low threshold "
287 << threshold.value << " assert: value "
288 << sensor->value << " raw data "
289 << sensor->rawValue << "\n";
290 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800291 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000292 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800293 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700294 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800295 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700296 }
Patrick Venture66235d42019-10-11 08:31:27 -0700297 else
James Feist251c7822018-09-12 12:54:15 -0700298 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800299 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700300 }
301 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800302 else
303 {
304 std::cerr << "Error determining threshold direction\n";
305 }
James Feist251c7822018-09-12 12:54:15 -0700306 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800307
Ed Tanous8a17c302021-09-02 15:07:11 -0700308 // Throttle debug output, so that it does not continuously spam
309 ++cDebugThrottle;
310 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800311 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700312 cDebugThrottle = 0;
313 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800314 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800315 std::cerr << "checkThresholds: High T=" << cHiTrue
316 << " F=" << cHiFalse << " M=" << cHiMidstate
317 << ", Low T=" << cLoTrue << " F=" << cLoFalse
318 << " M=" << cLoMidstate << "\n";
319 }
320 }
321
James Feist46342ec2019-03-06 14:03:41 -0800322 return thresholdChanges;
323}
324
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700325void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
326 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800327 double assertValue)
328{
329 struct TimerUsed timerUsed = {};
330 constexpr const size_t waitTime = 5;
331 TimerPair* pair = nullptr;
332
333 for (TimerPair& timer : timers)
334 {
335 if (!timer.first.used)
336 {
337 pair = &timer;
338 break;
339 }
340 }
341 if (pair == nullptr)
342 {
343 pair = &timers.emplace_back(timerUsed, boost::asio::deadline_timer(io));
344 }
345
346 pair->first.used = true;
347 pair->first.level = threshold.level;
348 pair->first.direction = threshold.direction;
349 pair->first.assert = assert;
350 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700351 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800352 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700353 auto sensorPtr = weakSensor.lock();
354 if (!sensorPtr)
355 {
356 return; // owner sensor has been destructed
357 }
358 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800359 pair->first.used = false;
360
361 if (ec == boost::asio::error::operation_aborted)
362 {
363 return; // we're being canceled
364 }
365 if (ec)
366 {
367 std::cerr << "timer error: " << ec.message() << "\n";
368 return;
369 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700370 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800371 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700372 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800373 threshold.direction, assert);
374 }
375 });
376}
377
James Feist46342ec2019-03-06 14:03:41 -0800378bool checkThresholds(Sensor* sensor)
379{
James Feist7b18b1e2019-05-14 13:42:09 -0700380 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700381 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
382 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800383 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700384 assertThresholds(sensor, change.assertValue, change.threshold.level,
385 change.threshold.direction, change.asserted);
386 if (change.threshold.level == thresholds::Level::CRITICAL &&
387 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800388 {
James Feist7b18b1e2019-05-14 13:42:09 -0700389 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800390 }
391 }
392
James Feistdc6c55f2018-10-31 12:53:20 -0700393 return status;
James Feist251c7822018-09-12 12:54:15 -0700394}
395
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700396void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
397 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800398{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700399 auto sensorPtr = weakSensor.lock();
400 if (!sensorPtr)
401 {
402 return; // sensor is destructed, should never be here
403 }
James Feist46342ec2019-03-06 14:03:41 -0800404
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700405 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700406 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
407 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800408 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700409 // When CPU is powered off, some volatges are expected to
410 // go below low thresholds. Filter these events with thresholdTimer.
411 // 1. always delay the assertion of low events to see if they are
412 // caused by power off event.
413 // 2. conditional delay the de-assertion of low events if there is
414 // an existing timer for assertion.
415 // 3. no delays for de-assert of low events if there is an existing
416 // de-assert for low event. This means 2nd de-assert would happen
417 // first and when timer expires for the previous one, no additional
418 // signal will be logged.
419 // 4. no delays for all high events.
420 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800421 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700422 if (change.asserted || thresholdTimer.hasActiveTimer(
423 change.threshold, !change.asserted))
424 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700425 thresholdTimer.startTimer(weakSensor, change.threshold,
426 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700427 continue;
428 }
James Feist46342ec2019-03-06 14:03:41 -0800429 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700430 assertThresholds(sensor, change.assertValue, change.threshold.level,
431 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800432 }
433}
434
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700435void assertThresholds(Sensor* sensor, double assertValue,
436 thresholds::Level level, thresholds::Direction direction,
437 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700438{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530439 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
440 sensor->getThresholdInterface(level);
441
James Feist251c7822018-09-12 12:54:15 -0700442 if (!interface)
443 {
444 std::cout << "trying to set uninitialized interface\n";
445 return;
446 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700447
Ed Tanous2049bd22022-07-09 07:20:26 -0700448 std::string property = Sensor::propertyAlarm(level, direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530449 if (property.empty())
450 {
451 std::cout << "Alarm property is empty \n";
452 return;
453 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700454 if (interface->set_property<bool, true>(property, assert))
455 {
456 try
457 {
458 // msg.get_path() is interface->get_object_path()
Patrick Williams92f8f512022-07-22 19:26:55 -0500459 sdbusplus::message_t msg =
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700460 interface->new_signal("ThresholdAsserted");
461
462 msg.append(sensor->name, interface->get_interface_name(), property,
463 assert, assertValue);
464 msg.signal_send();
465 }
Patrick Williams92f8f512022-07-22 19:26:55 -0500466 catch (const sdbusplus::exception_t& e)
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700467 {
468 std::cerr
469 << "Failed to send thresholdAsserted signal with assertValue\n";
470 }
471 }
James Feist251c7822018-09-12 12:54:15 -0700472}
473
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700474bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800475 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700476 const std::string& inputPath, const double& scaleFactor,
477 const double& offset)
James Feist6714a252018-09-10 15:26:18 -0700478{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200479 const boost::container::flat_map<
480 std::string, std::vector<std::tuple<const char*, thresholds::Level,
481 thresholds::Direction, double>>>
482 map = {
483 {"average",
484 {
485 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
486 0.0),
487 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
488 0.0),
489 }},
490 {"input",
491 {
492 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
493 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
494 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
495 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
496 offset),
497 }},
498 };
499
500 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700501 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200502 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200503 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700504 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200505 for (const auto& t : map.at(item))
506 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700507 const auto& [suffix, level, direction, offset] = t;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200508 auto attrPath =
509 boost::replace_all_copy(inputPath, item, suffix);
510 if (auto val = readFile(attrPath, scaleFactor))
511 {
512 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700513 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200514 {
515 std::cout << "Threshold: " << attrPath << ": " << *val
516 << "\n";
517 }
518 thresholdVector.emplace_back(level, direction, *val);
519 }
520 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700521 }
James Feist6714a252018-09-10 15:26:18 -0700522 }
James Feist6714a252018-09-10 15:26:18 -0700523 return true;
524}
525
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530526std::string getInterface(const Level thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700527{
Ed Tanousc8fed202022-01-12 14:24:45 -0800528 for (const ThresholdDefinition& thresh : thresProp)
James Feist6714a252018-09-10 15:26:18 -0700529 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800530 if (thresh.level == thresholdLevel)
531 {
532 return std::string("xyz.openbmc_project.Sensor.Threshold.") +
533 thresh.levelName;
534 }
James Feist6714a252018-09-10 15:26:18 -0700535 }
Ed Tanousc8fed202022-01-12 14:24:45 -0800536 return "";
James Feist6714a252018-09-10 15:26:18 -0700537}
538} // namespace thresholds