Move to libpldm instance id APIs

Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
Change-Id: I2955097a78c673f65054fa9bff1ef5243da136a2
Signed-off-by: Lakshmi Yadlapati <lakshmiy@us.ibm.com>
diff --git a/pldm.cpp b/pldm.cpp
index dc7a280..5ee626a 100644
--- a/pldm.cpp
+++ b/pldm.cpp
@@ -1,5 +1,7 @@
 #include "pldm.hpp"
 
+#include "libpldm/instance-id.h"
+
 #include "file.hpp"
 
 #include <libpldm/entity.h>
@@ -521,39 +523,63 @@
 
 bool Interface::getPldmInstanceId()
 {
+    pldm_instance_id_t id;
     if (!pldmInstanceID)
     {
         // Request new instance ID
-        auto& bus = open_power::occ::utils::getBus();
-        try
+        int rc = pldm_instance_id_alloc(pldmInstanceIdDb, tid, &id);
+        if (rc == -EAGAIN)
         {
-            auto method = bus.new_method_call(
-                "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
-                "xyz.openbmc_project.PLDM.Requester", "GetInstanceId");
-            method.append(mctpEid);
-            auto reply = bus.call(method);
-            uint8_t newInstanceId;
-            reply.read(newInstanceId);
-            pldmInstanceID = newInstanceId;
-            if (!throttleTraces)
-            {
-                log<level::INFO>(std::format("pldm: got new InstanceId: {}",
-                                             pldmInstanceID.value())
-                                     .c_str());
-            }
+            std::this_thread::sleep_for(std::chrono::milliseconds(100));
+            rc = pldm_instance_id_alloc(pldmInstanceIdDb, tid, &id);
         }
-        catch (const sdbusplus::exception_t& e)
+
+        if (rc)
         {
             log<level::ERR>(
-                std::format("pldm: GetInstanceId failed: {}", e.what())
+                std::format(
+                    "getPldmInstanceId: Failed to alloc ID for TID {}. RC{}",
+                    tid, rc)
                     .c_str());
             return false;
         }
+        pldmInstanceID.emplace(id);
+        if (!throttleTraces)
+        {
+            log<level::INFO>(
+                std::format("got id {} and set PldmInstanceId to {}", id,
+                            pldmInstanceID.value())
+                    .c_str());
+        }
     }
-
     return true;
 }
 
+void Interface::freePldmInstanceId()
+{
+    if (pldmInstanceID)
+    {
+        int rc = pldm_instance_id_free(pldmInstanceIdDb, tid,
+                                       pldmInstanceID.value());
+        if (rc)
+        {
+            log<level::ERR>(
+                std::format(
+                    "freePldmInstanceId: Failed to free ID {} for TID {}. RC{}",
+                    pldmInstanceID.value(), tid, rc)
+                    .c_str());
+            return;
+        }
+        if (!throttleTraces)
+        {
+            log<level::INFO>(
+                std::format("Freed PLDM instance ID {}", pldmInstanceID.value())
+                    .c_str());
+        }
+        pldmInstanceID = std::nullopt;
+    }
+}
+
 void Interface::sendPldm(const std::vector<uint8_t>& request,
                          const uint8_t instance, const bool rspExpected)
 {
@@ -573,6 +599,7 @@
                 "sendPldm: Failed to connect to MCTP socket, errno={}/{}",
                 openErrno, strerror(openErrno))
                 .c_str());
+        freePldmInstanceId();
         return;
     }
 
@@ -640,11 +667,6 @@
                     sendErrno, strerror(sendErrno))
                     .c_str());
         }
-        else
-        {
-            // Not waiting for response, instance ID should be freed
-            pldmInstanceID = std::nullopt;
-        }
         pldmClose();
     }
 }
@@ -694,6 +716,7 @@
 
 void Interface::pldmClose()
 {
+    freePldmInstanceId();
     if (pldmRspTimer.isEnabled())
     {
         // stop PLDM response timer
diff --git a/pldm.hpp b/pldm.hpp
index 5d093a1..641153f 100644
--- a/pldm.hpp
+++ b/pldm.hpp
@@ -1,8 +1,10 @@
 #pragma once
+
 #include "occ_events.hpp"
 #include "occ_status.hpp"
 #include "utils.hpp"
 
+#include <libpldm/instance-id.h>
 #include <libpldm/pldm.h>
 
 #include <sdbusplus/bus/match.hpp>
@@ -44,7 +46,7 @@
 {
   public:
     Interface() = delete;
-    ~Interface() = default;
+    //~Interface() = default;
     Interface(const Interface&) = delete;
     Interface& operator=(const Interface&) = delete;
     Interface(Interface&&) = delete;
@@ -80,7 +82,22 @@
         pldmRspTimer(
             sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
                 sdpEvent, std::bind(&Interface::pldmRspExpired, this)))
-    {}
+    {
+        int rc = pldm_instance_db_init_default(&pldmInstanceIdDb);
+        if (rc)
+        {
+            throw std::system_category().default_error_condition(rc);
+        }
+    }
+
+    ~Interface()
+    {
+        int rc = pldm_instance_db_destroy(pldmInstanceIdDb);
+        if (rc)
+        {
+            std::cout << "pldm_instance_db_destroy failed, rc =" << rc << "\n";
+        }
+    }
 
     /** @brief Fetch the state sensor PDRs and populate the cache with
      *         sensorId to OCC/SBE instance mapping information and the sensor
@@ -149,6 +166,10 @@
     void setTraceThrottle(const bool throttle);
 
   private:
+    /** @brief PLDM instance ID database object used to get instance IDs
+     */
+    pldm_instance_db* pldmInstanceIdDb = nullptr;
+
     /** @brief PLDM instance number used in PLDM requests
      */
     std::optional<uint8_t> pldmInstanceID;
@@ -303,12 +324,15 @@
         return (occInstanceToEffecter.empty() ? false : true);
     }
 
-    /** @brief Query PLDM for the MCTP requester instance id
+    /** @brief Get a PLDM requester instance id
      *
      * @return true if the id was found and false if not
      */
     bool getPldmInstanceId();
 
+    /** @brief Free PLDM requester instance id */
+    void freePldmInstanceId();
+
     /** @brief Encode a GetStateSensor command into a PLDM request
      *  @param[in] instance - OCC instance number
      *  @param[in] sensorId - OCC Active sensor ID number