blob: 868289492ea5472f5ef3399e9c659abc5df0042c [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
8#include <phosphor-logging/elog-errors.hpp>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -05009#include <phosphor-logging/lg2.hpp>
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060010
11namespace phosphor
12{
13namespace dump
14{
15namespace pldm
16{
17
18using namespace phosphor::logging;
19using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
20using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
21
22int openPLDM()
23{
24 auto fd = pldm_open();
25 if (fd < 0)
26 {
27 auto e = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050028 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"));
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060035 }
36 return fd;
37}
38
39uint8_t getPLDMInstanceID(uint8_t eid)
40{
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060041 constexpr auto pldmRequester = "xyz.openbmc_project.PLDM.Requester";
42 constexpr auto pldm = "/xyz/openbmc_project/pldm";
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060043 uint8_t instanceID = 0;
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060044
Dhruvaraj Subhashchandran9126ec02022-10-12 04:59:55 -050045 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
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050057 lg2::info("Got instanceId: {INSTANCE_ID} from PLDM eid: {EID}",
58 "INSTANCE_ID", instanceID, "EID", eid);
Dhruvaraj Subhashchandran9126ec02022-10-12 04:59:55 -050059 }
60 catch (const sdbusplus::exception::SdBusError& e)
61 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050062 lg2::error("Failed to get instance id error: {ERROR}", "ERROR", e);
Dhruvaraj Subhashchandran9126ec02022-10-12 04:59:55 -050063 elog<NotAllowed>(Reason("Failure in communicating with pldm service, "
64 "service may not be running"));
65 }
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060066 return instanceID;
67}
68
69} // namespace pldm
70} // namespace dump
71} // namespace phosphor