blob: fc2b08d61f2d27c871e293ff95481fe202dba6a5 [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"
Gilbert Chen6c7fed42022-02-22 15:40:17 +00009#include "platform-mc/manager.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070010#include "requester/handler.hpp"
Tom Josephfb3bc062021-08-17 07:48:11 -070011#include "requester/mctp_endpoint_discovery.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070012#include "requester/request.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050013
14#include <err.h>
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +053015#include <getopt.h>
George Liuc453e162022-12-21 17:16:23 +080016#include <libpldm/base.h>
17#include <libpldm/bios.h>
18#include <libpldm/pdr.h>
19#include <libpldm/platform.h>
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +100020#include <libpldm/transport.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050021#include <poll.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050022#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>
Pavithra Barithayab3b84b42024-08-23 11:43:57 +053034#include <cstdlib>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035#include <cstring>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050036#include <fstream>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050037#include <iomanip>
38#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"
Thu Nguyena34a64b2022-03-31 08:56:39 +070051#include "host-bmc/dbus_to_terminus_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
George Liua881c172021-06-21 18:28:11 +080064#include "oem_ibm.hpp"
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050065#endif
66
Thu Nguyen3f5a9692024-07-16 13:16:54 +000067constexpr const char* PLDMService = "xyz.openbmc_project.PLDM";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050068
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050069using namespace pldm;
Deepak Kodihalli37998bf2019-11-11 04:06:53 -060070using namespace sdeventplus;
71using namespace sdeventplus::source;
Tom Joseph02b4ee42021-05-02 22:44:36 -070072using namespace pldm::responder;
73using namespace pldm::utils;
Manojkiran Edaef773052021-07-29 09:29:28 +053074using sdeventplus::source::Signal;
75using namespace pldm::flightrecorder;
76
77void interruptFlightRecorderCallBack(Signal& /*signal*/,
78 const struct signalfd_siginfo*)
79{
Riya Dixit49cfb132023-03-02 04:26:53 -060080 error("Received SIGUR1(10) Signal interrupt");
Manojkiran Edaef773052021-07-29 09:29:28 +053081 // obtain the flight recorder instance and dump the recorder
82 FlightRecorder::GetInstance().playRecorder();
83}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050084
Archana Kakani62dd8ff2024-02-12 10:00:40 -060085void requestPLDMServiceName()
86{
Thu Nguyen3f5a9692024-07-16 13:16:54 +000087 try
88 {
89 auto& bus = pldm::utils::DBusHandler::getBus();
90 bus.request_name(PLDMService);
91 }
92 catch (const sdbusplus::exception_t& e)
93 {
94 error("Failed to request D-Bus name {NAME} with error {ERROR}.", "NAME",
95 PLDMService, "ERROR", e);
96 }
Archana Kakani62dd8ff2024-02-12 10:00:40 -060097}
98
Tom Joseph74f27c72021-05-16 07:58:53 -070099static std::optional<Response>
100 processRxMsg(const std::vector<uint8_t>& requestMsg, Invoker& invoker,
Tom Josephfb3bc062021-08-17 07:48:11 -0700101 requester::Handler<requester::Request>& handler,
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000102 fw_update::Manager* fwManager, pldm_tid_t tid)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500103{
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000104 uint8_t eid = tid;
105
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500106 pldm_header_info hdrFields{};
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000107 auto hdr = reinterpret_cast<const pldm_msg_hdr*>(requestMsg.data());
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500108 if (PLDM_SUCCESS != unpack_pldm_header(hdr, &hdrFields))
109 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600110 error("Empty PLDM request header");
Tom Joseph74f27c72021-05-16 07:58:53 -0700111 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500112 }
George Liub7095ff2021-06-14 16:01:57 +0800113
114 if (PLDM_RESPONSE != hdrFields.msg_type)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500115 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700116 Response response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500117 auto request = reinterpret_cast<const pldm_msg*>(hdr);
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000118 size_t requestLen = requestMsg.size() - sizeof(struct pldm_msg_hdr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600119 try
120 {
Tom Josephfb3bc062021-08-17 07:48:11 -0700121 if (hdrFields.pldm_type != PLDM_FWUP)
122 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400123 response =
124 invoker.handle(tid, hdrFields.pldm_type, hdrFields.command,
125 request, requestLen);
Tom Josephfb3bc062021-08-17 07:48:11 -0700126 }
127 else
128 {
129 response = fwManager->handleRequest(eid, hdrFields.command,
130 request, requestLen);
131 }
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600132 }
133 catch (const std::out_of_range& e)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500134 {
135 uint8_t completion_code = PLDM_ERROR_UNSUPPORTED_PLDM_CMD;
136 response.resize(sizeof(pldm_msg_hdr));
137 auto responseHdr = reinterpret_cast<pldm_msg_hdr*>(response.data());
138 pldm_header_info header{};
139 header.msg_type = PLDM_RESPONSE;
140 header.instance = hdrFields.instance;
141 header.pldm_type = hdrFields.pldm_type;
142 header.command = hdrFields.command;
George Liub7095ff2021-06-14 16:01:57 +0800143 if (PLDM_SUCCESS != pack_pldm_header(&header, responseHdr))
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500144 {
Riya Dixit087a7512024-04-06 14:28:08 -0500145 error(
146 "Failed to add response header for processing Rx, error - {ERROR}",
147 "ERROR", e);
Tom Joseph74f27c72021-05-16 07:58:53 -0700148 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500149 }
150 response.insert(response.end(), completion_code);
151 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700152 return response;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500153 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700154 else if (PLDM_RESPONSE == hdrFields.msg_type)
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600155 {
Tom Joseph74f27c72021-05-16 07:58:53 -0700156 auto response = reinterpret_cast<const pldm_msg*>(hdr);
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000157 size_t responseLen = requestMsg.size() - sizeof(struct pldm_msg_hdr);
Tom Joseph74f27c72021-05-16 07:58:53 -0700158 handler.handleResponse(eid, hdrFields.instance, hdrFields.pldm_type,
159 hdrFields.command, response, responseLen);
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600160 }
Tom Joseph74f27c72021-05-16 07:58:53 -0700161 return std::nullopt;
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500162}
163
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530164void optionUsage(void)
165{
Riya Dixit797f3382023-08-22 22:27:51 -0500166 info("Usage: pldmd [options]");
167 info("Options:");
168 info(" [--verbose] - would enable verbosity");
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530169}
170
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500171int main(int argc, char** argv)
172{
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530173 bool verbose = false;
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500174 static struct option long_options[] = {{"verbose", no_argument, 0, 'v'},
175 {0, 0, 0, 0}};
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530176
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500177 auto argflag = getopt_long(argc, argv, "v", long_options, nullptr);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530178 switch (argflag)
179 {
180 case 'v':
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500181 verbose = true;
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530182 break;
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400183 case -1:
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530184 break;
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400185 default:
Manojkiran Edaf25145f2022-09-10 01:43:39 -0500186 optionUsage();
Brad Bishopc80f3ef2021-10-07 16:34:13 -0400187 exit(EXIT_FAILURE);
Jinu Joy Thomas75dd4422019-07-22 12:47:12 +0530188 }
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000189 // Setup PLDM requester transport
190 auto hostEID = pldm::utils::readHostEID();
191 /* To maintain current behaviour until we have the infrastructure to find
192 * and use the correct TIDs */
193 pldm_tid_t TID = hostEID;
194 PldmTransport pldmTransport{};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500195 auto event = Event::get_default();
Tom Joseph02b4ee42021-05-02 22:44:36 -0700196 auto& bus = pldm::utils::DBusHandler::getBus();
Patrick Williams84b790c2022-07-22 19:26:56 -0500197 sdbusplus::server::manager_t objManager(bus,
198 "/xyz/openbmc_project/software");
Gilbert Cheneac61a42022-02-23 20:56:19 +0000199 sdbusplus::server::manager_t sensorObjManager(
200 bus, "/xyz/openbmc_project/sensors");
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +0930201
202 InstanceIdDb instanceIdDb;
203 dbus_api::Requester dbusImplReq(bus, "/xyz/openbmc_project/pldm",
204 instanceIdDb);
Patrick Williams705fa982023-09-27 02:32:20 -0500205 sdbusplus::server::manager_t inventoryManager(
George Liu682ee182020-12-25 15:24:33 +0800206 bus, "/xyz/openbmc_project/inventory");
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500207
Tom Joseph02b4ee42021-05-02 22:44:36 -0700208 Invoker invoker{};
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000209 requester::Handler<requester::Request> reqHandler(&pldmTransport, event,
210 instanceIdDb, verbose);
Manojkiran Eda9fffea22021-10-27 16:03:27 +0530211
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500212 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
213 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930214 if (!pdrRepo)
215 {
216 throw std::runtime_error("Failed to instantiate PDR repository");
217 }
Thu Nguyena34a64b2022-03-31 08:56:39 +0700218 DBusHandler dbusHandler;
219 std::unique_ptr<pldm::host_effecters::HostEffecterParser>
220 hostEffecterParser =
221 std::make_unique<pldm::host_effecters::HostEffecterParser>(
222 &instanceIdDb, pldmTransport.getEventSource(), pdrRepo.get(),
223 &dbusHandler, HOST_JSONS_DIR, &reqHandler);
224#ifdef LIBPLDMRESPONDER
225 using namespace pldm::state_sensor;
226 dbus_api::Host dbusImplHost(bus, "/xyz/openbmc_project/pldm");
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500227 std::unique_ptr<pldm_entity_association_tree,
228 decltype(&pldm_entity_association_tree_destroy)>
229 entityTree(pldm_entity_association_tree_init(),
230 pldm_entity_association_tree_destroy);
Andrew Jefferyabcd6ca2023-06-30 13:53:35 +0930231 if (!entityTree)
232 {
233 throw std::runtime_error(
234 "Failed to instantiate general PDR entity association tree");
235 }
Sampa Misrac073a202021-05-08 10:56:05 -0500236 std::unique_ptr<pldm_entity_association_tree,
237 decltype(&pldm_entity_association_tree_destroy)>
238 bmcEntityTree(pldm_entity_association_tree_init(),
239 pldm_entity_association_tree_destroy);
Andrew Jefferyabcd6ca2023-06-30 13:53:35 +0930240 if (!bmcEntityTree)
241 {
242 throw std::runtime_error(
243 "Failed to instantiate BMC PDR entity association tree");
244 }
Pavithra Barithaya319ebb32021-05-06 06:09:11 -0500245 std::shared_ptr<HostPDRHandler> hostPDRHandler;
George Liucae18662020-05-15 09:32:57 +0800246 std::unique_ptr<DbusToPLDMEvent> dbusToPLDMEventHandler;
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600247 std::unique_ptr<platform_config::Handler> platformConfigHandler{};
Kamalkumar Patel0a422692024-06-03 00:42:56 -0500248 platformConfigHandler =
249 std::make_unique<platform_config::Handler>(PDR_JSONS_DIR);
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500250
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500251 if (hostEID)
252 {
253 hostPDRHandler = std::make_shared<HostPDRHandler>(
254 pldmTransport.getEventSource(), hostEID, event, pdrRepo.get(),
255 EVENTS_JSONS_DIR, entityTree.get(), bmcEntityTree.get(),
George Liua881c172021-06-21 18:28:11 +0800256 instanceIdDb, &reqHandler);
257
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500258 // HostFirmware interface needs access to hostPDR to know if host
259 // is running
260 dbusImplHost.setHostPdrObj(hostPDRHandler);
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500261
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500262 dbusToPLDMEventHandler = std::make_unique<DbusToPLDMEvent>(
263 pldmTransport.getEventSource(), hostEID, instanceIdDb, &reqHandler);
264 }
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500265
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530266 auto fruHandler = std::make_unique<fru::Handler>(
Manojkiran Eda03b01ca2021-06-29 08:55:09 +0530267 FRU_JSONS_DIR, FRU_MASTER_JSON, pdrRepo.get(), entityTree.get(),
George Liua881c172021-06-21 18:28:11 +0800268 bmcEntityTree.get());
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500269
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530270 // FRU table is built lazily when a FRU command or Get PDR command is
271 // handled. To enable building FRU table, the FRU handler is passed to the
272 // Platform handler.
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000273
274 std::unique_ptr<platform_mc::Manager> platformManager =
275 std::make_unique<platform_mc::Manager>(event, reqHandler, instanceIdDb);
276
277 pldm::responder::platform::EventMap addOnEventHandlers{
Thu Nguyen9fc79122024-09-10 10:15:01 +0000278 {PLDM_CPER_EVENT,
279 {[&platformManager](const pldm_msg* request, size_t payloadLength,
280 uint8_t formatVersion, uint8_t tid,
281 size_t eventDataOffset) {
282 return platformManager->handleCperEvent(
283 request, payloadLength, formatVersion, tid, eventDataOffset);
284 }}},
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000285 {PLDM_SENSOR_EVENT,
286 {[&platformManager](const pldm_msg* request, size_t payloadLength,
287 uint8_t formatVersion, uint8_t tid,
288 size_t eventDataOffset) {
289 return platformManager->handleSensorEvent(
290 request, payloadLength, formatVersion, tid, eventDataOffset);
291 }}}};
292
Sampa Misraaea5dde2020-08-31 08:33:47 -0500293 auto platformHandler = std::make_unique<platform::Handler>(
Sagar Srinivas90314a32023-10-17 10:38:03 -0500294 &dbusHandler, hostEID, &instanceIdDb, PDR_JSONS_DIR, pdrRepo.get(),
295 hostPDRHandler.get(), dbusToPLDMEventHandler.get(), fruHandler.get(),
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000296 platformConfigHandler.get(), &reqHandler, event, true,
297 addOnEventHandlers);
Pavithra Barithayaa410c652021-07-22 01:32:47 -0500298
George Liua881c172021-06-21 18:28:11 +0800299 auto biosHandler = std::make_unique<bios::Handler>(
300 pldmTransport.getEventSource(), hostEID, &instanceIdDb, &reqHandler,
301 platformConfigHandler.get(), requestPLDMServiceName);
302
303 auto baseHandler = std::make_unique<base::Handler>(event);
304
305#ifdef OEM_IBM
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400306 pldm::oem_ibm::OemIBM oemIBM(
307 &dbusHandler, pldmTransport.getEventSource(), hostEID, pdrRepo.get(),
308 instanceIdDb, event, invoker, hostPDRHandler.get(),
309 platformHandler.get(), fruHandler.get(), baseHandler.get(),
310 &reqHandler);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500311#endif
312
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500313 invoker.registerHandler(PLDM_BIOS, std::move(biosHandler));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500314 invoker.registerHandler(PLDM_PLATFORM, std::move(platformHandler));
315 invoker.registerHandler(PLDM_FRU, std::move(fruHandler));
George Liua881c172021-06-21 18:28:11 +0800316 invoker.registerHandler(PLDM_BASE, std::move(baseHandler));
317
Tom Joseph02b4ee42021-05-02 22:44:36 -0700318 dbus_api::Pdr dbusImplPdr(bus, "/xyz/openbmc_project/pldm", pdrRepo.get());
319 sdbusplus::xyz::openbmc_project::PLDM::server::Event dbusImplEvent(
320 bus, "/xyz/openbmc_project/pldm");
Tom Joseph20aa3e02021-08-17 04:44:19 -0700321
Tom Joseph02b4ee42021-05-02 22:44:36 -0700322#endif
Sampa Misraaea5dde2020-08-31 08:33:47 -0500323
Tom Josephfb3bc062021-08-17 07:48:11 -0700324 std::unique_ptr<fw_update::Manager> fwManager =
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930325 std::make_unique<fw_update::Manager>(event, reqHandler, instanceIdDb);
Tom Josephfb3bc062021-08-17 07:48:11 -0700326 std::unique_ptr<MctpDiscovery> mctpDiscoveryHandler =
Gilbert Chen44524a52022-02-14 12:12:25 +0000327 std::make_unique<MctpDiscovery>(
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000328 bus, std::initializer_list<MctpDiscoveryHandlerIntf*>{
329 fwManager.get(), platformManager.get()});
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000330 auto callback = [verbose, &invoker, &reqHandler, &fwManager, &pldmTransport,
331 TID](IO& io, int fd, uint32_t revents) mutable {
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600332 if (!(revents & EPOLLIN))
333 {
334 return;
335 }
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000336 if (fd < 0)
337 {
338 return;
339 }
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600340
341 int returnCode = 0;
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000342 void* requestMsg;
343 size_t recvDataLength;
344 returnCode = pldmTransport.recvMsg(TID, requestMsg, recvDataLength);
345
346 if (returnCode == PLDM_REQUESTER_SUCCESS)
347 {
348 std::vector<uint8_t> requestMsgVec(
349 static_cast<uint8_t*>(requestMsg),
350 static_cast<uint8_t*>(requestMsg) + recvDataLength);
351 FlightRecorder::GetInstance().saveRecord(requestMsgVec, false);
352 if (verbose)
353 {
354 printBuffer(Rx, requestMsgVec);
355 }
356 // process message and send response
357 auto response = processRxMsg(requestMsgVec, invoker, reqHandler,
358 fwManager.get(), TID);
359 if (response.has_value())
360 {
361 FlightRecorder::GetInstance().saveRecord(*response, true);
362 if (verbose)
363 {
364 printBuffer(Tx, *response);
365 }
366
367 returnCode = pldmTransport.sendMsg(TID, (*response).data(),
368 (*response).size());
369 if (returnCode != PLDM_REQUESTER_SUCCESS)
370 {
Riya Dixit087a7512024-04-06 14:28:08 -0500371 warning(
372 "Failed to send pldmTransport message for TID '{TID}', response code '{RETURN_CODE}'",
373 "TID", TID, "RETURN_CODE", returnCode);
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000374 }
375 }
376 }
377 // TODO check that we get here if mctp-demux dies?
378 else if (returnCode == PLDM_REQUESTER_RECV_FAIL)
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500379 {
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500380 // MCTP daemon has closed the socket this daemon is connected to.
381 // This may or may not be an error scenario, in either case the
382 // recovery mechanism for this daemon is to restart, and hence exit
383 // the event loop, that will cause this daemon to exit with a
384 // failure code.
Riya Dixit087a7512024-04-06 14:28:08 -0500385 error(
386 "MCTP daemon closed the socket, IO exiting with response code '{RC}'",
387 "RC", returnCode);
Deepak Kodihalli23c52042020-09-01 03:04:32 -0500388 io.get_event().exit(0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500389 }
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500390 else
391 {
Riya Dixit087a7512024-04-06 14:28:08 -0500392 warning(
393 "Failed to receive PLDM request for pldmTransport, response code '{RETURN_CODE}'",
394 "RETURN_CODE", returnCode);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500395 }
Thu Nguyenefacb6f2024-04-12 01:37:46 +0000396 /* Free requestMsg after using */
397 free(requestMsg);
Deepak Kodihalli37998bf2019-11-11 04:06:53 -0600398 };
399
Deepak Kodihalli4de4d002019-11-11 02:41:43 -0600400 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Archana Kakani46f352e2024-03-17 08:21:08 -0500401#ifndef SYSTEM_SPECIFIC_BIOS_JSON
Thu Nguyen3f5a9692024-07-16 13:16:54 +0000402 try
403 {
404 bus.request_name(PLDMService);
405 }
406 catch (const sdbusplus::exception_t& e)
407 {
408 error("Failed to request D-Bus name {NAME} with error {ERROR}.", "NAME",
409 PLDMService, "ERROR", e);
410 }
Archana Kakani46f352e2024-03-17 08:21:08 -0500411#endif
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000412 IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback));
Manojkiran Eda88bc7182021-09-08 18:37:30 +0530413#ifdef LIBPLDMRESPONDER
414 if (hostPDRHandler)
415 {
416 hostPDRHandler->setHostFirmwareCondition();
417 }
418#endif
Manojkiran Edaef773052021-07-29 09:29:28 +0530419 stdplus::signal::block(SIGUSR1);
420 sdeventplus::source::Signal sigUsr1(
421 event, SIGUSR1, std::bind_front(&interruptFlightRecorderCallBack));
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +1000422 int returnCode = event.loop();
Brad Bishop3a22b972021-10-07 17:01:58 -0400423 if (returnCode)
424 {
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500425 exit(EXIT_FAILURE);
426 }
Brad Bishop3a22b972021-10-07 17:01:58 -0400427
428 exit(EXIT_SUCCESS);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500429}