George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 1 | #include <libpldm/platform.h> |
| 2 | #include <libpldm/pldm.h> |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 3 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 4 | #include <CLI/CLI.hpp> |
| 5 | |
| 6 | #include <array> |
| 7 | #include <iostream> |
| 8 | |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 9 | int main(int argc, char** argv) |
| 10 | { |
| 11 | CLI::App app{"Send PLDM command SetStateEffecterStates"}; |
| 12 | uint8_t mctpEid{}; |
| 13 | app.add_option("-m,--mctp_eid", mctpEid, "MCTP EID")->required(); |
| 14 | uint16_t effecterId{}; |
| 15 | app.add_option("-e,--effecter", effecterId, "Effecter Id")->required(); |
| 16 | uint8_t state{}; |
| 17 | app.add_option("-s,--state", state, "New state value")->required(); |
| 18 | CLI11_PARSE(app, argc, argv); |
| 19 | |
| 20 | // Encode PLDM Request message |
| 21 | uint8_t effecterCount = 1; |
| 22 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterId) + |
| 23 | sizeof(effecterCount) + |
| 24 | sizeof(set_effecter_state_field)> |
| 25 | requestMsg{}; |
| 26 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 27 | set_effecter_state_field stateField{PLDM_REQUEST_SET, state}; |
| 28 | auto rc = encode_set_state_effecter_states_req(0, effecterId, effecterCount, |
| 29 | &stateField, request); |
| 30 | if (rc != PLDM_SUCCESS) |
| 31 | { |
| 32 | std::cerr << "Message encode failure. PLDM error code = " << std::hex |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 33 | << std::showbase << rc << "\n"; |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 34 | return -1; |
| 35 | } |
| 36 | |
| 37 | // Open connection to MCTP socket |
| 38 | int fd = pldm_open(); |
| 39 | if (-1 == fd) |
| 40 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 41 | std::cerr << "Failed to init mctp" |
| 42 | << "\n"; |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | uint8_t* responseMsg = nullptr; |
| 47 | size_t responseMsgSize{}; |
| 48 | // Send PLDM request msg and wait for response |
| 49 | rc = pldm_send_recv(mctpEid, fd, requestMsg.data(), requestMsg.size(), |
| 50 | &responseMsg, &responseMsgSize); |
| 51 | if (0 > rc) |
| 52 | { |
| 53 | std::cerr << "Failed to send message/receive response. RC = " << rc |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 54 | << ", errno = " << errno << "\n"; |
Deepak Kodihalli | 9d494bb | 2019-11-05 01:28:43 -0600 | [diff] [blame] | 55 | return -1; |
| 56 | } |
| 57 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg); |
| 58 | std::cout << "Done. PLDM RC = " << std::hex << std::showbase |
| 59 | << static_cast<uint16_t>(response->payload[0]) << std::endl; |
| 60 | free(responseMsg); |
| 61 | |
| 62 | return 0; |
| 63 | } |