195e6b3c1SJohn Wang #include "bios_integer_attribute.hpp" 295e6b3c1SJohn Wang 3d130e1a3SDeepak Kodihalli #include "common/utils.hpp" 495e6b3c1SJohn Wang 55079ac4aSBrad Bishop using namespace pldm::utils; 65079ac4aSBrad Bishop 795e6b3c1SJohn Wang namespace pldm 895e6b3c1SJohn Wang { 995e6b3c1SJohn Wang namespace responder 1095e6b3c1SJohn Wang { 1195e6b3c1SJohn Wang namespace bios 1295e6b3c1SJohn Wang { 1395e6b3c1SJohn Wang 1495e6b3c1SJohn Wang BIOSIntegerAttribute::BIOSIntegerAttribute(const Json& entry, 1595e6b3c1SJohn Wang DBusHandler* const dbusHandler) : 1695e6b3c1SJohn Wang BIOSAttribute(entry, dbusHandler) 1795e6b3c1SJohn Wang { 1895e6b3c1SJohn Wang std::string attr = entry.at("attribute_name"); 1995e6b3c1SJohn Wang 2095e6b3c1SJohn Wang integerInfo.lowerBound = entry.at("lower_bound"); 2195e6b3c1SJohn Wang integerInfo.upperBound = entry.at("upper_bound"); 2295e6b3c1SJohn Wang integerInfo.scalarIncrement = entry.at("scalar_increment"); 2395e6b3c1SJohn Wang integerInfo.defaultValue = entry.at("default_value"); 2495e6b3c1SJohn Wang pldm_bios_table_attr_entry_integer_info info = { 2595e6b3c1SJohn Wang 0, 2695e6b3c1SJohn Wang readOnly, 2795e6b3c1SJohn Wang integerInfo.lowerBound, 2895e6b3c1SJohn Wang integerInfo.upperBound, 2995e6b3c1SJohn Wang integerInfo.scalarIncrement, 3095e6b3c1SJohn Wang integerInfo.defaultValue, 3195e6b3c1SJohn Wang }; 3295e6b3c1SJohn Wang const char* errmsg = nullptr; 3395e6b3c1SJohn Wang auto rc = pldm_bios_table_attr_entry_integer_info_check(&info, &errmsg); 3495e6b3c1SJohn Wang if (rc != PLDM_SUCCESS) 3595e6b3c1SJohn Wang { 3695e6b3c1SJohn Wang std::cerr << "Wrong filed for integer attribute, ATTRIBUTE_NAME=" 3795e6b3c1SJohn Wang << attr.c_str() << " ERRMSG=" << errmsg 3895e6b3c1SJohn Wang << " LOWER_BOUND=" << integerInfo.lowerBound 3995e6b3c1SJohn Wang << " UPPER_BOUND=" << integerInfo.upperBound 4095e6b3c1SJohn Wang << " DEFAULT_VALUE=" << integerInfo.defaultValue 4195e6b3c1SJohn Wang << " SCALAR_INCREMENT=" << integerInfo.scalarIncrement 4295e6b3c1SJohn Wang << "\n"; 4395e6b3c1SJohn Wang throw std::invalid_argument("Wrong field for integer attribute"); 4495e6b3c1SJohn Wang } 4595e6b3c1SJohn Wang } 4695e6b3c1SJohn Wang 4795e6b3c1SJohn Wang void BIOSIntegerAttribute::setAttrValueOnDbus( 4895e6b3c1SJohn Wang const pldm_bios_attr_val_table_entry* attrValueEntry, 4995e6b3c1SJohn Wang const pldm_bios_attr_table_entry*, const BIOSStringTable&) 5095e6b3c1SJohn Wang { 51*5bb9edb9SGeorge Liu if (!dBusMap.has_value()) 5295e6b3c1SJohn Wang { 5395e6b3c1SJohn Wang return; 5495e6b3c1SJohn Wang } 5595e6b3c1SJohn Wang auto currentValue = 5695e6b3c1SJohn Wang table::attribute_value::decodeIntegerEntry(attrValueEntry); 5795e6b3c1SJohn Wang 5895e6b3c1SJohn Wang if (dBusMap->propertyType == "uint8_t") 5995e6b3c1SJohn Wang { 6095e6b3c1SJohn Wang return dbusHandler->setDbusProperty(*dBusMap, 6195e6b3c1SJohn Wang static_cast<uint8_t>(currentValue)); 6295e6b3c1SJohn Wang } 6395e6b3c1SJohn Wang else if (dBusMap->propertyType == "uint16_t") 6495e6b3c1SJohn Wang { 6595e6b3c1SJohn Wang return dbusHandler->setDbusProperty( 6695e6b3c1SJohn Wang *dBusMap, static_cast<uint16_t>(currentValue)); 6795e6b3c1SJohn Wang } 6895e6b3c1SJohn Wang else if (dBusMap->propertyType == "int16_t") 6995e6b3c1SJohn Wang { 7095e6b3c1SJohn Wang return dbusHandler->setDbusProperty(*dBusMap, 7195e6b3c1SJohn Wang static_cast<int16_t>(currentValue)); 7295e6b3c1SJohn Wang } 7395e6b3c1SJohn Wang else if (dBusMap->propertyType == "uint32_t") 7495e6b3c1SJohn Wang { 7595e6b3c1SJohn Wang return dbusHandler->setDbusProperty( 7695e6b3c1SJohn Wang *dBusMap, static_cast<uint32_t>(currentValue)); 7795e6b3c1SJohn Wang } 7895e6b3c1SJohn Wang else if (dBusMap->propertyType == "int32_t") 7995e6b3c1SJohn Wang { 8095e6b3c1SJohn Wang return dbusHandler->setDbusProperty(*dBusMap, 8195e6b3c1SJohn Wang static_cast<int32_t>(currentValue)); 8295e6b3c1SJohn Wang } 8395e6b3c1SJohn Wang else if (dBusMap->propertyType == "uint64_t") 8495e6b3c1SJohn Wang { 8595e6b3c1SJohn Wang return dbusHandler->setDbusProperty(*dBusMap, currentValue); 8695e6b3c1SJohn Wang } 8795e6b3c1SJohn Wang else if (dBusMap->propertyType == "int64_t") 8895e6b3c1SJohn Wang { 8995e6b3c1SJohn Wang return dbusHandler->setDbusProperty(*dBusMap, 9095e6b3c1SJohn Wang static_cast<int64_t>(currentValue)); 9195e6b3c1SJohn Wang } 92aca897b7SGeorge Liu else if (dBusMap->propertyType == "double") 93aca897b7SGeorge Liu { 94aca897b7SGeorge Liu return dbusHandler->setDbusProperty(*dBusMap, 95aca897b7SGeorge Liu static_cast<double>(currentValue)); 96aca897b7SGeorge Liu } 9795e6b3c1SJohn Wang 9895e6b3c1SJohn Wang std::cerr << "Unsupported property type on dbus: " << dBusMap->propertyType 9995e6b3c1SJohn Wang << std::endl; 10095e6b3c1SJohn Wang throw std::invalid_argument("dbus type error"); 10195e6b3c1SJohn Wang } 10295e6b3c1SJohn Wang 103ca7b2524STom Joseph void BIOSIntegerAttribute::constructEntry( 104ca7b2524STom Joseph const BIOSStringTable& stringTable, Table& attrTable, Table& attrValueTable, 105ca7b2524STom Joseph std::optional<std::variant<int64_t, std::string>> optAttributeValue) 10695e6b3c1SJohn Wang { 10795e6b3c1SJohn Wang 10895e6b3c1SJohn Wang pldm_bios_table_attr_entry_integer_info info = { 10995e6b3c1SJohn Wang stringTable.findHandle(name), readOnly, 11095e6b3c1SJohn Wang integerInfo.lowerBound, integerInfo.upperBound, 11195e6b3c1SJohn Wang integerInfo.scalarIncrement, integerInfo.defaultValue, 11295e6b3c1SJohn Wang }; 11395e6b3c1SJohn Wang 11495e6b3c1SJohn Wang auto attrTableEntry = 11595e6b3c1SJohn Wang table::attribute::constructIntegerEntry(attrTable, &info); 11695e6b3c1SJohn Wang 11795e6b3c1SJohn Wang auto [attrHandle, attrType, _] = 11895e6b3c1SJohn Wang table::attribute::decodeHeader(attrTableEntry); 11995e6b3c1SJohn Wang 120ca7b2524STom Joseph int64_t currentValue{}; 121ca7b2524STom Joseph if (optAttributeValue.has_value()) 122ca7b2524STom Joseph { 123ca7b2524STom Joseph auto attributeValue = optAttributeValue.value(); 124ca7b2524STom Joseph if (attributeValue.index() == 0) 125ca7b2524STom Joseph { 126ca7b2524STom Joseph currentValue = std::get<int64_t>(attributeValue); 127ca7b2524STom Joseph } 128ca7b2524STom Joseph else 129ca7b2524STom Joseph { 130ca7b2524STom Joseph currentValue = getAttrValue(); 131ca7b2524STom Joseph } 132ca7b2524STom Joseph } 133ca7b2524STom Joseph else 134ca7b2524STom Joseph { 135ca7b2524STom Joseph currentValue = getAttrValue(); 136ca7b2524STom Joseph } 137ca7b2524STom Joseph 13895e6b3c1SJohn Wang table::attribute_value::constructIntegerEntry(attrValueTable, attrHandle, 13995e6b3c1SJohn Wang attrType, currentValue); 14095e6b3c1SJohn Wang } 14195e6b3c1SJohn Wang 14246ece063SSampa Misra uint64_t BIOSIntegerAttribute::getAttrValue(const PropertyValue& propertyValue) 14395e6b3c1SJohn Wang { 144081d03b6SAndrew Geissler uint64_t value = 0; 14595e6b3c1SJohn Wang if (dBusMap->propertyType == "uint8_t") 14695e6b3c1SJohn Wang { 14795e6b3c1SJohn Wang value = std::get<uint8_t>(propertyValue); 14895e6b3c1SJohn Wang } 14995e6b3c1SJohn Wang else if (dBusMap->propertyType == "uint16_t") 15095e6b3c1SJohn Wang { 15195e6b3c1SJohn Wang value = std::get<uint16_t>(propertyValue); 15295e6b3c1SJohn Wang } 15395e6b3c1SJohn Wang else if (dBusMap->propertyType == "int16_t") 15495e6b3c1SJohn Wang { 15595e6b3c1SJohn Wang value = std::get<int16_t>(propertyValue); 15695e6b3c1SJohn Wang } 15795e6b3c1SJohn Wang else if (dBusMap->propertyType == "uint32_t") 15895e6b3c1SJohn Wang { 15995e6b3c1SJohn Wang value = std::get<uint32_t>(propertyValue); 16095e6b3c1SJohn Wang } 16195e6b3c1SJohn Wang else if (dBusMap->propertyType == "int32_t") 16295e6b3c1SJohn Wang { 16395e6b3c1SJohn Wang value = std::get<int32_t>(propertyValue); 16495e6b3c1SJohn Wang } 16595e6b3c1SJohn Wang else if (dBusMap->propertyType == "uint64_t") 16695e6b3c1SJohn Wang { 16795e6b3c1SJohn Wang value = std::get<uint64_t>(propertyValue); 16895e6b3c1SJohn Wang } 16995e6b3c1SJohn Wang else if (dBusMap->propertyType == "int64_t") 17095e6b3c1SJohn Wang { 17195e6b3c1SJohn Wang value = std::get<int64_t>(propertyValue); 17295e6b3c1SJohn Wang } 173aca897b7SGeorge Liu else if (dBusMap->propertyType == "double") 174aca897b7SGeorge Liu { 175aca897b7SGeorge Liu value = std::get<double>(propertyValue); 176aca897b7SGeorge Liu } 177081d03b6SAndrew Geissler else 178081d03b6SAndrew Geissler { 179081d03b6SAndrew Geissler std::cerr << "Unsupported property type for getAttrValue: " 180081d03b6SAndrew Geissler << dBusMap->propertyType << std::endl; 181081d03b6SAndrew Geissler throw std::invalid_argument("dbus type error"); 182081d03b6SAndrew Geissler } 18395e6b3c1SJohn Wang return value; 18495e6b3c1SJohn Wang } 18595e6b3c1SJohn Wang 18695e6b3c1SJohn Wang uint64_t BIOSIntegerAttribute::getAttrValue() 18795e6b3c1SJohn Wang { 188*5bb9edb9SGeorge Liu if (!dBusMap.has_value()) 18995e6b3c1SJohn Wang { 19095e6b3c1SJohn Wang return integerInfo.defaultValue; 19195e6b3c1SJohn Wang } 19295e6b3c1SJohn Wang 19395e6b3c1SJohn Wang try 19495e6b3c1SJohn Wang { 19595e6b3c1SJohn Wang auto propertyValue = dbusHandler->getDbusPropertyVariant( 19695e6b3c1SJohn Wang dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(), 19795e6b3c1SJohn Wang dBusMap->interface.c_str()); 19895e6b3c1SJohn Wang 19995e6b3c1SJohn Wang return getAttrValue(propertyValue); 20095e6b3c1SJohn Wang } 20195e6b3c1SJohn Wang catch (const std::exception& e) 20295e6b3c1SJohn Wang { 20395e6b3c1SJohn Wang std::cerr << "Get Integer Attribute Value Error: AttributeName = " 20495e6b3c1SJohn Wang << name << std::endl; 20595e6b3c1SJohn Wang return integerInfo.defaultValue; 20695e6b3c1SJohn Wang } 20795e6b3c1SJohn Wang } 20895e6b3c1SJohn Wang 20946ece063SSampa Misra int BIOSIntegerAttribute::updateAttrVal(Table& newValue, uint16_t attrHdl, 21046ece063SSampa Misra uint8_t attrType, 21146ece063SSampa Misra const PropertyValue& newPropVal) 21246ece063SSampa Misra { 21346ece063SSampa Misra auto newVal = getAttrValue(newPropVal); 21446ece063SSampa Misra table::attribute_value::constructIntegerEntry(newValue, attrHdl, attrType, 21546ece063SSampa Misra newVal); 21646ece063SSampa Misra return PLDM_SUCCESS; 21746ece063SSampa Misra } 21846ece063SSampa Misra 2191244acfdSGeorge Liu void BIOSIntegerAttribute::generateAttributeEntry( 2201244acfdSGeorge Liu const std::variant<int64_t, std::string>& attributevalue, 2211244acfdSGeorge Liu Table& attrValueEntry) 2221244acfdSGeorge Liu { 2231244acfdSGeorge Liu attrValueEntry.resize(sizeof(pldm_bios_attr_val_table_entry) + 2241244acfdSGeorge Liu sizeof(int64_t) - 1); 2251244acfdSGeorge Liu 2261244acfdSGeorge Liu auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>( 2271244acfdSGeorge Liu attrValueEntry.data()); 2281244acfdSGeorge Liu 2291244acfdSGeorge Liu int64_t value = std::get<int64_t>(attributevalue); 2301244acfdSGeorge Liu entry->attr_type = 3; 2311244acfdSGeorge Liu memcpy(entry->value, &value, sizeof(int64_t)); 2321244acfdSGeorge Liu } 2331244acfdSGeorge Liu 23495e6b3c1SJohn Wang } // namespace bios 23595e6b3c1SJohn Wang } // namespace responder 23695e6b3c1SJohn Wang } // namespace pldm 237