James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 2 | #include "Utils.hpp" |
| 3 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 4 | #include <boost/asio/io_service.hpp> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame^] | 5 | #include <list> |
| 6 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 7 | #include <nlohmann/json.hpp> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame^] | 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 11 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 12 | struct Sensor; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 13 | namespace thresholds |
| 14 | { |
| 15 | enum Level |
| 16 | { |
| 17 | WARNING, |
| 18 | CRITICAL |
| 19 | }; |
| 20 | enum Direction |
| 21 | { |
| 22 | HIGH, |
| 23 | LOW |
| 24 | }; |
| 25 | struct Threshold |
| 26 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 27 | Threshold(const Level& lev, const Direction& dir, const double& val, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | bool write = true) : |
| 29 | level(lev), |
| 30 | direction(dir), value(val), writeable(write) |
| 31 | { |
| 32 | } |
| 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 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 45 | void assertThresholds(Sensor* sensor, thresholds::Level level, |
| 46 | thresholds::Direction direction, bool assert); |
| 47 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 48 | using TimerPair = std::pair<bool, boost::asio::deadline_timer>; |
| 49 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 50 | struct ThresholdTimer |
| 51 | { |
| 52 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 53 | ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) : |
| 54 | io(ioService), sensor(sensor) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | |
| 58 | void startTimer(const Threshold& threshold) |
| 59 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 60 | constexpr const size_t waitTime = 5; |
| 61 | TimerPair* pair = nullptr; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 62 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 63 | for (TimerPair& timer : timers) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 64 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 65 | if (!timer.first) |
| 66 | { |
| 67 | pair = &timer; |
| 68 | break; |
| 69 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 70 | } |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 71 | if (pair == nullptr) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 72 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 73 | pair = &timers.emplace_back(false, boost::asio::deadline_timer(io)); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 74 | } |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 75 | pair->first = true; |
| 76 | pair->second.expires_from_now(boost::posix_time::seconds(waitTime)); |
| 77 | pair->second.async_wait( |
| 78 | [this, pair, threshold](boost::system::error_code ec) { |
| 79 | pair->first = false; |
| 80 | |
| 81 | if (ec == boost::asio::error::operation_aborted) |
| 82 | { |
| 83 | return; // we're being canceled |
| 84 | } |
| 85 | else if (ec) |
| 86 | { |
| 87 | std::cerr << "timer error: " << ec.message() << "\n"; |
| 88 | return; |
| 89 | } |
| 90 | if (isPowerOn()) |
| 91 | { |
| 92 | assertThresholds(sensor, threshold.level, |
| 93 | threshold.direction, true); |
| 94 | } |
| 95 | }); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 96 | } |
| 97 | |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 98 | boost::asio::io_service& io; |
| 99 | std::list<TimerPair> timers; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 100 | Sensor* sensor; |
| 101 | }; |
| 102 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 103 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 104 | const SensorData& sensorData, |
| 105 | std::vector<thresholds::Threshold>& thresholdVector, |
| 106 | const std::string* matchLabel = nullptr); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 107 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 108 | bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds, |
| 109 | const std::string& inputPath, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 110 | const double& scaleFactor, |
| 111 | const double& offset = 0); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 112 | bool hasCriticalInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 113 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 114 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 115 | bool hasWarningInterface( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 116 | const std::vector<thresholds::Threshold>& thresholdVector); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 118 | void persistThreshold(const std::string& baseInterface, const std::string& path, |
| 119 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 120 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 121 | size_t thresholdCount); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 122 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 123 | void updateThresholds(Sensor* sensor); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 124 | // returns false if a critical threshold has been crossed, true otherwise |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 125 | bool checkThresholds(Sensor* sensor); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 126 | void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | } // namespace thresholds |