oem-ampere: eventManager: Handle pldmMessagePollEvent event class

Add Ampere OEM handler to handle `pldmMessagePollEvent` event class. The
handler will check if the event is one of the RAS_UE sensors IDs
(CORE_UE, SOC_UE, MCU_UE, PCIE_UE) and triggers the `ras_ue_fault` LED
if it is.

Change-Id: I767def72ffda6c20482633f8be0b0bfbd6eea4c7
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
diff --git a/oem/ampere/event/oem_event_manager.cpp b/oem/ampere/event/oem_event_manager.cpp
index b9fd114..0eadb3d 100644
--- a/oem/ampere/event/oem_event_manager.cpp
+++ b/oem/ampere/event/oem_event_manager.cpp
@@ -16,6 +16,7 @@
 
 #include <algorithm>
 #include <map>
+#include <set>
 #include <sstream>
 #include <string>
 #include <unordered_map>
@@ -41,6 +42,8 @@
 constexpr auto maxDIMMIdxBitNum = 24;
 constexpr auto maxDIMMInstantNum = 24;
 
+const std::set<uint16_t> rasUESensorIDs = {CORE_UE, MCU_UE, PCIE_UE, SOC_UE};
+
 /*
     An array of possible boot status of a boot stage.
     The index maps with byte 0 of boot code.
@@ -944,5 +947,52 @@
     return PLDM_SUCCESS;
 }
 
+int OemEventManager::handlepldmMessagePollEvent(
+    const pldm_msg* request, size_t payloadLength, uint8_t /* formatVersion */,
+    pldm_tid_t tid, size_t eventDataOffset)
+{
+    /* This OEM event handler is only used for SoC terminus*/
+    if (!tidToSocketNameMap.contains(tid))
+    {
+        return PLDM_SUCCESS;
+    }
+
+    auto eventData =
+        reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
+    auto eventDataSize = payloadLength - eventDataOffset;
+
+    pldm_message_poll_event poll_event{};
+    auto rc = decode_pldm_message_poll_event_data(eventData, eventDataSize,
+                                                  &poll_event);
+    if (rc)
+    {
+        lg2::error("Failed to decode PldmMessagePollEvent event, error {RC} ",
+                   "RC", rc);
+        return rc;
+    }
+
+    auto sensorID = poll_event.event_id;
+    /* The UE errors */
+    if (rasUESensorIDs.contains(sensorID))
+    {
+        pldm::utils::DBusMapping dbusMapping{
+            "/xyz/openbmc_project/led/groups/ras_ue_fault",
+            "xyz.openbmc_project.Led.Group", "Asserted", "bool"};
+        try
+        {
+            pldm::utils::DBusHandler().setDbusProperty(
+                dbusMapping, pldm::utils::PropertyValue{bool(true)});
+        }
+        catch (const std::exception& e)
+        {
+            lg2::error(
+                "Failed to set the RAS UE LED terminus ID {TID} sensor ID {SENSORID} - errors {ERROR}",
+                "TID", tid, "SENSORID", sensorID, "ERROR", e);
+        }
+    }
+
+    return PLDM_SUCCESS;
+}
+
 } // namespace oem_ampere
 } // namespace pldm
diff --git a/oem/ampere/event/oem_event_manager.hpp b/oem/ampere/event/oem_event_manager.hpp
index d7eb67b..acade3f 100644
--- a/oem/ampere/event/oem_event_manager.hpp
+++ b/oem/ampere/event/oem_event_manager.hpp
@@ -33,6 +33,11 @@
     SOC_HEALTH_AVAILABILITY = 170,
     BOOT_OVERALL = 175,
     WATCH_DOG = 179,
+    CORE_UE = 192,
+    MCU_UE = 194,
+    PCIE_UE = 196,
+    SOC_UE = 198,
+    SOC_BERT = 200,
 };
 
 namespace boot
@@ -265,6 +270,20 @@
     int processOemMsgPollEvent(pldm_tid_t tid, uint16_t eventId,
                                const uint8_t* eventData, size_t eventDataSize);
 
+    /** @brief Decode sensor event messages and handle correspondingly.
+     *
+     *  @param[in] request - the request message of sensor event
+     *  @param[in] payloadLength - the payload length of sensor event
+     *  @param[in] formatVersion - the format version of sensor event
+     *  @param[in] tid - TID
+     *  @param[in] eventDataOffset - the event data offset of sensor event
+     *
+     *  @return int - returned error code
+     */
+    int handlepldmMessagePollEvent(
+        const pldm_msg* request, size_t payloadLength,
+        uint8_t /* formatVersion */, pldm_tid_t tid, size_t eventDataOffset);
+
   protected:
     /** @brief Create prefix string for logging message.
      *
diff --git a/oem/ampere/oem_ampere.hpp b/oem/ampere/oem_ampere.hpp
index ff91652..8e8c301 100644
--- a/oem/ampere/oem_ampere.hpp
+++ b/oem/ampere/oem_ampere.hpp
@@ -122,6 +122,17 @@
                 return platformManager->handlePolledCperEvent(
                     tid, eventId, eventData, eventDataSize);
             }});
+
+        /* Register OEM handling for pldmMessagePollEvent */
+        platformHandler->registerEventHandlers(
+            PLDM_MESSAGE_POLL_EVENT,
+            {[oemEventManager](const pldm_msg* request, size_t payloadLength,
+                               uint8_t formatVersion, uint8_t tid,
+                               size_t eventDataOffset) {
+                return oemEventManager->handlepldmMessagePollEvent(
+                    request, payloadLength, formatVersion, tid,
+                    eventDataOffset);
+            }});
     }
 
   private: