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