oem-ampere: eventManager: Fix DIMMx_Status sensor IDs checking

DIMMx_Status sensor IDs have format `4 + 2x` with x from 0 to 23.

Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Change-Id: Iea8219e005a2773e288920b6aaa529b6f5d43de4
diff --git a/oem/ampere/event/oem_event_manager.cpp b/oem/ampere/event/oem_event_manager.cpp
index cf8e1ab..79266f5 100644
--- a/oem/ampere/event/oem_event_manager.cpp
+++ b/oem/ampere/event/oem_event_manager.cpp
@@ -271,6 +271,18 @@
     return description;
 }
 
+uint8_t OemEventManager::sensorIdToDIMMIdx(const uint16_t& sensorId)
+{
+    uint8_t dimmIdx = maxDIMMInstantNum;
+    int sensorId_Off = sensorId - 4;
+    if ((sensorId_Off >= 0) && ((sensorId_Off % 2) == 0) &&
+        ((sensorId_Off / 2) < maxDIMMInstantNum))
+    {
+        dimmIdx = sensorId_Off / 2;
+    }
+    return dimmIdx;
+}
+
 void OemEventManager::handleBootOverallEvent(
     pldm_tid_t /*tid*/, uint16_t /*sensorId*/, uint32_t presentReading)
 {
@@ -384,8 +396,7 @@
     }
 
     // DIMMx_Status sensorID 4+2*index (index 0 -> maxDIMMInstantNum-1)
-    if (auto dimmIdx = (sensorId - 4) / 2;
-        sensorId >= 4 && dimmIdx >= 0 && dimmIdx < maxDIMMInstantNum)
+    if (auto dimmIdx = sensorIdToDIMMIdx(sensorId); dimmIdx < maxDIMMInstantNum)
     {
         handleDIMMStatusEvent(tid, sensorId, presentReading);
         return PLDM_SUCCESS;
@@ -710,7 +721,12 @@
 
     description += prefixMsgStrCreation(tid, sensorId);
 
-    uint8_t dimmIdx = (sensorId - 4) / 2;
+    // DIMMx_Status sensorID 4+2*index (index 0 -> maxDIMMInstantNum-1)
+    auto dimmIdx = sensorIdToDIMMIdx(sensorId);
+    if (dimmIdx >= maxDIMMIdxBitNum)
+    {
+        return;
+    }
 
     description += "DIMM " + std::to_string(dimmIdx) + " ";
 
diff --git a/oem/ampere/event/oem_event_manager.hpp b/oem/ampere/event/oem_event_manager.hpp
index 521a16b..54f9cdd 100644
--- a/oem/ampere/event/oem_event_manager.hpp
+++ b/oem/ampere/event/oem_event_manager.hpp
@@ -280,6 +280,15 @@
      */
     std::string dimmIdxsToString(uint32_t dimmIdxs);
 
+    /** @brief Convert sensor ID to DIMM index. Return maxDIMMInstantNum
+     * in failure.
+     *
+     *  @param[in] sensorId - sensorID
+     *
+     *  @return uint8_t - DIMM index
+     */
+    uint8_t sensorIdToDIMMIdx(const uint16_t& sensorId);
+
     /** @brief Convert the DIMM training failure into logging string.
      *
      *  @param[in] failureInfo - the one-hot DIMM index byte