blob: b2034236f7f2e01190685dd8ac873c086d69d2e7 [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 }
88 else
89 {
90 cerr << "Unsupported pldm command type OR not supported yet : "
91 << args[1] << endl;
92 return;
93 }
94
95 uint8_t instanceId = PLDM_LOCAL_INSTANCE_ID;
96 uint32_t transferHandle = 0x0;
97 transfer_op_flag opFlag = PLDM_GET_FIRSTPART;
98
99 // encode the get_version request
100 auto returnCode = encode_get_version_req(instanceId, transferHandle, opFlag,
101 pldmType, request);
102 if (returnCode)
103 {
104 cerr << "Failed to encode request msg for GetPLDMVersion. RC = "
105 << returnCode << endl;
106 return;
107 }
108 cout << "Encoded request succesfully : RC = " << returnCode << endl;
109
110 // Insert the PLDM message type and EID at the begining of the request msg.
111 requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM);
112 requestMsg.insert(requestMsg.begin(), PLDM_ENTITY_ID);
113
114 cout << "Request Message:" << endl;
115 printBuffer(requestMsg);
116
117 // Create the response message
118 vector<uint8_t> responseMsg;
119
120 // Compares the response with request packet on first socket recv() call.
121 // If above condition is qualified then, reads the actual response from
122 // the socket to output buffer responseMsg.
123 returnCode = mctpSockSendRecv(requestMsg, responseMsg);
124 if (!returnCode)
125 {
126 cout << "Socket recv() successful : RC = " << returnCode << endl;
127 cout << "Response Message:" << endl;
128 printBuffer(responseMsg);
129 }
130 else
131 {
132 cerr << "Failed to recieve from socket : RC = " << returnCode << endl;
133 return;
134 }
135}
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500136
137/*
138 * Main function that handles the PLDM raw command response callback via mctp
139 *
140 */
141void handleRawOp(vector<std::string>&& args)
142{
143 // Minimu 3 bytes of header data needs to passed. else its a invalid request
144 if (size(args) < 3)
145 {
146 cerr << "Not enough arguments passed."
147 " Minimum, need to pass PLDM header raw data"
148 << endl;
149 return;
150 }
151
152 // Create a request packet and initialize it
153 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
154 requestMsg.clear();
155
156 // Read the raw data passed in the command line
157 // Form request message from the raw data passed
158 for (auto&& rawByte : args)
159 {
160 requestMsg.insert(requestMsg.end(), stoi(rawByte, nullptr, 16));
161 }
162
163 // Validating the payload raw data based on pldm command type
164 uint8_t pldmCmd = requestMsg[2];
165 switch (pldmCmd)
166 {
167 case PLDM_GET_PLDM_VERSION:
168 if (size(requestMsg) !=
169 (sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES))
170 {
171 cerr << "Not enough raw data provided." << endl;
172 cerr << "Total length can be = 3 bytes of header + 6 bytes"
173 " for payload. Please refer spec for details"
174 << endl;
175 return;
176 }
177 break;
178 case PLDM_GET_TID:
179 case PLDM_GET_PLDM_TYPES:
180 if (size(requestMsg) != sizeof(pldm_msg_hdr))
181 {
182 cerr << "Total length can be = 3 bytes of header + 0 bytes"
183 " for payload. Please refer spec for details"
184 << endl;
185 return;
186 }
187 break;
188 case PLDM_GET_PLDM_COMMANDS:
189 if (size(requestMsg) !=
190 sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES)
191 {
192 cerr << "Total length can be = 3 bytes of header + 5 bytes"
193 " for payload. Please refer spec for details"
194 << endl;
195 return;
196 }
197 break;
198 case PLDM_SET_STATE_EFFECTER_STATES:
199 // payload size depends on comp_effecter_count
200 // if count=1 then, request size will be 8 bytes including header
201 // if count=9 then, request size will be 19 bytes including header
202 if ((size(requestMsg) < 8) ||
203 (size(requestMsg) >
204 sizeof(pldm_msg_hdr) +
205 PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES))
206 {
207 cerr << "Total length can be = 3 bytes of header + min/max 8/19"
208 " bytes for payload. Please refer spec for details"
209 << endl;
210 return;
211 }
212 break;
213 default:
214 cerr << "Command Not supported/implemented : " << args[2] << endl;
215 cerr << "Contact backend Team" << endl;
216 return;
217 }
218
219 // Add the MCTP type and PLDM entity id at the end
220 requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM);
221 requestMsg.insert(requestMsg.begin(), PLDM_ENTITY_ID);
222
223 cout << "Request Message" << endl;
224 printBuffer(requestMsg);
225
226 // Create the response message
227 vector<uint8_t> responseMsg;
228
229 // Compares the response with request packet on first socket recv() call.
230 // If above condition is qualified then, reads the actual response from
231 // the socket to output buffer responseMsg.
232 int returnCode = mctpSockSendRecv(requestMsg, responseMsg);
233 if (!returnCode)
234 {
235 cout << "Socket recv() successful : RC = " << returnCode << endl;
236 cout << "Response Message:" << endl;
237 printBuffer(responseMsg);
238 }
239 else
240 {
241 cerr << "Failed to recieve from socket : RC = " << returnCode << endl;
242 return;
243 }
244}