More changes to support JEDEC format VPD

Change-Id: I7d8e4b0508bc1f92c3f6fd1a924a153ebe23f17e
Signed-off-by: jinuthomas <jinu.joy.thomas@in.ibm.com>
diff --git a/ibm_vpd_app.cpp b/ibm_vpd_app.cpp
index 57109b6..94b7f55 100644
--- a/ibm_vpd_app.cpp
+++ b/ibm_vpd_app.cpp
@@ -240,31 +240,34 @@
         }
         if constexpr (is_same<T, KeywordVpdMap>::value)
         {
-            if (get_if<Binary>(&kwVal.second))
+            if (auto keywordValue = get_if<Binary>(&kwVal.second))
             {
-                Binary vec(get_if<Binary>(&kwVal.second)->begin(),
-                           get_if<Binary>(&kwVal.second)->end());
+                Binary vec((*keywordValue).begin(), (*keywordValue).end());
                 prop.emplace(move(kw), move(vec));
             }
-            else
+            else if (auto keywordValue = get_if<std::string>(&kwVal.second))
+            {
+                Binary vec((*keywordValue).begin(), (*keywordValue).end());
+                prop.emplace(move(kw), move(vec));
+            }
+            else if (auto keywordValue = get_if<size_t>(&kwVal.second))
             {
                 if (kw == "MemorySizeInKB")
                 {
                     inventory::PropertyMap memProp;
-                    auto memVal = get_if<size_t>(&kwVal.second);
-                    if (memVal)
-                    {
-                        memProp.emplace(move(kw),
-                                        ((*memVal) * CONVERT_MB_TO_KB));
-                        interfaces.emplace(
-                            "xyz.openbmc_project.Inventory.Item.Dimm",
-                            move(memProp));
-                    }
-                    else
-                    {
-                        cerr << "MemorySizeInKB value not found in vpd map\n";
-                    }
+                    memProp.emplace(move(kw), ((*keywordValue)));
+                    interfaces.emplace(
+                        "xyz.openbmc_project.Inventory.Item.Dimm",
+                        move(memProp));
                 }
+                else
+                {
+                    cerr << "Unknown Keyword[" << kw << "] found ";
+                }
+            }
+            else
+            {
+                cerr << "Unknown Variant found ";
             }
         }
         else
@@ -362,10 +365,7 @@
                 {
                     if (!kw.empty() && vpdMap.count(kw))
                     {
-                        auto kwValue = get_if<Binary>(&vpdMap.at(kw));
-                        auto uintValue = get_if<size_t>(&vpdMap.at(kw));
-
-                        if (kwValue)
+                        if (auto kwValue = get_if<Binary>(&vpdMap.at(kw)))
                         {
                             auto prop =
                                 string((*kwValue).begin(), (*kwValue).end());
@@ -374,10 +374,26 @@
 
                             props.emplace(busProp, encoded);
                         }
-                        else if (uintValue)
+                        else if (auto kwValue =
+                                     get_if<std::string>(&vpdMap.at(kw)))
+                        {
+                            auto prop =
+                                string((*kwValue).begin(), (*kwValue).end());
+
+                            auto encoded = encodeKeyword(prop, encoding);
+
+                            props.emplace(busProp, encoded);
+                        }
+                        else if (auto uintValue =
+                                     get_if<size_t>(&vpdMap.at(kw)))
                         {
                             props.emplace(busProp, *uintValue);
                         }
+                        else
+                        {
+                            std::cerr << " Unknown Keyword [" << kw
+                                      << "] Encountered";
+                        }
                     }
                 }
             }
@@ -492,17 +508,17 @@
             {
                 // Now bind the device
                 string bind = json["frus"][file].at(0).value("devAddress", "");
-                cout << "Binding device " << bind << endl;
+                cout << "Binding device " << bind << std::endl;
                 string bindCmd = string("echo \"") + bind +
                                  string("\" > /sys/bus/i2c/drivers/at24/bind");
-                cout << bindCmd << endl;
+                cout << bindCmd << std::endl;
                 executeCmd(bindCmd);
 
                 // Check if device showed up (test for file)
                 if (!fs::exists(file))
                 {
                     cerr << "EEPROM " << file
-                         << " does not exist. Take failure action" << endl;
+                         << " does not exist. Take failure action" << std::endl;
                     // If not, then take failure postAction
                     executePostFailAction(json, file);
                 }
@@ -513,7 +529,8 @@
                 cerr << "VPD inventory JSON missing basic informations of "
                         "preAction "
                         "for this FRU : ["
-                     << file << "]. Executing executePostFailAction." << endl;
+                     << file << "]. Executing executePostFailAction."
+                     << std::endl;
 
                 // Take failure postAction
                 executePostFailAction(json, file);
@@ -1422,7 +1439,8 @@
         if (!fs::exists(file))
         {
             cout << "Device path: " << file
-                 << " does not exist. Spurious udev event? Exiting." << endl;
+                 << " does not exist. Spurious udev event? Exiting."
+                 << std::endl;
             return 0;
         }
 
@@ -1446,7 +1464,7 @@
             if ("xyz.openbmc_project.State.Chassis.PowerState.On" ==
                 getPowerState())
             {
-                cout << "This VPD cannot be read when power is ON" << endl;
+                cout << "This VPD cannot be read when power is ON" << std::endl;
                 return 0;
             }
         }
