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