1 #include "bios_string_attribute.hpp"
2 
3 #include "common/utils.hpp"
4 
5 #include <iostream>
6 #include <tuple>
7 #include <variant>
8 
9 namespace pldm
10 {
11 namespace responder
12 {
13 namespace bios
14 {
15 
16 BIOSStringAttribute::BIOSStringAttribute(const Json& entry,
17                                          DBusHandler* const dbusHandler) :
18     BIOSAttribute(entry, dbusHandler)
19 {
20     std::string strTypeTmp = entry.at("string_type");
21     auto iter = strTypeMap.find(strTypeTmp);
22     if (iter == strTypeMap.end())
23     {
24         std::cerr << "Wrong string type, STRING_TYPE=" << strTypeTmp
25                   << " ATTRIBUTE_NAME=" << name << "\n";
26         throw std::invalid_argument("Wrong string type");
27     }
28     stringInfo.stringType = static_cast<uint8_t>(iter->second);
29 
30     stringInfo.minLength = entry.at("minimum_string_length");
31     stringInfo.maxLength = entry.at("maximum_string_length");
32     stringInfo.defLength = entry.at("default_string_length");
33     stringInfo.defString = entry.at("default_string");
34 
35     pldm_bios_table_attr_entry_string_info info = {
36         0,
37         readOnly,
38         stringInfo.stringType,
39         stringInfo.minLength,
40         stringInfo.maxLength,
41         stringInfo.defLength,
42         stringInfo.defString.data(),
43     };
44 
45     const char* errmsg;
46     auto rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg);
47     if (rc != PLDM_SUCCESS)
48     {
49         std::cerr << "Wrong field for string attribute, ATTRIBUTE_NAME=" << name
50                   << " ERRMSG=" << errmsg
51                   << " MINIMUM_STRING_LENGTH=" << stringInfo.minLength
52                   << " MAXIMUM_STRING_LENGTH=" << stringInfo.maxLength
53                   << " DEFAULT_STRING_LENGTH=" << stringInfo.defLength
54                   << " DEFAULT_STRING=" << stringInfo.defString << "\n";
55         throw std::invalid_argument("Wrong field for string attribute");
56     }
57 }
58 
59 void BIOSStringAttribute::setAttrValueOnDbus(
60     const pldm_bios_attr_val_table_entry* attrValueEntry,
61     const pldm_bios_attr_table_entry*, const BIOSStringTable&)
62 {
63     if (readOnly)
64     {
65         return;
66     }
67 
68     PropertyValue value =
69         table::attribute_value::decodeStringEntry(attrValueEntry);
70     dbusHandler->setDbusProperty(*dBusMap, value);
71 }
72 
73 std::string BIOSStringAttribute::getAttrValue()
74 {
75     if (readOnly)
76     {
77         return stringInfo.defString;
78     }
79     try
80     {
81         return dbusHandler->getDbusProperty<std::string>(
82             dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
83             dBusMap->interface.c_str());
84     }
85     catch (const std::exception& e)
86     {
87         std::cerr << "Get String Attribute Value Error: AttributeName = "
88                   << name << std::endl;
89         return stringInfo.defString;
90     }
91 }
92 
93 void BIOSStringAttribute::constructEntry(const BIOSStringTable& stringTable,
94                                          Table& attrTable,
95                                          Table& attrValueTable)
96 {
97     pldm_bios_table_attr_entry_string_info info = {
98         stringTable.findHandle(name), readOnly,
99         stringInfo.stringType,        stringInfo.minLength,
100         stringInfo.maxLength,         stringInfo.defLength,
101         stringInfo.defString.data(),
102     };
103 
104     auto attrTableEntry =
105         table::attribute::constructStringEntry(attrTable, &info);
106     auto [attrHandle, attrType, _] =
107         table::attribute::decodeHeader(attrTableEntry);
108     auto currStr = getAttrValue();
109     table::attribute_value::constructStringEntry(attrValueTable, attrHandle,
110                                                  attrType, currStr);
111 }
112 
113 int BIOSStringAttribute::updateAttrVal(Table& newValue, uint16_t attrHdl,
114                                        uint8_t attrType,
115                                        const PropertyValue& newPropVal)
116 {
117     try
118     {
119         const auto& newStringValue = std::get<std::string>(newPropVal);
120         table::attribute_value::constructStringEntry(newValue, attrHdl,
121                                                      attrType, newStringValue);
122     }
123     catch (const std::bad_variant_access& e)
124     {
125         std::cerr << "invalid value passed for the property, error: "
126                   << e.what() << "\n";
127         return PLDM_ERROR;
128     }
129     return PLDM_SUCCESS;
130 }
131 
132 } // namespace bios
133 } // namespace responder
134 } // namespace pldm
135