blob: 37ce10899547f9e5f870c9147db18e87b1ccd50f [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
James Feist43d32fe2019-09-04 10:35:20 -070042using TimerPair = std::pair<bool, boost::asio::deadline_timer>;
43
James Feist46342ec2019-03-06 14:03:41 -080044struct ThresholdTimer
45{
46
James Feist43d32fe2019-09-04 10:35:20 -070047 ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) :
48 io(ioService), sensor(sensor)
James Feist46342ec2019-03-06 14:03:41 -080049 {
50 }
51
52 void startTimer(const Threshold& threshold)
53 {
James Feist43d32fe2019-09-04 10:35:20 -070054 constexpr const size_t waitTime = 5;
55 TimerPair* pair = nullptr;
James Feist46342ec2019-03-06 14:03:41 -080056
James Feist43d32fe2019-09-04 10:35:20 -070057 for (TimerPair& timer : timers)
James Feist46342ec2019-03-06 14:03:41 -080058 {
James Feist43d32fe2019-09-04 10:35:20 -070059 if (!timer.first)
60 {
61 pair = &timer;
62 break;
63 }
James Feist46342ec2019-03-06 14:03:41 -080064 }
James Feist43d32fe2019-09-04 10:35:20 -070065 if (pair == nullptr)
James Feist46342ec2019-03-06 14:03:41 -080066 {
James Feist43d32fe2019-09-04 10:35:20 -070067 pair = &timers.emplace_back(false, boost::asio::deadline_timer(io));
James Feist46342ec2019-03-06 14:03:41 -080068 }
James Feist43d32fe2019-09-04 10:35:20 -070069 pair->first = true;
70 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
71 pair->second.async_wait(
72 [this, pair, threshold](boost::system::error_code ec) {
73 pair->first = false;
74
75 if (ec == boost::asio::error::operation_aborted)
76 {
77 return; // we're being canceled
78 }
79 else if (ec)
80 {
81 std::cerr << "timer error: " << ec.message() << "\n";
82 return;
83 }
84 if (isPowerOn())
85 {
86 assertThresholds(sensor, threshold.level,
87 threshold.direction, true);
88 }
89 });
James Feist46342ec2019-03-06 14:03:41 -080090 }
91
James Feist43d32fe2019-09-04 10:35:20 -070092 boost::asio::io_service& io;
93 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -080094 Sensor* sensor;
95};
96
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080098 const SensorData& sensorData,
99 std::vector<thresholds::Threshold>& thresholdVector,
100 const std::string* matchLabel = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700101
James Feistd8705872019-02-08 13:26:09 -0800102bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
103 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700104 const double& scaleFactor,
105 const double& offset = 0);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700106bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800107 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist251c7822018-09-12 12:54:15 -0700108
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700109bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800110 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist6714a252018-09-10 15:26:18 -0700111
James Feistd8705872019-02-08 13:26:09 -0800112void persistThreshold(const std::string& baseInterface, const std::string& path,
113 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800114 std::shared_ptr<sdbusplus::asio::connection>& conn,
115 size_t thresholdCount);
James Feist251c7822018-09-12 12:54:15 -0700116
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800117void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700118// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800119bool checkThresholds(Sensor* sensor);
James Feist46342ec2019-03-06 14:03:41 -0800120void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700121} // namespace thresholds