Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 1 | #include "dump_utils.hpp" |
| 2 | |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 3 | #include <fmt/core.h> |
| 4 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace dump |
| 10 | { |
| 11 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 12 | using namespace phosphor::logging; |
| 13 | |
Patrick Williams | 9b18bf2 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 14 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 15 | const std::string& interface) |
| 16 | { |
| 17 | constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper"; |
| 18 | constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper"; |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 19 | |
| 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 Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 34 | log<level::ERR>(fmt::format("Error in mapper response for getting " |
| 35 | "service name, PATH({}), INTERFACE({})", |
| 36 | path, interface) |
| 37 | .c_str()); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 38 | return std::string{}; |
| 39 | } |
| 40 | } |
Patrick Williams | 9b18bf2 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 41 | catch (const sdbusplus::exception_t& e) |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 42 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 43 | log<level::ERR>(fmt::format("Error in mapper method call, " |
| 44 | "errormsg({}), PATH({}), INTERFACE({})", |
| 45 | e.what(), path, interface) |
| 46 | .c_str()); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 47 | return std::string{}; |
| 48 | } |
| 49 | return response[0].first; |
| 50 | } |
| 51 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 52 | BootProgress 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 Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 58 | auto value = |
| 59 | getStateValue(bootProgressInterface, hostStateObjPath, "BootProgress"); |
| 60 | return sdbusplus::xyz::openbmc_project::State::Boot::server::Progress:: |
| 61 | convertProgressStagesFromString(value); |
| 62 | } |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 63 | |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 64 | HostState 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 Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 74 | |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 75 | std::string getStateValue(const std::string& intf, const std::string& objPath, |
| 76 | const std::string& state) |
| 77 | { |
| 78 | std::string stateVal; |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 79 | try |
| 80 | { |
| 81 | auto bus = sdbusplus::bus::new_default(); |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 82 | auto service = getService(bus, objPath, intf); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 83 | |
| 84 | auto method = |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 85 | bus.new_method_call(service.c_str(), objPath.c_str(), |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 86 | "org.freedesktop.DBus.Properties", "Get"); |
| 87 | |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 88 | method.append(intf, state); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 89 | |
| 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 Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 99 | stateVal = std::get<std::string>(propertyVal); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 100 | } |
Patrick Williams | 9b18bf2 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 101 | catch (const sdbusplus::exception_t& e) |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 102 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 103 | log<level::ERR>(fmt::format("D-Bus call exception, OBJPATH({}), " |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 104 | "INTERFACE({}), PROPERTY({}) EXCEPTION({})", |
| 105 | objPath, intf, state, e.what()) |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 106 | .c_str()); |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 107 | throw std::runtime_error("Failed to get state property"); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 108 | } |
| 109 | catch (const std::bad_variant_access& e) |
| 110 | { |
| 111 | log<level::ERR>( |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 112 | fmt::format("Exception raised while read host state({}) property " |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 113 | "value, OBJPATH({}), INTERFACE({}), EXCEPTION({})", |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 114 | state, objPath, intf, e.what()) |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 115 | .c_str()); |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 116 | throw std::runtime_error("Failed to get host state property"); |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 117 | } |
| 118 | |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 119 | return stateVal; |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 120 | } |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 121 | |
| 122 | bool 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 Teja | 8a9736b | 2022-04-02 01:42:22 -0500 | [diff] [blame] | 128 | (bootProgressStatus == BootProgress::SystemSetup) || |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 129 | (bootProgressStatus == BootProgress::OSStart) || |
Dhruvaraj Subhashchandran | 706ae1b | 2022-09-21 01:05:01 -0500 | [diff] [blame] | 130 | (bootProgressStatus == BootProgress::OSRunning) || |
| 131 | (bootProgressStatus == BootProgress::PCIInit)) |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 132 | { |
| 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | } |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 137 | |
| 138 | bool isHostQuiesced() |
| 139 | { |
| 140 | return (phosphor::dump::getHostState() == HostState::Quiesced); |
| 141 | } |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 142 | } // namespace dump |
| 143 | } // namespace phosphor |