blob: 12de959f3847a52d69d1592cc115e6decd3edb7c [file] [log] [blame]
James Feist8fd8a582018-11-16 11:10:46 -08001#pragma once
Bruce Lee1263c3d2021-06-04 15:16:33 +08002#include "dbus-sensor_config.h"
James Feist8fd8a582018-11-16 11:10:46 -08003
Ed Tanous6cb732a2021-02-18 15:33:51 -08004#include <SensorPaths.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07005#include <Thresholds.hpp>
6#include <Utils.hpp>
James Feist38fb5982020-05-28 10:09:54 -07007#include <sdbusplus/asio/object_server.hpp>
8
Patrick Venturefd6ba732019-10-31 14:27:39 -07009#include <limits>
10#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070011#include <string>
12#include <vector>
James Feist8fd8a582018-11-16 11:10:46 -080013
James Feist1169eb42018-10-31 10:08:47 -070014constexpr size_t sensorFailedPollTimeMs = 5000;
James Feista5e58722019-04-22 14:43:11 -070015
Josh Lehan3bcd8232020-10-29 00:22:12 -070016// Enable useful logging with sensor instrumentation
17// This is intentionally not DEBUG, avoid clash with usage in .cpp files
18constexpr bool enableInstrumentation = false;
19
James Feista5e58722019-04-22 14:43:11 -070020constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value";
Jie Yang3291b9c2021-07-29 14:46:51 -070021constexpr const char* valueMutabilityInterfaceName =
22 "xyz.openbmc_project.Sensor.ValueMutability";
James Feist67601bd2020-06-16 17:14:44 -070023constexpr const char* availableInterfaceName =
24 "xyz.openbmc_project.State.Decorator.Availability";
James Feist961bf092020-07-01 16:38:12 -070025constexpr const char* operationalInterfaceName =
26 "xyz.openbmc_project.State.Decorator.OperationalStatus";
27constexpr const size_t errorThreshold = 5;
28
Josh Lehan3bcd8232020-10-29 00:22:12 -070029struct SensorInstrumentation
30{
31 // These are for instrumentation for debugging
32 int numCollectsGood = 0;
33 int numCollectsMiss = 0;
34 int numStreakGreats = 0;
35 int numStreakMisses = 0;
36 double minCollected = 0.0;
37 double maxCollected = 0.0;
38};
39
James Feist8fd8a582018-11-16 11:10:46 -080040struct Sensor
41{
James Feist930fcde2019-05-28 12:58:43 -070042 Sensor(const std::string& name,
James Feistd8705872019-02-08 13:26:09 -080043 std::vector<thresholds::Threshold>&& thresholdData,
44 const std::string& configurationPath, const std::string& objectType,
Jie Yang3291b9c2021-07-29 14:46:51 -070045 bool isSettable, bool isMutable, const double max, const double min,
James Feiste3338522020-09-15 15:40:30 -070046 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist961bf092020-07-01 16:38:12 -070047 PowerState readState = PowerState::always) :
Ed Tanous6cb732a2021-02-18 15:33:51 -080048 name(sensor_paths::escapePathForDbus(name)),
James Feist930fcde2019-05-28 12:58:43 -070049 configurationPath(configurationPath), objectType(objectType),
Jie Yang3291b9c2021-07-29 14:46:51 -070050 isSensorSettable(isSettable), isValueMutable(isMutable), maxValue(max),
51 minValue(min), thresholds(std::move(thresholdData)),
Josh Lehan883fb3a2020-02-27 14:41:39 -080052 hysteresisTrigger((max - min) * 0.01),
James Feiste3338522020-09-15 15:40:30 -070053 hysteresisPublish((max - min) * 0.0001), dbusConnection(conn),
Josh Lehan3bcd8232020-10-29 00:22:12 -070054 readState(readState), errCount(0),
55 instrumentation(enableInstrumentation
56 ? std::make_unique<SensorInstrumentation>()
57 : nullptr)
James Feist38fb5982020-05-28 10:09:54 -070058 {}
James Feist8fd8a582018-11-16 11:10:46 -080059 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080060 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070061 std::string name;
James Feistce3fca42018-11-21 12:58:24 -080062 std::string configurationPath;
63 std::string objectType;
Bruce Lee1263c3d2021-06-04 15:16:33 +080064 bool isSensorSettable;
Jie Yang3291b9c2021-07-29 14:46:51 -070065
66 /* A flag indicates if properties of xyz.openbmc_project.Sensor.Value
67 * interface are mutable. If mutable, then
68 * xyz.openbmc_project.Sensor.ValueMutability interface will be
69 * instantiated.
70 */
71 bool isValueMutable;
James Feistce3fca42018-11-21 12:58:24 -080072 double maxValue;
73 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080074 std::vector<thresholds::Threshold> thresholds;
75 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
James Feist078f2322019-03-08 11:09:05 -080076 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist67601bd2020-06-16 17:14:44 -070077 std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface;
James Feist961bf092020-07-01 16:38:12 -070078 std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface;
Jie Yang3291b9c2021-07-29 14:46:51 -070079 std::shared_ptr<sdbusplus::asio::dbus_interface> valueMutabilityInterface;
James Feist8fd8a582018-11-16 11:10:46 -080080 double value = std::numeric_limits<double>::quiet_NaN();
Zhikui Rend3da1282020-09-11 17:02:01 -070081 double rawValue = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053082 bool overriddenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053083 bool internalSet = false;
Josh Lehan883fb3a2020-02-27 14:41:39 -080084 double hysteresisTrigger;
85 double hysteresisPublish;
James Feiste3338522020-09-15 15:40:30 -070086 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
James Feist961bf092020-07-01 16:38:12 -070087 PowerState readState;
88 size_t errCount;
Josh Lehan3bcd8232020-10-29 00:22:12 -070089 std::unique_ptr<SensorInstrumentation> instrumentation;
90
Josh Lehanffe18342021-03-17 13:29:51 -070091 // This member variable provides a hook that can be used to receive
92 // notification whenever this Sensor's value is externally set via D-Bus.
93 // If interested, assign your own lambda to this variable, during
94 // construction of your Sensor subclass. See ExternalSensor for example.
95 std::function<void()> externalSetHook;
96
Ed Tanousc8fed202022-01-12 14:24:45 -080097 using Level = thresholds::Level;
98 using Direction = thresholds::Direction;
Jayashree Dhanapal45f27022021-12-07 12:56:35 +053099
Ed Tanousc8fed202022-01-12 14:24:45 -0800100 std::array<std::shared_ptr<sdbusplus::asio::dbus_interface>,
101 thresholds::thresProp.size()>
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530102 thresholdInterfaces;
103
104 std::shared_ptr<sdbusplus::asio::dbus_interface>
Ed Tanousc8fed202022-01-12 14:24:45 -0800105 getThresholdInterface(Level lev)
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530106 {
107 size_t index = static_cast<size_t>(lev);
108 if (index >= thresholdInterfaces.size())
109 {
110 std::cout << "Unknown threshold level \n";
111 return nullptr;
112 }
113 std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
114 thresholdInterfaces[index];
115 return interface;
116 }
117
Josh Lehan3bcd8232020-10-29 00:22:12 -0700118 void updateInstrumentation(double readValue)
119 {
120 // Do nothing if this feature is not enabled
121 if constexpr (!enableInstrumentation)
122 {
123 return;
124 }
125 if (!instrumentation)
126 {
127 return;
128 }
129
130 // Save some typing
131 auto& inst = *instrumentation;
132
133 // Show constants if first reading (even if unsuccessful)
134 if ((inst.numCollectsGood == 0) && (inst.numCollectsMiss == 0))
135 {
136 std::cerr << "Sensor " << name << ": Configuration min=" << minValue
137 << ", max=" << maxValue << ", type=" << objectType
138 << ", path=" << configurationPath << "\n";
139 }
140
141 // Sensors can use "nan" to indicate unavailable reading
142 if (!std::isfinite(readValue))
143 {
144 // Only show this if beginning a new streak
145 if (inst.numStreakMisses == 0)
146 {
147 std::cerr << "Sensor " << name
148 << ": Missing reading, Reading counts good="
149 << inst.numCollectsGood
150 << ", miss=" << inst.numCollectsMiss
151 << ", Prior good streak=" << inst.numStreakGreats
152 << "\n";
153 }
154
155 inst.numStreakGreats = 0;
156 ++(inst.numCollectsMiss);
157 ++(inst.numStreakMisses);
158
159 return;
160 }
161
162 // Only show this if beginning a new streak and not the first time
163 if ((inst.numStreakGreats == 0) && (inst.numCollectsGood != 0))
164 {
165 std::cerr << "Sensor " << name
166 << ": Recovered reading, Reading counts good="
167 << inst.numCollectsGood
168 << ", miss=" << inst.numCollectsMiss
169 << ", Prior miss streak=" << inst.numStreakMisses << "\n";
170 }
171
172 // Initialize min/max if the first successful reading
173 if (inst.numCollectsGood == 0)
174 {
175 std::cerr << "Sensor " << name << ": First reading=" << readValue
176 << "\n";
177
178 inst.minCollected = readValue;
179 inst.maxCollected = readValue;
180 }
181
182 inst.numStreakMisses = 0;
183 ++(inst.numCollectsGood);
184 ++(inst.numStreakGreats);
185
186 // Only provide subsequent output if new min/max established
187 if (readValue < inst.minCollected)
188 {
189 std::cerr << "Sensor " << name << ": Lowest reading=" << readValue
190 << "\n";
191
192 inst.minCollected = readValue;
193 }
194
195 if (readValue > inst.maxCollected)
196 {
197 std::cerr << "Sensor " << name << ": Highest reading=" << readValue
198 << "\n";
199
200 inst.maxCollected = readValue;
201 }
202 }
James Feistce3fca42018-11-21 12:58:24 -0800203
James Feistd8705872019-02-08 13:26:09 -0800204 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530205 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530206 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530207 {
Ed Tanous74cffa82022-01-25 13:00:28 -0800208 if (insecureSensorOverride == 0)
Bruce Lee1263c3d2021-06-04 15:16:33 +0800209 { // insecure sesnor override.
210 if (isSensorSettable == false)
211 { // sensor is not settable.
212 if (getManufacturingMode() == false)
213 { // manufacture mode is not enable.
Patrick Williams379b1132021-09-02 05:42:25 -0500214 std::cerr << "Sensor " << name
215 << ": Not allowed to set property value.\n";
216 return -EACCES;
Bruce Lee1263c3d2021-06-04 15:16:33 +0800217 }
218 }
219 }
220
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530221 oldValue = newValue;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530222 overriddenState = true;
223 // check thresholds for external set
224 value = newValue;
225 checkThresholds();
Josh Lehanffe18342021-03-17 13:29:51 -0700226
227 // Trigger the hook, as an external set has just happened
228 if (externalSetHook)
229 {
230 externalSetHook();
231 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530232 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530233 else if (!overriddenState)
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530234 {
235 oldValue = newValue;
236 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530237 return 1;
238 }
James Feistce3fca42018-11-21 12:58:24 -0800239
Andrei Kartashev39287412022-02-04 16:04:47 +0300240 void setInitialProperties(const std::string& unit,
241 const std::string& label = std::string(),
242 size_t thresholdSize = 0)
James Feistce3fca42018-11-21 12:58:24 -0800243 {
James Feist961bf092020-07-01 16:38:12 -0700244 if (readState == PowerState::on || readState == PowerState::biosPost)
245 {
Andrei Kartashev39287412022-02-04 16:04:47 +0300246 setupPowerMatch(dbusConnection);
James Feist961bf092020-07-01 16:38:12 -0700247 }
248
James Feist82bac4c2019-03-11 11:16:53 -0700249 createAssociation(association, configurationPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530250
Zev Weiss6b6891c2021-04-22 02:46:21 -0500251 sensorInterface->register_property("Unit", unit);
James Feistce3fca42018-11-21 12:58:24 -0800252 sensorInterface->register_property("MaxValue", maxValue);
253 sensorInterface->register_property("MinValue", minValue);
254 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -0800255 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800256 return setSensorValue(newValue, oldValue);
257 });
James Feistd8705872019-02-08 13:26:09 -0800258 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -0800259 {
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000260 if (std::isnan(threshold.hysteresis))
261 {
262 threshold.hysteresis = hysteresisTrigger;
263 }
Ed Tanous17551b82022-01-26 16:47:17 -0800264
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530265 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
266 getThresholdInterface(threshold.level);
267
James Feistce3fca42018-11-21 12:58:24 -0800268 if (!iface)
269 {
270 std::cout << "trying to set uninitialized interface\n";
271 continue;
272 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800273
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530274 std::string level =
275 propertyLevel(threshold.level, threshold.direction);
276 std::string alarm =
277 propertyAlarm(threshold.level, threshold.direction);
278
279 if ((level.empty()) || (alarm.empty()))
280 {
281 continue;
282 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800283 size_t thresSize =
284 label.empty() ? thresholds.size() : thresholdSize;
James Feistce3fca42018-11-21 12:58:24 -0800285 iface->register_property(
286 level, threshold.value,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800287 [&, label, thresSize](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800288 oldValue = request; // todo, just let the config do this?
289 threshold.value = request;
290 thresholds::persistThreshold(configurationPath, objectType,
Andrei Kartashev39287412022-02-04 16:04:47 +0300291 threshold, dbusConnection,
292 thresSize, label);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800293 // Invalidate previously remembered value,
294 // so new thresholds will be checked during next update,
295 // even if sensor reading remains unchanged.
296 value = std::numeric_limits<double>::quiet_NaN();
297
298 // Although tempting, don't call checkThresholds() from here
299 // directly. Let the regular sensor monitor call the same
300 // using updateValue(), which can check conditions like
301 // poweron, etc., before raising any event.
James Feistce3fca42018-11-21 12:58:24 -0800302 return 1;
303 });
304 iface->register_property(alarm, false);
305 }
306 if (!sensorInterface->initialize())
307 {
308 std::cerr << "error initializing value interface\n";
309 }
James Feistce3fca42018-11-21 12:58:24 -0800310
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530311 for (auto& thresIface : thresholdInterfaces)
James Feistce3fca42018-11-21 12:58:24 -0800312 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530313 if (thresIface)
314 {
315 if (!thresIface->initialize(true))
316 {
317 std::cerr << "Error initializing threshold interface \n";
318 }
319 }
James Feistce3fca42018-11-21 12:58:24 -0800320 }
James Feist67601bd2020-06-16 17:14:44 -0700321
Jie Yang3291b9c2021-07-29 14:46:51 -0700322 if (isValueMutable)
323 {
324 valueMutabilityInterface =
325 std::make_shared<sdbusplus::asio::dbus_interface>(
Andrei Kartashev39287412022-02-04 16:04:47 +0300326 dbusConnection, sensorInterface->get_object_path(),
Jie Yang3291b9c2021-07-29 14:46:51 -0700327 valueMutabilityInterfaceName);
328 valueMutabilityInterface->register_property("Mutable", true);
329 if (!valueMutabilityInterface->initialize())
330 {
331 std::cerr
332 << "error initializing sensor value mutability interface\n";
333 valueMutabilityInterface = nullptr;
334 }
335 }
336
James Feist67601bd2020-06-16 17:14:44 -0700337 if (!availableInterface)
338 {
339 availableInterface =
340 std::make_shared<sdbusplus::asio::dbus_interface>(
Andrei Kartashev39287412022-02-04 16:04:47 +0300341 dbusConnection, sensorInterface->get_object_path(),
James Feist67601bd2020-06-16 17:14:44 -0700342 availableInterfaceName);
343 availableInterface->register_property(
344 "Available", true, [this](const bool propIn, bool& old) {
345 if (propIn == old)
346 {
347 return 1;
348 }
James Feist961bf092020-07-01 16:38:12 -0700349 old = propIn;
James Feist67601bd2020-06-16 17:14:44 -0700350 if (!propIn)
351 {
352 updateValue(std::numeric_limits<double>::quiet_NaN());
353 }
James Feist67601bd2020-06-16 17:14:44 -0700354 return 1;
355 });
356 availableInterface->initialize();
357 }
James Feist961bf092020-07-01 16:38:12 -0700358 if (!operationalInterface)
359 {
360 operationalInterface =
361 std::make_shared<sdbusplus::asio::dbus_interface>(
Andrei Kartashev39287412022-02-04 16:04:47 +0300362 dbusConnection, sensorInterface->get_object_path(),
James Feist961bf092020-07-01 16:38:12 -0700363 operationalInterfaceName);
364 operationalInterface->register_property("Functional", true);
365 operationalInterface->initialize();
366 }
367 }
368
Ed Tanousc8fed202022-01-12 14:24:45 -0800369 std::string propertyLevel(const Level lev, const Direction dir)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530370 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800371 for (const thresholds::ThresholdDefinition& prop :
372 thresholds::thresProp)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530373 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800374 if (prop.level == lev)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530375 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800376 if (dir == Direction::HIGH)
377 {
378 return std::string(prop.levelName) + "High";
379 }
380 if (dir == Direction::LOW)
381 {
382 return std::string(prop.levelName) + "Low";
383 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530384 }
385 }
386 return "";
387 }
388
Ed Tanousc8fed202022-01-12 14:24:45 -0800389 std::string propertyAlarm(const Level lev, const Direction dir)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530390 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800391 for (const thresholds::ThresholdDefinition& prop :
392 thresholds::thresProp)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530393 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800394 if (prop.level == lev)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530395 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800396 if (dir == Direction::HIGH)
397 {
398 return std::string(prop.levelName) + "AlarmHigh";
399 }
400 if (dir == Direction::LOW)
401 {
402 return std::string(prop.levelName) + "AlarmLow";
403 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530404 }
405 }
406 return "";
407 }
408
James Feist961bf092020-07-01 16:38:12 -0700409 bool readingStateGood()
410 {
411 if (readState == PowerState::on && !isPowerOn())
412 {
413 return false;
414 }
415 if (readState == PowerState::biosPost &&
416 (!hasBiosPost() || !isPowerOn()))
417 {
418 return false;
419 }
420
421 return true;
422 }
423
424 void markFunctional(bool isFunctional)
425 {
426 if (operationalInterface)
427 {
428 operationalInterface->set_property("Functional", isFunctional);
429 }
430 if (isFunctional)
431 {
432 errCount = 0;
433 }
434 else
435 {
436 updateValue(std::numeric_limits<double>::quiet_NaN());
437 }
438 }
439
440 void markAvailable(bool isAvailable)
441 {
442 if (availableInterface)
443 {
444 availableInterface->set_property("Available", isAvailable);
445 errCount = 0;
446 }
447 }
448
449 void incrementError()
450 {
451 if (!readingStateGood())
452 {
453 markAvailable(false);
454 return;
455 }
456
457 if (errCount >= errorThreshold)
458 {
459 return;
460 }
461
462 errCount++;
463 if (errCount == errorThreshold)
464 {
465 std::cerr << "Sensor " << name << " reading error!\n";
466 markFunctional(false);
467 }
James Feistce3fca42018-11-21 12:58:24 -0800468 }
469
James Feistd8705872019-02-08 13:26:09 -0800470 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800471 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530472 // Ignore if overriding is enabled
James Feist961bf092020-07-01 16:38:12 -0700473 if (overriddenState)
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530474 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800475 return;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530476 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800477
James Feist961bf092020-07-01 16:38:12 -0700478 if (!readingStateGood())
479 {
480 markAvailable(false);
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200481 updateValueProperty(std::numeric_limits<double>::quiet_NaN());
James Feist961bf092020-07-01 16:38:12 -0700482 return;
483 }
484
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200485 updateValueProperty(newValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700486 updateInstrumentation(newValue);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800487
488 // Always check thresholds after changing the value,
489 // as the test against hysteresisTrigger now takes place in
490 // the thresholds::checkThresholds() method,
491 // which is called by checkThresholds() below,
492 // in all current implementations of sensors that have thresholds.
493 checkThresholds();
James Feist961bf092020-07-01 16:38:12 -0700494 if (!std::isnan(newValue))
495 {
496 markFunctional(true);
497 markAvailable(true);
498 }
James Feistce3fca42018-11-21 12:58:24 -0800499 }
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200500
501 void updateProperty(
502 std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
503 double& oldValue, const double& newValue, const char* dbusPropertyName)
504 {
505 if (requiresUpdate(oldValue, newValue))
506 {
507 oldValue = newValue;
Jae Hyun Yoo1a540b82020-07-30 23:33:18 -0700508 if (interface &&
509 !(interface->set_property(dbusPropertyName, newValue)))
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200510 {
511 std::cerr << "error setting property " << dbusPropertyName
512 << " to " << newValue << "\n";
513 }
514 }
515 }
516
517 bool requiresUpdate(const double& lVal, const double& rVal)
518 {
519 if (std::isnan(lVal) || std::isnan(rVal))
520 {
521 return true;
522 }
523 double diff = std::abs(lVal - rVal);
524 if (diff > hysteresisPublish)
525 {
526 return true;
527 }
528 return false;
529 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200530
531 private:
532 void updateValueProperty(const double& newValue)
533 {
534 // Indicate that it is internal set call, not an external overwrite
535 internalSet = true;
536 updateProperty(sensorInterface, value, newValue, "Value");
537 internalSet = false;
538 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530539};