blob: dc91fc1d3bc41aa9631ff48e03e2bb402a2cec75 [file] [log] [blame]
James Feist8fd8a582018-11-16 11:10:46 -08001#pragma once
2
Ed Tanous6cb732a2021-02-18 15:33:51 -08003#include <SensorPaths.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07004#include <Thresholds.hpp>
5#include <Utils.hpp>
James Feist38fb5982020-05-28 10:09:54 -07006#include <sdbusplus/asio/object_server.hpp>
7
Patrick Venturefd6ba732019-10-31 14:27:39 -07008#include <limits>
9#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070010#include <string>
11#include <vector>
James Feist8fd8a582018-11-16 11:10:46 -080012
James Feist1169eb42018-10-31 10:08:47 -070013constexpr size_t sensorFailedPollTimeMs = 5000;
James Feista5e58722019-04-22 14:43:11 -070014
Josh Lehan3bcd8232020-10-29 00:22:12 -070015// Enable useful logging with sensor instrumentation
16// This is intentionally not DEBUG, avoid clash with usage in .cpp files
17constexpr bool enableInstrumentation = false;
18
James Feista5e58722019-04-22 14:43:11 -070019constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value";
James Feist67601bd2020-06-16 17:14:44 -070020constexpr const char* availableInterfaceName =
21 "xyz.openbmc_project.State.Decorator.Availability";
James Feist961bf092020-07-01 16:38:12 -070022constexpr const char* operationalInterfaceName =
23 "xyz.openbmc_project.State.Decorator.OperationalStatus";
24constexpr const size_t errorThreshold = 5;
25
Josh Lehan3bcd8232020-10-29 00:22:12 -070026struct SensorInstrumentation
27{
28 // These are for instrumentation for debugging
29 int numCollectsGood = 0;
30 int numCollectsMiss = 0;
31 int numStreakGreats = 0;
32 int numStreakMisses = 0;
33 double minCollected = 0.0;
34 double maxCollected = 0.0;
35};
36
James Feist8fd8a582018-11-16 11:10:46 -080037struct Sensor
38{
James Feist930fcde2019-05-28 12:58:43 -070039 Sensor(const std::string& name,
James Feistd8705872019-02-08 13:26:09 -080040 std::vector<thresholds::Threshold>&& thresholdData,
41 const std::string& configurationPath, const std::string& objectType,
James Feist961bf092020-07-01 16:38:12 -070042 const double max, const double min,
James Feiste3338522020-09-15 15:40:30 -070043 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist961bf092020-07-01 16:38:12 -070044 PowerState readState = PowerState::always) :
Ed Tanous6cb732a2021-02-18 15:33:51 -080045 name(sensor_paths::escapePathForDbus(name)),
James Feist930fcde2019-05-28 12:58:43 -070046 configurationPath(configurationPath), objectType(objectType),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050047 maxValue(max), minValue(min), thresholds(std::move(thresholdData)),
Josh Lehan883fb3a2020-02-27 14:41:39 -080048 hysteresisTrigger((max - min) * 0.01),
James Feiste3338522020-09-15 15:40:30 -070049 hysteresisPublish((max - min) * 0.0001), dbusConnection(conn),
Josh Lehan3bcd8232020-10-29 00:22:12 -070050 readState(readState), errCount(0),
51 instrumentation(enableInstrumentation
52 ? std::make_unique<SensorInstrumentation>()
53 : nullptr)
James Feist38fb5982020-05-28 10:09:54 -070054 {}
James Feist8fd8a582018-11-16 11:10:46 -080055 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080056 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070057 std::string name;
James Feistce3fca42018-11-21 12:58:24 -080058 std::string configurationPath;
59 std::string objectType;
60 double maxValue;
61 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080062 std::vector<thresholds::Threshold> thresholds;
63 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
64 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
65 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
James Feist078f2322019-03-08 11:09:05 -080066 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist67601bd2020-06-16 17:14:44 -070067 std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface;
James Feist961bf092020-07-01 16:38:12 -070068 std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface;
James Feist8fd8a582018-11-16 11:10:46 -080069 double value = std::numeric_limits<double>::quiet_NaN();
Zhikui Rend3da1282020-09-11 17:02:01 -070070 double rawValue = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053071 bool overriddenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053072 bool internalSet = false;
Josh Lehan883fb3a2020-02-27 14:41:39 -080073 double hysteresisTrigger;
74 double hysteresisPublish;
James Feiste3338522020-09-15 15:40:30 -070075 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
James Feist961bf092020-07-01 16:38:12 -070076 PowerState readState;
77 size_t errCount;
Josh Lehan3bcd8232020-10-29 00:22:12 -070078 std::unique_ptr<SensorInstrumentation> instrumentation;
79
Josh Lehanffe18342021-03-17 13:29:51 -070080 // This member variable provides a hook that can be used to receive
81 // notification whenever this Sensor's value is externally set via D-Bus.
82 // If interested, assign your own lambda to this variable, during
83 // construction of your Sensor subclass. See ExternalSensor for example.
84 std::function<void()> externalSetHook;
85
Josh Lehan3bcd8232020-10-29 00:22:12 -070086 void updateInstrumentation(double readValue)
87 {
88 // Do nothing if this feature is not enabled
89 if constexpr (!enableInstrumentation)
90 {
91 return;
92 }
93 if (!instrumentation)
94 {
95 return;
96 }
97
98 // Save some typing
99 auto& inst = *instrumentation;
100
101 // Show constants if first reading (even if unsuccessful)
102 if ((inst.numCollectsGood == 0) && (inst.numCollectsMiss == 0))
103 {
104 std::cerr << "Sensor " << name << ": Configuration min=" << minValue
105 << ", max=" << maxValue << ", type=" << objectType
106 << ", path=" << configurationPath << "\n";
107 }
108
109 // Sensors can use "nan" to indicate unavailable reading
110 if (!std::isfinite(readValue))
111 {
112 // Only show this if beginning a new streak
113 if (inst.numStreakMisses == 0)
114 {
115 std::cerr << "Sensor " << name
116 << ": Missing reading, Reading counts good="
117 << inst.numCollectsGood
118 << ", miss=" << inst.numCollectsMiss
119 << ", Prior good streak=" << inst.numStreakGreats
120 << "\n";
121 }
122
123 inst.numStreakGreats = 0;
124 ++(inst.numCollectsMiss);
125 ++(inst.numStreakMisses);
126
127 return;
128 }
129
130 // Only show this if beginning a new streak and not the first time
131 if ((inst.numStreakGreats == 0) && (inst.numCollectsGood != 0))
132 {
133 std::cerr << "Sensor " << name
134 << ": Recovered reading, Reading counts good="
135 << inst.numCollectsGood
136 << ", miss=" << inst.numCollectsMiss
137 << ", Prior miss streak=" << inst.numStreakMisses << "\n";
138 }
139
140 // Initialize min/max if the first successful reading
141 if (inst.numCollectsGood == 0)
142 {
143 std::cerr << "Sensor " << name << ": First reading=" << readValue
144 << "\n";
145
146 inst.minCollected = readValue;
147 inst.maxCollected = readValue;
148 }
149
150 inst.numStreakMisses = 0;
151 ++(inst.numCollectsGood);
152 ++(inst.numStreakGreats);
153
154 // Only provide subsequent output if new min/max established
155 if (readValue < inst.minCollected)
156 {
157 std::cerr << "Sensor " << name << ": Lowest reading=" << readValue
158 << "\n";
159
160 inst.minCollected = readValue;
161 }
162
163 if (readValue > inst.maxCollected)
164 {
165 std::cerr << "Sensor " << name << ": Highest reading=" << readValue
166 << "\n";
167
168 inst.maxCollected = readValue;
169 }
170 }
James Feistce3fca42018-11-21 12:58:24 -0800171
James Feistd8705872019-02-08 13:26:09 -0800172 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530173 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530174 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530175 {
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530176 oldValue = newValue;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530177 overriddenState = true;
178 // check thresholds for external set
179 value = newValue;
180 checkThresholds();
Josh Lehanffe18342021-03-17 13:29:51 -0700181
182 // Trigger the hook, as an external set has just happened
183 if (externalSetHook)
184 {
185 externalSetHook();
186 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530187 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530188 else if (!overriddenState)
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530189 {
190 oldValue = newValue;
191 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530192 return 1;
193 }
James Feistce3fca42018-11-21 12:58:24 -0800194
195 void
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800196 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700197 const std::string& label = std::string(),
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800198 size_t thresholdSize = 0)
James Feistce3fca42018-11-21 12:58:24 -0800199 {
James Feist961bf092020-07-01 16:38:12 -0700200 if (readState == PowerState::on || readState == PowerState::biosPost)
201 {
202 setupPowerMatch(conn);
203 }
204
James Feist82bac4c2019-03-11 11:16:53 -0700205 createAssociation(association, configurationPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530206
James Feistce3fca42018-11-21 12:58:24 -0800207 sensorInterface->register_property("MaxValue", maxValue);
208 sensorInterface->register_property("MinValue", minValue);
209 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -0800210 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800211 return setSensorValue(newValue, oldValue);
212 });
James Feistd8705872019-02-08 13:26:09 -0800213 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -0800214 {
215 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
216 std::string level;
217 std::string alarm;
218 if (threshold.level == thresholds::Level::CRITICAL)
219 {
220 iface = thresholdInterfaceCritical;
221 if (threshold.direction == thresholds::Direction::HIGH)
222 {
223 level = "CriticalHigh";
224 alarm = "CriticalAlarmHigh";
225 }
226 else
227 {
228 level = "CriticalLow";
229 alarm = "CriticalAlarmLow";
230 }
231 }
232 else if (threshold.level == thresholds::Level::WARNING)
233 {
234 iface = thresholdInterfaceWarning;
235 if (threshold.direction == thresholds::Direction::HIGH)
236 {
237 level = "WarningHigh";
238 alarm = "WarningAlarmHigh";
239 }
240 else
241 {
242 level = "WarningLow";
243 alarm = "WarningAlarmLow";
244 }
245 }
246 else
247 {
248 std::cerr << "Unknown threshold level" << threshold.level
249 << "\n";
250 continue;
251 }
252 if (!iface)
253 {
254 std::cout << "trying to set uninitialized interface\n";
255 continue;
256 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800257
258 size_t thresSize =
259 label.empty() ? thresholds.size() : thresholdSize;
James Feistce3fca42018-11-21 12:58:24 -0800260 iface->register_property(
261 level, threshold.value,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800262 [&, label, thresSize](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800263 oldValue = request; // todo, just let the config do this?
264 threshold.value = request;
265 thresholds::persistThreshold(configurationPath, objectType,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800266 threshold, conn, thresSize,
267 label);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800268 // Invalidate previously remembered value,
269 // so new thresholds will be checked during next update,
270 // even if sensor reading remains unchanged.
271 value = std::numeric_limits<double>::quiet_NaN();
272
273 // Although tempting, don't call checkThresholds() from here
274 // directly. Let the regular sensor monitor call the same
275 // using updateValue(), which can check conditions like
276 // poweron, etc., before raising any event.
James Feistce3fca42018-11-21 12:58:24 -0800277 return 1;
278 });
279 iface->register_property(alarm, false);
280 }
281 if (!sensorInterface->initialize())
282 {
283 std::cerr << "error initializing value interface\n";
284 }
285 if (thresholdInterfaceWarning &&
Yong Lif902c052020-05-07 17:13:53 +0800286 !thresholdInterfaceWarning->initialize(true))
James Feistce3fca42018-11-21 12:58:24 -0800287 {
288 std::cerr << "error initializing warning threshold interface\n";
289 }
290
291 if (thresholdInterfaceCritical &&
Yong Lif902c052020-05-07 17:13:53 +0800292 !thresholdInterfaceCritical->initialize(true))
James Feistce3fca42018-11-21 12:58:24 -0800293 {
294 std::cerr << "error initializing critical threshold interface\n";
295 }
James Feist67601bd2020-06-16 17:14:44 -0700296
297 if (!availableInterface)
298 {
299 availableInterface =
300 std::make_shared<sdbusplus::asio::dbus_interface>(
301 conn, sensorInterface->get_object_path(),
302 availableInterfaceName);
303 availableInterface->register_property(
304 "Available", true, [this](const bool propIn, bool& old) {
305 if (propIn == old)
306 {
307 return 1;
308 }
James Feist961bf092020-07-01 16:38:12 -0700309 old = propIn;
James Feist67601bd2020-06-16 17:14:44 -0700310 if (!propIn)
311 {
312 updateValue(std::numeric_limits<double>::quiet_NaN());
313 }
James Feist67601bd2020-06-16 17:14:44 -0700314 return 1;
315 });
316 availableInterface->initialize();
317 }
James Feist961bf092020-07-01 16:38:12 -0700318 if (!operationalInterface)
319 {
320 operationalInterface =
321 std::make_shared<sdbusplus::asio::dbus_interface>(
322 conn, sensorInterface->get_object_path(),
323 operationalInterfaceName);
324 operationalInterface->register_property("Functional", true);
325 operationalInterface->initialize();
326 }
327 }
328
329 bool readingStateGood()
330 {
331 if (readState == PowerState::on && !isPowerOn())
332 {
333 return false;
334 }
335 if (readState == PowerState::biosPost &&
336 (!hasBiosPost() || !isPowerOn()))
337 {
338 return false;
339 }
340
341 return true;
342 }
343
344 void markFunctional(bool isFunctional)
345 {
346 if (operationalInterface)
347 {
348 operationalInterface->set_property("Functional", isFunctional);
349 }
350 if (isFunctional)
351 {
352 errCount = 0;
353 }
354 else
355 {
356 updateValue(std::numeric_limits<double>::quiet_NaN());
357 }
358 }
359
360 void markAvailable(bool isAvailable)
361 {
362 if (availableInterface)
363 {
364 availableInterface->set_property("Available", isAvailable);
365 errCount = 0;
366 }
367 }
368
369 void incrementError()
370 {
371 if (!readingStateGood())
372 {
373 markAvailable(false);
374 return;
375 }
376
377 if (errCount >= errorThreshold)
378 {
379 return;
380 }
381
382 errCount++;
383 if (errCount == errorThreshold)
384 {
385 std::cerr << "Sensor " << name << " reading error!\n";
386 markFunctional(false);
387 }
James Feistce3fca42018-11-21 12:58:24 -0800388 }
389
James Feistd8705872019-02-08 13:26:09 -0800390 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800391 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530392 // Ignore if overriding is enabled
James Feist961bf092020-07-01 16:38:12 -0700393 if (overriddenState)
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530394 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800395 return;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530396 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800397
James Feist961bf092020-07-01 16:38:12 -0700398 if (!readingStateGood())
399 {
400 markAvailable(false);
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200401 updateValueProperty(std::numeric_limits<double>::quiet_NaN());
James Feist961bf092020-07-01 16:38:12 -0700402 return;
403 }
404
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200405 updateValueProperty(newValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700406 updateInstrumentation(newValue);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800407
408 // Always check thresholds after changing the value,
409 // as the test against hysteresisTrigger now takes place in
410 // the thresholds::checkThresholds() method,
411 // which is called by checkThresholds() below,
412 // in all current implementations of sensors that have thresholds.
413 checkThresholds();
James Feist961bf092020-07-01 16:38:12 -0700414 if (!std::isnan(newValue))
415 {
416 markFunctional(true);
417 markAvailable(true);
418 }
James Feistce3fca42018-11-21 12:58:24 -0800419 }
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200420
421 void updateProperty(
422 std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
423 double& oldValue, const double& newValue, const char* dbusPropertyName)
424 {
425 if (requiresUpdate(oldValue, newValue))
426 {
427 oldValue = newValue;
Jae Hyun Yoo1a540b82020-07-30 23:33:18 -0700428 if (interface &&
429 !(interface->set_property(dbusPropertyName, newValue)))
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200430 {
431 std::cerr << "error setting property " << dbusPropertyName
432 << " to " << newValue << "\n";
433 }
434 }
435 }
436
437 bool requiresUpdate(const double& lVal, const double& rVal)
438 {
439 if (std::isnan(lVal) || std::isnan(rVal))
440 {
441 return true;
442 }
443 double diff = std::abs(lVal - rVal);
444 if (diff > hysteresisPublish)
445 {
446 return true;
447 }
448 return false;
449 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200450
451 private:
452 void updateValueProperty(const double& newValue)
453 {
454 // Indicate that it is internal set call, not an external overwrite
455 internalSet = true;
456 updateProperty(sensorInterface, value, newValue, "Value");
457 internalSet = false;
458 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530459};