Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #include "pldm_cmd_helper.hpp" |
| 2 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 3 | #include "common/transport.hpp" |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
| 5 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 6 | #include <libpldm/transport.h> |
| 7 | #include <libpldm/transport/af-mctp.h> |
| 8 | #include <libpldm/transport/mctp-demux.h> |
| 9 | #include <poll.h> |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 10 | #include <systemd/sd-bus.h> |
| 11 | |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 12 | #include <sdbusplus/server.hpp> |
| 13 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
| 14 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 15 | #include <exception> |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 16 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 17 | using namespace pldm::utils; |
| 18 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 19 | namespace pldmtool |
| 20 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 21 | namespace helper |
| 22 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 23 | |
| 24 | void CommandInterface::exec() |
| 25 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 26 | instanceId = instanceIdDb.next(mctp_eid); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 27 | auto [rc, requestMsg] = createRequestMsg(); |
| 28 | if (rc != PLDM_SUCCESS) |
| 29 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 30 | instanceIdDb.free(mctp_eid, instanceId); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 31 | std::cerr << "Failed to encode request message for " << pldmType << ":" |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 32 | << commandName << " rc = " << rc << "\n"; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 33 | return; |
| 34 | } |
| 35 | |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 36 | std::vector<uint8_t> responseMsg; |
| 37 | rc = pldmSendRecv(requestMsg, responseMsg); |
| 38 | |
| 39 | if (rc != PLDM_SUCCESS) |
| 40 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 41 | instanceIdDb.free(mctp_eid, instanceId); |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 42 | 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 Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 48 | instanceIdDb.free(mctp_eid, instanceId); |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int CommandInterface::pldmSendRecv(std::vector<uint8_t>& requestMsg, |
| 52 | std::vector<uint8_t>& responseMsg) |
| 53 | { |
Sridevi Ramesh | c91822b | 2020-02-27 09:26:41 -0600 | [diff] [blame] | 54 | // By default enable request/response msgs for pldmtool raw commands. |
| 55 | if (CommandInterface::pldmType == "raw") |
| 56 | { |
| 57 | pldmVerbose = true; |
| 58 | } |
| 59 | |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 60 | if (pldmVerbose) |
| 61 | { |
| 62 | std::cout << "pldmtool: "; |
| 63 | printBuffer(Tx, requestMsg); |
| 64 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 65 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 66 | 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 Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 75 | { |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 76 | std::cerr << "failed to pldm send recv\n"; |
| 77 | return rc; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 78 | } |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 79 | |
| 80 | responseMsg.resize(responseMessageSize); |
| 81 | memcpy(responseMsg.data(), responseMessage, responseMsg.size()); |
| 82 | |
| 83 | free(responseMessage); |
| 84 | |
| 85 | if (pldmVerbose) |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 86 | { |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 87 | std::cout << "pldmtool: "; |
| 88 | printBuffer(Rx, responseMsg); |
Pavithra Barithaya | f5ad6c7 | 2019-12-06 15:10:52 +0800 | [diff] [blame] | 89 | } |
John Wang | b754eee | 2020-02-15 16:10:25 +0800 | [diff] [blame] | 90 | return PLDM_SUCCESS; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 91 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 92 | } // namespace helper |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 93 | } // namespace pldmtool |