blob: 081408bb1990983d0af2c21d189a3e3ed35080ee [file] [log] [blame]
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001#include "virtualSensor.hpp"
2
Patrick Williams82b39c62021-07-28 16:22:27 -05003#include <phosphor-logging/lg2.hpp>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07004
5#include <fstream>
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07006
7static constexpr bool DEBUG = false;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07008static constexpr auto sensorDbusPath = "/xyz/openbmc_project/sensors/";
Rashmica Guptae7efe132021-07-27 19:42:11 +10009static constexpr auto vsThresholdsIfaceSuffix = ".Thresholds";
Tao Linf6b7e0a2022-10-09 09:35:44 +080010static constexpr std::array<const char*, 2> calculationIfaces = {
11 "xyz.openbmc_project.Configuration.ModifiedMedian",
12 "xyz.openbmc_project.Configuration.Maximum"};
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +100013static constexpr auto defaultHysteresis = 0;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070014
Patrick Williams82b39c62021-07-28 16:22:27 -050015PHOSPHOR_LOG2_USING_WITH_FLAGS;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070016
Tao Linf2e94222023-10-31 17:38:17 +080017namespace phosphor::virtual_sensor
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070018{
19
Lei YU0ab9d832022-07-19 07:12:50 +000020FuncMaxIgnoreNaN<double> VirtualSensor::funcMaxIgnoreNaN;
Lei YU87d35112022-10-24 05:54:25 +000021FuncSumIgnoreNaN<double> VirtualSensor::funcSumIgnoreNaN;
Lei YUc77b6b32023-06-08 12:02:05 +000022FuncIfNan<double> VirtualSensor::funcIfNan;
Lei YU0ab9d832022-07-19 07:12:50 +000023
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070024void printParams(const VirtualSensor::ParamMap& paramMap)
25{
26 for (const auto& p : paramMap)
27 {
28 const auto& p1 = p.first;
29 const auto& p2 = p.second;
30 auto val = p2->getParamValue();
Patrick Williamsfbd71452021-08-30 06:59:46 -050031 debug("Parameter: {PARAM} = {VALUE}", "PARAM", p1, "VALUE", val);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070032 }
33}
34
35double SensorParam::getParamValue()
36{
37 switch (paramType)
38 {
39 case constParam:
40 return value;
41 break;
Vijay Khemka7452a862020-08-11 16:01:23 -070042 case dbusParam:
43 return dbusSensor->getSensorValue();
44 break;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -070045 default:
46 throw std::invalid_argument("param type not supported");
47 }
48}
49
Lei YU0fcf0e12021-06-04 11:14:17 +080050using AssociationList =
51 std::vector<std::tuple<std::string, std::string, std::string>>;
52
53AssociationList getAssociationsFromJson(const Json& j)
54{
55 AssociationList assocs{};
56 try
57 {
58 j.get_to(assocs);
59 }
60 catch (const std::exception& ex)
61 {
Patrick Williams82b39c62021-07-28 16:22:27 -050062 error("Failed to parse association: {ERROR}", "ERROR", ex);
Lei YU0fcf0e12021-06-04 11:14:17 +080063 }
64 return assocs;
65}
66
Rashmica Guptae7efe132021-07-27 19:42:11 +100067template <typename U>
68struct VariantToNumber
69{
70 template <typename T>
71 U operator()(const T& t) const
72 {
73 if constexpr (std::is_convertible<T, U>::value)
74 {
75 return static_cast<U>(t);
76 }
77 throw std::invalid_argument("Invalid number type in config\n");
78 }
79};
80
81template <typename U>
82U getNumberFromConfig(const PropertyMap& map, const std::string& name,
Jiaqing Zhao190f6d02022-05-21 23:26:15 +080083 bool required,
84 U defaultValue = std::numeric_limits<U>::quiet_NaN())
Rashmica Guptae7efe132021-07-27 19:42:11 +100085{
86 if (auto itr = map.find(name); itr != map.end())
87 {
88 return std::visit(VariantToNumber<U>(), itr->second);
89 }
90 else if (required)
91 {
Patrick Williams82b39c62021-07-28 16:22:27 -050092 error("Required field {NAME} missing in config", "NAME", name);
Rashmica Guptae7efe132021-07-27 19:42:11 +100093 throw std::invalid_argument("Required field missing in config");
94 }
Jiaqing Zhao190f6d02022-05-21 23:26:15 +080095 return defaultValue;
Rashmica Guptae7efe132021-07-27 19:42:11 +100096}
97
98bool isCalculationType(const std::string& interface)
99{
100 auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(),
101 interface);
102 if (itr != calculationIfaces.end())
103 {
104 return true;
105 }
106 return false;
107}
108
109const std::string getThresholdType(const std::string& direction,
Rashmica Gupta05b1d412021-11-03 12:01:36 +1100110 const std::string& severity)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000111{
Rashmica Guptae7efe132021-07-27 19:42:11 +1000112 std::string suffix;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000113
114 if (direction == "less than")
115 {
116 suffix = "Low";
117 }
118 else if (direction == "greater than")
119 {
120 suffix = "High";
121 }
122 else
123 {
124 throw std::invalid_argument(
125 "Invalid threshold direction specified in entity manager");
126 }
Rashmica Gupta05b1d412021-11-03 12:01:36 +1100127 return severity + suffix;
128}
129
130std::string getSeverityField(const PropertyMap& propertyMap)
131{
132 static const std::array thresholdTypes{"Warning", "Critical",
133 "PerformanceLoss", "SoftShutdown",
134 "HardShutdown"};
135
136 std::string severity;
137 if (auto itr = propertyMap.find("Severity"); itr != propertyMap.end())
138 {
139 /* Severity should be a string, but can be an unsigned int */
140 if (std::holds_alternative<std::string>(itr->second))
141 {
142 severity = std::get<std::string>(itr->second);
143 if (0 == std::ranges::count(thresholdTypes, severity))
144 {
145 throw std::invalid_argument(
146 "Invalid threshold severity specified in entity manager");
147 }
148 }
149 else
150 {
Patrick Williams1226f202023-05-10 07:51:16 -0500151 auto sev = getNumberFromConfig<uint64_t>(propertyMap, "Severity",
152 true);
Rashmica Gupta05b1d412021-11-03 12:01:36 +1100153 /* Checking bounds ourselves so we throw invalid argument on
154 * invalid user input */
155 if (sev >= thresholdTypes.size())
156 {
157 throw std::invalid_argument(
158 "Invalid threshold severity specified in entity manager");
159 }
160 severity = thresholdTypes.at(sev);
161 }
162 }
163 return severity;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000164}
165
Tao Lin91799db2022-07-27 21:02:20 +0800166void parseThresholds(Json& thresholds, const PropertyMap& propertyMap,
167 const std::string& entityInterface = "")
Rashmica Guptae7efe132021-07-27 19:42:11 +1000168{
169 std::string direction;
170
Rashmica Guptae7efe132021-07-27 19:42:11 +1000171 auto value = getNumberFromConfig<double>(propertyMap, "Value", true);
172
Rashmica Gupta05b1d412021-11-03 12:01:36 +1100173 auto severity = getSeverityField(propertyMap);
174
175 if (auto itr = propertyMap.find("Direction"); itr != propertyMap.end())
Rashmica Guptae7efe132021-07-27 19:42:11 +1000176 {
177 direction = std::get<std::string>(itr->second);
178 }
179
180 auto threshold = getThresholdType(direction, severity);
181 thresholds[threshold] = value;
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000182
Patrick Williams1226f202023-05-10 07:51:16 -0500183 auto hysteresis = getNumberFromConfig<double>(propertyMap, "Hysteresis",
184 false);
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000185 if (hysteresis != std::numeric_limits<double>::quiet_NaN())
186 {
187 thresholds[threshold + "Hysteresis"] = hysteresis;
188 }
Tao Lin91799db2022-07-27 21:02:20 +0800189
190 if (!entityInterface.empty())
191 {
192 thresholds[threshold + "Direction"] = entityInterface;
193 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000194}
195
196void VirtualSensor::parseConfigInterface(const PropertyMap& propertyMap,
197 const std::string& sensorType,
198 const std::string& interface)
199{
200 /* Parse sensors / DBus params */
201 if (auto itr = propertyMap.find("Sensors"); itr != propertyMap.end())
202 {
203 auto sensors = std::get<std::vector<std::string>>(itr->second);
204 for (auto sensor : sensors)
205 {
206 std::replace(sensor.begin(), sensor.end(), ' ', '_');
207 auto sensorObjPath = sensorDbusPath + sensorType + "/" + sensor;
208
Patrick Williams1226f202023-05-10 07:51:16 -0500209 auto paramPtr = std::make_unique<SensorParam>(bus, sensorObjPath,
Tao Linf2e94222023-10-31 17:38:17 +0800210 *this);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000211 symbols.create_variable(sensor);
212 paramMap.emplace(std::move(sensor), std::move(paramPtr));
213 }
214 }
215 /* Get expression string */
216 if (!isCalculationType(interface))
217 {
218 throw std::invalid_argument("Invalid expression in interface");
219 }
220 exprStr = interface;
221
222 /* Get optional min and max input and output values */
223 ValueIface::maxValue(
224 getNumberFromConfig<double>(propertyMap, "MaxValue", false));
225 ValueIface::minValue(
226 getNumberFromConfig<double>(propertyMap, "MinValue", false));
227 maxValidInput =
Jiaqing Zhao190f6d02022-05-21 23:26:15 +0800228 getNumberFromConfig<double>(propertyMap, "MaxValidInput", false,
229 std::numeric_limits<double>::infinity());
Rashmica Guptae7efe132021-07-27 19:42:11 +1000230 minValidInput =
Jiaqing Zhao190f6d02022-05-21 23:26:15 +0800231 getNumberFromConfig<double>(propertyMap, "MinValidInput", false,
232 -std::numeric_limits<double>::infinity());
Rashmica Guptae7efe132021-07-27 19:42:11 +1000233}
234
Matt Spinlerce675222021-01-14 16:38:09 -0600235void VirtualSensor::initVirtualSensor(const Json& sensorConfig,
236 const std::string& objPath)
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700237{
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700238 static const Json empty{};
239
240 /* Get threshold values if defined in config */
241 auto threshold = sensorConfig.value("Threshold", empty);
Matt Spinlerf15189e2021-01-15 10:13:28 -0600242
Rashmica Gupta3e999192021-06-09 16:17:04 +1000243 createThresholds(threshold, objPath);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700244
Harvey Wuf6443742021-04-09 16:47:36 +0800245 /* Get MaxValue, MinValue setting if defined in config */
246 auto confDesc = sensorConfig.value("Desc", empty);
247 if (auto maxConf = confDesc.find("MaxValue");
248 maxConf != confDesc.end() && maxConf->is_number())
249 {
250 ValueIface::maxValue(maxConf->get<double>());
251 }
252 if (auto minConf = confDesc.find("MinValue");
253 minConf != confDesc.end() && minConf->is_number())
254 {
255 ValueIface::minValue(minConf->get<double>());
256 }
257
Lei YU0fcf0e12021-06-04 11:14:17 +0800258 /* Get optional association */
259 auto assocJson = sensorConfig.value("Associations", empty);
260 if (!assocJson.empty())
261 {
262 auto assocs = getAssociationsFromJson(assocJson);
263 if (!assocs.empty())
264 {
265 associationIface =
266 std::make_unique<AssociationObject>(bus, objPath.c_str());
267 associationIface->associations(assocs);
268 }
269 }
270
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700271 /* Get expression string */
Patrick Williams03c4c8e2022-04-14 22:19:44 -0500272 static constexpr auto exprKey = "Expression";
273 if (sensorConfig.contains(exprKey))
274 {
Patrick Williamsa9596782022-04-15 10:20:07 -0500275 auto& ref = sensorConfig.at(exprKey);
Patrick Williams03c4c8e2022-04-14 22:19:44 -0500276 if (ref.is_array())
277 {
278 exprStr = std::string{};
279 for (auto& s : ref)
280 {
281 exprStr += s;
282 }
283 }
284 else if (ref.is_string())
285 {
286 exprStr = std::string{ref};
287 }
288 }
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700289
290 /* Get all the parameter listed in configuration */
291 auto params = sensorConfig.value("Params", empty);
292
293 /* Check for constant parameter */
294 const auto& consParams = params.value("ConstParam", empty);
295 if (!consParams.empty())
296 {
297 for (auto& j : consParams)
298 {
299 if (j.find("ParamName") != j.end())
300 {
301 auto paramPtr = std::make_unique<SensorParam>(j["Value"]);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700302 std::string name = j["ParamName"];
303 symbols.create_variable(name);
304 paramMap.emplace(std::move(name), std::move(paramPtr));
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700305 }
306 else
307 {
308 /* Invalid configuration */
309 throw std::invalid_argument(
310 "ParamName not found in configuration");
311 }
312 }
313 }
314
Vijay Khemka7452a862020-08-11 16:01:23 -0700315 /* Check for dbus parameter */
316 auto dbusParams = params.value("DbusParam", empty);
317 if (!dbusParams.empty())
318 {
319 for (auto& j : dbusParams)
320 {
321 /* Get parameter dbus sensor descriptor */
322 auto desc = j.value("Desc", empty);
323 if ((!desc.empty()) && (j.find("ParamName") != j.end()))
324 {
325 std::string sensorType = desc.value("SensorType", "");
326 std::string name = desc.value("Name", "");
327
328 if (!sensorType.empty() && !name.empty())
329 {
George Liu1204b432021-12-29 17:24:48 +0800330 auto path = sensorDbusPath + sensorType + "/" + name;
Vijay Khemka7452a862020-08-11 16:01:23 -0700331
Patrick Williams1226f202023-05-10 07:51:16 -0500332 auto paramPtr = std::make_unique<SensorParam>(bus, path,
Tao Linf2e94222023-10-31 17:38:17 +0800333 *this);
George Liu1204b432021-12-29 17:24:48 +0800334 std::string paramName = j["ParamName"];
335 symbols.create_variable(paramName);
336 paramMap.emplace(std::move(paramName), std::move(paramPtr));
Vijay Khemka7452a862020-08-11 16:01:23 -0700337 }
338 }
339 }
340 }
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700341
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700342 symbols.add_constants();
Matt Spinler9f1ef4f2020-11-09 15:59:11 -0600343 symbols.add_package(vecopsPackage);
Lei YU0ab9d832022-07-19 07:12:50 +0000344 symbols.add_function("maxIgnoreNaN", funcMaxIgnoreNaN);
Lei YU87d35112022-10-24 05:54:25 +0000345 symbols.add_function("sumIgnoreNaN", funcSumIgnoreNaN);
Lei YUc77b6b32023-06-08 12:02:05 +0000346 symbols.add_function("ifNan", funcIfNan);
Lei YU0ab9d832022-07-19 07:12:50 +0000347
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
Tao Lindc777012022-07-27 20:41:46 +0800372void VirtualSensor::createAssociation(const std::string& objPath,
373 const std::string& entityPath)
374{
375 if (objPath.empty() || entityPath.empty())
376 {
377 return;
378 }
379
380 std::filesystem::path p(entityPath);
381 auto assocsDbus =
382 AssociationList{{"chassis", "all_sensors", p.parent_path().string()}};
Patrick Williams1226f202023-05-10 07:51:16 -0500383 associationIface = std::make_unique<AssociationObject>(bus,
384 objPath.c_str());
Tao Lindc777012022-07-27 20:41:46 +0800385 associationIface->associations(assocsDbus);
386}
387
Rashmica Guptae7efe132021-07-27 19:42:11 +1000388void VirtualSensor::initVirtualSensor(const InterfaceMap& interfaceMap,
389 const std::string& objPath,
390 const std::string& sensorType,
391 const std::string& calculationIface)
392{
393 Json thresholds;
Patrick Williams1226f202023-05-10 07:51:16 -0500394 const std::string vsThresholdsIntf = calculationIface +
395 vsThresholdsIfaceSuffix;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000396
397 for (const auto& [interface, propertyMap] : interfaceMap)
398 {
399 /* Each threshold is on it's own interface with a number as a suffix
400 * eg xyz.openbmc_project.Configuration.ModifiedMedian.Thresholds1 */
401 if (interface.find(vsThresholdsIntf) != std::string::npos)
402 {
Tao Lin91799db2022-07-27 21:02:20 +0800403 parseThresholds(thresholds, propertyMap, interface);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000404 }
405 else if (interface == calculationIface)
406 {
407 parseConfigInterface(propertyMap, sensorType, interface);
408 }
409 }
410
411 createThresholds(thresholds, objPath);
412 symbols.add_constants();
413 symbols.add_package(vecopsPackage);
414 expression.register_symbol_table(symbols);
415
Tao Lindc777012022-07-27 20:41:46 +0800416 createAssociation(objPath, entityPath);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000417 /* Print all parameters for debug purpose only */
418 if (DEBUG)
419 {
420 printParams(paramMap);
421 }
422}
423
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700424void VirtualSensor::setSensorValue(double value)
425{
Patrick Williams543bf662021-04-29 09:03:53 -0500426 value = std::clamp(value, ValueIface::minValue(), ValueIface::maxValue());
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700427 ValueIface::value(value);
428}
429
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000430double VirtualSensor::calculateValue(const std::string& calculation,
431 const VirtualSensor::ParamMap& paramMap)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000432{
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000433 auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(),
434 calculation);
435 if (itr == calculationIfaces.end())
436 {
437 return std::numeric_limits<double>::quiet_NaN();
438 }
439 else if (calculation == "xyz.openbmc_project.Configuration.ModifiedMedian")
440 {
441 return calculateModifiedMedianValue(paramMap);
442 }
Tao Linf6b7e0a2022-10-09 09:35:44 +0800443 else if (calculation == "xyz.openbmc_project.Configuration.Maximum")
444 {
445 return calculateMaximumValue(paramMap);
446 }
Rashmica Guptae7efe132021-07-27 19:42:11 +1000447 return std::numeric_limits<double>::quiet_NaN();
448}
449
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000450bool VirtualSensor::sensorInRange(double value)
451{
452 if (value <= this->maxValidInput && value >= this->minValidInput)
453 {
454 return true;
455 }
456 return false;
457}
458
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700459void VirtualSensor::updateVirtualSensor()
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700460{
461 for (auto& param : paramMap)
462 {
463 auto& name = param.first;
464 auto& data = param.second;
465 if (auto var = symbols.get_variable(name))
466 {
467 var->ref() = data->getParamValue();
468 }
469 else
470 {
471 /* Invalid parameter */
472 throw std::invalid_argument("ParamName not found in symbols");
473 }
474 }
Patrick Williams1226f202023-05-10 07:51:16 -0500475 auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(),
476 exprStr);
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000477 auto val = (itr == calculationIfaces.end())
478 ? expression.value()
479 : calculateValue(exprStr, paramMap);
Vijay Khemka32a71562020-09-10 15:29:18 -0700480
481 /* Set sensor value to dbus interface */
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700482 setSensorValue(val);
Vijay Khemka32a71562020-09-10 15:29:18 -0700483
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700484 if (DEBUG)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000485 {
Patrick Williamsfbd71452021-08-30 06:59:46 -0500486 debug("Sensor {NAME} = {VALUE}", "NAME", this->name, "VALUE", val);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000487 }
Vijay Khemka32a71562020-09-10 15:29:18 -0700488
Matt Spinler8f5e6112021-01-15 10:44:32 -0600489 /* Check sensor thresholds and log required message */
Matt Spinlerb306b032021-02-01 10:05:46 -0600490 checkThresholds(val, perfLossIface);
Patrick Williamsfdb826d2021-01-20 14:37:53 -0600491 checkThresholds(val, warningIface);
492 checkThresholds(val, criticalIface);
493 checkThresholds(val, softShutdownIface);
494 checkThresholds(val, hardShutdownIface);
Vijay Khemka3ed9a512020-08-21 16:13:05 -0700495}
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700496
Rashmica Gupta304fd0e2021-08-10 16:50:44 +1000497double VirtualSensor::calculateModifiedMedianValue(
498 const VirtualSensor::ParamMap& paramMap)
499{
500 std::vector<double> values;
501
502 for (auto& param : paramMap)
503 {
504 auto& name = param.first;
505 if (auto var = symbols.get_variable(name))
506 {
507 if (!sensorInRange(var->ref()))
508 {
509 continue;
510 }
511 values.push_back(var->ref());
512 }
513 }
514
515 size_t size = values.size();
516 std::sort(values.begin(), values.end());
517 switch (size)
518 {
519 case 2:
520 /* Choose biggest value */
521 return values.at(1);
522 case 0:
523 return std::numeric_limits<double>::quiet_NaN();
524 default:
525 /* Choose median value */
526 if (size % 2 == 0)
527 {
528 // Average of the two middle values
529 return (values.at(size / 2) + values.at(size / 2 - 1)) / 2;
530 }
531 else
532 {
533 return values.at((size - 1) / 2);
534 }
535 }
536}
537
Tao Linf6b7e0a2022-10-09 09:35:44 +0800538double VirtualSensor::calculateMaximumValue(
539 const VirtualSensor::ParamMap& paramMap)
540{
541 std::vector<double> values;
542
543 for (auto& param : paramMap)
544 {
545 auto& name = param.first;
546 if (auto var = symbols.get_variable(name))
547 {
548 if (!sensorInRange(var->ref()))
549 {
550 continue;
551 }
552 values.push_back(var->ref());
553 }
554 }
555 auto maxIt = std::max_element(values.begin(), values.end());
556 if (maxIt == values.end())
557 {
558 return std::numeric_limits<double>::quiet_NaN();
559 }
560 return *maxIt;
561}
562
Rashmica Gupta3e999192021-06-09 16:17:04 +1000563void VirtualSensor::createThresholds(const Json& threshold,
564 const std::string& objPath)
565{
566 if (threshold.empty())
567 {
568 return;
569 }
570 // Only create the threshold interfaces if
571 // at least one of their values is present.
572 if (threshold.contains("CriticalHigh") || threshold.contains("CriticalLow"))
573 {
574 criticalIface =
575 std::make_unique<Threshold<CriticalObject>>(bus, objPath.c_str());
576
Tao Lin91799db2022-07-27 21:02:20 +0800577 if (threshold.contains("CriticalHigh"))
578 {
579 criticalIface->setEntityInterfaceHigh(
580 threshold.value("CriticalHighDirection", ""));
581 if (DEBUG)
582 {
583 debug("Sensor Threshold:{NAME} = intf:{INTF}", "NAME", objPath,
584 "INTF", threshold.value("CriticalHighDirection", ""));
585 }
586 }
587 if (threshold.contains("CriticalLow"))
588 {
589 criticalIface->setEntityInterfaceLow(
590 threshold.value("CriticalLowDirection", ""));
591 if (DEBUG)
592 {
593 debug("Sensor Threshold:{NAME} = intf:{INTF}", "NAME", objPath,
594 "INTF", threshold.value("CriticalLowDirection", ""));
595 }
596 }
597
598 criticalIface->setEntityPath(entityPath);
599 if (DEBUG)
600 {
601 debug("Sensor Threshold:{NAME} = path:{PATH}", "NAME", objPath,
602 "PATH", entityPath);
603 }
Matt Spinlera291ce12023-02-06 15:12:44 -0600604
605 criticalIface->criticalHigh(threshold.value(
606 "CriticalHigh", std::numeric_limits<double>::quiet_NaN()));
607 criticalIface->criticalLow(threshold.value(
608 "CriticalLow", std::numeric_limits<double>::quiet_NaN()));
609 criticalIface->setHighHysteresis(
610 threshold.value("CriticalHighHysteresis", defaultHysteresis));
611 criticalIface->setLowHysteresis(
612 threshold.value("CriticalLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000613 }
614
615 if (threshold.contains("WarningHigh") || threshold.contains("WarningLow"))
616 {
617 warningIface =
618 std::make_unique<Threshold<WarningObject>>(bus, objPath.c_str());
619
Tao Lin91799db2022-07-27 21:02:20 +0800620 if (threshold.contains("WarningHigh"))
621 {
622 warningIface->setEntityInterfaceHigh(
623 threshold.value("WarningHighDirection", ""));
624 if (DEBUG)
625 {
626 debug("Sensor Threshold:{NAME} = intf:{INTF}", "NAME", objPath,
627 "INTF", threshold.value("WarningHighDirection", ""));
628 }
629 }
630 if (threshold.contains("WarningLow"))
631 {
632 warningIface->setEntityInterfaceLow(
633 threshold.value("WarningLowDirection", ""));
634 if (DEBUG)
635 {
636 debug("Sensor Threshold:{NAME} = intf:{INTF}", "NAME", objPath,
637 "INTF", threshold.value("WarningLowDirection", ""));
638 }
639 }
640
641 warningIface->setEntityPath(entityPath);
642 if (DEBUG)
643 {
644 debug("Sensor Threshold:{NAME} = path:{PATH}", "NAME", objPath,
645 "PATH", entityPath);
646 }
Matt Spinlera291ce12023-02-06 15:12:44 -0600647
648 warningIface->warningHigh(threshold.value(
649 "WarningHigh", std::numeric_limits<double>::quiet_NaN()));
650 warningIface->warningLow(threshold.value(
651 "WarningLow", std::numeric_limits<double>::quiet_NaN()));
652 warningIface->setHighHysteresis(
653 threshold.value("WarningHighHysteresis", defaultHysteresis));
654 warningIface->setLowHysteresis(
655 threshold.value("WarningLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000656 }
657
658 if (threshold.contains("HardShutdownHigh") ||
659 threshold.contains("HardShutdownLow"))
660 {
661 hardShutdownIface = std::make_unique<Threshold<HardShutdownObject>>(
662 bus, objPath.c_str());
663
664 hardShutdownIface->hardShutdownHigh(threshold.value(
665 "HardShutdownHigh", std::numeric_limits<double>::quiet_NaN()));
666 hardShutdownIface->hardShutdownLow(threshold.value(
667 "HardShutdownLow", std::numeric_limits<double>::quiet_NaN()));
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000668 hardShutdownIface->setHighHysteresis(
669 threshold.value("HardShutdownHighHysteresis", defaultHysteresis));
670 hardShutdownIface->setLowHysteresis(
671 threshold.value("HardShutdownLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000672 }
673
674 if (threshold.contains("SoftShutdownHigh") ||
675 threshold.contains("SoftShutdownLow"))
676 {
677 softShutdownIface = std::make_unique<Threshold<SoftShutdownObject>>(
678 bus, objPath.c_str());
679
680 softShutdownIface->softShutdownHigh(threshold.value(
681 "SoftShutdownHigh", std::numeric_limits<double>::quiet_NaN()));
682 softShutdownIface->softShutdownLow(threshold.value(
683 "SoftShutdownLow", std::numeric_limits<double>::quiet_NaN()));
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000684 softShutdownIface->setHighHysteresis(
685 threshold.value("SoftShutdownHighHysteresis", defaultHysteresis));
686 softShutdownIface->setLowHysteresis(
687 threshold.value("SoftShutdownLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000688 }
689
690 if (threshold.contains("PerformanceLossHigh") ||
691 threshold.contains("PerformanceLossLow"))
692 {
693 perfLossIface = std::make_unique<Threshold<PerformanceLossObject>>(
694 bus, objPath.c_str());
695
696 perfLossIface->performanceLossHigh(threshold.value(
697 "PerformanceLossHigh", std::numeric_limits<double>::quiet_NaN()));
698 perfLossIface->performanceLossLow(threshold.value(
699 "PerformanceLossLow", std::numeric_limits<double>::quiet_NaN()));
Rashmica Gupta1dff7dc2021-07-27 19:43:31 +1000700 perfLossIface->setHighHysteresis(threshold.value(
701 "PerformanceLossHighHysteresis", defaultHysteresis));
702 perfLossIface->setLowHysteresis(
703 threshold.value("PerformanceLossLowHysteresis", defaultHysteresis));
Rashmica Gupta3e999192021-06-09 16:17:04 +1000704 }
705}
706
Rashmica Guptae7efe132021-07-27 19:42:11 +1000707ManagedObjectType VirtualSensors::getObjectsFromDBus()
708{
709 ManagedObjectType objects;
710
711 try
712 {
Nan Zhouf6825b92022-09-20 20:52:43 +0000713 auto method = bus.new_method_call("xyz.openbmc_project.EntityManager",
714 "/xyz/openbmc_project/inventory",
Rashmica Guptae7efe132021-07-27 19:42:11 +1000715 "org.freedesktop.DBus.ObjectManager",
716 "GetManagedObjects");
717 auto reply = bus.call(method);
718 reply.read(objects);
719 }
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500720 catch (const sdbusplus::exception_t& ex)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000721 {
722 // If entity manager isn't running yet, keep going.
723 if (std::string("org.freedesktop.DBus.Error.ServiceUnknown") !=
724 ex.name())
725 {
Matt Spinler71b9c112022-10-18 09:14:45 -0500726 error("Could not reach entity-manager: {ERROR}", "ERROR", ex);
727 throw;
Rashmica Guptae7efe132021-07-27 19:42:11 +1000728 }
729 }
730
731 return objects;
732}
733
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500734void VirtualSensors::propertiesChanged(sdbusplus::message_t& msg)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000735{
736 std::string path;
737 PropertyMap properties;
738
739 msg.read(path, properties);
740
741 /* We get multiple callbacks for one sensor. 'Type' is a required field and
742 * is a unique label so use to to only proceed once per sensor */
743 if (properties.contains("Type"))
744 {
745 if (isCalculationType(path))
746 {
747 createVirtualSensorsFromDBus(path);
748 }
749 }
750}
751
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700752/** @brief Parsing Virtual Sensor config JSON file */
Patrick Williams32dff212023-02-09 13:54:18 -0600753Json VirtualSensors::parseConfigFile()
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700754{
Patrick Williams32dff212023-02-09 13:54:18 -0600755 using path = std::filesystem::path;
756 auto configFile = []() -> path {
757 static constexpr auto name = "virtual_sensor_config.json";
758
759 for (auto pathSeg : {std::filesystem::current_path(),
760 path{"/var/lib/phosphor-virtual-sensor"},
761 path{"/usr/share/phosphor-virtual-sensor"}})
762 {
763 auto file = pathSeg / name;
764 if (std::filesystem::exists(file))
765 {
766 return file;
767 }
768 }
769 return name;
770 }();
771
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700772 std::ifstream jsonFile(configFile);
773 if (!jsonFile.is_open())
774 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500775 error("config JSON file {FILENAME} not found", "FILENAME", configFile);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000776 return {};
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700777 }
778
779 auto data = Json::parse(jsonFile, nullptr, false);
780 if (data.is_discarded())
781 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500782 error("config readings JSON parser failure with {FILENAME}", "FILENAME",
783 configFile);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700784 throw std::exception{};
785 }
786
787 return data;
788}
789
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700790std::map<std::string, ValueIface::Unit> unitMap = {
791 {"temperature", ValueIface::Unit::DegreesC},
792 {"fan_tach", ValueIface::Unit::RPMS},
Konstantin Aladyshev9358f6b2024-03-14 14:23:40 +0300793 {"fan_pwm", ValueIface::Unit::Percent},
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700794 {"voltage", ValueIface::Unit::Volts},
795 {"altitude", ValueIface::Unit::Meters},
796 {"current", ValueIface::Unit::Amperes},
797 {"power", ValueIface::Unit::Watts},
798 {"energy", ValueIface::Unit::Joules},
Kumar Thangavel2b56ddb2021-01-13 20:16:11 +0530799 {"utilization", ValueIface::Unit::Percent},
Rashmica Gupta4ac7a7f2021-07-08 12:30:50 +1000800 {"airflow", ValueIface::Unit::CFM},
801 {"pressure", ValueIface::Unit::Pascals}};
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700802
Rashmica Guptae7efe132021-07-27 19:42:11 +1000803const std::string getSensorTypeFromUnit(const std::string& unit)
804{
805 std::string unitPrefix = "xyz.openbmc_project.Sensor.Value.Unit.";
806 for (auto [type, unitObj] : unitMap)
807 {
808 auto unitPath = ValueIface::convertUnitToString(unitObj);
809 if (unitPath == (unitPrefix + unit))
810 {
811 return type;
812 }
813 }
814 return "";
815}
816
817void VirtualSensors::setupMatches()
818{
819 /* Already setup */
820 if (!this->matches.empty())
821 {
822 return;
823 }
824
825 /* Setup matches */
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500826 auto eventHandler = [this](sdbusplus::message_t& message) {
Rashmica Guptae7efe132021-07-27 19:42:11 +1000827 if (message.is_method_error())
828 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500829 error("Callback method error");
Rashmica Guptae7efe132021-07-27 19:42:11 +1000830 return;
831 }
832 this->propertiesChanged(message);
833 };
834
835 for (const char* iface : calculationIfaces)
836 {
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500837 auto match = std::make_unique<sdbusplus::bus::match_t>(
Rashmica Guptae7efe132021-07-27 19:42:11 +1000838 bus,
839 sdbusplus::bus::match::rules::propertiesChangedNamespace(
840 "/xyz/openbmc_project/inventory", iface),
841 eventHandler);
842 this->matches.emplace_back(std::move(match));
843 }
844}
845
846void VirtualSensors::createVirtualSensorsFromDBus(
847 const std::string& calculationIface)
848{
849 if (calculationIface.empty())
850 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500851 error("No calculation type supplied");
Rashmica Guptae7efe132021-07-27 19:42:11 +1000852 return;
853 }
854 auto objects = getObjectsFromDBus();
855
856 /* Get virtual sensors config data */
857 for (const auto& [path, interfaceMap] : objects)
858 {
Rashmica Guptae7efe132021-07-27 19:42:11 +1000859 /* Find Virtual Sensor interfaces */
George Liu2db8d412023-08-21 16:41:04 +0800860 auto intfIter = interfaceMap.find(calculationIface);
861 if (intfIter == interfaceMap.end())
Rashmica Guptae7efe132021-07-27 19:42:11 +1000862 {
863 continue;
864 }
George Liu2db8d412023-08-21 16:41:04 +0800865
866 std::string name = path.filename();
Rashmica Guptae7efe132021-07-27 19:42:11 +1000867 if (name.empty())
868 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500869 error("Virtual Sensor name not found in entity manager config");
Rashmica Guptae7efe132021-07-27 19:42:11 +1000870 continue;
871 }
872 if (virtualSensorsMap.contains(name))
873 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500874 error("A virtual sensor named {NAME} already exists", "NAME", name);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000875 continue;
876 }
877
878 /* Extract the virtual sensor type as we need this to initialize the
879 * sensor */
George Liu2db8d412023-08-21 16:41:04 +0800880 std::string sensorType, sensorUnit;
881 auto propertyMap = intfIter->second;
882 auto proIter = propertyMap.find("Units");
883 if (proIter != propertyMap.end())
Rashmica Guptae7efe132021-07-27 19:42:11 +1000884 {
George Liu2db8d412023-08-21 16:41:04 +0800885 sensorUnit = std::get<std::string>(proIter->second);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000886 }
887 sensorType = getSensorTypeFromUnit(sensorUnit);
888 if (sensorType.empty())
889 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500890 error("Sensor unit type {TYPE} is not supported", "TYPE",
891 sensorUnit);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000892 continue;
893 }
894
895 try
896 {
George Liu2db8d412023-08-21 16:41:04 +0800897 auto objpath = static_cast<std::string>(path);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000898 auto virtObjPath = sensorDbusPath + sensorType + "/" + name;
899
900 auto virtualSensorPtr = std::make_unique<VirtualSensor>(
901 bus, virtObjPath.c_str(), interfaceMap, name, sensorType,
Tao Lindc777012022-07-27 20:41:46 +0800902 calculationIface, objpath);
Patrick Williams82b39c62021-07-28 16:22:27 -0500903 info("Added a new virtual sensor: {NAME} {TYPE}", "NAME", name,
904 "TYPE", sensorType);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000905 virtualSensorPtr->updateVirtualSensor();
906
907 /* Initialize unit value for virtual sensor */
908 virtualSensorPtr->ValueIface::unit(unitMap[sensorType]);
909 virtualSensorPtr->emit_object_added();
910
911 virtualSensorsMap.emplace(name, std::move(virtualSensorPtr));
912
913 /* Setup match for interfaces removed */
Patrick Williamsae10c522023-10-20 11:19:44 -0500914 auto intfRemoved = [this, objpath,
915 name](sdbusplus::message_t& message) {
Rashmica Guptae7efe132021-07-27 19:42:11 +1000916 if (!virtualSensorsMap.contains(name))
917 {
918 return;
919 }
920 sdbusplus::message::object_path path;
921 message.read(path);
922 if (static_cast<const std::string&>(path) == objpath)
923 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500924 info("Removed a virtual sensor: {NAME}", "NAME", name);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000925 virtualSensorsMap.erase(name);
926 }
927 };
Patrick Williams8e11ccc2022-07-22 19:26:57 -0500928 auto matchOnRemove = std::make_unique<sdbusplus::bus::match_t>(
Rashmica Guptae7efe132021-07-27 19:42:11 +1000929 bus,
930 sdbusplus::bus::match::rules::interfacesRemoved() +
931 sdbusplus::bus::match::rules::argNpath(0, objpath),
932 intfRemoved);
933 /* TODO: slight race condition here. Check that the config still
934 * exists */
935 this->matches.emplace_back(std::move(matchOnRemove));
936 }
Patrick Williamsdac26632021-10-06 14:38:47 -0500937 catch (const std::invalid_argument& ia)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000938 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500939 error("Failed to set up virtual sensor: {ERROR}", "ERROR", ia);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000940 }
941 }
942}
943
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700944void VirtualSensors::createVirtualSensors()
945{
946 static const Json empty{};
947
Patrick Williams32dff212023-02-09 13:54:18 -0600948 auto data = parseConfigFile();
Rashmica Guptae7efe132021-07-27 19:42:11 +1000949
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700950 // print values
951 if (DEBUG)
Rashmica Guptae7efe132021-07-27 19:42:11 +1000952 {
Patrick Williamsfbd71452021-08-30 06:59:46 -0500953 debug("JSON: {JSON}", "JSON", data.dump());
Rashmica Guptae7efe132021-07-27 19:42:11 +1000954 }
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700955
956 /* Get virtual sensors config data */
957 for (const auto& j : data)
958 {
959 auto desc = j.value("Desc", empty);
960 if (!desc.empty())
961 {
Rashmica Guptae7efe132021-07-27 19:42:11 +1000962 if (desc.value("Config", "") == "D-Bus")
963 {
964 /* Look on D-Bus for a virtual sensor config. Set up matches
965 * first because the configs may not be on D-Bus yet and we
966 * don't want to miss them */
967 setupMatches();
968
969 if (desc.contains("Type"))
970 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500971 auto type = desc.value("Type", "");
972 auto path = "xyz.openbmc_project.Configuration." + type;
973
Rashmica Guptae7efe132021-07-27 19:42:11 +1000974 if (!isCalculationType(path))
975 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500976 error("Invalid calculation type {TYPE} supplied.",
977 "TYPE", type);
Rashmica Guptae7efe132021-07-27 19:42:11 +1000978 continue;
979 }
980 createVirtualSensorsFromDBus(path);
981 }
982 continue;
983 }
984
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700985 std::string sensorType = desc.value("SensorType", "");
986 std::string name = desc.value("Name", "");
Rashmica Gupta665a0a22021-06-30 11:35:28 +1000987 std::replace(name.begin(), name.end(), ' ', '_');
Vijay Khemkaabcc94f2020-08-11 15:27:44 -0700988
989 if (!name.empty() && !sensorType.empty())
990 {
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700991 if (unitMap.find(sensorType) == unitMap.end())
992 {
Patrick Williams82b39c62021-07-28 16:22:27 -0500993 error("Sensor type {TYPE} is not supported", "TYPE",
994 sensorType);
Vijay Khemkae0d371e2020-09-21 18:35:52 -0700995 }
996 else
997 {
Rashmica Gupta67d8b9d2021-06-30 11:41:14 +1000998 if (virtualSensorsMap.find(name) != virtualSensorsMap.end())
999 {
Patrick Williams82b39c62021-07-28 16:22:27 -05001000 error("A virtual sensor named {NAME} already exists",
1001 "NAME", name);
Rashmica Gupta67d8b9d2021-06-30 11:41:14 +10001002 continue;
1003 }
Rashmica Gupta862c3d12021-08-06 12:19:31 +10001004 auto objPath = sensorDbusPath + sensorType + "/" + name;
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001005
Vijay Khemkae0d371e2020-09-21 18:35:52 -07001006 auto virtualSensorPtr = std::make_unique<VirtualSensor>(
1007 bus, objPath.c_str(), j, name);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001008
Patrick Williams82b39c62021-07-28 16:22:27 -05001009 info("Added a new virtual sensor: {NAME}", "NAME", name);
Vijay Khemkae0d371e2020-09-21 18:35:52 -07001010 virtualSensorPtr->updateVirtualSensor();
1011
1012 /* Initialize unit value for virtual sensor */
1013 virtualSensorPtr->ValueIface::unit(unitMap[sensorType]);
Rashmica Guptaa2fa63a2021-08-06 12:21:13 +10001014 virtualSensorPtr->emit_object_added();
Vijay Khemkae0d371e2020-09-21 18:35:52 -07001015
1016 virtualSensorsMap.emplace(std::move(name),
1017 std::move(virtualSensorPtr));
1018 }
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001019 }
1020 else
1021 {
Patrick Williams82b39c62021-07-28 16:22:27 -05001022 error(
1023 "Sensor type ({TYPE}) or name ({NAME}) not found in config file",
1024 "NAME", name, "TYPE", sensorType);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001025 }
1026 }
1027 else
1028 {
Patrick Williams82b39c62021-07-28 16:22:27 -05001029 error("Descriptor for new virtual sensor not found in config file");
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001030 }
1031 }
1032}
1033
Tao Linf2e94222023-10-31 17:38:17 +08001034} // namespace phosphor::virtual_sensor
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001035
1036/**
1037 * @brief Main
1038 */
1039int main()
1040{
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001041 // Get a handle to system dbus
1042 auto bus = sdbusplus::bus::new_default();
1043
Matt Spinler6c19e7d2021-01-12 16:26:45 -06001044 // Add the ObjectManager interface
Ed Tanousf7ec40a2022-10-04 17:39:40 -07001045 sdbusplus::server::manager_t objManager(bus,
1046 "/xyz/openbmc_project/sensors");
Matt Spinler6c19e7d2021-01-12 16:26:45 -06001047
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001048 // Create an virtual sensors object
Tao Linf2e94222023-10-31 17:38:17 +08001049 phosphor::virtual_sensor::VirtualSensors virtualSensors(bus);
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001050
1051 // Request service bus name
George Liu94921492023-08-21 16:18:14 +08001052 bus.request_name("xyz.openbmc_project.VirtualSensor");
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001053
Patrick Williamse6672392022-09-02 09:03:24 -05001054 // Run the dbus loop.
1055 bus.process_loop();
Vijay Khemkaabcc94f2020-08-11 15:27:44 -07001056
1057 return 0;
1058}