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 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 42 | using TimerPair = std::pair<bool, boost::asio::deadline_timer>; |
| 43 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 44 | struct ThresholdTimer |
| 45 | { |
| 46 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 47 | ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) : |
| 48 | io(ioService), sensor(sensor) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
| 52 | void startTimer(const Threshold& threshold) |
| 53 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 54 | constexpr const size_t waitTime = 5; |
| 55 | TimerPair* pair = nullptr; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 56 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 57 | for (TimerPair& timer : timers) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 58 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 59 | if (!timer.first) |
| 60 | { |
| 61 | pair = &timer; |
| 62 | break; |
| 63 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 64 | } |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 65 | if (pair == nullptr) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 66 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 67 | pair = &timers.emplace_back(false, boost::asio::deadline_timer(io)); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 68 | } |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 69 | 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 Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 90 | } |
| 91 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame^] | 92 | boost::asio::io_service& io; |
| 93 | std::list<TimerPair> timers; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 94 | Sensor* sensor; |
| 95 | }; |
| 96 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 97 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 98 | const SensorData& sensorData, |
| 99 | std::vector<thresholds::Threshold>& thresholdVector, |
| 100 | const std::string* matchLabel = nullptr); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 101 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 102 | bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds, |
| 103 | const std::string& inputPath, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 104 | const double& scaleFactor, |
| 105 | const double& offset = 0); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 106 | bool hasCriticalInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 107 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 108 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 109 | bool hasWarningInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 110 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 111 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 112 | void persistThreshold(const std::string& baseInterface, const std::string& path, |
| 113 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 114 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 115 | size_t thresholdCount); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 116 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 117 | void updateThresholds(Sensor* sensor); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 118 | // returns false if a critical threshold has been crossed, true otherwise |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 119 | bool checkThresholds(Sensor* sensor); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 120 | void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 121 | } // namespace thresholds |