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> |
Sridevi Ramesh | ca4a815 | 2020-08-11 09:26:19 -0500 | [diff] [blame] | 16 | #include <nlohmann/json.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 17 | |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 18 | #include <cstring> |
| 19 | #include <iomanip> |
| 20 | #include <iostream> |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 21 | #include <utility> |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 22 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 23 | namespace pldmtool |
| 24 | { |
| 25 | |
| 26 | namespace helper |
| 27 | { |
| 28 | |
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; |
Sridevi Ramesh | ca4a815 | 2020-08-11 09:26:19 -0500 | [diff] [blame] | 31 | using ordered_json = nlohmann::ordered_json; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 32 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 33 | /** @brief print the input message if pldmverbose is enabled |
| 34 | * |
| 35 | * @param[in] pldmVerbose - verbosity flag - true/false |
| 36 | * @param[in] msg - message to print |
| 37 | * @param[in] data - data to print |
| 38 | * |
| 39 | * @return - None |
| 40 | */ |
| 41 | |
| 42 | template <class T> |
| 43 | void Logger(bool pldmverbose, const char* msg, const T& data) |
| 44 | { |
| 45 | if (pldmverbose) |
| 46 | { |
| 47 | std::stringstream s; |
| 48 | s << data; |
| 49 | std::cout << msg << s.str() << std::endl; |
| 50 | } |
| 51 | } |
Sridevi Ramesh | ca4a815 | 2020-08-11 09:26:19 -0500 | [diff] [blame] | 52 | |
| 53 | /** @brief Display in JSON format. |
| 54 | * |
| 55 | * @param[in] data - data to print in json |
| 56 | * |
| 57 | * @return - None |
| 58 | */ |
| 59 | static inline void DisplayInJson(const ordered_json& data) |
| 60 | { |
| 61 | std::cout << data.dump(4) << std::endl; |
| 62 | } |
| 63 | |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 64 | /** @brief MCTP socket read/recieve |
| 65 | * |
| 66 | * @param[in] requestMsg - Request message to compare against loopback |
| 67 | * message recieved from mctp socket |
| 68 | * @param[out] responseMsg - Response buffer recieved from mctp socket |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 69 | * @param[in] pldmVerbose - verbosity flag - true/false |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 70 | * |
| 71 | * @return - 0 on success. |
| 72 | * -1 or -errno on failure. |
| 73 | */ |
| 74 | int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg, |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 75 | std::vector<uint8_t>& responseMsg, bool pldmVerbose); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 76 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 77 | class CommandInterface |
| 78 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 79 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 80 | public: |
| 81 | explicit CommandInterface(const char* type, const char* name, |
| 82 | CLI::App* app) : |
| 83 | pldmType(type), |
Deepak Kodihalli | d6638b7 | 2020-07-02 08:36:14 -0500 | [diff] [blame] | 84 | commandName(name), mctp_eid(PLDM_ENTITY_ID), pldmVerbose(false), |
| 85 | instanceId(0) |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 86 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 87 | app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID"); |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 88 | app->add_flag("-v, --verbose", pldmVerbose); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 89 | app->callback([&]() { exec(); }); |
| 90 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 91 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 92 | virtual ~CommandInterface() = default; |
| 93 | |
| 94 | virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0; |
| 95 | |
| 96 | virtual void parseResponseMsg(struct pldm_msg* responsePtr, |
| 97 | size_t payloadLength) = 0; |
| 98 | |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 99 | virtual void exec(); |
| 100 | |
| 101 | int pldmSendRecv(std::vector<uint8_t>& requestMsg, |
| 102 | std::vector<uint8_t>& responseMsg); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 103 | |
| 104 | private: |
| 105 | const std::string pldmType; |
| 106 | const std::string commandName; |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 107 | uint8_t mctp_eid; |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 108 | bool pldmVerbose; |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 109 | |
| 110 | protected: |
| 111 | uint8_t instanceId; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // namespace helper |
| 115 | } // namespace pldmtool |