blob: 7613777997961ba49200cdf1304050a21a1e0a30 [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,
Patrick Williamse212fb02022-07-22 19:26:57 -050015 const std::string& i_function, sdbusplus::message_t& o_method)
Ben Tyner188f1092021-02-01 09:33:06 -060016{
Ben Tyner4bbcb382021-02-22 09:29:00 -060017 int rc = RC_DBUS_ERROR; // assume error
Ben Tyner188f1092021-02-01 09:33:06 -060018
19 try
20 {
Ben Tyner188f1092021-02-01 09:33:06 -060021 auto bus = sdbusplus::bus::new_system(); // using system dbus
22
Ben Tyner42260bc2022-02-16 14:36:58 -060023 // we need to find the service implementing the interface
24 util::dbus::DBusService service;
Ben Tyner188f1092021-02-01 09:33:06 -060025
Ben Tyner42260bc2022-02-16 14:36:58 -060026 if (0 == util::dbus::findService(i_interface, i_path, service))
Ben Tyner188f1092021-02-01 09:33:06 -060027 {
Ben Tynera0982102022-02-16 14:19:41 -060028 // return the method
29 o_method =
30 bus.new_method_call(service.c_str(), i_path.c_str(),
31 i_interface.c_str(), i_function.c_str());
Ben Tyner42260bc2022-02-16 14:36:58 -060032
Ben Tyner4bbcb382021-02-22 09:29:00 -060033 rc = RC_SUCCESS;
Ben Tyner188f1092021-02-01 09:33:06 -060034 }
35 else
36 {
Ben Tyner6764d702021-02-12 09:17:23 -060037 // This trace will be picked up in event log
austinfcuibfa831a2022-01-26 15:37:07 -060038 trace::inf("dbusMethod service not found");
39 trace::inf(i_path.c_str());
40 trace::inf(i_interface.c_str());
Ben Tyner188f1092021-02-01 09:33:06 -060041 }
42 }
43 catch (const sdbusplus::exception::SdBusError& e)
44 {
austinfcuibfa831a2022-01-26 15:37:07 -060045 trace::err("dbusMethod exception");
46 trace::err(e.what());
Ben Tyner188f1092021-02-01 09:33:06 -060047 }
48
49 return rc;
50}
51
Ben Tyner188f1092021-02-01 09:33:06 -060052/** @brief Create a PEL from raw PEL data */
53void createPelRaw(const std::vector<uint8_t>& i_buffer)
54{
55 // Create FFDC file from buffer data
56 util::FFDCFile pelFile{util::FFDCFormat::Text};
57 auto fd = pelFile.getFileDescriptor();
Ben Tyner188f1092021-02-01 09:33:06 -060058
59 auto filePath = pelFile.getPath(); // path to ffdc file
60
Ben Tynerd7006092021-02-05 14:55:52 -060061 size_t numBytes = write(fd, i_buffer.data(), i_buffer.size());
62 if (i_buffer.size() != numBytes)
63 {
austinfcuibfa831a2022-01-26 15:37:07 -060064 trace::err("%s only %u of %u bytes written", filePath.c_str(), numBytes,
65 i_buffer.size());
Ben Tynerd7006092021-02-05 14:55:52 -060066 }
67
68 lseek(fd, 0, SEEK_SET);
69
Ben Tyner188f1092021-02-01 09:33:06 -060070 // Additional data for log
71 std::map<std::string, std::string> additional;
72 additional.emplace("RAWPEL", filePath.string());
73 additional.emplace("_PID", std::to_string(getpid()));
74
75 // dbus specifics
76 constexpr auto interface = "xyz.openbmc_project.Logging.Create";
77 constexpr auto function = "Create";
78
Patrick Williamse212fb02022-07-22 19:26:57 -050079 sdbusplus::message_t method;
Ben Tyner188f1092021-02-01 09:33:06 -060080
81 if (0 == dbusMethod(pathLogging, interface, function, method))
82 {
83 try
84 {
85 // append additional dbus call parameters
86 method.append(eventPelTerminate, levelPelError, additional);
87
88 // using system dbus, no reply
89 auto bus = sdbusplus::bus::new_system();
90 bus.call_noreply(method);
91 }
92 catch (const sdbusplus::exception::SdBusError& e)
93 {
austinfcuibfa831a2022-01-26 15:37:07 -060094 trace::err("createPelRaw exception");
95 trace::err(e.what());
Ben Tyner188f1092021-02-01 09:33:06 -060096 }
97 }
98}
99
100/** @brief Get file descriptor of exisitng PEL */
101int getPel(const uint32_t i_pelId)
102{
103 // GetPEL returns file descriptor (int)
104 int fd = -1;
105
106 // dbus specifics
107 constexpr auto interface = "org.open_power.Logging.PEL";
108 constexpr auto function = "GetPEL";
109
Patrick Williamse212fb02022-07-22 19:26:57 -0500110 sdbusplus::message_t method;
Ben Tyner188f1092021-02-01 09:33:06 -0600111
112 if (0 == dbusMethod(pathLogging, interface, function, method))
113 {
114 try
115 {
116 // additional dbus call parameters
117 method.append(i_pelId);
118
119 // using system dbus
120 auto bus = sdbusplus::bus::new_system();
121 auto response = bus.call(method);
122
123 // reply will be a unix file descriptor
124 sdbusplus::message::unix_fd reply;
125
Ben Tyner4bbcb382021-02-22 09:29:00 -0600126 // parse dbus response into reply
Ben Tyner188f1092021-02-01 09:33:06 -0600127 response.read(reply);
128
129 fd = dup(reply); // need to copy (dup) the file descriptor
130 }
131 catch (const sdbusplus::exception::SdBusError& e)
132 {
austinfcuibfa831a2022-01-26 15:37:07 -0600133 trace::err("getPel exception");
134 trace::err(e.what());
Ben Tyner188f1092021-02-01 09:33:06 -0600135 }
136 }
137
138 return fd; // file descriptor or -1
139}
140
Ben Tyner188f1092021-02-01 09:33:06 -0600141} // namespace attn