blob: 50075a2d004c7957dd3afe015c10589639f9e193 [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,
Jayashree Dhanapal091c92c2022-01-11 14:55:20 +053020 SOFTSHUTDOWN,
21 HARDSHUTDOWN,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053022 ERROR
James Feist6714a252018-09-10 15:26:18 -070023};
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053024enum class Direction
James Feist6714a252018-09-10 15:26:18 -070025{
26 HIGH,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053027 LOW,
28 ERROR
James Feist6714a252018-09-10 15:26:18 -070029};
30struct Threshold
31{
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100032 Threshold(
33 const Level& lev, const Direction& dir, const double& val,
34 const double hysteresis = std::numeric_limits<double>::quiet_NaN(),
35 bool write = true) :
James Feist6714a252018-09-10 15:26:18 -070036 level(lev),
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100037 direction(dir), value(val), hysteresis(hysteresis), writeable(write)
James Feist38fb5982020-05-28 10:09:54 -070038 {}
James Feist6714a252018-09-10 15:26:18 -070039 Level level;
40 Direction direction;
41 double value;
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100042 double hysteresis;
James Feist6714a252018-09-10 15:26:18 -070043 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080044
45 bool operator==(const Threshold& rhs) const
46 {
47 return (level == rhs.level && direction == rhs.direction &&
48 value == rhs.value);
49 }
James Feist6714a252018-09-10 15:26:18 -070050};
51
Zhikui Ren59b8b9e2020-06-26 18:34:22 -070052void assertThresholds(Sensor* sensor, double assertValue,
53 thresholds::Level level, thresholds::Direction direction,
54 bool assert);
James Feist46342ec2019-03-06 14:03:41 -080055
Yong Li10306bd2020-04-21 16:08:28 +080056struct TimerUsed
57{
58 bool used;
59 Level level;
60 Direction direction;
Zhikui Renbf7cbc82020-07-02 08:44:00 -070061 bool assert;
Yong Li10306bd2020-04-21 16:08:28 +080062};
63
64using TimerPair = std::pair<struct TimerUsed, boost::asio::deadline_timer>;
James Feist43d32fe2019-09-04 10:35:20 -070065
James Feist46342ec2019-03-06 14:03:41 -080066struct ThresholdTimer
67{
68
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070069 ThresholdTimer(boost::asio::io_service& ioService) : io(ioService)
James Feist38fb5982020-05-28 10:09:54 -070070 {}
James Feist46342ec2019-03-06 14:03:41 -080071
Zhikui Renbf7cbc82020-07-02 08:44:00 -070072 bool hasActiveTimer(const Threshold& threshold, bool assert)
73 {
74 for (TimerPair& timer : timers)
75 {
76 if (timer.first.used)
77 {
78 if ((timer.first.level == threshold.level) &&
79 (timer.first.direction == threshold.direction) &&
80 (timer.first.assert == assert))
81 {
82 return true;
83 }
84 }
85 }
86 return false;
87 }
88
89 void stopTimer(const Threshold& threshold, bool assert)
Yong Li10306bd2020-04-21 16:08:28 +080090 {
91 struct TimerUsed timerUsed = {};
92 for (TimerPair& timer : timers)
93 {
94 timerUsed = timer.first;
95 if (timerUsed.used)
96 {
97 if ((timerUsed.level == threshold.level) &&
Zhikui Renbf7cbc82020-07-02 08:44:00 -070098 (timerUsed.direction == threshold.direction) &&
99 (timerUsed.assert == assert))
Yong Li10306bd2020-04-21 16:08:28 +0800100 {
101 timer.second.cancel();
102 }
103 }
104 }
105 }
106
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700107 void startTimer(const std::weak_ptr<Sensor>& weakSensor,
108 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800109 double assertValue);
James Feist46342ec2019-03-06 14:03:41 -0800110
James Feist43d32fe2019-09-04 10:35:20 -0700111 boost::asio::io_service& io;
112 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800113};
114
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530115bool findOrder(Level lev, Direction dir);
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
James Feistd8705872019-02-08 13:26:09 -0800122bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
123 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700124 const double& scaleFactor,
125 const double& offset = 0);
James Feist251c7822018-09-12 12:54:15 -0700126
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530127std::string getInterface(const Level level);
James Feist6714a252018-09-10 15:26:18 -0700128
James Feistd8705872019-02-08 13:26:09 -0800129void persistThreshold(const std::string& baseInterface, const std::string& path,
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& label);
James Feist251c7822018-09-12 12:54:15 -0700133
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800134void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700135// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800136bool checkThresholds(Sensor* sensor);
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700137void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
138 ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800139
James Feist6714a252018-09-10 15:26:18 -0700140} // namespace thresholds