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 | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 5 | #include <nlohmann/json.hpp> |
| 6 | |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 7 | #include <list> |
| 8 | #include <memory> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 12 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 13 | struct Sensor; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 14 | namespace thresholds |
| 15 | { |
| 16 | enum Level |
| 17 | { |
| 18 | WARNING, |
| 19 | CRITICAL |
| 20 | }; |
| 21 | enum Direction |
| 22 | { |
| 23 | HIGH, |
| 24 | LOW |
| 25 | }; |
| 26 | struct Threshold |
| 27 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 28 | Threshold(const Level& lev, const Direction& dir, const double& val, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | bool write = true) : |
| 30 | level(lev), |
| 31 | direction(dir), value(val), writeable(write) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 32 | {} |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | Level level; |
| 34 | Direction direction; |
| 35 | double value; |
| 36 | bool writeable; |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 37 | |
| 38 | bool operator==(const Threshold& rhs) const |
| 39 | { |
| 40 | return (level == rhs.level && direction == rhs.direction && |
| 41 | value == rhs.value); |
| 42 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 45 | void assertThresholds(Sensor* sensor, double assertValue, |
| 46 | thresholds::Level level, thresholds::Direction direction, |
| 47 | bool assert); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 48 | |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 49 | struct TimerUsed |
| 50 | { |
| 51 | bool used; |
| 52 | Level level; |
| 53 | Direction direction; |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame^] | 54 | bool assert; |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | using TimerPair = std::pair<struct TimerUsed, boost::asio::deadline_timer>; |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 58 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 59 | struct ThresholdTimer |
| 60 | { |
| 61 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 62 | ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) : |
| 63 | io(ioService), sensor(sensor) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 64 | {} |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 65 | |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame^] | 66 | bool hasActiveTimer(const Threshold& threshold, bool assert) |
| 67 | { |
| 68 | for (TimerPair& timer : timers) |
| 69 | { |
| 70 | if (timer.first.used) |
| 71 | { |
| 72 | if ((timer.first.level == threshold.level) && |
| 73 | (timer.first.direction == threshold.direction) && |
| 74 | (timer.first.assert == assert)) |
| 75 | { |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | void stopTimer(const Threshold& threshold, bool assert) |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 84 | { |
| 85 | struct TimerUsed timerUsed = {}; |
| 86 | for (TimerPair& timer : timers) |
| 87 | { |
| 88 | timerUsed = timer.first; |
| 89 | if (timerUsed.used) |
| 90 | { |
| 91 | if ((timerUsed.level == threshold.level) && |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame^] | 92 | (timerUsed.direction == threshold.direction) && |
| 93 | (timerUsed.assert == assert)) |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 94 | { |
| 95 | timer.second.cancel(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame^] | 101 | void startTimer(const Threshold& threshold, bool assert, double assertValue) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 102 | { |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 103 | struct TimerUsed timerUsed = {}; |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 104 | constexpr const size_t waitTime = 5; |
| 105 | TimerPair* pair = nullptr; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 106 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 107 | for (TimerPair& timer : timers) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 108 | { |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 109 | if (!timer.first.used) |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 110 | { |
| 111 | pair = &timer; |
| 112 | break; |
| 113 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 114 | } |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 115 | if (pair == nullptr) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 116 | { |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 117 | pair = &timers.emplace_back(timerUsed, |
| 118 | boost::asio::deadline_timer(io)); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 119 | } |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame^] | 120 | |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 121 | pair->first.used = true; |
| 122 | pair->first.level = threshold.level; |
| 123 | pair->first.direction = threshold.direction; |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame^] | 124 | pair->first.assert = assert; |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 125 | pair->second.expires_from_now(boost::posix_time::seconds(waitTime)); |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame^] | 126 | pair->second.async_wait([this, pair, threshold, assert, |
| 127 | assertValue](boost::system::error_code ec) { |
| 128 | pair->first.used = false; |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 129 | |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame^] | 130 | if (ec == boost::asio::error::operation_aborted) |
| 131 | { |
| 132 | return; // we're being canceled |
| 133 | } |
| 134 | else if (ec) |
| 135 | { |
| 136 | std::cerr << "timer error: " << ec.message() << "\n"; |
| 137 | return; |
| 138 | } |
| 139 | if (isPowerOn()) |
| 140 | { |
| 141 | assertThresholds(sensor, assertValue, threshold.level, |
| 142 | threshold.direction, assert); |
| 143 | } |
| 144 | }); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 145 | } |
| 146 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 147 | boost::asio::io_service& io; |
| 148 | std::list<TimerPair> timers; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 149 | Sensor* sensor; |
| 150 | }; |
| 151 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 152 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 153 | const SensorData& sensorData, |
| 154 | std::vector<thresholds::Threshold>& thresholdVector, |
| 155 | const std::string* matchLabel = nullptr); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 157 | bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds, |
| 158 | const std::string& inputPath, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 159 | const double& scaleFactor, |
| 160 | const double& offset = 0); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 161 | bool hasCriticalInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 162 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 163 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 164 | bool hasWarningInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 165 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 166 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 167 | void persistThreshold(const std::string& baseInterface, const std::string& path, |
| 168 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 169 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 170 | size_t thresholdCount, const std::string& label); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 171 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 172 | void updateThresholds(Sensor* sensor); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 173 | // returns false if a critical threshold has been crossed, true otherwise |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 174 | bool checkThresholds(Sensor* sensor); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 175 | void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 176 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 177 | } // namespace thresholds |