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. |
Patrick Williams | 379b113 | 2021-09-02 05:42:25 -0500 | [diff] [blame] | 185 | std::cerr << "Sensor " << name |
| 186 | << ": Not allowed to set property value.\n"; |
| 187 | return -EACCES; |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 192 | oldValue = newValue; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 193 | overriddenState = true; |
| 194 | // check thresholds for external set |
| 195 | value = newValue; |
| 196 | checkThresholds(); |
Josh Lehan | ffe1834 | 2021-03-17 13:29:51 -0700 | [diff] [blame] | 197 | |
| 198 | // Trigger the hook, as an external set has just happened |
| 199 | if (externalSetHook) |
| 200 | { |
| 201 | externalSetHook(); |
| 202 | } |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 203 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 204 | else if (!overriddenState) |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 205 | { |
| 206 | oldValue = newValue; |
| 207 | } |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 208 | return 1; |
| 209 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 210 | |
| 211 | void |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 212 | setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn, |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 213 | const std::string& unit, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 214 | const std::string& label = std::string(), |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 215 | size_t thresholdSize = 0) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 216 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 217 | if (readState == PowerState::on || readState == PowerState::biosPost) |
| 218 | { |
| 219 | setupPowerMatch(conn); |
| 220 | } |
| 221 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 222 | createAssociation(association, configurationPath); |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 223 | |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 224 | sensorInterface->register_property("Unit", unit); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 225 | sensorInterface->register_property("MaxValue", maxValue); |
| 226 | sensorInterface->register_property("MinValue", minValue); |
| 227 | sensorInterface->register_property( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 228 | "Value", value, [&](const double& newValue, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 229 | return setSensorValue(newValue, oldValue); |
| 230 | }); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 231 | for (auto& threshold : thresholds) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 232 | { |
Rashmica Gupta | 1e34cec | 2021-08-31 16:47:39 +1000 | [diff] [blame^] | 233 | if (std::isnan(threshold.hysteresis)) |
| 234 | { |
| 235 | threshold.hysteresis = hysteresisTrigger; |
| 236 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 237 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 238 | std::string level; |
| 239 | std::string alarm; |
| 240 | if (threshold.level == thresholds::Level::CRITICAL) |
| 241 | { |
| 242 | iface = thresholdInterfaceCritical; |
| 243 | if (threshold.direction == thresholds::Direction::HIGH) |
| 244 | { |
| 245 | level = "CriticalHigh"; |
| 246 | alarm = "CriticalAlarmHigh"; |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | level = "CriticalLow"; |
| 251 | alarm = "CriticalAlarmLow"; |
| 252 | } |
| 253 | } |
| 254 | else if (threshold.level == thresholds::Level::WARNING) |
| 255 | { |
| 256 | iface = thresholdInterfaceWarning; |
| 257 | if (threshold.direction == thresholds::Direction::HIGH) |
| 258 | { |
| 259 | level = "WarningHigh"; |
| 260 | alarm = "WarningAlarmHigh"; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | level = "WarningLow"; |
| 265 | alarm = "WarningAlarmLow"; |
| 266 | } |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | std::cerr << "Unknown threshold level" << threshold.level |
| 271 | << "\n"; |
| 272 | continue; |
| 273 | } |
| 274 | if (!iface) |
| 275 | { |
| 276 | std::cout << "trying to set uninitialized interface\n"; |
| 277 | continue; |
| 278 | } |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 279 | |
| 280 | size_t thresSize = |
| 281 | label.empty() ? thresholds.size() : thresholdSize; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 282 | iface->register_property( |
| 283 | level, threshold.value, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 284 | [&, label, thresSize](const double& request, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 285 | oldValue = request; // todo, just let the config do this? |
| 286 | threshold.value = request; |
| 287 | thresholds::persistThreshold(configurationPath, objectType, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 288 | threshold, conn, thresSize, |
| 289 | label); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 290 | // Invalidate previously remembered value, |
| 291 | // so new thresholds will be checked during next update, |
| 292 | // even if sensor reading remains unchanged. |
| 293 | value = std::numeric_limits<double>::quiet_NaN(); |
| 294 | |
| 295 | // Although tempting, don't call checkThresholds() from here |
| 296 | // directly. Let the regular sensor monitor call the same |
| 297 | // using updateValue(), which can check conditions like |
| 298 | // poweron, etc., before raising any event. |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 299 | return 1; |
| 300 | }); |
| 301 | iface->register_property(alarm, false); |
| 302 | } |
| 303 | if (!sensorInterface->initialize()) |
| 304 | { |
| 305 | std::cerr << "error initializing value interface\n"; |
| 306 | } |
| 307 | if (thresholdInterfaceWarning && |
Yong Li | f902c05 | 2020-05-07 17:13:53 +0800 | [diff] [blame] | 308 | !thresholdInterfaceWarning->initialize(true)) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 309 | { |
| 310 | std::cerr << "error initializing warning threshold interface\n"; |
| 311 | } |
| 312 | |
| 313 | if (thresholdInterfaceCritical && |
Yong Li | f902c05 | 2020-05-07 17:13:53 +0800 | [diff] [blame] | 314 | !thresholdInterfaceCritical->initialize(true)) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 315 | { |
| 316 | std::cerr << "error initializing critical threshold interface\n"; |
| 317 | } |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 318 | |
| 319 | if (!availableInterface) |
| 320 | { |
| 321 | availableInterface = |
| 322 | std::make_shared<sdbusplus::asio::dbus_interface>( |
| 323 | conn, sensorInterface->get_object_path(), |
| 324 | availableInterfaceName); |
| 325 | availableInterface->register_property( |
| 326 | "Available", true, [this](const bool propIn, bool& old) { |
| 327 | if (propIn == old) |
| 328 | { |
| 329 | return 1; |
| 330 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 331 | old = propIn; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 332 | if (!propIn) |
| 333 | { |
| 334 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 335 | } |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame] | 336 | return 1; |
| 337 | }); |
| 338 | availableInterface->initialize(); |
| 339 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 340 | if (!operationalInterface) |
| 341 | { |
| 342 | operationalInterface = |
| 343 | std::make_shared<sdbusplus::asio::dbus_interface>( |
| 344 | conn, sensorInterface->get_object_path(), |
| 345 | operationalInterfaceName); |
| 346 | operationalInterface->register_property("Functional", true); |
| 347 | operationalInterface->initialize(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | bool readingStateGood() |
| 352 | { |
| 353 | if (readState == PowerState::on && !isPowerOn()) |
| 354 | { |
| 355 | return false; |
| 356 | } |
| 357 | if (readState == PowerState::biosPost && |
| 358 | (!hasBiosPost() || !isPowerOn())) |
| 359 | { |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | void markFunctional(bool isFunctional) |
| 367 | { |
| 368 | if (operationalInterface) |
| 369 | { |
| 370 | operationalInterface->set_property("Functional", isFunctional); |
| 371 | } |
| 372 | if (isFunctional) |
| 373 | { |
| 374 | errCount = 0; |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void markAvailable(bool isAvailable) |
| 383 | { |
| 384 | if (availableInterface) |
| 385 | { |
| 386 | availableInterface->set_property("Available", isAvailable); |
| 387 | errCount = 0; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | void incrementError() |
| 392 | { |
| 393 | if (!readingStateGood()) |
| 394 | { |
| 395 | markAvailable(false); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | if (errCount >= errorThreshold) |
| 400 | { |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | errCount++; |
| 405 | if (errCount == errorThreshold) |
| 406 | { |
| 407 | std::cerr << "Sensor " << name << " reading error!\n"; |
| 408 | markFunctional(false); |
| 409 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 410 | } |
| 411 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 412 | void updateValue(const double& newValue) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 413 | { |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 414 | // Ignore if overriding is enabled |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 415 | if (overriddenState) |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 416 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 417 | return; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 418 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 419 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 420 | if (!readingStateGood()) |
| 421 | { |
| 422 | markAvailable(false); |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 423 | updateValueProperty(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 427 | updateValueProperty(newValue); |
Josh Lehan | 3bcd823 | 2020-10-29 00:22:12 -0700 | [diff] [blame] | 428 | updateInstrumentation(newValue); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 429 | |
| 430 | // Always check thresholds after changing the value, |
| 431 | // as the test against hysteresisTrigger now takes place in |
| 432 | // the thresholds::checkThresholds() method, |
| 433 | // which is called by checkThresholds() below, |
| 434 | // in all current implementations of sensors that have thresholds. |
| 435 | checkThresholds(); |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 436 | if (!std::isnan(newValue)) |
| 437 | { |
| 438 | markFunctional(true); |
| 439 | markAvailable(true); |
| 440 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 441 | } |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 442 | |
| 443 | void updateProperty( |
| 444 | std::shared_ptr<sdbusplus::asio::dbus_interface>& interface, |
| 445 | double& oldValue, const double& newValue, const char* dbusPropertyName) |
| 446 | { |
| 447 | if (requiresUpdate(oldValue, newValue)) |
| 448 | { |
| 449 | oldValue = newValue; |
Jae Hyun Yoo | 1a540b8 | 2020-07-30 23:33:18 -0700 | [diff] [blame] | 450 | if (interface && |
| 451 | !(interface->set_property(dbusPropertyName, newValue))) |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 452 | { |
| 453 | std::cerr << "error setting property " << dbusPropertyName |
| 454 | << " to " << newValue << "\n"; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | bool requiresUpdate(const double& lVal, const double& rVal) |
| 460 | { |
| 461 | if (std::isnan(lVal) || std::isnan(rVal)) |
| 462 | { |
| 463 | return true; |
| 464 | } |
| 465 | double diff = std::abs(lVal - rVal); |
| 466 | if (diff > hysteresisPublish) |
| 467 | { |
| 468 | return true; |
| 469 | } |
| 470 | return false; |
| 471 | } |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 472 | |
| 473 | private: |
| 474 | void updateValueProperty(const double& newValue) |
| 475 | { |
| 476 | // Indicate that it is internal set call, not an external overwrite |
| 477 | internalSet = true; |
| 478 | updateProperty(sensorInterface, value, newValue, "Value"); |
| 479 | internalSet = false; |
| 480 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 481 | }; |