blob: 13488868bd17c6e9494a3d2d411e171e5f40d981 [file] [log] [blame]
Ramesh Iyyar3af5c322020-12-04 00:38:42 -06001// SPDX-License-Identifier: Apache-2.0
2
3#include "pldm_utils.hpp"
4
5#include "dump_utils.hpp"
6#include "xyz/openbmc_project/Common/error.hpp"
7
George Liu858fbb22021-07-01 12:25:44 +08008#include <fmt/core.h>
9
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060010#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/log.hpp>
12
13namespace phosphor
14{
15namespace dump
16{
17namespace pldm
18{
19
20using namespace phosphor::logging;
21using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
22using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
23
24int openPLDM()
25{
26 auto fd = pldm_open();
27 if (fd < 0)
28 {
29 auto e = errno;
George Liu858fbb22021-07-01 12:25:44 +080030 log<level::ERR>(
31 fmt::format("pldm_open failed, errno({}), FD({})", e, fd).c_str());
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060032 elog<NotAllowed>(Reason("Required host dump action via pldm is not "
33 "allowed due to pldm_open failed"));
34 }
35 return fd;
36}
37
38uint8_t getPLDMInstanceID(uint8_t eid)
39{
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060040 constexpr auto pldmRequester = "xyz.openbmc_project.PLDM.Requester";
41 constexpr auto pldm = "/xyz/openbmc_project/pldm";
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060042 uint8_t instanceID = 0;
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060043
Dhruvaraj Subhashchandran9126ec02022-10-12 04:59:55 -050044 try
45 {
46 auto bus = sdbusplus::bus::new_default();
47 auto service = phosphor::dump::getService(bus, pldm, pldmRequester);
48
49 auto method = bus.new_method_call(service.c_str(), pldm, pldmRequester,
50 "GetInstanceId");
51 method.append(eid);
52 auto reply = bus.call(method);
53
54 reply.read(instanceID);
55
56 log<level::INFO>(
57 fmt::format("Got instanceId({}) from PLDM eid({})", instanceID, eid)
58 .c_str());
59 }
60 catch (const sdbusplus::exception::SdBusError& e)
61 {
62 log<level::ERR>(
63 fmt::format("Failed to get instance id error({})", e.what())
64 .c_str());
65 elog<NotAllowed>(Reason("Failure in communicating with pldm service, "
66 "service may not be running"));
67 }
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060068 return instanceID;
69}
70
71} // namespace pldm
72} // namespace dump
73} // namespace phosphor