Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 1 | #include "common/transport.hpp" |
| 2 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 3 | #include <libpldm/base.h> |
| 4 | #include <libpldm/platform.h> |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 5 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 6 | #include <CLI/CLI.hpp> |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 8 | #include <sdeventplus/event.hpp> |
| 9 | #include <sdeventplus/source/io.hpp> |
| 10 | |
| 11 | #include <array> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 12 | |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 13 | using namespace sdeventplus; |
| 14 | using namespace sdeventplus::source; |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 15 | PHOSPHOR_LOG2_USING; |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 16 | |
| 17 | int 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 Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 28 | pldm_tid_t dstTid = static_cast<pldm_tid_t>(mctpEid); |
| 29 | |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 30 | // Encode PLDM Request message |
| 31 | uint8_t effecterCount = 1; |
| 32 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterId) + |
| 33 | sizeof(effecterCount) + |
| 34 | sizeof(set_effecter_state_field)> |
| 35 | 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 Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 42 | error("Message encode failure. PLDM error code = {RC}", "RC", lg2::hex, |
| 43 | rc); |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 44 | return -1; |
| 45 | } |
| 46 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 47 | PldmTransport pldmTransport{}; |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 48 | |
| 49 | // Create event loop and add a callback to handle EPOLLIN on fd |
| 50 | auto event = Event::get_default(); |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 51 | auto callback = [=, &pldmTransport](IO& io, int fd, |
| 52 | uint32_t revents) mutable { |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 53 | if (!(revents & EPOLLIN)) |
| 54 | { |
| 55 | return; |
| 56 | } |
| 57 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 58 | if (pldmTransport.getEventSource() != fd) |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 59 | { |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 60 | return; |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 61 | } |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 62 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 63 | void* responseMsg = nullptr; |
| 64 | size_t responseMsgSize{}; |
| 65 | pldm_tid_t srcTid; |
| 66 | auto rc = pldmTransport.recvMsg(srcTid, responseMsg, responseMsgSize); |
| 67 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg); |
| 68 | if (rc || dstTid != srcTid || |
| 69 | !pldm_msg_hdr_correlate_response(&request->hdr, &response->hdr)) |
| 70 | { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // We've got the response meant for the PLDM request msg that was sent |
| 75 | // out |
| 76 | io.set_enabled(Enabled::Off); |
| 77 | info("Done. PLDM RC = {RC}", "RC", lg2::hex, |
| 78 | static_cast<uint16_t>(response->payload[0])); |
| 79 | free(responseMsg); |
| 80 | exit(EXIT_SUCCESS); |
| 81 | }; |
| 82 | IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback)); |
| 83 | |
| 84 | rc = pldmTransport.sendMsg(dstTid, requestMsg.data(), requestMsg.size()); |
Sampa Misra | 9f8d2b0 | 2021-03-24 08:33:14 +0000 | [diff] [blame] | 85 | if (0 > rc) |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 86 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 87 | error( |
| 88 | "Failed to send message/receive response. RC = {RC} errno = {ERR}", |
| 89 | "RC", rc, "ERR", errno); |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | event.loop(); |
| 94 | |
| 95 | return 0; |
| 96 | } |