blob: 2d11bdeb347560f23b961f87da2eb503f7766971 [file] [log] [blame]
Zane Shelleyf36466f2022-02-11 15:10:55 -06001#include <attn/attn_common.hpp>
2#include <attn/attn_dbus.hpp>
3#include <attn/attn_logging.hpp>
Ben Tyner42260bc2022-02-16 14:36:58 -06004#include <util/dbus.hpp>
austinfcuibfa831a2022-01-26 15:37:07 -06005#include <util/trace.hpp>
Ben Tyner188f1092021-02-01 09:33:06 -06006
7#include <string>
8#include <vector>
9
10namespace attn
11{
12
Ben Tyner5c5db652021-02-22 18:22:35 -060013/** @brief Create a dbus method */
Ben Tyner188f1092021-02-01 09:33:06 -060014int dbusMethod(const std::string& i_path, const std::string& i_interface,
15 const std::string& i_function,
Ben Tynera0982102022-02-16 14:19:41 -060016 sdbusplus::message::message& o_method)
Ben Tyner188f1092021-02-01 09:33:06 -060017{
Ben Tyner4bbcb382021-02-22 09:29:00 -060018 int rc = RC_DBUS_ERROR; // assume error
Ben Tyner188f1092021-02-01 09:33:06 -060019
20 try
21 {
Ben Tyner188f1092021-02-01 09:33:06 -060022 auto bus = sdbusplus::bus::new_system(); // using system dbus
23
Ben Tyner42260bc2022-02-16 14:36:58 -060024 // we need to find the service implementing the interface
25 util::dbus::DBusService service;
Ben Tyner188f1092021-02-01 09:33:06 -060026
Ben Tyner42260bc2022-02-16 14:36:58 -060027 if (0 == util::dbus::findService(i_interface, i_path, service))
Ben Tyner188f1092021-02-01 09:33:06 -060028 {
Ben Tynera0982102022-02-16 14:19:41 -060029 // return the method
30 o_method =
31 bus.new_method_call(service.c_str(), i_path.c_str(),
32 i_interface.c_str(), i_function.c_str());
Ben Tyner42260bc2022-02-16 14:36:58 -060033
Ben Tyner4bbcb382021-02-22 09:29:00 -060034 rc = RC_SUCCESS;
Ben Tyner188f1092021-02-01 09:33:06 -060035 }
36 else
37 {
Ben Tyner6764d702021-02-12 09:17:23 -060038 // This trace will be picked up in event log
austinfcuibfa831a2022-01-26 15:37:07 -060039 trace::inf("dbusMethod service not found");
40 trace::inf(i_path.c_str());
41 trace::inf(i_interface.c_str());
Ben Tyner188f1092021-02-01 09:33:06 -060042 }
43 }
44 catch (const sdbusplus::exception::SdBusError& e)
45 {
austinfcuibfa831a2022-01-26 15:37:07 -060046 trace::err("dbusMethod exception");
47 trace::err(e.what());
Ben Tyner188f1092021-02-01 09:33:06 -060048 }
49
50 return rc;
51}
52
Ben Tyner188f1092021-02-01 09:33:06 -060053/** @brief Create a PEL from raw PEL data */
54void createPelRaw(const std::vector<uint8_t>& i_buffer)
55{
56 // Create FFDC file from buffer data
57 util::FFDCFile pelFile{util::FFDCFormat::Text};
58 auto fd = pelFile.getFileDescriptor();
Ben Tyner188f1092021-02-01 09:33:06 -060059
60 auto filePath = pelFile.getPath(); // path to ffdc file
61
Ben Tynerd7006092021-02-05 14:55:52 -060062 size_t numBytes = write(fd, i_buffer.data(), i_buffer.size());
63 if (i_buffer.size() != numBytes)
64 {
austinfcuibfa831a2022-01-26 15:37:07 -060065 trace::err("%s only %u of %u bytes written", filePath.c_str(), numBytes,
66 i_buffer.size());
Ben Tynerd7006092021-02-05 14:55:52 -060067 }
68
69 lseek(fd, 0, SEEK_SET);
70
Ben Tyner188f1092021-02-01 09:33:06 -060071 // Additional data for log
72 std::map<std::string, std::string> additional;
73 additional.emplace("RAWPEL", filePath.string());
74 additional.emplace("_PID", std::to_string(getpid()));
75
76 // dbus specifics
77 constexpr auto interface = "xyz.openbmc_project.Logging.Create";
78 constexpr auto function = "Create";
79
80 sdbusplus::message::message method;
81
82 if (0 == dbusMethod(pathLogging, interface, function, method))
83 {
84 try
85 {
86 // append additional dbus call parameters
87 method.append(eventPelTerminate, levelPelError, additional);
88
89 // using system dbus, no reply
90 auto bus = sdbusplus::bus::new_system();
91 bus.call_noreply(method);
92 }
93 catch (const sdbusplus::exception::SdBusError& e)
94 {
austinfcuibfa831a2022-01-26 15:37:07 -060095 trace::err("createPelRaw exception");
96 trace::err(e.what());
Ben Tyner188f1092021-02-01 09:33:06 -060097 }
98 }
99}
100
101/** @brief Get file descriptor of exisitng PEL */
102int getPel(const uint32_t i_pelId)
103{
104 // GetPEL returns file descriptor (int)
105 int fd = -1;
106
107 // dbus specifics
108 constexpr auto interface = "org.open_power.Logging.PEL";
109 constexpr auto function = "GetPEL";
110
111 sdbusplus::message::message method;
112
113 if (0 == dbusMethod(pathLogging, interface, function, method))
114 {
115 try
116 {
117 // additional dbus call parameters
118 method.append(i_pelId);
119
120 // using system dbus
121 auto bus = sdbusplus::bus::new_system();
122 auto response = bus.call(method);
123
124 // reply will be a unix file descriptor
125 sdbusplus::message::unix_fd reply;
126
Ben Tyner4bbcb382021-02-22 09:29:00 -0600127 // parse dbus response into reply
Ben Tyner188f1092021-02-01 09:33:06 -0600128 response.read(reply);
129
130 fd = dup(reply); // need to copy (dup) the file descriptor
131 }
132 catch (const sdbusplus::exception::SdBusError& e)
133 {
austinfcuibfa831a2022-01-26 15:37:07 -0600134 trace::err("getPel exception");
135 trace::err(e.what());
Ben Tyner188f1092021-02-01 09:33:06 -0600136 }
137 }
138
139 return fd; // file descriptor or -1
140}
141
Ben Tyner188f1092021-02-01 09:33:06 -0600142} // namespace attn