blob: a8321fd6af195697aefdf0b76781c94954228af3 [file] [log] [blame]
James Feist8fd8a582018-11-16 11:10:46 -08001#pragma once
2
Patrick Ventureca44b2f2019-10-31 11:02:26 -07003#include "Thresholds.hpp"
James Feist961bf092020-07-01 16:38:12 -07004#include "Utils.hpp"
Patrick Ventureca44b2f2019-10-31 11:02:26 -07005
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
15constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value";
James Feist67601bd2020-06-16 17:14:44 -070016constexpr const char* availableInterfaceName =
17 "xyz.openbmc_project.State.Decorator.Availability";
James Feist961bf092020-07-01 16:38:12 -070018constexpr const char* operationalInterfaceName =
19 "xyz.openbmc_project.State.Decorator.OperationalStatus";
20constexpr const size_t errorThreshold = 5;
21
James Feist8fd8a582018-11-16 11:10:46 -080022struct Sensor
23{
James Feist930fcde2019-05-28 12:58:43 -070024 Sensor(const std::string& name,
James Feistd8705872019-02-08 13:26:09 -080025 std::vector<thresholds::Threshold>&& thresholdData,
26 const std::string& configurationPath, const std::string& objectType,
James Feist961bf092020-07-01 16:38:12 -070027 const double max, const double min,
28 PowerState readState = PowerState::always) :
AppaRao Puli54ffb272020-06-02 19:09:38 +053029 name(std::regex_replace(name, std::regex("[^a-zA-Z0-9_/]+"), "_")),
James Feist930fcde2019-05-28 12:58:43 -070030 configurationPath(configurationPath), objectType(objectType),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050031 maxValue(max), minValue(min), thresholds(std::move(thresholdData)),
Josh Lehan883fb3a2020-02-27 14:41:39 -080032 hysteresisTrigger((max - min) * 0.01),
James Feist961bf092020-07-01 16:38:12 -070033 hysteresisPublish((max - min) * 0.0001), readState(readState),
34 errCount(0)
James Feist38fb5982020-05-28 10:09:54 -070035 {}
James Feist8fd8a582018-11-16 11:10:46 -080036 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080037 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070038 std::string name;
James Feistce3fca42018-11-21 12:58:24 -080039 std::string configurationPath;
40 std::string objectType;
41 double maxValue;
42 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080043 std::vector<thresholds::Threshold> thresholds;
44 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
45 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
46 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
James Feist078f2322019-03-08 11:09:05 -080047 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist67601bd2020-06-16 17:14:44 -070048 std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface;
James Feist961bf092020-07-01 16:38:12 -070049 std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface;
James Feist8fd8a582018-11-16 11:10:46 -080050 double value = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053051 bool overriddenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053052 bool internalSet = false;
Josh Lehan883fb3a2020-02-27 14:41:39 -080053 double hysteresisTrigger;
54 double hysteresisPublish;
James Feist961bf092020-07-01 16:38:12 -070055 PowerState readState;
56 size_t errCount;
James Feistce3fca42018-11-21 12:58:24 -080057
James Feistd8705872019-02-08 13:26:09 -080058 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053059 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053060 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053061 {
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053062 oldValue = newValue;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053063 overriddenState = true;
64 // check thresholds for external set
65 value = newValue;
66 checkThresholds();
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053067 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053068 else if (!overriddenState)
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053069 {
70 oldValue = newValue;
71 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053072 return 1;
73 }
James Feistce3fca42018-11-21 12:58:24 -080074
75 void
Cheng C Yang6b1247a2020-03-09 23:48:39 +080076 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn,
77 const std::string label = std::string(),
78 size_t thresholdSize = 0)
James Feistce3fca42018-11-21 12:58:24 -080079 {
James Feist961bf092020-07-01 16:38:12 -070080 if (readState == PowerState::on || readState == PowerState::biosPost)
81 {
82 setupPowerMatch(conn);
83 }
84
James Feist82bac4c2019-03-11 11:16:53 -070085 createAssociation(association, configurationPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +053086
James Feistce3fca42018-11-21 12:58:24 -080087 sensorInterface->register_property("MaxValue", maxValue);
88 sensorInterface->register_property("MinValue", minValue);
89 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -080090 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -080091 return setSensorValue(newValue, oldValue);
92 });
James Feistd8705872019-02-08 13:26:09 -080093 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -080094 {
95 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
96 std::string level;
97 std::string alarm;
98 if (threshold.level == thresholds::Level::CRITICAL)
99 {
100 iface = thresholdInterfaceCritical;
101 if (threshold.direction == thresholds::Direction::HIGH)
102 {
103 level = "CriticalHigh";
104 alarm = "CriticalAlarmHigh";
105 }
106 else
107 {
108 level = "CriticalLow";
109 alarm = "CriticalAlarmLow";
110 }
111 }
112 else if (threshold.level == thresholds::Level::WARNING)
113 {
114 iface = thresholdInterfaceWarning;
115 if (threshold.direction == thresholds::Direction::HIGH)
116 {
117 level = "WarningHigh";
118 alarm = "WarningAlarmHigh";
119 }
120 else
121 {
122 level = "WarningLow";
123 alarm = "WarningAlarmLow";
124 }
125 }
126 else
127 {
128 std::cerr << "Unknown threshold level" << threshold.level
129 << "\n";
130 continue;
131 }
132 if (!iface)
133 {
134 std::cout << "trying to set uninitialized interface\n";
135 continue;
136 }
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800137
138 size_t thresSize =
139 label.empty() ? thresholds.size() : thresholdSize;
James Feistce3fca42018-11-21 12:58:24 -0800140 iface->register_property(
141 level, threshold.value,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800142 [&, label, thresSize](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800143 oldValue = request; // todo, just let the config do this?
144 threshold.value = request;
145 thresholds::persistThreshold(configurationPath, objectType,
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800146 threshold, conn, thresSize,
147 label);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800148 // Invalidate previously remembered value,
149 // so new thresholds will be checked during next update,
150 // even if sensor reading remains unchanged.
151 value = std::numeric_limits<double>::quiet_NaN();
152
153 // Although tempting, don't call checkThresholds() from here
154 // directly. Let the regular sensor monitor call the same
155 // using updateValue(), which can check conditions like
156 // poweron, etc., before raising any event.
James Feistce3fca42018-11-21 12:58:24 -0800157 return 1;
158 });
159 iface->register_property(alarm, false);
160 }
161 if (!sensorInterface->initialize())
162 {
163 std::cerr << "error initializing value interface\n";
164 }
165 if (thresholdInterfaceWarning &&
Yong Lif902c052020-05-07 17:13:53 +0800166 !thresholdInterfaceWarning->initialize(true))
James Feistce3fca42018-11-21 12:58:24 -0800167 {
168 std::cerr << "error initializing warning threshold interface\n";
169 }
170
171 if (thresholdInterfaceCritical &&
Yong Lif902c052020-05-07 17:13:53 +0800172 !thresholdInterfaceCritical->initialize(true))
James Feistce3fca42018-11-21 12:58:24 -0800173 {
174 std::cerr << "error initializing critical threshold interface\n";
175 }
James Feist67601bd2020-06-16 17:14:44 -0700176
177 if (!availableInterface)
178 {
179 availableInterface =
180 std::make_shared<sdbusplus::asio::dbus_interface>(
181 conn, sensorInterface->get_object_path(),
182 availableInterfaceName);
183 availableInterface->register_property(
184 "Available", true, [this](const bool propIn, bool& old) {
185 if (propIn == old)
186 {
187 return 1;
188 }
James Feist961bf092020-07-01 16:38:12 -0700189 old = propIn;
James Feist67601bd2020-06-16 17:14:44 -0700190 if (!propIn)
191 {
192 updateValue(std::numeric_limits<double>::quiet_NaN());
193 }
James Feist67601bd2020-06-16 17:14:44 -0700194 return 1;
195 });
196 availableInterface->initialize();
197 }
James Feist961bf092020-07-01 16:38:12 -0700198 if (!operationalInterface)
199 {
200 operationalInterface =
201 std::make_shared<sdbusplus::asio::dbus_interface>(
202 conn, sensorInterface->get_object_path(),
203 operationalInterfaceName);
204 operationalInterface->register_property("Functional", true);
205 operationalInterface->initialize();
206 }
207 }
208
209 bool readingStateGood()
210 {
211 if (readState == PowerState::on && !isPowerOn())
212 {
213 return false;
214 }
215 if (readState == PowerState::biosPost &&
216 (!hasBiosPost() || !isPowerOn()))
217 {
218 return false;
219 }
220
221 return true;
222 }
223
224 void markFunctional(bool isFunctional)
225 {
226 if (operationalInterface)
227 {
228 operationalInterface->set_property("Functional", isFunctional);
229 }
230 if (isFunctional)
231 {
232 errCount = 0;
233 }
234 else
235 {
236 updateValue(std::numeric_limits<double>::quiet_NaN());
237 }
238 }
239
240 void markAvailable(bool isAvailable)
241 {
242 if (availableInterface)
243 {
244 availableInterface->set_property("Available", isAvailable);
245 errCount = 0;
246 }
247 }
248
249 void incrementError()
250 {
251 if (!readingStateGood())
252 {
253 markAvailable(false);
254 return;
255 }
256
257 if (errCount >= errorThreshold)
258 {
259 return;
260 }
261
262 errCount++;
263 if (errCount == errorThreshold)
264 {
265 std::cerr << "Sensor " << name << " reading error!\n";
266 markFunctional(false);
267 }
James Feistce3fca42018-11-21 12:58:24 -0800268 }
269
James Feistd8705872019-02-08 13:26:09 -0800270 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800271 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530272 // Ignore if overriding is enabled
James Feist961bf092020-07-01 16:38:12 -0700273 if (overriddenState)
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530274 {
Josh Lehan883fb3a2020-02-27 14:41:39 -0800275 return;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530276 }
Josh Lehan883fb3a2020-02-27 14:41:39 -0800277
James Feist961bf092020-07-01 16:38:12 -0700278 if (!readingStateGood())
279 {
280 markAvailable(false);
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200281 updateValueProperty(std::numeric_limits<double>::quiet_NaN());
James Feist961bf092020-07-01 16:38:12 -0700282 return;
283 }
284
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200285 updateValueProperty(newValue);
Josh Lehan883fb3a2020-02-27 14:41:39 -0800286
287 // Always check thresholds after changing the value,
288 // as the test against hysteresisTrigger now takes place in
289 // the thresholds::checkThresholds() method,
290 // which is called by checkThresholds() below,
291 // in all current implementations of sensors that have thresholds.
292 checkThresholds();
James Feist961bf092020-07-01 16:38:12 -0700293 if (!std::isnan(newValue))
294 {
295 markFunctional(true);
296 markAvailable(true);
297 }
James Feistce3fca42018-11-21 12:58:24 -0800298 }
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200299
300 void updateProperty(
301 std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
302 double& oldValue, const double& newValue, const char* dbusPropertyName)
303 {
304 if (requiresUpdate(oldValue, newValue))
305 {
306 oldValue = newValue;
Jae Hyun Yoo1a540b82020-07-30 23:33:18 -0700307 if (interface &&
308 !(interface->set_property(dbusPropertyName, newValue)))
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200309 {
310 std::cerr << "error setting property " << dbusPropertyName
311 << " to " << newValue << "\n";
312 }
313 }
314 }
315
316 bool requiresUpdate(const double& lVal, const double& rVal)
317 {
318 if (std::isnan(lVal) || std::isnan(rVal))
319 {
320 return true;
321 }
322 double diff = std::abs(lVal - rVal);
323 if (diff > hysteresisPublish)
324 {
325 return true;
326 }
327 return false;
328 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200329
330 private:
331 void updateValueProperty(const double& newValue)
332 {
333 // Indicate that it is internal set call, not an external overwrite
334 internalSet = true;
335 updateProperty(sensorInterface, value, newValue, "Value");
336 internalSet = false;
337 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530338};