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