blob: a89c1dc1b84d1971db29d1989bf135418c0838d2 [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) :
James Feist6714a252018-09-10 15:26:18 -070039 level(lev),
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100040 direction(dir), value(val), hysteresis(hysteresis), 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{
71
Ed Tanous1f978632023-02-28 18:16:39 -080072 explicit ThresholdTimer(boost::asio::io_context& ioService) : io(ioService)
James Feist38fb5982020-05-28 10:09:54 -070073 {}
James Feist46342ec2019-03-06 14:03:41 -080074
Zhikui Renbf7cbc82020-07-02 08:44:00 -070075 bool hasActiveTimer(const Threshold& threshold, bool assert)
76 {
77 for (TimerPair& timer : timers)
78 {
79 if (timer.first.used)
80 {
81 if ((timer.first.level == threshold.level) &&
82 (timer.first.direction == threshold.direction) &&
83 (timer.first.assert == assert))
84 {
85 return true;
86 }
87 }
88 }
89 return false;
90 }
91
92 void stopTimer(const Threshold& threshold, bool assert)
Yong Li10306bd2020-04-21 16:08:28 +080093 {
94 struct TimerUsed timerUsed = {};
95 for (TimerPair& timer : timers)
96 {
97 timerUsed = timer.first;
98 if (timerUsed.used)
99 {
100 if ((timerUsed.level == threshold.level) &&
Zhikui Renbf7cbc82020-07-02 08:44:00 -0700101 (timerUsed.direction == threshold.direction) &&
102 (timerUsed.assert == assert))
Yong Li10306bd2020-04-21 16:08:28 +0800103 {
104 timer.second.cancel();
105 }
106 }
107 }
108 }
109
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700110 void startTimer(const std::weak_ptr<Sensor>& weakSensor,
111 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800112 double assertValue);
James Feist46342ec2019-03-06 14:03:41 -0800113
Ed Tanous1f978632023-02-28 18:16:39 -0800114 boost::asio::io_context& io;
James Feist43d32fe2019-09-04 10:35:20 -0700115 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800116};
117
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700118bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800119 const SensorData& sensorData,
120 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -0500121 const std::string* matchLabel = nullptr, const int* sensorIndex = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700122
Ed Tanous2049bd22022-07-09 07:20:26 -0700123bool parseThresholdsFromAttr(
124 std::vector<thresholds::Threshold>& thresholdVector,
125 const std::string& inputPath, const double& scaleFactor,
126 const double& offset = 0);
James Feist251c7822018-09-12 12:54:15 -0700127
Ed Tanousc8fed202022-01-12 14:24:45 -0800128struct ThresholdDefinition
129{
130 Level level;
131 uint8_t sevOrder;
132 const char* levelName;
133};
134
Rashmica Gupta8d0fd3c2021-11-30 16:07:14 +1100135constexpr static std::array<thresholds::ThresholdDefinition, 5> thresProp = {
Ed Tanousc8fed202022-01-12 14:24:45 -0800136 {{Level::WARNING, 0, "Warning"},
137 {Level::CRITICAL, 1, "Critical"},
Rashmica Gupta8d0fd3c2021-11-30 16:07:14 +1100138 {Level::PERFORMANCELOSS, 2, "PerformanceLoss"},
139 {Level::SOFTSHUTDOWN, 3, "SoftShutdown"},
140 {Level::HARDSHUTDOWN, 4, "HardShutdown"}}};
Ed Tanousc8fed202022-01-12 14:24:45 -0800141
Ed Tanous2049bd22022-07-09 07:20:26 -0700142std::string getInterface(Level level);
James Feist6714a252018-09-10 15:26:18 -0700143
Ed Tanous2049bd22022-07-09 07:20:26 -0700144void persistThreshold(const std::string& path, const std::string& baseInterface,
James Feistd8705872019-02-08 13:26:09 -0800145 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800146 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800147 size_t thresholdCount, const std::string& label);
James Feist251c7822018-09-12 12:54:15 -0700148
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800149void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700150// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800151bool checkThresholds(Sensor* sensor);
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700152void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
153 ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800154
James Feist6714a252018-09-10 15:26:18 -0700155} // namespace thresholds