195e6b3c1SJohn Wang #include "bios_integer_attribute.hpp"
295e6b3c1SJohn Wang 
395e6b3c1SJohn Wang #include "utils.hpp"
495e6b3c1SJohn Wang 
595e6b3c1SJohn Wang namespace pldm
695e6b3c1SJohn Wang {
795e6b3c1SJohn Wang namespace responder
895e6b3c1SJohn Wang {
995e6b3c1SJohn Wang namespace bios
1095e6b3c1SJohn Wang {
1195e6b3c1SJohn Wang 
1295e6b3c1SJohn Wang BIOSIntegerAttribute::BIOSIntegerAttribute(const Json& entry,
1395e6b3c1SJohn Wang                                            DBusHandler* const dbusHandler) :
1495e6b3c1SJohn Wang     BIOSAttribute(entry, dbusHandler)
1595e6b3c1SJohn Wang {
1695e6b3c1SJohn Wang     std::string attr = entry.at("attribute_name");
1795e6b3c1SJohn Wang 
1895e6b3c1SJohn Wang     integerInfo.lowerBound = entry.at("lower_bound");
1995e6b3c1SJohn Wang     integerInfo.upperBound = entry.at("upper_bound");
2095e6b3c1SJohn Wang     integerInfo.scalarIncrement = entry.at("scalar_increment");
2195e6b3c1SJohn Wang     integerInfo.defaultValue = entry.at("default_value");
2295e6b3c1SJohn Wang     pldm_bios_table_attr_entry_integer_info info = {
2395e6b3c1SJohn Wang         0,
2495e6b3c1SJohn Wang         readOnly,
2595e6b3c1SJohn Wang         integerInfo.lowerBound,
2695e6b3c1SJohn Wang         integerInfo.upperBound,
2795e6b3c1SJohn Wang         integerInfo.scalarIncrement,
2895e6b3c1SJohn Wang         integerInfo.defaultValue,
2995e6b3c1SJohn Wang     };
3095e6b3c1SJohn Wang     const char* errmsg = nullptr;
3195e6b3c1SJohn Wang     auto rc = pldm_bios_table_attr_entry_integer_info_check(&info, &errmsg);
3295e6b3c1SJohn Wang     if (rc != PLDM_SUCCESS)
3395e6b3c1SJohn Wang     {
3495e6b3c1SJohn Wang         std::cerr << "Wrong filed for integer attribute, ATTRIBUTE_NAME="
3595e6b3c1SJohn Wang                   << attr.c_str() << " ERRMSG=" << errmsg
3695e6b3c1SJohn Wang                   << " LOWER_BOUND=" << integerInfo.lowerBound
3795e6b3c1SJohn Wang                   << " UPPER_BOUND=" << integerInfo.upperBound
3895e6b3c1SJohn Wang                   << " DEFAULT_VALUE=" << integerInfo.defaultValue
3995e6b3c1SJohn Wang                   << " SCALAR_INCREMENT=" << integerInfo.scalarIncrement
4095e6b3c1SJohn Wang                   << "\n";
4195e6b3c1SJohn Wang         throw std::invalid_argument("Wrong field for integer attribute");
4295e6b3c1SJohn Wang     }
4395e6b3c1SJohn Wang }
4495e6b3c1SJohn Wang 
4595e6b3c1SJohn Wang void BIOSIntegerAttribute::setAttrValueOnDbus(
4695e6b3c1SJohn Wang     const pldm_bios_attr_val_table_entry* attrValueEntry,
4795e6b3c1SJohn Wang     const pldm_bios_attr_table_entry*, const BIOSStringTable&)
4895e6b3c1SJohn Wang {
4995e6b3c1SJohn Wang     if (readOnly)
5095e6b3c1SJohn Wang     {
5195e6b3c1SJohn Wang         return;
5295e6b3c1SJohn Wang     }
5395e6b3c1SJohn Wang     auto currentValue =
5495e6b3c1SJohn Wang         table::attribute_value::decodeIntegerEntry(attrValueEntry);
5595e6b3c1SJohn Wang 
5695e6b3c1SJohn Wang     if (dBusMap->propertyType == "uint8_t")
5795e6b3c1SJohn Wang     {
5895e6b3c1SJohn Wang         return dbusHandler->setDbusProperty(*dBusMap,
5995e6b3c1SJohn Wang                                             static_cast<uint8_t>(currentValue));
6095e6b3c1SJohn Wang     }
6195e6b3c1SJohn Wang     else if (dBusMap->propertyType == "uint16_t")
6295e6b3c1SJohn Wang     {
6395e6b3c1SJohn Wang         return dbusHandler->setDbusProperty(
6495e6b3c1SJohn Wang             *dBusMap, static_cast<uint16_t>(currentValue));
6595e6b3c1SJohn Wang     }
6695e6b3c1SJohn Wang     else if (dBusMap->propertyType == "int16_t")
6795e6b3c1SJohn Wang     {
6895e6b3c1SJohn Wang         return dbusHandler->setDbusProperty(*dBusMap,
6995e6b3c1SJohn Wang                                             static_cast<int16_t>(currentValue));
7095e6b3c1SJohn Wang     }
7195e6b3c1SJohn Wang     else if (dBusMap->propertyType == "uint32_t")
7295e6b3c1SJohn Wang     {
7395e6b3c1SJohn Wang         return dbusHandler->setDbusProperty(
7495e6b3c1SJohn Wang             *dBusMap, static_cast<uint32_t>(currentValue));
7595e6b3c1SJohn Wang     }
7695e6b3c1SJohn Wang     else if (dBusMap->propertyType == "int32_t")
7795e6b3c1SJohn Wang     {
7895e6b3c1SJohn Wang         return dbusHandler->setDbusProperty(*dBusMap,
7995e6b3c1SJohn Wang                                             static_cast<int32_t>(currentValue));
8095e6b3c1SJohn Wang     }
8195e6b3c1SJohn Wang     else if (dBusMap->propertyType == "uint64_t")
8295e6b3c1SJohn Wang     {
8395e6b3c1SJohn Wang         return dbusHandler->setDbusProperty(*dBusMap, currentValue);
8495e6b3c1SJohn Wang     }
8595e6b3c1SJohn Wang     else if (dBusMap->propertyType == "int64_t")
8695e6b3c1SJohn Wang     {
8795e6b3c1SJohn Wang         return dbusHandler->setDbusProperty(*dBusMap,
8895e6b3c1SJohn Wang                                             static_cast<int64_t>(currentValue));
8995e6b3c1SJohn Wang     }
9095e6b3c1SJohn Wang 
9195e6b3c1SJohn Wang     std::cerr << "Unsupported property type on dbus: " << dBusMap->propertyType
9295e6b3c1SJohn Wang               << std::endl;
9395e6b3c1SJohn Wang     throw std::invalid_argument("dbus type error");
9495e6b3c1SJohn Wang }
9595e6b3c1SJohn Wang 
9695e6b3c1SJohn Wang void BIOSIntegerAttribute::constructEntry(const BIOSStringTable& stringTable,
9795e6b3c1SJohn Wang                                           Table& attrTable,
9895e6b3c1SJohn Wang                                           Table& attrValueTable)
9995e6b3c1SJohn Wang {
10095e6b3c1SJohn Wang 
10195e6b3c1SJohn Wang     pldm_bios_table_attr_entry_integer_info info = {
10295e6b3c1SJohn Wang         stringTable.findHandle(name), readOnly,
10395e6b3c1SJohn Wang         integerInfo.lowerBound,       integerInfo.upperBound,
10495e6b3c1SJohn Wang         integerInfo.scalarIncrement,  integerInfo.defaultValue,
10595e6b3c1SJohn Wang     };
10695e6b3c1SJohn Wang 
10795e6b3c1SJohn Wang     auto attrTableEntry =
10895e6b3c1SJohn Wang         table::attribute::constructIntegerEntry(attrTable, &info);
10995e6b3c1SJohn Wang 
11095e6b3c1SJohn Wang     auto [attrHandle, attrType, _] =
11195e6b3c1SJohn Wang         table::attribute::decodeHeader(attrTableEntry);
11295e6b3c1SJohn Wang 
11395e6b3c1SJohn Wang     auto currentValue = getAttrValue();
11495e6b3c1SJohn Wang     table::attribute_value::constructIntegerEntry(attrValueTable, attrHandle,
11595e6b3c1SJohn Wang                                                   attrType, currentValue);
11695e6b3c1SJohn Wang }
11795e6b3c1SJohn Wang 
118*46ece063SSampa Misra uint64_t BIOSIntegerAttribute::getAttrValue(const PropertyValue& propertyValue)
11995e6b3c1SJohn Wang {
12095e6b3c1SJohn Wang     uint64_t value;
12195e6b3c1SJohn Wang     if (dBusMap->propertyType == "uint8_t")
12295e6b3c1SJohn Wang     {
12395e6b3c1SJohn Wang         value = std::get<uint8_t>(propertyValue);
12495e6b3c1SJohn Wang     }
12595e6b3c1SJohn Wang     else if (dBusMap->propertyType == "uint16_t")
12695e6b3c1SJohn Wang     {
12795e6b3c1SJohn Wang         value = std::get<uint16_t>(propertyValue);
12895e6b3c1SJohn Wang     }
12995e6b3c1SJohn Wang     else if (dBusMap->propertyType == "int16_t")
13095e6b3c1SJohn Wang     {
13195e6b3c1SJohn Wang         value = std::get<int16_t>(propertyValue);
13295e6b3c1SJohn Wang     }
13395e6b3c1SJohn Wang     else if (dBusMap->propertyType == "uint32_t")
13495e6b3c1SJohn Wang     {
13595e6b3c1SJohn Wang         value = std::get<uint32_t>(propertyValue);
13695e6b3c1SJohn Wang     }
13795e6b3c1SJohn Wang     else if (dBusMap->propertyType == "int32_t")
13895e6b3c1SJohn Wang     {
13995e6b3c1SJohn Wang         value = std::get<int32_t>(propertyValue);
14095e6b3c1SJohn Wang     }
14195e6b3c1SJohn Wang     else if (dBusMap->propertyType == "uint64_t")
14295e6b3c1SJohn Wang     {
14395e6b3c1SJohn Wang         value = std::get<uint64_t>(propertyValue);
14495e6b3c1SJohn Wang     }
14595e6b3c1SJohn Wang     else if (dBusMap->propertyType == "int64_t")
14695e6b3c1SJohn Wang     {
14795e6b3c1SJohn Wang         value = std::get<int64_t>(propertyValue);
14895e6b3c1SJohn Wang     }
14995e6b3c1SJohn Wang     return value;
15095e6b3c1SJohn Wang }
15195e6b3c1SJohn Wang 
15295e6b3c1SJohn Wang uint64_t BIOSIntegerAttribute::getAttrValue()
15395e6b3c1SJohn Wang {
15495e6b3c1SJohn Wang     if (readOnly)
15595e6b3c1SJohn Wang     {
15695e6b3c1SJohn Wang         return integerInfo.defaultValue;
15795e6b3c1SJohn Wang     }
15895e6b3c1SJohn Wang 
15995e6b3c1SJohn Wang     try
16095e6b3c1SJohn Wang     {
16195e6b3c1SJohn Wang         auto propertyValue = dbusHandler->getDbusPropertyVariant(
16295e6b3c1SJohn Wang             dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
16395e6b3c1SJohn Wang             dBusMap->interface.c_str());
16495e6b3c1SJohn Wang 
16595e6b3c1SJohn Wang         return getAttrValue(propertyValue);
16695e6b3c1SJohn Wang     }
16795e6b3c1SJohn Wang     catch (const std::exception& e)
16895e6b3c1SJohn Wang     {
16995e6b3c1SJohn Wang         std::cerr << "Get Integer Attribute Value Error: AttributeName = "
17095e6b3c1SJohn Wang                   << name << std::endl;
17195e6b3c1SJohn Wang         return integerInfo.defaultValue;
17295e6b3c1SJohn Wang     }
17395e6b3c1SJohn Wang }
17495e6b3c1SJohn Wang 
175*46ece063SSampa Misra int BIOSIntegerAttribute::updateAttrVal(Table& newValue, uint16_t attrHdl,
176*46ece063SSampa Misra                                         uint8_t attrType,
177*46ece063SSampa Misra                                         const PropertyValue& newPropVal)
178*46ece063SSampa Misra {
179*46ece063SSampa Misra     auto newVal = getAttrValue(newPropVal);
180*46ece063SSampa Misra     table::attribute_value::constructIntegerEntry(newValue, attrHdl, attrType,
181*46ece063SSampa Misra                                                   newVal);
182*46ece063SSampa Misra     return PLDM_SUCCESS;
183*46ece063SSampa Misra }
184*46ece063SSampa Misra 
18595e6b3c1SJohn Wang } // namespace bios
18695e6b3c1SJohn Wang } // namespace responder
18795e6b3c1SJohn Wang } // namespace pldm
188