James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <Utils.hpp> |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 3 | #include <boost/asio/io_service.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 4 | #include <nlohmann/json.hpp> |
| 5 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 6 | struct Sensor; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 7 | namespace thresholds |
| 8 | { |
| 9 | enum Level |
| 10 | { |
| 11 | WARNING, |
| 12 | CRITICAL |
| 13 | }; |
| 14 | enum Direction |
| 15 | { |
| 16 | HIGH, |
| 17 | LOW |
| 18 | }; |
| 19 | struct Threshold |
| 20 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 21 | Threshold(const Level& lev, const Direction& dir, const double& val, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | 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 Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 31 | |
| 32 | bool operator==(const Threshold& rhs) const |
| 33 | { |
| 34 | return (level == rhs.level && direction == rhs.direction && |
| 35 | value == rhs.value); |
| 36 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 39 | void assertThresholds(Sensor* sensor, thresholds::Level level, |
| 40 | thresholds::Direction direction, bool assert); |
| 41 | |
| 42 | struct 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 Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 100 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 101 | const SensorData& sensorData, |
| 102 | std::vector<thresholds::Threshold>& thresholdVector, |
| 103 | const std::string* matchLabel = nullptr); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 104 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 105 | bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds, |
| 106 | const std::string& inputPath, |
| 107 | const double& scaleFactor); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 108 | bool hasCriticalInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 109 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 110 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 111 | bool hasWarningInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 112 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 114 | void persistThreshold(const std::string& baseInterface, const std::string& path, |
| 115 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 116 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 117 | size_t thresholdCount); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 118 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 119 | void updateThresholds(Sensor* sensor); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 120 | // returns false if a critical threshold has been crossed, true otherwise |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 121 | bool checkThresholds(Sensor* sensor); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 122 | void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 123 | } // namespace thresholds |