James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 3 | #include <SensorPaths.hpp> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 4 | #include <Thresholds.hpp> |
| 5 | #include <Utils.hpp> |
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 | |
Josh Lehan | 3bcd823 | 2020-10-29 00:22:12 -0700 | [diff] [blame] | 15 | // Enable useful logging with sensor instrumentation |
| 16 | // This is intentionally not DEBUG, avoid clash with usage in .cpp files |
| 17 | constexpr bool enableInstrumentation = false; |
| 18 | |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 19 | constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 20 | constexpr const char* availableInterfaceName = |
| 21 | "xyz.openbmc_project.State.Decorator.Availability"; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 22 | constexpr const char* operationalInterfaceName = |
| 23 | "xyz.openbmc_project.State.Decorator.OperationalStatus"; |
| 24 | constexpr const size_t errorThreshold = 5; |
| 25 | |
Josh Lehan | 3bcd823 | 2020-10-29 00:22:12 -0700 | [diff] [blame] | 26 | struct SensorInstrumentation |
| 27 | { |
| 28 | // These are for instrumentation for debugging |
| 29 | int numCollectsGood = 0; |
| 30 | int numCollectsMiss = 0; |
| 31 | int numStreakGreats = 0; |
| 32 | int numStreakMisses = 0; |
| 33 | double minCollected = 0.0; |
| 34 | double maxCollected = 0.0; |
| 35 | }; |
| 36 | |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 37 | struct Sensor |
| 38 | { |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 39 | Sensor(const std::string& name, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 40 | std::vector<thresholds::Threshold>&& thresholdData, |
| 41 | const std::string& configurationPath, const std::string& objectType, |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 42 | const double max, const double min, |
James Feist | e333852 | 2020-09-15 15:40:30 -0700 | [diff] [blame] | 43 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 44 | PowerState readState = PowerState::always) : |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 45 | name(sensor_paths::escapePathForDbus(name)), |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 46 | configurationPath(configurationPath), objectType(objectType), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 47 | maxValue(max), minValue(min), thresholds(std::move(thresholdData)), |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 48 | hysteresisTrigger((max - min) * 0.01), |
James Feist | e333852 | 2020-09-15 15:40:30 -0700 | [diff] [blame] | 49 | hysteresisPublish((max - min) * 0.0001), dbusConnection(conn), |
Josh Lehan | 3bcd823 | 2020-10-29 00:22:12 -0700 | [diff] [blame] | 50 | readState(readState), errCount(0), |
| 51 | instrumentation(enableInstrumentation |
| 52 | ? std::make_unique<SensorInstrumentation>() |
| 53 | : nullptr) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 54 | {} |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 55 | virtual ~Sensor() = default; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 56 | virtual void checkThresholds(void) = 0; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 57 | std::string name; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 58 | std::string configurationPath; |
| 59 | std::string objectType; |
| 60 | double maxValue; |
| 61 | double minValue; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 62 | std::vector<thresholds::Threshold> thresholds; |
| 63 | std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface; |
| 64 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning; |
| 65 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical; |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 66 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 67 | std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 68 | std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 69 | double value = std::numeric_limits<double>::quiet_NaN(); |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 70 | double rawValue = std::numeric_limits<double>::quiet_NaN(); |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 71 | bool overriddenState = false; |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 72 | bool internalSet = false; |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 73 | double hysteresisTrigger; |
| 74 | double hysteresisPublish; |
James Feist | e333852 | 2020-09-15 15:40:30 -0700 | [diff] [blame] | 75 | std::shared_ptr<sdbusplus::asio::connection> dbusConnection; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 76 | PowerState readState; |
| 77 | size_t errCount; |
Josh Lehan | 3bcd823 | 2020-10-29 00:22:12 -0700 | [diff] [blame] | 78 | std::unique_ptr<SensorInstrumentation> instrumentation; |
| 79 | |
| 80 | void updateInstrumentation(double readValue) |
| 81 | { |
| 82 | // Do nothing if this feature is not enabled |
| 83 | if constexpr (!enableInstrumentation) |
| 84 | { |
| 85 | return; |
| 86 | } |
| 87 | if (!instrumentation) |
| 88 | { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // Save some typing |
| 93 | auto& inst = *instrumentation; |
| 94 | |
| 95 | // Show constants if first reading (even if unsuccessful) |
| 96 | if ((inst.numCollectsGood == 0) && (inst.numCollectsMiss == 0)) |
| 97 | { |
| 98 | std::cerr << "Sensor " << name << ": Configuration min=" << minValue |
| 99 | << ", max=" << maxValue << ", type=" << objectType |
| 100 | << ", path=" << configurationPath << "\n"; |
| 101 | } |
| 102 | |
| 103 | // Sensors can use "nan" to indicate unavailable reading |
| 104 | if (!std::isfinite(readValue)) |
| 105 | { |
| 106 | // Only show this if beginning a new streak |
| 107 | if (inst.numStreakMisses == 0) |
| 108 | { |
| 109 | std::cerr << "Sensor " << name |
| 110 | << ": Missing reading, Reading counts good=" |
| 111 | << inst.numCollectsGood |
| 112 | << ", miss=" << inst.numCollectsMiss |
| 113 | << ", Prior good streak=" << inst.numStreakGreats |
| 114 | << "\n"; |
| 115 | } |
| 116 | |
| 117 | inst.numStreakGreats = 0; |
| 118 | ++(inst.numCollectsMiss); |
| 119 | ++(inst.numStreakMisses); |
| 120 | |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // Only show this if beginning a new streak and not the first time |
| 125 | if ((inst.numStreakGreats == 0) && (inst.numCollectsGood != 0)) |
| 126 | { |
| 127 | std::cerr << "Sensor " << name |
| 128 | << ": Recovered reading, Reading counts good=" |
| 129 | << inst.numCollectsGood |
| 130 | << ", miss=" << inst.numCollectsMiss |
| 131 | << ", Prior miss streak=" << inst.numStreakMisses << "\n"; |
| 132 | } |
| 133 | |
| 134 | // Initialize min/max if the first successful reading |
| 135 | if (inst.numCollectsGood == 0) |
| 136 | { |
| 137 | std::cerr << "Sensor " << name << ": First reading=" << readValue |
| 138 | << "\n"; |
| 139 | |
| 140 | inst.minCollected = readValue; |
| 141 | inst.maxCollected = readValue; |
| 142 | } |
| 143 | |
| 144 | inst.numStreakMisses = 0; |
| 145 | ++(inst.numCollectsGood); |
| 146 | ++(inst.numStreakGreats); |
| 147 | |
| 148 | // Only provide subsequent output if new min/max established |
| 149 | if (readValue < inst.minCollected) |
| 150 | { |
| 151 | std::cerr << "Sensor " << name << ": Lowest reading=" << readValue |
| 152 | << "\n"; |
| 153 | |
| 154 | inst.minCollected = readValue; |
| 155 | } |
| 156 | |
| 157 | if (readValue > inst.maxCollected) |
| 158 | { |
| 159 | std::cerr << "Sensor " << name << ": Highest reading=" << readValue |
| 160 | << "\n"; |
| 161 | |
| 162 | inst.maxCollected = readValue; |
| 163 | } |
| 164 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 165 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 166 | int setSensorValue(const double& newValue, double& oldValue) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 167 | { |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 168 | if (!internalSet) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 169 | { |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 170 | oldValue = newValue; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 171 | overriddenState = true; |
| 172 | // check thresholds for external set |
| 173 | value = newValue; |
| 174 | checkThresholds(); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 175 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 176 | else if (!overriddenState) |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 177 | { |
| 178 | oldValue = newValue; |
| 179 | } |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 180 | return 1; |
| 181 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 182 | |
| 183 | void |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 184 | setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 185 | const std::string& label = std::string(), |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 186 | size_t thresholdSize = 0) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 187 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 188 | if (readState == PowerState::on || readState == PowerState::biosPost) |
| 189 | { |
| 190 | setupPowerMatch(conn); |
| 191 | } |
| 192 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 193 | createAssociation(association, configurationPath); |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 194 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 195 | sensorInterface->register_property("MaxValue", maxValue); |
| 196 | sensorInterface->register_property("MinValue", minValue); |
| 197 | sensorInterface->register_property( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 198 | "Value", value, [&](const double& newValue, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 199 | return setSensorValue(newValue, oldValue); |
| 200 | }); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 201 | for (auto& threshold : thresholds) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 202 | { |
| 203 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 204 | std::string level; |
| 205 | std::string alarm; |
| 206 | if (threshold.level == thresholds::Level::CRITICAL) |
| 207 | { |
| 208 | iface = thresholdInterfaceCritical; |
| 209 | if (threshold.direction == thresholds::Direction::HIGH) |
| 210 | { |
| 211 | level = "CriticalHigh"; |
| 212 | alarm = "CriticalAlarmHigh"; |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | level = "CriticalLow"; |
| 217 | alarm = "CriticalAlarmLow"; |
| 218 | } |
| 219 | } |
| 220 | else if (threshold.level == thresholds::Level::WARNING) |
| 221 | { |
| 222 | iface = thresholdInterfaceWarning; |
| 223 | if (threshold.direction == thresholds::Direction::HIGH) |
| 224 | { |
| 225 | level = "WarningHigh"; |
| 226 | alarm = "WarningAlarmHigh"; |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | level = "WarningLow"; |
| 231 | alarm = "WarningAlarmLow"; |
| 232 | } |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | std::cerr << "Unknown threshold level" << threshold.level |
| 237 | << "\n"; |
| 238 | continue; |
| 239 | } |
| 240 | if (!iface) |
| 241 | { |
| 242 | std::cout << "trying to set uninitialized interface\n"; |
| 243 | continue; |
| 244 | } |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 245 | |
| 246 | size_t thresSize = |
| 247 | label.empty() ? thresholds.size() : thresholdSize; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 248 | iface->register_property( |
| 249 | level, threshold.value, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 250 | [&, label, thresSize](const double& request, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 251 | oldValue = request; // todo, just let the config do this? |
| 252 | threshold.value = request; |
| 253 | thresholds::persistThreshold(configurationPath, objectType, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 254 | threshold, conn, thresSize, |
| 255 | label); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 256 | // Invalidate previously remembered value, |
| 257 | // so new thresholds will be checked during next update, |
| 258 | // even if sensor reading remains unchanged. |
| 259 | value = std::numeric_limits<double>::quiet_NaN(); |
| 260 | |
| 261 | // Although tempting, don't call checkThresholds() from here |
| 262 | // directly. Let the regular sensor monitor call the same |
| 263 | // using updateValue(), which can check conditions like |
| 264 | // poweron, etc., before raising any event. |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 265 | return 1; |
| 266 | }); |
| 267 | iface->register_property(alarm, false); |
| 268 | } |
| 269 | if (!sensorInterface->initialize()) |
| 270 | { |
| 271 | std::cerr << "error initializing value interface\n"; |
| 272 | } |
| 273 | if (thresholdInterfaceWarning && |
Yong Li | f902c05 | 2020-05-07 17:13:53 +0800 | [diff] [blame] | 274 | !thresholdInterfaceWarning->initialize(true)) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 275 | { |
| 276 | std::cerr << "error initializing warning threshold interface\n"; |
| 277 | } |
| 278 | |
| 279 | if (thresholdInterfaceCritical && |
Yong Li | f902c05 | 2020-05-07 17:13:53 +0800 | [diff] [blame] | 280 | !thresholdInterfaceCritical->initialize(true)) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 281 | { |
| 282 | std::cerr << "error initializing critical threshold interface\n"; |
| 283 | } |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 284 | |
| 285 | if (!availableInterface) |
| 286 | { |
| 287 | availableInterface = |
| 288 | std::make_shared<sdbusplus::asio::dbus_interface>( |
| 289 | conn, sensorInterface->get_object_path(), |
| 290 | availableInterfaceName); |
| 291 | availableInterface->register_property( |
| 292 | "Available", true, [this](const bool propIn, bool& old) { |
| 293 | if (propIn == old) |
| 294 | { |
| 295 | return 1; |
| 296 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 297 | old = propIn; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 298 | if (!propIn) |
| 299 | { |
| 300 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 301 | } |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 302 | return 1; |
| 303 | }); |
| 304 | availableInterface->initialize(); |
| 305 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 306 | if (!operationalInterface) |
| 307 | { |
| 308 | operationalInterface = |
| 309 | std::make_shared<sdbusplus::asio::dbus_interface>( |
| 310 | conn, sensorInterface->get_object_path(), |
| 311 | operationalInterfaceName); |
| 312 | operationalInterface->register_property("Functional", true); |
| 313 | operationalInterface->initialize(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | bool readingStateGood() |
| 318 | { |
| 319 | if (readState == PowerState::on && !isPowerOn()) |
| 320 | { |
| 321 | return false; |
| 322 | } |
| 323 | if (readState == PowerState::biosPost && |
| 324 | (!hasBiosPost() || !isPowerOn())) |
| 325 | { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | void markFunctional(bool isFunctional) |
| 333 | { |
| 334 | if (operationalInterface) |
| 335 | { |
| 336 | operationalInterface->set_property("Functional", isFunctional); |
| 337 | } |
| 338 | if (isFunctional) |
| 339 | { |
| 340 | errCount = 0; |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void markAvailable(bool isAvailable) |
| 349 | { |
| 350 | if (availableInterface) |
| 351 | { |
| 352 | availableInterface->set_property("Available", isAvailable); |
| 353 | errCount = 0; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void incrementError() |
| 358 | { |
| 359 | if (!readingStateGood()) |
| 360 | { |
| 361 | markAvailable(false); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | if (errCount >= errorThreshold) |
| 366 | { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | errCount++; |
| 371 | if (errCount == errorThreshold) |
| 372 | { |
| 373 | std::cerr << "Sensor " << name << " reading error!\n"; |
| 374 | markFunctional(false); |
| 375 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 376 | } |
| 377 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 378 | void updateValue(const double& newValue) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 379 | { |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 380 | // Ignore if overriding is enabled |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 381 | if (overriddenState) |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 382 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 383 | return; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 384 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 385 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 386 | if (!readingStateGood()) |
| 387 | { |
| 388 | markAvailable(false); |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 389 | updateValueProperty(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 393 | updateValueProperty(newValue); |
Josh Lehan | 3bcd823 | 2020-10-29 00:22:12 -0700 | [diff] [blame] | 394 | updateInstrumentation(newValue); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 395 | |
| 396 | // Always check thresholds after changing the value, |
| 397 | // as the test against hysteresisTrigger now takes place in |
| 398 | // the thresholds::checkThresholds() method, |
| 399 | // which is called by checkThresholds() below, |
| 400 | // in all current implementations of sensors that have thresholds. |
| 401 | checkThresholds(); |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 402 | if (!std::isnan(newValue)) |
| 403 | { |
| 404 | markFunctional(true); |
| 405 | markAvailable(true); |
| 406 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 407 | } |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 408 | |
| 409 | void updateProperty( |
| 410 | std::shared_ptr<sdbusplus::asio::dbus_interface>& interface, |
| 411 | double& oldValue, const double& newValue, const char* dbusPropertyName) |
| 412 | { |
| 413 | if (requiresUpdate(oldValue, newValue)) |
| 414 | { |
| 415 | oldValue = newValue; |
Jae Hyun Yoo | 1a540b8 | 2020-07-30 23:33:18 -0700 | [diff] [blame] | 416 | if (interface && |
| 417 | !(interface->set_property(dbusPropertyName, newValue))) |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 418 | { |
| 419 | std::cerr << "error setting property " << dbusPropertyName |
| 420 | << " to " << newValue << "\n"; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | bool requiresUpdate(const double& lVal, const double& rVal) |
| 426 | { |
| 427 | if (std::isnan(lVal) || std::isnan(rVal)) |
| 428 | { |
| 429 | return true; |
| 430 | } |
| 431 | double diff = std::abs(lVal - rVal); |
| 432 | if (diff > hysteresisPublish) |
| 433 | { |
| 434 | return true; |
| 435 | } |
| 436 | return false; |
| 437 | } |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 438 | |
| 439 | private: |
| 440 | void updateValueProperty(const double& newValue) |
| 441 | { |
| 442 | // Indicate that it is internal set call, not an external overwrite |
| 443 | internalSet = true; |
| 444 | updateProperty(sensorInterface, value, newValue, "Value"); |
| 445 | internalSet = false; |
| 446 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 447 | }; |