blob: 601635c91b665d23c9d7972b611054f233b56e57 [file] [log] [blame]
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05001#include "pldm_base_cmd.hpp"
2
3#include "pldm_cmd_helper.hpp"
4
John Wang58a0e062019-11-08 15:38:15 +08005#include <map>
6#include <memory>
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05007#include <string>
John Wang58a0e062019-11-08 15:38:15 +08008#include <vector>
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05009
John Wang58a0e062019-11-08 15:38:15 +080010#include "libpldm/utils.h"
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050011
John Wang58a0e062019-11-08 15:38:15 +080012namespace pldmtool
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050013{
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050014
John Wang58a0e062019-11-08 15:38:15 +080015namespace base
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050016{
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050017
John Wang58a0e062019-11-08 15:38:15 +080018namespace
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050019{
John Wang58a0e062019-11-08 15:38:15 +080020
21using namespace pldmtool::helper;
22
23std::vector<std::unique_ptr<CommandInterface>> commands;
24const 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
31class 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. Kammath58229b22019-08-09 06:30:04 -050044 {
John Wang58a0e062019-11-08 15:38:15 +080045 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. Kammath58229b22019-08-09 06:30:04 -050049 }
50
John Wang58a0e062019-11-08 15:38:15 +080051 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050052 {
John Wang58a0e062019-11-08 15:38:15 +080053 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: "
60 << "rc=" << rc << ",cc=" << (int)cc << std::endl;
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050061 return;
John Wang58a0e062019-11-08 15:38:15 +080062 }
63
64 printPldmTypes(types);
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050065 }
66
John Wang58a0e062019-11-08 15:38:15 +080067 private:
68 void printPldmTypes(std::vector<bitfield8_t>& types)
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050069 {
John Wang58a0e062019-11-08 15:38:15 +080070 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. Kammath58229b22019-08-09 06:30:04 -050089 }
John Wang58a0e062019-11-08 15:38:15 +080090};
91
92class 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. Kammath58229b22019-08-09 06:30:04 -0500104 {
John Wang58a0e062019-11-08 15:38:15 +0800105 app->add_option("-t,--type", pldmType, "pldm supported type")
106 ->required()
107 ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case));
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500108 }
John Wang58a0e062019-11-08 15:38:15 +0800109 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: "
131 << "rc=" << rc << ",cc=" << (int)cc << std::endl;
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
152void 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. Kammath58229b22019-08-09 06:30:04 -0500165}
John Wang58a0e062019-11-08 15:38:15 +0800166
167} // namespace base
168} // namespace pldmtool