blob: c71f079f086d6608f7c50b17d7b0e9302234ad40 [file] [log] [blame]
George Liuc453e162022-12-21 17:16:23 +08001#include <libpldm/platform.h>
2#include <libpldm/pldm.h>
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -06003
George Liu6492f522020-06-16 10:34:05 +08004#include <CLI/CLI.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -06005#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +08006
7#include <array>
8#include <iostream>
9
Riya Dixit49cfb132023-03-02 04:26:53 -060010PHOSPHOR_LOG2_USING;
11
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060012int main(int argc, char** argv)
13{
14 CLI::App app{"Send PLDM command SetStateEffecterStates"};
15 uint8_t mctpEid{};
16 app.add_option("-m,--mctp_eid", mctpEid, "MCTP EID")->required();
17 uint16_t effecterId{};
18 app.add_option("-e,--effecter", effecterId, "Effecter Id")->required();
19 uint8_t state{};
20 app.add_option("-s,--state", state, "New state value")->required();
21 CLI11_PARSE(app, argc, argv);
22
23 // Encode PLDM Request message
24 uint8_t effecterCount = 1;
25 std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterId) +
26 sizeof(effecterCount) +
27 sizeof(set_effecter_state_field)>
28 requestMsg{};
29 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
30 set_effecter_state_field stateField{PLDM_REQUEST_SET, state};
31 auto rc = encode_set_state_effecter_states_req(0, effecterId, effecterCount,
32 &stateField, request);
33 if (rc != PLDM_SUCCESS)
34 {
Riya Dixit49cfb132023-03-02 04:26:53 -060035 error("Message encode failure. PLDM error code = {RC}", "RC", lg2::hex,
36 rc);
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060037 return -1;
38 }
39
40 // Open connection to MCTP socket
41 int fd = pldm_open();
42 if (-1 == fd)
43 {
Riya Dixit49cfb132023-03-02 04:26:53 -060044 error("Failed to init mctp");
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060045 return -1;
46 }
47
48 uint8_t* responseMsg = nullptr;
49 size_t responseMsgSize{};
50 // Send PLDM request msg and wait for response
51 rc = pldm_send_recv(mctpEid, fd, requestMsg.data(), requestMsg.size(),
52 &responseMsg, &responseMsgSize);
53 if (0 > rc)
54 {
Riya Dixit49cfb132023-03-02 04:26:53 -060055 error(
56 "Failed to send message/receive response. RC = {RC}, errno = {ERR}",
57 "RC", rc, "ERR", errno);
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060058 return -1;
59 }
60 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg);
Riya Dixit49cfb132023-03-02 04:26:53 -060061 info("Done. PLDM RC = {RC}", "RC", lg2::hex,
62 static_cast<uint16_t>(response->payload[0]));
Deepak Kodihalli9d494bb2019-11-05 01:28:43 -060063 free(responseMsg);
64
65 return 0;
66}