xref: /openbmc/pldm/pldmtool/pldm_cmd_helper.hpp (revision a792b216)
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), pldmVerbose(false),
81         instanceId(0)
82     {
83         app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
84         app->add_flag("-v, --verbose", pldmVerbose);
85         app->callback([&]() { exec(); });
86     }
87 
88     virtual ~CommandInterface() = default;
89 
90     virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0;
91 
92     virtual void parseResponseMsg(struct pldm_msg* responsePtr,
93                                   size_t payloadLength) = 0;
94 
95     virtual void exec();
96 
97     int pldmSendRecv(std::vector<uint8_t>& requestMsg,
98                      std::vector<uint8_t>& responseMsg);
99 
100   private:
101     const std::string pldmType;
102     const std::string commandName;
103     uint8_t mctp_eid;
104     bool pldmVerbose;
105 
106   protected:
107     uint8_t instanceId;
108 };
109 
110 } // namespace helper
111 } // namespace pldmtool
112