Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 3 | #include "common/utils.hpp" |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 4 | |
| 5 | #include <err.h> |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 6 | #include <libpldm/base.h> |
| 7 | #include <libpldm/bios.h> |
| 8 | #include <libpldm/fru.h> |
| 9 | #include <libpldm/platform.h> |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <sys/un.h> |
| 12 | #include <unistd.h> |
| 13 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 14 | #include <CLI/CLI.hpp> |
Sridevi Ramesh | ca4a815 | 2020-08-11 09:26:19 -0500 | [diff] [blame] | 15 | #include <nlohmann/json.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 | |
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; |
Sridevi Ramesh | ca4a815 | 2020-08-11 09:26:19 -0500 | [diff] [blame] | 30 | using ordered_json = nlohmann::ordered_json; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 31 | |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 32 | /** @brief print the input message if pldmverbose is enabled |
| 33 | * |
| 34 | * @param[in] pldmVerbose - verbosity flag - true/false |
| 35 | * @param[in] msg - message to print |
| 36 | * @param[in] data - data to print |
| 37 | * |
| 38 | * @return - None |
| 39 | */ |
| 40 | |
| 41 | template <class T> |
| 42 | void Logger(bool pldmverbose, const char* msg, const T& data) |
| 43 | { |
| 44 | if (pldmverbose) |
| 45 | { |
| 46 | std::stringstream s; |
| 47 | s << data; |
| 48 | std::cout << msg << s.str() << std::endl; |
| 49 | } |
| 50 | } |
Sridevi Ramesh | ca4a815 | 2020-08-11 09:26:19 -0500 | [diff] [blame] | 51 | |
| 52 | /** @brief Display in JSON format. |
| 53 | * |
| 54 | * @param[in] data - data to print in json |
| 55 | * |
| 56 | * @return - None |
| 57 | */ |
| 58 | static inline void DisplayInJson(const ordered_json& data) |
| 59 | { |
| 60 | std::cout << data.dump(4) << std::endl; |
| 61 | } |
| 62 | |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 63 | /** @brief MCTP socket read/recieve |
| 64 | * |
| 65 | * @param[in] requestMsg - Request message to compare against loopback |
| 66 | * message recieved from mctp socket |
| 67 | * @param[out] responseMsg - Response buffer recieved from mctp socket |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 68 | * @param[in] pldmVerbose - verbosity flag - true/false |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 69 | * |
| 70 | * @return - 0 on success. |
| 71 | * -1 or -errno on failure. |
| 72 | */ |
| 73 | int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg, |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 74 | std::vector<uint8_t>& responseMsg, bool pldmVerbose); |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 75 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 76 | class CommandInterface |
| 77 | { |
| 78 | public: |
| 79 | explicit CommandInterface(const char* type, const char* name, |
| 80 | CLI::App* app) : |
| 81 | pldmType(type), |
Deepak Kodihalli | d6638b7 | 2020-07-02 08:36:14 -0500 | [diff] [blame] | 82 | commandName(name), mctp_eid(PLDM_ENTITY_ID), pldmVerbose(false), |
| 83 | instanceId(0) |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 84 | { |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 85 | app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID"); |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 86 | app->add_flag("-v, --verbose", pldmVerbose); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 87 | app->callback([&]() { exec(); }); |
| 88 | } |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 89 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 90 | virtual ~CommandInterface() = default; |
| 91 | |
| 92 | virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0; |
| 93 | |
| 94 | virtual void parseResponseMsg(struct pldm_msg* responsePtr, |
| 95 | size_t payloadLength) = 0; |
| 96 | |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 97 | virtual void exec(); |
| 98 | |
| 99 | int pldmSendRecv(std::vector<uint8_t>& requestMsg, |
| 100 | std::vector<uint8_t>& responseMsg); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 101 | |
Chinmay Shripad Hegde | 526461a | 2023-01-11 13:04:02 +0530 | [diff] [blame] | 102 | /** |
| 103 | * @brief get MCTP endpoint ID |
| 104 | * |
| 105 | * @return uint8_t - MCTP endpoint ID |
| 106 | */ |
| 107 | inline uint8_t getMCTPEID() |
| 108 | { |
| 109 | return mctp_eid; |
| 110 | } |
| 111 | |
Pavithra Barithaya | a340eed | 2023-02-14 03:32:47 -0600 | [diff] [blame] | 112 | /** |
| 113 | * @brief get PLDM type |
| 114 | * |
| 115 | * @return pldm type |
| 116 | */ |
| 117 | inline std::string getPLDMType() |
| 118 | { |
| 119 | return pldmType; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @brief get command name |
| 124 | * |
| 125 | * @return the command name |
| 126 | */ |
| 127 | inline std::string getCommandName() |
| 128 | { |
| 129 | return commandName; |
| 130 | } |
| 131 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 132 | private: |
| 133 | const std::string pldmType; |
| 134 | const std::string commandName; |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 135 | uint8_t mctp_eid; |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 136 | bool pldmVerbose; |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 137 | |
| 138 | protected: |
| 139 | uint8_t instanceId; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | } // namespace helper |
| 143 | } // namespace pldmtool |