requester: Add new APIs to support multiple transports

This patch has two goals: (1) enable consumers to send PLDM messages
over different transports and (2) do this in a way that allows us to
move towards a cleaner and more complete set of requester APIs.

The sole transport option of MCTP via the userspace mctp-demux-daemon is
being deprecated. New transports are being added: MCTP via the kernel
(AF_MCTP) and eventually NC-SI. As such, the requester APIs need
updating to support multiple transports, as well as not having MCTP
specific details in the APIs. To avoid a flag day, the current APIs
(pldm_send, etc) have been rewritten terms of the new APIs.

The current APIs operate at the transport level - they don't implement
all of the behaviour necessary for a requester. As such, the new APIs to
send/recv a message have the prefix `pldm_transport`, rather than
`pldm_requester`. Given the level that these APIs are operating at,
these only send and receive a PLDM message.  Any additional logic, such
as looking for a response with a particular instance ID, belongs at the
requester abstraction level.

Some of the missing behaviours to fully be a PLDM requester are:
assigning instance IDs, request retransmission, implementing timeouts,
and enforcing only one PLDM request to a specific TID at a time. These
things are currently implemented in pldmd, meaning any consumer other
than pldmd using the libpldm "requester" APIs doesn't get the full
functionality of a requester and has to implement these things
themselves.

We would like to eventually move these behaviours into libpldm so the
libpldm requester APIs actually implement what is required to be a PLDM
requester.

The next steps to add in a full set of requester APIs, while enabling
the use of multiple transports looks something like this:

1) add instance id APIs into libpldm.
2) convert pldmd to use libpldm instance id APIs - so all users of PLDM
   are still using the same instance id allocation method.
3) convert all consumers of libpldm over to using the new libpldm APIs,
   including the instance id functions.
4) add in the AF_MCTP transport and move consumers over to it.
5) refactor the encode/decode functions to only encode/decode and not
   frame the message (ie, remove the instance id from these functions)
6) add additional requester functionality into libpldm, and have these
   use the `pldm_transport` APIs directly.
7) move consumers over to the new `pldm_requester` APIs.
8) remove unused code from pldmd.

Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
Change-Id: I06e602831f360bbd0efda53d410bfb5080b3100d
diff --git a/src/transport/transport.c b/src/transport/transport.c
new file mode 100644
index 0000000..0681e23
--- /dev/null
+++ b/src/transport/transport.c
@@ -0,0 +1,130 @@
+#include "libpldm/transport.h"
+#include "base.h"
+#include "libpldm/requester/pldm.h"
+#include "transport.h"
+
+#ifdef PLDM_HAS_POLL
+#include <poll.h>
+#endif
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifndef PLDM_HAS_POLL
+struct pollfd {
+	int fd;	       /* file descriptor */
+	short events;  /* requested events */
+	short revents; /* returned events */
+};
+
+static inline int poll(struct pollfd *fds __attribute__((unused)),
+		       int nfds __attribute__((unused)),
+		       int timeout __attribute__((unused)))
+{
+	return 0;
+}
+#endif
+
+pldm_requester_rc_t pldm_transport_poll(struct pldm_transport *transport,
+					int timeout)
+{
+	struct pollfd pollfd;
+	int rc = 0;
+	if (!transport) {
+		return PLDM_REQUESTER_INVALID_SETUP;
+	}
+	if (!transport->init_pollfd) {
+		return PLDM_REQUESTER_SUCCESS;
+	}
+
+	transport->init_pollfd(transport, &pollfd);
+	rc = poll(&pollfd, 1, timeout);
+	if (rc < 0) {
+		return PLDM_REQUESTER_POLL_FAIL;
+	}
+
+	return PLDM_REQUESTER_SUCCESS;
+}
+
+pldm_requester_rc_t pldm_transport_send_msg(struct pldm_transport *transport,
+					    pldm_tid_t tid,
+					    const void *pldm_req_msg,
+					    size_t req_msg_len)
+{
+	if (!transport || !pldm_req_msg) {
+		return PLDM_REQUESTER_INVALID_SETUP;
+	}
+
+	if (req_msg_len < sizeof(struct pldm_msg_hdr)) {
+		return PLDM_REQUESTER_NOT_REQ_MSG;
+	}
+
+	const struct pldm_msg_hdr *hdr = pldm_req_msg;
+	if (!hdr->request) {
+		return PLDM_REQUESTER_NOT_REQ_MSG;
+	}
+
+	return transport->send(transport, tid, pldm_req_msg, req_msg_len);
+}
+
+pldm_requester_rc_t pldm_transport_recv_msg(struct pldm_transport *transport,
+					    pldm_tid_t tid,
+					    void **pldm_resp_msg,
+					    size_t *resp_msg_len)
+{
+	if (!transport || !resp_msg_len) {
+		return PLDM_REQUESTER_INVALID_SETUP;
+	}
+
+	pldm_requester_rc_t rc =
+	    transport->recv(transport, tid, pldm_resp_msg, resp_msg_len);
+	if (rc != PLDM_REQUESTER_SUCCESS) {
+		return rc;
+	}
+
+	struct pldm_msg_hdr *hdr = *pldm_resp_msg;
+	if (hdr->request || hdr->datagram) {
+		free(*pldm_resp_msg);
+		*pldm_resp_msg = NULL;
+		return PLDM_REQUESTER_NOT_RESP_MSG;
+	}
+
+	uint8_t pldm_rc = 0;
+	if (*resp_msg_len < (sizeof(struct pldm_msg_hdr) + sizeof(pldm_rc))) {
+		free(*pldm_resp_msg);
+		*pldm_resp_msg = NULL;
+		return PLDM_REQUESTER_RESP_MSG_TOO_SMALL;
+	}
+
+	return PLDM_REQUESTER_SUCCESS;
+}
+
+pldm_requester_rc_t
+pldm_transport_send_recv_msg(struct pldm_transport *transport, pldm_tid_t tid,
+			     const void *pldm_req_msg, size_t req_msg_len,
+			     void **pldm_resp_msg, size_t *resp_msg_len)
+
+{
+	if (!resp_msg_len) {
+		return PLDM_REQUESTER_INVALID_SETUP;
+	}
+
+	pldm_requester_rc_t rc =
+	    pldm_transport_send_msg(transport, tid, pldm_req_msg, req_msg_len);
+	if (rc != PLDM_REQUESTER_SUCCESS) {
+		return rc;
+	}
+
+	while (1) {
+		rc = pldm_transport_poll(transport, -1);
+		if (rc != PLDM_REQUESTER_SUCCESS) {
+			break;
+		}
+		rc = pldm_transport_recv_msg(transport, tid, pldm_resp_msg,
+					     resp_msg_len);
+		if (rc == PLDM_REQUESTER_SUCCESS) {
+			break;
+		}
+	}
+
+	return rc;
+}