blob: 41538b68731dce291e83aff1bced727cef5e79ef [file] [log] [blame]
John Wang29683b52020-02-27 16:41:44 +08001#include "bios_string_attribute.hpp"
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/utils.hpp"
John Wang29683b52020-02-27 16:41:44 +08004
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{
George Liudaa69232020-09-02 17:22:09 +080063 if (readOnly || !dBusMap.has_value())
John Wang29683b52020-02-27 16:41:44 +080064 {
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{
George Liudaa69232020-09-02 17:22:09 +080075 if (readOnly || !dBusMap.has_value())
John Wang29683b52020-02-27 16:41:44 +080076 {
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
Sampa Misra46ece062020-03-18 07:17:44 -0500113int 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
George Liu1244acf2020-08-14 09:11:11 +0800132void BIOSStringAttribute::generateAttributeEntry(
133 const std::variant<int64_t, std::string>& attributevalue,
134 Table& attrValueEntry)
135{
136 std::string value = std::get<std::string>(attributevalue);
137 uint16_t len = value.size();
138
139 attrValueEntry.resize(sizeof(pldm_bios_attr_val_table_entry) +
140 sizeof(uint16_t) + len - 1);
141
142 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
143 attrValueEntry.data());
144
145 entry->attr_type = 1;
146 memcpy(entry->value, &len, sizeof(uint16_t));
147 memcpy(entry->value + sizeof(uint16_t), value.c_str(), value.size());
148}
149
John Wang29683b52020-02-27 16:41:44 +0800150} // namespace bios
151} // namespace responder
152} // namespace pldm