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 | auto tid = mctp_eid; |
| 67 | PldmTransport pldmTransport{}; |
Thu Nguyen | 9427089 | 2023-10-25 17:11:44 +0700 | [diff] [blame] | 68 | uint8_t retry = 0; |
| 69 | int rc = PLDM_ERROR; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 70 | |
Thu Nguyen | 9427089 | 2023-10-25 17:11:44 +0700 | [diff] [blame] | 71 | while (PLDM_REQUESTER_SUCCESS != rc && retry <= numRetries) |
| 72 | { |
| 73 | void* responseMessage = nullptr; |
| 74 | size_t responseMessageSize{}; |
| 75 | |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 76 | rc = |
| 77 | pldmTransport.sendRecvMsg(tid, requestMsg.data(), requestMsg.size(), |
| 78 | responseMessage, responseMessageSize); |
Thu Nguyen | 9427089 | 2023-10-25 17:11:44 +0700 | [diff] [blame] | 79 | 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 Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 99 | if (rc) |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 100 | { |
Thu Nguyen | 9427089 | 2023-10-25 17:11:44 +0700 | [diff] [blame] | 101 | std::cerr << "failed to pldm send recv error rc " << rc << std::endl; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 102 | } |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 103 | |
Thu Nguyen | 9427089 | 2023-10-25 17:11:44 +0700 | [diff] [blame] | 104 | return rc; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 105 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 106 | } // namespace helper |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 107 | } // namespace pldmtool |