libpldmresponder: construct BMC PDRs lazily

BMC's PDRs were constructed when the pldm daemon starts up. However, the
pldm daemon might rely on other services to start in order to create
PDRs. To solve this problem, construct the PDRs when the first GetPDR
call comes in. From a practical timing perspective, this ensures the
pldm daemon and the services it needs to build PDRs are all up and
running.

An alternative would be to add 'After' directives in pldm's service
file, however that can get complex when some of the services are not
present on all platforms.

Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
Change-Id: Id6ac9fd47b293f7e84e3837432b32b0e3c3f8a5a
diff --git a/libpldmresponder/platform.hpp b/libpldmresponder/platform.hpp
index c79a2ed..1ff6bc4 100644
--- a/libpldmresponder/platform.hpp
+++ b/libpldmresponder/platform.hpp
@@ -56,16 +56,21 @@
 class Handler : public CmdHandler
 {
   public:
-    Handler(const pldm::utils::DBusHandler& dBusIntf,
+    Handler(const pldm::utils::DBusHandler* dBusIntf,
             const std::string& pdrJsonsDir, const std::string& eventsJsonsDir,
             pldm_pdr* repo, HostPDRHandler* hostPDRHandler,
-            fru::Handler* fruHandler,
+            fru::Handler* fruHandler, bool buildPDRLazily = false,
             const std::optional<EventMap>& addOnHandlersMap = std::nullopt) :
         pdrRepo(repo),
         hostPDRHandler(hostPDRHandler), stateSensorHandler(eventsJsonsDir),
-        fruHandler(fruHandler)
+        fruHandler(fruHandler), dBusIntf(dBusIntf), pdrJsonsDir(pdrJsonsDir),
+        pdrCreated(false)
     {
-        generate(dBusIntf, pdrJsonsDir, pdrRepo);
+        if (!buildPDRLazily)
+        {
+            generate(*dBusIntf, pdrJsonsDir, pdrRepo);
+            pdrCreated = true;
+        }
 
         handlers.emplace(PLDM_GET_PDR,
                          [this](const pldm_msg* request, size_t payloadLength) {
@@ -404,6 +409,9 @@
     HostPDRHandler* hostPDRHandler;
     events::StateSensorHandler stateSensorHandler;
     fru::Handler* fruHandler;
+    const pldm::utils::DBusHandler* dBusIntf;
+    std::string pdrJsonsDir;
+    bool pdrCreated;
 };
 
 } // namespace platform