Add support for Previous State in Sensor events

Previously, the sensor events generated by the PLDM stack only
included the current state of the sensor without any reference
to the previous state.

This commit introduces a new feature to address this limitation
by caching the previous states of all sensors.

The enhancement ensures that platform event messages(sensor events)
now contain the correct previous state information alongside the
current state.

Behavior Model:
1. Upon the initial occurrence of a sensor event, both the current
   state and the previous state are identical, adhering to the
   specification.
2. Subsequently, when the PLDM stack sends out sensor events, it
   retrieves the previous state from its cache and populates it
   accordingly.

Testing:
1. Example: Change the value of a sensor, such as dimm8 identify, to
   false using busctl.
   Tx: 81 02 0a 01 00 00 4d 00 01 00 [ 01 01 ]
- Note: Initially, both the event state and the previous state are 01.

2. Change the value of dimm8 identify to true.
   Tx: 81 02 0a 01 00 00 4d 00 01 00 [ 02 01 ]
- Result: Event state: 02, Previous state: 01.

3. Change the value of dimm8 identify back to false.
   Tx: 81 02 0a 01 00 00 4d 00 01 00 [ 01 02 ]
- Result: Event state: 01, Previous state: 02.

4. Similar results can be obtained using the getStateSensorReadings
   command for the same sensor at respective times.

Change-Id: Ic93a55e61fd5128cecc698b3555a6639876882ed
Signed-off-by: Manojkiran Eda <manojkiran.eda@gmail.com>
diff --git a/libpldmresponder/platform_state_sensor.hpp b/libpldmresponder/platform_state_sensor.hpp
index b57a58c..12e4b04 100644
--- a/libpldmresponder/platform_state_sensor.hpp
+++ b/libpldmresponder/platform_state_sensor.hpp
@@ -82,7 +82,8 @@
 int getStateSensorReadingsHandler(
     const DBusInterface& dBusIntf, Handler& handler, uint16_t sensorId,
     uint8_t sensorRearmCnt, uint8_t& compSensorCnt,
-    std::vector<get_sensor_state_field>& stateField)
+    std::vector<get_sensor_state_field>& stateField,
+    const stateSensorCacheMaps& sensorCache)
 {
     using namespace pldm::responder::pdr;
     using namespace pldm::utils;
@@ -146,22 +147,44 @@
         const auto& [dbusMappings, dbusValMaps] = handler.getDbusObjMaps(
             sensorId, pldm::responder::pdr_utils::TypeId::PLDM_SENSOR_ID);
 
+        pldm::responder::pdr_utils::EventStates sensorCacheforSensor{};
+        if (sensorCache.contains(sensorId))
+        {
+            sensorCacheforSensor = sensorCache.at(sensorId);
+        }
         stateField.clear();
-        for (size_t i = 0; i < sensorRearmCnt; i++)
+        for (std::size_t i{0}; i < sensorRearmCnt; i++)
         {
             auto& dbusMapping = dbusMappings[i];
 
             uint8_t sensorEvent = getStateSensorEventState<DBusInterface>(
                 dBusIntf, dbusValMaps[i], dbusMapping);
 
+            uint8_t previousState = PLDM_SENSOR_UNKNOWN;
+
+            // if sensor cache is empty, then its the first
+            // get_state_sensor_reading on this sensor, set the previous state
+            // as the current state
+
+            if (sensorCacheforSensor.at(i) == PLDM_SENSOR_UNKNOWN)
+            {
+                previousState = sensorEvent;
+                handler.updateSensorCache(sensorId, i, previousState);
+            }
+            else
+            {
+                // sensor cache is not empty, so get the previous state from
+                // the sensor cache
+                previousState = sensorCacheforSensor[i];
+            }
             uint8_t opState = PLDM_SENSOR_ENABLED;
             if (sensorEvent == PLDM_SENSOR_UNKNOWN)
             {
                 opState = PLDM_SENSOR_UNAVAILABLE;
             }
 
-            stateField.push_back({opState, PLDM_SENSOR_NORMAL,
-                                  PLDM_SENSOR_UNKNOWN, sensorEvent});
+            stateField.push_back(
+                {opState, PLDM_SENSOR_NORMAL, previousState, sensorEvent});
         }
     }
     catch (const std::out_of_range& e)