blob: 6420328f90baba998ad09edc81fc8bed2476a785 [file] [log] [blame]
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +10001#include "common/transport.hpp"
2
George Liuc453e162022-12-21 17:16:23 +08003#include <libpldm/base.h>
4#include <libpldm/platform.h>
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -06005
George Liu6492f522020-06-16 10:34:05 +08006#include <CLI/CLI.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -06007#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +08008#include <sdeventplus/event.hpp>
9#include <sdeventplus/source/io.hpp>
10
11#include <array>
George Liu6492f522020-06-16 10:34:05 +080012
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060013using namespace sdeventplus;
14using namespace sdeventplus::source;
Riya Dixit49cfb132023-03-02 04:26:53 -060015PHOSPHOR_LOG2_USING;
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060016
17int main(int argc, char** argv)
18{
19 CLI::App app{"Send PLDM command SetStateEffecterStates"};
20 uint8_t mctpEid{};
21 app.add_option("-m,--mctp_eid", mctpEid, "MCTP EID")->required();
22 uint16_t effecterId{};
23 app.add_option("-e,--effecter", effecterId, "Effecter Id")->required();
24 uint8_t state{};
25 app.add_option("-s,--state", state, "New state value")->required();
26 CLI11_PARSE(app, argc, argv);
27
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100028 pldm_tid_t dstTid = static_cast<pldm_tid_t>(mctpEid);
29
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060030 // Encode PLDM Request message
31 uint8_t effecterCount = 1;
Patrick Williams16c2a0a2024-08-16 15:20:59 -040032 std::array<uint8_t,
33 sizeof(pldm_msg_hdr) + sizeof(effecterId) +
34 sizeof(effecterCount) + sizeof(set_effecter_state_field)>
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060035 requestMsg{};
36 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
37 set_effecter_state_field stateField{PLDM_REQUEST_SET, state};
38 auto rc = encode_set_state_effecter_states_req(0, effecterId, effecterCount,
39 &stateField, request);
40 if (rc != PLDM_SUCCESS)
41 {
Riya Dixit087a7512024-04-06 14:28:08 -050042 error(
43 "Failed to encode set state effecter states request, response code '{RC}'",
44 "RC", lg2::hex, rc);
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060045 return -1;
46 }
47
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100048 PldmTransport pldmTransport{};
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060049
50 // Create event loop and add a callback to handle EPOLLIN on fd
51 auto event = Event::get_default();
Patrick Williams16c2a0a2024-08-16 15:20:59 -040052 auto callback = [=,
53 &pldmTransport](IO& io, int fd, uint32_t revents) mutable {
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060054 if (!(revents & EPOLLIN))
55 {
56 return;
57 }
58
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100059 if (pldmTransport.getEventSource() != fd)
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060060 {
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100061 return;
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060062 }
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060063
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100064 void* responseMsg = nullptr;
65 size_t responseMsgSize{};
66 pldm_tid_t srcTid;
67 auto rc = pldmTransport.recvMsg(srcTid, responseMsg, responseMsgSize);
68 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg);
69 if (rc || dstTid != srcTid ||
70 !pldm_msg_hdr_correlate_response(&request->hdr, &response->hdr))
71 {
72 return;
73 }
74
75 // We've got the response meant for the PLDM request msg that was sent
76 // out
77 io.set_enabled(Enabled::Off);
Riya Dixit087a7512024-04-06 14:28:08 -050078 info(
79 "Done! Got the response for PLDM request message, response code '{RC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -050080 "RC", lg2::hex, response->payload[0]);
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100081 free(responseMsg);
82 exit(EXIT_SUCCESS);
83 };
84 IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback));
85
86 rc = pldmTransport.sendMsg(dstTid, requestMsg.data(), requestMsg.size());
Sampa Misra9f8d2b02021-03-24 08:33:14 +000087 if (0 > rc)
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060088 {
Riya Dixit49cfb132023-03-02 04:26:53 -060089 error(
Riya Dixit087a7512024-04-06 14:28:08 -050090 "Failed to send message/receive response, response code '{RC}' and error - {ERROR}",
91 "RC", rc, "ERROR", errno);
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060092 return -1;
93 }
94
95 event.loop();
96
97 return 0;
98}