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