Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
Dhruvaraj Subhashchandran | ca9236c | 2024-04-17 01:56:04 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/lg2.hpp> |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 4 | #include <watchdog_dbus.hpp> |
| 5 | #include <watchdog_logging.hpp> |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace watchdog |
| 11 | { |
| 12 | namespace dump |
| 13 | { |
| 14 | |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 15 | int dbusMethod(const std::string& path, const std::string& interface, |
Patrick Williams | fc43756 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 16 | const std::string& function, sdbusplus::message_t& method, |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 17 | const std::string& extended) |
| 18 | { |
| 19 | int rc = RC_DBUS_ERROR; // assume error |
| 20 | |
| 21 | try |
| 22 | { |
| 23 | constexpr auto serviceFind = "xyz.openbmc_project.ObjectMapper"; |
| 24 | constexpr auto pathFind = "/xyz/openbmc_project/object_mapper"; |
| 25 | constexpr auto interfaceFind = "xyz.openbmc_project.ObjectMapper"; |
| 26 | constexpr auto functionFind = "GetObject"; |
| 27 | |
| 28 | auto bus = sdbusplus::bus::new_system(); // using system dbus |
| 29 | |
| 30 | // method to find service from object path and object interface |
| 31 | auto newMethod = bus.new_method_call(serviceFind, pathFind, |
| 32 | interfaceFind, functionFind); |
| 33 | |
| 34 | // find the service for specified object path and interface |
| 35 | newMethod.append(path.c_str()); |
| 36 | newMethod.append(std::vector<std::string>({interface})); |
| 37 | auto reply = bus.call(newMethod); |
| 38 | |
| 39 | // dbus call results |
| 40 | std::map<std::string, std::vector<std::string>> responseFindService; |
| 41 | reply.read(responseFindService); |
| 42 | |
| 43 | // If we successfully found the service associated with the dbus object |
| 44 | // path and interface then create a method for the specified interface |
| 45 | // and function. |
| 46 | if (!responseFindService.empty()) |
| 47 | { |
| 48 | auto service = responseFindService.begin()->first; |
| 49 | |
| 50 | // Some methods (e.g. get attribute) take an extended parameter |
| 51 | if (extended == "") |
| 52 | { |
| 53 | // return the method |
Patrick Williams | a9779c4 | 2023-05-10 07:50:20 -0500 | [diff] [blame] | 54 | method = bus.new_method_call(service.c_str(), path.c_str(), |
| 55 | interface.c_str(), |
| 56 | function.c_str()); |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 57 | } |
| 58 | else |
| 59 | { |
| 60 | // return extended method |
Patrick Williams | a9779c4 | 2023-05-10 07:50:20 -0500 | [diff] [blame] | 61 | method = bus.new_method_call(service.c_str(), path.c_str(), |
| 62 | extended.c_str(), |
| 63 | function.c_str()); |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | rc = RC_SUCCESS; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // This trace will be picked up in event log |
Dhruvaraj Subhashchandran | ca9236c | 2024-04-17 01:56:04 -0500 | [diff] [blame] | 71 | lg2::info("dbusMethod service not found"); |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 72 | std::string traceMsgPath = std::string(path, maxTraceLen); |
Dhruvaraj Subhashchandran | ca9236c | 2024-04-17 01:56:04 -0500 | [diff] [blame] | 73 | lg2::info(traceMsgPath.c_str()); |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 74 | std::string traceMsgIface = std::string(interface, maxTraceLen); |
Dhruvaraj Subhashchandran | ca9236c | 2024-04-17 01:56:04 -0500 | [diff] [blame] | 75 | lg2::info(traceMsgIface.c_str()); |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 76 | } |
| 77 | } |
Patrick Williams | fc43756 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 78 | catch (const sdbusplus::exception_t& e) |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 79 | { |
Dhruvaraj Subhashchandran | ca9236c | 2024-04-17 01:56:04 -0500 | [diff] [blame] | 80 | lg2::error("Error in dbusMethod ({ERROR})", "ERROR", e); |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return rc; |
| 84 | } |
| 85 | |
| 86 | uint32_t createPel(const std::string& eventType, |
| 87 | std::map<std::string, std::string>& additional, |
| 88 | const std::vector<FFDCTuple>& ffdc) |
| 89 | { |
| 90 | // Create returns plid |
| 91 | int plid = 0; |
| 92 | |
| 93 | // Need to provide pid when using create or create-with-ffdc methods |
| 94 | additional.emplace("_PID", std::to_string(getpid())); |
| 95 | |
| 96 | // Sdbus call specifics |
| 97 | constexpr auto interface = "org.open_power.Logging.PEL"; |
| 98 | constexpr auto function = "CreatePELWithFFDCFiles"; |
| 99 | |
Patrick Williams | fc43756 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 100 | sdbusplus::message_t method; |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 101 | |
| 102 | if (0 == dbusMethod(pathLogging, interface, function, method)) |
| 103 | { |
| 104 | try |
| 105 | { |
Manojkiran Eda | eb46252 | 2024-06-17 11:05:27 +0530 | [diff] [blame] | 106 | // append additional dbus call parameters |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 107 | method.append(eventType, levelPelError, additional, ffdc); |
| 108 | |
| 109 | // using system dbus |
| 110 | auto bus = sdbusplus::bus::new_system(); |
| 111 | auto response = bus.call(method); |
| 112 | |
| 113 | // reply will be tuple containing bmc log id, platform log id |
| 114 | std::tuple<uint32_t, uint32_t> reply = {0, 0}; |
| 115 | |
| 116 | // parse dbus response into reply |
| 117 | response.read(reply); |
| 118 | plid = std::get<1>(reply); // platform log id is tuple "second" |
| 119 | } |
Patrick Williams | fc43756 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 120 | catch (const sdbusplus::exception_t& e) |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 121 | { |
Dhruvaraj Subhashchandran | ca9236c | 2024-04-17 01:56:04 -0500 | [diff] [blame] | 122 | lg2::error("Error in createPel CreatePELWithFFDCFiles ({ERROR})", |
| 123 | "ERROR", e); |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | return plid; // platform log id or 0 |
| 128 | } |
| 129 | |
Shantappa Teekappanavar | e6978e5 | 2022-03-03 18:34:25 -0600 | [diff] [blame] | 130 | bool isHostStateRunning() |
| 131 | { |
| 132 | constexpr auto path = "/xyz/openbmc_project/state/host0"; |
| 133 | constexpr auto interface = "xyz.openbmc_project.State.Host"; |
| 134 | constexpr auto extended = "org.freedesktop.DBus.Properties"; |
| 135 | constexpr auto function = "Get"; |
| 136 | |
Patrick Williams | ced5ff5 | 2022-11-26 09:41:59 -0600 | [diff] [blame] | 137 | sdbusplus::message_t method; |
Shantappa Teekappanavar | e6978e5 | 2022-03-03 18:34:25 -0600 | [diff] [blame] | 138 | |
| 139 | if (0 == dbusMethod(path, interface, function, method, extended)) |
| 140 | { |
| 141 | try |
| 142 | { |
| 143 | method.append(interface, "CurrentHostState"); |
| 144 | auto bus = sdbusplus::bus::new_system(); |
| 145 | auto response = bus.call(method); |
| 146 | std::variant<std::string> reply; |
| 147 | |
| 148 | response.read(reply); |
| 149 | std::string currentHostState(std::get<std::string>(reply)); |
| 150 | |
| 151 | if (currentHostState == |
| 152 | "xyz.openbmc_project.State.Host.HostState.Running") |
| 153 | { |
| 154 | return true; |
| 155 | } |
| 156 | } |
Patrick Williams | ced5ff5 | 2022-11-26 09:41:59 -0600 | [diff] [blame] | 157 | catch (const sdbusplus::exception_t& e) |
Shantappa Teekappanavar | e6978e5 | 2022-03-03 18:34:25 -0600 | [diff] [blame] | 158 | { |
Dhruvaraj Subhashchandran | ca9236c | 2024-04-17 01:56:04 -0500 | [diff] [blame] | 159 | lg2::error("Failed to read CurrentHostState property ({ERROR})", |
| 160 | "ERROR", e); |
Shantappa Teekappanavar | e6978e5 | 2022-03-03 18:34:25 -0600 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
Shantappa Teekappanavar | 1ac6162 | 2021-06-22 19:07:29 -0500 | [diff] [blame] | 167 | } // namespace dump |
| 168 | } // namespace watchdog |