blob: d71d0d3c81d7d68da6a3ae3a3475433c114eeb92 [file] [log] [blame]
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05001#pragma once
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/base.h"
4#include "libpldm/bios.h"
5#include "libpldm/fru.h"
6#include "libpldm/platform.h"
7
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05008#include "common/utils.hpp"
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05009
10#include <err.h>
11#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
George Liu83409572019-12-24 18:42:54 +080029using namespace pldm::utils;
John Wang58a0e062019-11-08 15:38:15 +080030constexpr uint8_t PLDM_ENTITY_ID = 8;
31constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
Sridevi Rameshca4a8152020-08-11 09:26:19 -050032using ordered_json = nlohmann::ordered_json;
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050033
34/** @brief Print the buffer
35 *
36 * @param[in] buffer - Buffer to print
Sridevi Rameshc91822b2020-02-27 09:26:41 -060037 * @param[in] pldmVerbose -verbosity flag - true/false
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050038 *
39 * @return - None
40 */
Sridevi Rameshc91822b2020-02-27 09:26:41 -060041void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose);
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050042
Sridevi Rameshc91822b2020-02-27 09:26:41 -060043/** @brief print the input message if pldmverbose is enabled
44 *
45 * @param[in] pldmVerbose - verbosity flag - true/false
46 * @param[in] msg - message to print
47 * @param[in] data - data to print
48 *
49 * @return - None
50 */
51
52template <class T>
53void Logger(bool pldmverbose, const char* msg, const T& data)
54{
55 if (pldmverbose)
56 {
57 std::stringstream s;
58 s << data;
59 std::cout << msg << s.str() << std::endl;
60 }
61}
Sridevi Rameshca4a8152020-08-11 09:26:19 -050062
63/** @brief Display in JSON format.
64 *
65 * @param[in] data - data to print in json
66 *
67 * @return - None
68 */
69static inline void DisplayInJson(const ordered_json& data)
70{
71 std::cout << data.dump(4) << std::endl;
72}
73
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050074/** @brief MCTP socket read/recieve
75 *
76 * @param[in] requestMsg - Request message to compare against loopback
77 * message recieved from mctp socket
78 * @param[out] responseMsg - Response buffer recieved from mctp socket
Sridevi Rameshc91822b2020-02-27 09:26:41 -060079 * @param[in] pldmVerbose - verbosity flag - true/false
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050080 *
81 * @return - 0 on success.
82 * -1 or -errno on failure.
83 */
84int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
Sridevi Rameshc91822b2020-02-27 09:26:41 -060085 std::vector<uint8_t>& responseMsg, bool pldmVerbose);
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050086
John Wang58a0e062019-11-08 15:38:15 +080087class CommandInterface
88{
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080089
John Wang58a0e062019-11-08 15:38:15 +080090 public:
91 explicit CommandInterface(const char* type, const char* name,
92 CLI::App* app) :
93 pldmType(type),
Deepak Kodihallid6638b72020-07-02 08:36:14 -050094 commandName(name), mctp_eid(PLDM_ENTITY_ID), pldmVerbose(false),
95 instanceId(0)
John Wang58a0e062019-11-08 15:38:15 +080096 {
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080097 app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
Sridevi Rameshc91822b2020-02-27 09:26:41 -060098 app->add_flag("-v, --verbose", pldmVerbose);
John Wang58a0e062019-11-08 15:38:15 +080099 app->callback([&]() { exec(); });
100 }
Sridevi Rameshc91822b2020-02-27 09:26:41 -0600101
John Wang58a0e062019-11-08 15:38:15 +0800102 virtual ~CommandInterface() = default;
103
104 virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0;
105
106 virtual void parseResponseMsg(struct pldm_msg* responsePtr,
107 size_t payloadLength) = 0;
108
John Wangb754eee2020-02-15 16:10:25 +0800109 virtual void exec();
110
111 int pldmSendRecv(std::vector<uint8_t>& requestMsg,
112 std::vector<uint8_t>& responseMsg);
John Wang58a0e062019-11-08 15:38:15 +0800113
114 private:
115 const std::string pldmType;
116 const std::string commandName;
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +0800117 uint8_t mctp_eid;
Sridevi Rameshc91822b2020-02-27 09:26:41 -0600118 bool pldmVerbose;
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600119
120 protected:
121 uint8_t instanceId;
John Wang58a0e062019-11-08 15:38:15 +0800122};
123
124} // namespace helper
125} // namespace pldmtool