1 // 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 
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <phosphor-logging/lg2.hpp>
10 
11 namespace phosphor
12 {
13 namespace dump
14 {
15 namespace pldm
16 {
17 
18 using namespace phosphor::logging;
19 using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
20 using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
21 
openPLDM()22 int openPLDM()
23 {
24     auto fd = pldm_open();
25     if (fd < 0)
26     {
27         auto e = errno;
28         lg2::error("pldm_open failed, errno: {ERRNO}, FD: FD", "ERRNO", e, "FD",
29                    fd);
30         elog<NotAllowed>(Reason(
31             "Required host dump action via pldm is not allowed due "
32             "to pldm_open failed"));
33     }
34     return fd;
35 }
36 
getPLDMInstanceID(uint8_t eid)37 uint8_t getPLDMInstanceID(uint8_t eid)
38 {
39     constexpr auto pldmRequester = "xyz.openbmc_project.PLDM.Requester";
40     constexpr auto pldm = "/xyz/openbmc_project/pldm";
41     uint8_t instanceID = 0;
42 
43     try
44     {
45         auto bus = sdbusplus::bus::new_default();
46         auto service = phosphor::dump::getService(bus, pldm, pldmRequester);
47 
48         auto method = bus.new_method_call(service.c_str(), pldm, pldmRequester,
49                                           "GetInstanceId");
50         method.append(eid);
51         auto reply = bus.call(method);
52 
53         reply.read(instanceID);
54 
55         lg2::info("Got instanceId: {INSTANCE_ID} from PLDM eid: {EID}",
56                   "INSTANCE_ID", instanceID, "EID", eid);
57     }
58     catch (const sdbusplus::exception::SdBusError& e)
59     {
60         lg2::error("Failed to get instance id error: {ERROR}", "ERROR", e);
61         elog<NotAllowed>(Reason("Failure in communicating with pldm service, "
62                                 "service may not be running"));
63     }
64     return instanceID;
65 }
66 
67 } // namespace pldm
68 } // namespace dump
69 } // namespace phosphor
70