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 | { |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 16 | enum class Level |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 17 | { |
| 18 | WARNING, |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 19 | CRITICAL, |
Jayashree Dhanapal | 091c92c | 2022-01-11 14:55:20 +0530 | [diff] [blame^] | 20 | SOFTSHUTDOWN, |
| 21 | HARDSHUTDOWN, |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 22 | ERROR |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | }; |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 24 | enum class Direction |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | { |
| 26 | HIGH, |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 27 | LOW, |
| 28 | ERROR |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | }; |
| 30 | struct Threshold |
| 31 | { |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 32 | Threshold( |
| 33 | const Level& lev, const Direction& dir, const double& val, |
| 34 | const double hysteresis = std::numeric_limits<double>::quiet_NaN(), |
| 35 | bool write = true) : |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 36 | level(lev), |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 37 | direction(dir), value(val), hysteresis(hysteresis), writeable(write) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 38 | {} |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | Level level; |
| 40 | Direction direction; |
| 41 | double value; |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 42 | double hysteresis; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 43 | bool writeable; |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 44 | |
| 45 | bool operator==(const Threshold& rhs) const |
| 46 | { |
| 47 | return (level == rhs.level && direction == rhs.direction && |
| 48 | value == rhs.value); |
| 49 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 52 | void assertThresholds(Sensor* sensor, double assertValue, |
| 53 | thresholds::Level level, thresholds::Direction direction, |
| 54 | bool assert); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 55 | |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 56 | struct TimerUsed |
| 57 | { |
| 58 | bool used; |
| 59 | Level level; |
| 60 | Direction direction; |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 61 | bool assert; |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | using TimerPair = std::pair<struct TimerUsed, boost::asio::deadline_timer>; |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 65 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 66 | struct ThresholdTimer |
| 67 | { |
| 68 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 69 | ThresholdTimer(boost::asio::io_service& ioService) : io(ioService) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 70 | {} |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 71 | |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 72 | bool hasActiveTimer(const Threshold& threshold, bool assert) |
| 73 | { |
| 74 | for (TimerPair& timer : timers) |
| 75 | { |
| 76 | if (timer.first.used) |
| 77 | { |
| 78 | if ((timer.first.level == threshold.level) && |
| 79 | (timer.first.direction == threshold.direction) && |
| 80 | (timer.first.assert == assert)) |
| 81 | { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | void stopTimer(const Threshold& threshold, bool assert) |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 90 | { |
| 91 | struct TimerUsed timerUsed = {}; |
| 92 | for (TimerPair& timer : timers) |
| 93 | { |
| 94 | timerUsed = timer.first; |
| 95 | if (timerUsed.used) |
| 96 | { |
| 97 | if ((timerUsed.level == threshold.level) && |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 98 | (timerUsed.direction == threshold.direction) && |
| 99 | (timerUsed.assert == assert)) |
Yong Li | 10306bd | 2020-04-21 16:08:28 +0800 | [diff] [blame] | 100 | { |
| 101 | timer.second.cancel(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 107 | void startTimer(const std::weak_ptr<Sensor>& weakSensor, |
| 108 | const Threshold& threshold, bool assert, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 109 | double assertValue); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 110 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 111 | boost::asio::io_service& io; |
| 112 | std::list<TimerPair> timers; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 115 | bool findOrder(Level lev, Direction dir); |
| 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 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 122 | bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds, |
| 123 | const std::string& inputPath, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 124 | const double& scaleFactor, |
| 125 | const double& offset = 0); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 126 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 127 | std::string getInterface(const Level level); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 128 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 129 | void persistThreshold(const std::string& baseInterface, const std::string& path, |
| 130 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 131 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 132 | size_t thresholdCount, const std::string& label); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 133 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 134 | void updateThresholds(Sensor* sensor); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 135 | // returns false if a critical threshold has been crossed, true otherwise |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 136 | bool checkThresholds(Sensor* sensor); |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 137 | void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor, |
| 138 | ThresholdTimer& thresholdTimer); |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 139 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 140 | } // namespace thresholds |