blob: f5e12bd71b51ea322f42da8bc70a29a352c8fd05 [file] [log] [blame]
Jayanth Othayothd31be2c2020-02-04 02:56:45 -06001#include "dump_utils.hpp"
2
George Liu858fbb22021-07-01 12:25:44 +08003#include <fmt/core.h>
4
Jayanth Othayothd31be2c2020-02-04 02:56:45 -06005#include <phosphor-logging/log.hpp>
6
7namespace phosphor
8{
9namespace dump
10{
11
Ramesh Iyyar22793862020-12-04 04:03:03 -060012using namespace phosphor::logging;
13
Patrick Williams9b18bf22022-07-22 19:26:55 -050014std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060015 const std::string& interface)
16{
17 constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper";
18 constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper";
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060019
20 auto method = bus.new_method_call(objectMapperName, objectMapperPath,
21 objectMapperName, "GetObject");
22
23 method.append(path);
24 method.append(std::vector<std::string>({interface}));
25
26 std::vector<std::pair<std::string, std::vector<std::string>>> response;
27
28 try
29 {
30 auto reply = bus.call(method);
31 reply.read(response);
32 if (response.empty())
33 {
George Liu858fbb22021-07-01 12:25:44 +080034 log<level::ERR>(fmt::format("Error in mapper response for getting "
35 "service name, PATH({}), INTERFACE({})",
36 path, interface)
37 .c_str());
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060038 return std::string{};
39 }
40 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050041 catch (const sdbusplus::exception_t& e)
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060042 {
George Liu858fbb22021-07-01 12:25:44 +080043 log<level::ERR>(fmt::format("Error in mapper method call, "
44 "errormsg({}), PATH({}), INTERFACE({})",
45 e.what(), path, interface)
46 .c_str());
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060047 return std::string{};
48 }
49 return response[0].first;
50}
51
Ramesh Iyyar22793862020-12-04 04:03:03 -060052BootProgress getBootProgress()
53{
54 constexpr auto bootProgressInterface =
55 "xyz.openbmc_project.State.Boot.Progress";
56 // TODO Need to change host instance if multiple instead "0"
57 constexpr auto hostStateObjPath = "/xyz/openbmc_project/state/host0";
58
59 BootProgress bootProgessStage;
60
61 try
62 {
63 auto bus = sdbusplus::bus::new_default();
64 auto service = getService(bus, hostStateObjPath, bootProgressInterface);
65
66 auto method =
67 bus.new_method_call(service.c_str(), hostStateObjPath,
68 "org.freedesktop.DBus.Properties", "Get");
69
70 method.append(bootProgressInterface, "BootProgress");
71
72 auto reply = bus.call(method);
73
74 using DBusValue_t =
75 std::variant<std::string, bool, std::vector<uint8_t>,
76 std::vector<std::string>>;
77 DBusValue_t propertyVal;
78
79 reply.read(propertyVal);
80
81 // BootProgress property type is string
82 std::string bootPgs(std::get<std::string>(propertyVal));
83
84 bootProgessStage = sdbusplus::xyz::openbmc_project::State::Boot::
85 server::Progress::convertProgressStagesFromString(bootPgs);
86 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050087 catch (const sdbusplus::exception_t& e)
Ramesh Iyyar22793862020-12-04 04:03:03 -060088 {
George Liu858fbb22021-07-01 12:25:44 +080089 log<level::ERR>(fmt::format("D-Bus call exception, OBJPATH({}), "
90 "INTERFACE({}), EXCEPTION({})",
91 hostStateObjPath, bootProgressInterface,
92 e.what())
93 .c_str());
Ramesh Iyyar22793862020-12-04 04:03:03 -060094 throw std::runtime_error("Failed to get BootProgress stage");
95 }
96 catch (const std::bad_variant_access& e)
97 {
98 log<level::ERR>(
George Liu858fbb22021-07-01 12:25:44 +080099 fmt::format("Exception raised while read BootProgress property "
100 "value, OBJPATH({}), INTERFACE({}), EXCEPTION({})",
101 hostStateObjPath, bootProgressInterface, e.what())
102 .c_str());
Ramesh Iyyar22793862020-12-04 04:03:03 -0600103 throw std::runtime_error("Failed to get BootProgress stage");
104 }
105
106 return bootProgessStage;
107}
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600108
109bool isHostRunning()
110{
111 // TODO #ibm-openbmc/dev/2858 Revisit the method for finding whether host
112 // is running.
113 BootProgress bootProgressStatus = phosphor::dump::getBootProgress();
114 if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
115 (bootProgressStatus == BootProgress::OSStart) ||
Dhruvaraj Subhashchandran706ae1b2022-09-21 01:05:01 -0500116 (bootProgressStatus == BootProgress::OSRunning) ||
117 (bootProgressStatus == BootProgress::PCIInit))
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600118 {
119 return true;
120 }
121 return false;
122}
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600123} // namespace dump
124} // namespace phosphor