Move to libpldm pldm_transport APIs

- Replaced the deprecated pldm transport APIs with the new libpldm
  pldm_transport APIs.

This change migrates the application off of the deprecated "requester"
APIs in libpldm.

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: Iedbfe936a710d37f75e737c3d307295ff55cf07b
Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
Signed-off-by: Lakshmi Yadlapati <lakshmiy@us.ibm.com>
diff --git a/meson.build b/meson.build
index 777f4d7..bbe2c33 100644
--- a/meson.build
+++ b/meson.build
@@ -43,6 +43,10 @@
 conf_data.set('PLDM', get_option('with-host-communication-protocol')=='pldm')
 conf_data.set('POWER10', get_option('power10-support').allowed())
 
+if cxx.has_header('poll.h')
+    conf_data.set('PLDM_HAS_POLL', 1)
+endif
+
 configure_file(output: 'config.h',
     configuration: conf_data
 )
diff --git a/pldm.cpp b/pldm.cpp
index 5ee626a..b573d66 100644
--- a/pldm.cpp
+++ b/pldm.cpp
@@ -8,6 +8,9 @@
 #include <libpldm/oem/ibm/state_set.h>
 #include <libpldm/platform.h>
 #include <libpldm/state_set.h>
+#include <libpldm/transport.h>
+#include <libpldm/transport/mctp-demux.h>
+#include <poll.h>
 
 #include <phosphor-logging/log.hpp>
 #include <sdbusplus/bus.hpp>
@@ -580,6 +583,66 @@
     }
 }
 
+int Interface::openMctpDemuxTransport()
+{
+    mctpDemux = nullptr;
+    int rc = pldm_transport_mctp_demux_init(&mctpDemux);
+    if (rc)
+    {
+        log<level::ERR>(
+            std::format(
+                "openMctpDemuxTransport: Failed to init MCTP demux transport, errno={}/{}",
+                rc, strerror(rc))
+                .c_str());
+        return -1;
+    }
+
+    if (pldm_transport_mctp_demux_map_tid(mctpDemux, mctpEid, mctpEid))
+    {
+        log<level::ERR>(
+            std::format(
+                "openMctpDemuxTransport: Failed to setup tid to eid mapping, errno={}/{}",
+                errno, strerror(errno))
+                .c_str());
+        pldmClose();
+        return -1;
+    }
+    pldmTransport = pldm_transport_mctp_demux_core(mctpDemux);
+
+    struct pollfd pollfd;
+    if (pldm_transport_mctp_demux_init_pollfd(pldmTransport, &pollfd))
+    {
+        log<level::ERR>(
+            std::format(
+                "openMctpDemuxTransport: Failed to get pollfd , errno={}/{}",
+                errno, strerror(errno))
+                .c_str());
+        pldmClose();
+        return -1;
+    }
+    pldmFd = pollfd.fd;
+    if (!throttleTraces)
+    {
+        log<level::INFO>(
+            std::format("openMctpDemuxTransport: pldmFd has fd={}", pldmFd)
+                .c_str());
+    }
+    return 0;
+}
+
+int Interface::pldmOpen()
+{
+    if (pldmTransport)
+    {
+        log<level::ERR>(
+            std::format("pldmOpen: pldmTransport already setup!, errno={}/{}",
+                        errno, strerror(errno))
+                .c_str());
+        return -1;
+    }
+    return openMctpDemuxTransport();
+}
+
 void Interface::sendPldm(const std::vector<uint8_t>& request,
                          const uint8_t instance, const bool rspExpected)
 {
@@ -589,20 +652,16 @@
         return;
     }
 
-    // Connect to MCTP socket
-    pldmFd = pldm_open();
-    auto openErrno = errno;
-    if (pldmFd == PLDM_REQUESTER_OPEN_FAIL)
+    auto rc = pldmOpen();
+    if (rc)
     {
         log<level::ERR>(
-            std::format(
-                "sendPldm: Failed to connect to MCTP socket, errno={}/{}",
-                openErrno, strerror(openErrno))
-                .c_str());
+            std::format("sendPldm: pldmOpen failed rc={}", rc).c_str());
         freePldmInstanceId();
         return;
     }
 
