James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 2 | #include <Utils.hpp> |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 3 | #include <boost/asio/deadline_timer.hpp> |
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 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame^] | 62 | ThresholdTimer(boost::asio::io_service& ioService) : io(ioService) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 63 | {} |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 64 | |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 65 | bool hasActiveTimer(const Threshold& threshold, bool assert) |
| 66 | { |
| 67 | for (TimerPair& timer : timers) |
| 68 | { |
| 69 | if (timer.first.used) |
| 70 | { |
| 71 | if ((timer.first.level == threshold.level) && |
| 72 | (timer.first.direction == threshold.direction) && |
| 73 | (timer.first.assert == assert)) |
| 74 | { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | void stopTimer(const Threshold& threshold, bool assert) |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 83 | { |
| 84 | struct TimerUsed timerUsed = {}; |
| 85 | for (TimerPair& timer : timers) |
| 86 | { |
| 87 | timerUsed = timer.first; |
| 88 | if (timerUsed.used) |
| 89 | { |
| 90 | if ((timerUsed.level == threshold.level) && |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 91 | (timerUsed.direction == threshold.direction) && |
| 92 | (timerUsed.assert == assert)) |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 93 | { |
| 94 | timer.second.cancel(); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame^] | 100 | void startTimer(const std::weak_ptr<Sensor>& weakSensor, |
| 101 | const Threshold& threshold, bool assert, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 102 | double assertValue); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 103 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 104 | boost::asio::io_service& io; |
| 105 | std::list<TimerPair> timers; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 108 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 109 | const SensorData& sensorData, |
| 110 | std::vector<thresholds::Threshold>& thresholdVector, |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 111 | const std::string* matchLabel = nullptr, const int* sensorIndex = nullptr); |
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 | bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds, |
| 114 | const std::string& inputPath, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 115 | const double& scaleFactor, |
| 116 | const double& offset = 0); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 117 | bool hasCriticalInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 118 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 119 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 120 | bool hasWarningInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 121 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 122 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 123 | void persistThreshold(const std::string& baseInterface, const std::string& path, |
| 124 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 125 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 126 | size_t thresholdCount, const std::string& label); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 127 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 128 | void updateThresholds(Sensor* sensor); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 129 | // returns false if a critical threshold has been crossed, true otherwise |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 130 | bool checkThresholds(Sensor* sensor); |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame^] | 131 | void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor, |
| 132 | ThresholdTimer& thresholdTimer); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 133 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | } // namespace thresholds |