blob: 1d9ec5d3e87aa42b263e5f3bad07c1f2691e3543 [file] [log] [blame]
John Wang29683b52020-02-27 16:41:44 +08001#include "bios_string_attribute.hpp"
2
3#include "utils.hpp"
4
5#include <iostream>
6#include <tuple>
7#include <variant>
8
9namespace pldm
10{
11namespace responder
12{
13namespace bios
14{
15
16BIOSStringAttribute::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
59void 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
73std::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
93void 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} // namespace bios
114} // namespace responder
115} // namespace pldm