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