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