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