blob: 6338290a37b97cabad623aed47456493b6a0c6d3 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10302
3#include "Utils.hpp"
4
Ed Tanous1f978632023-02-28 18:16:39 -08005#include <boost/asio/io_context.hpp>
Ed Tanous9b4a20e2022-09-06 08:47:11 -07006#include <boost/asio/steady_timer.hpp>
James Feist38fb5982020-05-28 10:09:54 -07007#include <nlohmann/json.hpp>
8
Patrick Venturefd6ba732019-10-31 14:27:39 -07009#include <list>
10#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070011#include <string>
12#include <utility>
13#include <vector>
James Feist6714a252018-09-10 15:26:18 -070014
James Feist251c7822018-09-12 12:54:15 -070015struct Sensor;
James Feist6714a252018-09-10 15:26:18 -070016namespace thresholds
17{
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053018enum class Level
James Feist6714a252018-09-10 15:26:18 -070019{
20 WARNING,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053021 CRITICAL,
Rashmica Gupta8d0fd3c2021-11-30 16:07:14 +110022 PERFORMANCELOSS,
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +053023 SOFTSHUTDOWN,
24 HARDSHUTDOWN,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053025 ERROR
James Feist6714a252018-09-10 15:26:18 -070026};
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053027enum class Direction
James Feist6714a252018-09-10 15:26:18 -070028{
29 HIGH,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053030 LOW,
31 ERROR
James Feist6714a252018-09-10 15:26:18 -070032};
33struct Threshold
34{
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100035 Threshold(
36 const Level& lev, const Direction& dir, const double& val,
37 const double hysteresis = std::numeric_limits<double>::quiet_NaN(),
38 bool write = true) :
Patrick Williams2aaf7172024-08-16 15:20:40 -040039 level(lev), direction(dir), value(val), hysteresis(hysteresis),
40 writeable(write)
James Feist38fb5982020-05-28 10:09:54 -070041 {}
James Feist6714a252018-09-10 15:26:18 -070042 Level level;
43 Direction direction;
44 double value;
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100045 double hysteresis;
James Feist6714a252018-09-10 15:26:18 -070046 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080047
48 bool operator==(const Threshold& rhs) const
49 {
50 return (level == rhs.level && direction == rhs.direction &&
51 value == rhs.value);
52 }
James Feist6714a252018-09-10 15:26:18 -070053};
54
Zhikui Ren59b8b9e2020-06-26 18:34:22 -070055void assertThresholds(Sensor* sensor, double assertValue,
56 thresholds::Level level, thresholds::Direction direction,
57 bool assert);
James Feist46342ec2019-03-06 14:03:41 -080058
Yong Li10306bd2020-04-21 16:08:28 +080059struct TimerUsed
60{
61 bool used;
62 Level level;
63 Direction direction;
Zhikui Renbf7cbc82020-07-02 08:44:00 -070064 bool assert;
Yong Li10306bd2020-04-21 16:08:28 +080065};
66
Ed Tanous9b4a20e2022-09-06 08:47:11 -070067using TimerPair = std::pair<struct TimerUsed, boost::asio::steady_timer>;
James Feist43d32fe2019-09-04 10:35:20 -070068
James Feist46342ec2019-03-06 14:03:41 -080069struct ThresholdTimer
70{
Ed Tanous1f978632023-02-28 18:16:39 -080071 explicit ThresholdTimer(boost::asio::io_context& ioService) : io(ioService)
James Feist38fb5982020-05-28 10:09:54 -070072 {}
James Feist46342ec2019-03-06 14:03:41 -080073
Zhikui Renbf7cbc82020-07-02 08:44:00 -070074 bool hasActiveTimer(const Threshold& threshold, bool assert)
75 {
76 for (TimerPair& timer : timers)
77 {
78 if (timer.first.used)
79 {
80 if ((timer.first.level == threshold.level) &&
81 (timer.first.direction == threshold.direction) &&
82 (timer.first.assert == assert))
83 {
84 return true;
85 }
86 }
87 }
88 return false;
89 }
90
91 void stopTimer(const Threshold& threshold, bool assert)
Yong Li10306bd2020-04-21 16:08:28 +080092 {
93 struct TimerUsed timerUsed = {};
94 for (TimerPair& timer : timers)
95 {
96 timerUsed = timer.first;
97 if (timerUsed.used)
98 {
99 if ((timerUsed.level == threshold.level) &&
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700100 (timerUsed.direction == threshold.direction) &&
101 (timerUsed.assert == assert))
Yong Li10306bd2020-04-21 16:08:28 +0800102 {
103 timer.second.cancel();
104 }
105 }
106 }
107 }
108
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700109 void startTimer(const std::weak_ptr<Sensor>& weakSensor,
110 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800111 double assertValue);
James Feist46342ec2019-03-06 14:03:41 -0800112
Ed Tanous1f978632023-02-28 18:16:39 -0800113 boost::asio::io_context& io;
James Feist43d32fe2019-09-04 10:35:20 -0700114 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800115};
116
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800118 const SensorData& sensorData,
119 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -0500120 const std::string* matchLabel = nullptr, const int* sensorIndex = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700121
Chris Sidesa3279232023-04-24 16:08:13 -0500122// Sensors touched by parseThresholdFromAttr() are forcibly updated with given
123// parameters, so callers are encouraged to specify a sane hysteresis value for
124// their HW. For reference, the hysteresis fomula used in Sensor.hpp is:
125// hysteresis.trigger = (max_val - min_val) * 0.01
Ed Tanous2049bd22022-07-09 07:20:26 -0700126bool parseThresholdsFromAttr(
127 std::vector<thresholds::Threshold>& thresholdVector,
128 const std::string& inputPath, const double& scaleFactor,
Chris Sidesa3279232023-04-24 16:08:13 -0500129 const double& offset = 0,
130 const double& hysteresis = std::numeric_limits<double>::quiet_NaN());
James Feist251c7822018-09-12 12:54:15 -0700131
Ed Tanousc8fed202022-01-12 14:24:45 -0800132struct ThresholdDefinition
133{
134 Level level;
135 uint8_t sevOrder;
136 const char* levelName;
137};
138
Rashmica Gupta8d0fd3c2021-11-30 16:07:14 +1100139constexpr static std::array<thresholds::ThresholdDefinition, 5> thresProp = {
Ed Tanousc8fed202022-01-12 14:24:45 -0800140 {{Level::WARNING, 0, "Warning"},
141 {Level::CRITICAL, 1, "Critical"},
Rashmica Gupta8d0fd3c2021-11-30 16:07:14 +1100142 {Level::PERFORMANCELOSS, 2, "PerformanceLoss"},
143 {Level::SOFTSHUTDOWN, 3, "SoftShutdown"},
144 {Level::HARDSHUTDOWN, 4, "HardShutdown"}}};
Ed Tanousc8fed202022-01-12 14:24:45 -0800145
Ed Tanous2049bd22022-07-09 07:20:26 -0700146std::string getInterface(Level level);
James Feist6714a252018-09-10 15:26:18 -0700147
Ed Tanous2049bd22022-07-09 07:20:26 -0700148void persistThreshold(const std::string& path, const std::string& baseInterface,
James Feistd8705872019-02-08 13:26:09 -0800149 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800150 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800151 size_t thresholdCount, const std::string& label);
James Feist251c7822018-09-12 12:54:15 -0700152
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800153void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700154// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800155bool checkThresholds(Sensor* sensor);
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700156void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
157 ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800158
James Feist6714a252018-09-10 15:26:18 -0700159} // namespace thresholds