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