Refactor the platform event message handler code

This commit makes slight code change to use some functions both
in BMC requester and responder flow.

Tested : Using pldmtool platformEventMessage is sent and D-Bus
property is verified.

Change-Id: Id433f136ad4c7be0d94817deed1d6b60df36b3e4
Signed-off-by: Pavithra Barithaya <pavithra.b@ibm.com>
diff --git a/host-bmc/host_pdr_handler.cpp b/host-bmc/host_pdr_handler.cpp
index 31065a4..2e8a0a3 100644
--- a/host-bmc/host_pdr_handler.cpp
+++ b/host-bmc/host_pdr_handler.cpp
@@ -23,10 +23,12 @@
 
 HostPDRHandler::HostPDRHandler(int mctp_fd, uint8_t mctp_eid,
                                sdeventplus::Event& event, pldm_pdr* repo,
+                               const std::string& eventsJsonsDir,
                                pldm_entity_association_tree* entityTree,
                                Requester& requester) :
     mctp_fd(mctp_fd),
-    mctp_eid(mctp_eid), event(event), repo(repo), entityTree(entityTree),
+    mctp_eid(mctp_eid), event(event), repo(repo),
+    stateSensorHandler(eventsJsonsDir), entityTree(entityTree),
     requester(requester)
 {
     fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson);
@@ -234,6 +236,18 @@
     }
 }
 
