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