Add state set id support for remote sensor events

In the current state, PLDM does not support having a state set ID
field in the event JSONs, this commit would add that support in
PLDM there by allowing hosts to have multiple sensors with the
same entity instance number, type and container id, but with different
state sets.

TESTED: with pldmtool raw commands sending sensor events to the BMC.

Signed-off-by: Sagar Srinivas <sagar.srinivas@ibm.com>
Change-Id: If9bb9bde27d6b35f5904ac199801dbdffde0f57e
diff --git a/libpldmresponder/event_parser.cpp b/libpldmresponder/event_parser.cpp
index d070758..303a553 100644
--- a/libpldmresponder/event_parser.cpp
+++ b/libpldmresponder/event_parser.cpp
@@ -5,7 +5,6 @@
 
 #include <filesystem>
 #include <fstream>
-#include <iostream>
 #include <set>
 
 PHOSPHOR_LOG2_USING;
@@ -58,6 +57,8 @@
                 static_cast<uint16_t>(entry.value("entityInstance", 0));
             stateSensorEntry.sensorOffset =
                 static_cast<uint8_t>(entry.value("sensorOffset", 0));
+            stateSensorEntry.stateSetid =
+                static_cast<uint16_t>(entry.value("stateSetId", 0));
 
             pldm::utils::DBusMapping dbusInfo{};
 
diff --git a/libpldmresponder/event_parser.hpp b/libpldmresponder/event_parser.hpp
index 4d95e3a..7498c4c 100644
--- a/libpldmresponder/event_parser.hpp
+++ b/libpldmresponder/event_parser.hpp
@@ -27,13 +27,14 @@
     pdr::EntityType entityType;
     pdr::EntityInstance entityInstance;
     pdr::SensorOffset sensorOffset;
+    pdr::StateSetId stateSetid;
 
     bool operator==(const StateSensorEntry& e) const
     {
-        return ((containerId == e.containerId) &&
-                (entityType == e.entityType) &&
-                (entityInstance == e.entityInstance) &&
-                (sensorOffset == e.sensorOffset));
+        return (
+            (containerId == e.containerId) && (entityType == e.entityType) &&
+            (entityInstance == e.entityInstance) &&
+            (sensorOffset == e.sensorOffset) && (stateSetid == e.stateSetid));
     }
 
     bool operator<(const StateSensorEntry& e) const
@@ -45,7 +46,10 @@
              (entityInstance < e.entityInstance)) ||
             ((containerId == e.containerId) && (entityType == e.entityType) &&
              (entityInstance == e.entityInstance) &&
-             (sensorOffset < e.sensorOffset)));
+             (sensorOffset < e.sensorOffset)) ||
+            ((containerId == e.containerId) && (entityType == e.entityType) &&
+             (entityInstance == e.entityInstance) &&
+             (sensorOffset == e.sensorOffset) && (stateSetid < e.stateSetid)));
     }
 };
 
diff --git a/libpldmresponder/pdr_utils.cpp b/libpldmresponder/pdr_utils.cpp
index 06f080d..e7f6d42 100644
--- a/libpldmresponder/pdr_utils.cpp
+++ b/libpldmresponder/pdr_utils.cpp
@@ -166,6 +166,7 @@
     CompositeSensorStates sensors{};
     auto statesPtr = pdr->possible_states;
     auto compositeSensorCount = pdr->composite_sensor_count;
+    std::vector<StateSetId> stateSetIds{};
 
     while (compositeSensorCount--)
     {
@@ -189,6 +190,8 @@
                       updateStates);
 
         sensors.emplace_back(std::move(possibleStates));
+        stateSetIds.emplace_back(state->state_set_id);
+
         if (compositeSensorCount)
         {
             statesPtr += sizeof(state_sensor_possible_states) +
@@ -200,8 +203,8 @@
         std::make_tuple(static_cast<ContainerID>(pdr->container_id),
                         static_cast<EntityType>(pdr->entity_type),
                         static_cast<EntityInstance>(pdr->entity_instance));
-    auto sensorInfo = std::make_tuple(std::move(entityInfo),
-                                      std::move(sensors));
+    auto sensorInfo = std::make_tuple(std::move(entityInfo), std::move(sensors),
+                                      std::move(stateSetIds));
     return std::make_tuple(pdr->terminus_handle, pdr->sensor_id,
                            std::move(sensorInfo));
 }
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 70ac4bc..0e5455c 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -450,10 +450,11 @@
 
         pldm::pdr::EntityInfo entityInfo{};
         pldm::pdr::CompositeSensorStates compositeSensorStates{};
+        std::vector<pldm::pdr::StateSetId> stateSetIds{};
 
         try
         {
-            std::tie(entityInfo, compositeSensorStates) =
+            std::tie(entityInfo, compositeSensorStates, stateSetIds) =
                 hostPDRHandler->lookupSensorInfo(sensorEntry);
         }
         catch (const std::out_of_range&)
