blob: 0f90118a75cfdfaed5c9be523ac05aa987298bf4 [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
80 void updateInstrumentation(double readValue)
81 {
82 // Do nothing if this feature is not enabled
83 if constexpr (!enableInstrumentation)
84 {
85 return;
86 }
87 if (!instrumentation)
88 {
89 return;
90 }
91
92 // Save some typing
93 auto& inst = *instrumentation;
94
95 // Show constants if first reading (even if unsuccessful)
96 if ((inst.numCollectsGood == 0) && (inst.numCollectsMiss == 0))
97 {
98 std::cerr << "Sensor " << name << ": Configuration min=" << minValue
99 << ", max=" << maxValue << ", type=" << objectType
100 << ", path=" << configurationPath << "\n";
101 }
102
103 // Sensors can use "nan" to indicate unavailable reading
104 if (!std::isfinite(readValue))
105 {
106 // Only show this if beginning a new streak
107 if (inst.numStreakMisses == 0)
108 {
109 std::cerr << "Sensor " << name
110 << ": Missing reading, Reading counts good="
111 << inst.numCollectsGood
112 << ", miss=" << inst.numCollectsMiss
113 << ", Prior good streak=" << inst.numStreakGreats
114 << "\n";
115 }
116
117 inst.numStreakGreats = 0;
118 ++(inst.numCollectsMiss);
119 ++(inst.numStreakMisses);
120
121 return;
122 }
123
124 // Only show this if beginning a new streak and not the first time
125 if ((inst.numStreakGreats == 0) && (inst.numCollectsGood != 0))
126 {
127 std::cerr << "Sensor " << name
128 << ": Recovered reading, Reading counts good="
129 << inst.numCollectsGood
130 << ", miss=" << inst.numCollectsMiss
131 << ", Prior miss streak=" << inst.numStreakMisses << "\n";
132 }
133
134 // Initialize min/max if the first successful reading
135 if (inst.numCollectsGood == 0)
136 {
137 std::cerr << "Sensor " << name << ": First reading=" << readValue
138 << "\n";
139
140 inst.minCollected = readValue;
141 inst.maxCollected = readValue;
142 }
143
144 inst.numStreakMisses = 0;
145 ++(inst.numCollectsGood);
146 ++(inst.numStreakGreats);
147
148 // Only provide subsequent output if new min/max established
149 if (readValue < inst.minCollected)
150 {
151 std::cerr << "Sensor " << name << ": Lowest reading=" << readValue
152 << "\n";
153
154 inst.minCollected = readValue;
155 }
156
157 if (readValue > inst.maxCollected)
158 {
159 std::cerr << "Sensor " << name << ": Highest reading=" << readValue
160 << "\n";
161
162 inst.maxCollected = readValue;
163 }
164 }
James Feistce3fca42018-11-21 12:58:24 -0800165
James Feistd8705872019-02-08 13:26:09 -0800166 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530167 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530168 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530169 {
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530170 oldValue = newValue;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530171 overriddenState = true;
172 // check thresholds for external set
173 value = newValue;
174 checkThresholds();
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530175 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530176 else if (!overriddenState)
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530177 {
178 oldValue = newValue;
179 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530180 return 1;
181 }
James Feistce3fca42018-11-21 12:58:24 -0800182
183 void
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800184 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700185 const std::string& label = std::string(),
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800186 size_t thresholdSize = 0)
James Feistce3fca42018-11-21 12:58:24 -0800187 {
James Feist961bf092020-07-01 16:38:12 -0700188 if (readState == PowerState::on || readState == PowerState::biosPost)
189 {
190 setupPowerMatch(conn);
191 }
192
James Feist82bac4c2019-03-11 11:16:53 -0700193 createAssociation(association, configurationPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530194
James Feistce3fca42018-11-21 12:58:24 -0800195 sensorInterface->register_property("MaxValue", maxValue);
196 sensorInterface->register_property("MinValue", minValue);
197 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -0800198 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800199 return setSensorValue(newValue, oldValue);
200 });
James Feistd8705872019-02-08 13:26:09 -0800201 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -0800202 {
203 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
204 std::string level;
205 std::string alarm;
206 if (threshold.level == thresholds::Level::CRITICAL)
207 {
208 iface = thresholdInterfaceCritical;
209 if (threshold.direction == thresholds::Direction::HIGH)
210 {
211 level = "CriticalHigh";
212 alarm = "CriticalAlarmHigh";
213 }
214 else
215 {
216 level = "CriticalLow";
217 alarm = "CriticalAlarmLow";
218 }
219 }
220 else if (threshold.level == thresholds::Level::WARNING)
221 {
222 iface = thresholdInterfaceWarning;
223 if (threshold.direction == thresholds::Direction::HIGH)
224 {
225 level = "WarningHigh";
226 alarm = "WarningAlarmHigh";
227 }
228 else
229 {
230 level = "WarningLow";
231 alarm = "WarningAlarmLow";
232 }
233 }
234 else
235 {
236 std::cerr << "Unknown threshold level" << threshold.level
237 << "\n";
238 continue;
239 }
240 if (!iface)
241 {
242 std::cout << "trying to set uninitialized interface\n";
243 continue;
244 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800245
246 size_t thresSize =
247 label.empty() ? thresholds.size() : thresholdSize;
James Feistce3fca42018-11-21 12:58:24 -0800248 iface->register_property(
249 level, threshold.value,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800250 [&, label, thresSize](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800251 oldValue = request; // todo, just let the config do this?
252 threshold.value = request;
253 thresholds::persistThreshold(configurationPath, objectType,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800254 threshold, conn, thresSize,
255 label);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800256 // Invalidate previously remembered value,
257 // so new thresholds will be checked during next update,
258 // even if sensor reading remains unchanged.
259 value = std::numeric_limits<double>::quiet_NaN();
260
261 // Although tempting, don't call checkThresholds() from here
262 // directly. Let the regular sensor monitor call the same
263 // using updateValue(), which can check conditions like
264 // poweron, etc., before raising any event.
James Feistce3fca42018-11-21 12:58:24 -0800265 return 1;
266 });
267 iface->register_property(alarm, false);
268 }
269 if (!sensorInterface->initialize())
270 {
271 std::cerr << "error initializing value interface\n";
272 }
273 if (thresholdInterfaceWarning &&
Yong Lif902c052020-05-07 17:13:53 +0800274 !thresholdInterfaceWarning->initialize(true))
James Feistce3fca42018-11-21 12:58:24 -0800275 {
276 std::cerr << "error initializing warning threshold interface\n";
277 }
278
279 if (thresholdInterfaceCritical &&
Yong Lif902c052020-05-07 17:13:53 +0800280 !thresholdInterfaceCritical->initialize(true))
James Feistce3fca42018-11-21 12:58:24 -0800281 {
282 std::cerr << "error initializing critical threshold interface\n";
283 }
James Feist67601bd2020-06-16 17:14:44 -0700284
285 if (!availableInterface)
286 {
287 availableInterface =
288 std::make_shared<sdbusplus::asio::dbus_interface>(
289 conn, sensorInterface->get_object_path(),
290 availableInterfaceName);
291 availableInterface->register_property(
292 "Available", true, [this](const bool propIn, bool& old) {
293 if (propIn == old)
294 {
295 return 1;
296 }
James Feist961bf092020-07-01 16:38:12 -0700297 old = propIn;
James Feist67601bd2020-06-16 17:14:44 -0700298 if (!propIn)
299 {
300 updateValue(std::numeric_limits<double>::quiet_NaN());
301 }
James Feist67601bd2020-06-16 17:14:44 -0700302 return 1;
303 });
304 availableInterface->initialize();
305 }
James Feist961bf092020-07-01 16:38:12 -0700306 if (!operationalInterface)
307 {
308 operationalInterface =
309 std::make_shared<sdbusplus::asio::dbus_interface>(
310 conn, sensorInterface->get_object_path(),
311 operationalInterfaceName);
312 operationalInterface->register_property("Functional", true);
313 operationalInterface->initialize();
314 }
315 }
316
317 bool readingStateGood()
318 {
319 if (readState == PowerState::on && !isPowerOn())
320 {
321 return false;
322 }
323 if (readState == PowerState::biosPost &&
324 (!hasBiosPost() || !isPowerOn()))
325 {
326 return false;
327 }
328
329 return true;
330 }
331
332 void markFunctional(bool isFunctional)
333 {
334 if (operationalInterface)
335 {
336 operationalInterface->set_property("Functional", isFunctional);
337 }
338 if (isFunctional)
339 {
340 errCount = 0;
341 }
342 else
343 {
344 updateValue(std::numeric_limits<double>::quiet_NaN());
345 }
346 }
347
348 void markAvailable(bool isAvailable)
349 {
350 if (availableInterface)
351 {
352 availableInterface->set_property("Available", isAvailable);
353 errCount = 0;
354 }
355 }
356
357 void incrementError()
358 {
359 if (!readingStateGood())
360 {
361 markAvailable(false);
362 return;
363 }
364
365 if (errCount >= errorThreshold)
366 {
367 return;
368 }
369
370 errCount++;
371 if (errCount == errorThreshold)
372 {
373 std::cerr << "Sensor " << name << " reading error!\n";
374 markFunctional(false);
375 }
James Feistce3fca42018-11-21 12:58:24 -0800376 }
377
James Feistd8705872019-02-08 13:26:09 -0800378 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800379 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530380 // Ignore if overriding is enabled
James Feist961bf092020-07-01 16:38:12 -0700381 if (overriddenState)
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530382 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800383 return;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530384 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800385
James Feist961bf092020-07-01 16:38:12 -0700386 if (!readingStateGood())
387 {
388 markAvailable(false);
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200389 updateValueProperty(std::numeric_limits<double>::quiet_NaN());
James Feist961bf092020-07-01 16:38:12 -0700390 return;
391 }
392
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200393 updateValueProperty(newValue);
Josh Lehan3bcd8232020-10-29 00:22:12 -0700394 updateInstrumentation(newValue);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800395
396 // Always check thresholds after changing the value,
397 // as the test against hysteresisTrigger now takes place in
398 // the thresholds::checkThresholds() method,
399 // which is called by checkThresholds() below,
400 // in all current implementations of sensors that have thresholds.
401 checkThresholds();
James Feist961bf092020-07-01 16:38:12 -0700402 if (!std::isnan(newValue))
403 {
404 markFunctional(true);
405 markAvailable(true);
406 }
James Feistce3fca42018-11-21 12:58:24 -0800407 }
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200408
409 void updateProperty(
410 std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
411 double& oldValue, const double& newValue, const char* dbusPropertyName)
412 {
413 if (requiresUpdate(oldValue, newValue))
414 {
415 oldValue = newValue;
Jae Hyun Yoo1a540b82020-07-30 23:33:18 -0700416 if (interface &&
417 !(interface->set_property(dbusPropertyName, newValue)))
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200418 {
419 std::cerr << "error setting property " << dbusPropertyName
420 << " to " << newValue << "\n";
421 }
422 }
423 }
424
425 bool requiresUpdate(const double& lVal, const double& rVal)
426 {
427 if (std::isnan(lVal) || std::isnan(rVal))
428 {
429 return true;
430 }
431 double diff = std::abs(lVal - rVal);
432 if (diff > hysteresisPublish)
433 {
434 return true;
435 }
436 return false;
437 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200438
439 private:
440 void updateValueProperty(const double& newValue)
441 {
442 // Indicate that it is internal set call, not an external overwrite
443 internalSet = true;
444 updateProperty(sensorInterface, value, newValue, "Value");
445 internalSet = false;
446 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530447};