Add HostPDRHandler class

HostPDRHandler has an API to fetch PDRs from the host.

The class HostPDRHandler has a function fetchPDR which basically calls
GetPDR on the host using libpldm's pldm_send_recv API.

The retrieved PDRs are stored in the BMCs primary PDR repo.

Change-Id: Ifd727316caf37d49f17e117b32ee105f6d941e0e
Signed-off-by: Pavithra Barithaya <pbaritha@in.ibm.com>
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/pldmd.cpp b/pldmd.cpp
index 501de5a..030fcb1 100644
--- a/pldmd.cpp
+++ b/pldmd.cpp
@@ -1,4 +1,5 @@
 #include "dbus_impl_requester.hpp"
+#include "host_pdr_handler.hpp"
 #include "invoker.hpp"
 #include "libpldmresponder/base.hpp"
 #include "libpldmresponder/bios.hpp"
@@ -17,6 +18,7 @@
 
 #include <cstdio>
 #include <cstring>
+#include <fstream>
 #include <iomanip>
 #include <iostream>
 #include <iterator>
@@ -146,26 +148,6 @@
             break;
     }
 
-    std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
-        pldm_pdr_init(), pldm_pdr_destroy);
-    std::unique_ptr<pldm_entity_association_tree,
-                    decltype(&pldm_entity_association_tree_destroy)>
-        entityTree(pldm_entity_association_tree_init(),
-                   pldm_entity_association_tree_destroy);
-
-    Invoker invoker{};
-    invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
-    invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
-    invoker.registerHandler(PLDM_PLATFORM, std::make_unique<platform::Handler>(
-                                               PDR_JSONS_DIR, pdrRepo.get()));
-    invoker.registerHandler(
-        PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(),
-                                                 entityTree.get()));
-
-#ifdef OEM_IBM
-    invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
-#endif
-
     /* Create local socket. */
     int returnCode = 0;
     int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
@@ -176,6 +158,37 @@
         exit(EXIT_FAILURE);
     }
 
+    auto event = Event::get_default();
+    std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
+        pldm_pdr_init(), pldm_pdr_destroy);
+    std::unique_ptr<pldm_entity_association_tree,
+                    decltype(&pldm_entity_association_tree_destroy)>
+        entityTree(pldm_entity_association_tree_init(),
+                   pldm_entity_association_tree_destroy);
+    auto& bus = pldm::utils::DBusHandler::getBus();
+    dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
+    std::unique_ptr<HostPDRHandler> hostPDRHandler;
+    auto hostEID = pldm::utils::readHostEID();
+    if (hostEID)
+    {
+        hostPDRHandler = std::make_unique<HostPDRHandler>(
+            sockfd, hostEID, event, pdrRepo.get(), dbusImplReq);
+    }
+
+    Invoker invoker{};
+    invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>());
+    invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>());
+    invoker.registerHandler(
+        PLDM_PLATFORM, std::make_unique<platform::Handler>(
+                           PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get()));
+    invoker.registerHandler(
+        PLDM_FRU, std::make_unique<fru::Handler>(FRU_JSONS_DIR, pdrRepo.get(),
+                                                 entityTree.get()));
+
+#ifdef OEM_IBM
+    invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>());
+#endif
+
     pldm::utils::CustomFD socketFd(sockfd);
 
     struct sockaddr_un addr
@@ -203,8 +216,6 @@
         exit(EXIT_FAILURE);
     }
 
-    auto& bus = pldm::utils::DBusHandler::getBus();
-    dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm");
     auto callback = [verbose, &invoker, &dbusImplReq](IO& /*io*/, int fd,
                                                       uint32_t revents) {
         if (!(revents & EPOLLIN))
@@ -292,7 +303,6 @@
         }
     };
 
-    auto event = Event::get_default();
     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
     bus.request_name("xyz.openbmc_project.PLDM");
     IO io(event, socketFd(), EPOLLIN, std::move(callback));