1f7f5da97SDeepak Kodihalli #include "pldm_cmd_helper.hpp"
2f7f5da97SDeepak Kodihalli
31ed5f7a6SRashmica Gupta #include "common/transport.hpp"
4f7f5da97SDeepak Kodihalli #include "xyz/openbmc_project/Common/error.hpp"
5f7f5da97SDeepak Kodihalli
61ed5f7a6SRashmica Gupta #include <libpldm/transport.h>
71ed5f7a6SRashmica Gupta #include <libpldm/transport/af-mctp.h>
81ed5f7a6SRashmica Gupta #include <libpldm/transport/mctp-demux.h>
91ed5f7a6SRashmica Gupta #include <poll.h>
10f7f5da97SDeepak Kodihalli #include <systemd/sd-bus.h>
11f7f5da97SDeepak Kodihalli
12f7f5da97SDeepak Kodihalli #include <sdbusplus/server.hpp>
13f7f5da97SDeepak Kodihalli #include <xyz/openbmc_project/Logging/Entry/server.hpp>
14f7f5da97SDeepak Kodihalli
15f7f5da97SDeepak Kodihalli #include <exception>
16f7f5da97SDeepak Kodihalli
175079ac4aSBrad Bishop using namespace pldm::utils;
185079ac4aSBrad Bishop
19f7f5da97SDeepak Kodihalli namespace pldmtool
20f7f5da97SDeepak Kodihalli {
21f7f5da97SDeepak Kodihalli namespace helper
22f7f5da97SDeepak Kodihalli {
23f7f5da97SDeepak Kodihalli
exec()24f7f5da97SDeepak Kodihalli void CommandInterface::exec()
25f7f5da97SDeepak Kodihalli {
263d03f3faSDung Cao instanceId = instanceIdDb.next(mctp_eid);
27f7f5da97SDeepak Kodihalli auto [rc, requestMsg] = createRequestMsg();
28f7f5da97SDeepak Kodihalli if (rc != PLDM_SUCCESS)
29f7f5da97SDeepak Kodihalli {
303d03f3faSDung Cao instanceIdDb.free(mctp_eid, instanceId);
31f7f5da97SDeepak Kodihalli std::cerr << "Failed to encode request message for " << pldmType << ":"
32f7f5da97SDeepak Kodihalli << commandName << " rc = " << rc << "\n";
33f7f5da97SDeepak Kodihalli return;
34f7f5da97SDeepak Kodihalli }
35f7f5da97SDeepak Kodihalli
36f7f5da97SDeepak Kodihalli std::vector<uint8_t> responseMsg;
37f7f5da97SDeepak Kodihalli rc = pldmSendRecv(requestMsg, responseMsg);
38f7f5da97SDeepak Kodihalli
39f7f5da97SDeepak Kodihalli if (rc != PLDM_SUCCESS)
40f7f5da97SDeepak Kodihalli {
413d03f3faSDung Cao instanceIdDb.free(mctp_eid, instanceId);
42f7f5da97SDeepak Kodihalli std::cerr << "pldmSendRecv: Failed to receive RC = " << rc << "\n";
43f7f5da97SDeepak Kodihalli return;
44f7f5da97SDeepak Kodihalli }
45f7f5da97SDeepak Kodihalli
46f7f5da97SDeepak Kodihalli auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg.data());
47f7f5da97SDeepak Kodihalli parseResponseMsg(responsePtr, responseMsg.size() - sizeof(pldm_msg_hdr));
483d03f3faSDung Cao instanceIdDb.free(mctp_eid, instanceId);
49f7f5da97SDeepak Kodihalli }
50f7f5da97SDeepak Kodihalli
pldmSendRecv(std::vector<uint8_t> & requestMsg,std::vector<uint8_t> & responseMsg)51f7f5da97SDeepak Kodihalli int CommandInterface::pldmSendRecv(std::vector<uint8_t>& requestMsg,
52f7f5da97SDeepak Kodihalli std::vector<uint8_t>& responseMsg)
53f7f5da97SDeepak Kodihalli {
54f7f5da97SDeepak Kodihalli // By default enable request/response msgs for pldmtool raw commands.
55f7f5da97SDeepak Kodihalli if (CommandInterface::pldmType == "raw")
56f7f5da97SDeepak Kodihalli {
57f7f5da97SDeepak Kodihalli pldmVerbose = true;
58f7f5da97SDeepak Kodihalli }
59f7f5da97SDeepak Kodihalli
60e5268cdaSTom Joseph if (pldmVerbose)
61e5268cdaSTom Joseph {
62e5268cdaSTom Joseph std::cout << "pldmtool: ";
63e5268cdaSTom Joseph printBuffer(Tx, requestMsg);
64e5268cdaSTom Joseph }
65f7f5da97SDeepak Kodihalli
661ed5f7a6SRashmica Gupta auto tid = mctp_eid;
671ed5f7a6SRashmica Gupta PldmTransport pldmTransport{};
689427089eSThu Nguyen uint8_t retry = 0;
699427089eSThu Nguyen int rc = PLDM_ERROR;
701ed5f7a6SRashmica Gupta
719427089eSThu Nguyen while (PLDM_REQUESTER_SUCCESS != rc && retry <= numRetries)
729427089eSThu Nguyen {
739427089eSThu Nguyen void* responseMessage = nullptr;
749427089eSThu Nguyen size_t responseMessageSize{};
759427089eSThu Nguyen
76*16c2a0a0SPatrick Williams rc =
77*16c2a0a0SPatrick Williams pldmTransport.sendRecvMsg(tid, requestMsg.data(), requestMsg.size(),
78*16c2a0a0SPatrick Williams responseMessage, responseMessageSize);
791ed5f7a6SRashmica Gupta if (rc)
801ed5f7a6SRashmica Gupta {
819427089eSThu Nguyen std::cerr << "[" << unsigned(retry) << "] pldm_send_recv error rc "
829427089eSThu Nguyen << rc << std::endl;
839427089eSThu Nguyen retry++;
849427089eSThu Nguyen continue;
851ed5f7a6SRashmica Gupta }
86f7f5da97SDeepak Kodihalli
87f7f5da97SDeepak Kodihalli responseMsg.resize(responseMessageSize);
88f7f5da97SDeepak Kodihalli memcpy(responseMsg.data(), responseMessage, responseMsg.size());
89f7f5da97SDeepak Kodihalli
90f7f5da97SDeepak Kodihalli free(responseMessage);
912b85d784SDung Cao
92e5268cdaSTom Joseph if (pldmVerbose)
93e5268cdaSTom Joseph {
94e5268cdaSTom Joseph std::cout << "pldmtool: ";
95e5268cdaSTom Joseph printBuffer(Rx, responseMsg);
96e5268cdaSTom Joseph }
979427089eSThu Nguyen }
989427089eSThu Nguyen
999427089eSThu Nguyen if (rc)
1009427089eSThu Nguyen {
1019427089eSThu Nguyen std::cerr << "failed to pldm send recv error rc " << rc << std::endl;
1029427089eSThu Nguyen }
1039427089eSThu Nguyen
1049427089eSThu Nguyen return rc;
105f7f5da97SDeepak Kodihalli }
106f7f5da97SDeepak Kodihalli } // namespace helper
107f7f5da97SDeepak Kodihalli } // namespace pldmtool
108