1 #pragma once 2 3 #include "libpldm/base.h" 4 #include "libpldm/bios.h" 5 #include "libpldm/fru.h" 6 #include "libpldm/platform.h" 7 8 #include "common/utils.hpp" 9 10 #include <err.h> 11 #include <sys/socket.h> 12 #include <sys/un.h> 13 #include <unistd.h> 14 15 #include <CLI/CLI.hpp> 16 #include <nlohmann/json.hpp> 17 18 #include <cstring> 19 #include <iomanip> 20 #include <iostream> 21 #include <utility> 22 23 namespace pldmtool 24 { 25 26 namespace helper 27 { 28 29 using namespace pldm::utils; 30 constexpr uint8_t PLDM_ENTITY_ID = 8; 31 constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; 32 using ordered_json = nlohmann::ordered_json; 33 34 /** @brief print the input message if pldmverbose is enabled 35 * 36 * @param[in] pldmVerbose - verbosity flag - true/false 37 * @param[in] msg - message to print 38 * @param[in] data - data to print 39 * 40 * @return - None 41 */ 42 43 template <class T> 44 void Logger(bool pldmverbose, const char* msg, const T& data) 45 { 46 if (pldmverbose) 47 { 48 std::stringstream s; 49 s << data; 50 std::cout << msg << s.str() << std::endl; 51 } 52 } 53 54 /** @brief Display in JSON format. 55 * 56 * @param[in] data - data to print in json 57 * 58 * @return - None 59 */ 60 static inline void DisplayInJson(const ordered_json& data) 61 { 62 std::cout << data.dump(4) << std::endl; 63 } 64 65 /** @brief MCTP socket read/recieve 66 * 67 * @param[in] requestMsg - Request message to compare against loopback 68 * message recieved from mctp socket 69 * @param[out] responseMsg - Response buffer recieved from mctp socket 70 * @param[in] pldmVerbose - verbosity flag - true/false 71 * 72 * @return - 0 on success. 73 * -1 or -errno on failure. 74 */ 75 int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg, 76 std::vector<uint8_t>& responseMsg, bool pldmVerbose); 77 78 class CommandInterface 79 { 80 81 public: 82 explicit CommandInterface(const char* type, const char* name, 83 CLI::App* app) : 84 pldmType(type), 85 commandName(name), mctp_eid(PLDM_ENTITY_ID), pldmVerbose(false), 86 instanceId(0) 87 { 88 app->add_option("-m,--mctp_eid", mctp_eid, "MCTP endpoint ID"); 89 app->add_flag("-v, --verbose", pldmVerbose); 90 app->callback([&]() { exec(); }); 91 } 92 93 virtual ~CommandInterface() = default; 94 95 virtual std::pair<int, std::vector<uint8_t>> createRequestMsg() = 0; 96 97 virtual void parseResponseMsg(struct pldm_msg* responsePtr, 98 size_t payloadLength) = 0; 99 100 virtual void exec(); 101 102 int pldmSendRecv(std::vector<uint8_t>& requestMsg, 103 std::vector<uint8_t>& responseMsg); 104 105 private: 106 const std::string pldmType; 107 const std::string commandName; 108 uint8_t mctp_eid; 109 bool pldmVerbose; 110 111 protected: 112 uint8_t instanceId; 113 }; 114 115 } // namespace helper 116 } // namespace pldmtool 117