xref: /openbmc/pldm/common/transport.hpp (revision b3b84b49)
1 #pragma once
2 
3 #include <libpldm/base.h>
4 #include <libpldm/pldm.h>
5 #include <poll.h>
6 
7 #include <cstddef>
8 
9 struct pldm_transport_mctp_demux;
10 struct pldm_transport_af_mctp;
11 
12 union TransportImpl
13 {
14     struct pldm_transport_mctp_demux* mctp_demux;
15     struct pldm_transport_af_mctp* af_mctp;
16 };
17 
18 /* RAII for pldm_transport */
19 class PldmTransport
20 {
21   public:
22     PldmTransport();
23     PldmTransport(const PldmTransport& other) = delete;
24     PldmTransport(const PldmTransport&& other) = delete;
25     PldmTransport& operator=(const PldmTransport& other) = delete;
26     PldmTransport& operator=(const PldmTransport&& other) = delete;
27     ~PldmTransport();
28 
29     /** @brief Provides a file descriptor that can be polled for readiness.
30      *
31      * Readiness generally indicates that a call to recvMsg() will immediately
32      * yield a message.
33      *
34      * @return The relevant file descriptor.
35      */
36     int getEventSource() const;
37 
38     /** @brief Asynchronously send a PLDM message to the specified terminus
39      *
40      * The message may be either a request or a response.
41      *
42      * @param[in] tid - The terminus ID of the message destination
43      * @param[in] tx - The encoded and framed message to send
44      * @param[in] len - The length of the buffer pointed-to by tx
45      *
46      * @return PLDM_REQUESTER_SUCCESS on success, otherwise an appropriate
47      *         PLDM_REQUESTER_* error code.
48      */
49     pldm_requester_rc_t sendMsg(pldm_tid_t tid, const void* tx, size_t len);
50 
51     /** @brief Asynchronously receive a PLDM message addressed to the local
52      * terminus
53      *
54      * The message may be either a request or a response.
55      *
56      * @param[out] tid - The terminus ID of the message source
57      * @param[out] rx - A pointer to the received, encoded message
58      * @param[out] len - The length of the buffer pointed-to by rx
59      *
60      * @return PLDM_REQUESTER_SUCCESS on success, otherwise an appropriate
61      *         PLDM_REQUESTER_* error code.
62      */
63     pldm_requester_rc_t recvMsg(pldm_tid_t& tid, void*& rx, size_t& len);
64 
65     /** @brief Synchronously exchange a request and response with the specified
66      * terminus.
67      *
68      * sendRecvMsg() is a wrapper for the non-compliant
69      * pldm_transport_send_recv_msg() API from libpldm. It is a crutch that may
70      * be used for to fulfil a PLDM request until libpldm implements a correct
71      * requester flow in accordance with the PLDM base specification (DSP0240).
72      *
73      * The implementation blocks after the request is sent until a response is
74      * received, or the upper time-bound on a PLDM exchange is reached. Control
75      * is only handed back to the caller once one of these two outcomes is
76      * achieved.
77      *
78      * @param[in] tid - The terminus ID of the endpoint with which the exchange
79      *                  will occur
80      * @param[in] tx - The encoded and framed message to send
81      * @param[in] txLen - The length of the buffer pointed-to by tx
82      * @param[out] rx - A pointer to the received, encoded message
83      * @param[out] rxLen - The length of the buffer pointed-to by rx
84      *
85      * @return PLDM_REQUESTER_SUCCESS on success, otherwise an appropriate
86      *         PLDM_REQUESTER_* error code.
87      */
88     pldm_requester_rc_t sendRecvMsg(pldm_tid_t tid, const void* tx,
89                                     size_t txLen, void*& rx, size_t& rxLen);
90 
91   private:
92     /** @brief A pollfd object for holding a file descriptor from the libpldm
93      *         transport implementation
94      */
95     pollfd pfd;
96 
97     /** @brief A union holding an appropriately-typed pointer to the selected
98      *         libpldm transport implementation
99      */
100     TransportImpl impl;
101 
102     /** @brief The abstract libpldm transport object for sending and receiving
103      *         PLDM messages.
104      */
105     struct pldm_transport* transport;
106 };
107