blob: d52cdc41dab3a53d0b3cd2bcacb8257cac082e67 [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
Riya Dixit49cfb132023-03-02 04:26:53 -06005#include <phosphor-logging/lg2.hpp>
6
John Wang29683b52020-02-27 16:41:44 +08007#include <tuple>
8#include <variant>
9
Riya Dixit49cfb132023-03-02 04:26:53 -060010PHOSPHOR_LOG2_USING;
11
Brad Bishop5079ac42021-08-19 18:35:06 -040012using namespace pldm::utils;
13
John Wang29683b52020-02-27 16:41:44 +080014namespace pldm
15{
16namespace responder
17{
18namespace bios
19{
John Wang29683b52020-02-27 16:41:44 +080020BIOSStringAttribute::BIOSStringAttribute(const Json& entry,
21 DBusHandler* const dbusHandler) :
22 BIOSAttribute(entry, dbusHandler)
23{
24 std::string strTypeTmp = entry.at("string_type");
25 auto iter = strTypeMap.find(strTypeTmp);
26 if (iter == strTypeMap.end())
27 {
Riya Dixit89644442024-03-31 05:39:59 -050028 error("Wrong string type '{TYPE}' for attribute '{ATTRIBUTE}'", "TYPE",
29 strTypeTmp, "ATTRIBUTE", name);
John Wang29683b52020-02-27 16:41:44 +080030 throw std::invalid_argument("Wrong string type");
31 }
32 stringInfo.stringType = static_cast<uint8_t>(iter->second);
33
34 stringInfo.minLength = entry.at("minimum_string_length");
35 stringInfo.maxLength = entry.at("maximum_string_length");
36 stringInfo.defLength = entry.at("default_string_length");
37 stringInfo.defString = entry.at("default_string");
38
39 pldm_bios_table_attr_entry_string_info info = {
40 0,
41 readOnly,
42 stringInfo.stringType,
43 stringInfo.minLength,
44 stringInfo.maxLength,
45 stringInfo.defLength,
46 stringInfo.defString.data(),
47 };
48
49 const char* errmsg;
50 auto rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg);
51 if (rc != PLDM_SUCCESS)
52 {
Riya Dixit49cfb132023-03-02 04:26:53 -060053 error(
Riya Dixit89644442024-03-31 05:39:59 -050054 "Wrong field for string attribute '{ATTRIBUTE}', error '{ERROR}', minimum string length '{MINIMUM_STRING_LENGTH}', maximum string length '{MAXIMUM_STRING_LENGTH}', default string length '{DEFAULT_STRING_LENGTH}' and default string '{DEFAULT_STRING}'",
55 "ATTRIBUTE", name, "ERROR", errmsg, "MINIMUM_STRING_LENGTH",
56 stringInfo.minLength, "MAXIMUM_STRING_LENGTH", stringInfo.maxLength,
57 "DEFAULT_STRING_LENGTH", stringInfo.defLength, "DEFAULT_STRING",
58 stringInfo.defString);
John Wang29683b52020-02-27 16:41:44 +080059 throw std::invalid_argument("Wrong field for string attribute");
60 }
61}
62
63void BIOSStringAttribute::setAttrValueOnDbus(
64 const pldm_bios_attr_val_table_entry* attrValueEntry,
65 const pldm_bios_attr_table_entry*, const BIOSStringTable&)
66{
George Liu5bb9edb2021-08-05 20:10:32 +080067 if (!dBusMap.has_value())
John Wang29683b52020-02-27 16:41:44 +080068 {
69 return;
70 }
71
72 PropertyValue value =
73 table::attribute_value::decodeStringEntry(attrValueEntry);
74 dbusHandler->setDbusProperty(*dBusMap, value);
75}
76
77std::string BIOSStringAttribute::getAttrValue()
78{
George Liu5bb9edb2021-08-05 20:10:32 +080079 if (!dBusMap.has_value())
John Wang29683b52020-02-27 16:41:44 +080080 {
81 return stringInfo.defString;
82 }
83 try
84 {
85 return dbusHandler->getDbusProperty<std::string>(
86 dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
87 dBusMap->interface.c_str());
88 }
89 catch (const std::exception& e)
90 {
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -050091 error(
Riya Dixit89644442024-03-31 05:39:59 -050092 "Failed to get string attribute '{ATTRIBUTE}' at path '{PATH}' and interface '{INTERFACE}' for property '{PROPERTY}', error - {ERROR}",
93 "ATTRIBUTE", name, "PATH", dBusMap->objectPath, "INTERFACE",
94 dBusMap->interface, "PROPERTY", dBusMap->propertyName, "ERROR", e);
John Wang29683b52020-02-27 16:41:44 +080095 return stringInfo.defString;
96 }
97}
98
Tom Josephca7b2522020-11-18 12:27:11 +053099void BIOSStringAttribute::constructEntry(
100 const BIOSStringTable& stringTable, Table& attrTable, Table& attrValueTable,
101 std::optional<std::variant<int64_t, std::string>> optAttributeValue)
John Wang29683b52020-02-27 16:41:44 +0800102{
103 pldm_bios_table_attr_entry_string_info info = {
104 stringTable.findHandle(name), readOnly,
105 stringInfo.stringType, stringInfo.minLength,
106 stringInfo.maxLength, stringInfo.defLength,
107 stringInfo.defString.data(),
108 };
109
Patrick Williams6da4f912023-05-10 07:50:53 -0500110 auto attrTableEntry = table::attribute::constructStringEntry(attrTable,
111 &info);
112 auto [attrHandle, attrType,
113 _] = table::attribute::decodeHeader(attrTableEntry);
Tom Josephca7b2522020-11-18 12:27:11 +0530114
115 std::string currStr{};
116 if (optAttributeValue.has_value())
117 {
118 auto attributeValue = optAttributeValue.value();
119 if (attributeValue.index() == 1)
120 {
121 currStr = std::get<std::string>(attributeValue);
122 }
123 else
124 {
125 currStr = getAttrValue();
126 }
127 }
128 else
129 {
130 currStr = getAttrValue();
131 }
132
John Wang29683b52020-02-27 16:41:44 +0800133 table::attribute_value::constructStringEntry(attrValueTable, attrHandle,
134 attrType, currStr);
135}
136
Sampa Misra46ece062020-03-18 07:17:44 -0500137int BIOSStringAttribute::updateAttrVal(Table& newValue, uint16_t attrHdl,
138 uint8_t attrType,
139 const PropertyValue& newPropVal)
140{
141 try
142 {
143 const auto& newStringValue = std::get<std::string>(newPropVal);
144 table::attribute_value::constructStringEntry(newValue, attrHdl,
145 attrType, newStringValue);
146 }
147 catch (const std::bad_variant_access& e)
148 {
Riya Dixit89644442024-03-31 05:39:59 -0500149 error("Invalid value passed for the property - {ERROR}", "ERROR", e);
Sampa Misra46ece062020-03-18 07:17:44 -0500150 return PLDM_ERROR;
151 }
152 return PLDM_SUCCESS;
153}
154
George Liu1244acf2020-08-14 09:11:11 +0800155void BIOSStringAttribute::generateAttributeEntry(
156 const std::variant<int64_t, std::string>& attributevalue,
157 Table& attrValueEntry)
158{
159 std::string value = std::get<std::string>(attributevalue);
160 uint16_t len = value.size();
161
162 attrValueEntry.resize(sizeof(pldm_bios_attr_val_table_entry) +
163 sizeof(uint16_t) + len - 1);
164
165 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
166 attrValueEntry.data());
167
168 entry->attr_type = 1;
169 memcpy(entry->value, &len, sizeof(uint16_t));
170 memcpy(entry->value + sizeof(uint16_t), value.c_str(), value.size());
171}
172
John Wang29683b52020-02-27 16:41:44 +0800173} // namespace bios
174} // namespace responder
175} // namespace pldm