bios: Implement BIOSStringAttribute

Implement BIOSStringAttribute, most of the code is copied from
bios/bios_parser.cpp.

Implement SetAttrValueOnDbus and constructEntry for string attribute

Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: Ic7c6b35d32738d698d7649f97cb7843606b8a2ba
diff --git a/libpldmresponder/bios_string_attribute.cpp b/libpldmresponder/bios_string_attribute.cpp
new file mode 100644
index 0000000..1d9ec5d
--- /dev/null
+++ b/libpldmresponder/bios_string_attribute.cpp
@@ -0,0 +1,115 @@
+#include "bios_string_attribute.hpp"
+
+#include "utils.hpp"
+
+#include <iostream>
+#include <tuple>
+#include <variant>
+
+namespace pldm
+{
+namespace responder
+{
+namespace bios
+{
+
+BIOSStringAttribute::BIOSStringAttribute(const Json& entry,
+                                         DBusHandler* const dbusHandler) :
+    BIOSAttribute(entry, dbusHandler)
+{
+    std::string strTypeTmp = entry.at("string_type");
+    auto iter = strTypeMap.find(strTypeTmp);
+    if (iter == strTypeMap.end())
+    {
+        std::cerr << "Wrong string type, STRING_TYPE=" << strTypeTmp
+                  << " ATTRIBUTE_NAME=" << name << "\n";
+        throw std::invalid_argument("Wrong string type");
+    }
+    stringInfo.stringType = static_cast<uint8_t>(iter->second);
+
+    stringInfo.minLength = entry.at("minimum_string_length");
+    stringInfo.maxLength = entry.at("maximum_string_length");
+    stringInfo.defLength = entry.at("default_string_length");
+    stringInfo.defString = entry.at("default_string");
+
+    pldm_bios_table_attr_entry_string_info info = {
+        0,
+        readOnly,
+        stringInfo.stringType,
+        stringInfo.minLength,
+        stringInfo.maxLength,
+        stringInfo.defLength,
+        stringInfo.defString.data(),
+    };
+
+    const char* errmsg;
+    auto rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg);
+    if (rc != PLDM_SUCCESS)
+    {
+        std::cerr << "Wrong field for string attribute, ATTRIBUTE_NAME=" << name
+                  << " ERRMSG=" << errmsg
+                  << " MINIMUM_STRING_LENGTH=" << stringInfo.minLength
+                  << " MAXIMUM_STRING_LENGTH=" << stringInfo.maxLength
+                  << " DEFAULT_STRING_LENGTH=" << stringInfo.defLength
+                  << " DEFAULT_STRING=" << stringInfo.defString << "\n";
+        throw std::invalid_argument("Wrong field for string attribute");
+    }
+}
+
+void BIOSStringAttribute::setAttrValueOnDbus(
+    const pldm_bios_attr_val_table_entry* attrValueEntry,
+    const pldm_bios_attr_table_entry*, const BIOSStringTable&)
+{
+    if (readOnly)
+    {
+        return;
+    }
+
+    PropertyValue value =
+        table::attribute_value::decodeStringEntry(attrValueEntry);
+    dbusHandler->setDbusProperty(*dBusMap, value);
+}
+
+std::string BIOSStringAttribute::getAttrValue()
+{
+    if (readOnly)
+    {
+        return stringInfo.defString;
+    }
+    try
+    {
+        return dbusHandler->getDbusProperty<std::string>(
+            dBusMap->objectPath.c_str(), dBusMap->propertyName.c_str(),
+            dBusMap->interface.c_str());
+    }
+    catch (const std::exception& e)
+    {
+        std::cerr << "Get String Attribute Value Error: AttributeName = "
+                  << name << std::endl;
+        return stringInfo.defString;
+    }
+}
+
+void BIOSStringAttribute::constructEntry(const BIOSStringTable& stringTable,
+                                         Table& attrTable,
+                                         Table& attrValueTable)
+{
+    pldm_bios_table_attr_entry_string_info info = {
+        stringTable.findHandle(name), readOnly,
+        stringInfo.stringType,        stringInfo.minLength,
+        stringInfo.maxLength,         stringInfo.defLength,
+        stringInfo.defString.data(),
+    };
+
+    auto attrTableEntry =
+        table::attribute::constructStringEntry(attrTable, &info);
+    auto [attrHandle, attrType, _] =
+        table::attribute::decodeHeader(attrTableEntry);
+    auto currStr = getAttrValue();
+    table::attribute_value::constructStringEntry(attrValueTable, attrHandle,
+                                                 attrType, currStr);
+}
+
+} // namespace bios
+} // namespace responder
+} // namespace pldm