pldm: Convert to using libpldm transport APIs

A significant amount of logic can be removed by exploiting the new
transport APIs provided by libpldm. Switch the pldm repository over to
use these by introducing an RAII wrapper for the APIs. The current
stance is to continue using the legacy mctp-demux transport
implementation, but we also provide a build option to switch to the
AF_MCTP transport.

We don't currently have the infrastructure in place to get the correct
TIDs, so to keep everything working as before use the EID as the TID in
the EID-to-TID mapping.

Change-Id: I366f079082b102cfc0e90db0f62208581eb8693e
Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
Signed-off-by: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Signed-off-by: Konstantin Aladyshev <aladyshev22@gmail.com>
diff --git a/utilities/requester/set_state_effecter_async.cpp b/utilities/requester/set_state_effecter_async.cpp
index 8a7a041..bf905e3 100644
--- a/utilities/requester/set_state_effecter_async.cpp
+++ b/utilities/requester/set_state_effecter_async.cpp
@@ -1,6 +1,7 @@
+#include "common/transport.hpp"
+
 #include <libpldm/base.h>
 #include <libpldm/platform.h>
-#include <libpldm/pldm.h>
 
 #include <CLI/CLI.hpp>
 #include <phosphor-logging/lg2.hpp>
@@ -25,6 +26,8 @@
     app.add_option("-s,--state", state, "New state value")->required();
     CLI11_PARSE(app, argc, argv);
 
+    pldm_tid_t dstTid = static_cast<pldm_tid_t>(mctpEid);
+
     // Encode PLDM Request message
     uint8_t effecterCount = 1;
     std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterId) +
@@ -42,42 +45,44 @@
         return -1;
     }
 
-    // Get fd of MCTP socket
-    int fd = pldm_open();
-    if (-1 == fd)
-    {
-        error("Failed to init mctp");
-        return -1;
-    }
+    PldmTransport pldmTransport{};
 
     // Create event loop and add a callback to handle EPOLLIN on fd
     auto event = Event::get_default();
-    auto callback = [=](IO& io, int fd, uint32_t revents) {
+    auto callback =
+        [=, &pldmTransport](IO& io, int fd, uint32_t revents) mutable {
         if (!(revents & EPOLLIN))
         {
             return;
         }
 
-        uint8_t* responseMsg = nullptr;
-        size_t responseMsgSize{};
-        auto rc = pldm_recv(mctpEid, fd, request->hdr.instance_id, &responseMsg,
-                            &responseMsgSize);
-        if (!rc)
+        if (pldmTransport.getEventSource() != fd)
         {
-            // We've got the response meant for the PLDM request msg that was
-            // sent out
-            io.set_enabled(Enabled::Off);
-            pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg);
-            info("Done. PLDM RC = {RC}", "RC", lg2::hex,
-                 static_cast<uint16_t>(response->payload[0]));
-            free(responseMsg);
-            exit(EXIT_SUCCESS);
+            return;
         }
-    };
-    IO io(event, fd, EPOLLIN, std::move(callback));
 
-    // Send PLDM Request message - pldm_send doesn't wait for response
-    rc = pldm_send(mctpEid, fd, requestMsg.data(), requestMsg.size());
+        void* responseMsg = nullptr;
+        size_t responseMsgSize{};
+        pldm_tid_t srcTid;
+        auto rc = pldmTransport.recvMsg(srcTid, responseMsg, responseMsgSize);
+        pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg);
+        if (rc || dstTid != srcTid ||
+            !pldm_msg_hdr_correlate_response(&request->hdr, &response->hdr))
+        {
+            return;
+        }
+
+        // We've got the response meant for the PLDM request msg that was sent
+        // out
+        io.set_enabled(Enabled::Off);
+        info("Done. PLDM RC = {RC}", "RC", lg2::hex,
+             static_cast<uint16_t>(response->payload[0]));
+        free(responseMsg);
+        exit(EXIT_SUCCESS);
+    };
+    IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback));
+
+    rc = pldmTransport.sendMsg(dstTid, requestMsg.data(), requestMsg.size());
     if (0 > rc)
     {
         error(