Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #include "pldm_cmd_helper.hpp" |
| 2 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 3 | namespace pldmtool |
| 4 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 5 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 6 | namespace helper |
| 7 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 8 | |
| 9 | /* |
| 10 | * print the input buffer |
| 11 | * |
| 12 | */ |
| 13 | void printBuffer(const std::vector<uint8_t>& buffer) |
| 14 | { |
| 15 | std::ostringstream tempStream; |
| 16 | if (!buffer.empty()) |
| 17 | { |
| 18 | for (int byte : buffer) |
| 19 | { |
| 20 | tempStream << std::setfill('0') << std::setw(2) << std::hex << byte |
| 21 | << " "; |
| 22 | } |
| 23 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 24 | std::cout << tempStream.str() << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | /* |
| 28 | * Initialize the socket, send pldm command & recieve response from socket |
| 29 | * |
| 30 | */ |
| 31 | int mctpSockSendRecv(const std::vector<uint8_t>& requestMsg, |
| 32 | std::vector<uint8_t>& responseMsg) |
| 33 | { |
| 34 | const char devPath[] = "\0mctp-mux"; |
| 35 | int returnCode = 0; |
| 36 | |
| 37 | int sockFd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 38 | if (-1 == sockFd) |
| 39 | { |
| 40 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 41 | std::cerr << "Failed to create the socket : RC = " << sockFd << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 42 | return returnCode; |
| 43 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 44 | std::cout << "Success in creating the socket : RC = " << sockFd |
| 45 | << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 46 | |
| 47 | struct sockaddr_un addr |
| 48 | { |
| 49 | }; |
| 50 | addr.sun_family = AF_UNIX; |
| 51 | |
| 52 | memcpy(addr.sun_path, devPath, sizeof(devPath) - 1); |
| 53 | |
| 54 | CustomFD socketFd(sockFd); |
| 55 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 56 | sizeof(devPath) + sizeof(addr.sun_family) - 1); |
| 57 | if (-1 == result) |
| 58 | { |
| 59 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 60 | std::cerr << "Failed to connect to socket : RC = " << returnCode |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 61 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 62 | return returnCode; |
| 63 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 64 | std::cout << "Success in connecting to socket : RC = " << returnCode |
| 65 | << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 66 | |
| 67 | auto pldmType = MCTP_MSG_TYPE_PLDM; |
| 68 | result = write(socketFd(), &pldmType, sizeof(pldmType)); |
| 69 | if (-1 == result) |
| 70 | { |
| 71 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 72 | std::cerr << "Failed to send message type as pldm to mctp : RC = " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 73 | << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 74 | return returnCode; |
| 75 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 76 | std::cout << "Success in sending message type as pldm to mctp : RC = " |
| 77 | << returnCode << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 78 | |
| 79 | result = send(socketFd(), requestMsg.data(), requestMsg.size(), 0); |
| 80 | if (-1 == result) |
| 81 | { |
| 82 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 83 | std::cerr << "Write to socket failure : RC = " << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 84 | return returnCode; |
| 85 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 86 | std::cout << "Write to socket successful : RC = " << result << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 87 | |
| 88 | // Read the response from socket |
| 89 | ssize_t peekedLength = recv(socketFd(), nullptr, 0, MSG_TRUNC | MSG_PEEK); |
| 90 | if (0 == peekedLength) |
| 91 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 92 | std::cerr << "Socket is closed : peekedLength = " << peekedLength |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 93 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 94 | return returnCode; |
| 95 | } |
| 96 | else if (peekedLength <= -1) |
| 97 | { |
| 98 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 99 | std::cerr << "recv() system call failed : RC = " << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 100 | return returnCode; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | // loopback response message |
| 105 | std::vector<uint8_t> loopBackRespMsg(peekedLength); |
| 106 | auto recvDataLength = |
| 107 | recv(socketFd(), reinterpret_cast<void*>(loopBackRespMsg.data()), |
| 108 | peekedLength, 0); |
| 109 | if (recvDataLength == peekedLength) |
| 110 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 111 | std::cout << "Total length:" << recvDataLength << std::endl; |
| 112 | std::cout << "Loopback response message:" << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 113 | printBuffer(loopBackRespMsg); |
| 114 | } |
| 115 | else |
| 116 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 117 | std::cerr << "Failure to read peeked length packet : RC = " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 118 | << returnCode << "\n"; |
| 119 | std::cerr << "peekedLength: " << peekedLength << "\n"; |
| 120 | std::cerr << "Total length: " << recvDataLength << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 121 | return returnCode; |
| 122 | } |
| 123 | |
| 124 | // Confirming on the first recv() the Request bit is set in |
| 125 | // pldm_msg_hdr struct. If set proceed with recv() or else, quit. |
| 126 | auto hdr = reinterpret_cast<const pldm_msg_hdr*>(&loopBackRespMsg[2]); |
| 127 | uint8_t request = hdr->request; |
| 128 | if (request == PLDM_REQUEST) |
| 129 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 130 | std::cout << "On first recv(),response == request : RC = " |
| 131 | << returnCode << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 132 | ssize_t peekedLength = |
| 133 | recv(socketFd(), nullptr, 0, MSG_PEEK | MSG_TRUNC); |
| 134 | |
| 135 | responseMsg.resize(peekedLength); |
| 136 | recvDataLength = |
| 137 | recv(socketFd(), reinterpret_cast<void*>(responseMsg.data()), |
| 138 | peekedLength, 0); |
| 139 | if (recvDataLength == peekedLength) |
| 140 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 141 | std::cout << "Total length: " << recvDataLength << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 142 | } |
| 143 | else |
| 144 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 145 | std::cerr << "Failure to read response length packet: length = " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 146 | << recvDataLength << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 147 | return returnCode; |
| 148 | } |
| 149 | } |
| 150 | else |
| 151 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 152 | std::cerr << "On first recv(),request != response : RC = " |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 153 | << returnCode << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 154 | return returnCode; |
| 155 | } |
| 156 | } |
| 157 | returnCode = shutdown(socketFd(), SHUT_RDWR); |
| 158 | if (-1 == returnCode) |
| 159 | { |
| 160 | returnCode = -errno; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 161 | std::cerr << "Failed to shutdown the socket : RC = " << returnCode |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 162 | << "\n"; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 163 | return returnCode; |
| 164 | } |
| 165 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 166 | std::cout << "Shutdown Socket successful : RC = " << returnCode |
| 167 | << std::endl; |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 168 | return PLDM_SUCCESS; |
| 169 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 170 | |
| 171 | void CommandInterface::exec() |
| 172 | { |
| 173 | auto [rc, requestMsg] = createRequestMsg(); |
| 174 | if (rc != PLDM_SUCCESS) |
| 175 | { |
| 176 | std::cerr << "Failed to encode request message for " << pldmType << ":" |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 177 | << commandName << " rc = " << rc << "\n"; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| 181 | std::cout << "Encode request successfully" << std::endl; |
| 182 | |
| 183 | // Insert the PLDM message type and EID at the begining of the request msg. |
| 184 | requestMsg.insert(requestMsg.begin(), MCTP_MSG_TYPE_PLDM); |
| 185 | requestMsg.insert(requestMsg.begin(), PLDM_ENTITY_ID); |
| 186 | |
| 187 | std::cout << "Request Message:" << std::endl; |
| 188 | printBuffer(requestMsg); |
| 189 | |
| 190 | std::vector<uint8_t> responseMsg; |
| 191 | rc = mctpSockSendRecv(requestMsg, responseMsg); |
| 192 | |
| 193 | if (rc != PLDM_SUCCESS) |
| 194 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 195 | std::cerr << "Failed to receive from socket: RC = " << rc << "\n"; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
| 199 | std::cout << "Response Message:" << std::endl; |
| 200 | printBuffer(responseMsg); |
| 201 | |
| 202 | auto responsePtr = reinterpret_cast<struct pldm_msg*>( |
| 203 | responseMsg.data() + 2 /*skip the mctp header*/); |
| 204 | parseResponseMsg(responsePtr, responseMsg.size() - |
| 205 | 2 /*skip the mctp header*/ - |
| 206 | sizeof(pldm_msg_hdr)); |
| 207 | } |
| 208 | |
| 209 | } // namespace helper |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 210 | } // namespace pldmtool |