blob: 9c7f94a341e33d98ae06a574d09f5ff2453b4949 [file] [log] [blame]
#include "pldm_cmd_helper.hpp"
#include "common/transport.hpp"
#include "xyz/openbmc_project/Common/error.hpp"
#include <libpldm/transport.h>
#include <libpldm/transport/af-mctp.h>
#include <libpldm/transport/mctp-demux.h>
#include <poll.h>
#include <systemd/sd-bus.h>
#include <sdbusplus/server.hpp>
#include <xyz/openbmc_project/Logging/Entry/server.hpp>
#include <exception>
using namespace pldm::utils;
namespace pldmtool
{
namespace helper
{
void CommandInterface::exec()
{
instanceId = instanceIdDb.next(mctp_eid);
auto [rc, requestMsg] = createRequestMsg();
if (rc != PLDM_SUCCESS)
{
instanceIdDb.free(mctp_eid, instanceId);
std::cerr << "Failed to encode request message for " << pldmType << ":"
<< commandName << " rc = " << rc << "\n";
return;
}
std::vector<uint8_t> responseMsg;
rc = pldmSendRecv(requestMsg, responseMsg);
if (rc != PLDM_SUCCESS)
{
instanceIdDb.free(mctp_eid, instanceId);
std::cerr << "pldmSendRecv: Failed to receive RC = " << rc << "\n";
return;
}
auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg.data());
parseResponseMsg(responsePtr, responseMsg.size() - sizeof(pldm_msg_hdr));
instanceIdDb.free(mctp_eid, instanceId);
}
int CommandInterface::pldmSendRecv(std::vector<uint8_t>& requestMsg,
std::vector<uint8_t>& responseMsg)
{
// By default enable request/response msgs for pldmtool raw commands.
if (CommandInterface::pldmType == "raw")
{
pldmVerbose = true;
}
if (pldmVerbose)
{
std::cout << "pldmtool: ";
printBuffer(Tx, requestMsg);
}
void* responseMessage = nullptr;
size_t responseMessageSize{};
auto tid = mctp_eid;
PldmTransport pldmTransport{};
int rc = pldmTransport.sendRecvMsg(tid, requestMsg.data(),
requestMsg.size(), responseMessage,
responseMessageSize);
if (rc)
{
std::cerr << "failed to pldm send recv\n";
return rc;
}
responseMsg.resize(responseMessageSize);
memcpy(responseMsg.data(), responseMessage, responseMsg.size());
free(responseMessage);
if (pldmVerbose)
{
std::cout << "pldmtool: ";
printBuffer(Rx, responseMsg);
}
return PLDM_SUCCESS;
}
} // namespace helper
} // namespace pldmtool