pldm_events: Parse state sensor PDRs from the Host PDR

Parse the State Sensor PDRs from the host firmware and build
the HostStateSensorMap data structure which will be used to
lookup the sensor info for the sensorEventType in the
PlatformEventMessage command. Also clear the HostStateSensorMap
when the host powers off.

Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
Change-Id: Id85a73f1a0a1caf4b4155a8d235b3e807383e53a
diff --git a/host_pdr_handler.hpp b/host_pdr_handler.hpp
index 850afdb..83abe5f 100644
--- a/host_pdr_handler.hpp
+++ b/host_pdr_handler.hpp
@@ -1,6 +1,8 @@
 #pragma once
 
 #include "dbus_impl_requester.hpp"
+#include "libpldmresponder/pdr_utils.hpp"
+#include "types.hpp"
 #include "utils.hpp"
 
 #include <map>
@@ -23,6 +25,31 @@
 using ChangeEntry = uint32_t;
 using PDRRecordHandles = std::vector<ChangeEntry>;
 
+/** @struct SensorEntry
+ *
+ *  SensorEntry is a unique key which maps a sensorEventType request in the
+ *  PlatformEventMessage command to a host sensor PDR. This struct is a key
+ *  in a std::map, so implemented operator==and operator<.
+ */
+struct SensorEntry
+{
+    pdr::TerminusID terminusID;
+    pdr::SensorID sensorID;
+
+    bool operator==(const SensorEntry& e) const
+    {
+        return ((terminusID == e.terminusID) && (sensorID == e.sensorID));
+    }
+
+    bool operator<(const SensorEntry& e) const
+    {
+        return ((terminusID < e.terminusID) ||
+                ((terminusID == e.terminusID) && (sensorID < e.sensorID)));
+    }
+};
+
+using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
+
 /** @class HostPDRHandler
  *  @brief This class can fetch and process PDRs from host firmware
  *  @details Provides an API to fetch PDRs from the host firmware. Upon
@@ -71,6 +98,18 @@
     void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
                                    uint8_t eventDataFormat);
 
+    /** @brief Lookup host sensor info corresponding to requested SensorEntry
+     *
+     *  @param[in] entry - TerminusID and SensorID
+     *
+     *  @return SensorInfo corresponding to the input paramter SensorEntry
+     *          throw std::out_of_range exception if not found
+     */
+    const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
+    {
+        return sensorMap.at(entry);
+    }
+
   private:
     /** @brief fetchPDR schedules work on the event loop, this method does the
      *  actual work. This is so that the PDR exchg with the host is async.
@@ -119,6 +158,12 @@
     std::map<EntityType, pldm_entity> parents;
     /** @brief D-Bus property changed signal match */
     std::unique_ptr<sdbusplus::bus::match::match> hostOffMatch;
+
+    /** @brief sensorMap is a lookup data structure that is build from the
+     *         hostPDR that speeds up the lookup of <TerminusID, SensorID> in
+     *         PlatformEventMessage command request.
+     */
+    HostStateSensorMap sensorMap;
 };
 
 } // namespace pldm