George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | #include "libpldm/base.h" |
| 2 | #include "libpldm/bios.h" |
| 3 | #include "libpldm/pdr.h" |
| 4 | #include "libpldm/platform.h" |
| 5 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 6 | #include "common/utils.hpp" |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 7 | #include "dbus_impl_requester.hpp" |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 8 | #include "invoker.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 9 | |
| 10 | #include <err.h> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 11 | #include <getopt.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 12 | #include <poll.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/un.h> |
| 17 | #include <unistd.h> |
| 18 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 19 | #include <sdeventplus/event.hpp> |
| 20 | #include <sdeventplus/source/io.hpp> |
| 21 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 22 | #include <cstdio> |
| 23 | #include <cstring> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 24 | #include <fstream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 25 | #include <iomanip> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 26 | #include <iostream> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 27 | #include <iterator> |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 28 | #include <memory> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 29 | #include <sstream> |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 30 | #include <stdexcept> |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 31 | #include <string> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 34 | #ifdef LIBPLDMRESPONDER |
| 35 | #include "dbus_impl_pdr.hpp" |
| 36 | #include "host-bmc/dbus_to_event_handler.hpp" |
| 37 | #include "host-bmc/dbus_to_host_effecters.hpp" |
| 38 | #include "host-bmc/host_pdr_handler.hpp" |
| 39 | #include "libpldmresponder/base.hpp" |
| 40 | #include "libpldmresponder/bios.hpp" |
| 41 | #include "libpldmresponder/fru.hpp" |
| 42 | #include "libpldmresponder/oem_handler.hpp" |
| 43 | #include "libpldmresponder/platform.hpp" |
| 44 | #include "xyz/openbmc_project/PLDM/Event/server.hpp" |
| 45 | #endif |
| 46 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 47 | #ifdef OEM_IBM |
| 48 | #include "libpldmresponder/file_io.hpp" |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 49 | #include "libpldmresponder/oem_ibm_handler.hpp" |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1; |
| 53 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 54 | using namespace pldm; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 55 | using namespace sdeventplus; |
| 56 | using namespace sdeventplus::source; |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 57 | using namespace pldm::responder; |
| 58 | using namespace pldm::utils; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 59 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 60 | static Response processRxMsg(const std::vector<uint8_t>& requestMsg, |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 61 | Invoker& invoker, dbus_api::Requester& requester) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 62 | { |
| 63 | |
| 64 | Response response; |
| 65 | uint8_t eid = requestMsg[0]; |
| 66 | uint8_t type = requestMsg[1]; |
| 67 | pldm_header_info hdrFields{}; |
| 68 | auto hdr = reinterpret_cast<const pldm_msg_hdr*>( |
| 69 | requestMsg.data() + sizeof(eid) + sizeof(type)); |
| 70 | if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields)) |
| 71 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 72 | std::cerr << "Empty PLDM request header \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 73 | } |
| 74 | else if (PLDM_RESPONSE != hdrFields.msg_type) |
| 75 | { |
| 76 | auto request = reinterpret_cast<const pldm_msg*>(hdr); |
| 77 | size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr) - |
| 78 | sizeof(eid) - sizeof(type); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 79 | try |
| 80 | { |
| 81 | response = invoker.handle(hdrFields.pldm_type, hdrFields.command, |
| 82 | request, requestLen); |
| 83 | } |
| 84 | catch (const std::out_of_range& e) |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 85 | { |
| 86 | uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD; |
| 87 | response.resize(sizeof(pldm_msg_hdr)); |
| 88 | auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data()); |
| 89 | pldm_header_info header{}; |
| 90 | header.msg_type = PLDM_RESPONSE; |
| 91 | header.instance = hdrFields.instance; |
| 92 | header.pldm_type = hdrFields.pldm_type; |
| 93 | header.command = hdrFields.command; |
| 94 | auto result = pack_pldm_header(&header, responseHdr); |
| 95 | if (PLDM_SUCCESS != result) |
| 96 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 97 | std::cerr << "Failed adding response header \n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 98 | } |
| 99 | response.insert(response.end(), completion_code); |
| 100 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 101 | } |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 102 | else |
| 103 | { |
| 104 | requester.markFree(eid, hdr->instance_id); |
| 105 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 106 | return response; |
| 107 | } |
| 108 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 109 | void optionUsage(void) |
| 110 | { |
| 111 | std::cerr << "Usage: pldmd [options]\n"; |
| 112 | std::cerr << "Options:\n"; |
| 113 | std::cerr |
| 114 | << " --verbose=<0/1> 0 - Disable verbosity, 1 - Enable verbosity\n"; |
| 115 | std::cerr << "Defaulted settings: --verbose=0 \n"; |
| 116 | } |
| 117 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 118 | int main(int argc, char** argv) |
| 119 | { |
| 120 | |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 121 | bool verbose = false; |
| 122 | static struct option long_options[] = { |
| 123 | {"verbose", required_argument, 0, 'v'}, {0, 0, 0, 0}}; |
| 124 | |
| 125 | auto argflag = getopt_long(argc, argv, "v:", long_options, nullptr); |
| 126 | switch (argflag) |
| 127 | { |
| 128 | case 'v': |
| 129 | switch (std::stoi(optarg)) |
| 130 | { |
| 131 | case 0: |
| 132 | verbose = false; |
| 133 | break; |
| 134 | case 1: |
| 135 | verbose = true; |
| 136 | break; |
| 137 | default: |
| 138 | optionUsage(); |
| 139 | break; |
| 140 | } |
| 141 | break; |
| 142 | default: |
| 143 | optionUsage(); |
| 144 | break; |
| 145 | } |
| 146 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 147 | /* Create local socket. */ |
| 148 | int returnCode = 0; |
| 149 | int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 150 | if (-1 == sockfd) |
| 151 | { |
| 152 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 153 | std::cerr << "Failed to create the socket, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 154 | exit(EXIT_FAILURE); |
| 155 | } |
| 156 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 157 | auto event = Event::get_default(); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 158 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 159 | dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm"); |
| 160 | Invoker invoker{}; |
| 161 | |
| 162 | #ifdef LIBPLDMRESPONDER |
| 163 | using namespace pldm::state_sensor; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 164 | std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo( |
| 165 | pldm_pdr_init(), pldm_pdr_destroy); |
| 166 | std::unique_ptr<pldm_entity_association_tree, |
| 167 | decltype(&pldm_entity_association_tree_destroy)> |
| 168 | entityTree(pldm_entity_association_tree_init(), |
| 169 | pldm_entity_association_tree_destroy); |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame^] | 170 | std::unique_ptr<pldm_entity_association_tree, |
| 171 | decltype(&pldm_entity_association_tree_destroy)> |
| 172 | bmcEntityTree(pldm_entity_association_tree_init(), |
| 173 | pldm_entity_association_tree_destroy); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 174 | std::unique_ptr<HostPDRHandler> hostPDRHandler; |
Sampa Misra | c0c6054 | 2020-07-01 02:34:25 -0500 | [diff] [blame] | 175 | std::unique_ptr<pldm::host_effecters::HostEffecterParser> |
| 176 | hostEffecterParser; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 177 | std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler; |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 178 | auto dbusHandler = std::make_unique<DBusHandler>(); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 179 | auto hostEID = pldm::utils::readHostEID(); |
| 180 | if (hostEID) |
| 181 | { |
| 182 | hostPDRHandler = std::make_unique<HostPDRHandler>( |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 183 | sockfd, hostEID, event, pdrRepo.get(), EVENTS_JSONS_DIR, |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame^] | 184 | entityTree.get(), bmcEntityTree.get(), dbusImplReq, verbose); |
Sampa Misra | c0c6054 | 2020-07-01 02:34:25 -0500 | [diff] [blame] | 185 | hostEffecterParser = |
| 186 | std::make_unique<pldm::host_effecters::HostEffecterParser>( |
Deepak Kodihalli | b5c227e | 2020-07-13 06:58:34 -0500 | [diff] [blame] | 187 | &dbusImplReq, sockfd, pdrRepo.get(), dbusHandler.get(), |
Sampa Misra | c0c6054 | 2020-07-01 02:34:25 -0500 | [diff] [blame] | 188 | HOST_JSONS_DIR, verbose); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 189 | dbusToPLDMEventHandler = |
| 190 | std::make_unique<DbusToPLDMEvent>(sockfd, hostEID, dbusImplReq); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 191 | } |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 192 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
| 193 | |
| 194 | #ifdef OEM_IBM |
| 195 | std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate = |
| 196 | std::make_unique<pldm::responder::CodeUpdate>(dbusHandler.get()); |
Varsha Kaverappa | 3ca29df | 2020-09-27 12:39:22 -0500 | [diff] [blame] | 197 | codeUpdate->clearDirPath(LID_STAGING_DIR); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 198 | oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>( |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 199 | dbusHandler.get(), codeUpdate.get(), sockfd, hostEID, dbusImplReq, |
| 200 | event); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 201 | codeUpdate->setOemPlatformHandler(oemPlatformHandler.get()); |
| 202 | invoker.registerHandler( |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 203 | PLDM_OEM, std::make_unique<oem_ibm::Handler>( |
| 204 | oemPlatformHandler.get(), sockfd, hostEID, &dbusImplReq)); |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 205 | #endif |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 206 | invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>()); |
Tom Joseph | 7f839f9 | 2020-09-21 10:20:44 +0530 | [diff] [blame] | 207 | invoker.registerHandler(PLDM_BIOS, std::make_unique<bios::Handler>( |
| 208 | sockfd, hostEID, &dbusImplReq)); |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 209 | auto fruHandler = std::make_unique<fru::Handler>( |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame^] | 210 | FRU_JSONS_DIR, pdrRepo.get(), entityTree.get(), bmcEntityTree.get()); |
Tom Joseph | 33e9c7e | 2020-06-11 22:09:52 +0530 | [diff] [blame] | 211 | // FRU table is built lazily when a FRU command or Get PDR command is |
| 212 | // handled. To enable building FRU table, the FRU handler is passed to the |
| 213 | // Platform handler. |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 214 | auto platformHandler = std::make_unique<platform::Handler>( |
| 215 | dbusHandler.get(), PDR_JSONS_DIR, pdrRepo.get(), hostPDRHandler.get(), |
| 216 | dbusToPLDMEventHandler.get(), fruHandler.get(), |
Sampa Misra | 5fb37d5 | 2021-03-06 07:26:00 -0600 | [diff] [blame] | 217 | oemPlatformHandler.get(), event, true); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 218 | #ifdef OEM_IBM |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 219 | pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler = |
| 220 | dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>( |
| 221 | oemPlatformHandler.get()); |
| 222 | oemIbmPlatformHandler->setPlatformHandler(platformHandler.get()); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 223 | #endif |
| 224 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 225 | invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler)); |
| 226 | invoker.registerHandler(PLDM_FRU, std::move(fruHandler)); |
Tom Joseph | 02b4ee4 | 2021-05-02 22:44:36 -0700 | [diff] [blame] | 227 | dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get()); |
| 228 | sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent( |
| 229 | bus, "/xyz/openbmc_project/pldm"); |
| 230 | #endif |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 231 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 232 | pldm::utils::CustomFD socketFd(sockfd); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 233 | |
| 234 | struct sockaddr_un addr |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 235 | {}; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 236 | addr.sun_family = AF_UNIX; |
| 237 | const char path[] = "\0mctp-mux"; |
| 238 | memcpy(addr.sun_path, path, sizeof(path) - 1); |
| 239 | int result = connect(socketFd(), reinterpret_cast<struct sockaddr*>(&addr), |
| 240 | sizeof(path) + sizeof(addr.sun_family) - 1); |
| 241 | if (-1 == result) |
| 242 | { |
| 243 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 244 | std::cerr << "Failed to connect to the socket, RC= " << returnCode |
| 245 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 246 | exit(EXIT_FAILURE); |
| 247 | } |
| 248 | |
| 249 | result = write(socketFd(), &MCTP_MSG_TYPE_PLDM, sizeof(MCTP_MSG_TYPE_PLDM)); |
| 250 | if (-1 == result) |
| 251 | { |
| 252 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 253 | std::cerr << "Failed to send message type as pldm to mctp, RC= " |
| 254 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 255 | exit(EXIT_FAILURE); |
| 256 | } |
| 257 | |
Deepak Kodihalli | 23c5204 | 2020-09-01 03:04:32 -0500 | [diff] [blame] | 258 | auto callback = [verbose, &invoker, &dbusImplReq](IO& io, int fd, |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 259 | uint32_t revents) { |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 260 | if (!(revents & EPOLLIN)) |
| 261 | { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | // Outgoing message. |
| 266 | struct iovec iov[2]{}; |
| 267 | |
| 268 | // This structure contains the parameter information for the response |
| 269 | // message. |
| 270 | struct msghdr msg |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 271 | {}; |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 272 | |
| 273 | int returnCode = 0; |
| 274 | ssize_t peekedLength = recv(fd, nullptr, 0, MSG_PEEK | MSG_TRUNC); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 275 | if (0 == peekedLength) |
| 276 | { |
Deepak Kodihalli | 23c5204 | 2020-09-01 03:04:32 -0500 | [diff] [blame] | 277 | // MCTP daemon has closed the socket this daemon is connected to. |
| 278 | // This may or may not be an error scenario, in either case the |
| 279 | // recovery mechanism for this daemon is to restart, and hence exit |
| 280 | // the event loop, that will cause this daemon to exit with a |
| 281 | // failure code. |
| 282 | io.get_event().exit(0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 283 | } |
| 284 | else if (peekedLength <= -1) |
| 285 | { |
| 286 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 287 | std::cerr << "recv system call failed, RC= " << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 288 | } |
| 289 | else |
| 290 | { |
| 291 | std::vector<uint8_t> requestMsg(peekedLength); |
| 292 | auto recvDataLength = recv( |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 293 | fd, static_cast<void*>(requestMsg.data()), peekedLength, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 294 | if (recvDataLength == peekedLength) |
| 295 | { |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 296 | if (verbose) |
| 297 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 298 | std::cout << "Received Msg" << std::endl; |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 299 | printBuffer(requestMsg, verbose); |
Jinu Joy Thomas | 75dd442 | 2019-07-22 12:47:12 +0530 | [diff] [blame] | 300 | } |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 301 | if (MCTP_MSG_TYPE_PLDM != requestMsg[1]) |
| 302 | { |
| 303 | // Skip this message and continue. |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 304 | std::cerr << "Encountered Non-PLDM type message" |
| 305 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 306 | } |
| 307 | else |
| 308 | { |
| 309 | // process message and send response |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 310 | auto response = |
| 311 | processRxMsg(requestMsg, invoker, dbusImplReq); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 312 | if (!response.empty()) |
| 313 | { |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 314 | if (verbose) |
| 315 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 316 | std::cout << "Sending Msg" << std::endl; |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 317 | printBuffer(response, verbose); |
Deepak Kodihalli | 8ffbbe0 | 2019-08-14 06:51:38 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Zahed Hossain | 09a96e0 | 2019-08-06 07:42:37 -0500 | [diff] [blame] | 320 | iov[0].iov_base = &requestMsg[0]; |
| 321 | iov[0].iov_len = |
| 322 | sizeof(requestMsg[0]) + sizeof(requestMsg[1]); |
| 323 | iov[1].iov_base = response.data(); |
| 324 | iov[1].iov_len = response.size(); |
| 325 | |
| 326 | msg.msg_iov = iov; |
| 327 | msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]); |
| 328 | |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 329 | int result = sendmsg(fd, &msg, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 330 | if (-1 == result) |
| 331 | { |
| 332 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 333 | std::cerr << "sendto system call failed, RC= " |
| 334 | << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | else |
| 340 | { |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 341 | std::cerr |
| 342 | << "Failure to read peeked length packet. peekedLength= " |
| 343 | << peekedLength << " recvDataLength=" << recvDataLength |
| 344 | << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 345 | } |
| 346 | } |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 347 | }; |
| 348 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 349 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 350 | bus.request_name("xyz.openbmc_project.PLDM"); |
Deepak Kodihalli | 37998bf | 2019-11-11 04:06:53 -0600 | [diff] [blame] | 351 | IO io(event, socketFd(), EPOLLIN, std::move(callback)); |
| 352 | event.loop(); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 353 | |
| 354 | result = shutdown(sockfd, SHUT_RDWR); |
| 355 | if (-1 == result) |
| 356 | { |
| 357 | returnCode = -errno; |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 358 | std::cerr << "Failed to shutdown the socket, RC=" << returnCode << "\n"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 359 | exit(EXIT_FAILURE); |
| 360 | } |
| 361 | exit(EXIT_FAILURE); |
| 362 | } |