Zane Shelley | f36466f | 2022-02-11 15:10:55 -0600 | [diff] [blame] | 1 | #include <attn/attn_common.hpp> |
| 2 | #include <attn/attn_dbus.hpp> |
| 3 | #include <attn/attn_logging.hpp> |
Ben Tyner | 42260bc | 2022-02-16 14:36:58 -0600 | [diff] [blame] | 4 | #include <util/dbus.hpp> |
austinfcui | bfa831a | 2022-01-26 15:37:07 -0600 | [diff] [blame] | 5 | #include <util/trace.hpp> |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace attn |
| 11 | { |
| 12 | |
Ben Tyner | 5c5db65 | 2021-02-22 18:22:35 -0600 | [diff] [blame] | 13 | /** @brief Create a dbus method */ |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 14 | int dbusMethod(const std::string& i_path, const std::string& i_interface, |
| 15 | const std::string& i_function, |
Ben Tyner | a098210 | 2022-02-16 14:19:41 -0600 | [diff] [blame] | 16 | sdbusplus::message::message& o_method) |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 17 | { |
Ben Tyner | 4bbcb38 | 2021-02-22 09:29:00 -0600 | [diff] [blame] | 18 | int rc = RC_DBUS_ERROR; // assume error |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 19 | |
| 20 | try |
| 21 | { |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 22 | auto bus = sdbusplus::bus::new_system(); // using system dbus |
| 23 | |
Ben Tyner | 42260bc | 2022-02-16 14:36:58 -0600 | [diff] [blame] | 24 | // we need to find the service implementing the interface |
| 25 | util::dbus::DBusService service; |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 26 | |
Ben Tyner | 42260bc | 2022-02-16 14:36:58 -0600 | [diff] [blame] | 27 | if (0 == util::dbus::findService(i_interface, i_path, service)) |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 28 | { |
Ben Tyner | a098210 | 2022-02-16 14:19:41 -0600 | [diff] [blame] | 29 | // 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 Tyner | 42260bc | 2022-02-16 14:36:58 -0600 | [diff] [blame] | 33 | |
Ben Tyner | 4bbcb38 | 2021-02-22 09:29:00 -0600 | [diff] [blame] | 34 | rc = RC_SUCCESS; |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 35 | } |
| 36 | else |
| 37 | { |
Ben Tyner | 6764d70 | 2021-02-12 09:17:23 -0600 | [diff] [blame] | 38 | // This trace will be picked up in event log |
austinfcui | bfa831a | 2022-01-26 15:37:07 -0600 | [diff] [blame] | 39 | trace::inf("dbusMethod service not found"); |
| 40 | trace::inf(i_path.c_str()); |
| 41 | trace::inf(i_interface.c_str()); |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | catch (const sdbusplus::exception::SdBusError& e) |
| 45 | { |
austinfcui | bfa831a | 2022-01-26 15:37:07 -0600 | [diff] [blame] | 46 | trace::err("dbusMethod exception"); |
| 47 | trace::err(e.what()); |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | return rc; |
| 51 | } |
| 52 | |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 53 | /** @brief Create a PEL from raw PEL data */ |
| 54 | void 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 Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 59 | |
| 60 | auto filePath = pelFile.getPath(); // path to ffdc file |
| 61 | |
Ben Tyner | d700609 | 2021-02-05 14:55:52 -0600 | [diff] [blame] | 62 | size_t numBytes = write(fd, i_buffer.data(), i_buffer.size()); |
| 63 | if (i_buffer.size() != numBytes) |
| 64 | { |
austinfcui | bfa831a | 2022-01-26 15:37:07 -0600 | [diff] [blame] | 65 | trace::err("%s only %u of %u bytes written", filePath.c_str(), numBytes, |
| 66 | i_buffer.size()); |
Ben Tyner | d700609 | 2021-02-05 14:55:52 -0600 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | lseek(fd, 0, SEEK_SET); |
| 70 | |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 71 | // 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 | { |
austinfcui | bfa831a | 2022-01-26 15:37:07 -0600 | [diff] [blame] | 95 | trace::err("createPelRaw exception"); |
| 96 | trace::err(e.what()); |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** @brief Get file descriptor of exisitng PEL */ |
| 102 | int 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 Tyner | 4bbcb38 | 2021-02-22 09:29:00 -0600 | [diff] [blame] | 127 | // parse dbus response into reply |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 128 | response.read(reply); |
| 129 | |
| 130 | fd = dup(reply); // need to copy (dup) the file descriptor |
| 131 | } |
| 132 | catch (const sdbusplus::exception::SdBusError& e) |
| 133 | { |
austinfcui | bfa831a | 2022-01-26 15:37:07 -0600 | [diff] [blame] | 134 | trace::err("getPel exception"); |
| 135 | trace::err(e.what()); |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | return fd; // file descriptor or -1 |
| 140 | } |
| 141 | |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 142 | } // namespace attn |