James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 2 | |
| 3 | #include "Utils.hpp" |
| 4 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 5 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 6 | #include <boost/asio/steady_timer.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 7 | #include <nlohmann/json.hpp> |
| 8 | |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 9 | #include <list> |
| 10 | #include <memory> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <utility> |
| 13 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 14 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 15 | struct Sensor; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 16 | namespace thresholds |
| 17 | { |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 18 | enum class Level |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | { |
| 20 | WARNING, |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 21 | CRITICAL, |
Rashmica Gupta | 8d0fd3c | 2021-11-30 16:07:14 +1100 | [diff] [blame] | 22 | PERFORMANCELOSS, |
Jayashree Dhanapal | 091c92c | 2022-01-11 14:55:20 +0530 | [diff] [blame] | 23 | SOFTSHUTDOWN, |
| 24 | HARDSHUTDOWN, |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 25 | ERROR |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 26 | }; |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 27 | enum class Direction |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | { |
| 29 | HIGH, |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 30 | LOW, |
| 31 | ERROR |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | }; |
| 33 | struct Threshold |
| 34 | { |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 35 | Threshold( |
| 36 | const Level& lev, const Direction& dir, const double& val, |
| 37 | const double hysteresis = std::numeric_limits<double>::quiet_NaN(), |
| 38 | bool write = true) : |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 39 | level(lev), direction(dir), value(val), hysteresis(hysteresis), |
| 40 | writeable(write) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 41 | {} |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | Level level; |
| 43 | Direction direction; |
| 44 | double value; |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 45 | double hysteresis; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | bool writeable; |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 47 | |
| 48 | bool operator==(const Threshold& rhs) const |
| 49 | { |
| 50 | return (level == rhs.level && direction == rhs.direction && |
| 51 | value == rhs.value); |
| 52 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 55 | void assertThresholds(Sensor* sensor, double assertValue, |
| 56 | thresholds::Level level, thresholds::Direction direction, |
| 57 | bool assert); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 58 | |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 59 | struct TimerUsed |
| 60 | { |
| 61 | bool used; |
| 62 | Level level; |
| 63 | Direction direction; |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 64 | bool assert; |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 65 | }; |
| 66 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 67 | using TimerPair = std::pair<struct TimerUsed, boost::asio::steady_timer>; |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 68 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 69 | struct ThresholdTimer |
| 70 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 71 | explicit ThresholdTimer(boost::asio::io_context& ioService) : io(ioService) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 72 | {} |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 73 | |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 74 | bool hasActiveTimer(const Threshold& threshold, bool assert) |
| 75 | { |
| 76 | for (TimerPair& timer : timers) |
| 77 | { |
| 78 | if (timer.first.used) |
| 79 | { |
| 80 | if ((timer.first.level == threshold.level) && |
| 81 | (timer.first.direction == threshold.direction) && |
| 82 | (timer.first.assert == assert)) |
| 83 | { |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | void stopTimer(const Threshold& threshold, bool assert) |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 92 | { |
| 93 | struct TimerUsed timerUsed = {}; |
| 94 | for (TimerPair& timer : timers) |
| 95 | { |
| 96 | timerUsed = timer.first; |
| 97 | if (timerUsed.used) |
| 98 | { |
| 99 | if ((timerUsed.level == threshold.level) && |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 100 | (timerUsed.direction == threshold.direction) && |
| 101 | (timerUsed.assert == assert)) |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 102 | { |
| 103 | timer.second.cancel(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 109 | void startTimer(const std::weak_ptr<Sensor>& weakSensor, |
| 110 | const Threshold& threshold, bool assert, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 111 | double assertValue); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 112 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 113 | boost::asio::io_context& io; |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 114 | std::list<TimerPair> timers; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 117 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 118 | const SensorData& sensorData, |
| 119 | std::vector<thresholds::Threshold>& thresholdVector, |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 120 | const std::string* matchLabel = nullptr, const int* sensorIndex = nullptr); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 121 | |
Chris Sides | a327923 | 2023-04-24 16:08:13 -0500 | [diff] [blame] | 122 | // Sensors touched by parseThresholdFromAttr() are forcibly updated with given |
| 123 | // parameters, so callers are encouraged to specify a sane hysteresis value for |
| 124 | // their HW. For reference, the hysteresis fomula used in Sensor.hpp is: |
| 125 | // hysteresis.trigger = (max_val - min_val) * 0.01 |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 126 | bool parseThresholdsFromAttr( |
| 127 | std::vector<thresholds::Threshold>& thresholdVector, |
| 128 | const std::string& inputPath, const double& scaleFactor, |
Chris Sides | a327923 | 2023-04-24 16:08:13 -0500 | [diff] [blame] | 129 | const double& offset = 0, |
| 130 | const double& hysteresis = std::numeric_limits<double>::quiet_NaN()); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 131 | |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 132 | struct ThresholdDefinition |
| 133 | { |
| 134 | Level level; |
| 135 | uint8_t sevOrder; |
| 136 | const char* levelName; |
| 137 | }; |
| 138 | |
Rashmica Gupta | 8d0fd3c | 2021-11-30 16:07:14 +1100 | [diff] [blame] | 139 | constexpr static std::array<thresholds::ThresholdDefinition, 5> thresProp = { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 140 | {{Level::WARNING, 0, "Warning"}, |
| 141 | {Level::CRITICAL, 1, "Critical"}, |
Rashmica Gupta | 8d0fd3c | 2021-11-30 16:07:14 +1100 | [diff] [blame] | 142 | {Level::PERFORMANCELOSS, 2, "PerformanceLoss"}, |
| 143 | {Level::SOFTSHUTDOWN, 3, "SoftShutdown"}, |
| 144 | {Level::HARDSHUTDOWN, 4, "HardShutdown"}}}; |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 145 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 146 | std::string getInterface(Level level); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 148 | void persistThreshold(const std::string& path, const std::string& baseInterface, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 149 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 150 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 151 | size_t thresholdCount, const std::string& label); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 152 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 153 | void updateThresholds(Sensor* sensor); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 154 | // returns false if a critical threshold has been crossed, true otherwise |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 155 | bool checkThresholds(Sensor* sensor); |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 156 | void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor, |
| 157 | ThresholdTimer& thresholdTimer); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 158 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 159 | } // namespace thresholds |