blob: 33206e29d9096c9b3b937320d638b6fb3fd3dfb8 [file] [log] [blame]
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001#include "virtualSensor.hpp"
2
3#include "config.hpp"
4
Patrick Williams82b39c62021-07-28 16:22:27 -05005#include <phosphor-logging/lg2.hpp>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07006#include <sdeventplus/event.hpp>
7
8#include <fstream>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07009
10static constexpr bool DEBUG = false;
11static constexpr auto busName = "xyz.openbmc_project.VirtualSensor";
12static constexpr auto sensorDbusPath = "/xyz/openbmc_project/sensors/";
Rashmica Guptae7efe132021-07-27 19:42:11 +100013static constexpr auto entityManagerBusName =
14 "xyz.openbmc_project.EntityManager";
15static constexpr auto vsThresholdsIfaceSuffix = ".Thresholds";
Rashmica Gupta304fd0e2021-08-10 16:50:44 +100016static constexpr std::array<const char*, 1> calculationIfaces = {
17 "xyz.openbmc_project.Configuration.ModifiedMedian"};
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +100018static constexpr auto defaultHysteresis = 0;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070019
Patrick Williams82b39c62021-07-28 16:22:27 -050020PHOSPHOR_LOG2_USING_WITH_FLAGS;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070021
Vijay Khemka51f898e2020-09-09 22:24:18 -070022int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error*)
23{
24 if (usrData == nullptr)
25 {
26 throw std::runtime_error("Invalid match");
27 }
28
29 auto sdbpMsg = sdbusplus::message::message(msg);
30 std::string msgIfce;
31 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
32
33 sdbpMsg.read(msgIfce, msgData);
34
35 if (msgData.find("Value") != msgData.end())
36 {
37 using namespace phosphor::virtualSensor;
38 VirtualSensor* obj = static_cast<VirtualSensor*>(usrData);
39 // TODO(openbmc/phosphor-virtual-sensor#1): updateVirtualSensor should
40 // be changed to take the information we got from the signal, to avoid
41 // having to do numerous dbus queries.
42 obj->updateVirtualSensor();
43 }
44 return 0;
45}
46
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070047namespace phosphor
48{
49namespace virtualSensor
50{
51
52void printParams(const VirtualSensor::ParamMap& paramMap)
53{
54 for (const auto& p : paramMap)
55 {
56 const auto& p1 = p.first;
57 const auto& p2 = p.second;
58 auto val = p2->getParamValue();
Patrick Williamsfbd71452021-08-30 06:59:46 -050059 debug("Parameter: {PARAM} = {VALUE}", "PARAM", p1, "VALUE", val);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070060 }
61}
62
63double SensorParam::getParamValue()
64{
65 switch (paramType)
66 {
67 case constParam:
68 return value;
69 break;
Vijay Khemka7452a862020-08-11 16:01:23 -070070 case dbusParam:
71 return dbusSensor->getSensorValue();
72 break;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070073 default:
74 throw std::invalid_argument("param type not supported");
75 }
76}
77
Lei YU0fcf0e12021-06-04 11:14:17 +080078using AssociationList =
79 std::vector<std::tuple<std::string, std::string, std::string>>;
80
81AssociationList getAssociationsFromJson(const Json& j)
82{
83 AssociationList assocs{};
84 try
85 {
86 j.get_to(assocs);
87 }
88 catch (const std::exception& ex)
89 {
Patrick Williams82b39c62021-07-28 16:22:27 -050090 error("Failed to parse association: {ERROR}", "ERROR", ex);
Lei YU0fcf0e12021-06-04 11:14:17 +080091 }
92 return assocs;
93}
94
Rashmica Guptae7efe132021-07-27 19:42:11 +100095template <typename U>
96struct VariantToNumber
97{
98 template <typename T>
99 U operator()(const T& t) const
100 {
101 if constexpr (std::is_convertible<T, U>::value)
102 {
103 return static_cast<U>(t);
104 }
105 throw std::invalid_argument("Invalid number type in config\n");
106 }
107};
108
109template <typename U>
110U getNumberFromConfig(const PropertyMap& map, const std::string& name,
111 bool required)
112{
113 if (auto itr = map.find(name); itr != map.end())
114 {
115 return std::visit(VariantToNumber<U>(), itr->second);
116 }
117 else if (required)
118 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500119 error("Required field {NAME} missing in config", "NAME", name);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000120 throw std::invalid_argument("Required field missing in config");
121 }
122 return std::numeric_limits<U>::quiet_NaN();
123}
124
125bool isCalculationType(const std::string& interface)
126{
127 auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(),
128 interface);
129 if (itr != calculationIfaces.end())
130 {
131 return true;
132 }
133 return false;
134}
135
136const std::string getThresholdType(const std::string& direction,
Rashmica Gupta05b1d412021-11-03 12:01:36 +1100137 const std::string& severity)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000138{
Rashmica Guptae7efe132021-07-27 19:42:11 +1000139 std::string suffix;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000140
141 if (direction == "less than")
142 {
143 suffix = "Low";
144 }
145 else if (direction == "greater than")
146 {
147 suffix = "High";
148 }
149 else
150 {
151 throw std::invalid_argument(
152 "Invalid threshold direction specified in entity manager");
153 }
Rashmica Gupta05b1d412021-11-03 12:01:36 +1100154 return severity + suffix;
155}
156
157std::string getSeverityField(const PropertyMap& propertyMap)
158{
159 static const std::array thresholdTypes{"Warning", "Critical",
160 "PerformanceLoss", "SoftShutdown",
161 "HardShutdown"};
162
163 std::string severity;
164 if (auto itr = propertyMap.find("Severity"); itr != propertyMap.end())
165 {
166 /* Severity should be a string, but can be an unsigned int */
167 if (std::holds_alternative<std::string>(itr->second))
168 {
169 severity = std::get<std::string>(itr->second);
170 if (0 == std::ranges::count(thresholdTypes, severity))
171 {
172 throw std::invalid_argument(
173 "Invalid threshold severity specified in entity manager");
174 }
175 }
176 else
177 {
178 auto sev =
179 getNumberFromConfig<uint64_t>(propertyMap, "Severity", true);
180 /* Checking bounds ourselves so we throw invalid argument on
181 * invalid user input */
182 if (sev >= thresholdTypes.size())
183 {
184 throw std::invalid_argument(
185 "Invalid threshold severity specified in entity manager");
186 }
187 severity = thresholdTypes.at(sev);
188 }
189 }
190 return severity;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000191}
192
193void parseThresholds(Json& thresholds, const PropertyMap& propertyMap)
194{
195 std::string direction;
196
Rashmica Guptae7efe132021-07-27 19:42:11 +1000197 auto value = getNumberFromConfig<double>(propertyMap, "Value", true);
198
Rashmica Gupta05b1d412021-11-03 12:01:36 +1100199 auto severity = getSeverityField(propertyMap);
200
201 if (auto itr = propertyMap.find("Direction"); itr != propertyMap.end())
Rashmica Guptae7efe132021-07-27 19:42:11 +1000202 {
203 direction = std::get<std::string>(itr->second);
204 }
205
206 auto threshold = getThresholdType(direction, severity);
207 thresholds[threshold] = value;
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000208
209 auto hysteresis =
210 getNumberFromConfig<double>(propertyMap, "Hysteresis", false);
211 if (hysteresis != std::numeric_limits<double>::quiet_NaN())
212 {
213 thresholds[threshold + "Hysteresis"] = hysteresis;
214 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000215}
216
217void VirtualSensor::parseConfigInterface(const PropertyMap& propertyMap,
218 const std::string& sensorType,
219 const std::string& interface)
220{
221 /* Parse sensors / DBus params */
222 if (auto itr = propertyMap.find("Sensors"); itr != propertyMap.end())
223 {
224 auto sensors = std::get<std::vector<std::string>>(itr->second);
225 for (auto sensor : sensors)
226 {
227 std::replace(sensor.begin(), sensor.end(), ' ', '_');
228 auto sensorObjPath = sensorDbusPath + sensorType + "/" + sensor;
229
230 auto paramPtr =
231 std::make_unique<SensorParam>(bus, sensorObjPath, this);
232 symbols.create_variable(sensor);
233 paramMap.emplace(std::move(sensor), std::move(paramPtr));
234 }
235 }
236 /* Get expression string */
237 if (!isCalculationType(interface))
238 {
239 throw std::invalid_argument("Invalid expression in interface");
240 }
241 exprStr = interface;
242
243 /* Get optional min and max input and output values */
244 ValueIface::maxValue(
245 getNumberFromConfig<double>(propertyMap, "MaxValue", false));
246 ValueIface::minValue(
247 getNumberFromConfig<double>(propertyMap, "MinValue", false));
248 maxValidInput =
249 getNumberFromConfig<double>(propertyMap, "MaxValidInput", false);
250 minValidInput =
251 getNumberFromConfig<double>(propertyMap, "MinValidInput", false);
252}
253
Matt Spinlerce675222021-01-14 16:38:09 -0600254void VirtualSensor::initVirtualSensor(const Json& sensorConfig,
255 const std::string& objPath)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700256{
257
258 static const Json empty{};
259
260 /* Get threshold values if defined in config */
261 auto threshold = sensorConfig.value("Threshold", empty);
Matt Spinlerf15189e2021-01-15 10:13:28 -0600262
Rashmica Gupta3e999192021-06-09 16:17:04 +1000263 createThresholds(threshold, objPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700264
Harvey Wuf6443742021-04-09 16:47:36 +0800265 /* Get MaxValue, MinValue setting if defined in config */
266 auto confDesc = sensorConfig.value("Desc", empty);
267 if (auto maxConf = confDesc.find("MaxValue");
268 maxConf != confDesc.end() && maxConf->is_number())
269 {
270 ValueIface::maxValue(maxConf->get<double>());
271 }
272 if (auto minConf = confDesc.find("MinValue");
273 minConf != confDesc.end() && minConf->is_number())
274 {
275 ValueIface::minValue(minConf->get<double>());
276 }
277
Lei YU0fcf0e12021-06-04 11:14:17 +0800278 /* Get optional association */
279 auto assocJson = sensorConfig.value("Associations", empty);
280 if (!assocJson.empty())
281 {
282 auto assocs = getAssociationsFromJson(assocJson);
283 if (!assocs.empty())
284 {
285 associationIface =
286 std::make_unique<AssociationObject>(bus, objPath.c_str());
287 associationIface->associations(assocs);
288 }
289 }
290
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700291 /* Get expression string */
292 exprStr = sensorConfig.value("Expression", "");
293
294 /* Get all the parameter listed in configuration */
295 auto params = sensorConfig.value("Params", empty);
296
297 /* Check for constant parameter */
298 const auto& consParams = params.value("ConstParam", empty);
299 if (!consParams.empty())
300 {
301 for (auto& j : consParams)
302 {
303 if (j.find("ParamName") != j.end())
304 {
305 auto paramPtr = std::make_unique<SensorParam>(j["Value"]);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700306 std::string name = j["ParamName"];
307 symbols.create_variable(name);
308 paramMap.emplace(std::move(name), std::move(paramPtr));
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700309 }
310 else
311 {
312 /* Invalid configuration */
313 throw std::invalid_argument(
314 "ParamName not found in configuration");
315 }
316 }
317 }
318
Vijay Khemka7452a862020-08-11 16:01:23 -0700319 /* Check for dbus parameter */
320 auto dbusParams = params.value("DbusParam", empty);
321 if (!dbusParams.empty())
322 {
323 for (auto& j : dbusParams)
324 {
325 /* Get parameter dbus sensor descriptor */
326 auto desc = j.value("Desc", empty);
327 if ((!desc.empty()) && (j.find("ParamName") != j.end()))
328 {
329 std::string sensorType = desc.value("SensorType", "");
330 std::string name = desc.value("Name", "");
331
332 if (!sensorType.empty() && !name.empty())
333 {
Rashmica Gupta862c3d12021-08-06 12:19:31 +1000334 auto objPath = sensorDbusPath + sensorType + "/" + name;
Vijay Khemka7452a862020-08-11 16:01:23 -0700335
Vijay Khemka51f898e2020-09-09 22:24:18 -0700336 auto paramPtr =
337 std::make_unique<SensorParam>(bus, objPath, this);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700338 std::string name = j["ParamName"];
339 symbols.create_variable(name);
340 paramMap.emplace(std::move(name), std::move(paramPtr));
Vijay Khemka7452a862020-08-11 16:01:23 -0700341 }
342 }
343 }
344 }
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700345
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700346 symbols.add_constants();
Matt Spinler9f1ef4f2020-11-09 15:59:11 -0600347 symbols.add_package(vecopsPackage);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700348 expression.register_symbol_table(symbols);
349
350 /* parser from exprtk */
351 exprtk::parser<double> parser{};
Matt Spinlerddc6dcd2020-11-09 11:16:31 -0600352 if (!parser.compile(exprStr, expression))
353 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500354 error("Expression compilation failed");
Matt Spinlerddc6dcd2020-11-09 11:16:31 -0600355
356 for (std::size_t i = 0; i < parser.error_count(); ++i)
357 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500358 auto err = parser.get_error(i);
359 error("Error parsing token at {POSITION}: {ERROR}", "POSITION",
360 err.token.position, "TYPE",
361 exprtk::parser_error::to_str(err.mode), "ERROR",
362 err.diagnostic);
Matt Spinlerddc6dcd2020-11-09 11:16:31 -0600363 }
364 throw std::runtime_error("Expression compilation failed");
365 }
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700366
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700367 /* Print all parameters for debug purpose only */
368 if (DEBUG)
369 printParams(paramMap);
370}
371
Rashmica Guptae7efe132021-07-27 19:42:11 +1000372void VirtualSensor::initVirtualSensor(const InterfaceMap& interfaceMap,
373 const std::string& objPath,
374 const std::string& sensorType,
375 const std::string& calculationIface)
376{
377 Json thresholds;
378 const std::string vsThresholdsIntf =
379 calculationIface + vsThresholdsIfaceSuffix;
380
381 for (const auto& [interface, propertyMap] : interfaceMap)
382 {
383 /* Each threshold is on it's own interface with a number as a suffix
384 * eg xyz.openbmc_project.Configuration.ModifiedMedian.Thresholds1 */
385 if (interface.find(vsThresholdsIntf) != std::string::npos)
386 {
387 parseThresholds(thresholds, propertyMap);
388 }
389 else if (interface == calculationIface)
390 {
391 parseConfigInterface(propertyMap, sensorType, interface);
392 }
393 }
394
395 createThresholds(thresholds, objPath);
396 symbols.add_constants();
397 symbols.add_package(vecopsPackage);
398 expression.register_symbol_table(symbols);
399
400 /* Print all parameters for debug purpose only */
401 if (DEBUG)
402 {
403 printParams(paramMap);
404 }
405}
406
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700407void VirtualSensor::setSensorValue(double value)
408{
Patrick Williams543bf662021-04-29 09:03:53 -0500409 value = std::clamp(value, ValueIface::minValue(), ValueIface::maxValue());
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700410 ValueIface::value(value);
411}
412
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000413double VirtualSensor::calculateValue(const std::string& calculation,
414 const VirtualSensor::ParamMap& paramMap)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000415{
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000416 auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(),
417 calculation);
418 if (itr == calculationIfaces.end())
419 {
420 return std::numeric_limits<double>::quiet_NaN();
421 }
422 else if (calculation == "xyz.openbmc_project.Configuration.ModifiedMedian")
423 {
424 return calculateModifiedMedianValue(paramMap);
425 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000426 return std::numeric_limits<double>::quiet_NaN();
427}
428
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000429bool VirtualSensor::sensorInRange(double value)
430{
431 if (value <= this->maxValidInput && value >= this->minValidInput)
432 {
433 return true;
434 }
435 return false;
436}
437
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700438void VirtualSensor::updateVirtualSensor()
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700439{
440 for (auto& param : paramMap)
441 {
442 auto& name = param.first;
443 auto& data = param.second;
444 if (auto var = symbols.get_variable(name))
445 {
446 var->ref() = data->getParamValue();
447 }
448 else
449 {
450 /* Invalid parameter */
451 throw std::invalid_argument("ParamName not found in symbols");
452 }
453 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000454 auto itr =
455 std::find(calculationIfaces.begin(), calculationIfaces.end(), exprStr);
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000456 auto val = (itr == calculationIfaces.end())
457 ? expression.value()
458 : calculateValue(exprStr, paramMap);
Vijay Khemka32a71562020-09-10 15:29:18 -0700459
460 /* Set sensor value to dbus interface */
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700461 setSensorValue(val);
Vijay Khemka32a71562020-09-10 15:29:18 -0700462
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700463 if (DEBUG)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000464 {
Patrick Williamsfbd71452021-08-30 06:59:46 -0500465 debug("Sensor {NAME} = {VALUE}", "NAME", this->name, "VALUE", val);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000466 }
Vijay Khemka32a71562020-09-10 15:29:18 -0700467
Matt Spinler8f5e6112021-01-15 10:44:32 -0600468 /* Check sensor thresholds and log required message */
Matt Spinlerb306b032021-02-01 10:05:46 -0600469 checkThresholds(val, perfLossIface);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600470 checkThresholds(val, warningIface);
471 checkThresholds(val, criticalIface);
472 checkThresholds(val, softShutdownIface);
473 checkThresholds(val, hardShutdownIface);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700474}
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700475
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000476double VirtualSensor::calculateModifiedMedianValue(
477 const VirtualSensor::ParamMap& paramMap)
478{
479 std::vector<double> values;
480
481 for (auto& param : paramMap)
482 {
483 auto& name = param.first;
484 if (auto var = symbols.get_variable(name))
485 {
486 if (!sensorInRange(var->ref()))
487 {
488 continue;
489 }
490 values.push_back(var->ref());
491 }
492 }
493
494 size_t size = values.size();
495 std::sort(values.begin(), values.end());
496 switch (size)
497 {
498 case 2:
499 /* Choose biggest value */
500 return values.at(1);
501 case 0:
502 return std::numeric_limits<double>::quiet_NaN();
503 default:
504 /* Choose median value */
505 if (size % 2 == 0)
506 {
507 // Average of the two middle values
508 return (values.at(size / 2) + values.at(size / 2 - 1)) / 2;
509 }
510 else
511 {
512 return values.at((size - 1) / 2);
513 }
514 }
515}
516
Rashmica Gupta3e999192021-06-09 16:17:04 +1000517void VirtualSensor::createThresholds(const Json& threshold,
518 const std::string& objPath)
519{
520 if (threshold.empty())
521 {
522 return;
523 }
524 // Only create the threshold interfaces if
525 // at least one of their values is present.
526 if (threshold.contains("CriticalHigh") || threshold.contains("CriticalLow"))
527 {
528 criticalIface =
529 std::make_unique<Threshold<CriticalObject>>(bus, objPath.c_str());
530
531 criticalIface->criticalHigh(threshold.value(
532 "CriticalHigh", std::numeric_limits<double>::quiet_NaN()));
533 criticalIface->criticalLow(threshold.value(
534 "CriticalLow", std::numeric_limits<double>::quiet_NaN()));
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000535 criticalIface->setHighHysteresis(
536 threshold.value("CriticalHighHysteresis", defaultHysteresis));
537 criticalIface->setLowHysteresis(
538 threshold.value("CriticalLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000539 }
540
541 if (threshold.contains("WarningHigh") || threshold.contains("WarningLow"))
542 {
543 warningIface =
544 std::make_unique<Threshold<WarningObject>>(bus, objPath.c_str());
545
546 warningIface->warningHigh(threshold.value(
547 "WarningHigh", std::numeric_limits<double>::quiet_NaN()));
548 warningIface->warningLow(threshold.value(
549 "WarningLow", std::numeric_limits<double>::quiet_NaN()));
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000550 warningIface->setHighHysteresis(
551 threshold.value("WarningHighHysteresis", defaultHysteresis));
552 warningIface->setLowHysteresis(
553 threshold.value("WarningLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000554 }
555
556 if (threshold.contains("HardShutdownHigh") ||
557 threshold.contains("HardShutdownLow"))
558 {
559 hardShutdownIface = std::make_unique<Threshold<HardShutdownObject>>(
560 bus, objPath.c_str());
561
562 hardShutdownIface->hardShutdownHigh(threshold.value(
563 "HardShutdownHigh", std::numeric_limits<double>::quiet_NaN()));
564 hardShutdownIface->hardShutdownLow(threshold.value(
565 "HardShutdownLow", std::numeric_limits<double>::quiet_NaN()));
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000566 hardShutdownIface->setHighHysteresis(
567 threshold.value("HardShutdownHighHysteresis", defaultHysteresis));
568 hardShutdownIface->setLowHysteresis(
569 threshold.value("HardShutdownLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000570 }
571
572 if (threshold.contains("SoftShutdownHigh") ||
573 threshold.contains("SoftShutdownLow"))
574 {
575 softShutdownIface = std::make_unique<Threshold<SoftShutdownObject>>(
576 bus, objPath.c_str());
577
578 softShutdownIface->softShutdownHigh(threshold.value(
579 "SoftShutdownHigh", std::numeric_limits<double>::quiet_NaN()));
580 softShutdownIface->softShutdownLow(threshold.value(
581 "SoftShutdownLow", std::numeric_limits<double>::quiet_NaN()));
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000582 softShutdownIface->setHighHysteresis(
583 threshold.value("SoftShutdownHighHysteresis", defaultHysteresis));
584 softShutdownIface->setLowHysteresis(
585 threshold.value("SoftShutdownLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000586 }
587
588 if (threshold.contains("PerformanceLossHigh") ||
589 threshold.contains("PerformanceLossLow"))
590 {
591 perfLossIface = std::make_unique<Threshold<PerformanceLossObject>>(
592 bus, objPath.c_str());
593
594 perfLossIface->performanceLossHigh(threshold.value(
595 "PerformanceLossHigh", std::numeric_limits<double>::quiet_NaN()));
596 perfLossIface->performanceLossLow(threshold.value(
597 "PerformanceLossLow", std::numeric_limits<double>::quiet_NaN()));
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000598 perfLossIface->setHighHysteresis(threshold.value(
599 "PerformanceLossHighHysteresis", defaultHysteresis));
600 perfLossIface->setLowHysteresis(
601 threshold.value("PerformanceLossLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000602 }
603}
604
Rashmica Guptae7efe132021-07-27 19:42:11 +1000605ManagedObjectType VirtualSensors::getObjectsFromDBus()
606{
607 ManagedObjectType objects;
608
609 try
610 {
611 auto method = bus.new_method_call(entityManagerBusName, "/",
612 "org.freedesktop.DBus.ObjectManager",
613 "GetManagedObjects");
614 auto reply = bus.call(method);
615 reply.read(objects);
616 }
Patrick Williams74c1e7d2021-09-02 09:50:55 -0500617 catch (const sdbusplus::exception::exception& ex)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000618 {
619 // If entity manager isn't running yet, keep going.
620 if (std::string("org.freedesktop.DBus.Error.ServiceUnknown") !=
621 ex.name())
622 {
623 throw ex.name();
624 }
625 }
626
627 return objects;
628}
629
630void VirtualSensors::propertiesChanged(sdbusplus::message::message& msg)
631{
632 std::string path;
633 PropertyMap properties;
634
635 msg.read(path, properties);
636
637 /* We get multiple callbacks for one sensor. 'Type' is a required field and
638 * is a unique label so use to to only proceed once per sensor */
639 if (properties.contains("Type"))
640 {
641 if (isCalculationType(path))
642 {
643 createVirtualSensorsFromDBus(path);
644 }
645 }
646}
647
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700648/** @brief Parsing Virtual Sensor config JSON file */
649Json VirtualSensors::parseConfigFile(const std::string configFile)
650{
651 std::ifstream jsonFile(configFile);
652 if (!jsonFile.is_open())
653 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500654 error("config JSON file {FILENAME} not found", "FILENAME", configFile);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000655 return {};
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700656 }
657
658 auto data = Json::parse(jsonFile, nullptr, false);
659 if (data.is_discarded())
660 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500661 error("config readings JSON parser failure with {FILENAME}", "FILENAME",
662 configFile);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700663 throw std::exception{};
664 }
665
666 return data;
667}
668
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700669std::map<std::string, ValueIface::Unit> unitMap = {
670 {"temperature", ValueIface::Unit::DegreesC},
671 {"fan_tach", ValueIface::Unit::RPMS},
672 {"voltage", ValueIface::Unit::Volts},
673 {"altitude", ValueIface::Unit::Meters},
674 {"current", ValueIface::Unit::Amperes},
675 {"power", ValueIface::Unit::Watts},
676 {"energy", ValueIface::Unit::Joules},
Kumar Thangavel2b56ddb2021-01-13 20:16:11 +0530677 {"utilization", ValueIface::Unit::Percent},
Rashmica Gupta4ac7a7f2021-07-08 12:30:50 +1000678 {"airflow", ValueIface::Unit::CFM},
679 {"pressure", ValueIface::Unit::Pascals}};
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700680
Rashmica Guptae7efe132021-07-27 19:42:11 +1000681const std::string getSensorTypeFromUnit(const std::string& unit)
682{
683 std::string unitPrefix = "xyz.openbmc_project.Sensor.Value.Unit.";
684 for (auto [type, unitObj] : unitMap)
685 {
686 auto unitPath = ValueIface::convertUnitToString(unitObj);
687 if (unitPath == (unitPrefix + unit))
688 {
689 return type;
690 }
691 }
692 return "";
693}
694
695void VirtualSensors::setupMatches()
696{
697 /* Already setup */
698 if (!this->matches.empty())
699 {
700 return;
701 }
702
703 /* Setup matches */
704 auto eventHandler = [this](sdbusplus::message::message& message) {
705 if (message.is_method_error())
706 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500707 error("Callback method error");
Rashmica Guptae7efe132021-07-27 19:42:11 +1000708 return;
709 }
710 this->propertiesChanged(message);
711 };
712
713 for (const char* iface : calculationIfaces)
714 {
715 auto match = std::make_unique<sdbusplus::bus::match::match>(
716 bus,
717 sdbusplus::bus::match::rules::propertiesChangedNamespace(
718 "/xyz/openbmc_project/inventory", iface),
719 eventHandler);
720 this->matches.emplace_back(std::move(match));
721 }
722}
723
724void VirtualSensors::createVirtualSensorsFromDBus(
725 const std::string& calculationIface)
726{
727 if (calculationIface.empty())
728 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500729 error("No calculation type supplied");
Rashmica Guptae7efe132021-07-27 19:42:11 +1000730 return;
731 }
732 auto objects = getObjectsFromDBus();
733
734 /* Get virtual sensors config data */
735 for (const auto& [path, interfaceMap] : objects)
736 {
737 auto objpath = static_cast<std::string>(path);
738 std::string name = path.filename();
739 std::string sensorType, sensorUnit;
740
741 /* Find Virtual Sensor interfaces */
742 if (!interfaceMap.contains(calculationIface))
743 {
744 continue;
745 }
746 if (name.empty())
747 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500748 error("Virtual Sensor name not found in entity manager config");
Rashmica Guptae7efe132021-07-27 19:42:11 +1000749 continue;
750 }
751 if (virtualSensorsMap.contains(name))
752 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500753 error("A virtual sensor named {NAME} already exists", "NAME", name);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000754 continue;
755 }
756
757 /* Extract the virtual sensor type as we need this to initialize the
758 * sensor */
759 for (const auto& [interface, propertyMap] : interfaceMap)
760 {
761 if (interface != calculationIface)
762 {
763 continue;
764 }
765 auto itr = propertyMap.find("Units");
766 if (itr != propertyMap.end())
767 {
768 sensorUnit = std::get<std::string>(itr->second);
769 break;
770 }
771 }
772 sensorType = getSensorTypeFromUnit(sensorUnit);
773 if (sensorType.empty())
774 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500775 error("Sensor unit type {TYPE} is not supported", "TYPE",
776 sensorUnit);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000777 continue;
778 }
779
780 try
781 {
782 auto virtObjPath = sensorDbusPath + sensorType + "/" + name;
783
784 auto virtualSensorPtr = std::make_unique<VirtualSensor>(
785 bus, virtObjPath.c_str(), interfaceMap, name, sensorType,
786 calculationIface);
Patrick Williams82b39c62021-07-28 16:22:27 -0500787 info("Added a new virtual sensor: {NAME} {TYPE}", "NAME", name,
788 "TYPE", sensorType);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000789 virtualSensorPtr->updateVirtualSensor();
790
791 /* Initialize unit value for virtual sensor */
792 virtualSensorPtr->ValueIface::unit(unitMap[sensorType]);
793 virtualSensorPtr->emit_object_added();
794
795 virtualSensorsMap.emplace(name, std::move(virtualSensorPtr));
796
797 /* Setup match for interfaces removed */
798 auto intfRemoved = [this, objpath,
799 name](sdbusplus::message::message& message) {
800 if (!virtualSensorsMap.contains(name))
801 {
802 return;
803 }
804 sdbusplus::message::object_path path;
805 message.read(path);
806 if (static_cast<const std::string&>(path) == objpath)
807 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500808 info("Removed a virtual sensor: {NAME}", "NAME", name);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000809 virtualSensorsMap.erase(name);
810 }
811 };
812 auto matchOnRemove = std::make_unique<sdbusplus::bus::match::match>(
813 bus,
814 sdbusplus::bus::match::rules::interfacesRemoved() +
815 sdbusplus::bus::match::rules::argNpath(0, objpath),
816 intfRemoved);
817 /* TODO: slight race condition here. Check that the config still
818 * exists */
819 this->matches.emplace_back(std::move(matchOnRemove));
820 }
Patrick Williamsdac26632021-10-06 14:38:47 -0500821 catch (const std::invalid_argument& ia)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000822 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500823 error("Failed to set up virtual sensor: {ERROR}", "ERROR", ia);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000824 }
825 }
826}
827
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700828void VirtualSensors::createVirtualSensors()
829{
830 static const Json empty{};
831
832 auto data = parseConfigFile(VIRTUAL_SENSOR_CONFIG_FILE);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000833
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700834 // print values
835 if (DEBUG)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000836 {
Patrick Williamsfbd71452021-08-30 06:59:46 -0500837 debug("JSON: {JSON}", "JSON", data.dump());
Rashmica Guptae7efe132021-07-27 19:42:11 +1000838 }
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700839
840 /* Get virtual sensors config data */
841 for (const auto& j : data)
842 {
843 auto desc = j.value("Desc", empty);
844 if (!desc.empty())
845 {
Rashmica Guptae7efe132021-07-27 19:42:11 +1000846 if (desc.value("Config", "") == "D-Bus")
847 {
848 /* Look on D-Bus for a virtual sensor config. Set up matches
849 * first because the configs may not be on D-Bus yet and we
850 * don't want to miss them */
851 setupMatches();
852
853 if (desc.contains("Type"))
854 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500855 auto type = desc.value("Type", "");
856 auto path = "xyz.openbmc_project.Configuration." + type;
857
Rashmica Guptae7efe132021-07-27 19:42:11 +1000858 if (!isCalculationType(path))
859 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500860 error("Invalid calculation type {TYPE} supplied.",
861 "TYPE", type);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000862 continue;
863 }
864 createVirtualSensorsFromDBus(path);
865 }
866 continue;
867 }
868
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700869 std::string sensorType = desc.value("SensorType", "");
870 std::string name = desc.value("Name", "");
Rashmica Gupta665a0a22021-06-30 11:35:28 +1000871 std::replace(name.begin(), name.end(), ' ', '_');
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700872
873 if (!name.empty() && !sensorType.empty())
874 {
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700875 if (unitMap.find(sensorType) == unitMap.end())
876 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500877 error("Sensor type {TYPE} is not supported", "TYPE",
878 sensorType);
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700879 }
880 else
881 {
Rashmica Gupta67d8b9d2021-06-30 11:41:14 +1000882 if (virtualSensorsMap.find(name) != virtualSensorsMap.end())
883 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500884 error("A virtual sensor named {NAME} already exists",
885 "NAME", name);
Rashmica Gupta67d8b9d2021-06-30 11:41:14 +1000886 continue;
887 }
Rashmica Gupta862c3d12021-08-06 12:19:31 +1000888 auto objPath = sensorDbusPath + sensorType + "/" + name;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700889
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700890 auto virtualSensorPtr = std::make_unique<VirtualSensor>(
891 bus, objPath.c_str(), j, name);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700892
Patrick Williams82b39c62021-07-28 16:22:27 -0500893 info("Added a new virtual sensor: {NAME}", "NAME", name);
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700894 virtualSensorPtr->updateVirtualSensor();
895
896 /* Initialize unit value for virtual sensor */
897 virtualSensorPtr->ValueIface::unit(unitMap[sensorType]);
Rashmica Guptaa2fa63a2021-08-06 12:21:13 +1000898 virtualSensorPtr->emit_object_added();
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700899
900 virtualSensorsMap.emplace(std::move(name),
901 std::move(virtualSensorPtr));
902 }
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700903 }
904 else
905 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500906 error(
907 "Sensor type ({TYPE}) or name ({NAME}) not found in config file",
908 "NAME", name, "TYPE", sensorType);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700909 }
910 }
911 else
912 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500913 error("Descriptor for new virtual sensor not found in config file");
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700914 }
915 }
916}
917
918} // namespace virtualSensor
919} // namespace phosphor
920
921/**
922 * @brief Main
923 */
924int main()
925{
926
927 // Get a default event loop
928 auto event = sdeventplus::Event::get_default();
929
930 // Get a handle to system dbus
931 auto bus = sdbusplus::bus::new_default();
932
Matt Spinler6c19e7d2021-01-12 16:26:45 -0600933 // Add the ObjectManager interface
934 sdbusplus::server::manager::manager objManager(bus, "/");
935
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700936 // Create an virtual sensors object
937 phosphor::virtualSensor::VirtualSensors virtualSensors(bus);
938
939 // Request service bus name
940 bus.request_name(busName);
941
942 // Attach the bus to sd_event to service user requests
943 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
944 event.loop();
945
946 return 0;
947}