platform-mc: Added EventManager

Added eventManager to handle sensor event class(00h) which is defined in
table 11 of DSP0248 v1.3.0. In this commit, the eventManager supports to
receive event asynchronously. The commit will also log the Ipmitool SEL
log and Redfish log for PLDM sensor event messages.

Change-Id: I1b337ccae454067841ffbbd8754631216a995542
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Signed-off-by: Gilbert Chen <gilbertc@nvidia.com>
diff --git a/platform-mc/numeric_sensor.cpp b/platform-mc/numeric_sensor.cpp
index af6efad..6b387dd 100644
--- a/platform-mc/numeric_sensor.cpp
+++ b/platform-mc/numeric_sensor.cpp
@@ -750,5 +750,127 @@
         }
     }
 }
+
+int NumericSensor::triggerThresholdEvent(
+    pldm::utils::Level eventType, pldm::utils::Direction direction,
+    double rawValue, bool newAlarm, bool assert)
+{
+    if (!valueIntf)
+    {
+        lg2::error(
+            "Failed to update thresholds sensor {NAME} D-Bus interfaces don't exist.",
+            "NAME", sensorName);
+        return PLDM_ERROR;
+    }
+
+    auto value = unitModifier(conversionFormula(rawValue));
+    lg2::error(
+        "triggerThresholdEvent eventType {TID}, direction {SID} value {VAL} newAlarm {PSTATE} assert {ESTATE}",
+        "TID", eventType, "SID", direction, "VAL", value, "PSTATE", newAlarm,
+        "ESTATE", assert);
+
+    switch (eventType)
+    {
+        case pldm::utils::Level::WARNING:
+        {
+            if (!thresholdWarningIntf)
+            {
+                lg2::error(
+                    "Error:Trigger sensor warning event for non warning threshold sensors {NAME}",
+                    "NAME", sensorName);
+                return PLDM_ERROR;
+            }
+            if (direction == pldm::utils::Direction::HIGH &&
+                !std::isnan(thresholdWarningIntf->warningHigh()))
+            {
+                auto alarm = thresholdWarningIntf->warningAlarmHigh();
+                if (alarm == newAlarm)
+                {
+                    return PLDM_SUCCESS;
+                }
+                thresholdWarningIntf->warningAlarmHigh(newAlarm);
+                if (assert)
+                {
+                    thresholdWarningIntf->warningHighAlarmAsserted(value);
+                }
+                else
+                {
+                    thresholdWarningIntf->warningHighAlarmDeasserted(value);
+                }
+            }
+            else if (direction == pldm::utils::Direction::LOW &&
+                     !std::isnan(thresholdWarningIntf->warningLow()))
+            {
+                auto alarm = thresholdWarningIntf->warningAlarmLow();
+                if (alarm == newAlarm)
+                {
+                    return PLDM_SUCCESS;
+                }
+                thresholdWarningIntf->warningAlarmLow(newAlarm);
+                if (assert)
+                {
+                    thresholdWarningIntf->warningLowAlarmAsserted(value);
+                }
+                else
+                {
+                    thresholdWarningIntf->warningLowAlarmDeasserted(value);
+                }
+            }
+            break;
+        }
+        case pldm::utils::Level::CRITICAL:
+        {
+            if (!thresholdCriticalIntf)
+            {
+                lg2::error(
+                    "Error:Trigger sensor Critical event for non warning threshold sensors {NAME}",
+                    "NAME", sensorName);
+                return PLDM_ERROR;
+            }
+            if (direction == pldm::utils::Direction::HIGH &&
+                !std::isnan(thresholdCriticalIntf->criticalHigh()))
+            {
+                auto alarm = thresholdCriticalIntf->criticalAlarmHigh();
+                if (alarm == newAlarm)
+                {
+                    return PLDM_SUCCESS;
+                }
+                thresholdCriticalIntf->criticalAlarmHigh(newAlarm);
+                if (assert)
+                {
+                    thresholdCriticalIntf->criticalHighAlarmAsserted(value);
+                }
+                else
+                {
+                    thresholdCriticalIntf->criticalHighAlarmDeasserted(value);
+                }
+            }
+            else if (direction == pldm::utils::Direction::LOW &&
+                     !std::isnan(thresholdCriticalIntf->criticalLow()))
+            {
+                auto alarm = thresholdCriticalIntf->criticalAlarmLow();
+                if (alarm == newAlarm)
+                {
+                    return PLDM_SUCCESS;
+                }
+                thresholdCriticalIntf->criticalAlarmLow(newAlarm);
+                if (assert)
+                {
+                    thresholdCriticalIntf->criticalLowAlarmAsserted(value);
+                }
+                else
+                {
+                    thresholdCriticalIntf->criticalLowAlarmDeasserted(value);
+                }
+            }
+            break;
+        }
+
+        default:
+            break;
+    }
+
+    return PLDM_SUCCESS;
+}
 } // namespace platform_mc
 } // namespace pldm