blob: 9c7f94a341e33d98ae06a574d09f5ff2453b4949 [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 void* responseMessage = nullptr;
67 size_t responseMessageSize{};
68 auto tid = mctp_eid;
69 PldmTransport pldmTransport{};
70
71 int rc = pldmTransport.sendRecvMsg(tid, requestMsg.data(),
72 requestMsg.size(), responseMessage,
73 responseMessageSize);
74 if (rc)
John Wang58a0e062019-11-08 15:38:15 +080075 {
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100076 std::cerr << "failed to pldm send recv\n";
77 return rc;
John Wang58a0e062019-11-08 15:38:15 +080078 }
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100079
80 responseMsg.resize(responseMessageSize);
81 memcpy(responseMsg.data(), responseMessage, responseMsg.size());
82
83 free(responseMessage);
84
85 if (pldmVerbose)
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080086 {
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100087 std::cout << "pldmtool: ";
88 printBuffer(Rx, responseMsg);
Pavithra Barithayaf5ad6c72019-12-06 15:10:52 +080089 }
John Wangb754eee2020-02-15 16:10:25 +080090 return PLDM_SUCCESS;
John Wang58a0e062019-11-08 15:38:15 +080091}
John Wang58a0e062019-11-08 15:38:15 +080092} // namespace helper
Sampa Misraaa8ae722019-12-12 03:20:40 -060093} // namespace pldmtool