blob: ee75a4f540ba0fcf72e31487873d81a0d3f45cc9 [file] [log] [blame]
Jayanth Othayothd31be2c2020-02-04 02:56:45 -06001#include "dump_utils.hpp"
2
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -05003#include <phosphor-logging/lg2.hpp>
Jayanth Othayothd31be2c2020-02-04 02:56:45 -06004#include <phosphor-logging/log.hpp>
5
6namespace phosphor
7{
8namespace dump
9{
10
Ramesh Iyyar22793862020-12-04 04:03:03 -060011using namespace phosphor::logging;
12
Patrick Williams9b18bf22022-07-22 19:26:55 -050013std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060014 const std::string& interface)
15{
16 constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper";
17 constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper";
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060018
19 auto method = bus.new_method_call(objectMapperName, objectMapperPath,
20 objectMapperName, "GetObject");
21
22 method.append(path);
23 method.append(std::vector<std::string>({interface}));
24
25 std::vector<std::pair<std::string, std::vector<std::string>>> response;
26
27 try
28 {
29 auto reply = bus.call(method);
30 reply.read(response);
31 if (response.empty())
32 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050033 lg2::error(
34 "Error in mapper response for getting service name, PATH: "
35 "{PATH}, INTERFACE: {INTERFACE}",
36 "PATH", path, "INTERFACE", interface);
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060037 return std::string{};
38 }
39 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050040 catch (const sdbusplus::exception_t& e)
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060041 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050042 lg2::error("Error in mapper method call, errormsg: {ERROR}, "
43 "PATH: {PATH}, INTERFACE: {INTERFACE}",
44 "ERROR", e, "PATH", path, "INTERFACE", interface);
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060045 return std::string{};
46 }
47 return response[0].first;
48}
49
Ramesh Iyyar22793862020-12-04 04:03:03 -060050BootProgress getBootProgress()
51{
52 constexpr auto bootProgressInterface =
53 "xyz.openbmc_project.State.Boot.Progress";
54 // TODO Need to change host instance if multiple instead "0"
55 constexpr auto hostStateObjPath = "/xyz/openbmc_project/state/host0";
56
57 BootProgress bootProgessStage;
58
59 try
60 {
61 auto bus = sdbusplus::bus::new_default();
62 auto service = getService(bus, hostStateObjPath, bootProgressInterface);
63
Patrick Williams78e88402023-05-10 07:50:48 -050064 auto method = bus.new_method_call(service.c_str(), hostStateObjPath,
65 "org.freedesktop.DBus.Properties",
66 "Get");
Ramesh Iyyar22793862020-12-04 04:03:03 -060067
68 method.append(bootProgressInterface, "BootProgress");
69
70 auto reply = bus.call(method);
71
72 using DBusValue_t =
73 std::variant<std::string, bool, std::vector<uint8_t>,
74 std::vector<std::string>>;
75 DBusValue_t propertyVal;
76
77 reply.read(propertyVal);
78
79 // BootProgress property type is string
80 std::string bootPgs(std::get<std::string>(propertyVal));
81
82 bootProgessStage = sdbusplus::xyz::openbmc_project::State::Boot::
83 server::Progress::convertProgressStagesFromString(bootPgs);
84 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050085 catch (const sdbusplus::exception_t& e)
Ramesh Iyyar22793862020-12-04 04:03:03 -060086 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050087 lg2::error("D-Bus call exception, OBJPATH: {OBJ_PATH}, "
88 "INTERFACE: {INTERFACE}, EXCEPTION: {ERROR}",
89 "OBJ_PATH", hostStateObjPath, "INTERFACE",
90 bootProgressInterface, "ERROR", e);
Ramesh Iyyar22793862020-12-04 04:03:03 -060091 throw std::runtime_error("Failed to get BootProgress stage");
92 }
93 catch (const std::bad_variant_access& e)
94 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050095 lg2::error("Exception raised while read BootProgress property value, "
96 "OBJPATH: {OBJ_PATH}, INTERFACE: {INTERFACE}, "
97 "EXCEPTION: {ERROR}",
98 "OBJ_PATH", hostStateObjPath, "INTERFACE",
99 bootProgressInterface, "ERROR", e);
Ramesh Iyyar22793862020-12-04 04:03:03 -0600100 throw std::runtime_error("Failed to get BootProgress stage");
101 }
102
103 return bootProgessStage;
104}
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600105
106bool isHostRunning()
107{
108 // TODO #ibm-openbmc/dev/2858 Revisit the method for finding whether host
109 // is running.
110 BootProgress bootProgressStatus = phosphor::dump::getBootProgress();
111 if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
Ravi Teja8a9736b2022-04-02 01:42:22 -0500112 (bootProgressStatus == BootProgress::SystemSetup) ||
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600113 (bootProgressStatus == BootProgress::OSStart) ||
Dhruvaraj Subhashchandran706ae1b2022-09-21 01:05:01 -0500114 (bootProgressStatus == BootProgress::OSRunning) ||
115 (bootProgressStatus == BootProgress::PCIInit))
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600116 {
117 return true;
118 }
119 return false;
120}
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600121} // namespace dump
122} // namespace phosphor