blob: bf5c7a62a98a594b89bf7dd778c0d4e0f33b9b25 [file] [log] [blame]
John Wang95e6b3c2020-02-13 09:43:24 +08001#include "bios_integer_attribute.hpp"
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/utils.hpp"
John Wang95e6b3c2020-02-13 09:43:24 +08004
Riya Dixit49cfb132023-03-02 04:26:53 -06005#include <phosphor-logging/lg2.hpp>
6
7PHOSPHOR_LOG2_USING;
8
Brad Bishop5079ac42021-08-19 18:35:06 -04009using namespace pldm::utils;
10
John Wang95e6b3c2020-02-13 09:43:24 +080011namespace pldm
12{
13namespace responder
14{
15namespace bios
16{
John Wang95e6b3c2020-02-13 09:43:24 +080017BIOSIntegerAttribute::BIOSIntegerAttribute(const Json& entry,
18 DBusHandler* const dbusHandler) :
19 BIOSAttribute(entry, dbusHandler)
20{
21 std::string attr = entry.at("attribute_name");
22
23 integerInfo.lowerBound = entry.at("lower_bound");
24 integerInfo.upperBound = entry.at("upper_bound");
25 integerInfo.scalarIncrement = entry.at("scalar_increment");
26 integerInfo.defaultValue = entry.at("default_value");
27 pldm_bios_table_attr_entry_integer_info info = {
28 0,
29 readOnly,
30 integerInfo.lowerBound,
31 integerInfo.upperBound,
32 integerInfo.scalarIncrement,
33 integerInfo.defaultValue,
34 };
35 const char* errmsg = nullptr;
36 auto rc = pldm_bios_table_attr_entry_integer_info_check(&info, &errmsg);
37 if (rc != PLDM_SUCCESS)
38 {
Riya Dixit49cfb132023-03-02 04:26:53 -060039 error(
40 "Wrong filed for integer attribute, ATTRIBUTE_NAME={ATTR_NAME} ERRMSG= {ERR_MSG} LOWER_BOUND={LOW_BOUND} UPPER_BOUND={UPPER_BOUND} DEFAULT_VALUE={DEF_VAL} SCALAR_INCREMENT={SCALAR_INCREMENT}",
41 "ATTR_NAME", attr.c_str(), "ERR_MSG", errmsg, "LOW_BOUND",
42 integerInfo.lowerBound, "UPPER_BOUND", integerInfo.upperBound,
43 "DEF_VAL", integerInfo.defaultValue, "SCALAR_INCREMENT",
44 integerInfo.scalarIncrement);
John Wang95e6b3c2020-02-13 09:43:24 +080045 throw std::invalid_argument("Wrong field for integer attribute");
46 }
47}
48
49void BIOSIntegerAttribute::setAttrValueOnDbus(
50 const pldm_bios_attr_val_table_entry* attrValueEntry,
51 const pldm_bios_attr_table_entry*, const BIOSStringTable&)
52{
George Liu5bb9edb2021-08-05 20:10:32 +080053 if (!dBusMap.has_value())
John Wang95e6b3c2020-02-13 09:43:24 +080054 {
55 return;
56 }
57 auto currentValue =
58 table::attribute_value::decodeIntegerEntry(attrValueEntry);
59
60 if (dBusMap->propertyType == "uint8_t")
61 {
62 return dbusHandler->setDbusProperty(*dBusMap,
63 static_cast<uint8_t>(currentValue));
64 }
65 else if (dBusMap->propertyType == "uint16_t")
66 {
67 return dbusHandler->setDbusProperty(
68 *dBusMap, static_cast<uint16_t>(currentValue));
69 }
70 else if (dBusMap->propertyType == "int16_t")
71 {
72 return dbusHandler->setDbusProperty(*dBusMap,
73 static_cast<int16_t>(currentValue));
74 }
75 else if (dBusMap->propertyType == "uint32_t")
76 {
77 return dbusHandler->setDbusProperty(
78 *dBusMap, static_cast<uint32_t>(currentValue));
79 }
80 else if (dBusMap->propertyType == "int32_t")
81 {
82 return dbusHandler->setDbusProperty(*dBusMap,
83 static_cast<int32_t>(currentValue));
84 }
85 else if (dBusMap->propertyType == "uint64_t")
86 {
87 return dbusHandler->setDbusProperty(*dBusMap, currentValue);
88 }
89 else if (dBusMap->propertyType == "int64_t")
90 {
91 return dbusHandler->setDbusProperty(*dBusMap,
92 static_cast<int64_t>(currentValue));
93 }
George Liuaca897b2020-08-18 14:47:17 +080094 else if (dBusMap->propertyType == "double")
95 {
96 return dbusHandler->setDbusProperty(*dBusMap,
97 static_cast<double>(currentValue));
98 }
John Wang95e6b3c2020-02-13 09:43:24 +080099
Riya Dixit49cfb132023-03-02 04:26:53 -0600100 error("Unsupported property type on dbus: {DBUS_PROP}", "DBUS_PROP",
101 dBusMap->propertyType);
John Wang95e6b3c2020-02-13 09:43:24 +0800102 throw std::invalid_argument("dbus type error");
103}
104
Tom Josephca7b2522020-11-18 12:27:11 +0530105void BIOSIntegerAttribute::constructEntry(
106 const BIOSStringTable& stringTable, Table& attrTable, Table& attrValueTable,
107 std::optional<std::variant<int64_t, std::string>> optAttributeValue)
John Wang95e6b3c2020-02-13 09:43:24 +0800108{
John Wang95e6b3c2020-02-13 09:43:24 +0800109 pldm_bios_table_attr_entry_integer_info info = {
110 stringTable.findHandle(name), readOnly,
111 integerInfo.lowerBound, integerInfo.upperBound,
112 integerInfo.scalarIncrement, integerInfo.defaultValue,
113 };
114
115 auto attrTableEntry =
116 table::attribute::constructIntegerEntry(attrTable, &info);
117
118 auto [attrHandle, attrType, _] =
119 table::attribute::decodeHeader(attrTableEntry);
120
Tom Josephca7b2522020-11-18 12:27:11 +0530121 int64_t currentValue{};
122 if (optAttributeValue.has_value())
123 {
124 auto attributeValue = optAttributeValue.value();
125 if (attributeValue.index() == 0)
126 {
127 currentValue = std::get<int64_t>(attributeValue);
128 }
129 else
130 {
131 currentValue = getAttrValue();
132 }
133 }
134 else
135 {
136 currentValue = getAttrValue();
137 }
138
John Wang95e6b3c2020-02-13 09:43:24 +0800139 table::attribute_value::constructIntegerEntry(attrValueTable, attrHandle,
140 attrType, currentValue);
141}
142
Sampa Misra46ece062020-03-18 07:17:44 -0500143uint64_t BIOSIntegerAttribute::getAttrValue(const PropertyValue& propertyValue)
John Wang95e6b3c2020-02-13 09:43:24 +0800144{
Andrew Geissler081d03b2020-12-14 18:53:33 -0600145 uint64_t value = 0;
John Wang95e6b3c2020-02-13 09:43:24 +0800146 if (dBusMap->propertyType == "uint8_t")
147 {
148 value = std::get<uint8_t>(propertyValue);
149 }
150 else if (dBusMap->propertyType == "uint16_t")
151 {
152 value = std::get<uint16_t>(propertyValue);
153 }
154 else if (dBusMap->propertyType == "int16_t")
155 {
156 value = std::get<int16_t>(propertyValue);
157 }
158 else if (dBusMap->propertyType == "uint32_t")
159 {
160 value = std::get<uint32_t>(propertyValue);
161 }
162 else if (dBusMap->propertyType == "int32_t")
163 {
164 value = std::get<int32_t>(propertyValue);
165 }
166 else if (dBusMap->propertyType == "uint64_t")
167 {
168 value = std::get<uint64_t>(propertyValue);
169 }
170 else if (dBusMap->propertyType == "int64_t")
171 {
172 value = std::get<int64_t>(propertyValue);
173 }
George Liuaca897b2020-08-18 14:47:17 +0800174 else if (dBusMap->propertyType == "double")
175 {
176 value = std::get<double>(propertyValue);
177 }
Andrew Geissler081d03b2020-12-14 18:53:33 -0600178 else
179 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600180 error("Unsupported property type for getAttrValue: {DBUS_PROP}",
181 "DBUS_PROP", dBusMap->propertyType);
Andrew Geissler081d03b2020-12-14 18:53:33 -0600182 throw std::invalid_argument("dbus type error");
183 }
John Wang95e6b3c2020-02-13 09:43:24 +0800184 return value;
185}
186
187uint64_t BIOSIntegerAttribute::getAttrValue()
188{
George Liu5bb9edb2021-08-05 20:10:32 +0800189 if (!dBusMap.has_value())
John Wang95e6b3c2020-02-13 09:43:24 +0800190 {
191 return integerInfo.defaultValue;
192 }
193
194 try
195 {
196 auto propertyValue = dbusHandler->getDbusPropertyVariant(
197 dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
198 dBusMap->interface.c_str());
199
200 return getAttrValue(propertyValue);
201 }
202 catch (const std::exception& e)
203 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600204 error("Get Integer Attribute Value Error: AttributeName = {ATTR_NAME}",
205 "ATTR_NAME", name);
John Wang95e6b3c2020-02-13 09:43:24 +0800206 return integerInfo.defaultValue;
207 }
208}
209
Sampa Misra46ece062020-03-18 07:17:44 -0500210int BIOSIntegerAttribute::updateAttrVal(Table& newValue, uint16_t attrHdl,
211 uint8_t attrType,
212 const PropertyValue& newPropVal)
213{
214 auto newVal = getAttrValue(newPropVal);
215 table::attribute_value::constructIntegerEntry(newValue, attrHdl, attrType,
216 newVal);
217 return PLDM_SUCCESS;
218}
219
George Liu1244acf2020-08-14 09:11:11 +0800220void BIOSIntegerAttribute::generateAttributeEntry(
221 const std::variant<int64_t, std::string>& attributevalue,
222 Table& attrValueEntry)
223{
224 attrValueEntry.resize(sizeof(pldm_bios_attr_val_table_entry) +
225 sizeof(int64_t) - 1);
226
227 auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
228 attrValueEntry.data());
229
230 int64_t value = std::get<int64_t>(attributevalue);
231 entry->attr_type = 3;
232 memcpy(entry->value, &value, sizeof(int64_t));
233}
234
John Wang95e6b3c2020-02-13 09:43:24 +0800235} // namespace bios
236} // namespace responder
Sampa Misra46ece062020-03-18 07:17:44 -0500237} // namespace pldm