blob: 7c199c11c3c277ca8e5549cbf4ebf29f281b4068 [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
5#include <string>
6
7constexpr uint8_t PLDM_ENTITY_ID = 8;
8constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
9constexpr uint8_t PLDM_LOCAL_INSTANCE_ID = 0;
10
11using namespace std;
12
13/*
14 * Main function that handles the GetPLDMTypes response callback via mctp
15 *
16 */
Deepak Kodihalli3c275e12019-09-21 06:39:39 -050017void getPLDMTypes(vector<std::string>&& /*args*/)
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050018{
19 // Create and encode the request message
20 vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
21 sizeof(MCTP_MSG_TYPE_PLDM) +
22 sizeof(PLDM_ENTITY_ID));
23 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
24
25 // Encode the get_types request message
26 uint8_t instanceId = PLDM_LOCAL_INSTANCE_ID;
27 auto returnCode = encode_get_types_req(instanceId, request);
28 if (returnCode)
29 {
30 cerr << "Failed to encode request msg for GetPLDMType : RC = "
31 << returnCode << endl;
32 return;
33 }
34 cout << "Encoded request succesfully : RC = " << returnCode << endl;
35
36 // Insert the PLDM message type and EID at the begining of the request msg.
37 requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM);
38 requestMsg.insert(requestMsg.begin(), PLDM_ENTITY_ID);
39
40 cout << "Request Message:" << endl;
41 printBuffer(requestMsg);
42
43 // Create the response message
44 vector<uint8_t> responseMsg;
45
46 // Compares the response with request packet on first socket recv() call.
47 // If above condition is qualified then, reads the actual response from
48 // the socket to output buffer responseMsg.
49 returnCode = mctpSockSendRecv(requestMsg, responseMsg);
50 if (!returnCode)
51 {
52 cout << "Socket recv() successful : RC = " << returnCode << endl;
53 cout << "Response Message : " << endl;
54 printBuffer(responseMsg);
55 }
56 else
57 {
58 cerr << "Failed to recieve from socket : RC = " << returnCode << endl;
59 return;
60 }
61}
62
63/*
64 * Main function that handles the GetPLDMVersion response callback via mctp
65 *
66 */
67void getPLDMVersion(vector<std::string>&& args)
68{
69 // Create a request packet
70 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
71 PLDM_GET_VERSION_REQ_BYTES);
72 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050073
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050074 uint8_t pldmType = 0x0;
75
76 if (!args[1].c_str())
77 {
78 cout << "Mandatory argument PLDM Command Type not provided!" << endl;
79 cout << "Run pldmtool --help for more information" << endl;
80 return;
81 }
82
83 if (!strcasecmp(args[1].c_str(), "base"))
84 {
85 cout << "PLDM Type requested : " << args[1] << endl;
86 pldmType = PLDM_BASE;
87 }
George Liu15af8582019-11-07 17:42:39 +080088 else if (!strcasecmp(args[1].c_str(), "bios"))
89 {
90 cout << "PLDM Type requested : " << args[1] << endl;
91 pldmType = PLDM_BIOS;
92 }
93 else if (!strcasecmp(args[1].c_str(), "fru"))
94 {
95 cout << "PLDM Type requested : " << args[1] << endl;
96 pldmType = PLDM_FRU;
97 }
98 else if (!strcasecmp(args[1].c_str(), "oem"))
99 {
100 cout << "PLDM Type requested : " << args[1] << endl;
101 pldmType = PLDM_OEM;
102 }
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -0500103 else
104 {
105 cerr << "Unsupported pldm command type OR not supported yet : "
106 << args[1] << endl;
107 return;
108 }
109
110 uint8_t instanceId = PLDM_LOCAL_INSTANCE_ID;
111 uint32_t transferHandle = 0x0;
112 transfer_op_flag opFlag = PLDM_GET_FIRSTPART;
113
114 // encode the get_version request
115 auto returnCode = encode_get_version_req(instanceId, transferHandle, opFlag,
116 pldmType, request);
117 if (returnCode)
118 {
119 cerr << "Failed to encode request msg for GetPLDMVersion. RC = "
120 << returnCode << endl;
121 return;
122 }
123 cout << "Encoded request succesfully : RC = " << returnCode << endl;
124
125 // Insert the PLDM message type and EID at the begining of the request msg.
126 requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM);
127 requestMsg.insert(requestMsg.begin(), PLDM_ENTITY_ID);
128
129 cout << "Request Message:" << endl;
130 printBuffer(requestMsg);
131
132 // Create the response message
133 vector<uint8_t> responseMsg;
134
135 // Compares the response with request packet on first socket recv() call.
136 // If above condition is qualified then, reads the actual response from
137 // the socket to output buffer responseMsg.
138 returnCode = mctpSockSendRecv(requestMsg, responseMsg);
139 if (!returnCode)
140 {
141 cout << "Socket recv() successful : RC = " << returnCode << endl;
142 cout << "Response Message:" << endl;
143 printBuffer(responseMsg);
144 }
145 else
146 {
147 cerr << "Failed to recieve from socket : RC = " << returnCode << endl;
148 return;
149 }
150}
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500151
152/*
153 * Main function that handles the PLDM raw command response callback via mctp
154 *
155 */
156void handleRawOp(vector<std::string>&& args)
157{
158 // Minimu 3 bytes of header data needs to passed. else its a invalid request
159 if (size(args) < 3)
160 {
161 cerr << "Not enough arguments passed."
162 " Minimum, need to pass PLDM header raw data"
163 << endl;
164 return;
165 }
166
167 // Create a request packet and initialize it
168 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
169 requestMsg.clear();
170
171 // Read the raw data passed in the command line
172 // Form request message from the raw data passed
173 for (auto&& rawByte : args)
174 {
175 requestMsg.insert(requestMsg.end(), stoi(rawByte, nullptr, 16));
176 }
177
178 // Validating the payload raw data based on pldm command type
179 uint8_t pldmCmd = requestMsg[2];
180 switch (pldmCmd)
181 {
182 case PLDM_GET_PLDM_VERSION:
183 if (size(requestMsg) !=
184 (sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES))
185 {
186 cerr << "Not enough raw data provided." << endl;
187 cerr << "Total length can be = 3 bytes of header + 6 bytes"
188 " for payload. Please refer spec for details"
189 << endl;
190 return;
191 }
192 break;
193 case PLDM_GET_TID:
194 case PLDM_GET_PLDM_TYPES:
195 if (size(requestMsg) != sizeof(pldm_msg_hdr))
196 {
197 cerr << "Total length can be = 3 bytes of header + 0 bytes"
198 " for payload. Please refer spec for details"
199 << endl;
200 return;
201 }
202 break;
203 case PLDM_GET_PLDM_COMMANDS:
204 if (size(requestMsg) !=
205 sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES)
206 {
207 cerr << "Total length can be = 3 bytes of header + 5 bytes"
208 " for payload. Please refer spec for details"
209 << endl;
210 return;
211 }
212 break;
213 case PLDM_SET_STATE_EFFECTER_STATES:
214 // payload size depends on comp_effecter_count
215 // if count=1 then, request size will be 8 bytes including header
216 // if count=9 then, request size will be 19 bytes including header
217 if ((size(requestMsg) < 8) ||
218 (size(requestMsg) >
219 sizeof(pldm_msg_hdr) +
220 PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES))
221 {
222 cerr << "Total length can be = 3 bytes of header + min/max 8/19"
223 " bytes for payload. Please refer spec for details"
224 << endl;
225 return;
226 }
227 break;
228 default:
229 cerr << "Command Not supported/implemented : " << args[2] << endl;
230 cerr << "Contact backend Team" << endl;
231 return;
232 }
233
234 // Add the MCTP type and PLDM entity id at the end
235 requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM);
236 requestMsg.insert(requestMsg.begin(), PLDM_ENTITY_ID);
237
238 cout << "Request Message" << endl;
239 printBuffer(requestMsg);
240
241 // Create the response message
242 vector<uint8_t> responseMsg;
243
244 // Compares the response with request packet on first socket recv() call.
245 // If above condition is qualified then, reads the actual response from
246 // the socket to output buffer responseMsg.
247 int returnCode = mctpSockSendRecv(requestMsg, responseMsg);
248 if (!returnCode)
249 {
250 cout << "Socket recv() successful : RC = " << returnCode << endl;
251 cout << "Response Message:" << endl;
252 printBuffer(responseMsg);
253 }
254 else
255 {
256 cerr << "Failed to recieve from socket : RC = " << returnCode << endl;
257 return;
258 }
259}