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