blob: 40f4223a59ad2db80761f126cae970578d988308 [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
240 void
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800241 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn,
Zev Weiss6b6891c2021-04-22 02:46:21 -0500242 const std::string& unit,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700243 const std::string& label = std::string(),
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800244 size_t thresholdSize = 0)
James Feistce3fca42018-11-21 12:58:24 -0800245 {
James Feist961bf092020-07-01 16:38:12 -0700246 if (readState == PowerState::on || readState == PowerState::biosPost)
247 {
248 setupPowerMatch(conn);
249 }
250
James Feist82bac4c2019-03-11 11:16:53 -0700251 createAssociation(association, configurationPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530252
Zev Weiss6b6891c2021-04-22 02:46:21 -0500253 sensorInterface->register_property("Unit", unit);
James Feistce3fca42018-11-21 12:58:24 -0800254 sensorInterface->register_property("MaxValue", maxValue);
255 sensorInterface->register_property("MinValue", minValue);
256 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -0800257 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800258 return setSensorValue(newValue, oldValue);
259 });
James Feistd8705872019-02-08 13:26:09 -0800260 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -0800261 {
Rashmica Gupta1e34cec2021-08-31 16:47:39 +1000262 if (std::isnan(threshold.hysteresis))
263 {
264 threshold.hysteresis = hysteresisTrigger;
265 }
Ed Tanousc8fed202022-01-12 14:24:45 -0800266 if (!thresholds::isValidLevel(threshold.level))
James Feistce3fca42018-11-21 12:58:24 -0800267 {
James Feistce3fca42018-11-21 12:58:24 -0800268 continue;
269 }
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530270 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
271 getThresholdInterface(threshold.level);
272
James Feistce3fca42018-11-21 12:58:24 -0800273 if (!iface)
274 {
275 std::cout << "trying to set uninitialized interface\n";
276 continue;
277 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800278
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530279 std::string level =
280 propertyLevel(threshold.level, threshold.direction);
281 std::string alarm =
282 propertyAlarm(threshold.level, threshold.direction);
283
284 if ((level.empty()) || (alarm.empty()))
285 {
286 continue;
287 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800288 size_t thresSize =
289 label.empty() ? thresholds.size() : thresholdSize;
James Feistce3fca42018-11-21 12:58:24 -0800290 iface->register_property(
291 level, threshold.value,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800292 [&, label, thresSize](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800293 oldValue = request; // todo, just let the config do this?
294 threshold.value = request;
295 thresholds::persistThreshold(configurationPath, objectType,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800296 threshold, conn, thresSize,
297 label);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800298 // Invalidate previously remembered value,
299 // so new thresholds will be checked during next update,
300 // even if sensor reading remains unchanged.
301 value = std::numeric_limits<double>::quiet_NaN();
302
303 // Although tempting, don't call checkThresholds() from here
304 // directly. Let the regular sensor monitor call the same
305 // using updateValue(), which can check conditions like
306 // poweron, etc., before raising any event.
James Feistce3fca42018-11-21 12:58:24 -0800307 return 1;
308 });
309 iface->register_property(alarm, false);
310 }
311 if (!sensorInterface->initialize())
312 {
313 std::cerr << "error initializing value interface\n";
314 }
James Feistce3fca42018-11-21 12:58:24 -0800315
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530316 for (auto& thresIface : thresholdInterfaces)
James Feistce3fca42018-11-21 12:58:24 -0800317 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530318 if (thresIface)
319 {
320 if (!thresIface->initialize(true))
321 {
322 std::cerr << "Error initializing threshold interface \n";
323 }
324 }
James Feistce3fca42018-11-21 12:58:24 -0800325 }
James Feist67601bd2020-06-16 17:14:44 -0700326
Jie Yang3291b9c2021-07-29 14:46:51 -0700327 if (isValueMutable)
328 {
329 valueMutabilityInterface =
330 std::make_shared<sdbusplus::asio::dbus_interface>(
331 conn, sensorInterface->get_object_path(),
332 valueMutabilityInterfaceName);
333 valueMutabilityInterface->register_property("Mutable", true);
334 if (!valueMutabilityInterface->initialize())
335 {
336 std::cerr
337 << "error initializing sensor value mutability interface\n";
338 valueMutabilityInterface = nullptr;
339 }
340 }
341
James Feist67601bd2020-06-16 17:14:44 -0700342 if (!availableInterface)
343 {
344 availableInterface =
345 std::make_shared<sdbusplus::asio::dbus_interface>(
346 conn, sensorInterface->get_object_path(),
347 availableInterfaceName);
348 availableInterface->register_property(
349 "Available", true, [this](const bool propIn, bool& old) {
350 if (propIn == old)
351 {
352 return 1;
353 }
James Feist961bf092020-07-01 16:38:12 -0700354 old = propIn;
James Feist67601bd2020-06-16 17:14:44 -0700355 if (!propIn)
356 {
357 updateValue(std::numeric_limits<double>::quiet_NaN());
358 }
James Feist67601bd2020-06-16 17:14:44 -0700359 return 1;
360 });
361 availableInterface->initialize();
362 }
James Feist961bf092020-07-01 16:38:12 -0700363 if (!operationalInterface)
364 {
365 operationalInterface =
366 std::make_shared<sdbusplus::asio::dbus_interface>(
367 conn, sensorInterface->get_object_path(),
368 operationalInterfaceName);
369 operationalInterface->register_property("Functional", true);
370 operationalInterface->initialize();
371 }
372 }
373
Ed Tanousc8fed202022-01-12 14:24:45 -0800374 std::string propertyLevel(const Level lev, const Direction dir)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530375 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800376 for (const thresholds::ThresholdDefinition& prop :
377 thresholds::thresProp)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530378 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800379 if (prop.level == lev)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530380 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800381 if (dir == Direction::HIGH)
382 {
383 return std::string(prop.levelName) + "High";
384 }
385 if (dir == Direction::LOW)
386 {
387 return std::string(prop.levelName) + "Low";
388 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530389 }
390 }
391 return "";
392 }
393
Ed Tanousc8fed202022-01-12 14:24:45 -0800394 std::string propertyAlarm(const Level lev, const Direction dir)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530395 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800396 for (const thresholds::ThresholdDefinition& prop :
397 thresholds::thresProp)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530398 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800399 if (prop.level == lev)
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530400 {
Ed Tanousc8fed202022-01-12 14:24:45 -0800401 if (dir == Direction::HIGH)
402 {
403 return std::string(prop.levelName) + "AlarmHigh";
404 }
405 if (dir == Direction::LOW)
406 {
407 return std::string(prop.levelName) + "AlarmLow";
408 }
Jayashree Dhanapal45f27022021-12-07 12:56:35 +0530409 }
410 }
411 return "";
412 }
413
James Feist961bf092020-07-01 16:38:12 -0700414 bool readingStateGood()
415 {
416 if (readState == PowerState::on && !isPowerOn())
417 {
418 return false;
419 }
420 if (readState == PowerState::biosPost &&
421 (!hasBiosPost() || !isPowerOn()))
422 {
423 return false;
424 }
425
426 return true;
427 }
428
429 void markFunctional(bool isFunctional)
430 {
431 if (operationalInterface)
432 {
433 operationalInterface->set_property("Functional", isFunctional);
434 }
435 if (isFunctional)
436 {
437 errCount = 0;
438 }
439 else
440 {
441 updateValue(std::numeric_limits<double>::quiet_NaN());
442 }
443 }
444
445 void markAvailable(bool isAvailable)
446 {
447 if (availableInterface)
448 {
449 availableInterface->set_property("Available", isAvailable);
450 errCount = 0;
451 }
452 }
453
454 void incrementError()
455 {
456 if (!readingStateGood())
457 {
458 markAvailable(false);
459 return;
460 }
461
462 if (errCount >= errorThreshold)
463 {
464 return;
465 }
466
467 errCount++;
468 if (errCount == errorThreshold)
469 {
470 std::cerr << "Sensor " << name << " reading error!\n";
471 markFunctional(false);
472 }
James Feistce3fca42018-11-21 12:58:24 -0800473 }
474
James Feistd8705872019-02-08 13:26:09 -0800475 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800476 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530477 // Ignore if overriding is enabled
James Feist961bf092020-07-01 16:38:12 -0700478 if (overriddenState)
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530479 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800480 return;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530481 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800482
James Feist961bf092020-07-01 16:38:12 -0700483 if (!readingStateGood())
484 {
485 markAvailable(false);
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200486 updateValueProperty(std::numeric_limits<double>::quiet_NaN());
James Feist961bf092020-07-01 16:38:12 -0700487 return;
488 }
489
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200490 updateValueProperty(newValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700491 updateInstrumentation(newValue);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800492
493 // Always check thresholds after changing the value,
494 // as the test against hysteresisTrigger now takes place in
495 // the thresholds::checkThresholds() method,
496 // which is called by checkThresholds() below,
497 // in all current implementations of sensors that have thresholds.
498 checkThresholds();
James Feist961bf092020-07-01 16:38:12 -0700499 if (!std::isnan(newValue))
500 {
501 markFunctional(true);
502 markAvailable(true);
503 }
James Feistce3fca42018-11-21 12:58:24 -0800504 }
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200505
506 void updateProperty(
507 std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
508 double& oldValue, const double& newValue, const char* dbusPropertyName)
509 {
510 if (requiresUpdate(oldValue, newValue))
511 {
512 oldValue = newValue;
Jae Hyun Yoo1a540b82020-07-30 23:33:18 -0700513 if (interface &&
514 !(interface->set_property(dbusPropertyName, newValue)))
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200515 {
516 std::cerr << "error setting property " << dbusPropertyName
517 << " to " << newValue << "\n";
518 }
519 }
520 }
521
522 bool requiresUpdate(const double& lVal, const double& rVal)
523 {
524 if (std::isnan(lVal) || std::isnan(rVal))
525 {
526 return true;
527 }
528 double diff = std::abs(lVal - rVal);
529 if (diff > hysteresisPublish)
530 {
531 return true;
532 }
533 return false;
534 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200535
536 private:
537 void updateValueProperty(const double& newValue)
538 {
539 // Indicate that it is internal set call, not an external overwrite
540 internalSet = true;
541 updateProperty(sensorInterface, value, newValue, "Value");
542 internalSet = false;
543 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530544};