BIOS: Implement attribute table with type string

Construct attribute table with type string based on string table.

Tested:
Following are the tables constructed from the sample json file present at
"test/bios_jsons/

$ hexdump -C stringTable
00000000  00 00 07 00 41 6c 6c 6f  77 65 64 01 00 10 00 43  |....Allowed....C|
00000010  6f 64 65 55 70 64 61 74  65 50 6f 6c 69 63 79 02  |odeUpdatePolicy.|
00000020  00 0a 00 43 6f 6e 63 75  72 72 65 6e 74 03 00 0a  |...Concurrent...|
00000030  00 44 69 73 72 75 70 74  69 76 65 04 00 0a 00 46  |.Disruptive....F|
00000040  57 42 6f 6f 74 53 69 64  65 05 00 0f 00 48 4d 43  |WBootSide....HMC|
00000050  4d 61 6e 61 67 65 64 53  74 61 74 65 06 00 10 00  |ManagedState....|
00000060  49 6e 62 61 6e 64 43 6f  64 65 55 70 64 61 74 65  |InbandCodeUpdate|
00000070  07 00 0a 00 4e 6f 74 41  6c 6c 6f 77 65 64 08 00  |....NotAllowed..|
00000080  03 00 4f 66 66 09 00 02  00 4f 6e 0a 00 04 00 50  |..Off....On....P|
00000090  65 72 6d 0b 00 04 00 54  65 6d 70 0c 00 0c 00 73  |erm....Temp....s|
000000a0  74 72 5f 65 78 61 6d 70  6c 65 31 0d 00 0c 00 73  |tr_example1....s|
000000b0  74 72 5f 65 78 61 6d 70  6c 65 32 0e 00 0c 00 73  |tr_example2....s|
000000c0  74 72 5f 65 78 61 6d 70  6c 65 33 00 6c 11 89 d4  |tr_example3.l...|

$ hexdump -C attributeTable
00000000  00 00 00 01 00 02 02 00  03 00 01 00 01 00 00 04  |................|
00000010  00 02 0a 00 0b 00 01 00  02 00 00 05 00 02 08 00  |................|
00000020  09 00 01 01 03 00 00 06  00 02 00 00 07 00 01 00  |................|
00000030  04 00 01 0c 00 01 01 00  64 00 03 00 61 62 63 05  |........d...abc.|
00000040  00 01 0d 00 02 00 00 64  00 00 00 06 00 01 0e 00  |.......d........|
00000050  00 01 00 64 00 02 00 65  66 00 00 00 72 ef 0c 2a  |...d...ef...r..*|

Change-Id: I272c59b96b5aa14571d01eeeaccd504ca7ac168b
Signed-off-by: Carol Wang <wangkair@cn.ibm.com>
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index 6669688..90d86c8 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -591,6 +591,9 @@
 
 namespace bios_type_string
 {
+
+using namespace bios_parser::bios_string;
+
 /** @brief Construct the attibute table for BIOS type String and
  *         String ReadOnly
  *  @param[in] BIOSStringTable - the string table
@@ -598,10 +601,63 @@
  *  @param[in,out] attributeTable - the attribute table
  *
  */
-void constructAttrTable(const BIOSTable& /*BIOSStringTable*/,
-                        const char* /*biosJsonDir*/, Table& /*attributeTable*/)
+void constructAttrTable(const BIOSTable& BIOSStringTable,
+                        const char* biosJsonDir, Table& attributeTable)
 {
-    // TODO
+    auto rc = setupValueLookup(biosJsonDir);
+    if (rc == -1)
+    {
+        log<level::ERR>("Failed to parse entries in Json file");
+        return;
+    }
+    const auto& attributeMap = getValues();
+    StringHandle strHandle;
+
+    for (const auto& [key, value] : attributeMap)
+    {
+        try
+        {
+            strHandle = findStringHandle(key, BIOSStringTable);
+        }
+        catch (InternalFailure& e)
+        {
+            log<level::ERR>("Could not find handle for BIOS string",
+                            entry("ATTRIBUTE=%s", key.c_str()));
+            continue;
+        }
+
+        const auto& [type, strType, minStrLen, maxStrLen, defaultStrLen,
+                     defaultStr] = value;
+        uint8_t typeOfAttr =
+            type ? PLDM_BIOS_STRING_READ_ONLY : PLDM_BIOS_STRING;
+
+        BIOSTableRow stringAttrTable(bios_parser::bios_string::attrTableSize +
+                                     defaultStr.size());
+        BIOSTableRow::iterator it = stringAttrTable.begin();
+        auto attrPtr = reinterpret_cast<struct pldm_bios_attr_table_entry*>(
+            stringAttrTable.data());
+        attrPtr->attr_handle = nextAttributeHandle();
+        attrPtr->attr_type = typeOfAttr;
+        attrPtr->string_handle = strHandle;
+
+        std::advance(it, (sizeof(struct pldm_bios_attr_table_entry) - 1));
+        std::copy_n(&strType, sizeof(uint8_t), it);
+        std::advance(it, sizeof(uint8_t));
+        std::copy_n(reinterpret_cast<const uint8_t*>(&minStrLen),
+                    sizeof(uint16_t), it);
+        std::advance(it, sizeof(uint16_t));
+        std::copy_n(reinterpret_cast<const uint8_t*>(&maxStrLen),
+                    sizeof(uint16_t), it);
+        std::advance(it, sizeof(uint16_t));
+        std::copy_n(reinterpret_cast<const uint8_t*>(&defaultStrLen),
+                    sizeof(uint16_t), it);
+        std::advance(it, sizeof(uint16_t));
+        std::copy_n(defaultStr.data(), defaultStr.size(), it);
+        std::advance(it, defaultStr.size());
+
+        attributeTable.insert(attributeTable.end(), stringAttrTable.begin(),
+                              stringAttrTable.end());
+    }
 }
 } // end namespace bios_type_string