blob: 209d68e80f7f8daf4e77814beeea27933a33c556 [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{
16enum Level
17{
18 WARNING,
19 CRITICAL
20};
21enum Direction
22{
23 HIGH,
24 LOW
25};
26struct Threshold
27{
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100028 Threshold(
29 const Level& lev, const Direction& dir, const double& val,
30 const double hysteresis = std::numeric_limits<double>::quiet_NaN(),
31 bool write = true) :
James Feist6714a252018-09-10 15:26:18 -070032 level(lev),
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100033 direction(dir), value(val), hysteresis(hysteresis), writeable(write)
James Feist38fb5982020-05-28 10:09:54 -070034 {}
James Feist6714a252018-09-10 15:26:18 -070035 Level level;
36 Direction direction;
37 double value;
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100038 double hysteresis;
James Feist6714a252018-09-10 15:26:18 -070039 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080040
41 bool operator==(const Threshold& rhs) const
42 {
43 return (level == rhs.level && direction == rhs.direction &&
44 value == rhs.value);
45 }
James Feist6714a252018-09-10 15:26:18 -070046};
47
Zhikui Ren59b8b9e2020-06-26 18:34:22 -070048void assertThresholds(Sensor* sensor, double assertValue,
49 thresholds::Level level, thresholds::Direction direction,
50 bool assert);
James Feist46342ec2019-03-06 14:03:41 -080051
Yong Li10306bd2020-04-21 16:08:28 +080052struct TimerUsed
53{
54 bool used;
55 Level level;
56 Direction direction;
Zhikui Renbf7cbc82020-07-02 08:44:00 -070057 bool assert;
Yong Li10306bd2020-04-21 16:08:28 +080058};
59
60using TimerPair = std::pair<struct TimerUsed, boost::asio::deadline_timer>;
James Feist43d32fe2019-09-04 10:35:20 -070061
James Feist46342ec2019-03-06 14:03:41 -080062struct ThresholdTimer
63{
64
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070065 ThresholdTimer(boost::asio::io_service& ioService) : io(ioService)
James Feist38fb5982020-05-28 10:09:54 -070066 {}
James Feist46342ec2019-03-06 14:03:41 -080067
Zhikui Renbf7cbc82020-07-02 08:44:00 -070068 bool hasActiveTimer(const Threshold& threshold, bool assert)
69 {
70 for (TimerPair& timer : timers)
71 {
72 if (timer.first.used)
73 {
74 if ((timer.first.level == threshold.level) &&
75 (timer.first.direction == threshold.direction) &&
76 (timer.first.assert == assert))
77 {
78 return true;
79 }
80 }
81 }
82 return false;
83 }
84
85 void stopTimer(const Threshold& threshold, bool assert)
Yong Li10306bd2020-04-21 16:08:28 +080086 {
87 struct TimerUsed timerUsed = {};
88 for (TimerPair& timer : timers)
89 {
90 timerUsed = timer.first;
91 if (timerUsed.used)
92 {
93 if ((timerUsed.level == threshold.level) &&
Zhikui Renbf7cbc82020-07-02 08:44:00 -070094 (timerUsed.direction == threshold.direction) &&
95 (timerUsed.assert == assert))
Yong Li10306bd2020-04-21 16:08:28 +080096 {
97 timer.second.cancel();
98 }
99 }
100 }
101 }
102
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700103 void startTimer(const std::weak_ptr<Sensor>& weakSensor,
104 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800105 double assertValue);
James Feist46342ec2019-03-06 14:03:41 -0800106
James Feist43d32fe2019-09-04 10:35:20 -0700107 boost::asio::io_service& io;
108 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800109};
110
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800112 const SensorData& sensorData,
113 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -0500114 const std::string* matchLabel = nullptr, const int* sensorIndex = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700115
James Feistd8705872019-02-08 13:26:09 -0800116bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
117 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700118 const double& scaleFactor,
119 const double& offset = 0);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800121 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist251c7822018-09-12 12:54:15 -0700122
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700123bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800124 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist6714a252018-09-10 15:26:18 -0700125
James Feistd8705872019-02-08 13:26:09 -0800126void persistThreshold(const std::string& baseInterface, const std::string& path,
127 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800128 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800129 size_t thresholdCount, const std::string& label);
James Feist251c7822018-09-12 12:54:15 -0700130
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800131void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700132// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800133bool checkThresholds(Sensor* sensor);
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700134void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
135 ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800136
James Feist6714a252018-09-10 15:26:18 -0700137} // namespace thresholds