#include "util.hpp" #include #include #include namespace openpower { namespace util { using namespace phosphor::logging; std::string getService(sdbusplus::bus::bus& bus, const std::string& objectPath, const std::string& interface) { constexpr auto mapperBusBame = "xyz.openbmc_project.ObjectMapper"; constexpr auto mapperObjectPath = "/xyz/openbmc_project/object_mapper"; constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper"; std::vector>> response; auto method = bus.new_method_call(mapperBusBame, mapperObjectPath, mapperInterface, "GetObject"); method.append(objectPath, std::vector({interface})); try { auto reply = bus.call(method); reply.read(response); } catch (const sdbusplus::exception::exception& e) { log(fmt::format("D-Bus call exception OBJPATH={}" "INTERFACE={} EXCEPTION={}", objectPath, interface, e.what()) .c_str()); throw std::runtime_error("Service name is not found"); } if (response.empty()) { throw std::runtime_error("Service name response is empty"); } return response.begin()->first; } bool isHostPoweringOff() { try { constexpr auto object = "/xyz/openbmc_project/state/host0"; constexpr auto service = "xyz.openbmc_project.State.Host"; constexpr auto interface = "xyz.openbmc_project.State.Host"; constexpr auto property = "CurrentHostState"; auto bus = sdbusplus::bus::new_default(); std::variant retval; auto properties = bus.new_method_call( service, object, "org.freedesktop.DBus.Properties", "Get"); properties.append(interface); properties.append(property); auto result = bus.call(properties); result.read(retval); const std::string* state = std::get_if(&retval); if (state == nullptr) { std::string err = fmt::format( "CurrentHostState property is not set ({})", object); log(err.c_str()); return false; } if ((*state == "xyz.openbmc_project.State.Host.HostState." "TransitioningToOff") || (*state == "xyz.openbmc_project.State.Host.HostState." "Off") || (*state == "xyz.openbmc_project.State.Host.HostState." "Quiesced")) { return true; } } catch (const std::exception& ex) { log( fmt::format("Failed to read CurrentHostState property ({})", ex.what()) .c_str()); } return false; } } // namespace util } // namespace openpower