Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 1 | #include "virtualSensor.hpp" |
| 2 | |
| 3 | #include "config.hpp" |
| 4 | |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 6 | |
| 7 | #include <fstream> |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 8 | |
| 9 | static constexpr bool DEBUG = false; |
| 10 | static constexpr auto busName = "xyz.openbmc_project.VirtualSensor"; |
| 11 | static constexpr auto sensorDbusPath = "/xyz/openbmc_project/sensors/"; |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 12 | static constexpr auto entityManagerBusName = |
| 13 | "xyz.openbmc_project.EntityManager"; |
| 14 | static constexpr auto vsThresholdsIfaceSuffix = ".Thresholds"; |
Rashmica Gupta | 304fd0e | 2021-08-10 16:50:44 +1000 | [diff] [blame] | 15 | static constexpr std::array<const char*, 1> calculationIfaces = { |
| 16 | "xyz.openbmc_project.Configuration.ModifiedMedian"}; |
Rashmica Gupta | 1dff7dc | 2021-07-27 19:43:31 +1000 | [diff] [blame] | 17 | static constexpr auto defaultHysteresis = 0; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 18 | |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 19 | PHOSPHOR_LOG2_USING_WITH_FLAGS; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 20 | |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 21 | int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error*) |
| 22 | { |
| 23 | if (usrData == nullptr) |
| 24 | { |
| 25 | throw std::runtime_error("Invalid match"); |
| 26 | } |
| 27 | |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 28 | auto sdbpMsg = sdbusplus::message_t(msg); |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 29 | std::string msgIfce; |
| 30 | std::map<std::string, std::variant<int64_t, double, bool>> msgData; |
| 31 | |
| 32 | sdbpMsg.read(msgIfce, msgData); |
| 33 | |
| 34 | if (msgData.find("Value") != msgData.end()) |
| 35 | { |
| 36 | using namespace phosphor::virtualSensor; |
| 37 | VirtualSensor* obj = static_cast<VirtualSensor*>(usrData); |
| 38 | // TODO(openbmc/phosphor-virtual-sensor#1): updateVirtualSensor should |
| 39 | // be changed to take the information we got from the signal, to avoid |
| 40 | // having to do numerous dbus queries. |
| 41 | obj->updateVirtualSensor(); |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 46 | namespace phosphor |
| 47 | { |
| 48 | namespace virtualSensor |
| 49 | { |
| 50 | |
| 51 | void printParams(const VirtualSensor::ParamMap& paramMap) |
| 52 | { |
| 53 | for (const auto& p : paramMap) |
| 54 | { |
| 55 | const auto& p1 = p.first; |
| 56 | const auto& p2 = p.second; |
| 57 | auto val = p2->getParamValue(); |
Patrick Williams | fbd7145 | 2021-08-30 06:59:46 -0500 | [diff] [blame] | 58 | debug("Parameter: {PARAM} = {VALUE}", "PARAM", p1, "VALUE", val); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | double SensorParam::getParamValue() |
| 63 | { |
| 64 | switch (paramType) |
| 65 | { |
| 66 | case constParam: |
| 67 | return value; |
| 68 | break; |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 69 | case dbusParam: |
| 70 | return dbusSensor->getSensorValue(); |
| 71 | break; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 72 | default: |
| 73 | throw std::invalid_argument("param type not supported"); |
| 74 | } |
| 75 | } |
| 76 | |
Lei YU | 0fcf0e1 | 2021-06-04 11:14:17 +0800 | [diff] [blame] | 77 | using AssociationList = |
| 78 | std::vector<std::tuple<std::string, std::string, std::string>>; |
| 79 | |
| 80 | AssociationList getAssociationsFromJson(const Json& j) |
| 81 | { |
| 82 | AssociationList assocs{}; |
| 83 | try |
| 84 | { |
| 85 | j.get_to(assocs); |
| 86 | } |
| 87 | catch (const std::exception& ex) |
| 88 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 89 | error("Failed to parse association: {ERROR}", "ERROR", ex); |
Lei YU | 0fcf0e1 | 2021-06-04 11:14:17 +0800 | [diff] [blame] | 90 | } |
| 91 | return assocs; |
| 92 | } |
| 93 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 94 | template <typename U> |
| 95 | struct VariantToNumber |
| 96 | { |
| 97 | template <typename T> |
| 98 | U operator()(const T& t) const |
| 99 | { |
| 100 | if constexpr (std::is_convertible<T, U>::value) |
| 101 | { |
| 102 | return static_cast<U>(t); |
| 103 | } |
| 104 | throw std::invalid_argument("Invalid number type in config\n"); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | template <typename U> |
| 109 | U getNumberFromConfig(const PropertyMap& map, const std::string& name, |
Jiaqing Zhao | 190f6d0 | 2022-05-21 23:26:15 +0800 | [diff] [blame] | 110 | bool required, |
| 111 | U defaultValue = std::numeric_limits<U>::quiet_NaN()) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 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 Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 119 | error("Required field {NAME} missing in config", "NAME", name); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 120 | throw std::invalid_argument("Required field missing in config"); |
| 121 | } |
Jiaqing Zhao | 190f6d0 | 2022-05-21 23:26:15 +0800 | [diff] [blame] | 122 | return defaultValue; |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool 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 | |
| 136 | const std::string getThresholdType(const std::string& direction, |
Rashmica Gupta | 05b1d41 | 2021-11-03 12:01:36 +1100 | [diff] [blame] | 137 | const std::string& severity) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 138 | { |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 139 | std::string suffix; |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 140 | |
| 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 Gupta | 05b1d41 | 2021-11-03 12:01:36 +1100 | [diff] [blame] | 154 | return severity + suffix; |
| 155 | } |
| 156 | |
| 157 | std::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 Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void parseThresholds(Json& thresholds, const PropertyMap& propertyMap) |
| 194 | { |
| 195 | std::string direction; |
| 196 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 197 | auto value = getNumberFromConfig<double>(propertyMap, "Value", true); |
| 198 | |
Rashmica Gupta | 05b1d41 | 2021-11-03 12:01:36 +1100 | [diff] [blame] | 199 | auto severity = getSeverityField(propertyMap); |
| 200 | |
| 201 | if (auto itr = propertyMap.find("Direction"); itr != propertyMap.end()) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 202 | { |
| 203 | direction = std::get<std::string>(itr->second); |
| 204 | } |
| 205 | |
| 206 | auto threshold = getThresholdType(direction, severity); |
| 207 | thresholds[threshold] = value; |
Rashmica Gupta | 1dff7dc | 2021-07-27 19:43:31 +1000 | [diff] [blame] | 208 | |
| 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 Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void 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 = |
Jiaqing Zhao | 190f6d0 | 2022-05-21 23:26:15 +0800 | [diff] [blame] | 249 | getNumberFromConfig<double>(propertyMap, "MaxValidInput", false, |
| 250 | std::numeric_limits<double>::infinity()); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 251 | minValidInput = |
Jiaqing Zhao | 190f6d0 | 2022-05-21 23:26:15 +0800 | [diff] [blame] | 252 | getNumberFromConfig<double>(propertyMap, "MinValidInput", false, |
| 253 | -std::numeric_limits<double>::infinity()); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 254 | } |
| 255 | |
Matt Spinler | ce67522 | 2021-01-14 16:38:09 -0600 | [diff] [blame] | 256 | void VirtualSensor::initVirtualSensor(const Json& sensorConfig, |
| 257 | const std::string& objPath) |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 258 | { |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 259 | static const Json empty{}; |
| 260 | |
| 261 | /* Get threshold values if defined in config */ |
| 262 | auto threshold = sensorConfig.value("Threshold", empty); |
Matt Spinler | f15189e | 2021-01-15 10:13:28 -0600 | [diff] [blame] | 263 | |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 264 | createThresholds(threshold, objPath); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 265 | |
Harvey Wu | f644374 | 2021-04-09 16:47:36 +0800 | [diff] [blame] | 266 | /* Get MaxValue, MinValue setting if defined in config */ |
| 267 | auto confDesc = sensorConfig.value("Desc", empty); |
| 268 | if (auto maxConf = confDesc.find("MaxValue"); |
| 269 | maxConf != confDesc.end() && maxConf->is_number()) |
| 270 | { |
| 271 | ValueIface::maxValue(maxConf->get<double>()); |
| 272 | } |
| 273 | if (auto minConf = confDesc.find("MinValue"); |
| 274 | minConf != confDesc.end() && minConf->is_number()) |
| 275 | { |
| 276 | ValueIface::minValue(minConf->get<double>()); |
| 277 | } |
| 278 | |
Lei YU | 0fcf0e1 | 2021-06-04 11:14:17 +0800 | [diff] [blame] | 279 | /* Get optional association */ |
| 280 | auto assocJson = sensorConfig.value("Associations", empty); |
| 281 | if (!assocJson.empty()) |
| 282 | { |
| 283 | auto assocs = getAssociationsFromJson(assocJson); |
| 284 | if (!assocs.empty()) |
| 285 | { |
| 286 | associationIface = |
| 287 | std::make_unique<AssociationObject>(bus, objPath.c_str()); |
| 288 | associationIface->associations(assocs); |
| 289 | } |
| 290 | } |
| 291 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 292 | /* Get expression string */ |
Patrick Williams | 03c4c8e | 2022-04-14 22:19:44 -0500 | [diff] [blame] | 293 | static constexpr auto exprKey = "Expression"; |
| 294 | if (sensorConfig.contains(exprKey)) |
| 295 | { |
Patrick Williams | a959678 | 2022-04-15 10:20:07 -0500 | [diff] [blame] | 296 | auto& ref = sensorConfig.at(exprKey); |
Patrick Williams | 03c4c8e | 2022-04-14 22:19:44 -0500 | [diff] [blame] | 297 | if (ref.is_array()) |
| 298 | { |
| 299 | exprStr = std::string{}; |
| 300 | for (auto& s : ref) |
| 301 | { |
| 302 | exprStr += s; |
| 303 | } |
| 304 | } |
| 305 | else if (ref.is_string()) |
| 306 | { |
| 307 | exprStr = std::string{ref}; |
| 308 | } |
| 309 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 310 | |
| 311 | /* Get all the parameter listed in configuration */ |
| 312 | auto params = sensorConfig.value("Params", empty); |
| 313 | |
| 314 | /* Check for constant parameter */ |
| 315 | const auto& consParams = params.value("ConstParam", empty); |
| 316 | if (!consParams.empty()) |
| 317 | { |
| 318 | for (auto& j : consParams) |
| 319 | { |
| 320 | if (j.find("ParamName") != j.end()) |
| 321 | { |
| 322 | auto paramPtr = std::make_unique<SensorParam>(j["Value"]); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 323 | std::string name = j["ParamName"]; |
| 324 | symbols.create_variable(name); |
| 325 | paramMap.emplace(std::move(name), std::move(paramPtr)); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 326 | } |
| 327 | else |
| 328 | { |
| 329 | /* Invalid configuration */ |
| 330 | throw std::invalid_argument( |
| 331 | "ParamName not found in configuration"); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 336 | /* Check for dbus parameter */ |
| 337 | auto dbusParams = params.value("DbusParam", empty); |
| 338 | if (!dbusParams.empty()) |
| 339 | { |
| 340 | for (auto& j : dbusParams) |
| 341 | { |
| 342 | /* Get parameter dbus sensor descriptor */ |
| 343 | auto desc = j.value("Desc", empty); |
| 344 | if ((!desc.empty()) && (j.find("ParamName") != j.end())) |
| 345 | { |
| 346 | std::string sensorType = desc.value("SensorType", ""); |
| 347 | std::string name = desc.value("Name", ""); |
| 348 | |
| 349 | if (!sensorType.empty() && !name.empty()) |
| 350 | { |
George Liu | 1204b43 | 2021-12-29 17:24:48 +0800 | [diff] [blame] | 351 | auto path = sensorDbusPath + sensorType + "/" + name; |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 352 | |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 353 | auto paramPtr = |
George Liu | 1204b43 | 2021-12-29 17:24:48 +0800 | [diff] [blame] | 354 | std::make_unique<SensorParam>(bus, path, this); |
| 355 | std::string paramName = j["ParamName"]; |
| 356 | symbols.create_variable(paramName); |
| 357 | paramMap.emplace(std::move(paramName), std::move(paramPtr)); |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 362 | |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 363 | symbols.add_constants(); |
Matt Spinler | 9f1ef4f | 2020-11-09 15:59:11 -0600 | [diff] [blame] | 364 | symbols.add_package(vecopsPackage); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 365 | expression.register_symbol_table(symbols); |
| 366 | |
| 367 | /* parser from exprtk */ |
| 368 | exprtk::parser<double> parser{}; |
Matt Spinler | ddc6dcd | 2020-11-09 11:16:31 -0600 | [diff] [blame] | 369 | if (!parser.compile(exprStr, expression)) |
| 370 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 371 | error("Expression compilation failed"); |
Matt Spinler | ddc6dcd | 2020-11-09 11:16:31 -0600 | [diff] [blame] | 372 | |
| 373 | for (std::size_t i = 0; i < parser.error_count(); ++i) |
| 374 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 375 | auto err = parser.get_error(i); |
| 376 | error("Error parsing token at {POSITION}: {ERROR}", "POSITION", |
| 377 | err.token.position, "TYPE", |
| 378 | exprtk::parser_error::to_str(err.mode), "ERROR", |
| 379 | err.diagnostic); |
Matt Spinler | ddc6dcd | 2020-11-09 11:16:31 -0600 | [diff] [blame] | 380 | } |
| 381 | throw std::runtime_error("Expression compilation failed"); |
| 382 | } |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 383 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 384 | /* Print all parameters for debug purpose only */ |
| 385 | if (DEBUG) |
| 386 | printParams(paramMap); |
| 387 | } |
| 388 | |
Tao Lin | dc77701 | 2022-07-27 20:41:46 +0800 | [diff] [blame^] | 389 | void VirtualSensor::createAssociation(const std::string& objPath, |
| 390 | const std::string& entityPath) |
| 391 | { |
| 392 | if (objPath.empty() || entityPath.empty()) |
| 393 | { |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | std::filesystem::path p(entityPath); |
| 398 | auto assocsDbus = |
| 399 | AssociationList{{"chassis", "all_sensors", p.parent_path().string()}}; |
| 400 | associationIface = |
| 401 | std::make_unique<AssociationObject>(bus, objPath.c_str()); |
| 402 | associationIface->associations(assocsDbus); |
| 403 | } |
| 404 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 405 | void VirtualSensor::initVirtualSensor(const InterfaceMap& interfaceMap, |
| 406 | const std::string& objPath, |
| 407 | const std::string& sensorType, |
| 408 | const std::string& calculationIface) |
| 409 | { |
| 410 | Json thresholds; |
| 411 | const std::string vsThresholdsIntf = |
| 412 | calculationIface + vsThresholdsIfaceSuffix; |
| 413 | |
| 414 | for (const auto& [interface, propertyMap] : interfaceMap) |
| 415 | { |
| 416 | /* Each threshold is on it's own interface with a number as a suffix |
| 417 | * eg xyz.openbmc_project.Configuration.ModifiedMedian.Thresholds1 */ |
| 418 | if (interface.find(vsThresholdsIntf) != std::string::npos) |
| 419 | { |
| 420 | parseThresholds(thresholds, propertyMap); |
| 421 | } |
| 422 | else if (interface == calculationIface) |
| 423 | { |
| 424 | parseConfigInterface(propertyMap, sensorType, interface); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | createThresholds(thresholds, objPath); |
| 429 | symbols.add_constants(); |
| 430 | symbols.add_package(vecopsPackage); |
| 431 | expression.register_symbol_table(symbols); |
| 432 | |
Tao Lin | dc77701 | 2022-07-27 20:41:46 +0800 | [diff] [blame^] | 433 | createAssociation(objPath, entityPath); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 434 | /* Print all parameters for debug purpose only */ |
| 435 | if (DEBUG) |
| 436 | { |
| 437 | printParams(paramMap); |
| 438 | } |
| 439 | } |
| 440 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 441 | void VirtualSensor::setSensorValue(double value) |
| 442 | { |
Patrick Williams | 543bf66 | 2021-04-29 09:03:53 -0500 | [diff] [blame] | 443 | value = std::clamp(value, ValueIface::minValue(), ValueIface::maxValue()); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 444 | ValueIface::value(value); |
| 445 | } |
| 446 | |
Rashmica Gupta | 304fd0e | 2021-08-10 16:50:44 +1000 | [diff] [blame] | 447 | double VirtualSensor::calculateValue(const std::string& calculation, |
| 448 | const VirtualSensor::ParamMap& paramMap) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 449 | { |
Rashmica Gupta | 304fd0e | 2021-08-10 16:50:44 +1000 | [diff] [blame] | 450 | auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(), |
| 451 | calculation); |
| 452 | if (itr == calculationIfaces.end()) |
| 453 | { |
| 454 | return std::numeric_limits<double>::quiet_NaN(); |
| 455 | } |
| 456 | else if (calculation == "xyz.openbmc_project.Configuration.ModifiedMedian") |
| 457 | { |
| 458 | return calculateModifiedMedianValue(paramMap); |
| 459 | } |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 460 | return std::numeric_limits<double>::quiet_NaN(); |
| 461 | } |
| 462 | |
Rashmica Gupta | 304fd0e | 2021-08-10 16:50:44 +1000 | [diff] [blame] | 463 | bool VirtualSensor::sensorInRange(double value) |
| 464 | { |
| 465 | if (value <= this->maxValidInput && value >= this->minValidInput) |
| 466 | { |
| 467 | return true; |
| 468 | } |
| 469 | return false; |
| 470 | } |
| 471 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 472 | void VirtualSensor::updateVirtualSensor() |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 473 | { |
| 474 | for (auto& param : paramMap) |
| 475 | { |
| 476 | auto& name = param.first; |
| 477 | auto& data = param.second; |
| 478 | if (auto var = symbols.get_variable(name)) |
| 479 | { |
| 480 | var->ref() = data->getParamValue(); |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | /* Invalid parameter */ |
| 485 | throw std::invalid_argument("ParamName not found in symbols"); |
| 486 | } |
| 487 | } |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 488 | auto itr = |
| 489 | std::find(calculationIfaces.begin(), calculationIfaces.end(), exprStr); |
Rashmica Gupta | 304fd0e | 2021-08-10 16:50:44 +1000 | [diff] [blame] | 490 | auto val = (itr == calculationIfaces.end()) |
| 491 | ? expression.value() |
| 492 | : calculateValue(exprStr, paramMap); |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 493 | |
| 494 | /* Set sensor value to dbus interface */ |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 495 | setSensorValue(val); |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 496 | |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 497 | if (DEBUG) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 498 | { |
Patrick Williams | fbd7145 | 2021-08-30 06:59:46 -0500 | [diff] [blame] | 499 | debug("Sensor {NAME} = {VALUE}", "NAME", this->name, "VALUE", val); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 500 | } |
Vijay Khemka | 32a7156 | 2020-09-10 15:29:18 -0700 | [diff] [blame] | 501 | |
Matt Spinler | 8f5e611 | 2021-01-15 10:44:32 -0600 | [diff] [blame] | 502 | /* Check sensor thresholds and log required message */ |
Matt Spinler | b306b03 | 2021-02-01 10:05:46 -0600 | [diff] [blame] | 503 | checkThresholds(val, perfLossIface); |
Patrick Williams | fdb826d | 2021-01-20 14:37:53 -0600 | [diff] [blame] | 504 | checkThresholds(val, warningIface); |
| 505 | checkThresholds(val, criticalIface); |
| 506 | checkThresholds(val, softShutdownIface); |
| 507 | checkThresholds(val, hardShutdownIface); |
Vijay Khemka | 3ed9a51 | 2020-08-21 16:13:05 -0700 | [diff] [blame] | 508 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 509 | |
Rashmica Gupta | 304fd0e | 2021-08-10 16:50:44 +1000 | [diff] [blame] | 510 | double VirtualSensor::calculateModifiedMedianValue( |
| 511 | const VirtualSensor::ParamMap& paramMap) |
| 512 | { |
| 513 | std::vector<double> values; |
| 514 | |
| 515 | for (auto& param : paramMap) |
| 516 | { |
| 517 | auto& name = param.first; |
| 518 | if (auto var = symbols.get_variable(name)) |
| 519 | { |
| 520 | if (!sensorInRange(var->ref())) |
| 521 | { |
| 522 | continue; |
| 523 | } |
| 524 | values.push_back(var->ref()); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | size_t size = values.size(); |
| 529 | std::sort(values.begin(), values.end()); |
| 530 | switch (size) |
| 531 | { |
| 532 | case 2: |
| 533 | /* Choose biggest value */ |
| 534 | return values.at(1); |
| 535 | case 0: |
| 536 | return std::numeric_limits<double>::quiet_NaN(); |
| 537 | default: |
| 538 | /* Choose median value */ |
| 539 | if (size % 2 == 0) |
| 540 | { |
| 541 | // Average of the two middle values |
| 542 | return (values.at(size / 2) + values.at(size / 2 - 1)) / 2; |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | return values.at((size - 1) / 2); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 551 | void VirtualSensor::createThresholds(const Json& threshold, |
| 552 | const std::string& objPath) |
| 553 | { |
| 554 | if (threshold.empty()) |
| 555 | { |
| 556 | return; |
| 557 | } |
| 558 | // Only create the threshold interfaces if |
| 559 | // at least one of their values is present. |
| 560 | if (threshold.contains("CriticalHigh") || threshold.contains("CriticalLow")) |
| 561 | { |
| 562 | criticalIface = |
| 563 | std::make_unique<Threshold<CriticalObject>>(bus, objPath.c_str()); |
| 564 | |
| 565 | criticalIface->criticalHigh(threshold.value( |
| 566 | "CriticalHigh", std::numeric_limits<double>::quiet_NaN())); |
| 567 | criticalIface->criticalLow(threshold.value( |
| 568 | "CriticalLow", std::numeric_limits<double>::quiet_NaN())); |
Rashmica Gupta | 1dff7dc | 2021-07-27 19:43:31 +1000 | [diff] [blame] | 569 | criticalIface->setHighHysteresis( |
| 570 | threshold.value("CriticalHighHysteresis", defaultHysteresis)); |
| 571 | criticalIface->setLowHysteresis( |
| 572 | threshold.value("CriticalLowHysteresis", defaultHysteresis)); |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | if (threshold.contains("WarningHigh") || threshold.contains("WarningLow")) |
| 576 | { |
| 577 | warningIface = |
| 578 | std::make_unique<Threshold<WarningObject>>(bus, objPath.c_str()); |
| 579 | |
| 580 | warningIface->warningHigh(threshold.value( |
| 581 | "WarningHigh", std::numeric_limits<double>::quiet_NaN())); |
| 582 | warningIface->warningLow(threshold.value( |
| 583 | "WarningLow", std::numeric_limits<double>::quiet_NaN())); |
Rashmica Gupta | 1dff7dc | 2021-07-27 19:43:31 +1000 | [diff] [blame] | 584 | warningIface->setHighHysteresis( |
| 585 | threshold.value("WarningHighHysteresis", defaultHysteresis)); |
| 586 | warningIface->setLowHysteresis( |
| 587 | threshold.value("WarningLowHysteresis", defaultHysteresis)); |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | if (threshold.contains("HardShutdownHigh") || |
| 591 | threshold.contains("HardShutdownLow")) |
| 592 | { |
| 593 | hardShutdownIface = std::make_unique<Threshold<HardShutdownObject>>( |
| 594 | bus, objPath.c_str()); |
| 595 | |
| 596 | hardShutdownIface->hardShutdownHigh(threshold.value( |
| 597 | "HardShutdownHigh", std::numeric_limits<double>::quiet_NaN())); |
| 598 | hardShutdownIface->hardShutdownLow(threshold.value( |
| 599 | "HardShutdownLow", std::numeric_limits<double>::quiet_NaN())); |
Rashmica Gupta | 1dff7dc | 2021-07-27 19:43:31 +1000 | [diff] [blame] | 600 | hardShutdownIface->setHighHysteresis( |
| 601 | threshold.value("HardShutdownHighHysteresis", defaultHysteresis)); |
| 602 | hardShutdownIface->setLowHysteresis( |
| 603 | threshold.value("HardShutdownLowHysteresis", defaultHysteresis)); |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | if (threshold.contains("SoftShutdownHigh") || |
| 607 | threshold.contains("SoftShutdownLow")) |
| 608 | { |
| 609 | softShutdownIface = std::make_unique<Threshold<SoftShutdownObject>>( |
| 610 | bus, objPath.c_str()); |
| 611 | |
| 612 | softShutdownIface->softShutdownHigh(threshold.value( |
| 613 | "SoftShutdownHigh", std::numeric_limits<double>::quiet_NaN())); |
| 614 | softShutdownIface->softShutdownLow(threshold.value( |
| 615 | "SoftShutdownLow", std::numeric_limits<double>::quiet_NaN())); |
Rashmica Gupta | 1dff7dc | 2021-07-27 19:43:31 +1000 | [diff] [blame] | 616 | softShutdownIface->setHighHysteresis( |
| 617 | threshold.value("SoftShutdownHighHysteresis", defaultHysteresis)); |
| 618 | softShutdownIface->setLowHysteresis( |
| 619 | threshold.value("SoftShutdownLowHysteresis", defaultHysteresis)); |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | if (threshold.contains("PerformanceLossHigh") || |
| 623 | threshold.contains("PerformanceLossLow")) |
| 624 | { |
| 625 | perfLossIface = std::make_unique<Threshold<PerformanceLossObject>>( |
| 626 | bus, objPath.c_str()); |
| 627 | |
| 628 | perfLossIface->performanceLossHigh(threshold.value( |
| 629 | "PerformanceLossHigh", std::numeric_limits<double>::quiet_NaN())); |
| 630 | perfLossIface->performanceLossLow(threshold.value( |
| 631 | "PerformanceLossLow", std::numeric_limits<double>::quiet_NaN())); |
Rashmica Gupta | 1dff7dc | 2021-07-27 19:43:31 +1000 | [diff] [blame] | 632 | perfLossIface->setHighHysteresis(threshold.value( |
| 633 | "PerformanceLossHighHysteresis", defaultHysteresis)); |
| 634 | perfLossIface->setLowHysteresis( |
| 635 | threshold.value("PerformanceLossLowHysteresis", defaultHysteresis)); |
Rashmica Gupta | 3e99919 | 2021-06-09 16:17:04 +1000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 639 | ManagedObjectType VirtualSensors::getObjectsFromDBus() |
| 640 | { |
| 641 | ManagedObjectType objects; |
| 642 | |
| 643 | try |
| 644 | { |
| 645 | auto method = bus.new_method_call(entityManagerBusName, "/", |
| 646 | "org.freedesktop.DBus.ObjectManager", |
| 647 | "GetManagedObjects"); |
| 648 | auto reply = bus.call(method); |
| 649 | reply.read(objects); |
| 650 | } |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 651 | catch (const sdbusplus::exception_t& ex) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 652 | { |
| 653 | // If entity manager isn't running yet, keep going. |
| 654 | if (std::string("org.freedesktop.DBus.Error.ServiceUnknown") != |
| 655 | ex.name()) |
| 656 | { |
| 657 | throw ex.name(); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return objects; |
| 662 | } |
| 663 | |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 664 | void VirtualSensors::propertiesChanged(sdbusplus::message_t& msg) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 665 | { |
| 666 | std::string path; |
| 667 | PropertyMap properties; |
| 668 | |
| 669 | msg.read(path, properties); |
| 670 | |
| 671 | /* We get multiple callbacks for one sensor. 'Type' is a required field and |
| 672 | * is a unique label so use to to only proceed once per sensor */ |
| 673 | if (properties.contains("Type")) |
| 674 | { |
| 675 | if (isCalculationType(path)) |
| 676 | { |
| 677 | createVirtualSensorsFromDBus(path); |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 682 | /** @brief Parsing Virtual Sensor config JSON file */ |
George Liu | 1204b43 | 2021-12-29 17:24:48 +0800 | [diff] [blame] | 683 | Json VirtualSensors::parseConfigFile(const std::string& configFile) |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 684 | { |
| 685 | std::ifstream jsonFile(configFile); |
| 686 | if (!jsonFile.is_open()) |
| 687 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 688 | error("config JSON file {FILENAME} not found", "FILENAME", configFile); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 689 | return {}; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | auto data = Json::parse(jsonFile, nullptr, false); |
| 693 | if (data.is_discarded()) |
| 694 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 695 | error("config readings JSON parser failure with {FILENAME}", "FILENAME", |
| 696 | configFile); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 697 | throw std::exception{}; |
| 698 | } |
| 699 | |
| 700 | return data; |
| 701 | } |
| 702 | |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 703 | std::map<std::string, ValueIface::Unit> unitMap = { |
| 704 | {"temperature", ValueIface::Unit::DegreesC}, |
| 705 | {"fan_tach", ValueIface::Unit::RPMS}, |
| 706 | {"voltage", ValueIface::Unit::Volts}, |
| 707 | {"altitude", ValueIface::Unit::Meters}, |
| 708 | {"current", ValueIface::Unit::Amperes}, |
| 709 | {"power", ValueIface::Unit::Watts}, |
| 710 | {"energy", ValueIface::Unit::Joules}, |
Kumar Thangavel | 2b56ddb | 2021-01-13 20:16:11 +0530 | [diff] [blame] | 711 | {"utilization", ValueIface::Unit::Percent}, |
Rashmica Gupta | 4ac7a7f | 2021-07-08 12:30:50 +1000 | [diff] [blame] | 712 | {"airflow", ValueIface::Unit::CFM}, |
| 713 | {"pressure", ValueIface::Unit::Pascals}}; |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 714 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 715 | const std::string getSensorTypeFromUnit(const std::string& unit) |
| 716 | { |
| 717 | std::string unitPrefix = "xyz.openbmc_project.Sensor.Value.Unit."; |
| 718 | for (auto [type, unitObj] : unitMap) |
| 719 | { |
| 720 | auto unitPath = ValueIface::convertUnitToString(unitObj); |
| 721 | if (unitPath == (unitPrefix + unit)) |
| 722 | { |
| 723 | return type; |
| 724 | } |
| 725 | } |
| 726 | return ""; |
| 727 | } |
| 728 | |
| 729 | void VirtualSensors::setupMatches() |
| 730 | { |
| 731 | /* Already setup */ |
| 732 | if (!this->matches.empty()) |
| 733 | { |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | /* Setup matches */ |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 738 | auto eventHandler = [this](sdbusplus::message_t& message) { |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 739 | if (message.is_method_error()) |
| 740 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 741 | error("Callback method error"); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 742 | return; |
| 743 | } |
| 744 | this->propertiesChanged(message); |
| 745 | }; |
| 746 | |
| 747 | for (const char* iface : calculationIfaces) |
| 748 | { |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 749 | auto match = std::make_unique<sdbusplus::bus::match_t>( |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 750 | bus, |
| 751 | sdbusplus::bus::match::rules::propertiesChangedNamespace( |
| 752 | "/xyz/openbmc_project/inventory", iface), |
| 753 | eventHandler); |
| 754 | this->matches.emplace_back(std::move(match)); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | void VirtualSensors::createVirtualSensorsFromDBus( |
| 759 | const std::string& calculationIface) |
| 760 | { |
| 761 | if (calculationIface.empty()) |
| 762 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 763 | error("No calculation type supplied"); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 764 | return; |
| 765 | } |
| 766 | auto objects = getObjectsFromDBus(); |
| 767 | |
| 768 | /* Get virtual sensors config data */ |
| 769 | for (const auto& [path, interfaceMap] : objects) |
| 770 | { |
| 771 | auto objpath = static_cast<std::string>(path); |
| 772 | std::string name = path.filename(); |
| 773 | std::string sensorType, sensorUnit; |
| 774 | |
| 775 | /* Find Virtual Sensor interfaces */ |
| 776 | if (!interfaceMap.contains(calculationIface)) |
| 777 | { |
| 778 | continue; |
| 779 | } |
| 780 | if (name.empty()) |
| 781 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 782 | error("Virtual Sensor name not found in entity manager config"); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 783 | continue; |
| 784 | } |
| 785 | if (virtualSensorsMap.contains(name)) |
| 786 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 787 | error("A virtual sensor named {NAME} already exists", "NAME", name); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 788 | continue; |
| 789 | } |
| 790 | |
| 791 | /* Extract the virtual sensor type as we need this to initialize the |
| 792 | * sensor */ |
| 793 | for (const auto& [interface, propertyMap] : interfaceMap) |
| 794 | { |
| 795 | if (interface != calculationIface) |
| 796 | { |
| 797 | continue; |
| 798 | } |
| 799 | auto itr = propertyMap.find("Units"); |
| 800 | if (itr != propertyMap.end()) |
| 801 | { |
| 802 | sensorUnit = std::get<std::string>(itr->second); |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | sensorType = getSensorTypeFromUnit(sensorUnit); |
| 807 | if (sensorType.empty()) |
| 808 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 809 | error("Sensor unit type {TYPE} is not supported", "TYPE", |
| 810 | sensorUnit); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 811 | continue; |
| 812 | } |
| 813 | |
| 814 | try |
| 815 | { |
| 816 | auto virtObjPath = sensorDbusPath + sensorType + "/" + name; |
| 817 | |
| 818 | auto virtualSensorPtr = std::make_unique<VirtualSensor>( |
| 819 | bus, virtObjPath.c_str(), interfaceMap, name, sensorType, |
Tao Lin | dc77701 | 2022-07-27 20:41:46 +0800 | [diff] [blame^] | 820 | calculationIface, objpath); |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 821 | info("Added a new virtual sensor: {NAME} {TYPE}", "NAME", name, |
| 822 | "TYPE", sensorType); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 823 | virtualSensorPtr->updateVirtualSensor(); |
| 824 | |
| 825 | /* Initialize unit value for virtual sensor */ |
| 826 | virtualSensorPtr->ValueIface::unit(unitMap[sensorType]); |
| 827 | virtualSensorPtr->emit_object_added(); |
| 828 | |
| 829 | virtualSensorsMap.emplace(name, std::move(virtualSensorPtr)); |
| 830 | |
| 831 | /* Setup match for interfaces removed */ |
| 832 | auto intfRemoved = [this, objpath, |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 833 | name](sdbusplus::message_t& message) { |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 834 | if (!virtualSensorsMap.contains(name)) |
| 835 | { |
| 836 | return; |
| 837 | } |
| 838 | sdbusplus::message::object_path path; |
| 839 | message.read(path); |
| 840 | if (static_cast<const std::string&>(path) == objpath) |
| 841 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 842 | info("Removed a virtual sensor: {NAME}", "NAME", name); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 843 | virtualSensorsMap.erase(name); |
| 844 | } |
| 845 | }; |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 846 | auto matchOnRemove = std::make_unique<sdbusplus::bus::match_t>( |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 847 | bus, |
| 848 | sdbusplus::bus::match::rules::interfacesRemoved() + |
| 849 | sdbusplus::bus::match::rules::argNpath(0, objpath), |
| 850 | intfRemoved); |
| 851 | /* TODO: slight race condition here. Check that the config still |
| 852 | * exists */ |
| 853 | this->matches.emplace_back(std::move(matchOnRemove)); |
| 854 | } |
Patrick Williams | dac2663 | 2021-10-06 14:38:47 -0500 | [diff] [blame] | 855 | catch (const std::invalid_argument& ia) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 856 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 857 | error("Failed to set up virtual sensor: {ERROR}", "ERROR", ia); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 862 | void VirtualSensors::createVirtualSensors() |
| 863 | { |
| 864 | static const Json empty{}; |
| 865 | |
| 866 | auto data = parseConfigFile(VIRTUAL_SENSOR_CONFIG_FILE); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 867 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 868 | // print values |
| 869 | if (DEBUG) |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 870 | { |
Patrick Williams | fbd7145 | 2021-08-30 06:59:46 -0500 | [diff] [blame] | 871 | debug("JSON: {JSON}", "JSON", data.dump()); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 872 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 873 | |
| 874 | /* Get virtual sensors config data */ |
| 875 | for (const auto& j : data) |
| 876 | { |
| 877 | auto desc = j.value("Desc", empty); |
| 878 | if (!desc.empty()) |
| 879 | { |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 880 | if (desc.value("Config", "") == "D-Bus") |
| 881 | { |
| 882 | /* Look on D-Bus for a virtual sensor config. Set up matches |
| 883 | * first because the configs may not be on D-Bus yet and we |
| 884 | * don't want to miss them */ |
| 885 | setupMatches(); |
| 886 | |
| 887 | if (desc.contains("Type")) |
| 888 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 889 | auto type = desc.value("Type", ""); |
| 890 | auto path = "xyz.openbmc_project.Configuration." + type; |
| 891 | |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 892 | if (!isCalculationType(path)) |
| 893 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 894 | error("Invalid calculation type {TYPE} supplied.", |
| 895 | "TYPE", type); |
Rashmica Gupta | e7efe13 | 2021-07-27 19:42:11 +1000 | [diff] [blame] | 896 | continue; |
| 897 | } |
| 898 | createVirtualSensorsFromDBus(path); |
| 899 | } |
| 900 | continue; |
| 901 | } |
| 902 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 903 | std::string sensorType = desc.value("SensorType", ""); |
| 904 | std::string name = desc.value("Name", ""); |
Rashmica Gupta | 665a0a2 | 2021-06-30 11:35:28 +1000 | [diff] [blame] | 905 | std::replace(name.begin(), name.end(), ' ', '_'); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 906 | |
| 907 | if (!name.empty() && !sensorType.empty()) |
| 908 | { |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 909 | if (unitMap.find(sensorType) == unitMap.end()) |
| 910 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 911 | error("Sensor type {TYPE} is not supported", "TYPE", |
| 912 | sensorType); |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 913 | } |
| 914 | else |
| 915 | { |
Rashmica Gupta | 67d8b9d | 2021-06-30 11:41:14 +1000 | [diff] [blame] | 916 | if (virtualSensorsMap.find(name) != virtualSensorsMap.end()) |
| 917 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 918 | error("A virtual sensor named {NAME} already exists", |
| 919 | "NAME", name); |
Rashmica Gupta | 67d8b9d | 2021-06-30 11:41:14 +1000 | [diff] [blame] | 920 | continue; |
| 921 | } |
Rashmica Gupta | 862c3d1 | 2021-08-06 12:19:31 +1000 | [diff] [blame] | 922 | auto objPath = sensorDbusPath + sensorType + "/" + name; |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 923 | |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 924 | auto virtualSensorPtr = std::make_unique<VirtualSensor>( |
| 925 | bus, objPath.c_str(), j, name); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 926 | |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 927 | info("Added a new virtual sensor: {NAME}", "NAME", name); |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 928 | virtualSensorPtr->updateVirtualSensor(); |
| 929 | |
| 930 | /* Initialize unit value for virtual sensor */ |
| 931 | virtualSensorPtr->ValueIface::unit(unitMap[sensorType]); |
Rashmica Gupta | a2fa63a | 2021-08-06 12:21:13 +1000 | [diff] [blame] | 932 | virtualSensorPtr->emit_object_added(); |
Vijay Khemka | e0d371e | 2020-09-21 18:35:52 -0700 | [diff] [blame] | 933 | |
| 934 | virtualSensorsMap.emplace(std::move(name), |
| 935 | std::move(virtualSensorPtr)); |
| 936 | } |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 937 | } |
| 938 | else |
| 939 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 940 | error( |
| 941 | "Sensor type ({TYPE}) or name ({NAME}) not found in config file", |
| 942 | "NAME", name, "TYPE", sensorType); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | else |
| 946 | { |
Patrick Williams | 82b39c6 | 2021-07-28 16:22:27 -0500 | [diff] [blame] | 947 | error("Descriptor for new virtual sensor not found in config file"); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | } // namespace virtualSensor |
| 953 | } // namespace phosphor |
| 954 | |
| 955 | /** |
| 956 | * @brief Main |
| 957 | */ |
| 958 | int main() |
| 959 | { |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 960 | // Get a handle to system dbus |
| 961 | auto bus = sdbusplus::bus::new_default(); |
| 962 | |
Matt Spinler | 6c19e7d | 2021-01-12 16:26:45 -0600 | [diff] [blame] | 963 | // Add the ObjectManager interface |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 964 | sdbusplus::server::manager_t objManager(bus, "/"); |
Matt Spinler | 6c19e7d | 2021-01-12 16:26:45 -0600 | [diff] [blame] | 965 | |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 966 | // Create an virtual sensors object |
| 967 | phosphor::virtualSensor::VirtualSensors virtualSensors(bus); |
| 968 | |
| 969 | // Request service bus name |
| 970 | bus.request_name(busName); |
| 971 | |
Patrick Williams | e667239 | 2022-09-02 09:03:24 -0500 | [diff] [blame] | 972 | // Run the dbus loop. |
| 973 | bus.process_loop(); |
Vijay Khemka | abcc94f | 2020-08-11 15:27:44 -0700 | [diff] [blame] | 974 | |
| 975 | return 0; |
| 976 | } |