James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 3 | #include "Thresholds.hpp" |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 4 | #include "Utils.hpp" |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 5 | |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 6 | #include <sdbusplus/asio/object_server.hpp> |
| 7 | |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 8 | #include <limits> |
| 9 | #include <memory> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 12 | |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 13 | constexpr size_t sensorFailedPollTimeMs = 5000; |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 14 | |
| 15 | constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 16 | constexpr const char* availableInterfaceName = |
| 17 | "xyz.openbmc_project.State.Decorator.Availability"; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 18 | constexpr const char* operationalInterfaceName = |
| 19 | "xyz.openbmc_project.State.Decorator.OperationalStatus"; |
| 20 | constexpr const size_t errorThreshold = 5; |
| 21 | |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 22 | struct Sensor |
| 23 | { |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 24 | Sensor(const std::string& name, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 25 | std::vector<thresholds::Threshold>&& thresholdData, |
| 26 | const std::string& configurationPath, const std::string& objectType, |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 27 | const double max, const double min, |
James Feist | e333852 | 2020-09-15 15:40:30 -0700 | [diff] [blame^] | 28 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 29 | PowerState readState = PowerState::always) : |
AppaRao Puli | 54ffb27 | 2020-06-02 19:09:38 +0530 | [diff] [blame] | 30 | name(std::regex_replace(name, std::regex("[^a-zA-Z0-9_/]+"), "_")), |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 31 | configurationPath(configurationPath), objectType(objectType), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 32 | maxValue(max), minValue(min), thresholds(std::move(thresholdData)), |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 33 | hysteresisTrigger((max - min) * 0.01), |
James Feist | e333852 | 2020-09-15 15:40:30 -0700 | [diff] [blame^] | 34 | hysteresisPublish((max - min) * 0.0001), dbusConnection(conn), |
| 35 | readState(readState), errCount(0) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 36 | {} |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 37 | virtual ~Sensor() = default; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 38 | virtual void checkThresholds(void) = 0; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 39 | std::string name; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 40 | std::string configurationPath; |
| 41 | std::string objectType; |
| 42 | double maxValue; |
| 43 | double minValue; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 44 | std::vector<thresholds::Threshold> thresholds; |
| 45 | std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface; |
| 46 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning; |
| 47 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical; |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 48 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 49 | std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 50 | std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 51 | double value = std::numeric_limits<double>::quiet_NaN(); |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 52 | double rawValue = std::numeric_limits<double>::quiet_NaN(); |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 53 | bool overriddenState = false; |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 54 | bool internalSet = false; |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 55 | double hysteresisTrigger; |
| 56 | double hysteresisPublish; |
James Feist | e333852 | 2020-09-15 15:40:30 -0700 | [diff] [blame^] | 57 | std::shared_ptr<sdbusplus::asio::connection> dbusConnection; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 58 | PowerState readState; |
| 59 | size_t errCount; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 60 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 61 | int setSensorValue(const double& newValue, double& oldValue) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 62 | { |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 63 | if (!internalSet) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 64 | { |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 65 | oldValue = newValue; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 66 | overriddenState = true; |
| 67 | // check thresholds for external set |
| 68 | value = newValue; |
| 69 | checkThresholds(); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 70 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 71 | else if (!overriddenState) |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 72 | { |
| 73 | oldValue = newValue; |
| 74 | } |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 75 | return 1; |
| 76 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 77 | |
| 78 | void |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 79 | setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 80 | const std::string label = std::string(), |
| 81 | size_t thresholdSize = 0) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 82 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 83 | if (readState == PowerState::on || readState == PowerState::biosPost) |
| 84 | { |
| 85 | setupPowerMatch(conn); |
| 86 | } |
| 87 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 88 | createAssociation(association, configurationPath); |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 89 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 90 | sensorInterface->register_property("MaxValue", maxValue); |
| 91 | sensorInterface->register_property("MinValue", minValue); |
| 92 | sensorInterface->register_property( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 93 | "Value", value, [&](const double& newValue, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 94 | return setSensorValue(newValue, oldValue); |
| 95 | }); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 96 | for (auto& threshold : thresholds) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 97 | { |
| 98 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 99 | std::string level; |
| 100 | std::string alarm; |
| 101 | if (threshold.level == thresholds::Level::CRITICAL) |
| 102 | { |
| 103 | iface = thresholdInterfaceCritical; |
| 104 | if (threshold.direction == thresholds::Direction::HIGH) |
| 105 | { |
| 106 | level = "CriticalHigh"; |
| 107 | alarm = "CriticalAlarmHigh"; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | level = "CriticalLow"; |
| 112 | alarm = "CriticalAlarmLow"; |
| 113 | } |
| 114 | } |
| 115 | else if (threshold.level == thresholds::Level::WARNING) |
| 116 | { |
| 117 | iface = thresholdInterfaceWarning; |
| 118 | if (threshold.direction == thresholds::Direction::HIGH) |
| 119 | { |
| 120 | level = "WarningHigh"; |
| 121 | alarm = "WarningAlarmHigh"; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | level = "WarningLow"; |
| 126 | alarm = "WarningAlarmLow"; |
| 127 | } |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | std::cerr << "Unknown threshold level" << threshold.level |
| 132 | << "\n"; |
| 133 | continue; |
| 134 | } |
| 135 | if (!iface) |
| 136 | { |
| 137 | std::cout << "trying to set uninitialized interface\n"; |
| 138 | continue; |
| 139 | } |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 140 | |
| 141 | size_t thresSize = |
| 142 | label.empty() ? thresholds.size() : thresholdSize; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 143 | iface->register_property( |
| 144 | level, threshold.value, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 145 | [&, label, thresSize](const double& request, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 146 | oldValue = request; // todo, just let the config do this? |
| 147 | threshold.value = request; |
| 148 | thresholds::persistThreshold(configurationPath, objectType, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 149 | threshold, conn, thresSize, |
| 150 | label); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 151 | // Invalidate previously remembered value, |
| 152 | // so new thresholds will be checked during next update, |
| 153 | // even if sensor reading remains unchanged. |
| 154 | value = std::numeric_limits<double>::quiet_NaN(); |
| 155 | |
| 156 | // Although tempting, don't call checkThresholds() from here |
| 157 | // directly. Let the regular sensor monitor call the same |
| 158 | // using updateValue(), which can check conditions like |
| 159 | // poweron, etc., before raising any event. |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 160 | return 1; |
| 161 | }); |
| 162 | iface->register_property(alarm, false); |
| 163 | } |
| 164 | if (!sensorInterface->initialize()) |
| 165 | { |
| 166 | std::cerr << "error initializing value interface\n"; |
| 167 | } |
| 168 | if (thresholdInterfaceWarning && |
Yong Li | f902c05 | 2020-05-07 17:13:53 +0800 | [diff] [blame] | 169 | !thresholdInterfaceWarning->initialize(true)) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 170 | { |
| 171 | std::cerr << "error initializing warning threshold interface\n"; |
| 172 | } |
| 173 | |
| 174 | if (thresholdInterfaceCritical && |
Yong Li | f902c05 | 2020-05-07 17:13:53 +0800 | [diff] [blame] | 175 | !thresholdInterfaceCritical->initialize(true)) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 176 | { |
| 177 | std::cerr << "error initializing critical threshold interface\n"; |
| 178 | } |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 179 | |
| 180 | if (!availableInterface) |
| 181 | { |
| 182 | availableInterface = |
| 183 | std::make_shared<sdbusplus::asio::dbus_interface>( |
| 184 | conn, sensorInterface->get_object_path(), |
| 185 | availableInterfaceName); |
| 186 | availableInterface->register_property( |
| 187 | "Available", true, [this](const bool propIn, bool& old) { |
| 188 | if (propIn == old) |
| 189 | { |
| 190 | return 1; |
| 191 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 192 | old = propIn; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 193 | if (!propIn) |
| 194 | { |
| 195 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 196 | } |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 197 | return 1; |
| 198 | }); |
| 199 | availableInterface->initialize(); |
| 200 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 201 | if (!operationalInterface) |
| 202 | { |
| 203 | operationalInterface = |
| 204 | std::make_shared<sdbusplus::asio::dbus_interface>( |
| 205 | conn, sensorInterface->get_object_path(), |
| 206 | operationalInterfaceName); |
| 207 | operationalInterface->register_property("Functional", true); |
| 208 | operationalInterface->initialize(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | bool readingStateGood() |
| 213 | { |
| 214 | if (readState == PowerState::on && !isPowerOn()) |
| 215 | { |
| 216 | return false; |
| 217 | } |
| 218 | if (readState == PowerState::biosPost && |
| 219 | (!hasBiosPost() || !isPowerOn())) |
| 220 | { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | void markFunctional(bool isFunctional) |
| 228 | { |
| 229 | if (operationalInterface) |
| 230 | { |
| 231 | operationalInterface->set_property("Functional", isFunctional); |
| 232 | } |
| 233 | if (isFunctional) |
| 234 | { |
| 235 | errCount = 0; |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void markAvailable(bool isAvailable) |
| 244 | { |
| 245 | if (availableInterface) |
| 246 | { |
| 247 | availableInterface->set_property("Available", isAvailable); |
| 248 | errCount = 0; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void incrementError() |
| 253 | { |
| 254 | if (!readingStateGood()) |
| 255 | { |
| 256 | markAvailable(false); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | if (errCount >= errorThreshold) |
| 261 | { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | errCount++; |
| 266 | if (errCount == errorThreshold) |
| 267 | { |
| 268 | std::cerr << "Sensor " << name << " reading error!\n"; |
| 269 | markFunctional(false); |
| 270 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 271 | } |
| 272 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 273 | void updateValue(const double& newValue) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 274 | { |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 275 | // Ignore if overriding is enabled |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 276 | if (overriddenState) |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 277 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 278 | return; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 279 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 280 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 281 | if (!readingStateGood()) |
| 282 | { |
| 283 | markAvailable(false); |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 284 | updateValueProperty(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 288 | updateValueProperty(newValue); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 289 | |
| 290 | // Always check thresholds after changing the value, |
| 291 | // as the test against hysteresisTrigger now takes place in |
| 292 | // the thresholds::checkThresholds() method, |
| 293 | // which is called by checkThresholds() below, |
| 294 | // in all current implementations of sensors that have thresholds. |
| 295 | checkThresholds(); |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 296 | if (!std::isnan(newValue)) |
| 297 | { |
| 298 | markFunctional(true); |
| 299 | markAvailable(true); |
| 300 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 301 | } |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 302 | |
| 303 | void updateProperty( |
| 304 | std::shared_ptr<sdbusplus::asio::dbus_interface>& interface, |
| 305 | double& oldValue, const double& newValue, const char* dbusPropertyName) |
| 306 | { |
| 307 | if (requiresUpdate(oldValue, newValue)) |
| 308 | { |
| 309 | oldValue = newValue; |
Jae Hyun Yoo | 1a540b8 | 2020-07-30 23:33:18 -0700 | [diff] [blame] | 310 | if (interface && |
| 311 | !(interface->set_property(dbusPropertyName, newValue))) |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 312 | { |
| 313 | std::cerr << "error setting property " << dbusPropertyName |
| 314 | << " to " << newValue << "\n"; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | bool requiresUpdate(const double& lVal, const double& rVal) |
| 320 | { |
| 321 | if (std::isnan(lVal) || std::isnan(rVal)) |
| 322 | { |
| 323 | return true; |
| 324 | } |
| 325 | double diff = std::abs(lVal - rVal); |
| 326 | if (diff > hysteresisPublish) |
| 327 | { |
| 328 | return true; |
| 329 | } |
| 330 | return false; |
| 331 | } |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 332 | |
| 333 | private: |
| 334 | void updateValueProperty(const double& newValue) |
| 335 | { |
| 336 | // Indicate that it is internal set call, not an external overwrite |
| 337 | internalSet = true; |
| 338 | updateProperty(sensorInterface, value, newValue, "Value"); |
| 339 | internalSet = false; |
| 340 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 341 | }; |