Make Terminus info a class variable

The terminus information is needed at various places in
pldm and instead of passing that structure around via
function arguments, it is cheaper to maintain it as a
class variable.

And this commit also removes the redundant structure that
stores partial terminus information.

Signed-off-by: Manojkiran Eda <manojkiran.eda@gmail.com>
Change-Id: If34cfe1c47bdeceaabdae779ee7cd8569beedf5d
diff --git a/common/types.hpp b/common/types.hpp
index 318dc7e..865f2c4 100644
--- a/common/types.hpp
+++ b/common/types.hpp
@@ -41,6 +41,7 @@
 using CompositeCount = uint8_t;
 using SensorOffset = uint8_t;
 using EventState = uint8_t;
+using TerminusValidity = uint8_t;
 
 //!< Subset of the State Set that is supported by a effecter/sensor
 using PossibleStates = std::set<uint8_t>;
diff --git a/host-bmc/host_pdr_handler.cpp b/host-bmc/host_pdr_handler.cpp
index d7051cf..63512a3 100644
--- a/host-bmc/host_pdr_handler.cpp
+++ b/host-bmc/host_pdr_handler.cpp
@@ -87,6 +87,15 @@
                 auto propVal = std::get<std::string>(value);
                 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
                 {
+                    // Delete all the remote terminus information
+                    for (const auto& terminusInfo : this->tlPDRInfo)
+                    {
+                        if (terminusInfo.first != TERMINUS_HANDLE)
+                        {
+                            this->tlPDRInfo.erase(terminusInfo.first);
+                        }
+                    }
+
                     pldm_pdr_remove_remote_pdrs(repo);
                     pldm_entity_association_tree_destroy_root(entityTree);
                     pldm_entity_association_tree_copy_root(bmcEntityTree,
@@ -332,8 +341,7 @@
     }
 }
 
-void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs,
-                                          const TLPDRMap& tlpdrInfo)
+void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs)
 {
     for (const auto& pdr : stateSensorPDRs)
     {
@@ -343,7 +351,7 @@
         sensorEntry.sensorID = sensorID;
         try
         {
-            sensorEntry.terminusID = tlpdrInfo.at(terminusHandle);
+            sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle));
         }
         // If there is no mapping for terminusHandle assign the reserved TID
         // value of 0xFF to indicate that.
