BIOS: Modify to support BIOS string type

Modify the definitions of constructAttrTable() to make it more convenient
to construct different type data together.

Change-Id: I607966eeaac888a9bd863cedcc80ce819f5b34d8
Signed-off-by: Carol Wang <wangkair@cn.ibm.com>
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index c88a25c..6669688 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -19,7 +19,6 @@
 
 using namespace pldm::responder::bios;
 using namespace bios_parser;
-using namespace bios_parser::bios_enum;
 
 namespace pldm
 {
@@ -28,6 +27,7 @@
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 using EpochTimeUS = uint64_t;
 using BIOSTableRow = std::vector<uint8_t>;
+using BIOSJsonName = std::string;
 
 constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
 constexpr auto padChksumMax = 7;
@@ -316,6 +316,8 @@
 namespace bios_type_enum
 {
 
+using namespace bios_parser::bios_enum;
+
 /** @brief Find the indices  into the array of the possible values of string
  *  handles for the current values.This is used in attribute value table
  *
@@ -392,15 +394,14 @@
  *         Enumeration ReadOnly
  *  @param[in] BIOSStringTable - the string table
  *  @param[in] biosJsonDir - path where the BIOS json files are present
+ *  @param[in,out] attributeTable - the attribute table
  *
- *  @return - Table - the attribute eenumeration table
  */
-Table constructAttrTable(const BIOSTable& BIOSStringTable,
-                         const char* biosJsonDir)
+void constructAttrTable(const BIOSTable& BIOSStringTable,
+                        const char* biosJsonDir, Table& attributeTable)
 {
     setupValueLookup(biosJsonDir);
     const auto& attributeMap = getValues();
-    Table attributeTable;
     StringHandle strHandle;
 
     for (const auto& [key, value] : attributeMap)
@@ -471,8 +472,6 @@
         std::move(enumAttrTable.begin(), enumAttrTable.end(),
                   std::back_inserter(attributeTable));
     }
-
-    return attributeTable;
 }
 
 /** @brief Construct the attibute value table for BIOS type Enumeration and
@@ -590,6 +589,29 @@
 
 } // end namespace bios_type_enum
 
+namespace bios_type_string
+{
+/** @brief Construct the attibute table for BIOS type String and
+ *         String ReadOnly
+ *  @param[in] BIOSStringTable - the string table
+ *  @param[in] biosJsonDir - path where the BIOS json files are present
+ *  @param[in,out] attributeTable - the attribute table
+ *
+ */
+void constructAttrTable(const BIOSTable& /*BIOSStringTable*/,
+                        const char* /*biosJsonDir*/, Table& /*attributeTable*/)
+{
+    // TODO
+}
+} // end namespace bios_type_string
+
+using typeHandler =
+    std::function<void(const BIOSTable& BIOSStringTable,
+                       const char* biosJsonDir, Table& attributeTable)>;
+std::map<BIOSJsonName, typeHandler> attrTypeHandlers{
+    {bios_parser::bIOSEnumJson, bios_type_enum::constructAttrTable},
+    {bios_parser::bIOSStrJson, bios_type_string::constructAttrTable}};
+
 /** @brief Construct the BIOS attribute table
  *
  *  @param[in] BIOSAttributeTable - the attribute table
@@ -611,12 +633,29 @@
     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
     uint32_t nxtTransferHandle = 0;
     uint8_t transferFlag = PLDM_START_AND_END;
-    size_t respPayloadLength{};
 
     if (BIOSAttributeTable.isEmpty())
     { // no persisted table, constructing fresh table and response
-        auto attributeTable =
-            bios_type_enum::constructAttrTable(BIOSStringTable, biosJsonDir);
+        Table attributeTable;
+        fs::path dir(biosJsonDir);
+
+        for (auto it = attrTypeHandlers.begin(); it != attrTypeHandlers.end();
+             it++)
+        {
+            fs::path file = dir / it->first;
+            if (fs::exists(file))
+            {
+                it->second(BIOSStringTable, biosJsonDir, attributeTable);
+            }
+        }
+
+        if (attributeTable.empty())
+        { // no available json file is found
+            encode_get_bios_table_resp(instanceID, PLDM_BIOS_TABLE_UNAVAILABLE,
+                                       nxtTransferHandle, transferFlag, nullptr,
+                                       response.size(), responsePtr);
+            return response;
+        }
 
         // calculate pad
         uint8_t padSize = utils::getNumPadBytes(attributeTable.size());
@@ -643,16 +682,14 @@
                         PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES +
                         attributeTable.size());
         responsePtr = reinterpret_cast<pldm_msg*>(response.data());
-        respPayloadLength = response.size();
         encode_get_bios_table_resp(instanceID, PLDM_SUCCESS, nxtTransferHandle,
                                    transferFlag, attributeTable.data(),
-                                   respPayloadLength, responsePtr);
+                                   response.size(), responsePtr);
     }
     else
     { // persisted table present, constructing response
-        respPayloadLength = response.size();
         encode_get_bios_table_resp(instanceID, PLDM_SUCCESS, nxtTransferHandle,
-                                   transferFlag, nullptr, respPayloadLength,
+                                   transferFlag, nullptr, response.size(),
                                    responsePtr); // filling up the header here
         BIOSAttributeTable.load(response);
     }
diff --git a/libpldmresponder/bios_parser.cpp b/libpldmresponder/bios_parser.cpp
index fa704e6..3933ad4 100644
--- a/libpldmresponder/bios_parser.cpp
+++ b/libpldmresponder/bios_parser.cpp
@@ -14,7 +14,6 @@
 using Json = nlohmann::json;
 namespace fs = std::filesystem;
 using namespace phosphor::logging;
-constexpr auto bIOSEnumJson = "enum_attrs.json";
 
 namespace bios_enum
 {
diff --git a/libpldmresponder/bios_parser.hpp b/libpldmresponder/bios_parser.hpp
index 0e21991..1f13f94 100644
--- a/libpldmresponder/bios_parser.hpp
+++ b/libpldmresponder/bios_parser.hpp
@@ -32,6 +32,8 @@
 {
 
 using Strings = std::vector<std::string>;
+inline constexpr auto bIOSEnumJson = "enum_attrs.json";
+inline constexpr auto bIOSStrJson = "string_attrs.json";
 
 /** @brief Parse every BIOS configuration JSON files in the directory path
  *         and populate all the attribute names and all the preconfigured