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