pldm: Adding support to find system type

This commit adds support to find the system type by checking the JSON
directory presence when entity manager sends the system names. Once
system type is set then PLDM continues the further operations like pdr
generation, system specific bios etc...

Testing:
Tested on real hardware

Change-Id: I3b0a7725a72cd3a3c8fe0a4c6ff6b38510a2a2ea
Signed-off-by: Kamalkumar Patel <kamalkumar.patel@ibm.com>
diff --git a/libpldmresponder/platform_config.cpp b/libpldmresponder/platform_config.cpp
index 8bc6b06..8d7fe79 100644
--- a/libpldmresponder/platform_config.cpp
+++ b/libpldmresponder/platform_config.cpp
@@ -39,11 +39,14 @@
     auto names =
         std::get<pldm::utils::Interfaces>(properties.at(namesProperty));
 
-    std::string systemType;
     if (!names.empty())
     {
-        // get only the first system type
-        systemType = names.front();
+        std::optional<std::string> sysType = getSysSpecificJsonDir(sysDirPath,
+                                                                   names);
+        if (sysType.has_value())
+        {
+            systemType = sysType.value();
+        }
         if (sysTypeCallback)
         {
             sysTypeCallback(systemType, true);
@@ -103,9 +106,14 @@
 
                     if (!systemList.empty())
                     {
-                        systemType = systemList.at(0);
+                        std::optional<std::string> sysType =
+                            getSysSpecificJsonDir(sysDirPath, systemList);
                         // once systemtype received,then resetting a callback
                         systemCompatibleMatchCallBack.reset();
+                        if (sysType.has_value())
+                        {
+                            systemType = sysType.value();
+                        }
                         return fs::path{systemType};
                     }
                 }
@@ -128,6 +136,37 @@
     return std::nullopt;
 }
 
+std::optional<std::string>
+    Handler::getSysSpecificJsonDir(const fs::path& dirPath,
+                                   const std::vector<std::string>& dirNames)
+{
+    // The current setup assumes that the BIOS and PDR configurations always
+    // come from the same system type. If, in the future, we need to use BIOS
+    // and PDR configurations from different system types, we should create
+    // separate system type folders for each and update the logic to support
+    // this.
+
+    if (dirPath.empty())
+    {
+        return std::nullopt;
+    }
+
+    for (const auto& dirEntry : std::filesystem::directory_iterator{dirPath})
+    {
+        if (dirEntry.is_directory())
+        {
+            const auto sysDir = dirEntry.path().filename().string();
+            if (std::find(dirNames.begin(), dirNames.end(), sysDir) !=
+                dirNames.end())
+            {
+                return sysDir;
+            }
+        }
+    }
+
+    return std::nullopt;
+}
+
 void Handler::registerSystemTypeCallback(SystemTypeCallback callback)
 {
     sysTypeCallback = callback;