blob: 4ad46d4cf092b7ec3cef98555a792d073c05f601 [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;
30constexpr uint8_t PLDM_LOCAL_INSTANCE_ID = 0;
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050031
32/** @brief Print the buffer
33 *
34 * @param[in] buffer - Buffer to print
35 *
36 * @return - None
37 */
38void printBuffer(const std::vector<uint8_t>& buffer);
39
40/** @brief MCTP socket read/recieve
41 *
42 * @param[in] requestMsg - Request message to compare against loopback
43 * message recieved from mctp socket
44 * @param[out] responseMsg - Response buffer recieved from mctp socket
45 *
46 * @return - 0 on success.
47 * -1 or -errno on failure.
48 */
49int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg,
50 std::vector<uint8_t>& responseMsg);
51
John Wang58a0e062019-11-08 15:38:15 +080052class CommandInterface
53{
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080054
John Wang58a0e062019-11-08 15:38:15 +080055 public:
56 explicit CommandInterface(const char* type, const char* name,
57 CLI::App* app) :
58 pldmType(type),
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080059 commandName(name), mctp_eid(PLDM_ENTITY_ID)
John Wang58a0e062019-11-08 15:38:15 +080060 {
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080061 app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID");
John Wang58a0e062019-11-08 15:38:15 +080062 app->callback([&]() { exec(); });
63 }
64 virtual ~CommandInterface() = default;
65
66 virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0;
67
68 virtual void parseResponseMsg(struct pldm_msg* responsePtr,
69 size_t payloadLength) = 0;
70
John Wangb754eee2020-02-15 16:10:25 +080071 virtual void exec();
72
73 int pldmSendRecv(std::vector<uint8_t>& requestMsg,
74 std::vector<uint8_t>& responseMsg);
John Wang58a0e062019-11-08 15:38:15 +080075
76 private:
77 const std::string pldmType;
78 const std::string commandName;
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080079 uint8_t mctp_eid;
John Wang58a0e062019-11-08 15:38:15 +080080};
81
82} // namespace helper
83} // namespace pldmtool