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