| 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 |  | 
| Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 103 | // The common scheme for sysfs files naming is: <type><number>_<item>. | 
 | 104 | // This function returns optionally these 3 elements as a tuple. | 
 | 105 | std::optional<std::tuple<std::string, std::string, std::string>> | 
 | 106 |     splitFileName(const std::filesystem::path& filePath); | 
 | 107 |  | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 108 | bool parseThresholdsFromConfig( | 
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 109 |     const SensorData& sensorData, | 
 | 110 |     std::vector<thresholds::Threshold>& thresholdVector, | 
 | 111 |     const std::string* matchLabel = nullptr); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 112 |  | 
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 113 | bool parseThresholdsFromAttr(std::vector<thresholds::Threshold>& thresholds, | 
 | 114 |                              const std::string& inputPath, | 
| Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 115 |                              const double& scaleFactor, | 
 | 116 |                              const double& offset = 0); | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 117 | bool hasCriticalInterface( | 
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 118 |     const std::vector<thresholds::Threshold>& thresholdVector); | 
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 119 |  | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 120 | bool hasWarningInterface( | 
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 121 |     const std::vector<thresholds::Threshold>& thresholdVector); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 122 |  | 
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 123 | void persistThreshold(const std::string& baseInterface, const std::string& path, | 
 | 124 |                       const thresholds::Threshold& threshold, | 
| James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 125 |                       std::shared_ptr<sdbusplus::asio::connection>& conn, | 
| Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 126 |                       size_t thresholdCount, const std::string& label); | 
| James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 127 |  | 
| Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 128 | void updateThresholds(Sensor* sensor); | 
| James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 129 | // returns false if a critical threshold has been crossed, true otherwise | 
| James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 130 | bool checkThresholds(Sensor* sensor); | 
| James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 131 | void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer); | 
| Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 132 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 133 | } // namespace thresholds |