+    pldm_tid_t pldmTID = static_cast<pldm_tid_t>(mctpEid);
     // Send the PLDM request message to HBRT
     if (rspExpected)
     {
@@ -614,21 +673,21 @@
         {
             log<level::INFO>(
                 std::format(
-                    "sendPldm: calling pldm_send(OCC{}, instance:{}, {} bytes)",
+                    "sendPldm: calling pldm_transport_send_msg(OCC{}, instance:{}, {} bytes)",
                     instance, pldmInstanceID.value(), request.size())
                     .c_str());
         }
         pldmResponseReceived = false;
         pldmResponseTimeout = false;
         pldmResponseOcc = instance;
-        auto pldmRc = pldm_send(mctpEid, pldmFd, request.data(),
-                                request.size());
+        auto pldmRc = pldm_transport_send_msg(pldmTransport, pldmTID,
+                                              request.data(), request.size());
         auto sendErrno = errno;
         if (pldmRc != PLDM_REQUESTER_SUCCESS)
         {
             log<level::ERR>(
                 std::format(
-                    "sendPldm: pldm_send failed with rc={} and errno={}/{}",
+                    "sendPldm: pldm_transport_send_msg failed with rc={} and errno={}/{}",
                     static_cast<
                         std::underlying_type_t<pldm_requester_error_codes>>(
                         pldmRc),
@@ -650,17 +709,18 @@
         {
             log<level::INFO>(
                 std::format(
-                    "sendPldm: calling pldm_send(mctpID:{}, fd:{}, {} bytes) for OCC{}",
+                    "sendPldm: calling pldm_transport_send_msg(mctpID:{}, fd:{}, {} bytes) for OCC{}",
                     mctpEid, pldmFd, request.size(), instance)
                     .c_str());
         }
-        auto rc = pldm_send(mctpEid, pldmFd, request.data(), request.size());
+        auto rc = pldm_transport_send_msg(pldmTransport, pldmTID,
+                                          request.data(), request.size());
         auto sendErrno = errno;
         if (rc)
         {
             log<level::ERR>(
                 std::format(
-                    "sendPldm: pldm_send(mctpID:{}, fd:{}, {} bytes) failed with rc={} and errno={}/{}",
+                    "sendPldm: pldm_transport_send_msg(mctpID:{}, fd:{}, {} bytes) failed with rc={} and errno={}/{}",
                     mctpEid, pldmFd, request.size(),
                     static_cast<
                         std::underlying_type_t<pldm_requester_error_codes>>(rc),
@@ -722,13 +782,17 @@
         // stop PLDM response timer
         pldmRspTimer.setEnabled(false);
     }
-    pldm_close();
+
+    pldm_transport_mctp_demux_destroy(mctpDemux);
+    mctpDemux = NULL;
     pldmFd = -1;
+    pldmTransport = NULL;
     eventSource.reset();
 }
 
-int Interface::pldmRspCallback(sd_event_source* /*es*/, int fd,
-                               uint32_t revents, void* userData)
+int Interface::pldmRspCallback(sd_event_source* /*es*/,
+                               __attribute__((unused)) int fd, uint32_t revents,
+                               void* userData)
 {
     if (!(revents & EPOLLIN))
     {
@@ -748,16 +812,18 @@
 
     uint8_t* responseMsg = nullptr;
     size_t responseMsgSize{};
+    pldm_tid_t pldmTID = static_cast<pldm_tid_t>(mctpEid);
 
     if (!throttleTraces)
     {
         log<level::INFO>(
-            std::format("pldmRspCallback: calling pldm_recv() instance:{}",
-                        pldmIface->pldmInstanceID.value())
+            std::format(
+                "pldmRspCallback: calling pldm_transport_recv_msg() instance:{}",
+                pldmIface->pldmInstanceID.value())
                 .c_str());
     }
-    auto rc = pldm_recv(mctpEid, fd, pldmIface->pldmInstanceID.value(),
-                        &responseMsg, &responseMsgSize);
+    auto rc = pldm_transport_recv_msg(pldmIface->pldmTransport, &pldmTID,
+                                      (void**)&responseMsg, &responseMsgSize);
     int lastErrno = errno;
     if (rc)
     {
@@ -765,7 +831,7 @@
         {
             log<level::ERR>(
                 std::format(
-                    "pldmRspCallback: pldm_recv failed with rc={}, errno={}/{}",
+                    "pldmRspCallback: pldm_transport_recv_msg failed with rc={}, errno={}/{}",
                     static_cast<
                         std::underlying_type_t<pldm_requester_error_codes>>(rc),
                     lastErrno, strerror(lastErrno))
@@ -778,8 +844,9 @@
     if (!throttleTraces)
     {
         log<level::INFO>(
-            std::format("pldmRspCallback: pldm_recv() rsp was {} bytes",
-                        responseMsgSize)
+            std::format(
+                "pldmRspCallback: pldm_transport_recv_msg() rsp was {} bytes",
+                responseMsgSize)
                 .c_str());
     }
 
diff --git a/pldm.hpp b/pldm.hpp
index 641153f..1f1bad1 100644
--- a/pldm.hpp
+++ b/pldm.hpp
@@ -6,6 +6,8 @@
 
 #include <libpldm/instance-id.h>
 #include <libpldm/pldm.h>
+#include <libpldm/transport.h>
+#include <libpldm/transport/mctp-demux.h>
 
 #include <sdbusplus/bus/match.hpp>
 #include <sdeventplus/event.hpp>
@@ -29,15 +31,15 @@
 using SensorToInstance = std::map<SensorID, open_power::occ::instanceID>;
 using TerminusID = uint8_t;
 
-/** @brief Hardcoded TID */
-constexpr TerminusID tid = 0;
-
 /** @brief OCC instance starts with 0 for example "occ0" */
 constexpr open_power::occ::instanceID start = 0;
 
 /** @brief Hardcoded mctpEid for HBRT */
 constexpr mctp_eid_t mctpEid = 10;
 
+/** @brief Hardcoded TID */
+constexpr TerminusID tid = mctpEid;
+
 /** @class Interface
  *
  *  @brief Abstracts the PLDM details related to the OCC
@@ -252,6 +254,11 @@
     /** @brief File descriptor for PLDM messages */
     int pldmFd = -1;
 
+    /** pldm transport instance  */
+    struct pldm_transport* pldmTransport = NULL;
+
+    struct pldm_transport_mctp_demux* mctpDemux;
+
     /** @brief The response for the PLDM request msg is received flag.
      */
     bool pldmResponseReceived = false;
@@ -341,6 +348,19 @@
      */
     std::vector<uint8_t> encodeGetStateSensorRequest(uint8_t instance,
                                                      uint16_t sensorId);
+
+    /** @brief setup PLDM transport for sending and receiving PLDM messages.
+     *
+     * @return true on success, otherwise return false
+     */
+    int pldmOpen(void);
+
+    /** @brief Opens the MCTP socket for sending and receiving messages.
+     *
+     * @return true on success, otherwise returns a negative error code
+     */
+    int openMctpDemuxTransport();
+
     /** @brief Send the PLDM request
      *
      * @param[in] request - the request data