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