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