blob: 8a7a041e983d0a4be6e297327dcf4d874c2f01df [file] [log] [blame]
George Liuc453e162022-12-21 17:16:23 +08001#include <libpldm/base.h>
2#include <libpldm/platform.h>
3#include <libpldm/pldm.h>
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -06004
George Liu6492f522020-06-16 10:34:05 +08005#include <CLI/CLI.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -06006#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +08007#include <sdeventplus/event.hpp>
8#include <sdeventplus/source/io.hpp>
9
10#include <array>
11#include <iostream>
12
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
28 // Encode PLDM Request message
29 uint8_t effecterCount = 1;
30 std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterId) +
31 sizeof(effecterCount) +
32 sizeof(set_effecter_state_field)>
33 requestMsg{};
34 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
35 set_effecter_state_field stateField{PLDM_REQUEST_SET, state};
36 auto rc = encode_set_state_effecter_states_req(0, effecterId, effecterCount,
37 &stateField, request);
38 if (rc != PLDM_SUCCESS)
39 {
Riya Dixit49cfb132023-03-02 04:26:53 -060040 error("Message encode failure. PLDM error code = {RC}", "RC", lg2::hex,
41 rc);
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060042 return -1;
43 }
44
45 // Get fd of MCTP socket
46 int fd = pldm_open();
47 if (-1 == fd)
48 {
Riya Dixit49cfb132023-03-02 04:26:53 -060049 error("Failed to init mctp");
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060050 return -1;
51 }
52
53 // Create event loop and add a callback to handle EPOLLIN on fd
54 auto event = Event::get_default();
55 auto callback = [=](IO& io, int fd, uint32_t revents) {
56 if (!(revents & EPOLLIN))
57 {
58 return;
59 }
60
61 uint8_t* responseMsg = nullptr;
62 size_t responseMsgSize{};
63 auto rc = pldm_recv(mctpEid, fd, request->hdr.instance_id, &responseMsg,
64 &responseMsgSize);
65 if (!rc)
66 {
67 // We've got the response meant for the PLDM request msg that was
68 // sent out
69 io.set_enabled(Enabled::Off);
70 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg);
Riya Dixit49cfb132023-03-02 04:26:53 -060071 info("Done. PLDM RC = {RC}", "RC", lg2::hex,
72 static_cast<uint16_t>(response->payload[0]));
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060073 free(responseMsg);
74 exit(EXIT_SUCCESS);
75 }
76 };
77 IO io(event, fd, EPOLLIN, std::move(callback));
78
79 // Send PLDM Request message - pldm_send doesn't wait for response
80 rc = pldm_send(mctpEid, fd, requestMsg.data(), requestMsg.size());
Sampa Misra9f8d2b02021-03-24 08:33:14 +000081 if (0 > rc)
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060082 {
Riya Dixit49cfb132023-03-02 04:26:53 -060083 error(
84 "Failed to send message/receive response. RC = {RC} errno = {ERR}",
85 "RC", rc, "ERR", errno);
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060086 return -1;
87 }
88
89 event.loop();
90
91 return 0;
92}