blob: 814de45543798185dbc68a99c82e108342c06ba2 [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
George Liu83409572019-12-24 18:42:54 +08008#include "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>
George Liu6492f522020-06-16 10:34:05 +080016
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050017#include <cstring>
18#include <iomanip>
19#include <iostream>
John Wang58a0e062019-11-08 15:38:15 +080020#include <utility>
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050021
John Wang58a0e062019-11-08 15:38:15 +080022namespace pldmtool
23{
24
25namespace helper
26{
27
George Liu83409572019-12-24 18:42:54 +080028using namespace pldm::utils;
John Wang58a0e062019-11-08 15:38:15 +080029constexpr uint8_t PLDM_ENTITY_ID = 8;
30constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050031
32/** @brief Print the buffer
33 *
34 * @param[in] buffer - Buffer to print
Sridevi Rameshc91822b2020-02-27 09:26:41 -060035 * @param[in] pldmVerbose -verbosity flag - true/false
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050036 *
37 * @return - None
38 */
Sridevi Rameshc91822b2020-02-27 09:26:41 -060039void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose);
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050040
Sridevi Rameshc91822b2020-02-27 09:26:41 -060041/** @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
50template <class T>
51void 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}
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050060/** @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
Sridevi Rameshc91822b2020-02-27 09:26:41 -060065 * @param[in] pldmVerbose - verbosity flag - true/false
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050066 *
67 * @return - 0 on success.
68 * -1 or -errno on failure.
69 */
70int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
Sridevi Rameshc91822b2020-02-27 09:26:41 -060071 std::vector<uint8_t>& responseMsg, bool pldmVerbose);
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050072
John Wang58a0e062019-11-08 15:38:15 +080073class CommandInterface
74{
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080075
John Wang58a0e062019-11-08 15:38:15 +080076 public:
77 explicit CommandInterface(const char* type, const char* name,
78 CLI::App* app) :
79 pldmType(type),
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -060080 commandName(name), mctp_eid(PLDM_ENTITY_ID), instanceId(0)
John Wang58a0e062019-11-08 15:38:15 +080081 {
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080082 app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
Sridevi Rameshc91822b2020-02-27 09:26:41 -060083 app->add_flag("-v, --verbose", pldmVerbose);
John Wang58a0e062019-11-08 15:38:15 +080084 app->callback([&]() { exec(); });
85 }
Sridevi Rameshc91822b2020-02-27 09:26:41 -060086
John Wang58a0e062019-11-08 15:38:15 +080087 virtual ~CommandInterface() = default;
88
89 virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0;
90
91 virtual void parseResponseMsg(struct pldm_msg* responsePtr,
92 size_t payloadLength) = 0;
93
John Wangb754eee2020-02-15 16:10:25 +080094 virtual void exec();
95
96 int pldmSendRecv(std::vector<uint8_t>& requestMsg,
97 std::vector<uint8_t>& responseMsg);
John Wang58a0e062019-11-08 15:38:15 +080098
99 private:
100 const std::string pldmType;
101 const std::string commandName;
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +0800102 uint8_t mctp_eid;
Sridevi Rameshc91822b2020-02-27 09:26:41 -0600103 bool pldmVerbose;
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600104
105 protected:
106 uint8_t instanceId;
John Wang58a0e062019-11-08 15:38:15 +0800107};
108
109} // namespace helper
110} // namespace pldmtool