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