Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #include "pldm_base_cmd.hpp" |
| 2 | |
| 3 | #include "pldm_cmd_helper.hpp" |
| 4 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 5 | #include <map> |
| 6 | #include <memory> |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 7 | #include <string> |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 8 | #include <vector> |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 9 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 10 | #include "libpldm/utils.h" |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 11 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 12 | namespace pldmtool |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 13 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 14 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 15 | namespace base |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 16 | { |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 17 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 18 | namespace |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 19 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 20 | |
| 21 | using namespace pldmtool::helper; |
| 22 | |
| 23 | std::vector<std::unique_ptr<CommandInterface>> commands; |
| 24 | const std::map<const char*, pldm_supported_types> pldmTypes{ |
| 25 | {"base", PLDM_BASE}, {"platform", PLDM_PLATFORM}, {"bios", PLDM_BIOS}, |
| 26 | {"fru", PLDM_FRU}, {"oem", PLDM_OEM}, |
| 27 | }; |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | class GetPLDMTypes : public CommandInterface |
| 32 | { |
| 33 | public: |
| 34 | ~GetPLDMTypes() = default; |
| 35 | GetPLDMTypes() = delete; |
| 36 | GetPLDMTypes(const GetPLDMTypes&) = delete; |
| 37 | GetPLDMTypes(GetPLDMTypes&&) = default; |
| 38 | GetPLDMTypes& operator=(const GetPLDMTypes&) = delete; |
| 39 | GetPLDMTypes& operator=(GetPLDMTypes&&) = default; |
| 40 | |
| 41 | using CommandInterface::CommandInterface; |
| 42 | |
| 43 | std::pair<int, std::vector<uint8_t>> createRequestMsg() override |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 44 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 45 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr)); |
| 46 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 47 | auto rc = encode_get_types_req(PLDM_LOCAL_INSTANCE_ID, request); |
| 48 | return {rc, requestMsg}; |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 49 | } |
| 50 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 51 | void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 52 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 53 | uint8_t cc = 0; |
| 54 | std::vector<bitfield8_t> types(8); |
| 55 | auto rc = decode_get_types_resp(responsePtr, payloadLength, &cc, |
| 56 | types.data()); |
| 57 | if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS) |
| 58 | { |
| 59 | std::cerr << "Response Message Error: " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 60 | << "rc=" << rc << ",cc=" << (int)cc << "\n"; |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 61 | return; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | printPldmTypes(types); |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 65 | } |
| 66 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 67 | private: |
| 68 | void printPldmTypes(std::vector<bitfield8_t>& types) |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 69 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 70 | std::cout << "Supported types:"; |
| 71 | for (int i = 0; i < PLDM_MAX_TYPES; i++) |
| 72 | { |
| 73 | bitfield8_t b = types[i / 8]; |
| 74 | if (b.byte & (1 << i % 8)) |
| 75 | { |
| 76 | std::cout << " " << i; |
| 77 | auto it = std::find_if( |
| 78 | pldmTypes.begin(), pldmTypes.end(), |
| 79 | [i](const auto& typePair) { return typePair.second == i; }); |
| 80 | if (it != pldmTypes.end()) |
| 81 | { |
| 82 | |
| 83 | std::cout << "(" << it->first << ")"; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | std::cout << std::endl; |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 89 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class GetPLDMVersion : public CommandInterface |
| 93 | { |
| 94 | public: |
| 95 | ~GetPLDMVersion() = default; |
| 96 | GetPLDMVersion() = delete; |
| 97 | GetPLDMVersion(const GetPLDMVersion&) = delete; |
| 98 | GetPLDMVersion(GetPLDMVersion&&) = default; |
| 99 | GetPLDMVersion& operator=(const GetPLDMVersion&) = delete; |
| 100 | GetPLDMVersion& operator=(GetPLDMVersion&&) = default; |
| 101 | |
| 102 | explicit GetPLDMVersion(const char* type, const char* name, CLI::App* app) : |
| 103 | CommandInterface(type, name, app) |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 104 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 105 | app->add_option("-t,--type", pldmType, "pldm supported type") |
| 106 | ->required() |
| 107 | ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case)); |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 108 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 109 | std::pair<int, std::vector<uint8_t>> createRequestMsg() override |
| 110 | { |
| 111 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 112 | PLDM_GET_VERSION_REQ_BYTES); |
| 113 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 114 | |
| 115 | auto rc = encode_get_version_req(PLDM_LOCAL_INSTANCE_ID, 0, |
| 116 | PLDM_GET_FIRSTPART, pldmType, request); |
| 117 | return {rc, requestMsg}; |
| 118 | } |
| 119 | |
| 120 | void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override |
| 121 | { |
| 122 | uint8_t cc = 0, transferFlag = 0; |
| 123 | uint32_t transferHandle = 0; |
| 124 | ver32_t version; |
| 125 | auto rc = |
| 126 | decode_get_version_resp(responsePtr, payloadLength, &cc, |
| 127 | &transferHandle, &transferFlag, &version); |
| 128 | if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS) |
| 129 | { |
| 130 | std::cerr << "Response Message Error: " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 131 | << "rc=" << rc << ",cc=" << (int)cc << "\n"; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | char buffer[16] = {0}; |
| 135 | ver2str(&version, buffer, sizeof(buffer)); |
| 136 | std::cout << "Type " << pldmType; |
| 137 | auto it = std::find_if( |
| 138 | pldmTypes.begin(), pldmTypes.end(), |
| 139 | [&](const auto& typePair) { return typePair.second == pldmType; }); |
| 140 | |
| 141 | if (it != pldmTypes.end()) |
| 142 | { |
| 143 | std::cout << "(" << it->first << ")"; |
| 144 | } |
| 145 | std::cout << ": " << buffer << std::endl; |
| 146 | } |
| 147 | |
| 148 | private: |
| 149 | pldm_supported_types pldmType; |
| 150 | }; |
| 151 | |
| 152 | void registerCommand(CLI::App& app) |
| 153 | { |
| 154 | auto base = app.add_subcommand("base", "base type command"); |
| 155 | base->require_subcommand(1); |
| 156 | auto getPLDMTypes = |
| 157 | base->add_subcommand("GetPLDMTypes", "get pldm supported types"); |
| 158 | commands.push_back( |
| 159 | std::make_unique<GetPLDMTypes>("base", "GetPLDMTypes", getPLDMTypes)); |
| 160 | |
| 161 | auto getPLDMVersion = |
| 162 | base->add_subcommand("GetPLDMVersion", "get version of a certain type"); |
| 163 | commands.push_back(std::make_unique<GetPLDMVersion>( |
| 164 | "base", "GetPLDMVersion", getPLDMVersion)); |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 165 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 166 | |
| 167 | } // namespace base |
| 168 | } // namespace pldmtool |