blob: e799cee2dc5d9f8adb41b0b8dfdc7fff606b8f07 [file] [log] [blame]
Sampa Misra46ece062020-03-18 07:17:44 -05001#include "config.h"
2
John Wang3be70852020-02-13 15:59:04 +08003#include "bios_enum_attribute.hpp"
4
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05005#include "common/utils.hpp"
John Wang3be70852020-02-13 15:59:04 +08006
7#include <iostream>
8
9namespace pldm
10{
11namespace responder
12{
13namespace bios
14{
15
16BIOSEnumAttribute::BIOSEnumAttribute(const Json& entry,
17 DBusHandler* const dbusHandler) :
18 BIOSAttribute(entry, dbusHandler)
19{
20 std::string attrName = entry.at("attribute_name");
21 Json pv = entry.at("possible_values");
22 for (auto& val : pv)
23 {
24 possibleValues.emplace_back(val);
25 }
26
27 std::vector<std::string> defaultValues;
28 Json dv = entry.at("default_values");
29 for (auto& val : dv)
30 {
31 defaultValues.emplace_back(val);
32 }
33 assert(defaultValues.size() == 1);
34 defaultValue = defaultValues[0];
35 if (!readOnly)
36 {
37 auto dbusValues = entry.at("dbus").at("property_values");
38 buildValMap(dbusValues);
39 }
40}
41
42uint8_t BIOSEnumAttribute::getValueIndex(const std::string& value,
43 const std::vector<std::string>& pVs)
44{
45 auto iter = std::find_if(pVs.begin(), pVs.end(),
46 [&value](const auto& v) { return v == value; });
47 if (iter == pVs.end())
48 {
49 throw std::invalid_argument("value must be one of possible value");
50 }
51 return iter - pVs.begin();
52}
53
54std::vector<uint16_t> BIOSEnumAttribute::getPossibleValuesHandle(
55 const BIOSStringTable& stringTable, const std::vector<std::string>& pVs)
56{
57 std::vector<uint16_t> possibleValuesHandle;
58 for (const auto& pv : pVs)
59 {
60 auto handle = stringTable.findHandle(pv);
61 possibleValuesHandle.push_back(handle);
62 }
63
64 return possibleValuesHandle;
65}
66
67void BIOSEnumAttribute::buildValMap(const Json& dbusVals)
68{
69 PropertyValue value;
70 size_t pos = 0;
71 for (auto it = dbusVals.begin(); it != dbusVals.end(); ++it, ++pos)
72 {
73 if (dBusMap->propertyType == "uint8_t")
74 {
75 value = static_cast<uint8_t>(it.value());
76 }
77 else if (dBusMap->propertyType == "uint16_t")
78 {
79 value = static_cast<uint16_t>(it.value());
80 }
81 else if (dBusMap->propertyType == "uint32_t")
82 {
83 value = static_cast<uint32_t>(it.value());
84 }
85 else if (dBusMap->propertyType == "uint64_t")
86 {
87 value = static_cast<uint64_t>(it.value());
88 }
89 else if (dBusMap->propertyType == "int16_t")
90 {
91 value = static_cast<int16_t>(it.value());
92 }
93 else if (dBusMap->propertyType == "int32_t")
94 {
95 value = static_cast<int32_t>(it.value());
96 }
97 else if (dBusMap->propertyType == "int64_t")
98 {
99 value = static_cast<int64_t>(it.value());
100 }
101 else if (dBusMap->propertyType == "bool")
102 {
103 value = static_cast<bool>(it.value());
104 }
105 else if (dBusMap->propertyType == "double")
106 {
107 value = static_cast<double>(it.value());
108 }
109 else if (dBusMap->propertyType == "string")
110 {
111 value = static_cast<std::string>(it.value());
112 }
113 else
114 {
115 std::cerr << "Unknown D-Bus property type, TYPE="
116 << dBusMap->propertyType << "\n";
117 throw std::invalid_argument("Unknown D-BUS property type");
118 }
119 valMap.emplace(value, possibleValues[pos]);
120 }
121}
122
123uint8_t BIOSEnumAttribute::getAttrValueIndex()
124{
125 auto defaultValueIndex = getValueIndex(defaultValue, possibleValues);
126 if (readOnly)
127 {
128 return defaultValueIndex;
129 }
130
131 try
132 {
133 auto propValue = dbusHandler->getDbusPropertyVariant(
134 dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
135 dBusMap->interface.c_str());
136 auto iter = valMap.find(propValue);
137 if (iter == valMap.end())
138 {
139 return defaultValueIndex;
140 }
141 auto currentValue = iter->second;
142 return getValueIndex(currentValue, possibleValues);
143 }
144 catch (const std::exception& e)
145 {
146 return defaultValueIndex;
147 }
148}
149
George Liu1244acf2020-08-14 09:11:11 +0800150uint8_t BIOSEnumAttribute::getAttrValueIndex(const PropertyValue& propValue)
151{
152 auto defaultValueIndex = getValueIndex(defaultValue, possibleValues);
153 if (readOnly)
154 {
155 return defaultValueIndex;
156 }
157
158 try
159 {
160 auto iter = valMap.find(propValue);
161 if (iter == valMap.end())
162 {
163 return defaultValueIndex;
164 }
165 auto currentValue = iter->second;
166 return getValueIndex(currentValue, possibleValues);
167 }
168 catch (const std::exception& e)
169 {
170 return defaultValueIndex;
171 }
172}
173
John Wang3be70852020-02-13 15:59:04 +0800174void BIOSEnumAttribute::setAttrValueOnDbus(
175 const pldm_bios_attr_val_table_entry* attrValueEntry,
176 const pldm_bios_attr_table_entry* attrEntry,
177 const BIOSStringTable& stringTable)
178{
179 if (readOnly)
180 {
181 return;
182 }
183 auto [pvHdls, _] = table::attribute::decodeEnumEntry(attrEntry);
184 auto currHdls = table::attribute_value::decodeEnumEntry(attrValueEntry);
185
186 assert(currHdls.size() == 1);
187
188 auto valueString = stringTable.findString(pvHdls[currHdls[0]]);
189
190 auto it = std::find_if(valMap.begin(), valMap.end(),
191 [&valueString](const auto& typePair) {
192 return typePair.second == valueString;
193 });
194 if (it == valMap.end())
195 {
196 return;
197 }
198
199 dbusHandler->setDbusProperty(*dBusMap, it->first);
200}
201
202void BIOSEnumAttribute::constructEntry(const BIOSStringTable& stringTable,
203 Table& attrTable, Table& attrValueTable)
204{
205 auto possibleValuesHandle =
206 getPossibleValuesHandle(stringTable, possibleValues);
207 std::vector<uint8_t> defaultIndices(1, 0);
208 defaultIndices[0] = getValueIndex(defaultValue, possibleValues);
209
210 pldm_bios_table_attr_entry_enum_info info = {
211 stringTable.findHandle(name), readOnly,
212 (uint8_t)possibleValuesHandle.size(), possibleValuesHandle.data(),
213 (uint8_t)defaultIndices.size(), defaultIndices.data(),
214 };
215
216 auto attrTableEntry =
217 table::attribute::constructEnumEntry(attrTable, &info);
218 auto [attrHandle, attrType, _] =
219 table::attribute::decodeHeader(attrTableEntry);
220
221 std::vector<uint8_t> currValueIndices(1, 0);
222 currValueIndices[0] = getAttrValueIndex();
223
224 table::attribute_value::constructEnumEntry(attrValueTable, attrHandle,
225 attrType, currValueIndices);
226}
227
Sampa Misra46ece062020-03-18 07:17:44 -0500228int BIOSEnumAttribute::updateAttrVal(Table& newValue, uint16_t attrHdl,
229 uint8_t attrType,
230 const PropertyValue& newPropVal)
231{
232 auto iter = valMap.find(newPropVal);
233 if (iter == valMap.end())
234 {
235 std::cerr << "Could not find index for new BIOS enum, value="
236 << std::get<std::string>(newPropVal) << "\n";
237 return PLDM_ERROR;
238 }
239 auto currentValue = iter->second;
240 std::vector<uint8_t> handleIndices{
241 getValueIndex(currentValue, possibleValues)};
242 table::attribute_value::constructEnumEntry(newValue, attrHdl, attrType,
243 handleIndices);
244 return PLDM_SUCCESS;
245}
246
George Liu1244acf2020-08-14 09:11:11 +0800247void BIOSEnumAttribute::generateAttributeEntry(
248 const std::variant<int64_t, std::string>& attributevalue,
249 Table& attrValueEntry)
250{
251 attrValueEntry.resize(sizeof(pldm_bios_attr_val_table_entry) + 1);
252
253 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
254 attrValueEntry.data());
255
256 std::string value = std::get<std::string>(attributevalue);
257 entry->attr_type = 0;
258 entry->value[0] = 1; // number of current values, default 1
259 entry->value[1] = getAttrValueIndex(value);
260}
261
John Wang3be70852020-02-13 15:59:04 +0800262} // namespace bios
263} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500264} // namespace pldm