blob: 07d70ecffb50de357cbcb54d6b67bd911785757c [file] [log] [blame]
James Feist8fd8a582018-11-16 11:10:46 -08001#pragma once
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10302
Bruce Lee1263c3d2021-06-04 15:16:33 +08003#include "dbus-sensor_config.h"
James Feist8fd8a582018-11-16 11:10:46 -08004
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10305#include "SensorPaths.hpp"
6#include "Thresholds.hpp"
7#include "Utils.hpp"
8
George Liu61984352025-02-24 14:47:34 +08009#include <phosphor-logging/lg2.hpp>
Ed Tanous18b61862025-01-30 10:56:28 -080010#include <sdbusplus/asio/connection.hpp>
James Feist38fb5982020-05-28 10:09:54 -070011#include <sdbusplus/asio/object_server.hpp>
Jonathan Doman256d8c12022-06-03 13:01:08 -070012#include <sdbusplus/exception.hpp>
James Feist38fb5982020-05-28 10:09:54 -070013
Ed Tanous18b61862025-01-30 10:56:28 -080014#include <array>
15#include <cerrno>
16#include <cmath>
17#include <cstddef>
18#include <cstdlib>
19#include <functional>
Patrick Venturefd6ba732019-10-31 14:27:39 -070020#include <limits>
21#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070022#include <string>
Ed Tanous18b61862025-01-30 10:56:28 -080023#include <utility>
Patrick Venturefd6ba732019-10-31 14:27:39 -070024#include <vector>
James Feist8fd8a582018-11-16 11:10:46 -080025
James Feist1169eb42018-10-31 10:08:47 -070026constexpr size_t sensorFailedPollTimeMs = 5000;
James Feista5e58722019-04-22 14:43:11 -070027
Josh Lehan3bcd8232020-10-29 00:22:12 -070028// Enable useful logging with sensor instrumentation
29// This is intentionally not DEBUG, avoid clash with usage in .cpp files
30constexpr bool enableInstrumentation = false;
31
James Feista5e58722019-04-22 14:43:11 -070032constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value";
Jie Yang3291b9c2021-07-29 14:46:51 -070033constexpr const char* valueMutabilityInterfaceName =
34 "xyz.openbmc_project.Sensor.ValueMutability";
James Feist67601bd2020-06-16 17:14:44 -070035constexpr const char* availableInterfaceName =
36 "xyz.openbmc_project.State.Decorator.Availability";
James Feist961bf092020-07-01 16:38:12 -070037constexpr const char* operationalInterfaceName =
38 "xyz.openbmc_project.State.Decorator.OperationalStatus";
39constexpr const size_t errorThreshold = 5;
40
Josh Lehan3bcd8232020-10-29 00:22:12 -070041struct 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 Doman256d8c12022-06-03 13:01:08 -070052struct 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 Feist8fd8a582018-11-16 11:10:46 -080068struct Sensor
69{
James Feist930fcde2019-05-28 12:58:43 -070070 Sensor(const std::string& name,
James Feistd8705872019-02-08 13:26:09 -080071 std::vector<thresholds::Threshold>&& thresholdData,
72 const std::string& configurationPath, const std::string& objectType,
Jie Yang3291b9c2021-07-29 14:46:51 -070073 bool isSettable, bool isMutable, const double max, const double min,
James Feiste3338522020-09-15 15:40:30 -070074 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist961bf092020-07-01 16:38:12 -070075 PowerState readState = PowerState::always) :
Ed Tanous6cb732a2021-02-18 15:33:51 -080076 name(sensor_paths::escapePathForDbus(name)),
Zev Weiss054aad82022-08-18 01:37:34 -070077 configurationPath(configurationPath),
Matt Spinler55832f32023-06-07 10:24:00 -050078 configInterface(configInterfaceName(objectType)),
Jie Yang3291b9c2021-07-29 14:46:51 -070079 isSensorSettable(isSettable), isValueMutable(isMutable), maxValue(max),
80 minValue(min), thresholds(std::move(thresholdData)),
Josh Lehan883fb3a2020-02-27 14:41:39 -080081 hysteresisTrigger((max - min) * 0.01),
James Feiste3338522020-09-15 15:40:30 -070082 hysteresisPublish((max - min) * 0.0001), dbusConnection(conn),
Ed Tanousb429f312022-06-27 16:09:53 -070083 readState(readState),
Josh Lehan3bcd8232020-10-29 00:22:12 -070084 instrumentation(enableInstrumentation
85 ? std::make_unique<SensorInstrumentation>()
86 : nullptr)
James Feist38fb5982020-05-28 10:09:54 -070087 {}
James Feist8fd8a582018-11-16 11:10:46 -080088 virtual ~Sensor() = default;
Ed Tanous201a1012024-04-03 18:07:28 -070089 virtual void checkThresholds() = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070090 std::string name;
James Feistce3fca42018-11-21 12:58:24 -080091 std::string configurationPath;
Matt Spinler55832f32023-06-07 10:24:00 -050092 std::string configInterface;
Bruce Lee1263c3d2021-06-04 15:16:33 +080093 bool isSensorSettable;
Jie Yang3291b9c2021-07-29 14:46:51 -070094
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 Feistce3fca42018-11-21 12:58:24 -0800101 double maxValue;
102 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -0800103 std::vector<thresholds::Threshold> thresholds;
104 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
James Feist078f2322019-03-08 11:09:05 -0800105 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist67601bd2020-06-16 17:14:44 -0700106 std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface;
James Feist961bf092020-07-01 16:38:12 -0700107 std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface;
Jie Yang3291b9c2021-07-29 14:46:51 -0700108 std::shared_ptr<sdbusplus::asio::dbus_interface> valueMutabilityInterface;
James Feist8fd8a582018-11-16 11:10:46 -0800109 double value = std::numeric_limits<double>::quiet_NaN();
Zhikui Rend3da1282020-09-11 17:02:01 -0700110 double rawValue = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530111 bool overriddenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530112 bool internalSet = false;
Josh Lehan883fb3a2020-02-27 14:41:39 -0800113 double hysteresisTrigger;
114 double hysteresisPublish;
James Feiste3338522020-09-15 15:40:30 -0700115 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
James Feist961bf092020-07-01 16:38:12 -0700116 PowerState readState;
Ed Tanousb429f312022-06-27 16:09:53 -0700117 size_t errCount{0};
Josh Lehan3bcd8232020-10-29 00:22:12 -0700118 std::unique_ptr<SensorInstrumentation> instrumentation;
119
Josh Lehanffe18342021-03-17 13:29:51 -0700120 // 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 Tanousc8fed202022-01-12 14:24:45 -0800126 using Level = thresholds::Level;
127 using Direction = thresholds::Direction;
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530128
Ed Tanousc8fed202022-01-12 14:24:45 -0800129 std::array<std::shared_ptr<sdbusplus::asio::dbus_interface>,
130 thresholds::thresProp.size()>
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530131 thresholdInterfaces;
132
Patrick Williams556e04b2025-02-01 08:22:22 -0500133 std::shared_ptr<sdbusplus::asio::dbus_interface> getThresholdInterface(
134 Level lev)
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530135 {
136 size_t index = static_cast<size_t>(lev);
137 if (index >= thresholdInterfaces.size())
138 {
George Liu61984352025-02-24 14:47:34 +0800139 lg2::info("Unknown threshold level");
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530140 return nullptr;
141 }
142 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
143 thresholdInterfaces[index];
144 return interface;
145 }
146
Ed Tanous2049bd22022-07-09 07:20:26 -0700147 void updateInstrumentation(double readValue) const
Josh Lehan3bcd8232020-10-29 00:22:12 -0700148 {
149 // Do nothing if this feature is not enabled
150 if constexpr (!enableInstrumentation)
151 {
152 return;
153 }
154 if (!instrumentation)
155 {
156 return;
157 }
158
159 // Save some typing
160 auto& inst = *instrumentation;
161
162 // Show constants if first reading (even if unsuccessful)
163 if ((inst.numCollectsGood == 0) && (inst.numCollectsMiss == 0))
164 {
George Liu61984352025-02-24 14:47:34 +0800165 lg2::info(
166 "Sensor name: {NAME}, min: {MIN}, max: {MAX}, type: {TYPE}, path: {PATH}",
167 "NAME", name, "MIN", minValue, "MAX", maxValue, "TYPE",
168 configInterface, "PATH", configurationPath);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700169 }
170
171 // Sensors can use "nan" to indicate unavailable reading
172 if (!std::isfinite(readValue))
173 {
174 // Only show this if beginning a new streak
175 if (inst.numStreakMisses == 0)
176 {
George Liu61984352025-02-24 14:47:34 +0800177 lg2::warning(
178 "Sensor name: {NAME}, Missing reading, Reading counts good= {NUM_COLLECTS_GOOD},"
179 " miss= {NUM_COLLECTS_MISS}, Prior good streak= {NUM_STREAK_GREATS}",
180 "NAME", name, "NUM_COLLECTS_GOOD", inst.numCollectsGood,
181 "NUM_COLLECTS_MISS", inst.numCollectsMiss,
182 "NUM_STREAK_GREATS", inst.numStreakGreats);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700183 }
184
185 inst.numStreakGreats = 0;
186 ++(inst.numCollectsMiss);
187 ++(inst.numStreakMisses);
188
189 return;
190 }
191
192 // Only show this if beginning a new streak and not the first time
193 if ((inst.numStreakGreats == 0) && (inst.numCollectsGood != 0))
194 {
George Liu61984352025-02-24 14:47:34 +0800195 lg2::info(
196 "Sensor name: {NAME}, Recovered reading, Reading counts good= {NUM_COLLECTS_GOOD},"
197 " miss= {NUM_COLLECTS_MISS}, Prior good streak= {NUM_STREAK_GREATS}",
198 "NAME", name, "NUM_COLLECTS_GOOD", inst.numCollectsGood,
199 "NUM_COLLECTS_MISS", inst.numCollectsMiss, "NUM_STREAK_GREATS",
200 inst.numStreakGreats);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700201 }
202
203 // Initialize min/max if the first successful reading
204 if (inst.numCollectsGood == 0)
205 {
George Liu61984352025-02-24 14:47:34 +0800206 lg2::info("Sensor name: {NAME}, First reading: {VALUE}", "NAME",
207 name, "VALUE", readValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700208
209 inst.minCollected = readValue;
210 inst.maxCollected = readValue;
211 }
212
213 inst.numStreakMisses = 0;
214 ++(inst.numCollectsGood);
215 ++(inst.numStreakGreats);
216
217 // Only provide subsequent output if new min/max established
218 if (readValue < inst.minCollected)
219 {
George Liu61984352025-02-24 14:47:34 +0800220 lg2::info("Sensor name: {NAME}, Lowest reading: {VALUE}", "NAME",
221 name, "VALUE", readValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700222
223 inst.minCollected = readValue;
224 }
225
226 if (readValue > inst.maxCollected)
227 {
George Liu61984352025-02-24 14:47:34 +0800228 lg2::info("Sensor name: {NAME}, Highest reading: {VALUE}", "NAME",
229 name, "VALUE", readValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700230
231 inst.maxCollected = readValue;
232 }
233 }
James Feistce3fca42018-11-21 12:58:24 -0800234
James Feistd8705872019-02-08 13:26:09 -0800235 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530236 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530237 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530238 {
Jonathan Doman256d8c12022-06-03 13:01:08 -0700239 if (insecureSensorOverride == 0 && !isSensorSettable &&
240 !getManufacturingMode())
241 {
242 throw SetSensorError();
Bruce Lee1263c3d2021-06-04 15:16:33 +0800243 }
244
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530245 oldValue = newValue;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530246 overriddenState = true;
247 // check thresholds for external set
248 value = newValue;
249 checkThresholds();
Josh Lehanffe18342021-03-17 13:29:51 -0700250
251 // Trigger the hook, as an external set has just happened
252 if (externalSetHook)
253 {
254 externalSetHook();
255 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530256 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530257 else if (!overriddenState)
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530258 {
259 oldValue = newValue;
260 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700261 return 1;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530262 }
James Feistce3fca42018-11-21 12:58:24 -0800263
Andrei Kartashev39287412022-02-04 16:04:47 +0300264 void setInitialProperties(const std::string& unit,
265 const std::string& label = std::string(),
266 size_t thresholdSize = 0)
James Feistce3fca42018-11-21 12:58:24 -0800267 {
Thu Nguyen6db8aae2022-10-04 08:12:48 +0700268 if (readState == PowerState::on || readState == PowerState::biosPost ||
269 readState == PowerState::chassisOn)
James Feist961bf092020-07-01 16:38:12 -0700270 {
Andrei Kartashev39287412022-02-04 16:04:47 +0300271 setupPowerMatch(dbusConnection);
James Feist961bf092020-07-01 16:38:12 -0700272 }
273
James Feist82bac4c2019-03-11 11:16:53 -0700274 createAssociation(association, configurationPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530275
Zev Weiss6b6891c2021-04-22 02:46:21 -0500276 sensorInterface->register_property("Unit", unit);
James Feistce3fca42018-11-21 12:58:24 -0800277 sensorInterface->register_property("MaxValue", maxValue);
278 sensorInterface->register_property("MinValue", minValue);
279 sensorInterface->register_property(
Jonathan Doman256d8c12022-06-03 13:01:08 -0700280 "Value", value, [this](const double& newValue, double& oldValue) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400281 return setSensorValue(newValue, oldValue);
282 });
Konstantin Aladyshev75872ef2021-05-13 11:17:58 +0300283
284 fillMissingThresholds();
285
James Feistd8705872019-02-08 13:26:09 -0800286 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -0800287 {
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000288 if (std::isnan(threshold.hysteresis))
289 {
290 threshold.hysteresis = hysteresisTrigger;
291 }
Ed Tanous17551b82022-01-26 16:47:17 -0800292
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530293 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
294 getThresholdInterface(threshold.level);
295
James Feistce3fca42018-11-21 12:58:24 -0800296 if (!iface)
297 {
George Liu61984352025-02-24 14:47:34 +0800298 lg2::info("trying to set uninitialized interface");
James Feistce3fca42018-11-21 12:58:24 -0800299 continue;
300 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800301
Patrick Williams2aaf7172024-08-16 15:20:40 -0400302 std::string level =
303 propertyLevel(threshold.level, threshold.direction);
304 std::string alarm =
305 propertyAlarm(threshold.level, threshold.direction);
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530306
307 if ((level.empty()) || (alarm.empty()))
308 {
309 continue;
310 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400311 size_t thresSize =
312 label.empty() ? thresholds.size() : thresholdSize;
James Feistce3fca42018-11-21 12:58:24 -0800313 iface->register_property(
314 level, threshold.value,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800315 [&, label, thresSize](const double& request, double& oldValue) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400316 oldValue = request; // todo, just let the config do this?
317 threshold.value = request;
318 thresholds::persistThreshold(
319 configurationPath, configInterface, threshold,
320 dbusConnection, thresSize, label);
321 // Invalidate previously remembered value,
322 // so new thresholds will be checked during next update,
323 // even if sensor reading remains unchanged.
324 value = std::numeric_limits<double>::quiet_NaN();
Josh Lehan883fb3a2020-02-27 14:41:39 -0800325
Patrick Williams2aaf7172024-08-16 15:20:40 -0400326 // Although tempting, don't call checkThresholds() from here
327 // directly. Let the regular sensor monitor call the same
328 // using updateValue(), which can check conditions like
329 // poweron, etc., before raising any event.
330 return 1;
331 });
James Feistce3fca42018-11-21 12:58:24 -0800332 iface->register_property(alarm, false);
333 }
334 if (!sensorInterface->initialize())
335 {
George Liu61984352025-02-24 14:47:34 +0800336 lg2::error("error initializing value interface");
James Feistce3fca42018-11-21 12:58:24 -0800337 }
James Feistce3fca42018-11-21 12:58:24 -0800338
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530339 for (auto& thresIface : thresholdInterfaces)
James Feistce3fca42018-11-21 12:58:24 -0800340 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530341 if (thresIface)
342 {
343 if (!thresIface->initialize(true))
344 {
George Liu61984352025-02-24 14:47:34 +0800345 lg2::error("Error initializing threshold interface");
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530346 }
347 }
James Feistce3fca42018-11-21 12:58:24 -0800348 }
James Feist67601bd2020-06-16 17:14:44 -0700349
Jie Yang3291b9c2021-07-29 14:46:51 -0700350 if (isValueMutable)
351 {
352 valueMutabilityInterface =
353 std::make_shared<sdbusplus::asio::dbus_interface>(
Andrei Kartashev39287412022-02-04 16:04:47 +0300354 dbusConnection, sensorInterface->get_object_path(),
Jie Yang3291b9c2021-07-29 14:46:51 -0700355 valueMutabilityInterfaceName);
356 valueMutabilityInterface->register_property("Mutable", true);
357 if (!valueMutabilityInterface->initialize())
358 {
George Liu61984352025-02-24 14:47:34 +0800359 lg2::error(
360 "error initializing sensor value mutability interface");
Jie Yang3291b9c2021-07-29 14:46:51 -0700361 valueMutabilityInterface = nullptr;
362 }
363 }
364
James Feist67601bd2020-06-16 17:14:44 -0700365 if (!availableInterface)
366 {
367 availableInterface =
368 std::make_shared<sdbusplus::asio::dbus_interface>(
Andrei Kartashev39287412022-02-04 16:04:47 +0300369 dbusConnection, sensorInterface->get_object_path(),
James Feist67601bd2020-06-16 17:14:44 -0700370 availableInterfaceName);
371 availableInterface->register_property(
372 "Available", true, [this](const bool propIn, bool& old) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400373 if (propIn == old)
374 {
375 return 1;
376 }
377 old = propIn;
378 if (!propIn)
379 {
380 updateValue(std::numeric_limits<double>::quiet_NaN());
381 }
James Feist67601bd2020-06-16 17:14:44 -0700382 return 1;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400383 });
James Feist67601bd2020-06-16 17:14:44 -0700384 availableInterface->initialize();
385 }
James Feist961bf092020-07-01 16:38:12 -0700386 if (!operationalInterface)
387 {
388 operationalInterface =
389 std::make_shared<sdbusplus::asio::dbus_interface>(
Andrei Kartashev39287412022-02-04 16:04:47 +0300390 dbusConnection, sensorInterface->get_object_path(),
James Feist961bf092020-07-01 16:38:12 -0700391 operationalInterfaceName);
392 operationalInterface->register_property("Functional", true);
393 operationalInterface->initialize();
394 }
395 }
396
Ed Tanous2049bd22022-07-09 07:20:26 -0700397 static std::string propertyLevel(const Level lev, const Direction dir)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530398 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800399 for (const thresholds::ThresholdDefinition& prop :
400 thresholds::thresProp)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530401 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800402 if (prop.level == lev)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530403 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800404 if (dir == Direction::HIGH)
405 {
406 return std::string(prop.levelName) + "High";
407 }
408 if (dir == Direction::LOW)
409 {
410 return std::string(prop.levelName) + "Low";
411 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530412 }
413 }
414 return "";
415 }
416
Ed Tanous2049bd22022-07-09 07:20:26 -0700417 static std::string propertyAlarm(const Level lev, const Direction dir)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530418 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800419 for (const thresholds::ThresholdDefinition& prop :
420 thresholds::thresProp)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530421 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800422 if (prop.level == lev)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530423 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800424 if (dir == Direction::HIGH)
425 {
426 return std::string(prop.levelName) + "AlarmHigh";
427 }
428 if (dir == Direction::LOW)
429 {
430 return std::string(prop.levelName) + "AlarmLow";
431 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530432 }
433 }
434 return "";
435 }
436
Ed Tanous2049bd22022-07-09 07:20:26 -0700437 bool readingStateGood() const
James Feist961bf092020-07-01 16:38:12 -0700438 {
Zev Weissece5c862022-08-05 14:34:59 -0700439 return ::readingStateGood(readState);
James Feist961bf092020-07-01 16:38:12 -0700440 }
441
442 void markFunctional(bool isFunctional)
443 {
444 if (operationalInterface)
445 {
446 operationalInterface->set_property("Functional", isFunctional);
447 }
448 if (isFunctional)
449 {
450 errCount = 0;
451 }
452 else
453 {
454 updateValue(std::numeric_limits<double>::quiet_NaN());
455 }
456 }
457
458 void markAvailable(bool isAvailable)
459 {
460 if (availableInterface)
461 {
462 availableInterface->set_property("Available", isAvailable);
463 errCount = 0;
464 }
465 }
466
467 void incrementError()
468 {
469 if (!readingStateGood())
470 {
471 markAvailable(false);
472 return;
473 }
474
475 if (errCount >= errorThreshold)
476 {
477 return;
478 }
479
480 errCount++;
481 if (errCount == errorThreshold)
482 {
George Liu61984352025-02-24 14:47:34 +0800483 lg2::error("Sensor name: {NAME}, reading error!", "NAME", name);
James Feist961bf092020-07-01 16:38:12 -0700484 markFunctional(false);
485 }
James Feistce3fca42018-11-21 12:58:24 -0800486 }
487
Ed Tanous2049bd22022-07-09 07:20:26 -0700488 bool inError() const
Andrew Jefferyfad36052022-03-15 21:44:44 +1030489 {
490 return errCount >= errorThreshold;
491 }
492
James Feistd8705872019-02-08 13:26:09 -0800493 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800494 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530495 // Ignore if overriding is enabled
James Feist961bf092020-07-01 16:38:12 -0700496 if (overriddenState)
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530497 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800498 return;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530499 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800500
James Feist961bf092020-07-01 16:38:12 -0700501 if (!readingStateGood())
502 {
503 markAvailable(false);
Konstantin Aladysheve4569252023-03-23 11:08:14 +0300504 for (auto& threshold : thresholds)
505 {
506 assertThresholds(this, value, threshold.level,
507 threshold.direction, false);
508 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200509 updateValueProperty(std::numeric_limits<double>::quiet_NaN());
James Feist961bf092020-07-01 16:38:12 -0700510 return;
511 }
512
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200513 updateValueProperty(newValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700514 updateInstrumentation(newValue);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800515
516 // Always check thresholds after changing the value,
517 // as the test against hysteresisTrigger now takes place in
518 // the thresholds::checkThresholds() method,
519 // which is called by checkThresholds() below,
520 // in all current implementations of sensors that have thresholds.
521 checkThresholds();
James Feist961bf092020-07-01 16:38:12 -0700522 if (!std::isnan(newValue))
523 {
524 markFunctional(true);
525 markAvailable(true);
526 }
James Feistce3fca42018-11-21 12:58:24 -0800527 }
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200528
529 void updateProperty(
530 std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
Ed Tanous2049bd22022-07-09 07:20:26 -0700531 double& oldValue, const double& newValue,
532 const char* dbusPropertyName) const
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200533 {
534 if (requiresUpdate(oldValue, newValue))
535 {
536 oldValue = newValue;
Jae Hyun Yoo1a540b82020-07-30 23:33:18 -0700537 if (interface &&
538 !(interface->set_property(dbusPropertyName, newValue)))
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200539 {
George Liu61984352025-02-24 14:47:34 +0800540 lg2::error("error setting property '{NAME}' to '{VALUE}'",
541 "NAME", dbusPropertyName, "VALUE", newValue);
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200542 }
543 }
544 }
545
Ed Tanous2049bd22022-07-09 07:20:26 -0700546 bool requiresUpdate(const double& lVal, const double& rVal) const
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200547 {
chaul.ampere2a5e2dc2022-07-21 06:03:18 +0000548 const auto lNan = std::isnan(lVal);
549 const auto rNan = std::isnan(rVal);
550 if (lNan || rNan)
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200551 {
chaul.ampere2a5e2dc2022-07-21 06:03:18 +0000552 return (lNan != rNan);
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200553 }
chaul.ampere2a5e2dc2022-07-21 06:03:18 +0000554 return std::abs(lVal - rVal) > hysteresisPublish;
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200555 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200556
557 private:
Konstantin Aladyshev75872ef2021-05-13 11:17:58 +0300558 // If one of the thresholds for a dbus interface is provided
559 // we have to set the other one as dbus properties are never
560 // optional.
561 void fillMissingThresholds()
562 {
Vikash Chandola2d5ee502023-07-06 15:57:09 +0530563 const std::size_t thresholdsLen = thresholds.size();
564 for (std::size_t index = 0; index < thresholdsLen; ++index)
Konstantin Aladyshev75872ef2021-05-13 11:17:58 +0300565 {
Vikash Chandola2d5ee502023-07-06 15:57:09 +0530566 const thresholds::Threshold& thisThreshold = thresholds[index];
Konstantin Aladyshev75872ef2021-05-13 11:17:58 +0300567 bool foundOpposite = false;
568 thresholds::Direction opposite = thresholds::Direction::HIGH;
569 if (thisThreshold.direction == thresholds::Direction::HIGH)
570 {
571 opposite = thresholds::Direction::LOW;
572 }
573 for (thresholds::Threshold& otherThreshold : thresholds)
574 {
575 if (thisThreshold.level != otherThreshold.level)
576 {
577 continue;
578 }
579 if (otherThreshold.direction != opposite)
580 {
581 continue;
582 }
583 foundOpposite = true;
584 break;
585 }
586 if (foundOpposite)
587 {
588 continue;
589 }
590 thresholds.emplace_back(thisThreshold.level, opposite,
591 std::numeric_limits<double>::quiet_NaN());
592 }
593 }
594
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200595 void updateValueProperty(const double& newValue)
596 {
597 // Indicate that it is internal set call, not an external overwrite
598 internalSet = true;
599 updateProperty(sensorInterface, value, newValue, "Value");
600 internalSet = false;
601 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530602};