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