blob: 5dcc00df1694e7bc647f46f4ac17c226b486b9a8 [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,
20 ERROR
James Feist6714a252018-09-10 15:26:18 -070021};
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053022enum class Direction
James Feist6714a252018-09-10 15:26:18 -070023{
24 HIGH,
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053025 LOW,
26 ERROR
James Feist6714a252018-09-10 15:26:18 -070027};
28struct Threshold
29{
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100030 Threshold(
31 const Level& lev, const Direction& dir, const double& val,
32 const double hysteresis = std::numeric_limits<double>::quiet_NaN(),
33 bool write = true) :
James Feist6714a252018-09-10 15:26:18 -070034 level(lev),
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100035 direction(dir), value(val), hysteresis(hysteresis), writeable(write)
James Feist38fb5982020-05-28 10:09:54 -070036 {}
James Feist6714a252018-09-10 15:26:18 -070037 Level level;
38 Direction direction;
39 double value;
Rashmica Gupta1e34cec2021-08-31 16:47:39 +100040 double hysteresis;
James Feist6714a252018-09-10 15:26:18 -070041 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080042
43 bool operator==(const Threshold& rhs) const
44 {
45 return (level == rhs.level && direction == rhs.direction &&
46 value == rhs.value);
47 }
James Feist6714a252018-09-10 15:26:18 -070048};
49
Zhikui Ren59b8b9e2020-06-26 18:34:22 -070050void assertThresholds(Sensor* sensor, double assertValue,
51 thresholds::Level level, thresholds::Direction direction,
52 bool assert);
James Feist46342ec2019-03-06 14:03:41 -080053
Yong Li10306bd2020-04-21 16:08:28 +080054struct TimerUsed
55{
56 bool used;
57 Level level;
58 Direction direction;
Zhikui Renbf7cbc82020-07-02 08:44:00 -070059 bool assert;
Yong Li10306bd2020-04-21 16:08:28 +080060};
61
62using TimerPair = std::pair<struct TimerUsed, boost::asio::deadline_timer>;
James Feist43d32fe2019-09-04 10:35:20 -070063
James Feist46342ec2019-03-06 14:03:41 -080064struct ThresholdTimer
65{
66
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070067 ThresholdTimer(boost::asio::io_service& ioService) : io(ioService)
James Feist38fb5982020-05-28 10:09:54 -070068 {}
James Feist46342ec2019-03-06 14:03:41 -080069
Zhikui Renbf7cbc82020-07-02 08:44:00 -070070 bool hasActiveTimer(const Threshold& threshold, bool assert)
71 {
72 for (TimerPair& timer : timers)
73 {
74 if (timer.first.used)
75 {
76 if ((timer.first.level == threshold.level) &&
77 (timer.first.direction == threshold.direction) &&
78 (timer.first.assert == assert))
79 {
80 return true;
81 }
82 }
83 }
84 return false;
85 }
86
87 void stopTimer(const Threshold& threshold, bool assert)
Yong Li10306bd2020-04-21 16:08:28 +080088 {
89 struct TimerUsed timerUsed = {};
90 for (TimerPair& timer : timers)
91 {
92 timerUsed = timer.first;
93 if (timerUsed.used)
94 {
95 if ((timerUsed.level == threshold.level) &&
Zhikui Renbf7cbc82020-07-02 08:44:00 -070096 (timerUsed.direction == threshold.direction) &&
97 (timerUsed.assert == assert))
Yong Li10306bd2020-04-21 16:08:28 +080098 {
99 timer.second.cancel();
100 }
101 }
102 }
103 }
104
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700105 void startTimer(const std::weak_ptr<Sensor>& weakSensor,
106 const Threshold& threshold, bool assert,
Jeff Lind9cd7042020-11-20 15:49:28 +0800107 double assertValue);
James Feist46342ec2019-03-06 14:03:41 -0800108
James Feist43d32fe2019-09-04 10:35:20 -0700109 boost::asio::io_service& io;
110 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800111};
112
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530113bool findOrder(Level lev, Direction dir);
114
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800116 const SensorData& sensorData,
117 std::vector<thresholds::Threshold>& thresholdVector,
Matt Spinler5636d522021-03-17 14:52:18 -0500118 const std::string* matchLabel = nullptr, const int* sensorIndex = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700119
James Feistd8705872019-02-08 13:26:09 -0800120bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
121 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700122 const double& scaleFactor,
123 const double& offset = 0);
James Feist251c7822018-09-12 12:54:15 -0700124
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530125std::string getInterface(const Level level);
James Feist6714a252018-09-10 15:26:18 -0700126
James Feistd8705872019-02-08 13:26:09 -0800127void persistThreshold(const std::string& baseInterface, const std::string& path,
128 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800129 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800130 size_t thresholdCount, const std::string& label);
James Feist251c7822018-09-12 12:54:15 -0700131
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800132void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700133// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800134bool checkThresholds(Sensor* sensor);
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700135void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
136 ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800137
James Feist6714a252018-09-10 15:26:18 -0700138} // namespace thresholds