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