blob: 16b8bb92e0cba9a9509645f4b92b7b73893cce6d [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2#include <Utils.hpp>
James Feist46342ec2019-03-06 14:03:41 -08003#include <boost/asio/io_service.hpp>
James Feist6714a252018-09-10 15:26:18 -07004#include <nlohmann/json.hpp>
5
James Feist251c7822018-09-12 12:54:15 -07006struct Sensor;
James Feist6714a252018-09-10 15:26:18 -07007namespace thresholds
8{
9enum Level
10{
11 WARNING,
12 CRITICAL
13};
14enum Direction
15{
16 HIGH,
17 LOW
18};
19struct Threshold
20{
James Feistd8705872019-02-08 13:26:09 -080021 Threshold(const Level& lev, const Direction& dir, const double& val,
James Feist6714a252018-09-10 15:26:18 -070022 bool write = true) :
23 level(lev),
24 direction(dir), value(val), writeable(write)
25 {
26 }
27 Level level;
28 Direction direction;
29 double value;
30 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080031
32 bool operator==(const Threshold& rhs) const
33 {
34 return (level == rhs.level && direction == rhs.direction &&
35 value == rhs.value);
36 }
James Feist6714a252018-09-10 15:26:18 -070037};
38
James Feist46342ec2019-03-06 14:03:41 -080039void assertThresholds(Sensor* sensor, thresholds::Level level,
40 thresholds::Direction direction, bool assert);
41
42struct ThresholdTimer
43{
44
45 ThresholdTimer(boost::asio::io_service& io, Sensor* sensor) :
46 criticalTimer(io), warningTimer(io), sensor(sensor)
47 {
48 }
49
50 void startTimer(const Threshold& threshold)
51 {
52 constexpr const size_t waitTime = 2;
53
54 if (threshold.level == WARNING && !warningRunning)
55 {
56 warningRunning = true;
57 warningTimer.expires_from_now(boost::posix_time::seconds(waitTime));
58 warningTimer.async_wait(
59 [this, threshold](boost::system::error_code ec) {
60 if (ec == boost::asio::error::operation_aborted)
61 {
62 return; // we're being canceled
63 }
64 if (isPowerOn())
65 {
66 assertThresholds(sensor, threshold.level,
67 threshold.direction, true);
68 }
69 warningRunning = false;
70 });
71 }
72 else if (threshold.level == CRITICAL && !criticalRunning)
73 {
74 criticalRunning = true;
75 criticalTimer.expires_from_now(
76 boost::posix_time::seconds(waitTime));
77 criticalTimer.async_wait(
78 [this, threshold](boost::system::error_code ec) {
79 if (ec == boost::asio::error::operation_aborted)
80 {
81 return; // we're being canceled
82 }
83 if (isPowerOn())
84 {
85 assertThresholds(sensor, threshold.level,
86 threshold.direction, true);
87 }
88 criticalRunning = false;
89 });
90 }
91 }
92
93 boost::asio::deadline_timer criticalTimer;
94 boost::asio::deadline_timer warningTimer;
95 bool criticalRunning = false;
96 bool warningRunning = false;
97 Sensor* sensor;
98};
99
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800101 const SensorData& sensorData,
102 std::vector<thresholds::Threshold>& thresholdVector,
103 const std::string* matchLabel = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700104
James Feistd8705872019-02-08 13:26:09 -0800105bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
106 const std::string& inputPath,
107 const double& scaleFactor);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700108bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800109 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist251c7822018-09-12 12:54:15 -0700110
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800112 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist6714a252018-09-10 15:26:18 -0700113
James Feistd8705872019-02-08 13:26:09 -0800114void persistThreshold(const std::string& baseInterface, const std::string& path,
115 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800116 std::shared_ptr<sdbusplus::asio::connection>& conn,
117 size_t thresholdCount);
James Feist251c7822018-09-12 12:54:15 -0700118
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800119void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700120// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800121bool checkThresholds(Sensor* sensor);
James Feist46342ec2019-03-06 14:03:41 -0800122void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700123} // namespace thresholds