1abcc94faSVijay Khemka #include "virtualSensor.hpp" 2abcc94faSVijay Khemka 3abcc94faSVijay Khemka #include "config.hpp" 4abcc94faSVijay Khemka 582b39c66SPatrick Williams #include <phosphor-logging/lg2.hpp> 6abcc94faSVijay Khemka #include <sdeventplus/event.hpp> 7abcc94faSVijay Khemka 8abcc94faSVijay Khemka #include <fstream> 9abcc94faSVijay Khemka 10abcc94faSVijay Khemka static constexpr bool DEBUG = false; 11abcc94faSVijay Khemka static constexpr auto busName = "xyz.openbmc_project.VirtualSensor"; 12abcc94faSVijay Khemka static constexpr auto sensorDbusPath = "/xyz/openbmc_project/sensors/"; 13e7efe135SRashmica Gupta static constexpr auto entityManagerBusName = 14e7efe135SRashmica Gupta "xyz.openbmc_project.EntityManager"; 15e7efe135SRashmica Gupta static constexpr auto vsThresholdsIfaceSuffix = ".Thresholds"; 16304fd0e4SRashmica Gupta static constexpr std::array<const char*, 1> calculationIfaces = { 17304fd0e4SRashmica Gupta "xyz.openbmc_project.Configuration.ModifiedMedian"}; 181dff7dceSRashmica Gupta static constexpr auto defaultHysteresis = 0; 19abcc94faSVijay Khemka 2082b39c66SPatrick Williams PHOSPHOR_LOG2_USING_WITH_FLAGS; 21abcc94faSVijay Khemka 2251f898e2SVijay Khemka int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error*) 2351f898e2SVijay Khemka { 2451f898e2SVijay Khemka if (usrData == nullptr) 2551f898e2SVijay Khemka { 2651f898e2SVijay Khemka throw std::runtime_error("Invalid match"); 2751f898e2SVijay Khemka } 2851f898e2SVijay Khemka 2951f898e2SVijay Khemka auto sdbpMsg = sdbusplus::message::message(msg); 3051f898e2SVijay Khemka std::string msgIfce; 3151f898e2SVijay Khemka std::map<std::string, std::variant<int64_t, double, bool>> msgData; 3251f898e2SVijay Khemka 3351f898e2SVijay Khemka sdbpMsg.read(msgIfce, msgData); 3451f898e2SVijay Khemka 3551f898e2SVijay Khemka if (msgData.find("Value") != msgData.end()) 3651f898e2SVijay Khemka { 3751f898e2SVijay Khemka using namespace phosphor::virtualSensor; 3851f898e2SVijay Khemka VirtualSensor* obj = static_cast<VirtualSensor*>(usrData); 3951f898e2SVijay Khemka // TODO(openbmc/phosphor-virtual-sensor#1): updateVirtualSensor should 4051f898e2SVijay Khemka // be changed to take the information we got from the signal, to avoid 4151f898e2SVijay Khemka // having to do numerous dbus queries. 4251f898e2SVijay Khemka obj->updateVirtualSensor(); 4351f898e2SVijay Khemka } 4451f898e2SVijay Khemka return 0; 4551f898e2SVijay Khemka } 4651f898e2SVijay Khemka 47abcc94faSVijay Khemka namespace phosphor 48abcc94faSVijay Khemka { 49abcc94faSVijay Khemka namespace virtualSensor 50abcc94faSVijay Khemka { 51abcc94faSVijay Khemka 52abcc94faSVijay Khemka void printParams(const VirtualSensor::ParamMap& paramMap) 53abcc94faSVijay Khemka { 54abcc94faSVijay Khemka for (const auto& p : paramMap) 55abcc94faSVijay Khemka { 56abcc94faSVijay Khemka const auto& p1 = p.first; 57abcc94faSVijay Khemka const auto& p2 = p.second; 58abcc94faSVijay Khemka auto val = p2->getParamValue(); 59fbd7145eSPatrick Williams debug("Parameter: {PARAM} = {VALUE}", "PARAM", p1, "VALUE", val); 60abcc94faSVijay Khemka } 61abcc94faSVijay Khemka } 62abcc94faSVijay Khemka 63abcc94faSVijay Khemka double SensorParam::getParamValue() 64abcc94faSVijay Khemka { 65abcc94faSVijay Khemka switch (paramType) 66abcc94faSVijay Khemka { 67abcc94faSVijay Khemka case constParam: 68abcc94faSVijay Khemka return value; 69abcc94faSVijay Khemka break; 707452a867SVijay Khemka case dbusParam: 717452a867SVijay Khemka return dbusSensor->getSensorValue(); 727452a867SVijay Khemka break; 73abcc94faSVijay Khemka default: 74abcc94faSVijay Khemka throw std::invalid_argument("param type not supported"); 75abcc94faSVijay Khemka } 76abcc94faSVijay Khemka } 77abcc94faSVijay Khemka 780fcf0e1cSLei YU using AssociationList = 790fcf0e1cSLei YU std::vector<std::tuple<std::string, std::string, std::string>>; 800fcf0e1cSLei YU 810fcf0e1cSLei YU AssociationList getAssociationsFromJson(const Json& j) 820fcf0e1cSLei YU { 830fcf0e1cSLei YU AssociationList assocs{}; 840fcf0e1cSLei YU try 850fcf0e1cSLei YU { 860fcf0e1cSLei YU j.get_to(assocs); 870fcf0e1cSLei YU } 880fcf0e1cSLei YU catch (const std::exception& ex) 890fcf0e1cSLei YU { 9082b39c66SPatrick Williams error("Failed to parse association: {ERROR}", "ERROR", ex); 910fcf0e1cSLei YU } 920fcf0e1cSLei YU return assocs; 930fcf0e1cSLei YU } 940fcf0e1cSLei YU 95e7efe135SRashmica Gupta template <typename U> 96e7efe135SRashmica Gupta struct VariantToNumber 97e7efe135SRashmica Gupta { 98e7efe135SRashmica Gupta template <typename T> 99e7efe135SRashmica Gupta U operator()(const T& t) const 100e7efe135SRashmica Gupta { 101e7efe135SRashmica Gupta if constexpr (std::is_convertible<T, U>::value) 102e7efe135SRashmica Gupta { 103e7efe135SRashmica Gupta return static_cast<U>(t); 104e7efe135SRashmica Gupta } 105e7efe135SRashmica Gupta throw std::invalid_argument("Invalid number type in config\n"); 106e7efe135SRashmica Gupta } 107e7efe135SRashmica Gupta }; 108e7efe135SRashmica Gupta 109e7efe135SRashmica Gupta template <typename U> 110e7efe135SRashmica Gupta U getNumberFromConfig(const PropertyMap& map, const std::string& name, 111e7efe135SRashmica Gupta bool required) 112e7efe135SRashmica Gupta { 113e7efe135SRashmica Gupta if (auto itr = map.find(name); itr != map.end()) 114e7efe135SRashmica Gupta { 115e7efe135SRashmica Gupta return std::visit(VariantToNumber<U>(), itr->second); 116e7efe135SRashmica Gupta } 117e7efe135SRashmica Gupta else if (required) 118e7efe135SRashmica Gupta { 11982b39c66SPatrick Williams error("Required field {NAME} missing in config", "NAME", name); 120e7efe135SRashmica Gupta throw std::invalid_argument("Required field missing in config"); 121e7efe135SRashmica Gupta } 122e7efe135SRashmica Gupta return std::numeric_limits<U>::quiet_NaN(); 123e7efe135SRashmica Gupta } 124e7efe135SRashmica Gupta 125e7efe135SRashmica Gupta bool isCalculationType(const std::string& interface) 126e7efe135SRashmica Gupta { 127e7efe135SRashmica Gupta auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(), 128e7efe135SRashmica Gupta interface); 129e7efe135SRashmica Gupta if (itr != calculationIfaces.end()) 130e7efe135SRashmica Gupta { 131e7efe135SRashmica Gupta return true; 132e7efe135SRashmica Gupta } 133e7efe135SRashmica Gupta return false; 134e7efe135SRashmica Gupta } 135e7efe135SRashmica Gupta 136e7efe135SRashmica Gupta const std::string getThresholdType(const std::string& direction, 13705b1d417SRashmica Gupta const std::string& severity) 138e7efe135SRashmica Gupta { 139e7efe135SRashmica Gupta std::string suffix; 140e7efe135SRashmica Gupta 141e7efe135SRashmica Gupta if (direction == "less than") 142e7efe135SRashmica Gupta { 143e7efe135SRashmica Gupta suffix = "Low"; 144e7efe135SRashmica Gupta } 145e7efe135SRashmica Gupta else if (direction == "greater than") 146e7efe135SRashmica Gupta { 147e7efe135SRashmica Gupta suffix = "High"; 148e7efe135SRashmica Gupta } 149e7efe135SRashmica Gupta else 150e7efe135SRashmica Gupta { 151e7efe135SRashmica Gupta throw std::invalid_argument( 152e7efe135SRashmica Gupta "Invalid threshold direction specified in entity manager"); 153e7efe135SRashmica Gupta } 15405b1d417SRashmica Gupta return severity + suffix; 15505b1d417SRashmica Gupta } 15605b1d417SRashmica Gupta 15705b1d417SRashmica Gupta std::string getSeverityField(const PropertyMap& propertyMap) 15805b1d417SRashmica Gupta { 15905b1d417SRashmica Gupta static const std::array thresholdTypes{"Warning", "Critical", 16005b1d417SRashmica Gupta "PerformanceLoss", "SoftShutdown", 16105b1d417SRashmica Gupta "HardShutdown"}; 16205b1d417SRashmica Gupta 16305b1d417SRashmica Gupta std::string severity; 16405b1d417SRashmica Gupta if (auto itr = propertyMap.find("Severity"); itr != propertyMap.end()) 16505b1d417SRashmica Gupta { 16605b1d417SRashmica Gupta /* Severity should be a string, but can be an unsigned int */ 16705b1d417SRashmica Gupta if (std::holds_alternative<std::string>(itr->second)) 16805b1d417SRashmica Gupta { 16905b1d417SRashmica Gupta severity = std::get<std::string>(itr->second); 17005b1d417SRashmica Gupta if (0 == std::ranges::count(thresholdTypes, severity)) 17105b1d417SRashmica Gupta { 17205b1d417SRashmica Gupta throw std::invalid_argument( 17305b1d417SRashmica Gupta "Invalid threshold severity specified in entity manager"); 17405b1d417SRashmica Gupta } 17505b1d417SRashmica Gupta } 17605b1d417SRashmica Gupta else 17705b1d417SRashmica Gupta { 17805b1d417SRashmica Gupta auto sev = 17905b1d417SRashmica Gupta getNumberFromConfig<uint64_t>(propertyMap, "Severity", true); 18005b1d417SRashmica Gupta /* Checking bounds ourselves so we throw invalid argument on 18105b1d417SRashmica Gupta * invalid user input */ 18205b1d417SRashmica Gupta if (sev >= thresholdTypes.size()) 18305b1d417SRashmica Gupta { 18405b1d417SRashmica Gupta throw std::invalid_argument( 18505b1d417SRashmica Gupta "Invalid threshold severity specified in entity manager"); 18605b1d417SRashmica Gupta } 18705b1d417SRashmica Gupta severity = thresholdTypes.at(sev); 18805b1d417SRashmica Gupta } 18905b1d417SRashmica Gupta } 19005b1d417SRashmica Gupta return severity; 191e7efe135SRashmica Gupta } 192e7efe135SRashmica Gupta 193e7efe135SRashmica Gupta void parseThresholds(Json& thresholds, const PropertyMap& propertyMap) 194e7efe135SRashmica Gupta { 195e7efe135SRashmica Gupta std::string direction; 196e7efe135SRashmica Gupta 197e7efe135SRashmica Gupta auto value = getNumberFromConfig<double>(propertyMap, "Value", true); 198e7efe135SRashmica Gupta 19905b1d417SRashmica Gupta auto severity = getSeverityField(propertyMap); 20005b1d417SRashmica Gupta 20105b1d417SRashmica Gupta if (auto itr = propertyMap.find("Direction"); itr != propertyMap.end()) 202e7efe135SRashmica Gupta { 203e7efe135SRashmica Gupta direction = std::get<std::string>(itr->second); 204e7efe135SRashmica Gupta } 205e7efe135SRashmica Gupta 206e7efe135SRashmica Gupta auto threshold = getThresholdType(direction, severity); 207e7efe135SRashmica Gupta thresholds[threshold] = value; 2081dff7dceSRashmica Gupta 2091dff7dceSRashmica Gupta auto hysteresis = 2101dff7dceSRashmica Gupta getNumberFromConfig<double>(propertyMap, "Hysteresis", false); 2111dff7dceSRashmica Gupta if (hysteresis != std::numeric_limits<double>::quiet_NaN()) 2121dff7dceSRashmica Gupta { 2131dff7dceSRashmica Gupta thresholds[threshold + "Hysteresis"] = hysteresis; 2141dff7dceSRashmica Gupta } 215e7efe135SRashmica Gupta } 216e7efe135SRashmica Gupta 217e7efe135SRashmica Gupta void VirtualSensor::parseConfigInterface(const PropertyMap& propertyMap, 218e7efe135SRashmica Gupta const std::string& sensorType, 219e7efe135SRashmica Gupta const std::string& interface) 220e7efe135SRashmica Gupta { 221e7efe135SRashmica Gupta /* Parse sensors / DBus params */ 222e7efe135SRashmica Gupta if (auto itr = propertyMap.find("Sensors"); itr != propertyMap.end()) 223e7efe135SRashmica Gupta { 224e7efe135SRashmica Gupta auto sensors = std::get<std::vector<std::string>>(itr->second); 225e7efe135SRashmica Gupta for (auto sensor : sensors) 226e7efe135SRashmica Gupta { 227e7efe135SRashmica Gupta std::replace(sensor.begin(), sensor.end(), ' ', '_'); 228e7efe135SRashmica Gupta auto sensorObjPath = sensorDbusPath + sensorType + "/" + sensor; 229e7efe135SRashmica Gupta 230e7efe135SRashmica Gupta auto paramPtr = 231e7efe135SRashmica Gupta std::make_unique<SensorParam>(bus, sensorObjPath, this); 232e7efe135SRashmica Gupta symbols.create_variable(sensor); 233e7efe135SRashmica Gupta paramMap.emplace(std::move(sensor), std::move(paramPtr)); 234e7efe135SRashmica Gupta } 235e7efe135SRashmica Gupta } 236e7efe135SRashmica Gupta /* Get expression string */ 237e7efe135SRashmica Gupta if (!isCalculationType(interface)) 238e7efe135SRashmica Gupta { 239e7efe135SRashmica Gupta throw std::invalid_argument("Invalid expression in interface"); 240e7efe135SRashmica Gupta } 241e7efe135SRashmica Gupta exprStr = interface; 242e7efe135SRashmica Gupta 243e7efe135SRashmica Gupta /* Get optional min and max input and output values */ 244e7efe135SRashmica Gupta ValueIface::maxValue( 245e7efe135SRashmica Gupta getNumberFromConfig<double>(propertyMap, "MaxValue", false)); 246e7efe135SRashmica Gupta ValueIface::minValue( 247e7efe135SRashmica Gupta getNumberFromConfig<double>(propertyMap, "MinValue", false)); 248e7efe135SRashmica Gupta maxValidInput = 249e7efe135SRashmica Gupta getNumberFromConfig<double>(propertyMap, "MaxValidInput", false); 250e7efe135SRashmica Gupta minValidInput = 251e7efe135SRashmica Gupta getNumberFromConfig<double>(propertyMap, "MinValidInput", false); 252e7efe135SRashmica Gupta } 253e7efe135SRashmica Gupta 254ce675228SMatt Spinler void VirtualSensor::initVirtualSensor(const Json& sensorConfig, 255ce675228SMatt Spinler const std::string& objPath) 256abcc94faSVijay Khemka { 257abcc94faSVijay Khemka 258abcc94faSVijay Khemka static const Json empty{}; 259abcc94faSVijay Khemka 260abcc94faSVijay Khemka /* Get threshold values if defined in config */ 261abcc94faSVijay Khemka auto threshold = sensorConfig.value("Threshold", empty); 262f15189e3SMatt Spinler 2633e99919bSRashmica Gupta createThresholds(threshold, objPath); 264abcc94faSVijay Khemka 265f6443742SHarvey Wu /* Get MaxValue, MinValue setting if defined in config */ 266f6443742SHarvey Wu auto confDesc = sensorConfig.value("Desc", empty); 267f6443742SHarvey Wu if (auto maxConf = confDesc.find("MaxValue"); 268f6443742SHarvey Wu maxConf != confDesc.end() && maxConf->is_number()) 269f6443742SHarvey Wu { 270f6443742SHarvey Wu ValueIface::maxValue(maxConf->get<double>()); 271f6443742SHarvey Wu } 272f6443742SHarvey Wu if (auto minConf = confDesc.find("MinValue"); 273f6443742SHarvey Wu minConf != confDesc.end() && minConf->is_number()) 274f6443742SHarvey Wu { 275f6443742SHarvey Wu ValueIface::minValue(minConf->get<double>()); 276f6443742SHarvey Wu } 277f6443742SHarvey Wu 2780fcf0e1cSLei YU /* Get optional association */ 2790fcf0e1cSLei YU auto assocJson = sensorConfig.value("Associations", empty); 2800fcf0e1cSLei YU if (!assocJson.empty()) 2810fcf0e1cSLei YU { 2820fcf0e1cSLei YU auto assocs = getAssociationsFromJson(assocJson); 2830fcf0e1cSLei YU if (!assocs.empty()) 2840fcf0e1cSLei YU { 2850fcf0e1cSLei YU associationIface = 2860fcf0e1cSLei YU std::make_unique<AssociationObject>(bus, objPath.c_str()); 2870fcf0e1cSLei YU associationIface->associations(assocs); 2880fcf0e1cSLei YU } 2890fcf0e1cSLei YU } 2900fcf0e1cSLei YU 291abcc94faSVijay Khemka /* Get expression string */ 292*03c4c8e2SPatrick Williams static constexpr auto exprKey = "Expression"; 293*03c4c8e2SPatrick Williams if (sensorConfig.contains(exprKey)) 294*03c4c8e2SPatrick Williams { 295*03c4c8e2SPatrick Williams auto ref = sensorConfig.at(exprKey); 296*03c4c8e2SPatrick Williams if (ref.is_array()) 297*03c4c8e2SPatrick Williams { 298*03c4c8e2SPatrick Williams exprStr = std::string{}; 299*03c4c8e2SPatrick Williams for (auto& s : ref) 300*03c4c8e2SPatrick Williams { 301*03c4c8e2SPatrick Williams exprStr += s; 302*03c4c8e2SPatrick Williams } 303*03c4c8e2SPatrick Williams } 304*03c4c8e2SPatrick Williams else if (ref.is_string()) 305*03c4c8e2SPatrick Williams { 306*03c4c8e2SPatrick Williams exprStr = std::string{ref}; 307*03c4c8e2SPatrick Williams } 308*03c4c8e2SPatrick Williams } 309abcc94faSVijay Khemka 310abcc94faSVijay Khemka /* Get all the parameter listed in configuration */ 311abcc94faSVijay Khemka auto params = sensorConfig.value("Params", empty); 312abcc94faSVijay Khemka 313abcc94faSVijay Khemka /* Check for constant parameter */ 314abcc94faSVijay Khemka const auto& consParams = params.value("ConstParam", empty); 315abcc94faSVijay Khemka if (!consParams.empty()) 316abcc94faSVijay Khemka { 317abcc94faSVijay Khemka for (auto& j : consParams) 318abcc94faSVijay Khemka { 319abcc94faSVijay Khemka if (j.find("ParamName") != j.end()) 320abcc94faSVijay Khemka { 321abcc94faSVijay Khemka auto paramPtr = std::make_unique<SensorParam>(j["Value"]); 3223ed9a516SVijay Khemka std::string name = j["ParamName"]; 3233ed9a516SVijay Khemka symbols.create_variable(name); 3243ed9a516SVijay Khemka paramMap.emplace(std::move(name), std::move(paramPtr)); 325abcc94faSVijay Khemka } 326abcc94faSVijay Khemka else 327abcc94faSVijay Khemka { 328abcc94faSVijay Khemka /* Invalid configuration */ 329abcc94faSVijay Khemka throw std::invalid_argument( 330abcc94faSVijay Khemka "ParamName not found in configuration"); 331abcc94faSVijay Khemka } 332abcc94faSVijay Khemka } 333abcc94faSVijay Khemka } 334abcc94faSVijay Khemka 3357452a867SVijay Khemka /* Check for dbus parameter */ 3367452a867SVijay Khemka auto dbusParams = params.value("DbusParam", empty); 3377452a867SVijay Khemka if (!dbusParams.empty()) 3387452a867SVijay Khemka { 3397452a867SVijay Khemka for (auto& j : dbusParams) 3407452a867SVijay Khemka { 3417452a867SVijay Khemka /* Get parameter dbus sensor descriptor */ 3427452a867SVijay Khemka auto desc = j.value("Desc", empty); 3437452a867SVijay Khemka if ((!desc.empty()) && (j.find("ParamName") != j.end())) 3447452a867SVijay Khemka { 3457452a867SVijay Khemka std::string sensorType = desc.value("SensorType", ""); 3467452a867SVijay Khemka std::string name = desc.value("Name", ""); 3477452a867SVijay Khemka 3487452a867SVijay Khemka if (!sensorType.empty() && !name.empty()) 3497452a867SVijay Khemka { 3501204b433SGeorge Liu auto path = sensorDbusPath + sensorType + "/" + name; 3517452a867SVijay Khemka 35251f898e2SVijay Khemka auto paramPtr = 3531204b433SGeorge Liu std::make_unique<SensorParam>(bus, path, this); 3541204b433SGeorge Liu std::string paramName = j["ParamName"]; 3551204b433SGeorge Liu symbols.create_variable(paramName); 3561204b433SGeorge Liu paramMap.emplace(std::move(paramName), std::move(paramPtr)); 3577452a867SVijay Khemka } 3587452a867SVijay Khemka } 3597452a867SVijay Khemka } 3607452a867SVijay Khemka } 361abcc94faSVijay Khemka 3623ed9a516SVijay Khemka symbols.add_constants(); 3639f1ef4f5SMatt Spinler symbols.add_package(vecopsPackage); 3643ed9a516SVijay Khemka expression.register_symbol_table(symbols); 3653ed9a516SVijay Khemka 3663ed9a516SVijay Khemka /* parser from exprtk */ 3673ed9a516SVijay Khemka exprtk::parser<double> parser{}; 368ddc6dcd6SMatt Spinler if (!parser.compile(exprStr, expression)) 369ddc6dcd6SMatt Spinler { 37082b39c66SPatrick Williams error("Expression compilation failed"); 371ddc6dcd6SMatt Spinler 372ddc6dcd6SMatt Spinler for (std::size_t i = 0; i < parser.error_count(); ++i) 373ddc6dcd6SMatt Spinler { 37482b39c66SPatrick Williams auto err = parser.get_error(i); 37582b39c66SPatrick Williams error("Error parsing token at {POSITION}: {ERROR}", "POSITION", 37682b39c66SPatrick Williams err.token.position, "TYPE", 37782b39c66SPatrick Williams exprtk::parser_error::to_str(err.mode), "ERROR", 37882b39c66SPatrick Williams err.diagnostic); 379ddc6dcd6SMatt Spinler } 380ddc6dcd6SMatt Spinler throw std::runtime_error("Expression compilation failed"); 381ddc6dcd6SMatt Spinler } 3823ed9a516SVijay Khemka 383abcc94faSVijay Khemka /* Print all parameters for debug purpose only */ 384abcc94faSVijay Khemka if (DEBUG) 385abcc94faSVijay Khemka printParams(paramMap); 386abcc94faSVijay Khemka } 387abcc94faSVijay Khemka 388e7efe135SRashmica Gupta void VirtualSensor::initVirtualSensor(const InterfaceMap& interfaceMap, 389e7efe135SRashmica Gupta const std::string& objPath, 390e7efe135SRashmica Gupta const std::string& sensorType, 391e7efe135SRashmica Gupta const std::string& calculationIface) 392e7efe135SRashmica Gupta { 393e7efe135SRashmica Gupta Json thresholds; 394e7efe135SRashmica Gupta const std::string vsThresholdsIntf = 395e7efe135SRashmica Gupta calculationIface + vsThresholdsIfaceSuffix; 396e7efe135SRashmica Gupta 397e7efe135SRashmica Gupta for (const auto& [interface, propertyMap] : interfaceMap) 398e7efe135SRashmica Gupta { 399e7efe135SRashmica Gupta /* Each threshold is on it's own interface with a number as a suffix 400e7efe135SRashmica Gupta * eg xyz.openbmc_project.Configuration.ModifiedMedian.Thresholds1 */ 401e7efe135SRashmica Gupta if (interface.find(vsThresholdsIntf) != std::string::npos) 402e7efe135SRashmica Gupta { 403e7efe135SRashmica Gupta parseThresholds(thresholds, propertyMap); 404e7efe135SRashmica Gupta } 405e7efe135SRashmica Gupta else if (interface == calculationIface) 406e7efe135SRashmica Gupta { 407e7efe135SRashmica Gupta parseConfigInterface(propertyMap, sensorType, interface); 408e7efe135SRashmica Gupta } 409e7efe135SRashmica Gupta } 410e7efe135SRashmica Gupta 411e7efe135SRashmica Gupta createThresholds(thresholds, objPath); 412e7efe135SRashmica Gupta symbols.add_constants(); 413e7efe135SRashmica Gupta symbols.add_package(vecopsPackage); 414e7efe135SRashmica Gupta expression.register_symbol_table(symbols); 415e7efe135SRashmica Gupta 416e7efe135SRashmica Gupta /* Print all parameters for debug purpose only */ 417e7efe135SRashmica Gupta if (DEBUG) 418e7efe135SRashmica Gupta { 419e7efe135SRashmica Gupta printParams(paramMap); 420e7efe135SRashmica Gupta } 421e7efe135SRashmica Gupta } 422e7efe135SRashmica Gupta 423abcc94faSVijay Khemka void VirtualSensor::setSensorValue(double value) 424abcc94faSVijay Khemka { 425543bf668SPatrick Williams value = std::clamp(value, ValueIface::minValue(), ValueIface::maxValue()); 426abcc94faSVijay Khemka ValueIface::value(value); 427abcc94faSVijay Khemka } 428abcc94faSVijay Khemka 429304fd0e4SRashmica Gupta double VirtualSensor::calculateValue(const std::string& calculation, 430304fd0e4SRashmica Gupta const VirtualSensor::ParamMap& paramMap) 431e7efe135SRashmica Gupta { 432304fd0e4SRashmica Gupta auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(), 433304fd0e4SRashmica Gupta calculation); 434304fd0e4SRashmica Gupta if (itr == calculationIfaces.end()) 435304fd0e4SRashmica Gupta { 436e7efe135SRashmica Gupta return std::numeric_limits<double>::quiet_NaN(); 437e7efe135SRashmica Gupta } 438304fd0e4SRashmica Gupta else if (calculation == "xyz.openbmc_project.Configuration.ModifiedMedian") 439304fd0e4SRashmica Gupta { 440304fd0e4SRashmica Gupta return calculateModifiedMedianValue(paramMap); 441304fd0e4SRashmica Gupta } 442304fd0e4SRashmica Gupta return std::numeric_limits<double>::quiet_NaN(); 443304fd0e4SRashmica Gupta } 444304fd0e4SRashmica Gupta 445304fd0e4SRashmica Gupta bool VirtualSensor::sensorInRange(double value) 446304fd0e4SRashmica Gupta { 447304fd0e4SRashmica Gupta if (value <= this->maxValidInput && value >= this->minValidInput) 448304fd0e4SRashmica Gupta { 449304fd0e4SRashmica Gupta return true; 450304fd0e4SRashmica Gupta } 451304fd0e4SRashmica Gupta return false; 452304fd0e4SRashmica Gupta } 453e7efe135SRashmica Gupta 454abcc94faSVijay Khemka void VirtualSensor::updateVirtualSensor() 4553ed9a516SVijay Khemka { 4563ed9a516SVijay Khemka for (auto& param : paramMap) 4573ed9a516SVijay Khemka { 4583ed9a516SVijay Khemka auto& name = param.first; 4593ed9a516SVijay Khemka auto& data = param.second; 4603ed9a516SVijay Khemka if (auto var = symbols.get_variable(name)) 4613ed9a516SVijay Khemka { 4623ed9a516SVijay Khemka var->ref() = data->getParamValue(); 4633ed9a516SVijay Khemka } 4643ed9a516SVijay Khemka else 4653ed9a516SVijay Khemka { 4663ed9a516SVijay Khemka /* Invalid parameter */ 4673ed9a516SVijay Khemka throw std::invalid_argument("ParamName not found in symbols"); 4683ed9a516SVijay Khemka } 4693ed9a516SVijay Khemka } 470e7efe135SRashmica Gupta auto itr = 471e7efe135SRashmica Gupta std::find(calculationIfaces.begin(), calculationIfaces.end(), exprStr); 472304fd0e4SRashmica Gupta auto val = (itr == calculationIfaces.end()) 473304fd0e4SRashmica Gupta ? expression.value() 474304fd0e4SRashmica Gupta : calculateValue(exprStr, paramMap); 47532a7156bSVijay Khemka 47632a7156bSVijay Khemka /* Set sensor value to dbus interface */ 4773ed9a516SVijay Khemka setSensorValue(val); 47832a7156bSVijay Khemka 4793ed9a516SVijay Khemka if (DEBUG) 480e7efe135SRashmica Gupta { 481fbd7145eSPatrick Williams debug("Sensor {NAME} = {VALUE}", "NAME", this->name, "VALUE", val); 482e7efe135SRashmica Gupta } 48332a7156bSVijay Khemka 4848f5e6119SMatt Spinler /* Check sensor thresholds and log required message */ 485b306b03dSMatt Spinler checkThresholds(val, perfLossIface); 486fdb826d5SPatrick Williams checkThresholds(val, warningIface); 487fdb826d5SPatrick Williams checkThresholds(val, criticalIface); 488fdb826d5SPatrick Williams checkThresholds(val, softShutdownIface); 489fdb826d5SPatrick Williams checkThresholds(val, hardShutdownIface); 4903ed9a516SVijay Khemka } 491abcc94faSVijay Khemka 492304fd0e4SRashmica Gupta double VirtualSensor::calculateModifiedMedianValue( 493304fd0e4SRashmica Gupta const VirtualSensor::ParamMap& paramMap) 494304fd0e4SRashmica Gupta { 495304fd0e4SRashmica Gupta std::vector<double> values; 496304fd0e4SRashmica Gupta 497304fd0e4SRashmica Gupta for (auto& param : paramMap) 498304fd0e4SRashmica Gupta { 499304fd0e4SRashmica Gupta auto& name = param.first; 500304fd0e4SRashmica Gupta if (auto var = symbols.get_variable(name)) 501304fd0e4SRashmica Gupta { 502304fd0e4SRashmica Gupta if (!sensorInRange(var->ref())) 503304fd0e4SRashmica Gupta { 504304fd0e4SRashmica Gupta continue; 505304fd0e4SRashmica Gupta } 506304fd0e4SRashmica Gupta values.push_back(var->ref()); 507304fd0e4SRashmica Gupta } 508304fd0e4SRashmica Gupta } 509304fd0e4SRashmica Gupta 510304fd0e4SRashmica Gupta size_t size = values.size(); 511304fd0e4SRashmica Gupta std::sort(values.begin(), values.end()); 512304fd0e4SRashmica Gupta switch (size) 513304fd0e4SRashmica Gupta { 514304fd0e4SRashmica Gupta case 2: 515304fd0e4SRashmica Gupta /* Choose biggest value */ 516304fd0e4SRashmica Gupta return values.at(1); 517304fd0e4SRashmica Gupta case 0: 518304fd0e4SRashmica Gupta return std::numeric_limits<double>::quiet_NaN(); 519304fd0e4SRashmica Gupta default: 520304fd0e4SRashmica Gupta /* Choose median value */ 521304fd0e4SRashmica Gupta if (size % 2 == 0) 522304fd0e4SRashmica Gupta { 523304fd0e4SRashmica Gupta // Average of the two middle values 524304fd0e4SRashmica Gupta return (values.at(size / 2) + values.at(size / 2 - 1)) / 2; 525304fd0e4SRashmica Gupta } 526304fd0e4SRashmica Gupta else 527304fd0e4SRashmica Gupta { 528304fd0e4SRashmica Gupta return values.at((size - 1) / 2); 529304fd0e4SRashmica Gupta } 530304fd0e4SRashmica Gupta } 531304fd0e4SRashmica Gupta } 532304fd0e4SRashmica Gupta 5333e99919bSRashmica Gupta void VirtualSensor::createThresholds(const Json& threshold, 5343e99919bSRashmica Gupta const std::string& objPath) 5353e99919bSRashmica Gupta { 5363e99919bSRashmica Gupta if (threshold.empty()) 5373e99919bSRashmica Gupta { 5383e99919bSRashmica Gupta return; 5393e99919bSRashmica Gupta } 5403e99919bSRashmica Gupta // Only create the threshold interfaces if 5413e99919bSRashmica Gupta // at least one of their values is present. 5423e99919bSRashmica Gupta if (threshold.contains("CriticalHigh") || threshold.contains("CriticalLow")) 5433e99919bSRashmica Gupta { 5443e99919bSRashmica Gupta criticalIface = 5453e99919bSRashmica Gupta std::make_unique<Threshold<CriticalObject>>(bus, objPath.c_str()); 5463e99919bSRashmica Gupta 5473e99919bSRashmica Gupta criticalIface->criticalHigh(threshold.value( 5483e99919bSRashmica Gupta "CriticalHigh", std::numeric_limits<double>::quiet_NaN())); 5493e99919bSRashmica Gupta criticalIface->criticalLow(threshold.value( 5503e99919bSRashmica Gupta "CriticalLow", std::numeric_limits<double>::quiet_NaN())); 5511dff7dceSRashmica Gupta criticalIface->setHighHysteresis( 5521dff7dceSRashmica Gupta threshold.value("CriticalHighHysteresis", defaultHysteresis)); 5531dff7dceSRashmica Gupta criticalIface->setLowHysteresis( 5541dff7dceSRashmica Gupta threshold.value("CriticalLowHysteresis", defaultHysteresis)); 5553e99919bSRashmica Gupta } 5563e99919bSRashmica Gupta 5573e99919bSRashmica Gupta if (threshold.contains("WarningHigh") || threshold.contains("WarningLow")) 5583e99919bSRashmica Gupta { 5593e99919bSRashmica Gupta warningIface = 5603e99919bSRashmica Gupta std::make_unique<Threshold<WarningObject>>(bus, objPath.c_str()); 5613e99919bSRashmica Gupta 5623e99919bSRashmica Gupta warningIface->warningHigh(threshold.value( 5633e99919bSRashmica Gupta "WarningHigh", std::numeric_limits<double>::quiet_NaN())); 5643e99919bSRashmica Gupta warningIface->warningLow(threshold.value( 5653e99919bSRashmica Gupta "WarningLow", std::numeric_limits<double>::quiet_NaN())); 5661dff7dceSRashmica Gupta warningIface->setHighHysteresis( 5671dff7dceSRashmica Gupta threshold.value("WarningHighHysteresis", defaultHysteresis)); 5681dff7dceSRashmica Gupta warningIface->setLowHysteresis( 5691dff7dceSRashmica Gupta threshold.value("WarningLowHysteresis", defaultHysteresis)); 5703e99919bSRashmica Gupta } 5713e99919bSRashmica Gupta 5723e99919bSRashmica Gupta if (threshold.contains("HardShutdownHigh") || 5733e99919bSRashmica Gupta threshold.contains("HardShutdownLow")) 5743e99919bSRashmica Gupta { 5753e99919bSRashmica Gupta hardShutdownIface = std::make_unique<Threshold<HardShutdownObject>>( 5763e99919bSRashmica Gupta bus, objPath.c_str()); 5773e99919bSRashmica Gupta 5783e99919bSRashmica Gupta hardShutdownIface->hardShutdownHigh(threshold.value( 5793e99919bSRashmica Gupta "HardShutdownHigh", std::numeric_limits<double>::quiet_NaN())); 5803e99919bSRashmica Gupta hardShutdownIface->hardShutdownLow(threshold.value( 5813e99919bSRashmica Gupta "HardShutdownLow", std::numeric_limits<double>::quiet_NaN())); 5821dff7dceSRashmica Gupta hardShutdownIface->setHighHysteresis( 5831dff7dceSRashmica Gupta threshold.value("HardShutdownHighHysteresis", defaultHysteresis)); 5841dff7dceSRashmica Gupta hardShutdownIface->setLowHysteresis( 5851dff7dceSRashmica Gupta threshold.value("HardShutdownLowHysteresis", defaultHysteresis)); 5863e99919bSRashmica Gupta } 5873e99919bSRashmica Gupta 5883e99919bSRashmica Gupta if (threshold.contains("SoftShutdownHigh") || 5893e99919bSRashmica Gupta threshold.contains("SoftShutdownLow")) 5903e99919bSRashmica Gupta { 5913e99919bSRashmica Gupta softShutdownIface = std::make_unique<Threshold<SoftShutdownObject>>( 5923e99919bSRashmica Gupta bus, objPath.c_str()); 5933e99919bSRashmica Gupta 5943e99919bSRashmica Gupta softShutdownIface->softShutdownHigh(threshold.value( 5953e99919bSRashmica Gupta "SoftShutdownHigh", std::numeric_limits<double>::quiet_NaN())); 5963e99919bSRashmica Gupta softShutdownIface->softShutdownLow(threshold.value( 5973e99919bSRashmica Gupta "SoftShutdownLow", std::numeric_limits<double>::quiet_NaN())); 5981dff7dceSRashmica Gupta softShutdownIface->setHighHysteresis( 5991dff7dceSRashmica Gupta threshold.value("SoftShutdownHighHysteresis", defaultHysteresis)); 6001dff7dceSRashmica Gupta softShutdownIface->setLowHysteresis( 6011dff7dceSRashmica Gupta threshold.value("SoftShutdownLowHysteresis", defaultHysteresis)); 6023e99919bSRashmica Gupta } 6033e99919bSRashmica Gupta 6043e99919bSRashmica Gupta if (threshold.contains("PerformanceLossHigh") || 6053e99919bSRashmica Gupta threshold.contains("PerformanceLossLow")) 6063e99919bSRashmica Gupta { 6073e99919bSRashmica Gupta perfLossIface = std::make_unique<Threshold<PerformanceLossObject>>( 6083e99919bSRashmica Gupta bus, objPath.c_str()); 6093e99919bSRashmica Gupta 6103e99919bSRashmica Gupta perfLossIface->performanceLossHigh(threshold.value( 6113e99919bSRashmica Gupta "PerformanceLossHigh", std::numeric_limits<double>::quiet_NaN())); 6123e99919bSRashmica Gupta perfLossIface->performanceLossLow(threshold.value( 6133e99919bSRashmica Gupta "PerformanceLossLow", std::numeric_limits<double>::quiet_NaN())); 6141dff7dceSRashmica Gupta perfLossIface->setHighHysteresis(threshold.value( 6151dff7dceSRashmica Gupta "PerformanceLossHighHysteresis", defaultHysteresis)); 6161dff7dceSRashmica Gupta perfLossIface->setLowHysteresis( 6171dff7dceSRashmica Gupta threshold.value("PerformanceLossLowHysteresis", defaultHysteresis)); 6183e99919bSRashmica Gupta } 6193e99919bSRashmica Gupta } 6203e99919bSRashmica Gupta 621e7efe135SRashmica Gupta ManagedObjectType VirtualSensors::getObjectsFromDBus() 622e7efe135SRashmica Gupta { 623e7efe135SRashmica Gupta ManagedObjectType objects; 624e7efe135SRashmica Gupta 625e7efe135SRashmica Gupta try 626e7efe135SRashmica Gupta { 627e7efe135SRashmica Gupta auto method = bus.new_method_call(entityManagerBusName, "/", 628e7efe135SRashmica Gupta "org.freedesktop.DBus.ObjectManager", 629e7efe135SRashmica Gupta "GetManagedObjects"); 630e7efe135SRashmica Gupta auto reply = bus.call(method); 631e7efe135SRashmica Gupta reply.read(objects); 632e7efe135SRashmica Gupta } 63374c1e7d8SPatrick Williams catch (const sdbusplus::exception::exception& ex) 634e7efe135SRashmica Gupta { 635e7efe135SRashmica Gupta // If entity manager isn't running yet, keep going. 636e7efe135SRashmica Gupta if (std::string("org.freedesktop.DBus.Error.ServiceUnknown") != 637e7efe135SRashmica Gupta ex.name()) 638e7efe135SRashmica Gupta { 639e7efe135SRashmica Gupta throw ex.name(); 640e7efe135SRashmica Gupta } 641e7efe135SRashmica Gupta } 642e7efe135SRashmica Gupta 643e7efe135SRashmica Gupta return objects; 644e7efe135SRashmica Gupta } 645e7efe135SRashmica Gupta 646e7efe135SRashmica Gupta void VirtualSensors::propertiesChanged(sdbusplus::message::message& msg) 647e7efe135SRashmica Gupta { 648e7efe135SRashmica Gupta std::string path; 649e7efe135SRashmica Gupta PropertyMap properties; 650e7efe135SRashmica Gupta 651e7efe135SRashmica Gupta msg.read(path, properties); 652e7efe135SRashmica Gupta 653e7efe135SRashmica Gupta /* We get multiple callbacks for one sensor. 'Type' is a required field and 654e7efe135SRashmica Gupta * is a unique label so use to to only proceed once per sensor */ 655e7efe135SRashmica Gupta if (properties.contains("Type")) 656e7efe135SRashmica Gupta { 657e7efe135SRashmica Gupta if (isCalculationType(path)) 658e7efe135SRashmica Gupta { 659e7efe135SRashmica Gupta createVirtualSensorsFromDBus(path); 660e7efe135SRashmica Gupta } 661e7efe135SRashmica Gupta } 662e7efe135SRashmica Gupta } 663e7efe135SRashmica Gupta 664abcc94faSVijay Khemka /** @brief Parsing Virtual Sensor config JSON file */ 6651204b433SGeorge Liu Json VirtualSensors::parseConfigFile(const std::string& configFile) 666abcc94faSVijay Khemka { 667abcc94faSVijay Khemka std::ifstream jsonFile(configFile); 668abcc94faSVijay Khemka if (!jsonFile.is_open()) 669abcc94faSVijay Khemka { 67082b39c66SPatrick Williams error("config JSON file {FILENAME} not found", "FILENAME", configFile); 671e7efe135SRashmica Gupta return {}; 672abcc94faSVijay Khemka } 673abcc94faSVijay Khemka 674abcc94faSVijay Khemka auto data = Json::parse(jsonFile, nullptr, false); 675abcc94faSVijay Khemka if (data.is_discarded()) 676abcc94faSVijay Khemka { 67782b39c66SPatrick Williams error("config readings JSON parser failure with {FILENAME}", "FILENAME", 67882b39c66SPatrick Williams configFile); 679abcc94faSVijay Khemka throw std::exception{}; 680abcc94faSVijay Khemka } 681abcc94faSVijay Khemka 682abcc94faSVijay Khemka return data; 683abcc94faSVijay Khemka } 684abcc94faSVijay Khemka 685e0d371e4SVijay Khemka std::map<std::string, ValueIface::Unit> unitMap = { 686e0d371e4SVijay Khemka {"temperature", ValueIface::Unit::DegreesC}, 687e0d371e4SVijay Khemka {"fan_tach", ValueIface::Unit::RPMS}, 688e0d371e4SVijay Khemka {"voltage", ValueIface::Unit::Volts}, 689e0d371e4SVijay Khemka {"altitude", ValueIface::Unit::Meters}, 690e0d371e4SVijay Khemka {"current", ValueIface::Unit::Amperes}, 691e0d371e4SVijay Khemka {"power", ValueIface::Unit::Watts}, 692e0d371e4SVijay Khemka {"energy", ValueIface::Unit::Joules}, 6932b56ddb3SKumar Thangavel {"utilization", ValueIface::Unit::Percent}, 6944ac7a7f2SRashmica Gupta {"airflow", ValueIface::Unit::CFM}, 6954ac7a7f2SRashmica Gupta {"pressure", ValueIface::Unit::Pascals}}; 696e0d371e4SVijay Khemka 697e7efe135SRashmica Gupta const std::string getSensorTypeFromUnit(const std::string& unit) 698e7efe135SRashmica Gupta { 699e7efe135SRashmica Gupta std::string unitPrefix = "xyz.openbmc_project.Sensor.Value.Unit."; 700e7efe135SRashmica Gupta for (auto [type, unitObj] : unitMap) 701e7efe135SRashmica Gupta { 702e7efe135SRashmica Gupta auto unitPath = ValueIface::convertUnitToString(unitObj); 703e7efe135SRashmica Gupta if (unitPath == (unitPrefix + unit)) 704e7efe135SRashmica Gupta { 705e7efe135SRashmica Gupta return type; 706e7efe135SRashmica Gupta } 707e7efe135SRashmica Gupta } 708e7efe135SRashmica Gupta return ""; 709e7efe135SRashmica Gupta } 710e7efe135SRashmica Gupta 711e7efe135SRashmica Gupta void VirtualSensors::setupMatches() 712e7efe135SRashmica Gupta { 713e7efe135SRashmica Gupta /* Already setup */ 714e7efe135SRashmica Gupta if (!this->matches.empty()) 715e7efe135SRashmica Gupta { 716e7efe135SRashmica Gupta return; 717e7efe135SRashmica Gupta } 718e7efe135SRashmica Gupta 719e7efe135SRashmica Gupta /* Setup matches */ 720e7efe135SRashmica Gupta auto eventHandler = [this](sdbusplus::message::message& message) { 721e7efe135SRashmica Gupta if (message.is_method_error()) 722e7efe135SRashmica Gupta { 72382b39c66SPatrick Williams error("Callback method error"); 724e7efe135SRashmica Gupta return; 725e7efe135SRashmica Gupta } 726e7efe135SRashmica Gupta this->propertiesChanged(message); 727e7efe135SRashmica Gupta }; 728e7efe135SRashmica Gupta 729e7efe135SRashmica Gupta for (const char* iface : calculationIfaces) 730e7efe135SRashmica Gupta { 731e7efe135SRashmica Gupta auto match = std::make_unique<sdbusplus::bus::match::match>( 732e7efe135SRashmica Gupta bus, 733e7efe135SRashmica Gupta sdbusplus::bus::match::rules::propertiesChangedNamespace( 734e7efe135SRashmica Gupta "/xyz/openbmc_project/inventory", iface), 735e7efe135SRashmica Gupta eventHandler); 736e7efe135SRashmica Gupta this->matches.emplace_back(std::move(match)); 737e7efe135SRashmica Gupta } 738e7efe135SRashmica Gupta } 739e7efe135SRashmica Gupta 740e7efe135SRashmica Gupta void VirtualSensors::createVirtualSensorsFromDBus( 741e7efe135SRashmica Gupta const std::string& calculationIface) 742e7efe135SRashmica Gupta { 743e7efe135SRashmica Gupta if (calculationIface.empty()) 744e7efe135SRashmica Gupta { 74582b39c66SPatrick Williams error("No calculation type supplied"); 746e7efe135SRashmica Gupta return; 747e7efe135SRashmica Gupta } 748e7efe135SRashmica Gupta auto objects = getObjectsFromDBus(); 749e7efe135SRashmica Gupta 750e7efe135SRashmica Gupta /* Get virtual sensors config data */ 751e7efe135SRashmica Gupta for (const auto& [path, interfaceMap] : objects) 752e7efe135SRashmica Gupta { 753e7efe135SRashmica Gupta auto objpath = static_cast<std::string>(path); 754e7efe135SRashmica Gupta std::string name = path.filename(); 755e7efe135SRashmica Gupta std::string sensorType, sensorUnit; 756e7efe135SRashmica Gupta 757e7efe135SRashmica Gupta /* Find Virtual Sensor interfaces */ 758e7efe135SRashmica Gupta if (!interfaceMap.contains(calculationIface)) 759e7efe135SRashmica Gupta { 760e7efe135SRashmica Gupta continue; 761e7efe135SRashmica Gupta } 762e7efe135SRashmica Gupta if (name.empty()) 763e7efe135SRashmica Gupta { 76482b39c66SPatrick Williams error("Virtual Sensor name not found in entity manager config"); 765e7efe135SRashmica Gupta continue; 766e7efe135SRashmica Gupta } 767e7efe135SRashmica Gupta if (virtualSensorsMap.contains(name)) 768e7efe135SRashmica Gupta { 76982b39c66SPatrick Williams error("A virtual sensor named {NAME} already exists", "NAME", name); 770e7efe135SRashmica Gupta continue; 771e7efe135SRashmica Gupta } 772e7efe135SRashmica Gupta 773e7efe135SRashmica Gupta /* Extract the virtual sensor type as we need this to initialize the 774e7efe135SRashmica Gupta * sensor */ 775e7efe135SRashmica Gupta for (const auto& [interface, propertyMap] : interfaceMap) 776e7efe135SRashmica Gupta { 777e7efe135SRashmica Gupta if (interface != calculationIface) 778e7efe135SRashmica Gupta { 779e7efe135SRashmica Gupta continue; 780e7efe135SRashmica Gupta } 781e7efe135SRashmica Gupta auto itr = propertyMap.find("Units"); 782e7efe135SRashmica Gupta if (itr != propertyMap.end()) 783e7efe135SRashmica Gupta { 784e7efe135SRashmica Gupta sensorUnit = std::get<std::string>(itr->second); 785e7efe135SRashmica Gupta break; 786e7efe135SRashmica Gupta } 787e7efe135SRashmica Gupta } 788e7efe135SRashmica Gupta sensorType = getSensorTypeFromUnit(sensorUnit); 789e7efe135SRashmica Gupta if (sensorType.empty()) 790e7efe135SRashmica Gupta { 79182b39c66SPatrick Williams error("Sensor unit type {TYPE} is not supported", "TYPE", 79282b39c66SPatrick Williams sensorUnit); 793e7efe135SRashmica Gupta continue; 794e7efe135SRashmica Gupta } 795e7efe135SRashmica Gupta 796e7efe135SRashmica Gupta try 797e7efe135SRashmica Gupta { 798e7efe135SRashmica Gupta auto virtObjPath = sensorDbusPath + sensorType + "/" + name; 799e7efe135SRashmica Gupta 800e7efe135SRashmica Gupta auto virtualSensorPtr = std::make_unique<VirtualSensor>( 801e7efe135SRashmica Gupta bus, virtObjPath.c_str(), interfaceMap, name, sensorType, 802e7efe135SRashmica Gupta calculationIface); 80382b39c66SPatrick Williams info("Added a new virtual sensor: {NAME} {TYPE}", "NAME", name, 80482b39c66SPatrick Williams "TYPE", sensorType); 805e7efe135SRashmica Gupta virtualSensorPtr->updateVirtualSensor(); 806e7efe135SRashmica Gupta 807e7efe135SRashmica Gupta /* Initialize unit value for virtual sensor */ 808e7efe135SRashmica Gupta virtualSensorPtr->ValueIface::unit(unitMap[sensorType]); 809e7efe135SRashmica Gupta virtualSensorPtr->emit_object_added(); 810e7efe135SRashmica Gupta 811e7efe135SRashmica Gupta virtualSensorsMap.emplace(name, std::move(virtualSensorPtr)); 812e7efe135SRashmica Gupta 813e7efe135SRashmica Gupta /* Setup match for interfaces removed */ 814e7efe135SRashmica Gupta auto intfRemoved = [this, objpath, 815e7efe135SRashmica Gupta name](sdbusplus::message::message& message) { 816e7efe135SRashmica Gupta if (!virtualSensorsMap.contains(name)) 817e7efe135SRashmica Gupta { 818e7efe135SRashmica Gupta return; 819e7efe135SRashmica Gupta } 820e7efe135SRashmica Gupta sdbusplus::message::object_path path; 821e7efe135SRashmica Gupta message.read(path); 822e7efe135SRashmica Gupta if (static_cast<const std::string&>(path) == objpath) 823e7efe135SRashmica Gupta { 82482b39c66SPatrick Williams info("Removed a virtual sensor: {NAME}", "NAME", name); 825e7efe135SRashmica Gupta virtualSensorsMap.erase(name); 826e7efe135SRashmica Gupta } 827e7efe135SRashmica Gupta }; 828e7efe135SRashmica Gupta auto matchOnRemove = std::make_unique<sdbusplus::bus::match::match>( 829e7efe135SRashmica Gupta bus, 830e7efe135SRashmica Gupta sdbusplus::bus::match::rules::interfacesRemoved() + 831e7efe135SRashmica Gupta sdbusplus::bus::match::rules::argNpath(0, objpath), 832e7efe135SRashmica Gupta intfRemoved); 833e7efe135SRashmica Gupta /* TODO: slight race condition here. Check that the config still 834e7efe135SRashmica Gupta * exists */ 835e7efe135SRashmica Gupta this->matches.emplace_back(std::move(matchOnRemove)); 836e7efe135SRashmica Gupta } 837dac2663cSPatrick Williams catch (const std::invalid_argument& ia) 838e7efe135SRashmica Gupta { 83982b39c66SPatrick Williams error("Failed to set up virtual sensor: {ERROR}", "ERROR", ia); 840e7efe135SRashmica Gupta } 841e7efe135SRashmica Gupta } 842e7efe135SRashmica Gupta } 843e7efe135SRashmica Gupta 844abcc94faSVijay Khemka void VirtualSensors::createVirtualSensors() 845abcc94faSVijay Khemka { 846abcc94faSVijay Khemka static const Json empty{}; 847abcc94faSVijay Khemka 848abcc94faSVijay Khemka auto data = parseConfigFile(VIRTUAL_SENSOR_CONFIG_FILE); 849e7efe135SRashmica Gupta 850abcc94faSVijay Khemka // print values 851abcc94faSVijay Khemka if (DEBUG) 852e7efe135SRashmica Gupta { 853fbd7145eSPatrick Williams debug("JSON: {JSON}", "JSON", data.dump()); 854e7efe135SRashmica Gupta } 855abcc94faSVijay Khemka 856abcc94faSVijay Khemka /* Get virtual sensors config data */ 857abcc94faSVijay Khemka for (const auto& j : data) 858abcc94faSVijay Khemka { 859abcc94faSVijay Khemka auto desc = j.value("Desc", empty); 860abcc94faSVijay Khemka if (!desc.empty()) 861abcc94faSVijay Khemka { 862e7efe135SRashmica Gupta if (desc.value("Config", "") == "D-Bus") 863e7efe135SRashmica Gupta { 864e7efe135SRashmica Gupta /* Look on D-Bus for a virtual sensor config. Set up matches 865e7efe135SRashmica Gupta * first because the configs may not be on D-Bus yet and we 866e7efe135SRashmica Gupta * don't want to miss them */ 867e7efe135SRashmica Gupta setupMatches(); 868e7efe135SRashmica Gupta 869e7efe135SRashmica Gupta if (desc.contains("Type")) 870e7efe135SRashmica Gupta { 87182b39c66SPatrick Williams auto type = desc.value("Type", ""); 87282b39c66SPatrick Williams auto path = "xyz.openbmc_project.Configuration." + type; 87382b39c66SPatrick Williams 874e7efe135SRashmica Gupta if (!isCalculationType(path)) 875e7efe135SRashmica Gupta { 87682b39c66SPatrick Williams error("Invalid calculation type {TYPE} supplied.", 87782b39c66SPatrick Williams "TYPE", type); 878e7efe135SRashmica Gupta continue; 879e7efe135SRashmica Gupta } 880e7efe135SRashmica Gupta createVirtualSensorsFromDBus(path); 881e7efe135SRashmica Gupta } 882e7efe135SRashmica Gupta continue; 883e7efe135SRashmica Gupta } 884e7efe135SRashmica Gupta 885abcc94faSVijay Khemka std::string sensorType = desc.value("SensorType", ""); 886abcc94faSVijay Khemka std::string name = desc.value("Name", ""); 887665a0a29SRashmica Gupta std::replace(name.begin(), name.end(), ' ', '_'); 888abcc94faSVijay Khemka 889abcc94faSVijay Khemka if (!name.empty() && !sensorType.empty()) 890abcc94faSVijay Khemka { 891e0d371e4SVijay Khemka if (unitMap.find(sensorType) == unitMap.end()) 892e0d371e4SVijay Khemka { 89382b39c66SPatrick Williams error("Sensor type {TYPE} is not supported", "TYPE", 89482b39c66SPatrick Williams sensorType); 895e0d371e4SVijay Khemka } 896e0d371e4SVijay Khemka else 897e0d371e4SVijay Khemka { 89867d8b9d2SRashmica Gupta if (virtualSensorsMap.find(name) != virtualSensorsMap.end()) 89967d8b9d2SRashmica Gupta { 90082b39c66SPatrick Williams error("A virtual sensor named {NAME} already exists", 90182b39c66SPatrick Williams "NAME", name); 90267d8b9d2SRashmica Gupta continue; 90367d8b9d2SRashmica Gupta } 904862c3d1eSRashmica Gupta auto objPath = sensorDbusPath + sensorType + "/" + name; 905abcc94faSVijay Khemka 90632a7156bSVijay Khemka auto virtualSensorPtr = std::make_unique<VirtualSensor>( 90732a7156bSVijay Khemka bus, objPath.c_str(), j, name); 908abcc94faSVijay Khemka 90982b39c66SPatrick Williams info("Added a new virtual sensor: {NAME}", "NAME", name); 9103ed9a516SVijay Khemka virtualSensorPtr->updateVirtualSensor(); 911e0d371e4SVijay Khemka 912e0d371e4SVijay Khemka /* Initialize unit value for virtual sensor */ 913e0d371e4SVijay Khemka virtualSensorPtr->ValueIface::unit(unitMap[sensorType]); 914a2fa63a6SRashmica Gupta virtualSensorPtr->emit_object_added(); 915e0d371e4SVijay Khemka 9163ed9a516SVijay Khemka virtualSensorsMap.emplace(std::move(name), 9173ed9a516SVijay Khemka std::move(virtualSensorPtr)); 918abcc94faSVijay Khemka } 919e0d371e4SVijay Khemka } 920abcc94faSVijay Khemka else 921abcc94faSVijay Khemka { 92282b39c66SPatrick Williams error( 92382b39c66SPatrick Williams "Sensor type ({TYPE}) or name ({NAME}) not found in config file", 92482b39c66SPatrick Williams "NAME", name, "TYPE", sensorType); 925abcc94faSVijay Khemka } 926abcc94faSVijay Khemka } 927abcc94faSVijay Khemka else 928abcc94faSVijay Khemka { 92982b39c66SPatrick Williams error("Descriptor for new virtual sensor not found in config file"); 930abcc94faSVijay Khemka } 931abcc94faSVijay Khemka } 932abcc94faSVijay Khemka } 933abcc94faSVijay Khemka 934abcc94faSVijay Khemka } // namespace virtualSensor 935abcc94faSVijay Khemka } // namespace phosphor 936abcc94faSVijay Khemka 937abcc94faSVijay Khemka /** 938abcc94faSVijay Khemka * @brief Main 939abcc94faSVijay Khemka */ 940abcc94faSVijay Khemka int main() 941abcc94faSVijay Khemka { 942abcc94faSVijay Khemka 943abcc94faSVijay Khemka // Get a default event loop 944abcc94faSVijay Khemka auto event = sdeventplus::Event::get_default(); 945abcc94faSVijay Khemka 946abcc94faSVijay Khemka // Get a handle to system dbus 947abcc94faSVijay Khemka auto bus = sdbusplus::bus::new_default(); 948abcc94faSVijay Khemka 9496c19e7d2SMatt Spinler // Add the ObjectManager interface 9506c19e7d2SMatt Spinler sdbusplus::server::manager::manager objManager(bus, "/"); 9516c19e7d2SMatt Spinler 952abcc94faSVijay Khemka // Create an virtual sensors object 953abcc94faSVijay Khemka phosphor::virtualSensor::VirtualSensors virtualSensors(bus); 954abcc94faSVijay Khemka 955abcc94faSVijay Khemka // Request service bus name 956abcc94faSVijay Khemka bus.request_name(busName); 957abcc94faSVijay Khemka 958abcc94faSVijay Khemka // Attach the bus to sd_event to service user requests 959abcc94faSVijay Khemka bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 960abcc94faSVijay Khemka event.loop(); 961abcc94faSVijay Khemka 962abcc94faSVijay Khemka return 0; 963abcc94faSVijay Khemka } 964