xref: /openbmc/phosphor-debug-collector/dump_utils.cpp (revision d73a663c79943ae97194347ca1ee3f8d454f6bfa)
1 #include "dump_utils.hpp"
2 
3 #include <phosphor-logging/lg2.hpp>
4 
5 namespace phosphor
6 {
7 namespace dump
8 {
9 
10 std::string getService(sdbusplus::bus_t& bus, const std::string& path,
11                        const std::string& interface)
12 {
13     constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper";
14     constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper";
15 
16     auto method = bus.new_method_call(objectMapperName, objectMapperPath,
17                                       objectMapperName, "GetObject");
18 
19     method.append(path);
20     method.append(std::vector<std::string>({interface}));
21 
22     std::vector<std::pair<std::string, std::vector<std::string>>> response;
23 
24     try
25     {
26         auto reply = bus.call(method);
27         reply.read(response);
28         if (response.empty())
29         {
30             lg2::error(
31                 "Error in mapper response for getting service name, PATH: "
32                 "{PATH}, INTERFACE: {INTERFACE}",
33                 "PATH", path, "INTERFACE", interface);
34             return std::string{};
35         }
36     }
37     catch (const sdbusplus::exception_t& e)
38     {
39         lg2::error("Error in mapper method call, errormsg: {ERROR}, "
40                    "PATH: {PATH}, INTERFACE: {INTERFACE}",
41                    "ERROR", e, "PATH", path, "INTERFACE", interface);
42         return std::string{};
43     }
44     return response[0].first;
45 }
46 
47 BootProgress getBootProgress()
48 {
49     constexpr auto bootProgressInterface =
50         "xyz.openbmc_project.State.Boot.Progress";
51     // TODO Need to change host instance if multiple instead "0"
52     constexpr auto hostStateObjPath = "/xyz/openbmc_project/state/host0";
53 
54     BootProgress bootProgessStage;
55 
56     try
57     {
58         auto bus = sdbusplus::bus::new_default();
59         auto service = getService(bus, hostStateObjPath, bootProgressInterface);
60 
61         auto method = bus.new_method_call(service.c_str(), hostStateObjPath,
62                                           "org.freedesktop.DBus.Properties",
63                                           "Get");
64 
65         method.append(bootProgressInterface, "BootProgress");
66 
67         auto reply = bus.call(method);
68 
69         using DBusValue_t =
70             std::variant<std::string, bool, std::vector<uint8_t>,
71                          std::vector<std::string>>;
72         DBusValue_t propertyVal;
73 
74         reply.read(propertyVal);
75 
76         // BootProgress property type is string
77         std::string bootPgs(std::get<std::string>(propertyVal));
78 
79         bootProgessStage = sdbusplus::xyz::openbmc_project::State::Boot::
80             server::Progress::convertProgressStagesFromString(bootPgs);
81     }
82     catch (const sdbusplus::exception_t& e)
83     {
84         lg2::error("D-Bus call exception, OBJPATH: {OBJ_PATH}, "
85                    "INTERFACE: {INTERFACE}, EXCEPTION: {ERROR}",
86                    "OBJ_PATH", hostStateObjPath, "INTERFACE",
87                    bootProgressInterface, "ERROR", e);
88         throw std::runtime_error("Failed to get BootProgress stage");
89     }
90     catch (const std::bad_variant_access& e)
91     {
92         lg2::error("Exception raised while read BootProgress property value, "
93                    "OBJPATH: {OBJ_PATH}, INTERFACE: {INTERFACE}, "
94                    "EXCEPTION: {ERROR}",
95                    "OBJ_PATH", hostStateObjPath, "INTERFACE",
96                    bootProgressInterface, "ERROR", e);
97         throw std::runtime_error("Failed to get BootProgress stage");
98     }
99 
100     return bootProgessStage;
101 }
102 
103 bool isHostRunning()
104 {
105     // TODO #ibm-openbmc/dev/2858 Revisit the method for finding whether host
106     // is running.
107     BootProgress bootProgressStatus = phosphor::dump::getBootProgress();
108     if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
109         (bootProgressStatus == BootProgress::SystemSetup) ||
110         (bootProgressStatus == BootProgress::OSStart) ||
111         (bootProgressStatus == BootProgress::OSRunning) ||
112         (bootProgressStatus == BootProgress::PCIInit))
113     {
114         return true;
115     }
116     return false;
117 }
118 } // namespace dump
119 } // namespace phosphor
120