blob: d60e3020c3511dac86e88e7db9dabf696eb2ab63 [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: "
Sampa Misraaa8ae722019-12-12 03:20:40 -060060 << "rc=" << rc << ",cc=" << (int)cc << "\n";
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: "
Sampa Misraaa8ae722019-12-12 03:20:40 -0600131 << "rc=" << rc << ",cc=" << (int)cc << "\n";
John Wang58a0e062019-11-08 15:38:15 +0800132 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
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600152class GetTID : public CommandInterface
153{
154 public:
155 ~GetTID() = default;
156 GetTID() = delete;
157 GetTID(const GetTID&) = delete;
158 GetTID(GetTID&&) = default;
159 GetTID& operator=(const GetTID&) = delete;
160 GetTID& operator=(GetTID&&) = default;
161
162 using CommandInterface::CommandInterface;
163
164 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
165 {
166 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
167 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
168 auto rc = encode_get_tid_req(PLDM_LOCAL_INSTANCE_ID, request);
169 return {rc, requestMsg};
170 }
171
172 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
173 {
174 uint8_t cc = 0;
175 uint8_t tid = 0;
176 std::vector<bitfield8_t> types(8);
177 auto rc = decode_get_tid_resp(responsePtr, payloadLength, &cc, &tid);
178 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
179 {
180 std::cerr << "Response Message Error: "
181 << "rc=" << rc << ",cc=" << (int)cc << "\n";
182 return;
183 }
184 std::cout << "Parsed Response Msg: " << std::endl;
185 std::cout << "TID : " << static_cast<uint32_t>(tid) << std::endl;
186 }
187};
188
John Wang58a0e062019-11-08 15:38:15 +0800189void registerCommand(CLI::App& app)
190{
191 auto base = app.add_subcommand("base", "base type command");
192 base->require_subcommand(1);
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600193
John Wang58a0e062019-11-08 15:38:15 +0800194 auto getPLDMTypes =
195 base->add_subcommand("GetPLDMTypes", "get pldm supported types");
196 commands.push_back(
197 std::make_unique<GetPLDMTypes>("base", "GetPLDMTypes", getPLDMTypes));
198
199 auto getPLDMVersion =
200 base->add_subcommand("GetPLDMVersion", "get version of a certain type");
201 commands.push_back(std::make_unique<GetPLDMVersion>(
202 "base", "GetPLDMVersion", getPLDMVersion));
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600203
204 auto getPLDMTID = base->add_subcommand("GetTID", "get Terminus ID (TID)");
205 commands.push_back(std::make_unique<GetTID>("base", "GetTID", getPLDMTID));
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500206}
John Wang58a0e062019-11-08 15:38:15 +0800207
208} // namespace base
209} // namespace pldmtool