+int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry,
+                                           pdr::EventState state)
+{
+    auto rc = stateSensorHandler.eventAction(entry, state);
+    if (rc != PLDM_SUCCESS)
+    {
+        std::cerr << "Failed to fetch and update D-bus property, rc = " << rc
+                  << std::endl;
+        return rc;
+    }
+    return PLDM_SUCCESS;
+}
 bool HostPDRHandler::getParent(EntityType type, pldm_entity& parent)
 {
     auto found = parents.find(type);
diff --git a/host-bmc/host_pdr_handler.hpp b/host-bmc/host_pdr_handler.hpp
index 9377cb6..3c998a2 100644
--- a/host-bmc/host_pdr_handler.hpp
+++ b/host-bmc/host_pdr_handler.hpp
@@ -5,6 +5,7 @@
 
 #include "common/types.hpp"
 #include "common/utils.hpp"
+#include "libpldmresponder/event_parser.hpp"
 #include "libpldmresponder/pdr_utils.hpp"
 #include "pldmd/dbus_impl_requester.hpp"
 
@@ -17,6 +18,7 @@
 #include <vector>
 
 using namespace pldm::dbus_api;
+using namespace pldm::responder::events;
 
 namespace pldm
 {
@@ -79,11 +81,13 @@
      *  @param[in] mctp_eid - MCTP EID of host firmware
      *  @param[in] event - reference of main event loop of pldmd
      *  @param[in] repo - pointer to BMC's primary PDR repo
+     *  @param[in] eventsJsonDir - directory path which has the config JSONs
      *  @param[in] tree - pointer to BMC's entity association tree
      *  @param[in] requester - reference to Requester object
      */
     explicit HostPDRHandler(int mctp_fd, uint8_t mctp_eid,
                             sdeventplus::Event& event, pldm_pdr* repo,
+                            const std::string& eventsJsonsDir,
                             pldm_entity_association_tree* entityTree,
                             Requester& requester);
 
@@ -115,6 +119,16 @@
         return sensorMap.at(entry);
     }
 
+    /** @brief Handles state sensor event
+     *
+     *  @param[in] entry - state sensor entry
+     *  @param[in] state - event state
+     *
+     *  @return PLDM completion code
+     */
+    int handleStateSensorEvent(const StateSensorEntry& entry,
+                               pdr::EventState state);
+
     /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
      *         structure
      *
@@ -157,6 +171,8 @@
     sdeventplus::Event& event;
     /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
     pldm_pdr* repo;
+
+    StateSensorHandler stateSensorHandler;
     /** @brief Pointer to BMC's entity association tree */
     pldm_entity_association_tree* entityTree;
     /** @brief reference to Requester object, primarily used to access API to
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index ecbc121..d2d1d6e 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -399,7 +399,8 @@
         const auto& [containerId, entityType, entityInstance] = entityInfo;
         events::StateSensorEntry stateSensorEntry{containerId, entityType,
                                                   entityInstance, sensorOffset};
-        return stateSensorHandler.eventAction(stateSensorEntry, eventState);
+        return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
+                                                      eventState);
     }
     else
     {
diff --git a/libpldmresponder/platform.hpp b/libpldmresponder/platform.hpp
index 3eda7cb..ef2434d 100644
--- a/libpldmresponder/platform.hpp
+++ b/libpldmresponder/platform.hpp
@@ -52,13 +52,13 @@
 {
   public:
     Handler(const pldm::utils::DBusHandler* dBusIntf,
-            const std::string& pdrJsonsDir, const std::string& eventsJsonsDir,
-            pldm_pdr* repo, HostPDRHandler* hostPDRHandler,
+            const std::string& pdrJsonsDir, pldm_pdr* repo,
+            HostPDRHandler* hostPDRHandler,
             DbusToPLDMEvent* dbusToPLDMEventHandler, fru::Handler* fruHandler,
             bool buildPDRLazily = false,
             const std::optional<EventMap>& addOnHandlersMap = std::nullopt) :
         pdrRepo(repo),
-        hostPDRHandler(hostPDRHandler), stateSensorHandler(eventsJsonsDir),
+        hostPDRHandler(hostPDRHandler),
         dbusToPLDMEventHandler(dbusToPLDMEventHandler), fruHandler(fruHandler),
         dBusIntf(dBusIntf), pdrJsonsDir(pdrJsonsDir), pdrCreated(false)
     {
@@ -438,7 +438,6 @@
     DbusObjMaps effecterDbusObjMaps{};
     DbusObjMaps sensorDbusObjMaps{};
     HostPDRHandler* hostPDRHandler;
-    events::StateSensorHandler stateSensorHandler;
     DbusToPLDMEvent* dbusToPLDMEventHandler;
     fru::Handler* fruHandler;
     const pldm::utils::DBusHandler* dBusIntf;
diff --git a/pldmd/pldmd.cpp b/pldmd/pldmd.cpp
index 1495db4..c99ab18 100644
--- a/pldmd/pldmd.cpp
+++ b/pldmd/pldmd.cpp
@@ -182,8 +182,8 @@
     if (hostEID)
     {
         hostPDRHandler = std::make_unique<HostPDRHandler>(
-            sockfd, hostEID, event, pdrRepo.get(), entityTree.get(),
-            dbusImplReq);
+            sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR,
+            entityTree.get(), dbusImplReq);
         hostEffecterParser =
             std::make_unique<pldm::host_effecters::HostEffecterParser>(
                 &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(),
@@ -201,12 +201,11 @@
     // FRU table is built lazily when a FRU command or Get PDR command is
     // handled. To enable building FRU table, the FRU handler is passed to the
     // Platform handler.
-    invoker.registerHandler(PLDM_PLATFORM, std::make_unique<platform::Handler>(
-                                               dbusHandler.get(), PDR_JSONS_DIR,
-                                               EVENTS_JSONS_DIR, pdrRepo.get(),
-                                               hostPDRHandler.get(),
-                                               dbusToPLDMEventHandler.get(),
-                                               fruHandler.get(), true));
+    invoker.registerHandler(
+        PLDM_PLATFORM, std::make_unique<platform::Handler>(
+                           dbusHandler.get(), PDR_JSONS_DIR, pdrRepo.get(),
+                           hostPDRHandler.get(), dbusToPLDMEventHandler.get(),
+                           fruHandler.get(), true));
     invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
 
 #ifdef OEM_IBM
diff --git a/test/libpldmresponder_pdr_effecter_test.cpp b/test/libpldmresponder_pdr_effecter_test.cpp
index 7c8d29d..77f59a5 100644
--- a/test/libpldmresponder_pdr_effecter_test.cpp
+++ b/test/libpldmresponder_pdr_effecter_test.cpp
@@ -27,8 +27,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
 
@@ -126,8 +126,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", "",
-                    inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_NUMERIC_EFFECTER_PDR);
 
@@ -172,8 +172,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
 
@@ -193,8 +193,8 @@
         .WillRepeatedly(Return("foo.bar"));
 
     auto inPDRRepo = pldm_pdr_init();
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", "",
-                    inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     uint16_t entityType = 33;
     uint16_t entityInstance = 0;
     uint16_t containerId = 0;
diff --git a/test/libpldmresponder_pdr_sensor_test.cpp b/test/libpldmresponder_pdr_sensor_test.cpp
index 946720a..6429fc6 100644
--- a/test/libpldmresponder_pdr_sensor_test.cpp
+++ b/test/libpldmresponder_pdr_sensor_test.cpp
@@ -32,8 +32,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", "",
-                    inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     handler.getPDR(req, requestPayloadLength);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
@@ -82,8 +82,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", "",
-                    inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     handler.getPDR(req, requestPayloadLength);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
diff --git a/test/libpldmresponder_platform_test.cpp b/test/libpldmresponder_platform_test.cpp
index 1c561ea..71fec26 100644
--- a/test/libpldmresponder_platform_test.cpp
+++ b/test/libpldmresponder_platform_test.cpp
@@ -39,8 +39,8 @@
         .WillRepeatedly(Return("foo.bar"));
 
     auto pdrRepo = pldm_pdr_init();
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
+                    nullptr, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -76,8 +76,8 @@
         .WillRepeatedly(Return("foo.bar"));
 
     auto pdrRepo = pldm_pdr_init();
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
+                    nullptr, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -107,8 +107,8 @@
         .WillRepeatedly(Return("foo.bar"));
 
     auto pdrRepo = pldm_pdr_init();
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
+                    nullptr, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -136,8 +136,8 @@
         .WillRepeatedly(Return("foo.bar"));
 
     auto pdrRepo = pldm_pdr_init();
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
+                    nullptr, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -167,8 +167,8 @@
         .WillRepeatedly(Return("foo.bar"));
 
     auto pdrRepo = pldm_pdr_init();
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
+                    nullptr, nullptr, nullptr);
     Repo repo(pdrRepo);
     ASSERT_EQ(repo.empty(), false);
     auto response = handler.getPDR(req, requestPayloadLength);
@@ -229,8 +229,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     handler.getPDR(req, requestPayloadLength);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
@@ -275,8 +275,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
-                    "./event_jsons/good", inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     handler.getPDR(req, requestPayloadLength);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
@@ -320,8 +320,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto numericEffecterPdrRepo = pldm_pdr_init();
     Repo numericEffecterPDRs(numericEffecterPdrRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", "",
-                    inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
 
@@ -362,8 +362,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto numericEffecterPdrRepo = pldm_pdr_init();
     Repo numericEffecterPDRs(numericEffecterPdrRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", "",
-                    inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
 
@@ -535,7 +535,7 @@
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
     MockdBusHandler mockedUtils;
-    Handler handler(&mockedUtils, "", "", inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "", inPDRRepo, nullptr, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_TERMINUS_LOCATOR_PDR);
 
@@ -578,8 +578,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", "",
-                    inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
     pdr_utils::PdrEntry e;
@@ -624,8 +624,8 @@
     auto inPDRRepo = pldm_pdr_init();
     auto outPDRRepo = pldm_pdr_init();
     Repo outRepo(outPDRRepo);
-    Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", "",
-                    inPDRRepo, nullptr, nullptr, nullptr);
+    Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
+                    nullptr, nullptr, nullptr);
     Repo inRepo(inPDRRepo);
     getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
     pdr_utils::PdrEntry e;