John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 1 | #include "bios_string_attribute.hpp" |
| 2 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 3 | #include "common/utils.hpp" |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 4 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
| 6 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 7 | #include <tuple> |
| 8 | #include <variant> |
| 9 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 10 | PHOSPHOR_LOG2_USING; |
| 11 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 12 | using namespace pldm::utils; |
| 13 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 14 | namespace pldm |
| 15 | { |
| 16 | namespace responder |
| 17 | { |
| 18 | namespace bios |
| 19 | { |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 20 | BIOSStringAttribute::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 Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 28 | error("Wrong string type '{TYPE}' for attribute '{ATTRIBUTE}'", "TYPE", |
| 29 | strTypeTmp, "ATTRIBUTE", name); |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 30 | 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"); |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 36 | stringInfo.defString = entry.at("default_string"); |
Archana Kakani | 7d47570 | 2024-05-22 08:30:27 -0500 | [diff] [blame] | 37 | stringInfo.defLength = |
| 38 | static_cast<uint16_t>((stringInfo.defString).length()); |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 39 | |
| 40 | pldm_bios_table_attr_entry_string_info info = { |
| 41 | 0, |
| 42 | readOnly, |
| 43 | stringInfo.stringType, |
| 44 | stringInfo.minLength, |
| 45 | stringInfo.maxLength, |
| 46 | stringInfo.defLength, |
| 47 | stringInfo.defString.data(), |
| 48 | }; |
| 49 | |
| 50 | const char* errmsg; |
| 51 | auto rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg); |
| 52 | if (rc != PLDM_SUCCESS) |
| 53 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 54 | error( |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 55 | "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}'", |
| 56 | "ATTRIBUTE", name, "ERROR", errmsg, "MINIMUM_STRING_LENGTH", |
| 57 | stringInfo.minLength, "MAXIMUM_STRING_LENGTH", stringInfo.maxLength, |
| 58 | "DEFAULT_STRING_LENGTH", stringInfo.defLength, "DEFAULT_STRING", |
| 59 | stringInfo.defString); |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 60 | throw std::invalid_argument("Wrong field for string attribute"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void BIOSStringAttribute::setAttrValueOnDbus( |
| 65 | const pldm_bios_attr_val_table_entry* attrValueEntry, |
| 66 | const pldm_bios_attr_table_entry*, const BIOSStringTable&) |
| 67 | { |
George Liu | 5bb9edb | 2021-08-05 20:10:32 +0800 | [diff] [blame] | 68 | if (!dBusMap.has_value()) |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 69 | { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | PropertyValue value = |
| 74 | table::attribute_value::decodeStringEntry(attrValueEntry); |
| 75 | dbusHandler->setDbusProperty(*dBusMap, value); |
| 76 | } |
| 77 | |
| 78 | std::string BIOSStringAttribute::getAttrValue() |
| 79 | { |
George Liu | 5bb9edb | 2021-08-05 20:10:32 +0800 | [diff] [blame] | 80 | if (!dBusMap.has_value()) |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 81 | { |
| 82 | return stringInfo.defString; |
| 83 | } |
| 84 | try |
| 85 | { |
| 86 | return dbusHandler->getDbusProperty<std::string>( |
| 87 | dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(), |
| 88 | dBusMap->interface.c_str()); |
| 89 | } |
| 90 | catch (const std::exception& e) |
| 91 | { |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 92 | error( |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 93 | "Failed to get string attribute '{ATTRIBUTE}' at path '{PATH}' and interface '{INTERFACE}' for property '{PROPERTY}', error - {ERROR}", |
| 94 | "ATTRIBUTE", name, "PATH", dBusMap->objectPath, "INTERFACE", |
| 95 | dBusMap->interface, "PROPERTY", dBusMap->propertyName, "ERROR", e); |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 96 | return stringInfo.defString; |
| 97 | } |
| 98 | } |
| 99 | |
Tom Joseph | ca7b252 | 2020-11-18 12:27:11 +0530 | [diff] [blame] | 100 | void BIOSStringAttribute::constructEntry( |
| 101 | const BIOSStringTable& stringTable, Table& attrTable, Table& attrValueTable, |
| 102 | std::optional<std::variant<int64_t, std::string>> optAttributeValue) |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 103 | { |
| 104 | pldm_bios_table_attr_entry_string_info info = { |
| 105 | stringTable.findHandle(name), readOnly, |
| 106 | stringInfo.stringType, stringInfo.minLength, |
| 107 | stringInfo.maxLength, stringInfo.defLength, |
| 108 | stringInfo.defString.data(), |
| 109 | }; |
| 110 | |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 111 | auto attrTableEntry = |
| 112 | table::attribute::constructStringEntry(attrTable, &info); |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 113 | auto [attrHandle, attrType, |
| 114 | _] = table::attribute::decodeHeader(attrTableEntry); |
Tom Joseph | ca7b252 | 2020-11-18 12:27:11 +0530 | [diff] [blame] | 115 | |
| 116 | std::string currStr{}; |
| 117 | if (optAttributeValue.has_value()) |
| 118 | { |
| 119 | auto attributeValue = optAttributeValue.value(); |
| 120 | if (attributeValue.index() == 1) |
| 121 | { |
| 122 | currStr = std::get<std::string>(attributeValue); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | currStr = getAttrValue(); |
| 127 | } |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | currStr = getAttrValue(); |
| 132 | } |
| 133 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 134 | table::attribute_value::constructStringEntry(attrValueTable, attrHandle, |
| 135 | attrType, currStr); |
| 136 | } |
| 137 | |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 138 | int BIOSStringAttribute::updateAttrVal(Table& newValue, uint16_t attrHdl, |
| 139 | uint8_t attrType, |
| 140 | const PropertyValue& newPropVal) |
| 141 | { |
| 142 | try |
| 143 | { |
| 144 | const auto& newStringValue = std::get<std::string>(newPropVal); |
| 145 | table::attribute_value::constructStringEntry(newValue, attrHdl, |
| 146 | attrType, newStringValue); |
| 147 | } |
| 148 | catch (const std::bad_variant_access& e) |
| 149 | { |
Riya Dixit | 8964444 | 2024-03-31 05:39:59 -0500 | [diff] [blame] | 150 | error("Invalid value passed for the property - {ERROR}", "ERROR", e); |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 151 | return PLDM_ERROR; |
| 152 | } |
| 153 | return PLDM_SUCCESS; |
| 154 | } |
| 155 | |
George Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 156 | void BIOSStringAttribute::generateAttributeEntry( |
| 157 | const std::variant<int64_t, std::string>& attributevalue, |
| 158 | Table& attrValueEntry) |
| 159 | { |
| 160 | std::string value = std::get<std::string>(attributevalue); |
| 161 | uint16_t len = value.size(); |
| 162 | |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 163 | attrValueEntry.resize( |
| 164 | sizeof(pldm_bios_attr_val_table_entry) + sizeof(uint16_t) + len - 1); |
George Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 165 | |
| 166 | auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>( |
| 167 | attrValueEntry.data()); |
| 168 | |
| 169 | entry->attr_type = 1; |
| 170 | memcpy(entry->value, &len, sizeof(uint16_t)); |
| 171 | memcpy(entry->value + sizeof(uint16_t), value.c_str(), value.size()); |
| 172 | } |
| 173 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 174 | } // namespace bios |
| 175 | } // namespace responder |
| 176 | } // namespace pldm |