blob: d89ab49f3bb02d9376528880ef843e206421e2e5 [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
Brad Bishop5079ac42021-08-19 18:35:06 -04009using namespace pldm::utils;
10
John Wang29683b52020-02-27 16:41:44 +080011namespace pldm
12{
13namespace responder
14{
15namespace bios
16{
17
18BIOSStringAttribute::BIOSStringAttribute(const Json& entry,
19 DBusHandler* const dbusHandler) :
20 BIOSAttribute(entry, dbusHandler)
21{
22 std::string strTypeTmp = entry.at("string_type");
23 auto iter = strTypeMap.find(strTypeTmp);
24 if (iter == strTypeMap.end())
25 {
26 std::cerr << "Wrong string type, STRING_TYPE=" << strTypeTmp
27 << " ATTRIBUTE_NAME=" << name << "\n";
28 throw std::invalid_argument("Wrong string type");
29 }
30 stringInfo.stringType = static_cast<uint8_t>(iter->second);
31
32 stringInfo.minLength = entry.at("minimum_string_length");
33 stringInfo.maxLength = entry.at("maximum_string_length");
34 stringInfo.defLength = entry.at("default_string_length");
35 stringInfo.defString = entry.at("default_string");
36
37 pldm_bios_table_attr_entry_string_info info = {
38 0,
39 readOnly,
40 stringInfo.stringType,
41 stringInfo.minLength,
42 stringInfo.maxLength,
43 stringInfo.defLength,
44 stringInfo.defString.data(),
45 };
46
47 const char* errmsg;
48 auto rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg);
49 if (rc != PLDM_SUCCESS)
50 {
51 std::cerr << "Wrong field for string attribute, ATTRIBUTE_NAME=" << name
52 << " ERRMSG=" << errmsg
53 << " MINIMUM_STRING_LENGTH=" << stringInfo.minLength
54 << " MAXIMUM_STRING_LENGTH=" << stringInfo.maxLength
55 << " DEFAULT_STRING_LENGTH=" << stringInfo.defLength
56 << " DEFAULT_STRING=" << stringInfo.defString << "\n";
57 throw std::invalid_argument("Wrong field for string attribute");
58 }
59}
60
61void BIOSStringAttribute::setAttrValueOnDbus(
62 const pldm_bios_attr_val_table_entry* attrValueEntry,
63 const pldm_bios_attr_table_entry*, const BIOSStringTable&)
64{
George Liu5bb9edb2021-08-05 20:10:32 +080065 if (!dBusMap.has_value())
John Wang29683b52020-02-27 16:41:44 +080066 {
67 return;
68 }
69
70 PropertyValue value =
71 table::attribute_value::decodeStringEntry(attrValueEntry);
72 dbusHandler->setDbusProperty(*dBusMap, value);
73}
74
75std::string BIOSStringAttribute::getAttrValue()
76{
George Liu5bb9edb2021-08-05 20:10:32 +080077 if (!dBusMap.has_value())
John Wang29683b52020-02-27 16:41:44 +080078 {
79 return stringInfo.defString;
80 }
81 try
82 {
83 return dbusHandler->getDbusProperty<std::string>(
84 dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
85 dBusMap->interface.c_str());
86 }
87 catch (const std::exception& e)
88 {
89 std::cerr << "Get String Attribute Value Error: AttributeName = "
90 << name << std::endl;
91 return stringInfo.defString;
92 }
93}
94
Tom Josephca7b2522020-11-18 12:27:11 +053095void BIOSStringAttribute::constructEntry(
96 const BIOSStringTable& stringTable, Table& attrTable, Table& attrValueTable,
97 std::optional<std::variant<int64_t, std::string>> optAttributeValue)
John Wang29683b52020-02-27 16:41:44 +080098{
99 pldm_bios_table_attr_entry_string_info info = {
100 stringTable.findHandle(name), readOnly,
101 stringInfo.stringType, stringInfo.minLength,
102 stringInfo.maxLength, stringInfo.defLength,
103 stringInfo.defString.data(),
104 };
105
106 auto attrTableEntry =
107 table::attribute::constructStringEntry(attrTable, &info);
108 auto [attrHandle, attrType, _] =
109 table::attribute::decodeHeader(attrTableEntry);
Tom Josephca7b2522020-11-18 12:27:11 +0530110
111 std::string currStr{};
112 if (optAttributeValue.has_value())
113 {
114 auto attributeValue = optAttributeValue.value();
115 if (attributeValue.index() == 1)
116 {
117 currStr = std::get<std::string>(attributeValue);
118 }
119 else
120 {
121 currStr = getAttrValue();
122 }
123 }
124 else
125 {
126 currStr = getAttrValue();
127 }
128
John Wang29683b52020-02-27 16:41:44 +0800129 table::attribute_value::constructStringEntry(attrValueTable, attrHandle,
130 attrType, currStr);
131}
132
Sampa Misra46ece062020-03-18 07:17:44 -0500133int BIOSStringAttribute::updateAttrVal(Table& newValue, uint16_t attrHdl,
134 uint8_t attrType,
135 const PropertyValue& newPropVal)
136{
137 try
138 {
139 const auto& newStringValue = std::get<std::string>(newPropVal);
140 table::attribute_value::constructStringEntry(newValue, attrHdl,
141 attrType, newStringValue);
142 }
143 catch (const std::bad_variant_access& e)
144 {
145 std::cerr << "invalid value passed for the property, error: "
146 << e.what() << "\n";
147 return PLDM_ERROR;
148 }
149 return PLDM_SUCCESS;
150}
151
George Liu1244acf2020-08-14 09:11:11 +0800152void BIOSStringAttribute::generateAttributeEntry(
153 const std::variant<int64_t, std::string>& attributevalue,
154 Table& attrValueEntry)
155{
156 std::string value = std::get<std::string>(attributevalue);
157 uint16_t len = value.size();
158
159 attrValueEntry.resize(sizeof(pldm_bios_attr_val_table_entry) +
160 sizeof(uint16_t) + len - 1);
161
162 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
163 attrValueEntry.data());
164
165 entry->attr_type = 1;
166 memcpy(entry->value, &len, sizeof(uint16_t));
167 memcpy(entry->value + sizeof(uint16_t), value.c_str(), value.size());
168}
169
John Wang29683b52020-02-27 16:41:44 +0800170} // namespace bios
171} // namespace responder
172} // namespace pldm