blob: 42c634fa54bcfc1c1affd90d06d6d18b2a568f1c [file] [log] [blame]
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10301#include "Thresholds.hpp"
2
Ed Tanouseacbfdd2024-04-04 12:00:24 -07003#include "Utils.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10304#include "VariantVisitors.hpp"
5#include "sensor.hpp"
6
James Feist6714a252018-09-10 15:26:18 -07007#include <boost/algorithm/string/replace.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -07008#include <boost/asio/error.hpp>
9#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070010#include <boost/container/flat_map.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070011#include <sdbusplus/asio/connection.hpp>
12#include <sdbusplus/asio/object_server.hpp>
13#include <sdbusplus/exception.hpp>
14#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070015
16#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070017#include <chrono>
18#include <cstddef>
19#include <cstdint>
James Feist6714a252018-09-10 15:26:18 -070020#include <iostream>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070021#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <string>
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020024#include <tuple>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <utility>
26#include <variant>
27#include <vector>
James Feist6714a252018-09-10 15:26:18 -070028
Ed Tanous8a57ec02020-10-09 12:46:52 -070029static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070030namespace thresholds
31{
Ed Tanousc8fed202022-01-12 14:24:45 -080032Level findThresholdLevel(uint8_t sev)
James Feist6714a252018-09-10 15:26:18 -070033{
Ed Tanousc8fed202022-01-12 14:24:45 -080034 for (const ThresholdDefinition& prop : thresProp)
James Feist6714a252018-09-10 15:26:18 -070035 {
Ed Tanousc8fed202022-01-12 14:24:45 -080036 if (prop.sevOrder == sev)
James Feist6714a252018-09-10 15:26:18 -070037 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053038 return prop.level;
James Feist6714a252018-09-10 15:26:18 -070039 }
40 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053041 return Level::ERROR;
James Feist6714a252018-09-10 15:26:18 -070042}
43
Ed Tanousc8fed202022-01-12 14:24:45 -080044Direction findThresholdDirection(const std::string& direct)
James Feist6714a252018-09-10 15:26:18 -070045{
Ed Tanousc8fed202022-01-12 14:24:45 -080046 if (direct == "greater than")
James Feist6714a252018-09-10 15:26:18 -070047 {
Ed Tanousc8fed202022-01-12 14:24:45 -080048 return Direction::HIGH;
49 }
50 if (direct == "less than")
51 {
52 return Direction::LOW;
James Feist6714a252018-09-10 15:26:18 -070053 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053054 return Direction::ERROR;
James Feist6714a252018-09-10 15:26:18 -070055}
56
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070057bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080058 const SensorData& sensorData,
59 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -050060 const std::string* matchLabel, const int* sensorIndex)
James Feist6714a252018-09-10 15:26:18 -070061{
Zev Weissf783c692022-08-12 18:21:02 -070062 for (const auto& [intf, cfg] : sensorData)
James Feist6714a252018-09-10 15:26:18 -070063 {
Zev Weissf783c692022-08-12 18:21:02 -070064 if (intf.find("Thresholds") == std::string::npos)
James Feist6714a252018-09-10 15:26:18 -070065 {
66 continue;
67 }
68 if (matchLabel != nullptr)
69 {
Zev Weissf783c692022-08-12 18:21:02 -070070 auto labelFind = cfg.find("Label");
71 if (labelFind == cfg.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -070072 {
James Feist6714a252018-09-10 15:26:18 -070073 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070074 }
James Feist3eb82622019-02-08 13:10:22 -080075 if (std::visit(VariantToStringVisitor(), labelFind->second) !=
76 *matchLabel)
Ed Tanous8a57ec02020-10-09 12:46:52 -070077 {
James Feist6714a252018-09-10 15:26:18 -070078 continue;
Ed Tanous8a57ec02020-10-09 12:46:52 -070079 }
James Feist6714a252018-09-10 15:26:18 -070080 }
Matt Spinler5636d522021-03-17 14:52:18 -050081
82 if (sensorIndex != nullptr)
83 {
Zev Weissf783c692022-08-12 18:21:02 -070084 auto indexFind = cfg.find("Index");
Matt Spinler5636d522021-03-17 14:52:18 -050085
86 // If we're checking for index 1, a missing Index is OK.
Zev Weissf783c692022-08-12 18:21:02 -070087 if ((indexFind == cfg.end()) && (*sensorIndex != 1))
Matt Spinler5636d522021-03-17 14:52:18 -050088 {
89 continue;
90 }
91
Zev Weissf783c692022-08-12 18:21:02 -070092 if ((indexFind != cfg.end()) &&
Matt Spinler5636d522021-03-17 14:52:18 -050093 (std::visit(VariantToIntVisitor(), indexFind->second) !=
94 *sensorIndex))
95 {
96 continue;
97 }
98 }
99
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000100 double hysteresis = std::numeric_limits<double>::quiet_NaN();
Zev Weissf783c692022-08-12 18:21:02 -0700101 auto hysteresisFind = cfg.find("Hysteresis");
102 if (hysteresisFind != cfg.end())
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000103 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500104 hysteresis = std::visit(VariantToDoubleVisitor(),
105 hysteresisFind->second);
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000106 }
107
Zev Weissf783c692022-08-12 18:21:02 -0700108 auto directionFind = cfg.find("Direction");
109 auto severityFind = cfg.find("Severity");
110 auto valueFind = cfg.find("Value");
111 if (valueFind == cfg.end() || severityFind == cfg.end() ||
112 directionFind == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700113 {
Matt Spinler5636d522021-03-17 14:52:18 -0500114 std::cerr << "Malformed threshold on configuration interface "
Zev Weissf783c692022-08-12 18:21:02 -0700115 << intf << "\n";
James Feist6714a252018-09-10 15:26:18 -0700116 return false;
117 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500118 unsigned int severity = std::visit(VariantToUnsignedIntVisitor(),
119 severityFind->second);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530120
Patrick Williams779c96a2023-05-10 07:50:42 -0500121 std::string directions = std::visit(VariantToStringVisitor(),
122 directionFind->second);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530123
Ed Tanousc8fed202022-01-12 14:24:45 -0800124 Level level = findThresholdLevel(severity);
125 Direction direction = findThresholdDirection(directions);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530126
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +0530127 if ((level == Level::ERROR) || (direction == Direction::ERROR))
James Feist6714a252018-09-10 15:26:18 -0700128 {
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530129 continue;
James Feist6714a252018-09-10 15:26:18 -0700130 }
James Feist13f340b2019-03-07 16:36:11 -0800131 double val = std::visit(VariantToDoubleVisitor(), valueFind->second);
James Feist6714a252018-09-10 15:26:18 -0700132
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000133 thresholdVector.emplace_back(level, direction, val, hysteresis);
James Feist6714a252018-09-10 15:26:18 -0700134 }
135 return true;
136}
137
James Feistd8705872019-02-08 13:26:09 -0800138void persistThreshold(const std::string& path, const std::string& baseInterface,
139 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800140 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800141 size_t thresholdCount, const std::string& labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700142{
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500143 for (size_t ii = 0; ii < thresholdCount; ii++)
James Feist6714a252018-09-10 15:26:18 -0700144 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500145 std::string thresholdInterface = baseInterface + ".Thresholds" +
146 std::to_string(ii);
James Feist6714a252018-09-10 15:26:18 -0700147 conn->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700148 [&, path, threshold, thresholdInterface,
149 labelMatch](const boost::system::error_code& ec,
150 const SensorBaseConfigMap& result) {
Ed Tanousbb679322022-05-16 16:10:00 -0700151 if (ec)
152 {
153 return; // threshold not supported
154 }
James Feist6714a252018-09-10 15:26:18 -0700155
Ed Tanousbb679322022-05-16 16:10:00 -0700156 if (!labelMatch.empty())
157 {
158 auto labelFind = result.find("Label");
159 if (labelFind == result.end())
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800160 {
Ed Tanousbb679322022-05-16 16:10:00 -0700161 std::cerr << "No label in threshold configuration\n";
James Feist6714a252018-09-10 15:26:18 -0700162 return;
163 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500164 std::string label = std::visit(VariantToStringVisitor(),
165 labelFind->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700166 if (label != labelMatch)
James Feist6714a252018-09-10 15:26:18 -0700167 {
Ed Tanousbb679322022-05-16 16:10:00 -0700168 return;
James Feist6714a252018-09-10 15:26:18 -0700169 }
Ed Tanousbb679322022-05-16 16:10:00 -0700170 }
James Feist6714a252018-09-10 15:26:18 -0700171
Ed Tanousbb679322022-05-16 16:10:00 -0700172 auto directionFind = result.find("Direction");
173 auto severityFind = result.find("Severity");
174 auto valueFind = result.find("Value");
175 if (valueFind == result.end() || severityFind == result.end() ||
176 directionFind == result.end())
177 {
178 std::cerr << "Malformed threshold in configuration\n";
179 return;
180 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500181 unsigned int severity = std::visit(VariantToUnsignedIntVisitor(),
182 severityFind->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700183
Patrick Williams779c96a2023-05-10 07:50:42 -0500184 std::string dir = std::visit(VariantToStringVisitor(),
185 directionFind->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700186 if ((findThresholdLevel(severity) != threshold.level) ||
187 (findThresholdDirection(dir) != threshold.direction))
188 {
189 return; // not the droid we're looking for
190 }
191
192 std::variant<double> value(threshold.value);
193 conn->async_method_call(
194 [](const boost::system::error_code& ec) {
195 if (ec)
196 {
197 std::cerr << "Error setting threshold " << ec << "\n";
198 }
Patrick Williams597e8422023-10-20 11:19:01 -0500199 },
Ed Tanousbb679322022-05-16 16:10:00 -0700200 entityManagerName, path, "org.freedesktop.DBus.Properties",
201 "Set", thresholdInterface, "Value", value);
Patrick Williams597e8422023-10-20 11:19:01 -0500202 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700203 entityManagerName, path, "org.freedesktop.DBus.Properties",
James Feist6714a252018-09-10 15:26:18 -0700204 "GetAll", thresholdInterface);
205 }
206}
207
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800208void updateThresholds(Sensor* sensor)
209{
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800210 for (const auto& threshold : sensor->thresholds)
211 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530212 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
213 sensor->getThresholdInterface(threshold.level);
214
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800215 if (!interface)
216 {
217 continue;
218 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530219
Patrick Williams779c96a2023-05-10 07:50:42 -0500220 std::string property = Sensor::propertyLevel(threshold.level,
221 threshold.direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530222 if (property.empty())
223 {
224 continue;
225 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800226 interface->set_property(property, threshold.value);
227 }
228}
229
Josh Lehan883fb3a2020-02-27 14:41:39 -0800230// Debugging counters
231static int cHiTrue = 0;
232static int cHiFalse = 0;
233static int cHiMidstate = 0;
234static int cLoTrue = 0;
235static int cLoFalse = 0;
236static int cLoMidstate = 0;
237static int cDebugThrottle = 0;
Zhikui Rend3da1282020-09-11 17:02:01 -0700238static constexpr int assertLogCount = 10;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800239
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700240struct ChangeParam
James Feist251c7822018-09-12 12:54:15 -0700241{
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700242 ChangeParam(Threshold whichThreshold, bool status, double value) :
243 threshold(whichThreshold), asserted(status), assertValue(value)
244 {}
245
246 Threshold threshold;
247 bool asserted;
248 double assertValue;
249};
250
251static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value)
252{
253 std::vector<ChangeParam> thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700254 if (sensor->thresholds.empty())
255 {
James Feist46342ec2019-03-06 14:03:41 -0800256 return thresholdChanges;
James Feist251c7822018-09-12 12:54:15 -0700257 }
James Feist46342ec2019-03-06 14:03:41 -0800258
James Feistd8705872019-02-08 13:26:09 -0800259 for (auto& threshold : sensor->thresholds)
James Feist251c7822018-09-12 12:54:15 -0700260 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800261 // Use "Schmitt trigger" logic to avoid threshold trigger spam,
262 // if value is noisy while hovering very close to a threshold.
263 // When a threshold is crossed, indicate true immediately,
264 // but require more distance to be crossed the other direction,
265 // before resetting the indicator back to false.
James Feist46342ec2019-03-06 14:03:41 -0800266 if (threshold.direction == thresholds::Direction::HIGH)
James Feistdc6c55f2018-10-31 12:53:20 -0700267 {
James Feist551087a2019-12-09 11:17:12 -0800268 if (value >= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700269 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700270 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700271 if (++cHiTrue < assertLogCount)
272 {
273 std::cerr << "Sensor " << sensor->name << " high threshold "
274 << threshold.value << " assert: value " << value
275 << " raw data " << sensor->rawValue << "\n";
276 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800277 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000278 else if (value < (threshold.value - threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800279 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700280 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800281 ++cHiFalse;
James Feist251c7822018-09-12 12:54:15 -0700282 }
Patrick Venture66235d42019-10-11 08:31:27 -0700283 else
James Feist251c7822018-09-12 12:54:15 -0700284 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800285 ++cHiMidstate;
James Feist251c7822018-09-12 12:54:15 -0700286 }
287 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800288 else if (threshold.direction == thresholds::Direction::LOW)
James Feist251c7822018-09-12 12:54:15 -0700289 {
James Feist551087a2019-12-09 11:17:12 -0800290 if (value <= threshold.value)
James Feist251c7822018-09-12 12:54:15 -0700291 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700292 thresholdChanges.emplace_back(threshold, true, value);
Zhikui Rend3da1282020-09-11 17:02:01 -0700293 if (++cLoTrue < assertLogCount)
294 {
295 std::cerr << "Sensor " << sensor->name << " low threshold "
296 << threshold.value << " assert: value "
297 << sensor->value << " raw data "
298 << sensor->rawValue << "\n";
299 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800300 }
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000301 else if (value > (threshold.value + threshold.hysteresis))
Josh Lehan883fb3a2020-02-27 14:41:39 -0800302 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700303 thresholdChanges.emplace_back(threshold, false, value);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800304 ++cLoFalse;
James Feist251c7822018-09-12 12:54:15 -0700305 }
Patrick Venture66235d42019-10-11 08:31:27 -0700306 else
James Feist251c7822018-09-12 12:54:15 -0700307 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800308 ++cLoMidstate;
James Feist251c7822018-09-12 12:54:15 -0700309 }
310 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800311 else
312 {
313 std::cerr << "Error determining threshold direction\n";
314 }
James Feist251c7822018-09-12 12:54:15 -0700315 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800316
Ed Tanous8a17c302021-09-02 15:07:11 -0700317 // Throttle debug output, so that it does not continuously spam
318 ++cDebugThrottle;
319 if (cDebugThrottle >= 1000)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800320 {
Ed Tanous8a17c302021-09-02 15:07:11 -0700321 cDebugThrottle = 0;
322 if constexpr (debug)
Josh Lehan883fb3a2020-02-27 14:41:39 -0800323 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800324 std::cerr << "checkThresholds: High T=" << cHiTrue
325 << " F=" << cHiFalse << " M=" << cHiMidstate
326 << ", Low T=" << cLoTrue << " F=" << cLoFalse
327 << " M=" << cLoMidstate << "\n";
328 }
329 }
330
James Feist46342ec2019-03-06 14:03:41 -0800331 return thresholdChanges;
332}
333
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700334void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor,
335 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800336 double assertValue)
337{
338 struct TimerUsed timerUsed = {};
339 constexpr const size_t waitTime = 5;
340 TimerPair* pair = nullptr;
341
342 for (TimerPair& timer : timers)
343 {
344 if (!timer.first.used)
345 {
346 pair = &timer;
347 break;
348 }
349 }
350 if (pair == nullptr)
351 {
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700352 pair = &timers.emplace_back(timerUsed, boost::asio::steady_timer(io));
Jeff Lind9cd7042020-11-20 15:49:28 +0800353 }
354
355 pair->first.used = true;
356 pair->first.level = threshold.level;
357 pair->first.direction = threshold.direction;
358 pair->first.assert = assert;
Ed Tanous83db50c2023-03-01 10:20:24 -0800359 pair->second.expires_after(std::chrono::seconds(waitTime));
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700360 pair->second.async_wait([weakSensor, pair, threshold, assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800361 assertValue](boost::system::error_code ec) {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700362 auto sensorPtr = weakSensor.lock();
363 if (!sensorPtr)
364 {
365 return; // owner sensor has been destructed
366 }
367 // pair is valid as long as sensor is valid
Jeff Lind9cd7042020-11-20 15:49:28 +0800368 pair->first.used = false;
369
370 if (ec == boost::asio::error::operation_aborted)
371 {
372 return; // we're being canceled
373 }
374 if (ec)
375 {
376 std::cerr << "timer error: " << ec.message() << "\n";
377 return;
378 }
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700379 if (sensorPtr->readingStateGood())
Jeff Lind9cd7042020-11-20 15:49:28 +0800380 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700381 assertThresholds(sensorPtr.get(), assertValue, threshold.level,
Jeff Lind9cd7042020-11-20 15:49:28 +0800382 threshold.direction, assert);
383 }
384 });
385}
386
James Feist46342ec2019-03-06 14:03:41 -0800387bool checkThresholds(Sensor* sensor)
388{
James Feist7b18b1e2019-05-14 13:42:09 -0700389 bool status = true;
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700390 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
391 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800392 {
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700393 assertThresholds(sensor, change.assertValue, change.threshold.level,
394 change.threshold.direction, change.asserted);
395 if (change.threshold.level == thresholds::Level::CRITICAL &&
396 change.asserted)
James Feist46342ec2019-03-06 14:03:41 -0800397 {
James Feist7b18b1e2019-05-14 13:42:09 -0700398 status = false;
James Feist46342ec2019-03-06 14:03:41 -0800399 }
400 }
401
James Feistdc6c55f2018-10-31 12:53:20 -0700402 return status;
James Feist251c7822018-09-12 12:54:15 -0700403}
404
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700405void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
406 ThresholdTimer& thresholdTimer)
James Feist46342ec2019-03-06 14:03:41 -0800407{
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700408 auto sensorPtr = weakSensor.lock();
409 if (!sensorPtr)
410 {
411 return; // sensor is destructed, should never be here
412 }
James Feist46342ec2019-03-06 14:03:41 -0800413
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700414 Sensor* sensor = sensorPtr.get();
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700415 std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value);
416 for (const auto& change : changes)
James Feist46342ec2019-03-06 14:03:41 -0800417 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700418 // When CPU is powered off, some volatges are expected to
419 // go below low thresholds. Filter these events with thresholdTimer.
420 // 1. always delay the assertion of low events to see if they are
421 // caused by power off event.
422 // 2. conditional delay the de-assertion of low events if there is
423 // an existing timer for assertion.
424 // 3. no delays for de-assert of low events if there is an existing
425 // de-assert for low event. This means 2nd de-assert would happen
426 // first and when timer expires for the previous one, no additional
427 // signal will be logged.
428 // 4. no delays for all high events.
429 if (change.threshold.direction == thresholds::Direction::LOW)
James Feist46342ec2019-03-06 14:03:41 -0800430 {
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700431 if (change.asserted || thresholdTimer.hasActiveTimer(
432 change.threshold, !change.asserted))
433 {
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700434 thresholdTimer.startTimer(weakSensor, change.threshold,
435 change.asserted, change.assertValue);
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700436 continue;
437 }
James Feist46342ec2019-03-06 14:03:41 -0800438 }
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700439 assertThresholds(sensor, change.assertValue, change.threshold.level,
440 change.threshold.direction, change.asserted);
James Feist46342ec2019-03-06 14:03:41 -0800441 }
442}
443
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700444void assertThresholds(Sensor* sensor, double assertValue,
445 thresholds::Level level, thresholds::Direction direction,
446 bool assert)
James Feist251c7822018-09-12 12:54:15 -0700447{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530448 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
449 sensor->getThresholdInterface(level);
450
James Feist251c7822018-09-12 12:54:15 -0700451 if (!interface)
452 {
453 std::cout << "trying to set uninitialized interface\n";
454 return;
455 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700456
Ed Tanous2049bd22022-07-09 07:20:26 -0700457 std::string property = Sensor::propertyAlarm(level, direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530458 if (property.empty())
459 {
460 std::cout << "Alarm property is empty \n";
461 return;
462 }
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700463 if (interface->set_property<bool, true>(property, assert))
464 {
465 try
466 {
467 // msg.get_path() is interface->get_object_path()
Patrick Williams92f8f512022-07-22 19:26:55 -0500468 sdbusplus::message_t msg =
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700469 interface->new_signal("ThresholdAsserted");
470
471 msg.append(sensor->name, interface->get_interface_name(), property,
472 assert, assertValue);
473 msg.signal_send();
474 }
Patrick Williams92f8f512022-07-22 19:26:55 -0500475 catch (const sdbusplus::exception_t& e)
Zhikui Ren59b8b9e2020-06-26 18:34:22 -0700476 {
477 std::cerr
478 << "Failed to send thresholdAsserted signal with assertValue\n";
479 }
480 }
James Feist251c7822018-09-12 12:54:15 -0700481}
482
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700483bool parseThresholdsFromAttr(
James Feistd8705872019-02-08 13:26:09 -0800484 std::vector<thresholds::Threshold>& thresholdVector,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700485 const std::string& inputPath, const double& scaleFactor,
Chris Sidesa3279232023-04-24 16:08:13 -0500486 const double& offset, const double& hysteresis)
James Feist6714a252018-09-10 15:26:18 -0700487{
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200488 const boost::container::flat_map<
489 std::string, std::vector<std::tuple<const char*, thresholds::Level,
490 thresholds::Direction, double>>>
491 map = {
492 {"average",
493 {
494 std::make_tuple("average_min", Level::WARNING, Direction::LOW,
495 0.0),
496 std::make_tuple("average_max", Level::WARNING, Direction::HIGH,
497 0.0),
498 }},
499 {"input",
500 {
501 std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0),
502 std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0),
503 std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0),
504 std::make_tuple("crit", Level::CRITICAL, Direction::HIGH,
505 offset),
506 }},
507 };
508
509 if (auto fileParts = splitFileName(inputPath))
James Feist6714a252018-09-10 15:26:18 -0700510 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200511 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200512 if (map.count(item) != 0)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700513 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200514 for (const auto& t : map.at(item))
515 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700516 const auto& [suffix, level, direction, offset] = t;
Patrick Williams779c96a2023-05-10 07:50:42 -0500517 auto attrPath = boost::replace_all_copy(inputPath, item,
518 suffix);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200519 if (auto val = readFile(attrPath, scaleFactor))
520 {
521 *val += offset;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700522 if (debug)
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200523 {
524 std::cout << "Threshold: " << attrPath << ": " << *val
525 << "\n";
526 }
Chris Sidesa3279232023-04-24 16:08:13 -0500527 thresholdVector.emplace_back(level, direction, *val,
528 hysteresis);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200529 }
530 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700531 }
James Feist6714a252018-09-10 15:26:18 -0700532 }
James Feist6714a252018-09-10 15:26:18 -0700533 return true;
534}
535
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530536std::string getInterface(const Level thresholdLevel)
James Feist6714a252018-09-10 15:26:18 -0700537{
Ed Tanousc8fed202022-01-12 14:24:45 -0800538 for (const ThresholdDefinition& thresh : thresProp)
James Feist6714a252018-09-10 15:26:18 -0700539 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800540 if (thresh.level == thresholdLevel)
541 {
542 return std::string("xyz.openbmc_project.Sensor.Threshold.") +
543 thresh.levelName;
544 }
James Feist6714a252018-09-10 15:26:18 -0700545 }
Ed Tanousc8fed202022-01-12 14:24:45 -0800546 return "";
James Feist6714a252018-09-10 15:26:18 -0700547}
548} // namespace thresholds