blob: afd49dd5757a7f04a595d313afe14cacfd12b456 [file] [log] [blame]
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05001#include "pldm_cmd_helper.hpp"
2
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +10003#include "common/transport.hpp"
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -06004#include "xyz/openbmc_project/Common/error.hpp"
5
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +10006#include <libpldm/transport.h>
7#include <libpldm/transport/af-mctp.h>
8#include <libpldm/transport/mctp-demux.h>
9#include <poll.h>
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -060010#include <systemd/sd-bus.h>
11
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -060012#include <sdbusplus/server.hpp>
13#include <xyz/openbmc_project/Logging/Entry/server.hpp>
14
George Liu6492f522020-06-16 10:34:05 +080015#include <exception>
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080016
Brad Bishop5079ac42021-08-19 18:35:06 -040017using namespace pldm::utils;
18
John Wang58a0e062019-11-08 15:38:15 +080019namespace pldmtool
20{
John Wang58a0e062019-11-08 15:38:15 +080021namespace helper
22{
John Wang58a0e062019-11-08 15:38:15 +080023
24void CommandInterface::exec()
25{
Dung Cao3d03f3f2023-09-07 06:51:33 +000026 instanceId = instanceIdDb.next(mctp_eid);
John Wang58a0e062019-11-08 15:38:15 +080027 auto [rc, requestMsg] = createRequestMsg();
28 if (rc != PLDM_SUCCESS)
29 {
Dung Cao3d03f3f2023-09-07 06:51:33 +000030 instanceIdDb.free(mctp_eid, instanceId);
John Wang58a0e062019-11-08 15:38:15 +080031 std::cerr << "Failed to encode request message for " << pldmType << ":"
Sampa Misraaa8ae722019-12-12 03:20:40 -060032 << commandName << " rc = " << rc << "\n";
John Wang58a0e062019-11-08 15:38:15 +080033 return;
34 }
35
John Wangb754eee2020-02-15 16:10:25 +080036 std::vector<uint8_t> responseMsg;
37 rc = pldmSendRecv(requestMsg, responseMsg);
38
39 if (rc != PLDM_SUCCESS)
40 {
Dung Cao3d03f3f2023-09-07 06:51:33 +000041 instanceIdDb.free(mctp_eid, instanceId);
John Wangb754eee2020-02-15 16:10:25 +080042 std::cerr << "pldmSendRecv: Failed to receive RC = " << rc << "\n";
43 return;
44 }
45
46 auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg.data());
47 parseResponseMsg(responsePtr, responseMsg.size() - sizeof(pldm_msg_hdr));
Dung Cao3d03f3f2023-09-07 06:51:33 +000048 instanceIdDb.free(mctp_eid, instanceId);
John Wangb754eee2020-02-15 16:10:25 +080049}
50
51int CommandInterface::pldmSendRecv(std::vector<uint8_t>& requestMsg,
52 std::vector<uint8_t>& responseMsg)
53{
Sridevi Rameshc91822b2020-02-27 09:26:41 -060054 // By default enable request/response msgs for pldmtool raw commands.
55 if (CommandInterface::pldmType == "raw")
56 {
57 pldmVerbose = true;
58 }
59
Tom Josephe5268cd2021-09-07 13:04:03 +053060 if (pldmVerbose)
61 {
62 std::cout << "pldmtool: ";
63 printBuffer(Tx, requestMsg);
64 }
John Wang58a0e062019-11-08 15:38:15 +080065
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100066 auto tid = mctp_eid;
67 PldmTransport pldmTransport{};
Thu Nguyen94270892023-10-25 17:11:44 +070068 uint8_t retry = 0;
69 int rc = PLDM_ERROR;
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100070
Thu Nguyen94270892023-10-25 17:11:44 +070071 while (PLDM_REQUESTER_SUCCESS != rc && retry <= numRetries)
72 {
73 void* responseMessage = nullptr;
74 size_t responseMessageSize{};
75
76 rc = pldmTransport.sendRecvMsg(tid, requestMsg.data(),
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100077 requestMsg.size(), responseMessage,
78 responseMessageSize);
Thu Nguyen94270892023-10-25 17:11:44 +070079 if (rc)
80 {
81 std::cerr << "[" << unsigned(retry) << "] pldm_send_recv error rc "
82 << rc << std::endl;
83 retry++;
84 continue;
85 }
86
87 responseMsg.resize(responseMessageSize);
88 memcpy(responseMsg.data(), responseMessage, responseMsg.size());
89
90 free(responseMessage);
91
92 if (pldmVerbose)
93 {
94 std::cout << "pldmtool: ";
95 printBuffer(Rx, responseMsg);
96 }
97 }
98
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100099 if (rc)
John Wang58a0e062019-11-08 15:38:15 +0800100 {
Thu Nguyen94270892023-10-25 17:11:44 +0700101 std::cerr << "failed to pldm send recv error rc " << rc << std::endl;
John Wang58a0e062019-11-08 15:38:15 +0800102 }
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000103
Thu Nguyen94270892023-10-25 17:11:44 +0700104 return rc;
John Wang58a0e062019-11-08 15:38:15 +0800105}
John Wang58a0e062019-11-08 15:38:15 +0800106} // namespace helper
Sampa Misraaa8ae722019-12-12 03:20:40 -0600107} // namespace pldmtool