blob: b98241b50c11ac327d8e108388fb0df951a35a0d [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";
James Feist67601bd2020-06-16 17:14:44 -070021constexpr const char* availableInterfaceName =
22 "xyz.openbmc_project.State.Decorator.Availability";
James Feist961bf092020-07-01 16:38:12 -070023constexpr const char* operationalInterfaceName =
24 "xyz.openbmc_project.State.Decorator.OperationalStatus";
25constexpr const size_t errorThreshold = 5;
26
Josh Lehan3bcd8232020-10-29 00:22:12 -070027struct SensorInstrumentation
28{
29 // These are for instrumentation for debugging
30 int numCollectsGood = 0;
31 int numCollectsMiss = 0;
32 int numStreakGreats = 0;
33 int numStreakMisses = 0;
34 double minCollected = 0.0;
35 double maxCollected = 0.0;
36};
37
James Feist8fd8a582018-11-16 11:10:46 -080038struct Sensor
39{
James Feist930fcde2019-05-28 12:58:43 -070040 Sensor(const std::string& name,
James Feistd8705872019-02-08 13:26:09 -080041 std::vector<thresholds::Threshold>&& thresholdData,
42 const std::string& configurationPath, const std::string& objectType,
Bruce Lee1263c3d2021-06-04 15:16:33 +080043 bool isSettable, const double max, const double min,
James Feiste3338522020-09-15 15:40:30 -070044 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist961bf092020-07-01 16:38:12 -070045 PowerState readState = PowerState::always) :
Ed Tanous6cb732a2021-02-18 15:33:51 -080046 name(sensor_paths::escapePathForDbus(name)),
James Feist930fcde2019-05-28 12:58:43 -070047 configurationPath(configurationPath), objectType(objectType),
Bruce Lee1263c3d2021-06-04 15:16:33 +080048 isSensorSettable(isSettable), maxValue(max), minValue(min),
49 thresholds(std::move(thresholdData)),
Josh Lehan883fb3a2020-02-27 14:41:39 -080050 hysteresisTrigger((max - min) * 0.01),
James Feiste3338522020-09-15 15:40:30 -070051 hysteresisPublish((max - min) * 0.0001), dbusConnection(conn),
Josh Lehan3bcd8232020-10-29 00:22:12 -070052 readState(readState), errCount(0),
53 instrumentation(enableInstrumentation
54 ? std::make_unique<SensorInstrumentation>()
55 : nullptr)
James Feist38fb5982020-05-28 10:09:54 -070056 {}
James Feist8fd8a582018-11-16 11:10:46 -080057 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080058 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070059 std::string name;
James Feistce3fca42018-11-21 12:58:24 -080060 std::string configurationPath;
61 std::string objectType;
Bruce Lee1263c3d2021-06-04 15:16:33 +080062 bool isSensorSettable;
James Feistce3fca42018-11-21 12:58:24 -080063 double maxValue;
64 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080065 std::vector<thresholds::Threshold> thresholds;
66 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
67 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
68 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
James Feist078f2322019-03-08 11:09:05 -080069 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist67601bd2020-06-16 17:14:44 -070070 std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface;
James Feist961bf092020-07-01 16:38:12 -070071 std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface;
James Feist8fd8a582018-11-16 11:10:46 -080072 double value = std::numeric_limits<double>::quiet_NaN();
Zhikui Rend3da1282020-09-11 17:02:01 -070073 double rawValue = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053074 bool overriddenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053075 bool internalSet = false;
Josh Lehan883fb3a2020-02-27 14:41:39 -080076 double hysteresisTrigger;
77 double hysteresisPublish;
James Feiste3338522020-09-15 15:40:30 -070078 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
James Feist961bf092020-07-01 16:38:12 -070079 PowerState readState;
80 size_t errCount;
Josh Lehan3bcd8232020-10-29 00:22:12 -070081 std::unique_ptr<SensorInstrumentation> instrumentation;
82
Josh Lehanffe18342021-03-17 13:29:51 -070083 // This member variable provides a hook that can be used to receive
84 // notification whenever this Sensor's value is externally set via D-Bus.
85 // If interested, assign your own lambda to this variable, during
86 // construction of your Sensor subclass. See ExternalSensor for example.
87 std::function<void()> externalSetHook;
88
Josh Lehan3bcd8232020-10-29 00:22:12 -070089 void updateInstrumentation(double readValue)
90 {
91 // Do nothing if this feature is not enabled
92 if constexpr (!enableInstrumentation)
93 {
94 return;
95 }
96 if (!instrumentation)
97 {
98 return;
99 }
100
101 // Save some typing
102 auto& inst = *instrumentation;
103
104 // Show constants if first reading (even if unsuccessful)
105 if ((inst.numCollectsGood == 0) && (inst.numCollectsMiss == 0))
106 {
107 std::cerr << "Sensor " << name << ": Configuration min=" << minValue
108 << ", max=" << maxValue << ", type=" << objectType
109 << ", path=" << configurationPath << "\n";
110 }
111
112 // Sensors can use "nan" to indicate unavailable reading
113 if (!std::isfinite(readValue))
114 {
115 // Only show this if beginning a new streak
116 if (inst.numStreakMisses == 0)
117 {
118 std::cerr << "Sensor " << name
119 << ": Missing reading, Reading counts good="
120 << inst.numCollectsGood
121 << ", miss=" << inst.numCollectsMiss
122 << ", Prior good streak=" << inst.numStreakGreats
123 << "\n";
124 }
125
126 inst.numStreakGreats = 0;
127 ++(inst.numCollectsMiss);
128 ++(inst.numStreakMisses);
129
130 return;
131 }
132
133 // Only show this if beginning a new streak and not the first time
134 if ((inst.numStreakGreats == 0) && (inst.numCollectsGood != 0))
135 {
136 std::cerr << "Sensor " << name
137 << ": Recovered reading, Reading counts good="
138 << inst.numCollectsGood
139 << ", miss=" << inst.numCollectsMiss
140 << ", Prior miss streak=" << inst.numStreakMisses << "\n";
141 }
142
143 // Initialize min/max if the first successful reading
144 if (inst.numCollectsGood == 0)
145 {
146 std::cerr << "Sensor " << name << ": First reading=" << readValue
147 << "\n";
148
149 inst.minCollected = readValue;
150 inst.maxCollected = readValue;
151 }
152
153 inst.numStreakMisses = 0;
154 ++(inst.numCollectsGood);
155 ++(inst.numStreakGreats);
156
157 // Only provide subsequent output if new min/max established
158 if (readValue < inst.minCollected)
159 {
160 std::cerr << "Sensor " << name << ": Lowest reading=" << readValue
161 << "\n";
162
163 inst.minCollected = readValue;
164 }
165
166 if (readValue > inst.maxCollected)
167 {
168 std::cerr << "Sensor " << name << ": Highest reading=" << readValue
169 << "\n";
170
171 inst.maxCollected = readValue;
172 }
173 }
James Feistce3fca42018-11-21 12:58:24 -0800174
James Feistd8705872019-02-08 13:26:09 -0800175 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530176 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530177 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530178 {
Bruce Lee1263c3d2021-06-04 15:16:33 +0800179 if (insecureSensorOverride == false)
180 { // insecure sesnor override.
181 if (isSensorSettable == false)
182 { // sensor is not settable.
183 if (getManufacturingMode() == false)
184 { // manufacture mode is not enable.
185 throw sdbusplus::exception::SdBusError(
186 -EACCES, "Not allow set porperty value.");
187 }
188 }
189 }
190
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530191 oldValue = newValue;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530192 overriddenState = true;
193 // check thresholds for external set
194 value = newValue;
195 checkThresholds();
Josh Lehanffe18342021-03-17 13:29:51 -0700196
197 // Trigger the hook, as an external set has just happened
198 if (externalSetHook)
199 {
200 externalSetHook();
201 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530202 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530203 else if (!overriddenState)
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530204 {
205 oldValue = newValue;
206 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530207 return 1;
208 }
James Feistce3fca42018-11-21 12:58:24 -0800209
210 void
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800211 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn,
Zev Weiss6b6891c2021-04-22 02:46:21 -0500212 const std::string& unit,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700213 const std::string& label = std::string(),
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800214 size_t thresholdSize = 0)
James Feistce3fca42018-11-21 12:58:24 -0800215 {
James Feist961bf092020-07-01 16:38:12 -0700216 if (readState == PowerState::on || readState == PowerState::biosPost)
217 {
218 setupPowerMatch(conn);
219 }
220
James Feist82bac4c2019-03-11 11:16:53 -0700221 createAssociation(association, configurationPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530222
Zev Weiss6b6891c2021-04-22 02:46:21 -0500223 sensorInterface->register_property("Unit", unit);
James Feistce3fca42018-11-21 12:58:24 -0800224 sensorInterface->register_property("MaxValue", maxValue);
225 sensorInterface->register_property("MinValue", minValue);
226 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -0800227 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800228 return setSensorValue(newValue, oldValue);
229 });
James Feistd8705872019-02-08 13:26:09 -0800230 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -0800231 {
232 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
233 std::string level;
234 std::string alarm;
235 if (threshold.level == thresholds::Level::CRITICAL)
236 {
237 iface = thresholdInterfaceCritical;
238 if (threshold.direction == thresholds::Direction::HIGH)
239 {
240 level = "CriticalHigh";
241 alarm = "CriticalAlarmHigh";
242 }
243 else
244 {
245 level = "CriticalLow";
246 alarm = "CriticalAlarmLow";
247 }
248 }
249 else if (threshold.level == thresholds::Level::WARNING)
250 {
251 iface = thresholdInterfaceWarning;
252 if (threshold.direction == thresholds::Direction::HIGH)
253 {
254 level = "WarningHigh";
255 alarm = "WarningAlarmHigh";
256 }
257 else
258 {
259 level = "WarningLow";
260 alarm = "WarningAlarmLow";
261 }
262 }
263 else
264 {
265 std::cerr << "Unknown threshold level" << threshold.level
266 << "\n";
267 continue;
268 }
269 if (!iface)
270 {
271 std::cout << "trying to set uninitialized interface\n";
272 continue;
273 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800274
275 size_t thresSize =
276 label.empty() ? thresholds.size() : thresholdSize;
James Feistce3fca42018-11-21 12:58:24 -0800277 iface->register_property(
278 level, threshold.value,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800279 [&, label, thresSize](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800280 oldValue = request; // todo, just let the config do this?
281 threshold.value = request;
282 thresholds::persistThreshold(configurationPath, objectType,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800283 threshold, conn, thresSize,
284 label);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800285 // Invalidate previously remembered value,
286 // so new thresholds will be checked during next update,
287 // even if sensor reading remains unchanged.
288 value = std::numeric_limits<double>::quiet_NaN();
289
290 // Although tempting, don't call checkThresholds() from here
291 // directly. Let the regular sensor monitor call the same
292 // using updateValue(), which can check conditions like
293 // poweron, etc., before raising any event.
James Feistce3fca42018-11-21 12:58:24 -0800294 return 1;
295 });
296 iface->register_property(alarm, false);
297 }
298 if (!sensorInterface->initialize())
299 {
300 std::cerr << "error initializing value interface\n";
301 }
302 if (thresholdInterfaceWarning &&
Yong Lif902c052020-05-07 17:13:53 +0800303 !thresholdInterfaceWarning->initialize(true))
James Feistce3fca42018-11-21 12:58:24 -0800304 {
305 std::cerr << "error initializing warning threshold interface\n";
306 }
307
308 if (thresholdInterfaceCritical &&
Yong Lif902c052020-05-07 17:13:53 +0800309 !thresholdInterfaceCritical->initialize(true))
James Feistce3fca42018-11-21 12:58:24 -0800310 {
311 std::cerr << "error initializing critical threshold interface\n";
312 }
James Feist67601bd2020-06-16 17:14:44 -0700313
314 if (!availableInterface)
315 {
316 availableInterface =
317 std::make_shared<sdbusplus::asio::dbus_interface>(
318 conn, sensorInterface->get_object_path(),
319 availableInterfaceName);
320 availableInterface->register_property(
321 "Available", true, [this](const bool propIn, bool& old) {
322 if (propIn == old)
323 {
324 return 1;
325 }
James Feist961bf092020-07-01 16:38:12 -0700326 old = propIn;
James Feist67601bd2020-06-16 17:14:44 -0700327 if (!propIn)
328 {
329 updateValue(std::numeric_limits<double>::quiet_NaN());
330 }
James Feist67601bd2020-06-16 17:14:44 -0700331 return 1;
332 });
333 availableInterface->initialize();
334 }
James Feist961bf092020-07-01 16:38:12 -0700335 if (!operationalInterface)
336 {
337 operationalInterface =
338 std::make_shared<sdbusplus::asio::dbus_interface>(
339 conn, sensorInterface->get_object_path(),
340 operationalInterfaceName);
341 operationalInterface->register_property("Functional", true);
342 operationalInterface->initialize();
343 }
344 }
345
346 bool readingStateGood()
347 {
348 if (readState == PowerState::on && !isPowerOn())
349 {
350 return false;
351 }
352 if (readState == PowerState::biosPost &&
353 (!hasBiosPost() || !isPowerOn()))
354 {
355 return false;
356 }
357
358 return true;
359 }
360
361 void markFunctional(bool isFunctional)
362 {
363 if (operationalInterface)
364 {
365 operationalInterface->set_property("Functional", isFunctional);
366 }
367 if (isFunctional)
368 {
369 errCount = 0;
370 }
371 else
372 {
373 updateValue(std::numeric_limits<double>::quiet_NaN());
374 }
375 }
376
377 void markAvailable(bool isAvailable)
378 {
379 if (availableInterface)
380 {
381 availableInterface->set_property("Available", isAvailable);
382 errCount = 0;
383 }
384 }
385
386 void incrementError()
387 {
388 if (!readingStateGood())
389 {
390 markAvailable(false);
391 return;
392 }
393
394 if (errCount >= errorThreshold)
395 {
396 return;
397 }
398
399 errCount++;
400 if (errCount == errorThreshold)
401 {
402 std::cerr << "Sensor " << name << " reading error!\n";
403 markFunctional(false);
404 }
James Feistce3fca42018-11-21 12:58:24 -0800405 }
406
James Feistd8705872019-02-08 13:26:09 -0800407 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800408 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530409 // Ignore if overriding is enabled
James Feist961bf092020-07-01 16:38:12 -0700410 if (overriddenState)
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530411 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800412 return;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530413 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800414
James Feist961bf092020-07-01 16:38:12 -0700415 if (!readingStateGood())
416 {
417 markAvailable(false);
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200418 updateValueProperty(std::numeric_limits<double>::quiet_NaN());
James Feist961bf092020-07-01 16:38:12 -0700419 return;
420 }
421
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200422 updateValueProperty(newValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700423 updateInstrumentation(newValue);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800424
425 // Always check thresholds after changing the value,
426 // as the test against hysteresisTrigger now takes place in
427 // the thresholds::checkThresholds() method,
428 // which is called by checkThresholds() below,
429 // in all current implementations of sensors that have thresholds.
430 checkThresholds();
James Feist961bf092020-07-01 16:38:12 -0700431 if (!std::isnan(newValue))
432 {
433 markFunctional(true);
434 markAvailable(true);
435 }
James Feistce3fca42018-11-21 12:58:24 -0800436 }
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200437
438 void updateProperty(
439 std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
440 double& oldValue, const double& newValue, const char* dbusPropertyName)
441 {
442 if (requiresUpdate(oldValue, newValue))
443 {
444 oldValue = newValue;
Jae Hyun Yoo1a540b82020-07-30 23:33:18 -0700445 if (interface &&
446 !(interface->set_property(dbusPropertyName, newValue)))
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200447 {
448 std::cerr << "error setting property " << dbusPropertyName
449 << " to " << newValue << "\n";
450 }
451 }
452 }
453
454 bool requiresUpdate(const double& lVal, const double& rVal)
455 {
456 if (std::isnan(lVal) || std::isnan(rVal))
457 {
458 return true;
459 }
460 double diff = std::abs(lVal - rVal);
461 if (diff > hysteresisPublish)
462 {
463 return true;
464 }
465 return false;
466 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200467
468 private:
469 void updateValueProperty(const double& newValue)
470 {
471 // Indicate that it is internal set call, not an external overwrite
472 internalSet = true;
473 updateProperty(sensorInterface, value, newValue, "Value");
474 internalSet = false;
475 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530476};