1 #include "config.h"
2 
3 #include "ramoops_manager.hpp"
4 
5 #include <fmt/core.h>
6 
7 #include <sdbusplus/exception.hpp>
8 
9 #include <filesystem>
10 
11 namespace phosphor
12 {
13 namespace dump
14 {
15 namespace ramoops
16 {
17 
18 Manager::Manager(const std::string& filePath)
19 {
20     namespace fs = std::filesystem;
21 
22     fs::path dir(filePath);
23     if (!fs::exists(dir) || fs::is_empty(dir))
24     {
25         return;
26     }
27 
28     std::vector<std::string> files;
29     files.push_back(filePath);
30 
31     createHelper(files);
32 }
33 
34 void Manager::createHelper(const std::vector<std::string>& files)
35 {
36     constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
37     constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
38     constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
39     constexpr auto IFACE_INTERNAL("xyz.openbmc_project.Dump.Internal.Create");
40     constexpr auto RAMOOPS =
41         "xyz.openbmc_project.Dump.Internal.Create.Type.Ramoops";
42 
43     auto b = sdbusplus::bus::new_default();
44     auto mapper = b.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
45                                     MAPPER_INTERFACE, "GetObject");
46     mapper.append(OBJ_INTERNAL, std::set<std::string>({IFACE_INTERNAL}));
47 
48     std::map<std::string, std::set<std::string>> mapperResponse;
49     try
50     {
51         auto mapperResponseMsg = b.call(mapper);
52         mapperResponseMsg.read(mapperResponse);
53     }
54     catch (const sdbusplus::exception_t& e)
55     {
56         log<level::ERR>(
57             fmt::format("Failed to parse dump create message, error({})",
58                         e.what())
59                 .c_str());
60         return;
61     }
62     if (mapperResponse.empty())
63     {
64         log<level::ERR>("Error reading mapper response");
65         return;
66     }
67 
68     const auto& host = mapperResponse.cbegin()->first;
69     auto m = b.new_method_call(host.c_str(), OBJ_INTERNAL, IFACE_INTERNAL,
70                                "Create");
71     m.append(RAMOOPS, files);
72     try
73     {
74         b.call_noreply(m);
75     }
76     catch (const sdbusplus::exception_t& e)
77     {
78         log<level::ERR>(
79             fmt::format("Failed to create ramoops dump, errormsg({})", e.what())
80                 .c_str());
81     }
82 }
83 
84 } // namespace ramoops
85 } // namespace dump
86 } // namespace phosphor
87