1b8335568SBen Tyner #include <attn_common.hpp> 24bbcb38fSBen Tyner #include <attn_dbus.hpp> 3188f1096SBen Tyner #include <attn_logging.hpp> 4*bfa831a8Saustinfcui #include <util/trace.hpp> 5188f1096SBen Tyner 6188f1096SBen Tyner #include <string> 7188f1096SBen Tyner #include <vector> 8188f1096SBen Tyner 9188f1096SBen Tyner namespace attn 10188f1096SBen Tyner { 11188f1096SBen Tyner 125c5db65aSBen Tyner /** @brief Create a dbus method */ 13188f1096SBen Tyner int dbusMethod(const std::string& i_path, const std::string& i_interface, 14188f1096SBen Tyner const std::string& i_function, 154bbcb38fSBen Tyner sdbusplus::message::message& o_method, 165c5db65aSBen Tyner const std::string& i_extended) 17188f1096SBen Tyner { 184bbcb38fSBen Tyner int rc = RC_DBUS_ERROR; // assume error 19188f1096SBen Tyner 20188f1096SBen Tyner try 21188f1096SBen Tyner { 22188f1096SBen Tyner constexpr auto serviceFind = "xyz.openbmc_project.ObjectMapper"; 23188f1096SBen Tyner constexpr auto pathFind = "/xyz/openbmc_project/object_mapper"; 24188f1096SBen Tyner constexpr auto interfaceFind = "xyz.openbmc_project.ObjectMapper"; 25188f1096SBen Tyner constexpr auto functionFind = "GetObject"; 26188f1096SBen Tyner 27188f1096SBen Tyner auto bus = sdbusplus::bus::new_system(); // using system dbus 28188f1096SBen Tyner 29188f1096SBen Tyner // method to find service from object path and object interface 30188f1096SBen Tyner auto method = bus.new_method_call(serviceFind, pathFind, interfaceFind, 31188f1096SBen Tyner functionFind); 32188f1096SBen Tyner 33188f1096SBen Tyner // find the service for specified object path and interface 34188f1096SBen Tyner method.append(i_path.c_str()); 35188f1096SBen Tyner method.append(std::vector<std::string>({i_interface})); 36188f1096SBen Tyner auto reply = bus.call(method); 37188f1096SBen Tyner 38188f1096SBen Tyner // dbus call results 39188f1096SBen Tyner std::map<std::string, std::vector<std::string>> responseFindService; 40188f1096SBen Tyner reply.read(responseFindService); 41188f1096SBen Tyner 42188f1096SBen Tyner // If we successfully found the service associated with the dbus object 43188f1096SBen Tyner // path and interface then create a method for the specified interface 44188f1096SBen Tyner // and function. 45188f1096SBen Tyner if (!responseFindService.empty()) 46188f1096SBen Tyner { 47188f1096SBen Tyner auto service = responseFindService.begin()->first; 48188f1096SBen Tyner 494bbcb38fSBen Tyner // Some methods (e.g. get attribute) take an extended parameter 504bbcb38fSBen Tyner if (i_extended == "") 514bbcb38fSBen Tyner { 52188f1096SBen Tyner // return the method 534bbcb38fSBen Tyner o_method = bus.new_method_call(service.c_str(), i_path.c_str(), 544bbcb38fSBen Tyner i_interface.c_str(), 554bbcb38fSBen Tyner i_function.c_str()); 564bbcb38fSBen Tyner } 574bbcb38fSBen Tyner else 584bbcb38fSBen Tyner { 594bbcb38fSBen Tyner // return extended method 60188f1096SBen Tyner o_method = 61188f1096SBen Tyner bus.new_method_call(service.c_str(), i_path.c_str(), 624bbcb38fSBen Tyner i_extended.c_str(), i_function.c_str()); 634bbcb38fSBen Tyner } 64188f1096SBen Tyner 654bbcb38fSBen Tyner rc = RC_SUCCESS; 66188f1096SBen Tyner } 67188f1096SBen Tyner else 68188f1096SBen Tyner { 696764d70eSBen Tyner // This trace will be picked up in event log 70*bfa831a8Saustinfcui trace::inf("dbusMethod service not found"); 71*bfa831a8Saustinfcui trace::inf(i_path.c_str()); 72*bfa831a8Saustinfcui trace::inf(i_interface.c_str()); 73188f1096SBen Tyner } 74188f1096SBen Tyner } 75188f1096SBen Tyner catch (const sdbusplus::exception::SdBusError& e) 76188f1096SBen Tyner { 77*bfa831a8Saustinfcui trace::err("dbusMethod exception"); 78*bfa831a8Saustinfcui trace::err(e.what()); 79188f1096SBen Tyner } 80188f1096SBen Tyner 81188f1096SBen Tyner return rc; 82188f1096SBen Tyner } 83188f1096SBen Tyner 84188f1096SBen Tyner /** @brief Create a PEL for the specified event type */ 85188f1096SBen Tyner uint32_t createPel(const std::string& i_event, 86188f1096SBen Tyner std::map<std::string, std::string>& i_additional, 87188f1096SBen Tyner const std::vector<util::FFDCTuple>& i_ffdc) 88188f1096SBen Tyner { 89188f1096SBen Tyner // CreatePELWithFFDCFiles returns plid 90188f1096SBen Tyner int plid = 0; 91188f1096SBen Tyner 92188f1096SBen Tyner // Need to provide pid when using create or create-with-ffdc methods 93188f1096SBen Tyner i_additional.emplace("_PID", std::to_string(getpid())); 94188f1096SBen Tyner 95188f1096SBen Tyner // Sdbus call specifics 96188f1096SBen Tyner constexpr auto interface = "org.open_power.Logging.PEL"; 97188f1096SBen Tyner constexpr auto function = "CreatePELWithFFDCFiles"; 98188f1096SBen Tyner 99188f1096SBen Tyner sdbusplus::message::message method; 100188f1096SBen Tyner 101188f1096SBen Tyner if (0 == dbusMethod(pathLogging, interface, function, method)) 102188f1096SBen Tyner { 103188f1096SBen Tyner try 104188f1096SBen Tyner { 105188f1096SBen Tyner // append additional dbus call paramaters 106188f1096SBen Tyner method.append(i_event, levelPelError, i_additional, i_ffdc); 107188f1096SBen Tyner 108188f1096SBen Tyner // using system dbus 109188f1096SBen Tyner auto bus = sdbusplus::bus::new_system(); 110188f1096SBen Tyner auto response = bus.call(method); 111188f1096SBen Tyner 112188f1096SBen Tyner // reply will be tuple containing bmc log id, platform log id 113188f1096SBen Tyner std::tuple<uint32_t, uint32_t> reply = {0, 0}; 114188f1096SBen Tyner 1154bbcb38fSBen Tyner // parse dbus response into reply 116188f1096SBen Tyner response.read(reply); 117188f1096SBen Tyner plid = std::get<1>(reply); // platform log id is tuple "second" 118188f1096SBen Tyner } 119188f1096SBen Tyner catch (const sdbusplus::exception::SdBusError& e) 120188f1096SBen Tyner { 121*bfa831a8Saustinfcui trace::err("createPel exception"); 122*bfa831a8Saustinfcui trace::err(e.what()); 123188f1096SBen Tyner } 124188f1096SBen Tyner } 125188f1096SBen Tyner 126188f1096SBen Tyner return plid; // platform log id or 0 127188f1096SBen Tyner } 128188f1096SBen Tyner 129188f1096SBen Tyner /** @brief Create a PEL from raw PEL data */ 130188f1096SBen Tyner void createPelRaw(const std::vector<uint8_t>& i_buffer) 131188f1096SBen Tyner { 132188f1096SBen Tyner // Create FFDC file from buffer data 133188f1096SBen Tyner util::FFDCFile pelFile{util::FFDCFormat::Text}; 134188f1096SBen Tyner auto fd = pelFile.getFileDescriptor(); 135188f1096SBen Tyner 136188f1096SBen Tyner auto filePath = pelFile.getPath(); // path to ffdc file 137188f1096SBen Tyner 138d700609cSBen Tyner size_t numBytes = write(fd, i_buffer.data(), i_buffer.size()); 139d700609cSBen Tyner if (i_buffer.size() != numBytes) 140d700609cSBen Tyner { 141*bfa831a8Saustinfcui trace::err("%s only %u of %u bytes written", filePath.c_str(), numBytes, 142*bfa831a8Saustinfcui i_buffer.size()); 143d700609cSBen Tyner } 144d700609cSBen Tyner 145d700609cSBen Tyner lseek(fd, 0, SEEK_SET); 146d700609cSBen Tyner 147188f1096SBen Tyner // Additional data for log 148188f1096SBen Tyner std::map<std::string, std::string> additional; 149188f1096SBen Tyner additional.emplace("RAWPEL", filePath.string()); 150188f1096SBen Tyner additional.emplace("_PID", std::to_string(getpid())); 151188f1096SBen Tyner 152188f1096SBen Tyner // dbus specifics 153188f1096SBen Tyner constexpr auto interface = "xyz.openbmc_project.Logging.Create"; 154188f1096SBen Tyner constexpr auto function = "Create"; 155188f1096SBen Tyner 156188f1096SBen Tyner sdbusplus::message::message method; 157188f1096SBen Tyner 158188f1096SBen Tyner if (0 == dbusMethod(pathLogging, interface, function, method)) 159188f1096SBen Tyner { 160188f1096SBen Tyner try 161188f1096SBen Tyner { 162188f1096SBen Tyner // append additional dbus call parameters 163188f1096SBen Tyner method.append(eventPelTerminate, levelPelError, additional); 164188f1096SBen Tyner 165188f1096SBen Tyner // using system dbus, no reply 166188f1096SBen Tyner auto bus = sdbusplus::bus::new_system(); 167188f1096SBen Tyner bus.call_noreply(method); 168188f1096SBen Tyner } 169188f1096SBen Tyner catch (const sdbusplus::exception::SdBusError& e) 170188f1096SBen Tyner { 171*bfa831a8Saustinfcui trace::err("createPelRaw exception"); 172*bfa831a8Saustinfcui trace::err(e.what()); 173188f1096SBen Tyner } 174188f1096SBen Tyner } 175188f1096SBen Tyner } 176188f1096SBen Tyner 177188f1096SBen Tyner /** @brief Get file descriptor of exisitng PEL */ 178188f1096SBen Tyner int getPel(const uint32_t i_pelId) 179188f1096SBen Tyner { 180188f1096SBen Tyner // GetPEL returns file descriptor (int) 181188f1096SBen Tyner int fd = -1; 182188f1096SBen Tyner 183188f1096SBen Tyner // dbus specifics 184188f1096SBen Tyner constexpr auto interface = "org.open_power.Logging.PEL"; 185188f1096SBen Tyner constexpr auto function = "GetPEL"; 186188f1096SBen Tyner 187188f1096SBen Tyner sdbusplus::message::message method; 188188f1096SBen Tyner 189188f1096SBen Tyner if (0 == dbusMethod(pathLogging, interface, function, method)) 190188f1096SBen Tyner { 191188f1096SBen Tyner try 192188f1096SBen Tyner { 193188f1096SBen Tyner // additional dbus call parameters 194188f1096SBen Tyner method.append(i_pelId); 195188f1096SBen Tyner 196188f1096SBen Tyner // using system dbus 197188f1096SBen Tyner auto bus = sdbusplus::bus::new_system(); 198188f1096SBen Tyner auto response = bus.call(method); 199188f1096SBen Tyner 200188f1096SBen Tyner // reply will be a unix file descriptor 201188f1096SBen Tyner sdbusplus::message::unix_fd reply; 202188f1096SBen Tyner 2034bbcb38fSBen Tyner // parse dbus response into reply 204188f1096SBen Tyner response.read(reply); 205188f1096SBen Tyner 206188f1096SBen Tyner fd = dup(reply); // need to copy (dup) the file descriptor 207188f1096SBen Tyner } 208188f1096SBen Tyner catch (const sdbusplus::exception::SdBusError& e) 209188f1096SBen Tyner { 210*bfa831a8Saustinfcui trace::err("getPel exception"); 211*bfa831a8Saustinfcui trace::err(e.what()); 212188f1096SBen Tyner } 213188f1096SBen Tyner } 214188f1096SBen Tyner 215188f1096SBen Tyner return fd; // file descriptor or -1 216188f1096SBen Tyner } 217188f1096SBen Tyner 218188f1096SBen Tyner } // namespace attn 219