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