@@ -464,7 +465,7 @@
             try
             {
                 sensorEntry.terminusID = PLDM_TID_RESERVED;
-                std::tie(entityInfo, compositeSensorStates) =
+                std::tie(entityInfo, compositeSensorStates, stateSetIds) =
                     hostPDRHandler->lookupSensorInfo(sensorEntry);
             }
             // If there is no mapping for events return PLDM_SUCCESS
@@ -487,7 +488,8 @@
 
         const auto& [containerId, entityType, entityInstance] = entityInfo;
         events::StateSensorEntry stateSensorEntry{containerId, entityType,
-                                                  entityInstance, sensorOffset};
+                                                  entityInstance, sensorOffset,
+                                                  stateSetIds[sensorOffset]};
         return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
                                                       eventState);
     }
diff --git a/libpldmresponder/test/event_jsons/good/event_state_sensor.json b/libpldmresponder/test/event_jsons/good/event_state_sensor.json
index f917b90..b966076 100644
--- a/libpldmresponder/test/event_jsons/good/event_state_sensor.json
+++ b/libpldmresponder/test/event_jsons/good/event_state_sensor.json
@@ -5,6 +5,7 @@
             "entityType": 64,
             "entityInstance": 1,
             "sensorOffset": 0,
+            "stateSetId": 1,
             "event_states": [0, 1, 2],
             "dbus": {
                 "object_path": "/xyz/abc/def",
@@ -23,6 +24,7 @@
             "entityType": 64,
             "entityInstance": 1,
             "sensorOffset": 1,
+            "stateSetId": 1,
             "event_states": [2, 3],
             "dbus": {
                 "object_path": "/xyz/abc/def",
@@ -37,6 +39,7 @@
             "entityType": 67,
             "entityInstance": 2,
             "sensorOffset": 0,
+            "stateSetId": 1,
             "event_states": [0, 1],
             "dbus": {
                 "object_path": "/xyz/abc/ghi",
@@ -45,6 +48,21 @@
                 "property_type": "bool",
                 "property_values": [false, true]
             }
+        },
+        {
+            "containerID": 2,
+            "entityType": 67,
+            "entityInstance": 2,
+            "sensorOffset": 0,
+            "stateSetId": 2,
+            "event_states": [0, 1, 2],
+            "dbus": {
+                "object_path": "/xyz/abc/jkl",
+                "interface": "xyz.openbmc_project.example4.value",
+                "property_name": "value4",
+                "property_type": "string",
+                "property_values": ["Enabled", "Disabled", "Auto"]
+            }
         }
     ]
 }
diff --git a/libpldmresponder/test/libpldmresponder_platform_test.cpp b/libpldmresponder/test/libpldmresponder_platform_test.cpp
index c56cf06..ad3cb8e 100644
--- a/libpldmresponder/test/libpldmresponder_platform_test.cpp
+++ b/libpldmresponder/test/libpldmresponder_platform_test.cpp
@@ -603,7 +603,7 @@
 
     // Event Entry 1
     {
-        StateSensorEntry entry{1, 64, 1, 0};
+        StateSensorEntry entry{1, 64, 1, 0, 1};
         const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
         DBusMapping mapping{"/xyz/abc/def",
                             "xyz.openbmc_project.example1.value", "value1",
@@ -626,7 +626,7 @@
 
     // Event Entry 2
     {
-        StateSensorEntry entry{1, 64, 1, 1};
+        StateSensorEntry entry{1, 64, 1, 1, 1};
         const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
         DBusMapping mapping{"/xyz/abc/def",
                             "xyz.openbmc_project.example2.value", "value2",
@@ -643,7 +643,7 @@
 
     // Event Entry 3
     {
-        StateSensorEntry entry{2, 67, 2, 0};
+        StateSensorEntry entry{2, 67, 2, 0, 1};
         const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
         DBusMapping mapping{"/xyz/abc/ghi",
                             "xyz.openbmc_project.example3.value", "value3",
@@ -658,9 +658,29 @@
         ASSERT_EQ(value1 == propValue1, true);
     }
 
+    // Event Entry 4
+    {
+        StateSensorEntry entry{2, 67, 2, 0, 2};
+        const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
+        DBusMapping mapping{"/xyz/abc/jkl",
+                            "xyz.openbmc_project.example4.value", "value4",
+                            "string"};
+        ASSERT_EQ(mapping == dbusMapping, true);
+
+        const auto& propValue0 = eventStateMap.at(eventState0);
+        const auto& propValue1 = eventStateMap.at(eventState1);
+        const auto& propValue2 = eventStateMap.at(eventState2);
+        PropertyValue value0{std::in_place_type<std::string>, "Enabled"};
+        PropertyValue value1{std::in_place_type<std::string>, "Disabled"};
+        PropertyValue value2{std::in_place_type<std::string>, "Auto"};
+        ASSERT_EQ(value0 == propValue0, true);
+        ASSERT_EQ(value1 == propValue1, true);
+        ASSERT_EQ(value2 == propValue2, true);
+    }
+
     // Invalid Entry
     {
-        StateSensorEntry entry{0, 0, 0, 0};
+        StateSensorEntry entry{0, 0, 0, 0, 1};
         ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
     }
 }