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