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