blob: 74246aeaf4de3da06a86666ed092da853190e85c [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
5namespace phosphor
6{
7namespace dump
8{
9
Patrick Williams9b18bf22022-07-22 19:26:55 -050010std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060011 const std::string& interface)
12{
13 constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper";
14 constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper";
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060015
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 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050030 lg2::error(
31 "Error in mapper response for getting service name, PATH: "
32 "{PATH}, INTERFACE: {INTERFACE}",
33 "PATH", path, "INTERFACE", interface);
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060034 return std::string{};
35 }
36 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050037 catch (const sdbusplus::exception_t& e)
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060038 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050039 lg2::error("Error in mapper method call, errormsg: {ERROR}, "
40 "PATH: {PATH}, INTERFACE: {INTERFACE}",
41 "ERROR", e, "PATH", path, "INTERFACE", interface);
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060042 return std::string{};
43 }
44 return response[0].first;
45}
46
Ramesh Iyyar22793862020-12-04 04:03:03 -060047BootProgress 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
Patrick Williams78e88402023-05-10 07:50:48 -050061 auto method = bus.new_method_call(service.c_str(), hostStateObjPath,
62 "org.freedesktop.DBus.Properties",
63 "Get");
Ramesh Iyyar22793862020-12-04 04:03:03 -060064
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 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050082 catch (const sdbusplus::exception_t& e)
Ramesh Iyyar22793862020-12-04 04:03:03 -060083 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050084 lg2::error("D-Bus call exception, OBJPATH: {OBJ_PATH}, "
85 "INTERFACE: {INTERFACE}, EXCEPTION: {ERROR}",
86 "OBJ_PATH", hostStateObjPath, "INTERFACE",
87 bootProgressInterface, "ERROR", e);
Ramesh Iyyar22793862020-12-04 04:03:03 -060088 throw std::runtime_error("Failed to get BootProgress stage");
89 }
90 catch (const std::bad_variant_access& e)
91 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050092 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);
Ramesh Iyyar22793862020-12-04 04:03:03 -060097 throw std::runtime_error("Failed to get BootProgress stage");
98 }
99
100 return bootProgessStage;
101}
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600102
103bool 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) ||
Ravi Teja8a9736b2022-04-02 01:42:22 -0500109 (bootProgressStatus == BootProgress::SystemSetup) ||
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600110 (bootProgressStatus == BootProgress::OSStart) ||
Dhruvaraj Subhashchandran706ae1b2022-09-21 01:05:01 -0500111 (bootProgressStatus == BootProgress::OSRunning) ||
112 (bootProgressStatus == BootProgress::PCIInit))
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600113 {
114 return true;
115 }
116 return false;
117}
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600118} // namespace dump
119} // namespace phosphor