Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 1 | #include "Thresholds.hpp" |
| 2 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 3 | #include "Utils.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 4 | #include "VariantVisitors.hpp" |
| 5 | #include "sensor.hpp" |
| 6 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 7 | #include <boost/algorithm/string/replace.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 8 | #include <boost/asio/error.hpp> |
| 9 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 10 | #include <boost/container/flat_map.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 11 | #include <sdbusplus/asio/connection.hpp> |
| 12 | #include <sdbusplus/asio/object_server.hpp> |
| 13 | #include <sdbusplus/exception.hpp> |
| 14 | #include <sdbusplus/message.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 15 | |
| 16 | #include <array> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 17 | #include <chrono> |
| 18 | #include <cstddef> |
| 19 | #include <cstdint> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | #include <iostream> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 21 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 22 | #include <memory> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 23 | #include <string> |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 24 | #include <tuple> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 25 | #include <utility> |
| 26 | #include <variant> |
| 27 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 29 | static constexpr bool debug = false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 30 | namespace thresholds |
| 31 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 32 | Level findThresholdLevel(uint8_t sev) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 34 | for (const ThresholdDefinition& prop : thresProp) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 36 | if (prop.sevOrder == sev) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | { |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 38 | return prop.level; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 41 | return Level::ERROR; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 44 | Direction findThresholdDirection(const std::string& direct) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 45 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 46 | if (direct == "greater than") |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 47 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 48 | return Direction::HIGH; |
| 49 | } |
| 50 | if (direct == "less than") |
| 51 | { |
| 52 | return Direction::LOW; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 53 | } |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 54 | return Direction::ERROR; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 57 | bool parseThresholdsFromConfig( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 58 | const SensorData& sensorData, |
| 59 | std::vector<thresholds::Threshold>& thresholdVector, |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 60 | const std::string* matchLabel, const int* sensorIndex) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 61 | { |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 62 | for (const auto& [intf, cfg] : sensorData) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | { |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 64 | if (intf.find("Thresholds") == std::string::npos) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | { |
| 66 | continue; |
| 67 | } |
| 68 | if (matchLabel != nullptr) |
| 69 | { |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 70 | auto labelFind = cfg.find("Label"); |
| 71 | if (labelFind == cfg.end()) |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 72 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 73 | continue; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 74 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 75 | if (std::visit(VariantToStringVisitor(), labelFind->second) != |
| 76 | *matchLabel) |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 77 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 78 | continue; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 79 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | } |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 81 | |
| 82 | if (sensorIndex != nullptr) |
| 83 | { |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 84 | auto indexFind = cfg.find("Index"); |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 85 | |
| 86 | // If we're checking for index 1, a missing Index is OK. |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 87 | if ((indexFind == cfg.end()) && (*sensorIndex != 1)) |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 88 | { |
| 89 | continue; |
| 90 | } |
| 91 | |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 92 | if ((indexFind != cfg.end()) && |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 93 | (std::visit(VariantToIntVisitor(), indexFind->second) != |
| 94 | *sensorIndex)) |
| 95 | { |
| 96 | continue; |
| 97 | } |
| 98 | } |
| 99 | |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 100 | double hysteresis = std::numeric_limits<double>::quiet_NaN(); |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 101 | auto hysteresisFind = cfg.find("Hysteresis"); |
| 102 | if (hysteresisFind != cfg.end()) |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 103 | { |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 104 | hysteresis = std::visit(VariantToDoubleVisitor(), |
| 105 | hysteresisFind->second); |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 106 | } |
| 107 | |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 108 | auto directionFind = cfg.find("Direction"); |
| 109 | auto severityFind = cfg.find("Severity"); |
| 110 | auto valueFind = cfg.find("Value"); |
| 111 | if (valueFind == cfg.end() || severityFind == cfg.end() || |
| 112 | directionFind == cfg.end()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | { |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 114 | std::cerr << "Malformed threshold on configuration interface " |
Zev Weiss | f783c69 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 115 | << intf << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 118 | unsigned int severity = std::visit(VariantToUnsignedIntVisitor(), |
| 119 | severityFind->second); |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 120 | |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 121 | std::string directions = std::visit(VariantToStringVisitor(), |
| 122 | directionFind->second); |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 123 | |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 124 | Level level = findThresholdLevel(severity); |
| 125 | Direction direction = findThresholdDirection(directions); |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 126 | |
Jayashree Dhanapal | 091c92c | 2022-01-11 14:55:20 +0530 | [diff] [blame] | 127 | if ((level == Level::ERROR) || (direction == Direction::ERROR)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 128 | { |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 129 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 130 | } |
James Feist | 13f340b | 2019-03-07 16:36:11 -0800 | [diff] [blame] | 131 | double val = std::visit(VariantToDoubleVisitor(), valueFind->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 132 | |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 133 | thresholdVector.emplace_back(level, direction, val, hysteresis); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 138 | void persistThreshold(const std::string& path, const std::string& baseInterface, |
| 139 | const thresholds::Threshold& threshold, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 140 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 141 | size_t thresholdCount, const std::string& labelMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 142 | { |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 143 | for (size_t ii = 0; ii < thresholdCount; ii++) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 144 | { |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 145 | std::string thresholdInterface = baseInterface + ".Thresholds" + |
| 146 | std::to_string(ii); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | conn->async_method_call( |
Zev Weiss | afd1504 | 2022-07-18 12:28:40 -0700 | [diff] [blame] | 148 | [&, path, threshold, thresholdInterface, |
| 149 | labelMatch](const boost::system::error_code& ec, |
| 150 | const SensorBaseConfigMap& result) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 151 | if (ec) |
| 152 | { |
| 153 | return; // threshold not supported |
| 154 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 155 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 156 | if (!labelMatch.empty()) |
| 157 | { |
| 158 | auto labelFind = result.find("Label"); |
| 159 | if (labelFind == result.end()) |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 160 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 161 | std::cerr << "No label in threshold configuration\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 162 | return; |
| 163 | } |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 164 | std::string label = std::visit(VariantToStringVisitor(), |
| 165 | labelFind->second); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 166 | if (label != labelMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 167 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 168 | return; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 169 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 170 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 171 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 172 | auto directionFind = result.find("Direction"); |
| 173 | auto severityFind = result.find("Severity"); |
| 174 | auto valueFind = result.find("Value"); |
| 175 | if (valueFind == result.end() || severityFind == result.end() || |
| 176 | directionFind == result.end()) |
| 177 | { |
| 178 | std::cerr << "Malformed threshold in configuration\n"; |
| 179 | return; |
| 180 | } |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 181 | unsigned int severity = std::visit(VariantToUnsignedIntVisitor(), |
| 182 | severityFind->second); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 183 | |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 184 | std::string dir = std::visit(VariantToStringVisitor(), |
| 185 | directionFind->second); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 186 | if ((findThresholdLevel(severity) != threshold.level) || |
| 187 | (findThresholdDirection(dir) != threshold.direction)) |
| 188 | { |
| 189 | return; // not the droid we're looking for |
| 190 | } |
| 191 | |
| 192 | std::variant<double> value(threshold.value); |
| 193 | conn->async_method_call( |
| 194 | [](const boost::system::error_code& ec) { |
| 195 | if (ec) |
| 196 | { |
| 197 | std::cerr << "Error setting threshold " << ec << "\n"; |
| 198 | } |
Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 199 | }, |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 200 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
| 201 | "Set", thresholdInterface, "Value", value); |
Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 202 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 203 | entityManagerName, path, "org.freedesktop.DBus.Properties", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 204 | "GetAll", thresholdInterface); |
| 205 | } |
| 206 | } |
| 207 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 208 | void updateThresholds(Sensor* sensor) |
| 209 | { |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 210 | for (const auto& threshold : sensor->thresholds) |
| 211 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 212 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 213 | sensor->getThresholdInterface(threshold.level); |
| 214 | |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 215 | if (!interface) |
| 216 | { |
| 217 | continue; |
| 218 | } |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 219 | |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 220 | std::string property = Sensor::propertyLevel(threshold.level, |
| 221 | threshold.direction); |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 222 | if (property.empty()) |
| 223 | { |
| 224 | continue; |
| 225 | } |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 226 | interface->set_property(property, threshold.value); |
| 227 | } |
| 228 | } |
| 229 | |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 230 | // Debugging counters |
| 231 | static int cHiTrue = 0; |
| 232 | static int cHiFalse = 0; |
| 233 | static int cHiMidstate = 0; |
| 234 | static int cLoTrue = 0; |
| 235 | static int cLoFalse = 0; |
| 236 | static int cLoMidstate = 0; |
| 237 | static int cDebugThrottle = 0; |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 238 | static constexpr int assertLogCount = 10; |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 239 | |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 240 | struct ChangeParam |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 241 | { |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 242 | ChangeParam(Threshold whichThreshold, bool status, double value) : |
| 243 | threshold(whichThreshold), asserted(status), assertValue(value) |
| 244 | {} |
| 245 | |
| 246 | Threshold threshold; |
| 247 | bool asserted; |
| 248 | double assertValue; |
| 249 | }; |
| 250 | |
| 251 | static std::vector<ChangeParam> checkThresholds(Sensor* sensor, double value) |
| 252 | { |
| 253 | std::vector<ChangeParam> thresholdChanges; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 254 | if (sensor->thresholds.empty()) |
| 255 | { |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 256 | return thresholdChanges; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 257 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 258 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 259 | for (auto& threshold : sensor->thresholds) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 260 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 261 | // Use "Schmitt trigger" logic to avoid threshold trigger spam, |
| 262 | // if value is noisy while hovering very close to a threshold. |
| 263 | // When a threshold is crossed, indicate true immediately, |
| 264 | // but require more distance to be crossed the other direction, |
| 265 | // before resetting the indicator back to false. |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 266 | if (threshold.direction == thresholds::Direction::HIGH) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 267 | { |
James Feist | 551087a | 2019-12-09 11:17:12 -0800 | [diff] [blame] | 268 | if (value >= threshold.value) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 269 | { |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 270 | thresholdChanges.emplace_back(threshold, true, value); |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 271 | if (++cHiTrue < assertLogCount) |
| 272 | { |
| 273 | std::cerr << "Sensor " << sensor->name << " high threshold " |
| 274 | << threshold.value << " assert: value " << value |
| 275 | << " raw data " << sensor->rawValue << "\n"; |
| 276 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 277 | } |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 278 | else if (value < (threshold.value - threshold.hysteresis)) |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 279 | { |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 280 | thresholdChanges.emplace_back(threshold, false, value); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 281 | ++cHiFalse; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 282 | } |
Patrick Venture | 66235d4 | 2019-10-11 08:31:27 -0700 | [diff] [blame] | 283 | else |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 284 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 285 | ++cHiMidstate; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 288 | else if (threshold.direction == thresholds::Direction::LOW) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 289 | { |
James Feist | 551087a | 2019-12-09 11:17:12 -0800 | [diff] [blame] | 290 | if (value <= threshold.value) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 291 | { |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 292 | thresholdChanges.emplace_back(threshold, true, value); |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 293 | if (++cLoTrue < assertLogCount) |
| 294 | { |
| 295 | std::cerr << "Sensor " << sensor->name << " low threshold " |
| 296 | << threshold.value << " assert: value " |
| 297 | << sensor->value << " raw data " |
| 298 | << sensor->rawValue << "\n"; |
| 299 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 300 | } |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame] | 301 | else if (value > (threshold.value + threshold.hysteresis)) |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 302 | { |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 303 | thresholdChanges.emplace_back(threshold, false, value); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 304 | ++cLoFalse; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 305 | } |
Patrick Venture | 66235d4 | 2019-10-11 08:31:27 -0700 | [diff] [blame] | 306 | else |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 307 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 308 | ++cLoMidstate; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 311 | else |
| 312 | { |
| 313 | std::cerr << "Error determining threshold direction\n"; |
| 314 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 315 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 316 | |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 317 | // Throttle debug output, so that it does not continuously spam |
| 318 | ++cDebugThrottle; |
| 319 | if (cDebugThrottle >= 1000) |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 320 | { |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 321 | cDebugThrottle = 0; |
| 322 | if constexpr (debug) |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 323 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 324 | std::cerr << "checkThresholds: High T=" << cHiTrue |
| 325 | << " F=" << cHiFalse << " M=" << cHiMidstate |
| 326 | << ", Low T=" << cLoTrue << " F=" << cLoFalse |
| 327 | << " M=" << cLoMidstate << "\n"; |
| 328 | } |
| 329 | } |
| 330 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 331 | return thresholdChanges; |
| 332 | } |
| 333 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 334 | void ThresholdTimer::startTimer(const std::weak_ptr<Sensor>& weakSensor, |
| 335 | const Threshold& threshold, bool assert, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 336 | double assertValue) |
| 337 | { |
| 338 | struct TimerUsed timerUsed = {}; |
| 339 | constexpr const size_t waitTime = 5; |
| 340 | TimerPair* pair = nullptr; |
| 341 | |
| 342 | for (TimerPair& timer : timers) |
| 343 | { |
| 344 | if (!timer.first.used) |
| 345 | { |
| 346 | pair = &timer; |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | if (pair == nullptr) |
| 351 | { |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 352 | pair = &timers.emplace_back(timerUsed, boost::asio::steady_timer(io)); |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | pair->first.used = true; |
| 356 | pair->first.level = threshold.level; |
| 357 | pair->first.direction = threshold.direction; |
| 358 | pair->first.assert = assert; |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 359 | pair->second.expires_after(std::chrono::seconds(waitTime)); |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 360 | pair->second.async_wait([weakSensor, pair, threshold, assert, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 361 | assertValue](boost::system::error_code ec) { |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 362 | auto sensorPtr = weakSensor.lock(); |
| 363 | if (!sensorPtr) |
| 364 | { |
| 365 | return; // owner sensor has been destructed |
| 366 | } |
| 367 | // pair is valid as long as sensor is valid |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 368 | pair->first.used = false; |
| 369 | |
| 370 | if (ec == boost::asio::error::operation_aborted) |
| 371 | { |
| 372 | return; // we're being canceled |
| 373 | } |
| 374 | if (ec) |
| 375 | { |
| 376 | std::cerr << "timer error: " << ec.message() << "\n"; |
| 377 | return; |
| 378 | } |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 379 | if (sensorPtr->readingStateGood()) |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 380 | { |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 381 | assertThresholds(sensorPtr.get(), assertValue, threshold.level, |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 382 | threshold.direction, assert); |
| 383 | } |
| 384 | }); |
| 385 | } |
| 386 | |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 387 | bool checkThresholds(Sensor* sensor) |
| 388 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 389 | bool status = true; |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 390 | std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value); |
| 391 | for (const auto& change : changes) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 392 | { |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 393 | assertThresholds(sensor, change.assertValue, change.threshold.level, |
| 394 | change.threshold.direction, change.asserted); |
| 395 | if (change.threshold.level == thresholds::Level::CRITICAL && |
| 396 | change.asserted) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 397 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 398 | status = false; |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 402 | return status; |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 405 | void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor, |
| 406 | ThresholdTimer& thresholdTimer) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 407 | { |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 408 | auto sensorPtr = weakSensor.lock(); |
| 409 | if (!sensorPtr) |
| 410 | { |
| 411 | return; // sensor is destructed, should never be here |
| 412 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 413 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 414 | Sensor* sensor = sensorPtr.get(); |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 415 | std::vector<ChangeParam> changes = checkThresholds(sensor, sensor->value); |
| 416 | for (const auto& change : changes) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 417 | { |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 418 | // When CPU is powered off, some volatges are expected to |
| 419 | // go below low thresholds. Filter these events with thresholdTimer. |
| 420 | // 1. always delay the assertion of low events to see if they are |
| 421 | // caused by power off event. |
| 422 | // 2. conditional delay the de-assertion of low events if there is |
| 423 | // an existing timer for assertion. |
| 424 | // 3. no delays for de-assert of low events if there is an existing |
| 425 | // de-assert for low event. This means 2nd de-assert would happen |
| 426 | // first and when timer expires for the previous one, no additional |
| 427 | // signal will be logged. |
| 428 | // 4. no delays for all high events. |
| 429 | if (change.threshold.direction == thresholds::Direction::LOW) |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 430 | { |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 431 | if (change.asserted || thresholdTimer.hasActiveTimer( |
| 432 | change.threshold, !change.asserted)) |
| 433 | { |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 434 | thresholdTimer.startTimer(weakSensor, change.threshold, |
| 435 | change.asserted, change.assertValue); |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 436 | continue; |
| 437 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 438 | } |
Zhikui Ren | bf7cbc8 | 2020-07-02 08:44:00 -0700 | [diff] [blame] | 439 | assertThresholds(sensor, change.assertValue, change.threshold.level, |
| 440 | change.threshold.direction, change.asserted); |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 444 | void assertThresholds(Sensor* sensor, double assertValue, |
| 445 | thresholds::Level level, thresholds::Direction direction, |
| 446 | bool assert) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 447 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 448 | std::shared_ptr<sdbusplus::asio::dbus_interface> interface = |
| 449 | sensor->getThresholdInterface(level); |
| 450 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 451 | if (!interface) |
| 452 | { |
| 453 | std::cout << "trying to set uninitialized interface\n"; |
| 454 | return; |
| 455 | } |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 456 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 457 | std::string property = Sensor::propertyAlarm(level, direction); |
Jayashree Dhanapal | 45f2702 | 2021-12-07 12:56:35 +0530 | [diff] [blame] | 458 | if (property.empty()) |
| 459 | { |
| 460 | std::cout << "Alarm property is empty \n"; |
| 461 | return; |
| 462 | } |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 463 | if (interface->set_property<bool, true>(property, assert)) |
| 464 | { |
| 465 | try |
| 466 | { |
| 467 | // msg.get_path() is interface->get_object_path() |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 468 | sdbusplus::message_t msg = |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 469 | interface->new_signal("ThresholdAsserted"); |
| 470 | |
| 471 | msg.append(sensor->name, interface->get_interface_name(), property, |
| 472 | assert, assertValue); |
| 473 | msg.signal_send(); |
| 474 | } |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 475 | catch (const sdbusplus::exception_t& e) |
Zhikui Ren | 59b8b9e | 2020-06-26 18:34:22 -0700 | [diff] [blame] | 476 | { |
| 477 | std::cerr |
| 478 | << "Failed to send thresholdAsserted signal with assertValue\n"; |
| 479 | } |
| 480 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 483 | bool parseThresholdsFromAttr( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 484 | std::vector<thresholds::Threshold>& thresholdVector, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 485 | const std::string& inputPath, const double& scaleFactor, |
Chris Sides | a327923 | 2023-04-24 16:08:13 -0500 | [diff] [blame] | 486 | const double& offset, const double& hysteresis) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 487 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 488 | const boost::container::flat_map< |
| 489 | std::string, std::vector<std::tuple<const char*, thresholds::Level, |
| 490 | thresholds::Direction, double>>> |
| 491 | map = { |
| 492 | {"average", |
| 493 | { |
| 494 | std::make_tuple("average_min", Level::WARNING, Direction::LOW, |
| 495 | 0.0), |
| 496 | std::make_tuple("average_max", Level::WARNING, Direction::HIGH, |
| 497 | 0.0), |
| 498 | }}, |
| 499 | {"input", |
| 500 | { |
| 501 | std::make_tuple("min", Level::WARNING, Direction::LOW, 0.0), |
| 502 | std::make_tuple("max", Level::WARNING, Direction::HIGH, 0.0), |
| 503 | std::make_tuple("lcrit", Level::CRITICAL, Direction::LOW, 0.0), |
| 504 | std::make_tuple("crit", Level::CRITICAL, Direction::HIGH, |
| 505 | offset), |
| 506 | }}, |
| 507 | }; |
| 508 | |
| 509 | if (auto fileParts = splitFileName(inputPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 510 | { |
Zbigniew Kurzynski | dbfd466 | 2020-09-28 18:06:00 +0200 | [diff] [blame] | 511 | auto& [type, nr, item] = *fileParts; |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 512 | if (map.count(item) != 0) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 513 | { |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 514 | for (const auto& t : map.at(item)) |
| 515 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 516 | const auto& [suffix, level, direction, offset] = t; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 517 | auto attrPath = boost::replace_all_copy(inputPath, item, |
| 518 | suffix); |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 519 | if (auto val = readFile(attrPath, scaleFactor)) |
| 520 | { |
| 521 | *val += offset; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 522 | if (debug) |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 523 | { |
| 524 | std::cout << "Threshold: " << attrPath << ": " << *val |
| 525 | << "\n"; |
| 526 | } |
Chris Sides | a327923 | 2023-04-24 16:08:13 -0500 | [diff] [blame] | 527 | thresholdVector.emplace_back(level, direction, *val, |
| 528 | hysteresis); |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 529 | } |
| 530 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 531 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 532 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 533 | return true; |
| 534 | } |
| 535 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 536 | std::string getInterface(const Level thresholdLevel) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 537 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 538 | for (const ThresholdDefinition& thresh : thresProp) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 539 | { |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 540 | if (thresh.level == thresholdLevel) |
| 541 | { |
| 542 | return std::string("xyz.openbmc_project.Sensor.Threshold.") + |
| 543 | thresh.levelName; |
| 544 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 545 | } |
Ed Tanous | c8fed20 | 2022-01-12 14:24:45 -0800 | [diff] [blame] | 546 | return ""; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 547 | } |
| 548 | } // namespace thresholds |