xref: /openbmc/pldm/common/transport.hpp (revision b3b84b497a20c33f5c49e67e89c20638929166a3)
11ed5f7a6SRashmica Gupta #pragma once
21ed5f7a6SRashmica Gupta 
31ed5f7a6SRashmica Gupta #include <libpldm/base.h>
41ed5f7a6SRashmica Gupta #include <libpldm/pldm.h>
51ed5f7a6SRashmica Gupta #include <poll.h>
6*b3b84b49SPavithra Barithaya 
7*b3b84b49SPavithra Barithaya #include <cstddef>
81ed5f7a6SRashmica Gupta 
91ed5f7a6SRashmica Gupta struct pldm_transport_mctp_demux;
101ed5f7a6SRashmica Gupta struct pldm_transport_af_mctp;
111ed5f7a6SRashmica Gupta 
121ed5f7a6SRashmica Gupta union TransportImpl
131ed5f7a6SRashmica Gupta {
141ed5f7a6SRashmica Gupta     struct pldm_transport_mctp_demux* mctp_demux;
151ed5f7a6SRashmica Gupta     struct pldm_transport_af_mctp* af_mctp;
161ed5f7a6SRashmica Gupta };
171ed5f7a6SRashmica Gupta 
181ed5f7a6SRashmica Gupta /* RAII for pldm_transport */
191ed5f7a6SRashmica Gupta class PldmTransport
201ed5f7a6SRashmica Gupta {
211ed5f7a6SRashmica Gupta   public:
221ed5f7a6SRashmica Gupta     PldmTransport();
231ed5f7a6SRashmica Gupta     PldmTransport(const PldmTransport& other) = delete;
241ed5f7a6SRashmica Gupta     PldmTransport(const PldmTransport&& other) = delete;
251ed5f7a6SRashmica Gupta     PldmTransport& operator=(const PldmTransport& other) = delete;
261ed5f7a6SRashmica Gupta     PldmTransport& operator=(const PldmTransport&& other) = delete;
271ed5f7a6SRashmica Gupta     ~PldmTransport();
281ed5f7a6SRashmica Gupta 
291ed5f7a6SRashmica Gupta     /** @brief Provides a file descriptor that can be polled for readiness.
301ed5f7a6SRashmica Gupta      *
311ed5f7a6SRashmica Gupta      * Readiness generally indicates that a call to recvMsg() will immediately
321ed5f7a6SRashmica Gupta      * yield a message.
331ed5f7a6SRashmica Gupta      *
341ed5f7a6SRashmica Gupta      * @return The relevant file descriptor.
351ed5f7a6SRashmica Gupta      */
361ed5f7a6SRashmica Gupta     int getEventSource() const;
371ed5f7a6SRashmica Gupta 
381ed5f7a6SRashmica Gupta     /** @brief Asynchronously send a PLDM message to the specified terminus
391ed5f7a6SRashmica Gupta      *
401ed5f7a6SRashmica Gupta      * The message may be either a request or a response.
411ed5f7a6SRashmica Gupta      *
421ed5f7a6SRashmica Gupta      * @param[in] tid - The terminus ID of the message destination
431ed5f7a6SRashmica Gupta      * @param[in] tx - The encoded and framed message to send
441ed5f7a6SRashmica Gupta      * @param[in] len - The length of the buffer pointed-to by tx
451ed5f7a6SRashmica Gupta      *
461ed5f7a6SRashmica Gupta      * @return PLDM_REQUESTER_SUCCESS on success, otherwise an appropriate
471ed5f7a6SRashmica Gupta      *         PLDM_REQUESTER_* error code.
481ed5f7a6SRashmica Gupta      */
491ed5f7a6SRashmica Gupta     pldm_requester_rc_t sendMsg(pldm_tid_t tid, const void* tx, size_t len);
501ed5f7a6SRashmica Gupta 
511ed5f7a6SRashmica Gupta     /** @brief Asynchronously receive a PLDM message addressed to the local
521ed5f7a6SRashmica Gupta      * terminus
531ed5f7a6SRashmica Gupta      *
541ed5f7a6SRashmica Gupta      * The message may be either a request or a response.
551ed5f7a6SRashmica Gupta      *
561ed5f7a6SRashmica Gupta      * @param[out] tid - The terminus ID of the message source
571ed5f7a6SRashmica Gupta      * @param[out] rx - A pointer to the received, encoded message
581ed5f7a6SRashmica Gupta      * @param[out] len - The length of the buffer pointed-to by rx
591ed5f7a6SRashmica Gupta      *
601ed5f7a6SRashmica Gupta      * @return PLDM_REQUESTER_SUCCESS on success, otherwise an appropriate
611ed5f7a6SRashmica Gupta      *         PLDM_REQUESTER_* error code.
621ed5f7a6SRashmica Gupta      */
631ed5f7a6SRashmica Gupta     pldm_requester_rc_t recvMsg(pldm_tid_t& tid, void*& rx, size_t& len);
641ed5f7a6SRashmica Gupta 
651ed5f7a6SRashmica Gupta     /** @brief Synchronously exchange a request and response with the specified
661ed5f7a6SRashmica Gupta      * terminus.
671ed5f7a6SRashmica Gupta      *
681ed5f7a6SRashmica Gupta      * sendRecvMsg() is a wrapper for the non-compliant
691ed5f7a6SRashmica Gupta      * pldm_transport_send_recv_msg() API from libpldm. It is a crutch that may
701ed5f7a6SRashmica Gupta      * be used for to fulfil a PLDM request until libpldm implements a correct
711ed5f7a6SRashmica Gupta      * requester flow in accordance with the PLDM base specification (DSP0240).
721ed5f7a6SRashmica Gupta      *
731ed5f7a6SRashmica Gupta      * The implementation blocks after the request is sent until a response is
741ed5f7a6SRashmica Gupta      * received, or the upper time-bound on a PLDM exchange is reached. Control
751ed5f7a6SRashmica Gupta      * is only handed back to the caller once one of these two outcomes is
761ed5f7a6SRashmica Gupta      * achieved.
771ed5f7a6SRashmica Gupta      *
781ed5f7a6SRashmica Gupta      * @param[in] tid - The terminus ID of the endpoint with which the exchange
791ed5f7a6SRashmica Gupta      *                  will occur
801ed5f7a6SRashmica Gupta      * @param[in] tx - The encoded and framed message to send
811ed5f7a6SRashmica Gupta      * @param[in] txLen - The length of the buffer pointed-to by tx
821ed5f7a6SRashmica Gupta      * @param[out] rx - A pointer to the received, encoded message
831ed5f7a6SRashmica Gupta      * @param[out] rxLen - The length of the buffer pointed-to by rx
841ed5f7a6SRashmica Gupta      *
851ed5f7a6SRashmica Gupta      * @return PLDM_REQUESTER_SUCCESS on success, otherwise an appropriate
861ed5f7a6SRashmica Gupta      *         PLDM_REQUESTER_* error code.
871ed5f7a6SRashmica Gupta      */
881ed5f7a6SRashmica Gupta     pldm_requester_rc_t sendRecvMsg(pldm_tid_t tid, const void* tx,
891ed5f7a6SRashmica Gupta                                     size_t txLen, void*& rx, size_t& rxLen);
901ed5f7a6SRashmica Gupta 
911ed5f7a6SRashmica Gupta   private:
921ed5f7a6SRashmica Gupta     /** @brief A pollfd object for holding a file descriptor from the libpldm
931ed5f7a6SRashmica Gupta      *         transport implementation
941ed5f7a6SRashmica Gupta      */
951ed5f7a6SRashmica Gupta     pollfd pfd;
961ed5f7a6SRashmica Gupta 
971ed5f7a6SRashmica Gupta     /** @brief A union holding an appropriately-typed pointer to the selected
981ed5f7a6SRashmica Gupta      *         libpldm transport implementation
991ed5f7a6SRashmica Gupta      */
1001ed5f7a6SRashmica Gupta     TransportImpl impl;
1011ed5f7a6SRashmica Gupta 
1021ed5f7a6SRashmica Gupta     /** @brief The abstract libpldm transport object for sending and receiving
1031ed5f7a6SRashmica Gupta      *         PLDM messages.
1041ed5f7a6SRashmica Gupta      */
1051ed5f7a6SRashmica Gupta     struct pldm_transport* transport;
1061ed5f7a6SRashmica Gupta };
107