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