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