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