@@ -361,9 +369,7 @@
 {
     static bool merged = false;
     static PDRList stateSensorPDRs{};
-    static TLPDRMap tlpdrInfo{};
     uint32_t nextRecordHandle{};
-    std::vector<TlInfo> tlInfo;
     uint8_t tlEid = 0;
     bool tlValid = true;
     uint32_t rh = 0;
@@ -440,10 +446,6 @@
                     auto tlpdr =
                         reinterpret_cast<const pldm_terminus_locator_pdr*>(
                             pdr.data());
-                    tlpdrInfo.emplace(
-                        static_cast<pldm::pdr::TerminusHandle>(
-                            tlpdr->terminus_handle),
-                        static_cast<pldm::pdr::TerminusID>(tlpdr->tid));
 
                     terminusHandle = tlpdr->terminus_handle;
                     tid = tlpdr->tid;
@@ -460,9 +462,9 @@
                     {
                         tlValid = false;
                     }
-                    tlInfo.emplace_back(
-                        TlInfo{tlpdr->validity, static_cast<uint8_t>(tlEid),
-                               tlpdr->tid, tlpdr->terminus_handle});
+                    tlPDRInfo.insert_or_assign(
+                        tlpdr->terminus_handle,
+                        std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity));
                 }
                 else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
                 {
@@ -485,13 +487,12 @@
     if (!nextRecordHandle)
     {
         /*received last record*/
-        this->parseStateSensorPDRs(stateSensorPDRs, tlpdrInfo);
+        this->parseStateSensorPDRs(stateSensorPDRs);
         if (isHostUp())
         {
-            this->setHostSensorState(stateSensorPDRs, tlInfo);
+            this->setHostSensorState(stateSensorPDRs);
         }
         stateSensorPDRs.clear();
-        tlpdrInfo.clear();
         if (merged)
         {
             merged = false;
@@ -579,8 +580,7 @@
     return responseReceived;
 }
 
-void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs,
-                                        const std::vector<TlInfo>& tlinfo)
+void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs)
 {
     for (const auto& stateSensorPDR : stateSensorPDRs)
     {
@@ -597,18 +597,18 @@
 
         uint16_t sensorId = pdr->sensor_id;
 
-        for (auto info : tlinfo)
+        for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo)
         {
-            if (info.terminusHandle == pdr->terminus_handle)
+            if (terminusHandle == pdr->terminus_handle)
             {
-                if (info.valid == PLDM_TL_PDR_VALID)
+                if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID)
                 {
-                    mctp_eid = info.eid;
+                    mctp_eid = std::get<1>(terminusInfo);
                 }
 
                 bitfield8_t sensorRearm;
                 sensorRearm.byte = 0;
-                uint8_t tid = info.tid;
+                uint8_t tid = std::get<0>(terminusInfo);
 
                 auto instanceId = requester.getInstanceId(mctp_eid);
                 std::vector<uint8_t> requestMsg(
diff --git a/host-bmc/host_pdr_handler.hpp b/host-bmc/host_pdr_handler.hpp
index 6e8e03d..9ea8611 100644
--- a/host-bmc/host_pdr_handler.hpp
+++ b/host-bmc/host_pdr_handler.hpp
@@ -50,18 +50,6 @@
     }
 };
 
-/* @struct TerminusLocatorInfo
- * Contains validity, eid, terminus_id and terminus handle
- * of a terminus locator PDR.
- */
-struct TlInfo
-{
-    uint8_t valid;
-    uint8_t eid;
-    uint8_t tid;
-    uint16_t terminusHandle;
-};
-
 using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
 using PDRList = std::vector<std::vector<uint8_t>>;
 
@@ -84,7 +72,9 @@
     HostPDRHandler& operator=(HostPDRHandler&&) = delete;
     ~HostPDRHandler() = default;
 
-    using TLPDRMap = std::map<pdr::TerminusHandle, pdr::TerminusID>;
+    using TerminusInfo =
+        std::tuple<pdr::TerminusID, pdr::EID, pdr::TerminusValidity>;
+    using TLPDRMap = std::map<pdr::TerminusHandle, TerminusInfo>;
 
     /** @brief Constructor
      *  @param[in] mctp_fd - fd of MCTP communications socket
@@ -148,11 +138,9 @@
      *         structure
      *
      *  @param[in] stateSensorPDRs - host state sensor PDRs
-     *  @param[in] tlpdrInfo - terminus locator PDRs info
      *
      */
-    void parseStateSensorPDRs(const PDRList& stateSensorPDRs,
-                              const TLPDRMap& tlpdrInfo);
+    void parseStateSensorPDRs(const PDRList& stateSensorPDRs);
 
     /** @brief this function sends a GetPDR request to Host firmware.
      *  And processes the PDRs based on type
@@ -168,15 +156,16 @@
     /** @brief set HostSensorStates when pldmd starts or restarts
      *  and updates the D-Bus property
      *  @param[in] stateSensorPDRs - host state sensor PDRs
-     *  @param[in] tlinfo - vector of struct TlInfo
      */
-    void setHostSensorState(const PDRList& stateSensorPDRs,
-                            const std::vector<TlInfo>& tlinfo);
+    void setHostSensorState(const PDRList& stateSensorPDRs);
 
     /** @brief check whether Host is running when pldmd starts
      */
     bool isHostUp();
 
+    /** @brief map that captures various terminus information **/
+    TLPDRMap tlPDRInfo;
+
   private:
     /** @brief deferred function to fetch PDR from Host, scheduled to work on
      *  the event loop. The PDR exchg with the host is async.
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 0ad5fb6..2d6ed21 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -623,6 +623,12 @@
     pdrEntry.data = pdrBuffer.data();
     pdrEntry.size = pdrBuffer.size();
     repo.addRecord(pdrEntry);
+    if (hostPDRHandler)
+    {
+        hostPDRHandler->tlPDRInfo.insert_or_assign(
+            pdr->terminus_handle,
+            std::make_tuple(pdr->tid, locatorValue->eid, pdr->validity));
+    }
 }
 
 Response Handler::getStateSensorReadings(const pldm_msg* request,