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