blob: 94480bb3557d51b6d63e8e49363af3e79453146f [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Ed Tanous8a57ec02020-10-09 12:46:52 -07002#include <Utils.hpp>
James Feist8086aba2020-08-25 16:00:59 -07003#include <boost/asio/deadline_timer.hpp>
James Feist46342ec2019-03-06 14:03:41 -08004#include <boost/asio/io_service.hpp>
James Feist38fb5982020-05-28 10:09:54 -07005#include <nlohmann/json.hpp>
6
Patrick Venturefd6ba732019-10-31 14:27:39 -07007#include <list>
8#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -07009#include <string>
10#include <utility>
11#include <vector>
James Feist6714a252018-09-10 15:26:18 -070012
James Feist251c7822018-09-12 12:54:15 -070013struct Sensor;
James Feist6714a252018-09-10 15:26:18 -070014namespace thresholds
15{
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053016enum class Level
James Feist6714a252018-09-10 15:26:18 -070017{
18 WARNING,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053019 CRITICAL,
Rashmica Gupta8d0fd3c2021-11-30 16:07:14 +110020 PERFORMANCELOSS,
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +053021 SOFTSHUTDOWN,
22 HARDSHUTDOWN,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053023 ERROR
James Feist6714a252018-09-10 15:26:18 -070024};
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053025enum class Direction
James Feist6714a252018-09-10 15:26:18 -070026{
27 HIGH,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053028 LOW,
29 ERROR
James Feist6714a252018-09-10 15:26:18 -070030};
31struct Threshold
32{
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100033 Threshold(
34 const Level& lev, const Direction& dir, const double& val,
35 const double hysteresis = std::numeric_limits<double>::quiet_NaN(),
36 bool write = true) :
James Feist6714a252018-09-10 15:26:18 -070037 level(lev),
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100038 direction(dir), value(val), hysteresis(hysteresis), writeable(write)
James Feist38fb5982020-05-28 10:09:54 -070039 {}
James Feist6714a252018-09-10 15:26:18 -070040 Level level;
41 Direction direction;
42 double value;
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100043 double hysteresis;
James Feist6714a252018-09-10 15:26:18 -070044 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080045
46 bool operator==(const Threshold& rhs) const
47 {
48 return (level == rhs.level && direction == rhs.direction &&
49 value == rhs.value);
50 }
James Feist6714a252018-09-10 15:26:18 -070051};
52
Zhikui Ren59b8b9e2020-06-26 18:34:22 -070053void assertThresholds(Sensor* sensor, double assertValue,
54 thresholds::Level level, thresholds::Direction direction,
55 bool assert);
James Feist46342ec2019-03-06 14:03:41 -080056
Yong Li10306bd2020-04-21 16:08:28 +080057struct TimerUsed
58{
59 bool used;
60 Level level;
61 Direction direction;
Zhikui Renbf7cbc82020-07-02 08:44:00 -070062 bool assert;
Yong Li10306bd2020-04-21 16:08:28 +080063};
64
65using TimerPair = std::pair<struct TimerUsed, boost::asio::deadline_timer>;
James Feist43d32fe2019-09-04 10:35:20 -070066
James Feist46342ec2019-03-06 14:03:41 -080067struct ThresholdTimer
68{
69
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070070 ThresholdTimer(boost::asio::io_service& ioService) : io(ioService)
James Feist38fb5982020-05-28 10:09:54 -070071 {}
James Feist46342ec2019-03-06 14:03:41 -080072
Zhikui Renbf7cbc82020-07-02 08:44:00 -070073 bool hasActiveTimer(const Threshold& threshold, bool assert)
74 {
75 for (TimerPair& timer : timers)
76 {
77 if (timer.first.used)
78 {
79 if ((timer.first.level == threshold.level) &&
80 (timer.first.direction == threshold.direction) &&
81 (timer.first.assert == assert))
82 {
83 return true;
84 }
85 }
86 }
87 return false;
88 }
89
90 void stopTimer(const Threshold& threshold, bool assert)
Yong Li10306bd2020-04-21 16:08:28 +080091 {
92 struct TimerUsed timerUsed = {};
93 for (TimerPair& timer : timers)
94 {
95 timerUsed = timer.first;
96 if (timerUsed.used)
97 {
98 if ((timerUsed.level == threshold.level) &&
Zhikui Renbf7cbc82020-07-02 08:44:00 -070099 (timerUsed.direction == threshold.direction) &&
100 (timerUsed.assert == assert))
Yong Li10306bd2020-04-21 16:08:28 +0800101 {
102 timer.second.cancel();
103 }
104 }
105 }
106 }
107
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700108 void startTimer(const std::weak_ptr<Sensor>& weakSensor,
109 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800110 double assertValue);
James Feist46342ec2019-03-06 14:03:41 -0800111
James Feist43d32fe2019-09-04 10:35:20 -0700112 boost::asio::io_service& io;
113 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800114};
115
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800117 const SensorData& sensorData,
118 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -0500119 const std::string* matchLabel = nullptr, const int* sensorIndex = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700120
James Feistd8705872019-02-08 13:26:09 -0800121bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
122 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700123 const double& scaleFactor,
124 const double& offset = 0);
James Feist251c7822018-09-12 12:54:15 -0700125
Ed Tanousc8fed202022-01-12 14:24:45 -0800126struct ThresholdDefinition
127{
128 Level level;
129 uint8_t sevOrder;
130 const char* levelName;
131};
132
Rashmica Gupta8d0fd3c2021-11-30 16:07:14 +1100133constexpr static std::array<thresholds::ThresholdDefinition, 5> thresProp = {
Ed Tanousc8fed202022-01-12 14:24:45 -0800134 {{Level::WARNING, 0, "Warning"},
135 {Level::CRITICAL, 1, "Critical"},
Rashmica Gupta8d0fd3c2021-11-30 16:07:14 +1100136 {Level::PERFORMANCELOSS, 2, "PerformanceLoss"},
137 {Level::SOFTSHUTDOWN, 3, "SoftShutdown"},
138 {Level::HARDSHUTDOWN, 4, "HardShutdown"}}};
Ed Tanousc8fed202022-01-12 14:24:45 -0800139
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530140std::string getInterface(const Level level);
James Feist6714a252018-09-10 15:26:18 -0700141
James Feistd8705872019-02-08 13:26:09 -0800142void persistThreshold(const std::string& baseInterface, const std::string& path,
143 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800144 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800145 size_t thresholdCount, const std::string& label);
James Feist251c7822018-09-12 12:54:15 -0700146
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800147void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700148// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800149bool checkThresholds(Sensor* sensor);
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700150void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
151 ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800152
James Feist6714a252018-09-10 15:26:18 -0700153} // namespace thresholds