event: Handle platform event message

Provide infrastructure to set the properties for the D-Bus object
depending on the EventEntry, Where <EventEntry - uint32_t>=
<uint8_t - EventState> <uint8_t - SensorOffset> <uint16_t - SensorID>
Based on the EventEntry value the corresponding dBus property will be
set.

Current EventEntryMap contains EventEntries mapped for the sensorID 7 &
8 representing the COM Channel 1001 (VMI) port 0's & port 1's ipv4
"STATE SET 15" respectively each for "valid" & "invalid" Configuration
States as listed in DSP0249_1.0.0 State Set Specification.

Change-Id: Iff26f5879ec632053de7de46154f962ce95a7512
Signed-off-by: Zahed Hossain <zahzahed@in.ibm.com>
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 0a33d86..86c5a2c 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -16,6 +16,38 @@
 
 static const Json empty{};
 
+using EventEntryMap = std::map<EventEntry, DBusInfo>;
+
+const EventEntryMap eventEntryMap = {
+    {
+        0x01010007, // SensorID for VMI Port 0 ipv4 = 7, SensorOffset for the
+                    // State Set ID 15 = 1 & PLDM State Set Enumeration List = 1
+                    // (Valid Configuration)
+        {"/xyz/openbmc_project/network/vmi/intf0/ipv4/addr0",
+         "xyz.openbmc_project.Object.Enable", "Enabled", "bool", true},
+    },
+    {
+        0x02010007, // SensorID for VMI Port 0 ipv4 = 7, SensorOffset for the
+                    // State Set ID 15 = 1 & PLDM State Set Enumeration List = 2
+                    // (Invalid Configuration)
+        {"/xyz/openbmc_project/network/vmi/intf0/ipv4/addr0",
+         "xyz.openbmc_project.Object.Enable", "Enabled", "bool", false},
+    },
+    {
+        0x01010008, // SensorID for VMI Port 1 ipv4 = 8, SensorOffset for the
+                    // State Set ID 15 = 1 & PLDM State Set Enumeration List = 1
+                    // (Valid Configuration)
+        {"/xyz/openbmc_project/network/vmi/intf1/ipv4/addr0",
+         "xyz.openbmc_project.Object.Enable", "Enabled", "bool", true},
+    },
+    {
+        0x02010008, // SensorID for VMI Port 1 ipv4 = 8, SensorOffset for the
+                    // State Set ID 15 = 1 & PLDM State Set Enumeration List = 2
+                    // (Invalid Configuration)
+        {"/xyz/openbmc_project/network/vmi/intf1/ipv4/addr0",
+         "xyz.openbmc_project.Object.Enable", "Enabled", "bool", false},
+    }};
+
 void Handler::addDbusObjMaps(
     uint16_t effecterId,
     std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj)
@@ -260,15 +292,31 @@
         return rc;
     }
 
+    auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
+                          eventDataOffset + eventClassDataOffset;
+    auto eventClassDataSize =
+        payloadLength - eventDataOffset - eventClassDataOffset;
+
     if (eventClass == PLDM_STATE_SENSOR_STATE)
     {
         uint8_t sensorOffset{};
         uint8_t eventState{};
         uint8_t previousEventState{};
 
-        rc = decode_state_sensor_data(&eventClass, eventClassDataOffset,
+        rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
                                       &sensorOffset, &eventState,
                                       &previousEventState);
+        if (rc != PLDM_SUCCESS)
+        {
+            return PLDM_ERROR;
+        }
+
+        rc = setSensorEventData(sensorId, sensorOffset, eventState);
+
+        if (rc != PLDM_SUCCESS)
+        {
+            return PLDM_ERROR;
+        }
     }
     else
     {
@@ -278,6 +326,40 @@
     return PLDM_SUCCESS;
 }
 
+int Handler::setSensorEventData(uint16_t sensorId, uint8_t sensorOffset,
+                                uint8_t eventState)
+{
+    EventEntry eventEntry = ((static_cast<uint32_t>(eventState)) << 24) +
+                            ((static_cast<uint32_t>(sensorOffset)) << 16) +
+                            sensorId;
+    auto iter = eventEntryMap.find(eventEntry);
+    if (iter == eventEntryMap.end())
+    {
+        return PLDM_SUCCESS;
+    }
+
+    const auto& dBusInfo = iter->second;
+    try
+    {
+        pldm::utils::DBusMapping dbusMapping{
+            dBusInfo.dBusValues.objectPath, dBusInfo.dBusValues.interface,
+            dBusInfo.dBusValues.propertyName, dBusInfo.dBusValues.propertyType};
+        pldm::utils::DBusHandler().setDbusProperty(dbusMapping,
+                                                   dBusInfo.dBusPropertyValue);
+    }
+    catch (std::exception& e)
+    {
+
+        std::cerr
+            << "Error Setting dbus property,SensorID=" << eventEntry
+            << "DBusInfo=" << dBusInfo.dBusValues.objectPath
+            << dBusInfo.dBusValues.interface << dBusInfo.dBusValues.propertyName
+            << "ERROR=" << e.what() << "\n";
+        return PLDM_ERROR;
+    }
+    return PLDM_SUCCESS;
+}
+
 } // namespace platform
 } // namespace responder
 } // namespace pldm
diff --git a/libpldmresponder/platform.hpp b/libpldmresponder/platform.hpp
index a435dca..696434b 100644
--- a/libpldmresponder/platform.hpp
+++ b/libpldmresponder/platform.hpp
@@ -40,6 +40,15 @@
 using EventHandlers = std::vector<EventHandler>;
 using EventMap = std::map<EventType, EventHandlers>;
 
+// EventEntry = <uint8_t> - EventState <uint8_t> - SensorOffset <uint16_t> -
+// SensorID
+using EventEntry = uint32_t;
+struct DBusInfo
+{
+    pldm::utils::DBusMapping dBusValues;
+    pldm::utils::PropertyValue dBusPropertyValue;
+};
+
 class Handler : public CmdHandler
 {
   public:
@@ -186,6 +195,18 @@
     int sensorEvent(const pldm_msg* request, size_t payloadLength,
                     uint8_t formatVersion, uint8_t tid, size_t eventDataOffset);
 
+    /** @brief Handler for setting Sensor event data
+     *
+     *  @param[in] sensorId - sensorID value of the sensor
+     *  @param[in] sensorOffset - Identifies which state sensor within a
+     * composite state sensor the event is being returned for
+     *  @param[in] eventState - The event state value from the state change that
+     * triggered the event message
+     *  @return PLDM completion code
+     */
+    int setSensorEventData(uint16_t sensorId, uint8_t sensorOffset,
+                           uint8_t eventState);
+
     /** @brief Function to set the effecter requested by pldm requester
      *  @param[in] dBusIntf - The interface object
      *  @param[in] effecterId - Effecter ID sent by the requester to act on