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