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