blob: f1e7cc65666f1f6674cc03d2150cc0ae38ea043a [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 Feist6714a252018-09-10 15:26:18 -07005#include <nlohmann/json.hpp>
6
James Feist251c7822018-09-12 12:54:15 -07007struct Sensor;
James Feist6714a252018-09-10 15:26:18 -07008namespace thresholds
9{
10enum Level
11{
12 WARNING,
13 CRITICAL
14};
15enum Direction
16{
17 HIGH,
18 LOW
19};
20struct Threshold
21{
James Feistd8705872019-02-08 13:26:09 -080022 Threshold(const Level& lev, const Direction& dir, const double& val,
James Feist6714a252018-09-10 15:26:18 -070023 bool write = true) :
24 level(lev),
25 direction(dir), value(val), writeable(write)
26 {
27 }
28 Level level;
29 Direction direction;
30 double value;
31 bool writeable;
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -080032
33 bool operator==(const Threshold& rhs) const
34 {
35 return (level == rhs.level && direction == rhs.direction &&
36 value == rhs.value);
37 }
James Feist6714a252018-09-10 15:26:18 -070038};
39
James Feist46342ec2019-03-06 14:03:41 -080040void assertThresholds(Sensor* sensor, thresholds::Level level,
41 thresholds::Direction direction, bool assert);
42
James Feist43d32fe2019-09-04 10:35:20 -070043using TimerPair = std::pair<bool, boost::asio::deadline_timer>;
44
James Feist46342ec2019-03-06 14:03:41 -080045struct ThresholdTimer
46{
47
James Feist43d32fe2019-09-04 10:35:20 -070048 ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) :
49 io(ioService), sensor(sensor)
James Feist46342ec2019-03-06 14:03:41 -080050 {
51 }
52
53 void startTimer(const Threshold& threshold)
54 {
James Feist43d32fe2019-09-04 10:35:20 -070055 constexpr const size_t waitTime = 5;
56 TimerPair* pair = nullptr;
James Feist46342ec2019-03-06 14:03:41 -080057
James Feist43d32fe2019-09-04 10:35:20 -070058 for (TimerPair& timer : timers)
James Feist46342ec2019-03-06 14:03:41 -080059 {
James Feist43d32fe2019-09-04 10:35:20 -070060 if (!timer.first)
61 {
62 pair = &timer;
63 break;
64 }
James Feist46342ec2019-03-06 14:03:41 -080065 }
James Feist43d32fe2019-09-04 10:35:20 -070066 if (pair == nullptr)
James Feist46342ec2019-03-06 14:03:41 -080067 {
James Feist43d32fe2019-09-04 10:35:20 -070068 pair = &timers.emplace_back(false, boost::asio::deadline_timer(io));
James Feist46342ec2019-03-06 14:03:41 -080069 }
James Feist43d32fe2019-09-04 10:35:20 -070070 pair->first = true;
71 pair->second.expires_from_now(boost::posix_time::seconds(waitTime));
72 pair->second.async_wait(
73 [this, pair, threshold](boost::system::error_code ec) {
74 pair->first = false;
75
76 if (ec == boost::asio::error::operation_aborted)
77 {
78 return; // we're being canceled
79 }
80 else if (ec)
81 {
82 std::cerr << "timer error: " << ec.message() << "\n";
83 return;
84 }
85 if (isPowerOn())
86 {
87 assertThresholds(sensor, threshold.level,
88 threshold.direction, true);
89 }
90 });
James Feist46342ec2019-03-06 14:03:41 -080091 }
92
James Feist43d32fe2019-09-04 10:35:20 -070093 boost::asio::io_service& io;
94 std::list<TimerPair> timers;
James Feist46342ec2019-03-06 14:03:41 -080095 Sensor* sensor;
96};
97
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098bool parseThresholdsFromConfig(
James Feistd8705872019-02-08 13:26:09 -080099 const SensorData& sensorData,
100 std::vector<thresholds::Threshold>& thresholdVector,
101 const std::string* matchLabel = nullptr);
James Feist6714a252018-09-10 15:26:18 -0700102
James Feistd8705872019-02-08 13:26:09 -0800103bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds,
104 const std::string& inputPath,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700105 const double& scaleFactor,
106 const double& offset = 0);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700107bool hasCriticalInterface(
James Feistd8705872019-02-08 13:26:09 -0800108 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist251c7822018-09-12 12:54:15 -0700109
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110bool hasWarningInterface(
James Feistd8705872019-02-08 13:26:09 -0800111 const std::vector<thresholds::Threshold>& thresholdVector);
James Feist6714a252018-09-10 15:26:18 -0700112
James Feistd8705872019-02-08 13:26:09 -0800113void persistThreshold(const std::string& baseInterface, const std::string& path,
114 const thresholds::Threshold& threshold,
James Feista222ba72019-03-01 15:57:51 -0800115 std::shared_ptr<sdbusplus::asio::connection>& conn,
116 size_t thresholdCount);
James Feist251c7822018-09-12 12:54:15 -0700117
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800118void updateThresholds(Sensor* sensor);
James Feistdc6c55f2018-10-31 12:53:20 -0700119// returns false if a critical threshold has been crossed, true otherwise
James Feistd8705872019-02-08 13:26:09 -0800120bool checkThresholds(Sensor* sensor);
James Feist46342ec2019-03-06 14:03:41 -0800121void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700122} // namespace thresholds