blob: bea01bd2928978ba4f51ee9ef1eeb0cf56c52c3d [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
34 *
35 * @return - None
36 */
37void printBuffer(const std::vector<uint8_t>& buffer);
38
39/** @brief MCTP socket read/recieve
40 *
41 * @param[in] requestMsg - Request message to compare against loopback
42 * message recieved from mctp socket
43 * @param[out] responseMsg - Response buffer recieved from mctp socket
44 *
45 * @return - 0 on success.
46 * -1 or -errno on failure.
47 */
48int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
49 std::vector<uint8_t>& responseMsg);
50
John Wang58a0e062019-11-08 15:38:15 +080051class CommandInterface
52{
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080053
John Wang58a0e062019-11-08 15:38:15 +080054 public:
55 explicit CommandInterface(const char* type, const char* name,
56 CLI::App* app) :
57 pldmType(type),
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -060058 commandName(name), mctp_eid(PLDM_ENTITY_ID), instanceId(0)
John Wang58a0e062019-11-08 15:38:15 +080059 {
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080060 app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
John Wang58a0e062019-11-08 15:38:15 +080061 app->callback([&]() { exec(); });
62 }
63 virtual ~CommandInterface() = default;
64
65 virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0;
66
67 virtual void parseResponseMsg(struct pldm_msg* responsePtr,
68 size_t payloadLength) = 0;
69
John Wangb754eee2020-02-15 16:10:25 +080070 virtual void exec();
71
72 int pldmSendRecv(std::vector<uint8_t>& requestMsg,
73 std::vector<uint8_t>& responseMsg);
John Wang58a0e062019-11-08 15:38:15 +080074
75 private:
76 const std::string pldmType;
77 const std::string commandName;
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080078 uint8_t mctp_eid;
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -060079
80 protected:
81 uint8_t instanceId;
John Wang58a0e062019-11-08 15:38:15 +080082};
83
84} // namespace helper
85} // namespace pldmtool