blob: e2ca648a8b0742f6eaacc00207187623cc72201a [file] [log] [blame]
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +10001
Manojkiran Edaef773052021-07-29 09:29:28 +05302#include "common/flight_recorder.hpp"
Andrew Jeffery2abbce72023-10-18 10:17:35 +10303#include "common/instance_id.hpp"
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +10004#include "common/transport.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05005#include "common/utils.hpp"
Deepak Kodihalli4de4d002019-11-11 02:41:43 -06006#include "dbus_impl_requester.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -07007#include "fw-update/manager.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06008#include "invoker.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07009#include "requester/handler.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -070010#include "requester/mctp_endpoint_discovery.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070011#include "requester/request.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050012
13#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053014#include <getopt.h>
George Liuc453e162022-12-21 17:16:23 +080015#include <libpldm/base.h>
16#include <libpldm/bios.h>
17#include <libpldm/pdr.h>
18#include <libpldm/platform.h>
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100019#include <libpldm/transport.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050020#include <poll.h>
21#include <stdlib.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24#include <sys/un.h>
25#include <unistd.h>
26
Riya Dixit49cfb132023-03-02 04:26:53 -060027#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +080028#include <sdeventplus/event.hpp>
29#include <sdeventplus/source/io.hpp>
Manojkiran Edaef773052021-07-29 09:29:28 +053030#include <sdeventplus/source/signal.hpp>
31#include <stdplus/signal.hpp>
George Liu6492f522020-06-16 10:34:05 +080032
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050033#include <cstdio>
34#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050035#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050036#include <iomanip>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053037#include <iostream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050038#include <iterator>
Deepak Kodihallic682fe22020-03-04 00:42:54 -060039#include <memory>
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100040#include <ranges>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050041#include <sstream>
Deepak Kodihallibc669f12019-11-28 08:52:07 -060042#include <stdexcept>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053043#include <string>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050044#include <vector>
45
Riya Dixit49cfb132023-03-02 04:26:53 -060046PHOSPHOR_LOG2_USING;
47
Tom Joseph02b4ee42021-05-02 22:44:36 -070048#ifdef LIBPLDMRESPONDER
49#include "dbus_impl_pdr.hpp"
50#include "host-bmc/dbus_to_event_handler.hpp"
51#include "host-bmc/dbus_to_host_effecters.hpp"
Tom Joseph20aa3e02021-08-17 04:44:19 -070052#include "host-bmc/host_condition.hpp"
Tom Joseph02b4ee42021-05-02 22:44:36 -070053#include "host-bmc/host_pdr_handler.hpp"
54#include "libpldmresponder/base.hpp"
55#include "libpldmresponder/bios.hpp"
56#include "libpldmresponder/fru.hpp"
57#include "libpldmresponder/oem_handler.hpp"
58#include "libpldmresponder/platform.hpp"
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060059#include "libpldmresponder/platform_config.hpp"
Tom Joseph02b4ee42021-05-02 22:44:36 -070060#include "xyz/openbmc_project/PLDM/Event/server.hpp"
61#endif
62
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050063#ifdef OEM_IBM
64#include "libpldmresponder/file_io.hpp"
Pavithra Barithayaa410c652021-07-22 01:32:47 -050065#include "libpldmresponder/fru_oem_ibm.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050066#include "libpldmresponder/oem_ibm_handler.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050067#endif
68
69constexpr uint8_t MCTP_MSG_TYPE_PLDM = 1;
70
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050071using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060072using namespace sdeventplus;
73using namespace sdeventplus::source;
Tom Joseph02b4ee42021-05-02 22:44:36 -070074using namespace pldm::responder;
75using namespace pldm::utils;
Manojkiran Edaef773052021-07-29 09:29:28 +053076using sdeventplus::source::Signal;
77using namespace pldm::flightrecorder;
78
79void interruptFlightRecorderCallBack(Signal& /*signal*/,
80 const struct signalfd_siginfo*)
81{
Riya Dixit49cfb132023-03-02 04:26:53 -060082 error("Received SIGUR1(10) Signal interrupt");
Manojkiran Edaef773052021-07-29 09:29:28 +053083 // obtain the flight recorder instance and dump the recorder
84 FlightRecorder::GetInstance().playRecorder();
85}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050086
Tom Joseph74f27c72021-05-16 07:58:53 -070087static std::optional<Response>
88 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker,
Tom Josephfb3bc062021-08-17 07:48:11 -070089 requester::Handler<requester::Request>& handler,
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100090 fw_update::Manager* fwManager, pldm_tid_t tid)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050091{
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100092 uint8_t eid = tid;
93
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050094 pldm_header_info hdrFields{};
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100095 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(requestMsg.data());
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050096 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
97 {
Riya Dixit49cfb132023-03-02 04:26:53 -060098 error("Empty PLDM request header");
Tom Joseph74f27c72021-05-16 07:58:53 -070099 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500100 }
George Liub7095ff2021-06-14 16:01:57 +0800101
102 if (PLDM_RESPONSE != hdrFields.msg_type)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500103 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700104 Response response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500105 auto request = reinterpret_cast<const pldm_msg*>(hdr);
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000106 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600107 try
108 {
Tom Josephfb3bc062021-08-17 07:48:11 -0700109 if (hdrFields.pldm_type != PLDM_FWUP)
110 {
Delphine CC Chiud2e48992023-12-05 16:29:51 +0800111 response = invoker.handle(tid, hdrFields.pldm_type,
Patrick Williams6da4f912023-05-10 07:50:53 -0500112 hdrFields.command, request,
113 requestLen);
Tom Josephfb3bc062021-08-17 07:48:11 -0700114 }
115 else
116 {
117 response = fwManager->handleRequest(eid, hdrFields.command,
118 request, requestLen);
119 }
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600120 }
121 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500122 {
123 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
124 response.resize(sizeof(pldm_msg_hdr));
125 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
126 pldm_header_info header{};
127 header.msg_type = PLDM_RESPONSE;
128 header.instance = hdrFields.instance;
129 header.pldm_type = hdrFields.pldm_type;
130 header.command = hdrFields.command;
George Liub7095ff2021-06-14 16:01:57 +0800131 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500132 {
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500133 error("Failed adding response header: {ERROR}", "ERROR", e);
Tom Joseph74f27c72021-05-16 07:58:53 -0700134 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500135 }
136 response.insert(response.end(), completion_code);
137 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700138 return response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500139 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700140 else if (PLDM_RESPONSE == hdrFields.msg_type)
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600141 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700142 auto response = reinterpret_cast<const pldm_msg*>(hdr);
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000143 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr);
Tom Joseph74f27c72021-05-16 07:58:53 -0700144 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type,
145 hdrFields.command, response, responseLen);
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600146 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700147 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500148}
149
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530150void optionUsage(void)
151{
Riya Dixit797f3382023-08-22 22:27:51 -0500152 info("Usage: pldmd [options]");
153 info("Options:");
154 info(" [--verbose] - would enable verbosity");
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530155}
156
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500157int main(int argc, char** argv)
158{
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530159 bool verbose = false;
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500160 static struct option long_options[] = {{"verbose", no_argument, 0, 'v'},
161 {0, 0, 0, 0}};
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530162
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500163 auto argflag = getopt_long(argc, argv, "v", long_options, nullptr);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530164 switch (argflag)
165 {
166 case 'v':
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500167 verbose = true;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530168 break;
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400169 case -1:
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530170 break;
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400171 default:
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500172 optionUsage();
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400173 exit(EXIT_FAILURE);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530174 }
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000175 // Setup PLDM requester transport
176 auto hostEID = pldm::utils::readHostEID();
177 /* To maintain current behaviour until we have the infrastructure to find
178 * and use the correct TIDs */
179 pldm_tid_t TID = hostEID;
180 PldmTransport pldmTransport{};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500181 auto event = Event::get_default();
Tom Joseph02b4ee42021-05-02 22:44:36 -0700182 auto& bus = pldm::utils::DBusHandler::getBus();
Patrick Williams84b790c2022-07-22 19:26:56 -0500183 sdbusplus::server::manager_t objManager(bus,
184 "/xyz/openbmc_project/software");
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930185
186 InstanceIdDb instanceIdDb;
187 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm",
188 instanceIdDb);
Patrick Williams705fa982023-09-27 02:32:20 -0500189 sdbusplus::server::manager_t inventoryManager(
George Liu682ee182020-12-25 15:24:33 +0800190 bus, "/xyz/openbmc_project/inventory");
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500191
Tom Joseph02b4ee42021-05-02 22:44:36 -0700192 Invoker invoker{};
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000193 requester::Handler<requester::Request> reqHandler(&pldmTransport, event,
194 instanceIdDb, verbose);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530195
Tom Joseph02b4ee42021-05-02 22:44:36 -0700196#ifdef LIBPLDMRESPONDER
197 using namespace pldm::state_sensor;
Tom Joseph20aa3e02021-08-17 04:44:19 -0700198 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500199 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
200 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930201 if (!pdrRepo)
202 {
203 throw std::runtime_error("Failed to instantiate PDR repository");
204 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500205 std::unique_ptr<pldm_entity_association_tree,
206 decltype(&pldm_entity_association_tree_destroy)>
207 entityTree(pldm_entity_association_tree_init(),
208 pldm_entity_association_tree_destroy);
Andrew Jefferyabcd6ca2023-06-30 13:53:35 +0930209 if (!entityTree)
210 {
211 throw std::runtime_error(
212 "Failed to instantiate general PDR entity association tree");
213 }
Sampa Misrac073a202021-05-08 10:56:05 -0500214 std::unique_ptr<pldm_entity_association_tree,
215 decltype(&pldm_entity_association_tree_destroy)>
216 bmcEntityTree(pldm_entity_association_tree_init(),
217 pldm_entity_association_tree_destroy);
Andrew Jefferyabcd6ca2023-06-30 13:53:35 +0930218 if (!bmcEntityTree)
219 {
220 throw std::runtime_error(
221 "Failed to instantiate BMC PDR entity association tree");
222 }
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500223 std::shared_ptr<HostPDRHandler> hostPDRHandler;
Sampa Misrac0c60542020-07-01 02:34:25 -0500224 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
225 hostEffecterParser;
George Liucae18662020-05-15 09:32:57 +0800226 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400227 DBusHandler dbusHandler;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500228 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{};
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600229 std::unique_ptr<platform_config::Handler> platformConfigHandler{};
230 platformConfigHandler = std::make_unique<platform_config::Handler>();
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500231 std::unique_ptr<oem_fru::Handler> oemFruHandler{};
232
Sampa Misraaea5dde2020-08-31 08:33:47 -0500233#ifdef OEM_IBM
234 std::unique_ptr<pldm::responder::CodeUpdate> codeUpdate =
Brad Bishopfc0d14d2021-10-12 19:34:20 -0400235 std::make_unique<pldm::responder::CodeUpdate>(&dbusHandler);
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500236 codeUpdate->clearDirPath(LID_STAGING_DIR);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500237 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>(
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000238 &dbusHandler, codeUpdate.get(), pldmTransport.getEventSource(), hostEID,
239 instanceIdDb, event, &reqHandler);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500240 codeUpdate->setOemPlatformHandler(oemPlatformHandler.get());
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500241 oemFruHandler = std::make_unique<oem_ibm_fru::Handler>(pdrRepo.get());
Sampa Misrac0c79482021-06-02 08:01:54 -0500242 invoker.registerHandler(PLDM_OEM, std::make_unique<oem_ibm::Handler>(
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000243 oemPlatformHandler.get(),
244 pldmTransport.getEventSource(),
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930245 hostEID, &instanceIdDb, &reqHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500246#endif
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500247 if (hostEID)
248 {
249 hostPDRHandler = std::make_shared<HostPDRHandler>(
250 pldmTransport.getEventSource(), hostEID, event, pdrRepo.get(),
251 EVENTS_JSONS_DIR, entityTree.get(), bmcEntityTree.get(),
252 instanceIdDb, &reqHandler, oemPlatformHandler.get());
253 // HostFirmware interface needs access to hostPDR to know if host
254 // is running
255 dbusImplHost.setHostPdrObj(hostPDRHandler);
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500256
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500257 hostEffecterParser =
258 std::make_unique<pldm::host_effecters::HostEffecterParser>(
259 &instanceIdDb, pldmTransport.getEventSource(), pdrRepo.get(),
260 &dbusHandler, HOST_JSONS_DIR, &reqHandler);
261 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>(
262 pldmTransport.getEventSource(), hostEID, instanceIdDb, &reqHandler);
263 }
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500264 auto biosHandler = std::make_unique<bios::Handler>(
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000265 pldmTransport.getEventSource(), hostEID, &instanceIdDb, &reqHandler,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600266 platformConfigHandler.get());
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500267
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530268 auto fruHandler = std::make_unique<fru::Handler>(
Manojkiran Eda03b01ca2021-06-29 08:55:09 +0530269 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(),
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500270 bmcEntityTree.get(), oemFruHandler.get());
271
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530272 // FRU table is built lazily when a FRU command or Get PDR command is
273 // handled. To enable building FRU table, the FRU handler is passed to the
274 // Platform handler.
Sampa Misraaea5dde2020-08-31 08:33:47 -0500275 auto platformHandler = std::make_unique<platform::Handler>(
Sagar Srinivas90314a32023-10-17 10:38:03 -0500276 &dbusHandler, hostEID, &instanceIdDb, PDR_JSONS_DIR, pdrRepo.get(),
277 hostPDRHandler.get(), dbusToPLDMEventHandler.get(), fruHandler.get(),
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600278 oemPlatformHandler.get(), platformConfigHandler.get(), &reqHandler,
279 event, true);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500280#ifdef OEM_IBM
Sampa Misraaea5dde2020-08-31 08:33:47 -0500281 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
282 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
283 oemPlatformHandler.get());
284 oemIbmPlatformHandler->setPlatformHandler(platformHandler.get());
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500285
286 pldm::responder::oem_ibm_fru::Handler* oemIbmFruHandler =
287 dynamic_cast<pldm::responder::oem_ibm_fru::Handler*>(
288 oemFruHandler.get());
289 oemIbmFruHandler->setIBMFruHandler(fruHandler.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500290#endif
291
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500292 invoker.registerHandler(PLDM_BIOS, std::move(biosHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500293 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
Sagar Srinivas90314a32023-10-17 10:38:03 -0500294 invoker.registerHandler(PLDM_BASE, std::make_unique<base::Handler>(
295 event, oemPlatformHandler.get()));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500296 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
Tom Joseph02b4ee42021-05-02 22:44:36 -0700297 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
298 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
299 bus, "/xyz/openbmc_project/pldm");
Tom Joseph20aa3e02021-08-17 04:44:19 -0700300
Tom Joseph02b4ee42021-05-02 22:44:36 -0700301#endif
Sampa Misraaea5dde2020-08-31 08:33:47 -0500302
Tom Josephfb3bc062021-08-17 07:48:11 -0700303 std::unique_ptr<fw_update::Manager> fwManager =
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930304 std::make_unique<fw_update::Manager>(event, reqHandler, instanceIdDb);
Tom Josephfb3bc062021-08-17 07:48:11 -0700305 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler =
306 std::make_unique<MctpDiscovery>(bus, fwManager.get());
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000307 auto callback = [verbose, &invoker, &reqHandler, &fwManager, &pldmTransport,
308 TID](IO& io, int fd, uint32_t revents) mutable {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600309 if (!(revents & EPOLLIN))
310 {
311 return;
312 }
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000313 if (fd < 0)
314 {
315 return;
316 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600317
318 int returnCode = 0;
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000319 void* requestMsg;
320 size_t recvDataLength;
321 returnCode = pldmTransport.recvMsg(TID, requestMsg, recvDataLength);
322
323 if (returnCode == PLDM_REQUESTER_SUCCESS)
324 {
325 std::vector<uint8_t> requestMsgVec(
326 static_cast<uint8_t*>(requestMsg),
327 static_cast<uint8_t*>(requestMsg) + recvDataLength);
328 FlightRecorder::GetInstance().saveRecord(requestMsgVec, false);
329 if (verbose)
330 {
331 printBuffer(Rx, requestMsgVec);
332 }
333 // process message and send response
334 auto response = processRxMsg(requestMsgVec, invoker, reqHandler,
335 fwManager.get(), TID);
336 if (response.has_value())
337 {
338 FlightRecorder::GetInstance().saveRecord(*response, true);
339 if (verbose)
340 {
341 printBuffer(Tx, *response);
342 }
343
344 returnCode = pldmTransport.sendMsg(TID, (*response).data(),
345 (*response).size());
346 if (returnCode != PLDM_REQUESTER_SUCCESS)
347 {
348 warning("Failed to send PLDM response: {RETURN_CODE}",
349 "RETURN_CODE", returnCode);
350 }
351 }
352 }
353 // TODO check that we get here if mctp-demux dies?
354 else if (returnCode == PLDM_REQUESTER_RECV_FAIL)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500355 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500356 // MCTP daemon has closed the socket this daemon is connected to.
357 // This may or may not be an error scenario, in either case the
358 // recovery mechanism for this daemon is to restart, and hence exit
359 // the event loop, that will cause this daemon to exit with a
360 // failure code.
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000361 error("io exiting");
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500362 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500363 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500364 else
365 {
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000366 warning("Failed to receive PLDM request: {RETURN_CODE}",
367 "RETURN_CODE", returnCode);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500368 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600369 };
370
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600371 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
372 bus.request_name("xyz.openbmc_project.PLDM");
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000373 IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback));
Manojkiran Eda88bc7182021-09-08 18:37:30 +0530374#ifdef LIBPLDMRESPONDER
375 if (hostPDRHandler)
376 {
377 hostPDRHandler->setHostFirmwareCondition();
378 }
379#endif
Manojkiran Edaef773052021-07-29 09:29:28 +0530380 stdplus::signal::block(SIGUSR1);
381 sdeventplus::source::Signal sigUsr1(
382 event, SIGUSR1, std::bind_front(&interruptFlightRecorderCallBack));
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000383 int returnCode = event.loop();
Brad Bishop3a22b972021-10-07 17:01:58 -0400384 if (returnCode)
385 {
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500386 exit(EXIT_FAILURE);
387 }
Brad Bishop3a22b972021-10-07 17:01:58 -0400388
389 exit(EXIT_SUCCESS);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500390}