xref: /openbmc/pldm/pldmtool/pldm_cmd_helper.hpp (revision 579a34a3)
1 #pragma once
2 
3 #include "libpldm/base.h"
4 #include "libpldm/bios.h"
5 #include "libpldm/fru.h"
6 #include "libpldm/platform.h"
7 
8 #include "common/utils.hpp"
9 
10 #include <err.h>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 #include <unistd.h>
14 
15 #include <CLI/CLI.hpp>
16 
17 #include <cstring>
18 #include <iomanip>
19 #include <iostream>
20 #include <utility>
21 
22 namespace pldmtool
23 {
24 
25 namespace helper
26 {
27 
28 using namespace pldm::utils;
29 constexpr uint8_t PLDM_ENTITY_ID = 8;
30 constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
31 
32 /** @brief Print the buffer
33  *
34  *  @param[in]  buffer  - Buffer to print
35  *  @param[in]  pldmVerbose -verbosity flag - true/false
36  *
37  *  @return - None
38  */
39 void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose);
40 
41 /** @brief print the input message if pldmverbose is enabled
42  *
43  *  @param[in]  pldmVerbose - verbosity flag - true/false
44  *  @param[in]  msg         - message to print
45  *  @param[in]  data        - data to print
46  *
47  *  @return - None
48  */
49 
50 template <class T>
51 void Logger(bool pldmverbose, const char* msg, const T& data)
52 {
53     if (pldmverbose)
54     {
55         std::stringstream s;
56         s << data;
57         std::cout << msg << s.str() << std::endl;
58     }
59 }
60 /** @brief MCTP socket read/recieve
61  *
62  *  @param[in]  requestMsg - Request message to compare against loopback
63  *              message recieved from mctp socket
64  *  @param[out] responseMsg - Response buffer recieved from mctp socket
65  *  @param[in]  pldmVerbose - verbosity flag - true/false
66  *
67  *  @return -   0 on success.
68  *             -1 or -errno on failure.
69  */
70 int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
71                      std::vector<uint8_t>& responseMsg, bool pldmVerbose);
72 
73 class CommandInterface
74 {
75 
76   public:
77     explicit CommandInterface(const char* type, const char* name,
78                               CLI::App* app) :
79         pldmType(type),
80         commandName(name), mctp_eid(PLDM_ENTITY_ID), instanceId(0)
81     {
82         app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
83         app->add_flag("-v, --verbose", pldmVerbose);
84         app->callback([&]() { exec(); });
85     }
86 
87     virtual ~CommandInterface() = default;
88 
89     virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0;
90 
91     virtual void parseResponseMsg(struct pldm_msg* responsePtr,
92                                   size_t payloadLength) = 0;
93 
94     virtual void exec();
95 
96     int pldmSendRecv(std::vector<uint8_t>& requestMsg,
97                      std::vector<uint8_t>& responseMsg);
98 
99   private:
100     const std::string pldmType;
101     const std::string commandName;
102     uint8_t mctp_eid;
103     bool pldmVerbose;
104 
105   protected:
106     uint8_t instanceId;
107 };
108 
109 } // namespace helper
110 } // namespace pldmtool
111