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