blob: 978da83a915cf2bd61f789ba52e44f4983e8ca27 [file] [log] [blame]
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05001#pragma once
2
George Liu83409572019-12-24 18:42:54 +08003#include "utils.hpp"
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05004
5#include <err.h>
6#include <sys/socket.h>
7#include <sys/un.h>
8#include <unistd.h>
9
John Wang58a0e062019-11-08 15:38:15 +080010#include <CLI/CLI.hpp>
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050011#include <cstring>
12#include <iomanip>
13#include <iostream>
John Wang58a0e062019-11-08 15:38:15 +080014#include <utility>
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050015
16#include "libpldm/base.h"
Sridevi Ramesh98576432019-11-27 10:10:28 -060017#include "libpldm/bios.h"
Sridevi Rameshd4489752019-12-08 09:03:29 -060018#include "libpldm/fru.h"
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050019#include "libpldm/platform.h"
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050020
John Wang58a0e062019-11-08 15:38:15 +080021namespace pldmtool
22{
23
24namespace helper
25{
26
George Liu83409572019-12-24 18:42:54 +080027using namespace pldm::utils;
John Wang58a0e062019-11-08 15:38:15 +080028constexpr uint8_t PLDM_ENTITY_ID = 8;
29constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050030
31/** @brief Print the buffer
32 *
33 * @param[in] buffer - Buffer to print
Sridevi Rameshc91822b2020-02-27 09:26:41 -060034 * @param[in] pldmVerbose -verbosity flag - true/false
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050035 *
36 * @return - None
37 */
Sridevi Rameshc91822b2020-02-27 09:26:41 -060038void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose);
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050039
Sridevi Rameshc91822b2020-02-27 09:26:41 -060040/** @brief print the input message if pldmverbose is enabled
41 *
42 * @param[in] pldmVerbose - verbosity flag - true/false
43 * @param[in] msg - message to print
44 * @param[in] data - data to print
45 *
46 * @return - None
47 */
48
49template <class T>
50void Logger(bool pldmverbose, const char* msg, const T& data)
51{
52 if (pldmverbose)
53 {
54 std::stringstream s;
55 s << data;
56 std::cout << msg << s.str() << std::endl;
57 }
58}
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050059/** @brief MCTP socket read/recieve
60 *
61 * @param[in] requestMsg - Request message to compare against loopback
62 * message recieved from mctp socket
63 * @param[out] responseMsg - Response buffer recieved from mctp socket
Sridevi Rameshc91822b2020-02-27 09:26:41 -060064 * @param[in] pldmVerbose - verbosity flag - true/false
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050065 *
66 * @return - 0 on success.
67 * -1 or -errno on failure.
68 */
69int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
Sridevi Rameshc91822b2020-02-27 09:26:41 -060070 std::vector<uint8_t>& responseMsg, bool pldmVerbose);
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050071
John Wang58a0e062019-11-08 15:38:15 +080072class CommandInterface
73{
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080074
John Wang58a0e062019-11-08 15:38:15 +080075 public:
76 explicit CommandInterface(const char* type, const char* name,
77 CLI::App* app) :
78 pldmType(type),
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -060079 commandName(name), mctp_eid(PLDM_ENTITY_ID), instanceId(0)
John Wang58a0e062019-11-08 15:38:15 +080080 {
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080081 app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
Sridevi Rameshc91822b2020-02-27 09:26:41 -060082 app->add_flag("-v, --verbose", pldmVerbose);
John Wang58a0e062019-11-08 15:38:15 +080083 app->callback([&]() { exec(); });
84 }
Sridevi Rameshc91822b2020-02-27 09:26:41 -060085
John Wang58a0e062019-11-08 15:38:15 +080086 virtual ~CommandInterface() = default;
87
88 virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0;
89
90 virtual void parseResponseMsg(struct pldm_msg* responsePtr,
91 size_t payloadLength) = 0;
92
John Wangb754eee2020-02-15 16:10:25 +080093 virtual void exec();
94
95 int pldmSendRecv(std::vector<uint8_t>& requestMsg,
96 std::vector<uint8_t>& responseMsg);
John Wang58a0e062019-11-08 15:38:15 +080097
98 private:
99 const std::string pldmType;
100 const std::string commandName;
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +0800101 uint8_t mctp_eid;
Sridevi Rameshc91822b2020-02-27 09:26:41 -0600102 bool pldmVerbose;
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600103
104 protected:
105 uint8_t instanceId;
John Wang58a0e062019-11-08 15:38:15 +0800106};
107
108} // namespace helper
109} // namespace pldmtool