Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 3 | #include "libpldm/base.h" |
| 4 | #include "libpldm/bios.h" |
| 5 | #include "libpldm/fru.h" |
| 6 | #include "libpldm/platform.h" |
| 7 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 8 | #include "common/utils.hpp" |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 9 | |
| 10 | #include <err.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <sys/un.h> |
| 13 | #include <unistd.h> |
| 14 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 15 | #include <CLI/CLI.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 16 | |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 17 | #include <cstring> |
| 18 | #include <iomanip> |
| 19 | #include <iostream> |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 20 | #include <utility> |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 21 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 22 | namespace pldmtool |
| 23 | { |
| 24 | |
| 25 | namespace helper |
| 26 | { |
| 27 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 28 | using namespace pldm::utils; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 29 | constexpr uint8_t PLDM_ENTITY_ID = 8; |
| 30 | constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 31 | |
| 32 | /** @brief Print the buffer |
| 33 | * |
| 34 | * @param[in] buffer - Buffer to print |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 35 | * @param[in] pldmVerbose -verbosity flag - true/false |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 36 | * |
| 37 | * @return - None |
| 38 | */ |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 39 | void printBuffer(const std::vector<uint8_t>& buffer, bool pldmVerbose); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 40 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 41 | /** @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 | |
| 50 | template <class T> |
| 51 | void 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. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 60 | /** @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 Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 65 | * @param[in] pldmVerbose - verbosity flag - true/false |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 66 | * |
| 67 | * @return - 0 on success. |
| 68 | * -1 or -errno on failure. |
| 69 | */ |
| 70 | int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg, |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 71 | std::vector<uint8_t>& responseMsg, bool pldmVerbose); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 72 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 73 | class CommandInterface |
| 74 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 75 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 76 | public: |
| 77 | explicit CommandInterface(const char* type, const char* name, |
| 78 | CLI::App* app) : |
| 79 | pldmType(type), |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 80 | commandName(name), mctp_eid(PLDM_ENTITY_ID), instanceId(0) |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 81 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 82 | app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID"); |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 83 | app->add_flag("-v, --verbose", pldmVerbose); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 84 | app->callback([&]() { exec(); }); |
| 85 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 86 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 87 | 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 Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 94 | virtual void exec(); |
| 95 | |
| 96 | int pldmSendRecv(std::vector<uint8_t>& requestMsg, |
| 97 | std::vector<uint8_t>& responseMsg); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 98 | |
| 99 | private: |
| 100 | const std::string pldmType; |
| 101 | const std::string commandName; |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 102 | uint8_t mctp_eid; |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 103 | bool pldmVerbose; |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 104 | |
| 105 | protected: |
| 106 | uint8_t instanceId; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace helper |
| 110 | } // namespace pldmtool |