@@ -1454,7 +1472,7 @@
         // Check if this VPD should be recollected at all
         if (!needsRecollection(js, file))
         {
-            cout << "Skip VPD recollection for: " << file << endl;
+            cout << "Skip VPD recollection for: " << file << std::endl;
             return 0;
         }
 
diff --git a/vpd-parser/isdimm_vpd_parser.cpp b/vpd-parser/isdimm_vpd_parser.cpp
index c7d4a52..334a2a3 100644
--- a/vpd-parser/isdimm_vpd_parser.cpp
+++ b/vpd-parser/isdimm_vpd_parser.cpp
@@ -139,7 +139,7 @@
             iterator[SPD_JEDEC_DDR4_SDRAM_ADDR_OFFSET],
             iterator[SPD_JEDEC_DDR4_DRAM_PRI_PACKAGE_OFFSET],
             iterator[SPD_JEDEC_DDR4_DRAM_MODULE_ORG_OFFSET] & 0x0F);
-    std::string partNumber(tmpPN, sizeof(tmpPN));
+    std::string partNumber(tmpPN, sizeof(tmpPN) - 1);
     return partNumber;
 }
 
@@ -153,7 +153,7 @@
             iterator[SPD_JEDEC_DDR4_SN_BYTE1_OFFSET],
             iterator[SPD_JEDEC_DDR4_SN_BYTE2_OFFSET],
             iterator[SPD_JEDEC_DDR4_SN_BYTE3_OFFSET]);
-    std::string serialNumber(tmpSN, sizeof(tmpSN));
+    std::string serialNumber(tmpSN, sizeof(tmpSN) - 1);
     return serialNumber;
 }
 
@@ -270,55 +270,45 @@
     if ((iterator[constants::SPD_BYTE_2] & constants::SPD_BYTE_MASK) ==
         constants::SPD_DRAM_TYPE_DDR5)
     {
-        auto dimmSize = getDDR5DimmCapacity(iterator);
+        size_t dimmSize = getDDR5DimmCapacity(iterator);
         if (!dimmSize)
         {
             std::cerr << "Error: Calculated dimm size is 0.";
         }
-        else if (dimmSize < constants::CONVERT_MB_TO_KB)
-        {
-            keywordValueMap.emplace("MemorySizeInMB", dimmSize);
-        }
         else
         {
-            size_t dimmCapacityInGB = dimmSize / constants::CONVERT_MB_TO_KB;
-            keywordValueMap.emplace("MemorySizeInGB", dimmCapacityInGB);
+            keywordValueMap.emplace("MemorySizeInKB", dimmSize);
         }
         auto partNumber = getDDR5PartNumber(iterator);
-        keywordValueMap.emplace("PN", move(partNumber));
         auto fruNumber = getDDR5FruNumber(partNumber);
         keywordValueMap.emplace("FN", move(fruNumber));
         auto serialNumber = getDDR5SerialNumber(iterator);
         keywordValueMap.emplace("SN", move(serialNumber));
         auto ccin = getDDR5CCIN(partNumber);
         keywordValueMap.emplace("CC", move(ccin));
+        keywordValueMap.emplace("PN", move(partNumber));
     }
     else if ((iterator[constants::SPD_BYTE_2] & constants::SPD_BYTE_MASK) ==
              constants::SPD_DRAM_TYPE_DDR4)
     {
-        auto dimmSize = getDDR4DimmCapacity(iterator);
+        size_t dimmSize = getDDR4DimmCapacity(iterator);
         if (!dimmSize)
         {
             std::cerr << "Error: Calculated dimm size is 0.";
         }
-        else if (dimmSize < constants::CONVERT_MB_TO_KB)
-        {
-            keywordValueMap.emplace("MemorySizeInMB", dimmSize);
-        }
         else
         {
-            size_t dimmCapacityInGB = dimmSize / constants::CONVERT_MB_TO_KB;
-            keywordValueMap.emplace("MemorySizeInGB", dimmCapacityInGB);
+            keywordValueMap.emplace("MemorySizeInKB",
+                                    (dimmSize * constants::CONVERT_MB_TO_KB));
         }
-        size_t dimmCapacityInGB = dimmSize / constants::CONVERT_MB_TO_KB;
-        keywordValueMap.emplace("MemorySizeInGB", dimmCapacityInGB);
+
         auto partNumber = getDDR4PartNumber(iterator);
-        keywordValueMap.emplace("PN", move(partNumber));
         auto fruNumber = getDDR4FruNumber(partNumber);
-        keywordValueMap.emplace("FN", move(fruNumber));
         auto serialNumber = getDDR4SerialNumber(iterator);
-        keywordValueMap.emplace("SN", move(serialNumber));
         auto ccin = getDDR4CCIN(partNumber);
+        keywordValueMap.emplace("PN", move(partNumber));
+        keywordValueMap.emplace("FN", move(fruNumber));
+        keywordValueMap.emplace("SN", move(serialNumber));
         keywordValueMap.emplace("CC", move(ccin));
     }
     return keywordValueMap;