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