1abcc94faSVijay Khemka #include "virtualSensor.hpp" 2abcc94faSVijay Khemka 382b39c66SPatrick Williams #include <phosphor-logging/lg2.hpp> 4abcc94faSVijay Khemka 5abcc94faSVijay Khemka #include <fstream> 6abcc94faSVijay Khemka 7abcc94faSVijay Khemka static constexpr bool DEBUG = false; 8abcc94faSVijay Khemka static constexpr auto busName = "xyz.openbmc_project.VirtualSensor"; 9abcc94faSVijay Khemka static constexpr auto sensorDbusPath = "/xyz/openbmc_project/sensors/"; 10e7efe135SRashmica Gupta static constexpr auto vsThresholdsIfaceSuffix = ".Thresholds"; 11f6b7e0a4STao Lin static constexpr std::array<const char*, 2> calculationIfaces = { 12f6b7e0a4STao Lin "xyz.openbmc_project.Configuration.ModifiedMedian", 13f6b7e0a4STao Lin "xyz.openbmc_project.Configuration.Maximum"}; 141dff7dceSRashmica Gupta static constexpr auto defaultHysteresis = 0; 15abcc94faSVijay Khemka 1682b39c66SPatrick Williams PHOSPHOR_LOG2_USING_WITH_FLAGS; 17abcc94faSVijay Khemka 1851f898e2SVijay Khemka int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error*) 1951f898e2SVijay Khemka { 2051f898e2SVijay Khemka if (usrData == nullptr) 2151f898e2SVijay Khemka { 2251f898e2SVijay Khemka throw std::runtime_error("Invalid match"); 2351f898e2SVijay Khemka } 2451f898e2SVijay Khemka 258e11cccbSPatrick Williams auto sdbpMsg = sdbusplus::message_t(msg); 2651f898e2SVijay Khemka std::string msgIfce; 2751f898e2SVijay Khemka std::map<std::string, std::variant<int64_t, double, bool>> msgData; 2851f898e2SVijay Khemka 2951f898e2SVijay Khemka sdbpMsg.read(msgIfce, msgData); 3051f898e2SVijay Khemka 3151f898e2SVijay Khemka if (msgData.find("Value") != msgData.end()) 3251f898e2SVijay Khemka { 3351f898e2SVijay Khemka using namespace phosphor::virtualSensor; 3451f898e2SVijay Khemka VirtualSensor* obj = static_cast<VirtualSensor*>(usrData); 3551f898e2SVijay Khemka // TODO(openbmc/phosphor-virtual-sensor#1): updateVirtualSensor should 3651f898e2SVijay Khemka // be changed to take the information we got from the signal, to avoid 3751f898e2SVijay Khemka // having to do numerous dbus queries. 3851f898e2SVijay Khemka obj->updateVirtualSensor(); 3951f898e2SVijay Khemka } 4051f898e2SVijay Khemka return 0; 4151f898e2SVijay Khemka } 4251f898e2SVijay Khemka 43abcc94faSVijay Khemka namespace phosphor 44abcc94faSVijay Khemka { 45abcc94faSVijay Khemka namespace virtualSensor 46abcc94faSVijay Khemka { 47abcc94faSVijay Khemka 48abcc94faSVijay Khemka void printParams(const VirtualSensor::ParamMap& paramMap) 49abcc94faSVijay Khemka { 50abcc94faSVijay Khemka for (const auto& p : paramMap) 51abcc94faSVijay Khemka { 52abcc94faSVijay Khemka const auto& p1 = p.first; 53abcc94faSVijay Khemka const auto& p2 = p.second; 54abcc94faSVijay Khemka auto val = p2->getParamValue(); 55fbd7145eSPatrick Williams debug("Parameter: {PARAM} = {VALUE}", "PARAM", p1, "VALUE", val); 56abcc94faSVijay Khemka } 57abcc94faSVijay Khemka } 58abcc94faSVijay Khemka 59abcc94faSVijay Khemka double SensorParam::getParamValue() 60abcc94faSVijay Khemka { 61abcc94faSVijay Khemka switch (paramType) 62abcc94faSVijay Khemka { 63abcc94faSVijay Khemka case constParam: 64abcc94faSVijay Khemka return value; 65abcc94faSVijay Khemka break; 667452a867SVijay Khemka case dbusParam: 677452a867SVijay Khemka return dbusSensor->getSensorValue(); 687452a867SVijay Khemka break; 69abcc94faSVijay Khemka default: 70abcc94faSVijay Khemka throw std::invalid_argument("param type not supported"); 71abcc94faSVijay Khemka } 72abcc94faSVijay Khemka } 73abcc94faSVijay Khemka 740fcf0e1cSLei YU using AssociationList = 750fcf0e1cSLei YU std::vector<std::tuple<std::string, std::string, std::string>>; 760fcf0e1cSLei YU 770fcf0e1cSLei YU AssociationList getAssociationsFromJson(const Json& j) 780fcf0e1cSLei YU { 790fcf0e1cSLei YU AssociationList assocs{}; 800fcf0e1cSLei YU try 810fcf0e1cSLei YU { 820fcf0e1cSLei YU j.get_to(assocs); 830fcf0e1cSLei YU } 840fcf0e1cSLei YU catch (const std::exception& ex) 850fcf0e1cSLei YU { 8682b39c66SPatrick Williams error("Failed to parse association: {ERROR}", "ERROR", ex); 870fcf0e1cSLei YU } 880fcf0e1cSLei YU return assocs; 890fcf0e1cSLei YU } 900fcf0e1cSLei YU 91e7efe135SRashmica Gupta template <typename U> 92e7efe135SRashmica Gupta struct VariantToNumber 93e7efe135SRashmica Gupta { 94e7efe135SRashmica Gupta template <typename T> 95e7efe135SRashmica Gupta U operator()(const T& t) const 96e7efe135SRashmica Gupta { 97e7efe135SRashmica Gupta if constexpr (std::is_convertible<T, U>::value) 98e7efe135SRashmica Gupta { 99e7efe135SRashmica Gupta return static_cast<U>(t); 100e7efe135SRashmica Gupta } 101e7efe135SRashmica Gupta throw std::invalid_argument("Invalid number type in config\n"); 102e7efe135SRashmica Gupta } 103e7efe135SRashmica Gupta }; 104e7efe135SRashmica Gupta 105e7efe135SRashmica Gupta template <typename U> 106e7efe135SRashmica Gupta U getNumberFromConfig(const PropertyMap& map, const std::string& name, 107190f6d06SJiaqing Zhao bool required, 108190f6d06SJiaqing Zhao U defaultValue = std::numeric_limits<U>::quiet_NaN()) 109e7efe135SRashmica Gupta { 110e7efe135SRashmica Gupta if (auto itr = map.find(name); itr != map.end()) 111e7efe135SRashmica Gupta { 112e7efe135SRashmica Gupta return std::visit(VariantToNumber<U>(), itr->second); 113e7efe135SRashmica Gupta } 114e7efe135SRashmica Gupta else if (required) 115e7efe135SRashmica Gupta { 11682b39c66SPatrick Williams error("Required field {NAME} missing in config", "NAME", name); 117e7efe135SRashmica Gupta throw std::invalid_argument("Required field missing in config"); 118e7efe135SRashmica Gupta } 119190f6d06SJiaqing Zhao return defaultValue; 120e7efe135SRashmica Gupta } 121e7efe135SRashmica Gupta 122e7efe135SRashmica Gupta bool isCalculationType(const std::string& interface) 123e7efe135SRashmica Gupta { 124e7efe135SRashmica Gupta auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(), 125e7efe135SRashmica Gupta interface); 126e7efe135SRashmica Gupta if (itr != calculationIfaces.end()) 127e7efe135SRashmica Gupta { 128e7efe135SRashmica Gupta return true; 129e7efe135SRashmica Gupta } 130e7efe135SRashmica Gupta return false; 131e7efe135SRashmica Gupta } 132e7efe135SRashmica Gupta 133e7efe135SRashmica Gupta const std::string getThresholdType(const std::string& direction, 13405b1d417SRashmica Gupta const std::string& severity) 135e7efe135SRashmica Gupta { 136e7efe135SRashmica Gupta std::string suffix; 137e7efe135SRashmica Gupta 138e7efe135SRashmica Gupta if (direction == "less than") 139e7efe135SRashmica Gupta { 140e7efe135SRashmica Gupta suffix = "Low"; 141e7efe135SRashmica Gupta } 142e7efe135SRashmica Gupta else if (direction == "greater than") 143e7efe135SRashmica Gupta { 144e7efe135SRashmica Gupta suffix = "High"; 145e7efe135SRashmica Gupta } 146e7efe135SRashmica Gupta else 147e7efe135SRashmica Gupta { 148e7efe135SRashmica Gupta throw std::invalid_argument( 149e7efe135SRashmica Gupta "Invalid threshold direction specified in entity manager"); 150e7efe135SRashmica Gupta } 15105b1d417SRashmica Gupta return severity + suffix; 15205b1d417SRashmica Gupta } 15305b1d417SRashmica Gupta 15405b1d417SRashmica Gupta std::string getSeverityField(const PropertyMap& propertyMap) 15505b1d417SRashmica Gupta { 15605b1d417SRashmica Gupta static const std::array thresholdTypes{"Warning", "Critical", 15705b1d417SRashmica Gupta "PerformanceLoss", "SoftShutdown", 15805b1d417SRashmica Gupta "HardShutdown"}; 15905b1d417SRashmica Gupta 16005b1d417SRashmica Gupta std::string severity; 16105b1d417SRashmica Gupta if (auto itr = propertyMap.find("Severity"); itr != propertyMap.end()) 16205b1d417SRashmica Gupta { 16305b1d417SRashmica Gupta /* Severity should be a string, but can be an unsigned int */ 16405b1d417SRashmica Gupta if (std::holds_alternative<std::string>(itr->second)) 16505b1d417SRashmica Gupta { 16605b1d417SRashmica Gupta severity = std::get<std::string>(itr->second); 16705b1d417SRashmica Gupta if (0 == std::ranges::count(thresholdTypes, severity)) 16805b1d417SRashmica Gupta { 16905b1d417SRashmica Gupta throw std::invalid_argument( 17005b1d417SRashmica Gupta "Invalid threshold severity specified in entity manager"); 17105b1d417SRashmica Gupta } 17205b1d417SRashmica Gupta } 17305b1d417SRashmica Gupta else 17405b1d417SRashmica Gupta { 175*1226f208SPatrick Williams auto sev = getNumberFromConfig<uint64_t>(propertyMap, "Severity", 176*1226f208SPatrick Williams true); 17705b1d417SRashmica Gupta /* Checking bounds ourselves so we throw invalid argument on 17805b1d417SRashmica Gupta * invalid user input */ 17905b1d417SRashmica Gupta if (sev >= thresholdTypes.size()) 18005b1d417SRashmica Gupta { 18105b1d417SRashmica Gupta throw std::invalid_argument( 18205b1d417SRashmica Gupta "Invalid threshold severity specified in entity manager"); 18305b1d417SRashmica Gupta } 18405b1d417SRashmica Gupta severity = thresholdTypes.at(sev); 18505b1d417SRashmica Gupta } 18605b1d417SRashmica Gupta } 18705b1d417SRashmica Gupta return severity; 188e7efe135SRashmica Gupta } 189e7efe135SRashmica Gupta 19091799dbdSTao Lin void parseThresholds(Json& thresholds, const PropertyMap& propertyMap, 19191799dbdSTao Lin const std::string& entityInterface = "") 192e7efe135SRashmica Gupta { 193e7efe135SRashmica Gupta std::string direction; 194e7efe135SRashmica Gupta 195e7efe135SRashmica Gupta auto value = getNumberFromConfig<double>(propertyMap, "Value", true); 196e7efe135SRashmica Gupta 19705b1d417SRashmica Gupta auto severity = getSeverityField(propertyMap); 19805b1d417SRashmica Gupta 19905b1d417SRashmica Gupta if (auto itr = propertyMap.find("Direction"); itr != propertyMap.end()) 200e7efe135SRashmica Gupta { 201e7efe135SRashmica Gupta direction = std::get<std::string>(itr->second); 202e7efe135SRashmica Gupta } 203e7efe135SRashmica Gupta 204e7efe135SRashmica Gupta auto threshold = getThresholdType(direction, severity); 205e7efe135SRashmica Gupta thresholds[threshold] = value; 2061dff7dceSRashmica Gupta 207*1226f208SPatrick Williams auto hysteresis = getNumberFromConfig<double>(propertyMap, "Hysteresis", 208*1226f208SPatrick Williams false); 2091dff7dceSRashmica Gupta if (hysteresis != std::numeric_limits<double>::quiet_NaN()) 2101dff7dceSRashmica Gupta { 2111dff7dceSRashmica Gupta thresholds[threshold + "Hysteresis"] = hysteresis; 2121dff7dceSRashmica Gupta } 21391799dbdSTao Lin 21491799dbdSTao Lin if (!entityInterface.empty()) 21591799dbdSTao Lin { 21691799dbdSTao Lin thresholds[threshold + "Direction"] = entityInterface; 21791799dbdSTao Lin } 218e7efe135SRashmica Gupta } 219e7efe135SRashmica Gupta 220e7efe135SRashmica Gupta void VirtualSensor::parseConfigInterface(const PropertyMap& propertyMap, 221e7efe135SRashmica Gupta const std::string& sensorType, 222e7efe135SRashmica Gupta const std::string& interface) 223e7efe135SRashmica Gupta { 224e7efe135SRashmica Gupta /* Parse sensors / DBus params */ 225e7efe135SRashmica Gupta if (auto itr = propertyMap.find("Sensors"); itr != propertyMap.end()) 226e7efe135SRashmica Gupta { 227e7efe135SRashmica Gupta auto sensors = std::get<std::vector<std::string>>(itr->second); 228e7efe135SRashmica Gupta for (auto sensor : sensors) 229e7efe135SRashmica Gupta { 230e7efe135SRashmica Gupta std::replace(sensor.begin(), sensor.end(), ' ', '_'); 231e7efe135SRashmica Gupta auto sensorObjPath = sensorDbusPath + sensorType + "/" + sensor; 232e7efe135SRashmica Gupta 233*1226f208SPatrick Williams auto paramPtr = std::make_unique<SensorParam>(bus, sensorObjPath, 234*1226f208SPatrick Williams this); 235e7efe135SRashmica Gupta symbols.create_variable(sensor); 236e7efe135SRashmica Gupta paramMap.emplace(std::move(sensor), std::move(paramPtr)); 237e7efe135SRashmica Gupta } 238e7efe135SRashmica Gupta } 239e7efe135SRashmica Gupta /* Get expression string */ 240e7efe135SRashmica Gupta if (!isCalculationType(interface)) 241e7efe135SRashmica Gupta { 242e7efe135SRashmica Gupta throw std::invalid_argument("Invalid expression in interface"); 243e7efe135SRashmica Gupta } 244e7efe135SRashmica Gupta exprStr = interface; 245e7efe135SRashmica Gupta 246e7efe135SRashmica Gupta /* Get optional min and max input and output values */ 247e7efe135SRashmica Gupta ValueIface::maxValue( 248e7efe135SRashmica Gupta getNumberFromConfig<double>(propertyMap, "MaxValue", false)); 249e7efe135SRashmica Gupta ValueIface::minValue( 250e7efe135SRashmica Gupta getNumberFromConfig<double>(propertyMap, "MinValue", false)); 251e7efe135SRashmica Gupta maxValidInput = 252190f6d06SJiaqing Zhao getNumberFromConfig<double>(propertyMap, "MaxValidInput", false, 253190f6d06SJiaqing Zhao std::numeric_limits<double>::infinity()); 254e7efe135SRashmica Gupta minValidInput = 255190f6d06SJiaqing Zhao getNumberFromConfig<double>(propertyMap, "MinValidInput", false, 256190f6d06SJiaqing Zhao -std::numeric_limits<double>::infinity()); 257e7efe135SRashmica Gupta } 258e7efe135SRashmica Gupta 259ce675228SMatt Spinler void VirtualSensor::initVirtualSensor(const Json& sensorConfig, 260ce675228SMatt Spinler const std::string& objPath) 261abcc94faSVijay Khemka { 262abcc94faSVijay Khemka static const Json empty{}; 263abcc94faSVijay Khemka 264abcc94faSVijay Khemka /* Get threshold values if defined in config */ 265abcc94faSVijay Khemka auto threshold = sensorConfig.value("Threshold", empty); 266f15189e3SMatt Spinler 2673e99919bSRashmica Gupta createThresholds(threshold, objPath); 268abcc94faSVijay Khemka 269f6443742SHarvey Wu /* Get MaxValue, MinValue setting if defined in config */ 270f6443742SHarvey Wu auto confDesc = sensorConfig.value("Desc", empty); 271f6443742SHarvey Wu if (auto maxConf = confDesc.find("MaxValue"); 272f6443742SHarvey Wu maxConf != confDesc.end() && maxConf->is_number()) 273f6443742SHarvey Wu { 274f6443742SHarvey Wu ValueIface::maxValue(maxConf->get<double>()); 275f6443742SHarvey Wu } 276f6443742SHarvey Wu if (auto minConf = confDesc.find("MinValue"); 277f6443742SHarvey Wu minConf != confDesc.end() && minConf->is_number()) 278f6443742SHarvey Wu { 279f6443742SHarvey Wu ValueIface::minValue(minConf->get<double>()); 280f6443742SHarvey Wu } 281f6443742SHarvey Wu 2820fcf0e1cSLei YU /* Get optional association */ 2830fcf0e1cSLei YU auto assocJson = sensorConfig.value("Associations", empty); 2840fcf0e1cSLei YU if (!assocJson.empty()) 2850fcf0e1cSLei YU { 2860fcf0e1cSLei YU auto assocs = getAssociationsFromJson(assocJson); 2870fcf0e1cSLei YU if (!assocs.empty()) 2880fcf0e1cSLei YU { 2890fcf0e1cSLei YU associationIface = 2900fcf0e1cSLei YU std::make_unique<AssociationObject>(bus, objPath.c_str()); 2910fcf0e1cSLei YU associationIface->associations(assocs); 2920fcf0e1cSLei YU } 2930fcf0e1cSLei YU } 2940fcf0e1cSLei YU 295abcc94faSVijay Khemka /* Get expression string */ 29603c4c8e2SPatrick Williams static constexpr auto exprKey = "Expression"; 29703c4c8e2SPatrick Williams if (sensorConfig.contains(exprKey)) 29803c4c8e2SPatrick Williams { 299a959678cSPatrick Williams auto& ref = sensorConfig.at(exprKey); 30003c4c8e2SPatrick Williams if (ref.is_array()) 30103c4c8e2SPatrick Williams { 30203c4c8e2SPatrick Williams exprStr = std::string{}; 30303c4c8e2SPatrick Williams for (auto& s : ref) 30403c4c8e2SPatrick Williams { 30503c4c8e2SPatrick Williams exprStr += s; 30603c4c8e2SPatrick Williams } 30703c4c8e2SPatrick Williams } 30803c4c8e2SPatrick Williams else if (ref.is_string()) 30903c4c8e2SPatrick Williams { 31003c4c8e2SPatrick Williams exprStr = std::string{ref}; 31103c4c8e2SPatrick Williams } 31203c4c8e2SPatrick Williams } 313abcc94faSVijay Khemka 314abcc94faSVijay Khemka /* Get all the parameter listed in configuration */ 315abcc94faSVijay Khemka auto params = sensorConfig.value("Params", empty); 316abcc94faSVijay Khemka 317abcc94faSVijay Khemka /* Check for constant parameter */ 318abcc94faSVijay Khemka const auto& consParams = params.value("ConstParam", empty); 319abcc94faSVijay Khemka if (!consParams.empty()) 320abcc94faSVijay Khemka { 321abcc94faSVijay Khemka for (auto& j : consParams) 322abcc94faSVijay Khemka { 323abcc94faSVijay Khemka if (j.find("ParamName") != j.end()) 324abcc94faSVijay Khemka { 325abcc94faSVijay Khemka auto paramPtr = std::make_unique<SensorParam>(j["Value"]); 3263ed9a516SVijay Khemka std::string name = j["ParamName"]; 3273ed9a516SVijay Khemka symbols.create_variable(name); 3283ed9a516SVijay Khemka paramMap.emplace(std::move(name), std::move(paramPtr)); 329abcc94faSVijay Khemka } 330abcc94faSVijay Khemka else 331abcc94faSVijay Khemka { 332abcc94faSVijay Khemka /* Invalid configuration */ 333abcc94faSVijay Khemka throw std::invalid_argument( 334abcc94faSVijay Khemka "ParamName not found in configuration"); 335abcc94faSVijay Khemka } 336abcc94faSVijay Khemka } 337abcc94faSVijay Khemka } 338abcc94faSVijay Khemka 3397452a867SVijay Khemka /* Check for dbus parameter */ 3407452a867SVijay Khemka auto dbusParams = params.value("DbusParam", empty); 3417452a867SVijay Khemka if (!dbusParams.empty()) 3427452a867SVijay Khemka { 3437452a867SVijay Khemka for (auto& j : dbusParams) 3447452a867SVijay Khemka { 3457452a867SVijay Khemka /* Get parameter dbus sensor descriptor */ 3467452a867SVijay Khemka auto desc = j.value("Desc", empty); 3477452a867SVijay Khemka if ((!desc.empty()) && (j.find("ParamName") != j.end())) 3487452a867SVijay Khemka { 3497452a867SVijay Khemka std::string sensorType = desc.value("SensorType", ""); 3507452a867SVijay Khemka std::string name = desc.value("Name", ""); 3517452a867SVijay Khemka 3527452a867SVijay Khemka if (!sensorType.empty() && !name.empty()) 3537452a867SVijay Khemka { 3541204b433SGeorge Liu auto path = sensorDbusPath + sensorType + "/" + name; 3557452a867SVijay Khemka 356*1226f208SPatrick Williams auto paramPtr = std::make_unique<SensorParam>(bus, path, 357*1226f208SPatrick Williams this); 3581204b433SGeorge Liu std::string paramName = j["ParamName"]; 3591204b433SGeorge Liu symbols.create_variable(paramName); 3601204b433SGeorge Liu paramMap.emplace(std::move(paramName), std::move(paramPtr)); 3617452a867SVijay Khemka } 3627452a867SVijay Khemka } 3637452a867SVijay Khemka } 3647452a867SVijay Khemka } 365abcc94faSVijay Khemka 3663ed9a516SVijay Khemka symbols.add_constants(); 3679f1ef4f5SMatt Spinler symbols.add_package(vecopsPackage); 3683ed9a516SVijay Khemka expression.register_symbol_table(symbols); 3693ed9a516SVijay Khemka 3703ed9a516SVijay Khemka /* parser from exprtk */ 3713ed9a516SVijay Khemka exprtk::parser<double> parser{}; 372ddc6dcd6SMatt Spinler if (!parser.compile(exprStr, expression)) 373ddc6dcd6SMatt Spinler { 37482b39c66SPatrick Williams error("Expression compilation failed"); 375ddc6dcd6SMatt Spinler 376ddc6dcd6SMatt Spinler for (std::size_t i = 0; i < parser.error_count(); ++i) 377ddc6dcd6SMatt Spinler { 37882b39c66SPatrick Williams auto err = parser.get_error(i); 37982b39c66SPatrick Williams error("Error parsing token at {POSITION}: {ERROR}", "POSITION", 38082b39c66SPatrick Williams err.token.position, "TYPE", 38182b39c66SPatrick Williams exprtk::parser_error::to_str(err.mode), "ERROR", 38282b39c66SPatrick Williams err.diagnostic); 383ddc6dcd6SMatt Spinler } 384ddc6dcd6SMatt Spinler throw std::runtime_error("Expression compilation failed"); 385ddc6dcd6SMatt Spinler } 3863ed9a516SVijay Khemka 387abcc94faSVijay Khemka /* Print all parameters for debug purpose only */ 388abcc94faSVijay Khemka if (DEBUG) 389abcc94faSVijay Khemka printParams(paramMap); 390abcc94faSVijay Khemka } 391abcc94faSVijay Khemka 392dc777015STao Lin void VirtualSensor::createAssociation(const std::string& objPath, 393dc777015STao Lin const std::string& entityPath) 394dc777015STao Lin { 395dc777015STao Lin if (objPath.empty() || entityPath.empty()) 396dc777015STao Lin { 397dc777015STao Lin return; 398dc777015STao Lin } 399dc777015STao Lin 400dc777015STao Lin std::filesystem::path p(entityPath); 401dc777015STao Lin auto assocsDbus = 402dc777015STao Lin AssociationList{{"chassis", "all_sensors", p.parent_path().string()}}; 403*1226f208SPatrick Williams associationIface = std::make_unique<AssociationObject>(bus, 404*1226f208SPatrick Williams objPath.c_str()); 405dc777015STao Lin associationIface->associations(assocsDbus); 406dc777015STao Lin } 407dc777015STao Lin 408e7efe135SRashmica Gupta void VirtualSensor::initVirtualSensor(const InterfaceMap& interfaceMap, 409e7efe135SRashmica Gupta const std::string& objPath, 410e7efe135SRashmica Gupta const std::string& sensorType, 411e7efe135SRashmica Gupta const std::string& calculationIface) 412e7efe135SRashmica Gupta { 413e7efe135SRashmica Gupta Json thresholds; 414*1226f208SPatrick Williams const std::string vsThresholdsIntf = calculationIface + 415*1226f208SPatrick Williams vsThresholdsIfaceSuffix; 416e7efe135SRashmica Gupta 417e7efe135SRashmica Gupta for (const auto& [interface, propertyMap] : interfaceMap) 418e7efe135SRashmica Gupta { 419e7efe135SRashmica Gupta /* Each threshold is on it's own interface with a number as a suffix 420e7efe135SRashmica Gupta * eg xyz.openbmc_project.Configuration.ModifiedMedian.Thresholds1 */ 421e7efe135SRashmica Gupta if (interface.find(vsThresholdsIntf) != std::string::npos) 422e7efe135SRashmica Gupta { 42391799dbdSTao Lin parseThresholds(thresholds, propertyMap, interface); 424e7efe135SRashmica Gupta } 425e7efe135SRashmica Gupta else if (interface == calculationIface) 426e7efe135SRashmica Gupta { 427e7efe135SRashmica Gupta parseConfigInterface(propertyMap, sensorType, interface); 428e7efe135SRashmica Gupta } 429e7efe135SRashmica Gupta } 430e7efe135SRashmica Gupta 431e7efe135SRashmica Gupta createThresholds(thresholds, objPath); 432e7efe135SRashmica Gupta symbols.add_constants(); 433e7efe135SRashmica Gupta symbols.add_package(vecopsPackage); 434e7efe135SRashmica Gupta expression.register_symbol_table(symbols); 435e7efe135SRashmica Gupta 436dc777015STao Lin createAssociation(objPath, entityPath); 437e7efe135SRashmica Gupta /* Print all parameters for debug purpose only */ 438e7efe135SRashmica Gupta if (DEBUG) 439e7efe135SRashmica Gupta { 440e7efe135SRashmica Gupta printParams(paramMap); 441e7efe135SRashmica Gupta } 442e7efe135SRashmica Gupta } 443e7efe135SRashmica Gupta 444abcc94faSVijay Khemka void VirtualSensor::setSensorValue(double value) 445abcc94faSVijay Khemka { 446543bf668SPatrick Williams value = std::clamp(value, ValueIface::minValue(), ValueIface::maxValue()); 447abcc94faSVijay Khemka ValueIface::value(value); 448abcc94faSVijay Khemka } 449abcc94faSVijay Khemka 450304fd0e4SRashmica Gupta double VirtualSensor::calculateValue(const std::string& calculation, 451304fd0e4SRashmica Gupta const VirtualSensor::ParamMap& paramMap) 452e7efe135SRashmica Gupta { 453304fd0e4SRashmica Gupta auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(), 454304fd0e4SRashmica Gupta calculation); 455304fd0e4SRashmica Gupta if (itr == calculationIfaces.end()) 456304fd0e4SRashmica Gupta { 457e7efe135SRashmica Gupta return std::numeric_limits<double>::quiet_NaN(); 458e7efe135SRashmica Gupta } 459304fd0e4SRashmica Gupta else if (calculation == "xyz.openbmc_project.Configuration.ModifiedMedian") 460304fd0e4SRashmica Gupta { 461304fd0e4SRashmica Gupta return calculateModifiedMedianValue(paramMap); 462304fd0e4SRashmica Gupta } 463f6b7e0a4STao Lin else if (calculation == "xyz.openbmc_project.Configuration.Maximum") 464f6b7e0a4STao Lin { 465f6b7e0a4STao Lin return calculateMaximumValue(paramMap); 466f6b7e0a4STao Lin } 467304fd0e4SRashmica Gupta return std::numeric_limits<double>::quiet_NaN(); 468304fd0e4SRashmica Gupta } 469304fd0e4SRashmica Gupta 470304fd0e4SRashmica Gupta bool VirtualSensor::sensorInRange(double value) 471304fd0e4SRashmica Gupta { 472304fd0e4SRashmica Gupta if (value <= this->maxValidInput && value >= this->minValidInput) 473304fd0e4SRashmica Gupta { 474304fd0e4SRashmica Gupta return true; 475304fd0e4SRashmica Gupta } 476304fd0e4SRashmica Gupta return false; 477304fd0e4SRashmica Gupta } 478e7efe135SRashmica Gupta 479abcc94faSVijay Khemka void VirtualSensor::updateVirtualSensor() 4803ed9a516SVijay Khemka { 4813ed9a516SVijay Khemka for (auto& param : paramMap) 4823ed9a516SVijay Khemka { 4833ed9a516SVijay Khemka auto& name = param.first; 4843ed9a516SVijay Khemka auto& data = param.second; 4853ed9a516SVijay Khemka if (auto var = symbols.get_variable(name)) 4863ed9a516SVijay Khemka { 4873ed9a516SVijay Khemka var->ref() = data->getParamValue(); 4883ed9a516SVijay Khemka } 4893ed9a516SVijay Khemka else 4903ed9a516SVijay Khemka { 4913ed9a516SVijay Khemka /* Invalid parameter */ 4923ed9a516SVijay Khemka throw std::invalid_argument("ParamName not found in symbols"); 4933ed9a516SVijay Khemka } 4943ed9a516SVijay Khemka } 495*1226f208SPatrick Williams auto itr = std::find(calculationIfaces.begin(), calculationIfaces.end(), 496*1226f208SPatrick Williams exprStr); 497304fd0e4SRashmica Gupta auto val = (itr == calculationIfaces.end()) 498304fd0e4SRashmica Gupta ? expression.value() 499304fd0e4SRashmica Gupta : calculateValue(exprStr, paramMap); 50032a7156bSVijay Khemka 50132a7156bSVijay Khemka /* Set sensor value to dbus interface */ 5023ed9a516SVijay Khemka setSensorValue(val); 50332a7156bSVijay Khemka 5043ed9a516SVijay Khemka if (DEBUG) 505e7efe135SRashmica Gupta { 506fbd7145eSPatrick Williams debug("Sensor {NAME} = {VALUE}", "NAME", this->name, "VALUE", val); 507e7efe135SRashmica Gupta } 50832a7156bSVijay Khemka 5098f5e6119SMatt Spinler /* Check sensor thresholds and log required message */ 510b306b03dSMatt Spinler checkThresholds(val, perfLossIface); 511fdb826d5SPatrick Williams checkThresholds(val, warningIface); 512fdb826d5SPatrick Williams checkThresholds(val, criticalIface); 513fdb826d5SPatrick Williams checkThresholds(val, softShutdownIface); 514fdb826d5SPatrick Williams checkThresholds(val, hardShutdownIface); 5153ed9a516SVijay Khemka } 516abcc94faSVijay Khemka 517304fd0e4SRashmica Gupta double VirtualSensor::calculateModifiedMedianValue( 518304fd0e4SRashmica Gupta const VirtualSensor::ParamMap& paramMap) 519304fd0e4SRashmica Gupta { 520304fd0e4SRashmica Gupta std::vector<double> values; 521304fd0e4SRashmica Gupta 522304fd0e4SRashmica Gupta for (auto& param : paramMap) 523304fd0e4SRashmica Gupta { 524304fd0e4SRashmica Gupta auto& name = param.first; 525304fd0e4SRashmica Gupta if (auto var = symbols.get_variable(name)) 526304fd0e4SRashmica Gupta { 527304fd0e4SRashmica Gupta if (!sensorInRange(var->ref())) 528304fd0e4SRashmica Gupta { 529304fd0e4SRashmica Gupta continue; 530304fd0e4SRashmica Gupta } 531304fd0e4SRashmica Gupta values.push_back(var->ref()); 532304fd0e4SRashmica Gupta } 533304fd0e4SRashmica Gupta } 534304fd0e4SRashmica Gupta 535304fd0e4SRashmica Gupta size_t size = values.size(); 536304fd0e4SRashmica Gupta std::sort(values.begin(), values.end()); 537304fd0e4SRashmica Gupta switch (size) 538304fd0e4SRashmica Gupta { 539304fd0e4SRashmica Gupta case 2: 540304fd0e4SRashmica Gupta /* Choose biggest value */ 541304fd0e4SRashmica Gupta return values.at(1); 542304fd0e4SRashmica Gupta case 0: 543304fd0e4SRashmica Gupta return std::numeric_limits<double>::quiet_NaN(); 544304fd0e4SRashmica Gupta default: 545304fd0e4SRashmica Gupta /* Choose median value */ 546304fd0e4SRashmica Gupta if (size % 2 == 0) 547304fd0e4SRashmica Gupta { 548304fd0e4SRashmica Gupta // Average of the two middle values 549304fd0e4SRashmica Gupta return (values.at(size / 2) + values.at(size / 2 - 1)) / 2; 550304fd0e4SRashmica Gupta } 551304fd0e4SRashmica Gupta else 552304fd0e4SRashmica Gupta { 553304fd0e4SRashmica Gupta return values.at((size - 1) / 2); 554304fd0e4SRashmica Gupta } 555304fd0e4SRashmica Gupta } 556304fd0e4SRashmica Gupta } 557304fd0e4SRashmica Gupta 558f6b7e0a4STao Lin double VirtualSensor::calculateMaximumValue( 559f6b7e0a4STao Lin const VirtualSensor::ParamMap& paramMap) 560f6b7e0a4STao Lin { 561f6b7e0a4STao Lin std::vector<double> values; 562f6b7e0a4STao Lin 563f6b7e0a4STao Lin for (auto& param : paramMap) 564f6b7e0a4STao Lin { 565f6b7e0a4STao Lin auto& name = param.first; 566f6b7e0a4STao Lin if (auto var = symbols.get_variable(name)) 567f6b7e0a4STao Lin { 568f6b7e0a4STao Lin if (!sensorInRange(var->ref())) 569f6b7e0a4STao Lin { 570f6b7e0a4STao Lin continue; 571f6b7e0a4STao Lin } 572f6b7e0a4STao Lin values.push_back(var->ref()); 573f6b7e0a4STao Lin } 574f6b7e0a4STao Lin } 575f6b7e0a4STao Lin auto maxIt = std::max_element(values.begin(), values.end()); 576f6b7e0a4STao Lin if (maxIt == values.end()) 577f6b7e0a4STao Lin { 578f6b7e0a4STao Lin return std::numeric_limits<double>::quiet_NaN(); 579f6b7e0a4STao Lin } 580f6b7e0a4STao Lin return *maxIt; 581f6b7e0a4STao Lin } 582f6b7e0a4STao Lin 5833e99919bSRashmica Gupta void VirtualSensor::createThresholds(const Json& threshold, 5843e99919bSRashmica Gupta const std::string& objPath) 5853e99919bSRashmica Gupta { 5863e99919bSRashmica Gupta if (threshold.empty()) 5873e99919bSRashmica Gupta { 5883e99919bSRashmica Gupta return; 5893e99919bSRashmica Gupta } 5903e99919bSRashmica Gupta // Only create the threshold interfaces if 5913e99919bSRashmica Gupta // at least one of their values is present. 5923e99919bSRashmica Gupta if (threshold.contains("CriticalHigh") || threshold.contains("CriticalLow")) 5933e99919bSRashmica Gupta { 5943e99919bSRashmica Gupta criticalIface = 5953e99919bSRashmica Gupta std::make_unique<Threshold<CriticalObject>>(bus, objPath.c_str()); 5963e99919bSRashmica Gupta 59791799dbdSTao Lin if (threshold.contains("CriticalHigh")) 59891799dbdSTao Lin { 59991799dbdSTao Lin criticalIface->setEntityInterfaceHigh( 60091799dbdSTao Lin threshold.value("CriticalHighDirection", "")); 60191799dbdSTao Lin if (DEBUG) 60291799dbdSTao Lin { 60391799dbdSTao Lin debug("Sensor Threshold:{NAME} = intf:{INTF}", "NAME", objPath, 60491799dbdSTao Lin "INTF", threshold.value("CriticalHighDirection", "")); 60591799dbdSTao Lin } 60691799dbdSTao Lin } 60791799dbdSTao Lin if (threshold.contains("CriticalLow")) 60891799dbdSTao Lin { 60991799dbdSTao Lin criticalIface->setEntityInterfaceLow( 61091799dbdSTao Lin threshold.value("CriticalLowDirection", "")); 61191799dbdSTao Lin if (DEBUG) 61291799dbdSTao Lin { 61391799dbdSTao Lin debug("Sensor Threshold:{NAME} = intf:{INTF}", "NAME", objPath, 61491799dbdSTao Lin "INTF", threshold.value("CriticalLowDirection", "")); 61591799dbdSTao Lin } 61691799dbdSTao Lin } 61791799dbdSTao Lin 61891799dbdSTao Lin criticalIface->setEntityPath(entityPath); 61991799dbdSTao Lin if (DEBUG) 62091799dbdSTao Lin { 62191799dbdSTao Lin debug("Sensor Threshold:{NAME} = path:{PATH}", "NAME", objPath, 62291799dbdSTao Lin "PATH", entityPath); 62391799dbdSTao Lin } 624a291ce1aSMatt Spinler 625a291ce1aSMatt Spinler criticalIface->criticalHigh(threshold.value( 626a291ce1aSMatt Spinler "CriticalHigh", std::numeric_limits<double>::quiet_NaN())); 627a291ce1aSMatt Spinler criticalIface->criticalLow(threshold.value( 628a291ce1aSMatt Spinler "CriticalLow", std::numeric_limits<double>::quiet_NaN())); 629a291ce1aSMatt Spinler criticalIface->setHighHysteresis( 630a291ce1aSMatt Spinler threshold.value("CriticalHighHysteresis", defaultHysteresis)); 631a291ce1aSMatt Spinler criticalIface->setLowHysteresis( 632a291ce1aSMatt Spinler threshold.value("CriticalLowHysteresis", defaultHysteresis)); 6333e99919bSRashmica Gupta } 6343e99919bSRashmica Gupta 6353e99919bSRashmica Gupta if (threshold.contains("WarningHigh") || threshold.contains("WarningLow")) 6363e99919bSRashmica Gupta { 6373e99919bSRashmica Gupta warningIface = 6383e99919bSRashmica Gupta std::make_unique<Threshold<WarningObject>>(bus, objPath.c_str()); 6393e99919bSRashmica Gupta 64091799dbdSTao Lin if (threshold.contains("WarningHigh")) 64191799dbdSTao Lin { 64291799dbdSTao Lin warningIface->setEntityInterfaceHigh( 64391799dbdSTao Lin threshold.value("WarningHighDirection", "")); 64491799dbdSTao Lin if (DEBUG) 64591799dbdSTao Lin { 64691799dbdSTao Lin debug("Sensor Threshold:{NAME} = intf:{INTF}", "NAME", objPath, 64791799dbdSTao Lin "INTF", threshold.value("WarningHighDirection", "")); 64891799dbdSTao Lin } 64991799dbdSTao Lin } 65091799dbdSTao Lin if (threshold.contains("WarningLow")) 65191799dbdSTao Lin { 65291799dbdSTao Lin warningIface->setEntityInterfaceLow( 65391799dbdSTao Lin threshold.value("WarningLowDirection", "")); 65491799dbdSTao Lin if (DEBUG) 65591799dbdSTao Lin { 65691799dbdSTao Lin debug("Sensor Threshold:{NAME} = intf:{INTF}", "NAME", objPath, 65791799dbdSTao Lin "INTF", threshold.value("WarningLowDirection", "")); 65891799dbdSTao Lin } 65991799dbdSTao Lin } 66091799dbdSTao Lin 66191799dbdSTao Lin warningIface->setEntityPath(entityPath); 66291799dbdSTao Lin if (DEBUG) 66391799dbdSTao Lin { 66491799dbdSTao Lin debug("Sensor Threshold:{NAME} = path:{PATH}", "NAME", objPath, 66591799dbdSTao Lin "PATH", entityPath); 66691799dbdSTao Lin } 667a291ce1aSMatt Spinler 668a291ce1aSMatt Spinler warningIface->warningHigh(threshold.value( 669a291ce1aSMatt Spinler "WarningHigh", std::numeric_limits<double>::quiet_NaN())); 670a291ce1aSMatt Spinler warningIface->warningLow(threshold.value( 671a291ce1aSMatt Spinler "WarningLow", std::numeric_limits<double>::quiet_NaN())); 672a291ce1aSMatt Spinler warningIface->setHighHysteresis( 673a291ce1aSMatt Spinler threshold.value("WarningHighHysteresis", defaultHysteresis)); 674a291ce1aSMatt Spinler warningIface->setLowHysteresis( 675a291ce1aSMatt Spinler threshold.value("WarningLowHysteresis", defaultHysteresis)); 6763e99919bSRashmica Gupta } 6773e99919bSRashmica Gupta 6783e99919bSRashmica Gupta if (threshold.contains("HardShutdownHigh") || 6793e99919bSRashmica Gupta threshold.contains("HardShutdownLow")) 6803e99919bSRashmica Gupta { 6813e99919bSRashmica Gupta hardShutdownIface = std::make_unique<Threshold<HardShutdownObject>>( 6823e99919bSRashmica Gupta bus, objPath.c_str()); 6833e99919bSRashmica Gupta 6843e99919bSRashmica Gupta hardShutdownIface->hardShutdownHigh(threshold.value( 6853e99919bSRashmica Gupta "HardShutdownHigh", std::numeric_limits<double>::quiet_NaN())); 6863e99919bSRashmica Gupta hardShutdownIface->hardShutdownLow(threshold.value( 6873e99919bSRashmica Gupta "HardShutdownLow", std::numeric_limits<double>::quiet_NaN())); 6881dff7dceSRashmica Gupta hardShutdownIface->setHighHysteresis( 6891dff7dceSRashmica Gupta threshold.value("HardShutdownHighHysteresis", defaultHysteresis)); 6901dff7dceSRashmica Gupta hardShutdownIface->setLowHysteresis( 6911dff7dceSRashmica Gupta threshold.value("HardShutdownLowHysteresis", defaultHysteresis)); 6923e99919bSRashmica Gupta } 6933e99919bSRashmica Gupta 6943e99919bSRashmica Gupta if (threshold.contains("SoftShutdownHigh") || 6953e99919bSRashmica Gupta threshold.contains("SoftShutdownLow")) 6963e99919bSRashmica Gupta { 6973e99919bSRashmica Gupta softShutdownIface = std::make_unique<Threshold<SoftShutdownObject>>( 6983e99919bSRashmica Gupta bus, objPath.c_str()); 6993e99919bSRashmica Gupta 7003e99919bSRashmica Gupta softShutdownIface->softShutdownHigh(threshold.value( 7013e99919bSRashmica Gupta "SoftShutdownHigh", std::numeric_limits<double>::quiet_NaN())); 7023e99919bSRashmica Gupta softShutdownIface->softShutdownLow(threshold.value( 7033e99919bSRashmica Gupta "SoftShutdownLow", std::numeric_limits<double>::quiet_NaN())); 7041dff7dceSRashmica Gupta softShutdownIface->setHighHysteresis( 7051dff7dceSRashmica Gupta threshold.value("SoftShutdownHighHysteresis", defaultHysteresis)); 7061dff7dceSRashmica Gupta softShutdownIface->setLowHysteresis( 7071dff7dceSRashmica Gupta threshold.value("SoftShutdownLowHysteresis", defaultHysteresis)); 7083e99919bSRashmica Gupta } 7093e99919bSRashmica Gupta 7103e99919bSRashmica Gupta if (threshold.contains("PerformanceLossHigh") || 7113e99919bSRashmica Gupta threshold.contains("PerformanceLossLow")) 7123e99919bSRashmica Gupta { 7133e99919bSRashmica Gupta perfLossIface = std::make_unique<Threshold<PerformanceLossObject>>( 7143e99919bSRashmica Gupta bus, objPath.c_str()); 7153e99919bSRashmica Gupta 7163e99919bSRashmica Gupta perfLossIface->performanceLossHigh(threshold.value( 7173e99919bSRashmica Gupta "PerformanceLossHigh", std::numeric_limits<double>::quiet_NaN())); 7183e99919bSRashmica Gupta perfLossIface->performanceLossLow(threshold.value( 7193e99919bSRashmica Gupta "PerformanceLossLow", std::numeric_limits<double>::quiet_NaN())); 7201dff7dceSRashmica Gupta perfLossIface->setHighHysteresis(threshold.value( 7211dff7dceSRashmica Gupta "PerformanceLossHighHysteresis", defaultHysteresis)); 7221dff7dceSRashmica Gupta perfLossIface->setLowHysteresis( 7231dff7dceSRashmica Gupta threshold.value("PerformanceLossLowHysteresis", defaultHysteresis)); 7243e99919bSRashmica Gupta } 7253e99919bSRashmica Gupta } 7263e99919bSRashmica Gupta 727e7efe135SRashmica Gupta ManagedObjectType VirtualSensors::getObjectsFromDBus() 728e7efe135SRashmica Gupta { 729e7efe135SRashmica Gupta ManagedObjectType objects; 730e7efe135SRashmica Gupta 731e7efe135SRashmica Gupta try 732e7efe135SRashmica Gupta { 733f6825b91SNan Zhou auto method = bus.new_method_call("xyz.openbmc_project.EntityManager", 734f6825b91SNan Zhou "/xyz/openbmc_project/inventory", 735e7efe135SRashmica Gupta "org.freedesktop.DBus.ObjectManager", 736e7efe135SRashmica Gupta "GetManagedObjects"); 737e7efe135SRashmica Gupta auto reply = bus.call(method); 738e7efe135SRashmica Gupta reply.read(objects); 739e7efe135SRashmica Gupta } 7408e11cccbSPatrick Williams catch (const sdbusplus::exception_t& ex) 741e7efe135SRashmica Gupta { 742e7efe135SRashmica Gupta // If entity manager isn't running yet, keep going. 743e7efe135SRashmica Gupta if (std::string("org.freedesktop.DBus.Error.ServiceUnknown") != 744e7efe135SRashmica Gupta ex.name()) 745e7efe135SRashmica Gupta { 74671b9c116SMatt Spinler error("Could not reach entity-manager: {ERROR}", "ERROR", ex); 74771b9c116SMatt Spinler throw; 748e7efe135SRashmica Gupta } 749e7efe135SRashmica Gupta } 750e7efe135SRashmica Gupta 751e7efe135SRashmica Gupta return objects; 752e7efe135SRashmica Gupta } 753e7efe135SRashmica Gupta 7548e11cccbSPatrick Williams void VirtualSensors::propertiesChanged(sdbusplus::message_t& msg) 755e7efe135SRashmica Gupta { 756e7efe135SRashmica Gupta std::string path; 757e7efe135SRashmica Gupta PropertyMap properties; 758e7efe135SRashmica Gupta 759e7efe135SRashmica Gupta msg.read(path, properties); 760e7efe135SRashmica Gupta 761e7efe135SRashmica Gupta /* We get multiple callbacks for one sensor. 'Type' is a required field and 762e7efe135SRashmica Gupta * is a unique label so use to to only proceed once per sensor */ 763e7efe135SRashmica Gupta if (properties.contains("Type")) 764e7efe135SRashmica Gupta { 765e7efe135SRashmica Gupta if (isCalculationType(path)) 766e7efe135SRashmica Gupta { 767e7efe135SRashmica Gupta createVirtualSensorsFromDBus(path); 768e7efe135SRashmica Gupta } 769e7efe135SRashmica Gupta } 770e7efe135SRashmica Gupta } 771e7efe135SRashmica Gupta 772abcc94faSVijay Khemka /** @brief Parsing Virtual Sensor config JSON file */ 77332dff21bSPatrick Williams Json VirtualSensors::parseConfigFile() 774abcc94faSVijay Khemka { 77532dff21bSPatrick Williams using path = std::filesystem::path; 77632dff21bSPatrick Williams auto configFile = []() -> path { 77732dff21bSPatrick Williams static constexpr auto name = "virtual_sensor_config.json"; 77832dff21bSPatrick Williams 77932dff21bSPatrick Williams for (auto pathSeg : {std::filesystem::current_path(), 78032dff21bSPatrick Williams path{"/var/lib/phosphor-virtual-sensor"}, 78132dff21bSPatrick Williams path{"/usr/share/phosphor-virtual-sensor"}}) 78232dff21bSPatrick Williams { 78332dff21bSPatrick Williams auto file = pathSeg / name; 78432dff21bSPatrick Williams if (std::filesystem::exists(file)) 78532dff21bSPatrick Williams { 78632dff21bSPatrick Williams return file; 78732dff21bSPatrick Williams } 78832dff21bSPatrick Williams } 78932dff21bSPatrick Williams return name; 79032dff21bSPatrick Williams }(); 79132dff21bSPatrick Williams 792abcc94faSVijay Khemka std::ifstream jsonFile(configFile); 793abcc94faSVijay Khemka if (!jsonFile.is_open()) 794abcc94faSVijay Khemka { 79582b39c66SPatrick Williams error("config JSON file {FILENAME} not found", "FILENAME", configFile); 796e7efe135SRashmica Gupta return {}; 797abcc94faSVijay Khemka } 798abcc94faSVijay Khemka 799abcc94faSVijay Khemka auto data = Json::parse(jsonFile, nullptr, false); 800abcc94faSVijay Khemka if (data.is_discarded()) 801abcc94faSVijay Khemka { 80282b39c66SPatrick Williams error("config readings JSON parser failure with {FILENAME}", "FILENAME", 80382b39c66SPatrick Williams configFile); 804abcc94faSVijay Khemka throw std::exception{}; 805abcc94faSVijay Khemka } 806abcc94faSVijay Khemka 807abcc94faSVijay Khemka return data; 808abcc94faSVijay Khemka } 809abcc94faSVijay Khemka 810e0d371e4SVijay Khemka std::map<std::string, ValueIface::Unit> unitMap = { 811e0d371e4SVijay Khemka {"temperature", ValueIface::Unit::DegreesC}, 812e0d371e4SVijay Khemka {"fan_tach", ValueIface::Unit::RPMS}, 813e0d371e4SVijay Khemka {"voltage", ValueIface::Unit::Volts}, 814e0d371e4SVijay Khemka {"altitude", ValueIface::Unit::Meters}, 815e0d371e4SVijay Khemka {"current", ValueIface::Unit::Amperes}, 816e0d371e4SVijay Khemka {"power", ValueIface::Unit::Watts}, 817e0d371e4SVijay Khemka {"energy", ValueIface::Unit::Joules}, 8182b56ddb3SKumar Thangavel {"utilization", ValueIface::Unit::Percent}, 8194ac7a7f2SRashmica Gupta {"airflow", ValueIface::Unit::CFM}, 8204ac7a7f2SRashmica Gupta {"pressure", ValueIface::Unit::Pascals}}; 821e0d371e4SVijay Khemka 822e7efe135SRashmica Gupta const std::string getSensorTypeFromUnit(const std::string& unit) 823e7efe135SRashmica Gupta { 824e7efe135SRashmica Gupta std::string unitPrefix = "xyz.openbmc_project.Sensor.Value.Unit."; 825e7efe135SRashmica Gupta for (auto [type, unitObj] : unitMap) 826e7efe135SRashmica Gupta { 827e7efe135SRashmica Gupta auto unitPath = ValueIface::convertUnitToString(unitObj); 828e7efe135SRashmica Gupta if (unitPath == (unitPrefix + unit)) 829e7efe135SRashmica Gupta { 830e7efe135SRashmica Gupta return type; 831e7efe135SRashmica Gupta } 832e7efe135SRashmica Gupta } 833e7efe135SRashmica Gupta return ""; 834e7efe135SRashmica Gupta } 835e7efe135SRashmica Gupta 836e7efe135SRashmica Gupta void VirtualSensors::setupMatches() 837e7efe135SRashmica Gupta { 838e7efe135SRashmica Gupta /* Already setup */ 839e7efe135SRashmica Gupta if (!this->matches.empty()) 840e7efe135SRashmica Gupta { 841e7efe135SRashmica Gupta return; 842e7efe135SRashmica Gupta } 843e7efe135SRashmica Gupta 844e7efe135SRashmica Gupta /* Setup matches */ 8458e11cccbSPatrick Williams auto eventHandler = [this](sdbusplus::message_t& message) { 846e7efe135SRashmica Gupta if (message.is_method_error()) 847e7efe135SRashmica Gupta { 84882b39c66SPatrick Williams error("Callback method error"); 849e7efe135SRashmica Gupta return; 850e7efe135SRashmica Gupta } 851e7efe135SRashmica Gupta this->propertiesChanged(message); 852e7efe135SRashmica Gupta }; 853e7efe135SRashmica Gupta 854e7efe135SRashmica Gupta for (const char* iface : calculationIfaces) 855e7efe135SRashmica Gupta { 8568e11cccbSPatrick Williams auto match = std::make_unique<sdbusplus::bus::match_t>( 857e7efe135SRashmica Gupta bus, 858e7efe135SRashmica Gupta sdbusplus::bus::match::rules::propertiesChangedNamespace( 859e7efe135SRashmica Gupta "/xyz/openbmc_project/inventory", iface), 860e7efe135SRashmica Gupta eventHandler); 861e7efe135SRashmica Gupta this->matches.emplace_back(std::move(match)); 862e7efe135SRashmica Gupta } 863e7efe135SRashmica Gupta } 864e7efe135SRashmica Gupta 865e7efe135SRashmica Gupta void VirtualSensors::createVirtualSensorsFromDBus( 866e7efe135SRashmica Gupta const std::string& calculationIface) 867e7efe135SRashmica Gupta { 868e7efe135SRashmica Gupta if (calculationIface.empty()) 869e7efe135SRashmica Gupta { 87082b39c66SPatrick Williams error("No calculation type supplied"); 871e7efe135SRashmica Gupta return; 872e7efe135SRashmica Gupta } 873e7efe135SRashmica Gupta auto objects = getObjectsFromDBus(); 874e7efe135SRashmica Gupta 875e7efe135SRashmica Gupta /* Get virtual sensors config data */ 876e7efe135SRashmica Gupta for (const auto& [path, interfaceMap] : objects) 877e7efe135SRashmica Gupta { 878e7efe135SRashmica Gupta auto objpath = static_cast<std::string>(path); 879e7efe135SRashmica Gupta std::string name = path.filename(); 880e7efe135SRashmica Gupta std::string sensorType, sensorUnit; 881e7efe135SRashmica Gupta 882e7efe135SRashmica Gupta /* Find Virtual Sensor interfaces */ 883e7efe135SRashmica Gupta if (!interfaceMap.contains(calculationIface)) 884e7efe135SRashmica Gupta { 885e7efe135SRashmica Gupta continue; 886e7efe135SRashmica Gupta } 887e7efe135SRashmica Gupta if (name.empty()) 888e7efe135SRashmica Gupta { 88982b39c66SPatrick Williams error("Virtual Sensor name not found in entity manager config"); 890e7efe135SRashmica Gupta continue; 891e7efe135SRashmica Gupta } 892e7efe135SRashmica Gupta if (virtualSensorsMap.contains(name)) 893e7efe135SRashmica Gupta { 89482b39c66SPatrick Williams error("A virtual sensor named {NAME} already exists", "NAME", name); 895e7efe135SRashmica Gupta continue; 896e7efe135SRashmica Gupta } 897e7efe135SRashmica Gupta 898e7efe135SRashmica Gupta /* Extract the virtual sensor type as we need this to initialize the 899e7efe135SRashmica Gupta * sensor */ 900e7efe135SRashmica Gupta for (const auto& [interface, propertyMap] : interfaceMap) 901e7efe135SRashmica Gupta { 902e7efe135SRashmica Gupta if (interface != calculationIface) 903e7efe135SRashmica Gupta { 904e7efe135SRashmica Gupta continue; 905e7efe135SRashmica Gupta } 906e7efe135SRashmica Gupta auto itr = propertyMap.find("Units"); 907e7efe135SRashmica Gupta if (itr != propertyMap.end()) 908e7efe135SRashmica Gupta { 909e7efe135SRashmica Gupta sensorUnit = std::get<std::string>(itr->second); 910e7efe135SRashmica Gupta break; 911e7efe135SRashmica Gupta } 912e7efe135SRashmica Gupta } 913e7efe135SRashmica Gupta sensorType = getSensorTypeFromUnit(sensorUnit); 914e7efe135SRashmica Gupta if (sensorType.empty()) 915e7efe135SRashmica Gupta { 91682b39c66SPatrick Williams error("Sensor unit type {TYPE} is not supported", "TYPE", 91782b39c66SPatrick Williams sensorUnit); 918e7efe135SRashmica Gupta continue; 919e7efe135SRashmica Gupta } 920e7efe135SRashmica Gupta 921e7efe135SRashmica Gupta try 922e7efe135SRashmica Gupta { 923e7efe135SRashmica Gupta auto virtObjPath = sensorDbusPath + sensorType + "/" + name; 924e7efe135SRashmica Gupta 925e7efe135SRashmica Gupta auto virtualSensorPtr = std::make_unique<VirtualSensor>( 926e7efe135SRashmica Gupta bus, virtObjPath.c_str(), interfaceMap, name, sensorType, 927dc777015STao Lin calculationIface, objpath); 92882b39c66SPatrick Williams info("Added a new virtual sensor: {NAME} {TYPE}", "NAME", name, 92982b39c66SPatrick Williams "TYPE", sensorType); 930e7efe135SRashmica Gupta virtualSensorPtr->updateVirtualSensor(); 931e7efe135SRashmica Gupta 932e7efe135SRashmica Gupta /* Initialize unit value for virtual sensor */ 933e7efe135SRashmica Gupta virtualSensorPtr->ValueIface::unit(unitMap[sensorType]); 934e7efe135SRashmica Gupta virtualSensorPtr->emit_object_added(); 935e7efe135SRashmica Gupta 936e7efe135SRashmica Gupta virtualSensorsMap.emplace(name, std::move(virtualSensorPtr)); 937e7efe135SRashmica Gupta 938e7efe135SRashmica Gupta /* Setup match for interfaces removed */ 939*1226f208SPatrick Williams auto intfRemoved = 940*1226f208SPatrick Williams [this, objpath, name](sdbusplus::message_t& message) { 941e7efe135SRashmica Gupta if (!virtualSensorsMap.contains(name)) 942e7efe135SRashmica Gupta { 943e7efe135SRashmica Gupta return; 944e7efe135SRashmica Gupta } 945e7efe135SRashmica Gupta sdbusplus::message::object_path path; 946e7efe135SRashmica Gupta message.read(path); 947e7efe135SRashmica Gupta if (static_cast<const std::string&>(path) == objpath) 948e7efe135SRashmica Gupta { 94982b39c66SPatrick Williams info("Removed a virtual sensor: {NAME}", "NAME", name); 950e7efe135SRashmica Gupta virtualSensorsMap.erase(name); 951e7efe135SRashmica Gupta } 952e7efe135SRashmica Gupta }; 9538e11cccbSPatrick Williams auto matchOnRemove = std::make_unique<sdbusplus::bus::match_t>( 954e7efe135SRashmica Gupta bus, 955e7efe135SRashmica Gupta sdbusplus::bus::match::rules::interfacesRemoved() + 956e7efe135SRashmica Gupta sdbusplus::bus::match::rules::argNpath(0, objpath), 957e7efe135SRashmica Gupta intfRemoved); 958e7efe135SRashmica Gupta /* TODO: slight race condition here. Check that the config still 959e7efe135SRashmica Gupta * exists */ 960e7efe135SRashmica Gupta this->matches.emplace_back(std::move(matchOnRemove)); 961e7efe135SRashmica Gupta } 962dac2663cSPatrick Williams catch (const std::invalid_argument& ia) 963e7efe135SRashmica Gupta { 96482b39c66SPatrick Williams error("Failed to set up virtual sensor: {ERROR}", "ERROR", ia); 965e7efe135SRashmica Gupta } 966e7efe135SRashmica Gupta } 967e7efe135SRashmica Gupta } 968e7efe135SRashmica Gupta 969abcc94faSVijay Khemka void VirtualSensors::createVirtualSensors() 970abcc94faSVijay Khemka { 971abcc94faSVijay Khemka static const Json empty{}; 972abcc94faSVijay Khemka 97332dff21bSPatrick Williams auto data = parseConfigFile(); 974e7efe135SRashmica Gupta 975abcc94faSVijay Khemka // print values 976abcc94faSVijay Khemka if (DEBUG) 977e7efe135SRashmica Gupta { 978fbd7145eSPatrick Williams debug("JSON: {JSON}", "JSON", data.dump()); 979e7efe135SRashmica Gupta } 980abcc94faSVijay Khemka 981abcc94faSVijay Khemka /* Get virtual sensors config data */ 982abcc94faSVijay Khemka for (const auto& j : data) 983abcc94faSVijay Khemka { 984abcc94faSVijay Khemka auto desc = j.value("Desc", empty); 985abcc94faSVijay Khemka if (!desc.empty()) 986abcc94faSVijay Khemka { 987e7efe135SRashmica Gupta if (desc.value("Config", "") == "D-Bus") 988e7efe135SRashmica Gupta { 989e7efe135SRashmica Gupta /* Look on D-Bus for a virtual sensor config. Set up matches 990e7efe135SRashmica Gupta * first because the configs may not be on D-Bus yet and we 991e7efe135SRashmica Gupta * don't want to miss them */ 992e7efe135SRashmica Gupta setupMatches(); 993e7efe135SRashmica Gupta 994e7efe135SRashmica Gupta if (desc.contains("Type")) 995e7efe135SRashmica Gupta { 99682b39c66SPatrick Williams auto type = desc.value("Type", ""); 99782b39c66SPatrick Williams auto path = "xyz.openbmc_project.Configuration." + type; 99882b39c66SPatrick Williams 999e7efe135SRashmica Gupta if (!isCalculationType(path)) 1000e7efe135SRashmica Gupta { 100182b39c66SPatrick Williams error("Invalid calculation type {TYPE} supplied.", 100282b39c66SPatrick Williams "TYPE", type); 1003e7efe135SRashmica Gupta continue; 1004e7efe135SRashmica Gupta } 1005e7efe135SRashmica Gupta createVirtualSensorsFromDBus(path); 1006e7efe135SRashmica Gupta } 1007e7efe135SRashmica Gupta continue; 1008e7efe135SRashmica Gupta } 1009e7efe135SRashmica Gupta 1010abcc94faSVijay Khemka std::string sensorType = desc.value("SensorType", ""); 1011abcc94faSVijay Khemka std::string name = desc.value("Name", ""); 1012665a0a29SRashmica Gupta std::replace(name.begin(), name.end(), ' ', '_'); 1013abcc94faSVijay Khemka 1014abcc94faSVijay Khemka if (!name.empty() && !sensorType.empty()) 1015abcc94faSVijay Khemka { 1016e0d371e4SVijay Khemka if (unitMap.find(sensorType) == unitMap.end()) 1017e0d371e4SVijay Khemka { 101882b39c66SPatrick Williams error("Sensor type {TYPE} is not supported", "TYPE", 101982b39c66SPatrick Williams sensorType); 1020e0d371e4SVijay Khemka } 1021e0d371e4SVijay Khemka else 1022e0d371e4SVijay Khemka { 102367d8b9d2SRashmica Gupta if (virtualSensorsMap.find(name) != virtualSensorsMap.end()) 102467d8b9d2SRashmica Gupta { 102582b39c66SPatrick Williams error("A virtual sensor named {NAME} already exists", 102682b39c66SPatrick Williams "NAME", name); 102767d8b9d2SRashmica Gupta continue; 102867d8b9d2SRashmica Gupta } 1029862c3d1eSRashmica Gupta auto objPath = sensorDbusPath + sensorType + "/" + name; 1030abcc94faSVijay Khemka 103132a7156bSVijay Khemka auto virtualSensorPtr = std::make_unique<VirtualSensor>( 103232a7156bSVijay Khemka bus, objPath.c_str(), j, name); 1033abcc94faSVijay Khemka 103482b39c66SPatrick Williams info("Added a new virtual sensor: {NAME}", "NAME", name); 10353ed9a516SVijay Khemka virtualSensorPtr->updateVirtualSensor(); 1036e0d371e4SVijay Khemka 1037e0d371e4SVijay Khemka /* Initialize unit value for virtual sensor */ 1038e0d371e4SVijay Khemka virtualSensorPtr->ValueIface::unit(unitMap[sensorType]); 1039a2fa63a6SRashmica Gupta virtualSensorPtr->emit_object_added(); 1040e0d371e4SVijay Khemka 10413ed9a516SVijay Khemka virtualSensorsMap.emplace(std::move(name), 10423ed9a516SVijay Khemka std::move(virtualSensorPtr)); 1043abcc94faSVijay Khemka } 1044e0d371e4SVijay Khemka } 1045abcc94faSVijay Khemka else 1046abcc94faSVijay Khemka { 104782b39c66SPatrick Williams error( 104882b39c66SPatrick Williams "Sensor type ({TYPE}) or name ({NAME}) not found in config file", 104982b39c66SPatrick Williams "NAME", name, "TYPE", sensorType); 1050abcc94faSVijay Khemka } 1051abcc94faSVijay Khemka } 1052abcc94faSVijay Khemka else 1053abcc94faSVijay Khemka { 105482b39c66SPatrick Williams error("Descriptor for new virtual sensor not found in config file"); 1055abcc94faSVijay Khemka } 1056abcc94faSVijay Khemka } 1057abcc94faSVijay Khemka } 1058abcc94faSVijay Khemka 1059abcc94faSVijay Khemka } // namespace virtualSensor 1060abcc94faSVijay Khemka } // namespace phosphor 1061abcc94faSVijay Khemka 1062abcc94faSVijay Khemka /** 1063abcc94faSVijay Khemka * @brief Main 1064abcc94faSVijay Khemka */ 1065abcc94faSVijay Khemka int main() 1066abcc94faSVijay Khemka { 1067abcc94faSVijay Khemka // Get a handle to system dbus 1068abcc94faSVijay Khemka auto bus = sdbusplus::bus::new_default(); 1069abcc94faSVijay Khemka 10706c19e7d2SMatt Spinler // Add the ObjectManager interface 1071f7ec40aaSEd Tanous sdbusplus::server::manager_t objManager(bus, 1072f7ec40aaSEd Tanous "/xyz/openbmc_project/sensors"); 10736c19e7d2SMatt Spinler 1074abcc94faSVijay Khemka // Create an virtual sensors object 1075abcc94faSVijay Khemka phosphor::virtualSensor::VirtualSensors virtualSensors(bus); 1076abcc94faSVijay Khemka 1077abcc94faSVijay Khemka // Request service bus name 1078abcc94faSVijay Khemka bus.request_name(busName); 1079abcc94faSVijay Khemka 1080e667239dSPatrick Williams // Run the dbus loop. 1081e667239dSPatrick Williams bus.process_loop(); 1082abcc94faSVijay Khemka 1083abcc94faSVijay Khemka return 0; 1084abcc94faSVijay Khemka } 1085