blob: 183fa349dfd513e7fb8f4d453e7691a0afe718fe [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Patrick Ventureca44b2f2019-10-31 11:02:26 -07002#include "Utils.hpp"
3
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{
James Feistd8705872019-02-08 13:26:09 -080028 Threshold(const Level& lev, const Direction& dir, const double& val,
James Feist6714a252018-09-10 15:26:18 -070029 bool write = true) :
30 level(lev),
31 direction(dir), value(val), writeable(write)
James Feist38fb5982020-05-28 10:09:54 -070032 {}
James Feist6714a252018-09-10 15:26:18 -070033 Level level;
34 Direction direction;
35 double value;
36 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080037
38 bool operator==(const Threshold& rhs) const
39 {
40 return (level == rhs.level && direction == rhs.direction &&
41 value == rhs.value);
42 }
James Feist6714a252018-09-10 15:26:18 -070043};
44
James Feist46342ec2019-03-06 14:03:41 -080045void assertThresholds(Sensor* sensor, thresholds::Level level,
46 thresholds::Direction direction, bool assert);
47
Yong Li10306bd2020-04-21 16:08:28 +080048struct TimerUsed
49{
50 bool used;
51 Level level;
52 Direction direction;
53};
54
55using TimerPair = std::pair<struct TimerUsed, boost::asio::deadline_timer>;
James Feist43d32fe2019-09-04 10:35:20 -070056
James Feist46342ec2019-03-06 14:03:41 -080057struct ThresholdTimer
58{
59
James Feist43d32fe2019-09-04 10:35:20 -070060 ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) :
61 io(ioService), sensor(sensor)
James Feist38fb5982020-05-28 10:09:54 -070062 {}
James Feist46342ec2019-03-06 14:03:41 -080063
Yong Li10306bd2020-04-21 16:08:28 +080064 void stopTimer(const Threshold& threshold)
65 {
66 struct TimerUsed timerUsed = {};
67 for (TimerPair& timer : timers)
68 {
69 timerUsed = timer.first;
70 if (timerUsed.used)
71 {
72 if ((timerUsed.level == threshold.level) &&
73 (timerUsed.direction == threshold.direction))
74 {
75 timer.second.cancel();
76 }
77 }
78 }
79 }
80
James Feist46342ec2019-03-06 14:03:41 -080081 void startTimer(const Threshold& threshold)
82 {
Yong Li10306bd2020-04-21 16:08:28 +080083 struct TimerUsed timerUsed = {};
James Feist43d32fe2019-09-04 10:35:20 -070084 constexpr const size_t waitTime = 5;
85 TimerPair* pair = nullptr;
James Feist46342ec2019-03-06 14:03:41 -080086
James Feist43d32fe2019-09-04 10:35:20 -070087 for (TimerPair& timer : timers)
James Feist46342ec2019-03-06 14:03:41 -080088 {
Yong Li10306bd2020-04-21 16:08:28 +080089 if (!timer.first.used)
James Feist43d32fe2019-09-04 10:35:20 -070090 {
91 pair = &timer;
92 break;
93 }
James Feist46342ec2019-03-06 14:03:41 -080094 }
James Feist43d32fe2019-09-04 10:35:20 -070095 if (pair == nullptr)
James Feist46342ec2019-03-06 14:03:41 -080096 {
Yong Li10306bd2020-04-21 16:08:28 +080097 pair = &timers.emplace_back(timerUsed,
98 boost::asio::deadline_timer(io));
James Feist46342ec2019-03-06 14:03:41 -080099 }
Yong Li10306bd2020-04-21 16:08:28 +0800100 pair->first.used = true;
101 pair->first.level = threshold.level;
102 pair->first.direction = threshold.direction;
James Feist43d32fe2019-09-04 10:35:20 -0700103 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
104 pair->second.async_wait(
105 [this, pair, threshold](boost::system::error_code ec) {
Yong Li10306bd2020-04-21 16:08:28 +0800106 pair->first.used = false;
James Feist43d32fe2019-09-04 10:35:20 -0700107
108 if (ec == boost::asio::error::operation_aborted)
109 {
110 return; // we're being canceled
111 }
112 else if (ec)
113 {
114 std::cerr << "timer error: " << ec.message() << "\n";
115 return;
116 }
117 if (isPowerOn())
118 {
119 assertThresholds(sensor, threshold.level,
120 threshold.direction, true);
121 }
122 });
James Feist46342ec2019-03-06 14:03:41 -0800123 }
124
James Feist43d32fe2019-09-04 10:35:20 -0700125 boost::asio::io_service& io;
126 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -0800127 Sensor* sensor;
128};
129
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700130bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -0800131 const SensorData& sensorData,
132 std::vector<thresholds::Threshold>& thresholdVector,
133 const std::string* matchLabel = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700134
James Feistd8705872019-02-08 13:26:09 -0800135bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
136 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700137 const double& scaleFactor,
138 const double& offset = 0);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700139bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800140 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist251c7822018-09-12 12:54:15 -0700141
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700142bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800143 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist6714a252018-09-10 15:26:18 -0700144
James Feistd8705872019-02-08 13:26:09 -0800145void persistThreshold(const std::string& baseInterface, const std::string& path,
146 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800147 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800148 size_t thresholdCount, const std::string& label);
James Feist251c7822018-09-12 12:54:15 -0700149
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800150void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700151// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800152bool checkThresholds(Sensor* sensor);
James Feist46342ec2019-03-06 14:03:41 -0800153void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800154
James Feist6714a252018-09-10 15:26:18 -0700155} // namespace thresholds