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