blob: e66897413fb041c14e3c649a095fa8c9fc27ff7d [file] [log] [blame]
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05001#pragma once
2
Dung Cao3d03f3f2023-09-07 06:51:33 +00003#include "common/instance_id.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05004#include "common/utils.hpp"
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05005
6#include <err.h>
George Liuc453e162022-12-21 17:16:23 +08007#include <libpldm/base.h>
8#include <libpldm/bios.h>
9#include <libpldm/fru.h>
10#include <libpldm/platform.h>
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050011#include <sys/socket.h>
12#include <sys/un.h>
13#include <unistd.h>
14
John Wang58a0e062019-11-08 15:38:15 +080015#include <CLI/CLI.hpp>
Sridevi Rameshca4a8152020-08-11 09:26:19 -050016#include <nlohmann/json.hpp>
George Liu6492f522020-06-16 10:34:05 +080017
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050018#include <cstring>
19#include <iomanip>
20#include <iostream>
John Wang58a0e062019-11-08 15:38:15 +080021#include <utility>
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050022
John Wang58a0e062019-11-08 15:38:15 +080023namespace pldmtool
24{
25
26namespace helper
27{
28
John Wang58a0e062019-11-08 15:38:15 +080029constexpr uint8_t PLDM_ENTITY_ID = 8;
Sridevi Rameshca4a8152020-08-11 09:26:19 -050030using ordered_json = nlohmann::ordered_json;
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050031
Sridevi Rameshc91822b2020-02-27 09:26:41 -060032/** @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
41template <class T>
42void 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}
Sridevi Rameshca4a8152020-08-11 09:26:19 -050051
52/** @brief Display in JSON format.
53 *
54 * @param[in] data - data to print in json
55 *
56 * @return - None
57 */
58static inline void DisplayInJson(const ordered_json& data)
59{
60 std::cout << data.dump(4) << std::endl;
61}
62
Manojkiran Eda2576aec2024-06-17 12:05:17 +053063/** @brief MCTP socket read/receive
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050064 *
65 * @param[in] requestMsg - Request message to compare against loopback
Manojkiran Eda2576aec2024-06-17 12:05:17 +053066 * message received from mctp socket
67 * @param[out] responseMsg - Response buffer received from mctp socket
Sridevi Rameshc91822b2020-02-27 09:26:41 -060068 * @param[in] pldmVerbose - verbosity flag - true/false
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050069 *
70 * @return - 0 on success.
71 * -1 or -errno on failure.
72 */
73int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
Sridevi Rameshc91822b2020-02-27 09:26:41 -060074 std::vector<uint8_t>& responseMsg, bool pldmVerbose);
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050075
John Wang58a0e062019-11-08 15:38:15 +080076class CommandInterface
77{
78 public:
79 explicit CommandInterface(const char* type, const char* name,
80 CLI::App* app) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040081 pldmType(type), commandName(name), mctp_eid(PLDM_ENTITY_ID),
82 pldmVerbose(false), instanceId(0)
John Wang58a0e062019-11-08 15:38:15 +080083 {
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080084 app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
Sridevi Rameshc91822b2020-02-27 09:26:41 -060085 app->add_flag("-v, --verbose", pldmVerbose);
Thu Nguyen94270892023-10-25 17:11:44 +070086 app->add_option("-n, --retry-count", numRetries,
87 "Number of retry when PLDM request message is failed");
John Wang58a0e062019-11-08 15:38:15 +080088 app->callback([&]() { exec(); });
89 }
Sridevi Rameshc91822b2020-02-27 09:26:41 -060090
John Wang58a0e062019-11-08 15:38:15 +080091 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
John Wangb754eee2020-02-15 16:10:25 +080098 virtual void exec();
99
100 int pldmSendRecv(std::vector<uint8_t>& requestMsg,
101 std::vector<uint8_t>& responseMsg);
John Wang58a0e062019-11-08 15:38:15 +0800102
Chinmay Shripad Hegde526461a2023-01-11 13:04:02 +0530103 /**
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
Pavithra Barithayaa340eed2023-02-14 03:32:47 -0600113 /**
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
John Wang58a0e062019-11-08 15:38:15 +0800133 private:
134 const std::string pldmType;
135 const std::string commandName;
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +0800136 uint8_t mctp_eid;
Sridevi Rameshc91822b2020-02-27 09:26:41 -0600137 bool pldmVerbose;
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600138
139 protected:
140 uint8_t instanceId;
Dung Cao3d03f3f2023-09-07 06:51:33 +0000141 pldm::InstanceIdDb instanceIdDb;
Thu Nguyen94270892023-10-25 17:11:44 +0700142 uint8_t numRetries = 0;
John Wang58a0e062019-11-08 15:38:15 +0800143};
144
145} // namespace helper
146} // namespace pldmtool