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