blob: 45f41539807e5a49b4ac23626c17c40f1b0b3abd [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";
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050058 auto value =
59 getStateValue(bootProgressInterface, hostStateObjPath, "BootProgress");
60 return sdbusplus::xyz::openbmc_project::State::Boot::server::Progress::
61 convertProgressStagesFromString(value);
62}
Ramesh Iyyar22793862020-12-04 04:03:03 -060063
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050064HostState getHostState()
65{
66 constexpr auto hostStateInterface = "xyz.openbmc_project.State.Host";
67 // TODO Need to change host instance if multiple instead "0"
68 constexpr auto hostStateObjPath = "/xyz/openbmc_project/state/host0";
69 auto value =
70 getStateValue(hostStateInterface, hostStateObjPath, "CurrentHostState");
71 return sdbusplus::xyz::openbmc_project::State::server::Host::
72 convertHostStateFromString(value);
73}
Ramesh Iyyar22793862020-12-04 04:03:03 -060074
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050075std::string getStateValue(const std::string& intf, const std::string& objPath,
76 const std::string& state)
77{
78 std::string stateVal;
Ramesh Iyyar22793862020-12-04 04:03:03 -060079 try
80 {
81 auto bus = sdbusplus::bus::new_default();
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050082 auto service = getService(bus, objPath, intf);
Ramesh Iyyar22793862020-12-04 04:03:03 -060083
84 auto method =
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050085 bus.new_method_call(service.c_str(), objPath.c_str(),
Ramesh Iyyar22793862020-12-04 04:03:03 -060086 "org.freedesktop.DBus.Properties", "Get");
87
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050088 method.append(intf, state);
Ramesh Iyyar22793862020-12-04 04:03:03 -060089
90 auto reply = bus.call(method);
91
92 using DBusValue_t =
93 std::variant<std::string, bool, std::vector<uint8_t>,
94 std::vector<std::string>>;
95 DBusValue_t propertyVal;
96
97 reply.read(propertyVal);
98
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050099 stateVal = std::get<std::string>(propertyVal);
Ramesh Iyyar22793862020-12-04 04:03:03 -0600100 }
Patrick Williams9b18bf22022-07-22 19:26:55 -0500101 catch (const sdbusplus::exception_t& e)
Ramesh Iyyar22793862020-12-04 04:03:03 -0600102 {
George Liu858fbb22021-07-01 12:25:44 +0800103 log<level::ERR>(fmt::format("D-Bus call exception, OBJPATH({}), "
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500104 "INTERFACE({}), PROPERTY({}) EXCEPTION({})",
105 objPath, intf, state, e.what())
George Liu858fbb22021-07-01 12:25:44 +0800106 .c_str());
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500107 throw std::runtime_error("Failed to get state property");
Ramesh Iyyar22793862020-12-04 04:03:03 -0600108 }
109 catch (const std::bad_variant_access& e)
110 {
111 log<level::ERR>(
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500112 fmt::format("Exception raised while read host state({}) property "
George Liu858fbb22021-07-01 12:25:44 +0800113 "value, OBJPATH({}), INTERFACE({}), EXCEPTION({})",
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500114 state, objPath, intf, e.what())
George Liu858fbb22021-07-01 12:25:44 +0800115 .c_str());
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500116 throw std::runtime_error("Failed to get host state property");
Ramesh Iyyar22793862020-12-04 04:03:03 -0600117 }
118
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500119 return stateVal;
Ramesh Iyyar22793862020-12-04 04:03:03 -0600120}
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600121
122bool isHostRunning()
123{
124 // TODO #ibm-openbmc/dev/2858 Revisit the method for finding whether host
125 // is running.
126 BootProgress bootProgressStatus = phosphor::dump::getBootProgress();
127 if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
Ravi Teja8a9736b2022-04-02 01:42:22 -0500128 (bootProgressStatus == BootProgress::SystemSetup) ||
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600129 (bootProgressStatus == BootProgress::OSStart) ||
Dhruvaraj Subhashchandran706ae1b2022-09-21 01:05:01 -0500130 (bootProgressStatus == BootProgress::OSRunning) ||
131 (bootProgressStatus == BootProgress::PCIInit))
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600132 {
133 return true;
134 }
135 return false;
136}
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500137
138bool isHostQuiesced()
139{
140 return (phosphor::dump::getHostState() == HostState::Quiesced);
141}
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600142} // namespace dump
143} // namespace phosphor