xref: /openbmc/openpower-proc-control/util.cpp (revision 969196c323cb9bac9281195191c5774184d1daad)
1c4831812SJayanth Othayoth #include "util.hpp"
2c4831812SJayanth Othayoth 
3c4831812SJayanth Othayoth #include <phosphor-logging/elog.hpp>
4c4831812SJayanth Othayoth 
5e0dd7af4SJayanth Othayoth #include <format>
687441239SJayanth Othayoth #include <sstream>
787441239SJayanth Othayoth #include <variant>
8c4831812SJayanth Othayoth #include <vector>
9c4831812SJayanth Othayoth 
10c4831812SJayanth Othayoth namespace openpower
11c4831812SJayanth Othayoth {
12c4831812SJayanth Othayoth namespace util
13c4831812SJayanth Othayoth {
14c4831812SJayanth Othayoth using namespace phosphor::logging;
15c4831812SJayanth Othayoth 
getService(sdbusplus::bus_t & bus,const std::string & objectPath,const std::string & interface)16aaea6867SPatrick Williams std::string getService(sdbusplus::bus_t& bus, const std::string& objectPath,
17c4831812SJayanth Othayoth                        const std::string& interface)
18c4831812SJayanth Othayoth {
19c4831812SJayanth Othayoth     constexpr auto mapperBusBame = "xyz.openbmc_project.ObjectMapper";
20c4831812SJayanth Othayoth     constexpr auto mapperObjectPath = "/xyz/openbmc_project/object_mapper";
21c4831812SJayanth Othayoth     constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
22c4831812SJayanth Othayoth     std::vector<std::pair<std::string, std::vector<std::string>>> response;
23c4831812SJayanth Othayoth     auto method = bus.new_method_call(mapperBusBame, mapperObjectPath,
24c4831812SJayanth Othayoth                                       mapperInterface, "GetObject");
25c4831812SJayanth Othayoth     method.append(objectPath, std::vector<std::string>({interface}));
26c4831812SJayanth Othayoth     try
27c4831812SJayanth Othayoth     {
28c4831812SJayanth Othayoth         auto reply = bus.call(method);
29c4831812SJayanth Othayoth         reply.read(response);
30c4831812SJayanth Othayoth     }
31aaea6867SPatrick Williams     catch (const sdbusplus::exception_t& e)
32c4831812SJayanth Othayoth     {
33e0dd7af4SJayanth Othayoth         log<level::ERR>(std::format("D-Bus call exception OBJPATH={}"
34c4831812SJayanth Othayoth                                     "INTERFACE={}  EXCEPTION={}",
354b062597SJayanth Othayoth                                     objectPath, interface, e.what())
36c4831812SJayanth Othayoth                             .c_str());
37c4831812SJayanth Othayoth 
38c4831812SJayanth Othayoth         throw std::runtime_error("Service name is not found");
39c4831812SJayanth Othayoth     }
40c4831812SJayanth Othayoth 
41c4831812SJayanth Othayoth     if (response.empty())
42c4831812SJayanth Othayoth     {
43c4831812SJayanth Othayoth         throw std::runtime_error("Service name response is empty");
44c4831812SJayanth Othayoth     }
45c4831812SJayanth Othayoth     return response.begin()->first;
46c4831812SJayanth Othayoth }
474d5b5bfeSMarri Devender Rao 
isHostPoweringOff()484d5b5bfeSMarri Devender Rao bool isHostPoweringOff()
494d5b5bfeSMarri Devender Rao {
504d5b5bfeSMarri Devender Rao     try
514d5b5bfeSMarri Devender Rao     {
524d5b5bfeSMarri Devender Rao         constexpr auto object = "/xyz/openbmc_project/state/host0";
534d5b5bfeSMarri Devender Rao         constexpr auto service = "xyz.openbmc_project.State.Host";
544d5b5bfeSMarri Devender Rao         constexpr auto interface = "xyz.openbmc_project.State.Host";
554d5b5bfeSMarri Devender Rao         constexpr auto property = "CurrentHostState";
564d5b5bfeSMarri Devender Rao         auto bus = sdbusplus::bus::new_default();
574d5b5bfeSMarri Devender Rao 
584d5b5bfeSMarri Devender Rao         std::variant<std::string> retval;
594d5b5bfeSMarri Devender Rao         auto properties = bus.new_method_call(
604d5b5bfeSMarri Devender Rao             service, object, "org.freedesktop.DBus.Properties", "Get");
614d5b5bfeSMarri Devender Rao         properties.append(interface);
624d5b5bfeSMarri Devender Rao         properties.append(property);
634d5b5bfeSMarri Devender Rao         auto result = bus.call(properties);
644d5b5bfeSMarri Devender Rao         result.read(retval);
654d5b5bfeSMarri Devender Rao 
664d5b5bfeSMarri Devender Rao         const std::string* state = std::get_if<std::string>(&retval);
674d5b5bfeSMarri Devender Rao         if (state == nullptr)
684d5b5bfeSMarri Devender Rao         {
69e0dd7af4SJayanth Othayoth             std::string err = std::format(
704d5b5bfeSMarri Devender Rao                 "CurrentHostState property is not set ({})", object);
714d5b5bfeSMarri Devender Rao             log<level::ERR>(err.c_str());
724d5b5bfeSMarri Devender Rao             return false;
734d5b5bfeSMarri Devender Rao         }
744d5b5bfeSMarri Devender Rao         if ((*state == "xyz.openbmc_project.State.Host.HostState."
754d5b5bfeSMarri Devender Rao                        "TransitioningToOff") ||
764d5b5bfeSMarri Devender Rao             (*state == "xyz.openbmc_project.State.Host.HostState."
774d5b5bfeSMarri Devender Rao                        "Off") ||
784d5b5bfeSMarri Devender Rao             (*state == "xyz.openbmc_project.State.Host.HostState."
794d5b5bfeSMarri Devender Rao                        "Quiesced"))
804d5b5bfeSMarri Devender Rao         {
814d5b5bfeSMarri Devender Rao             return true;
824d5b5bfeSMarri Devender Rao         }
834d5b5bfeSMarri Devender Rao     }
844d5b5bfeSMarri Devender Rao     catch (const std::exception& ex)
854d5b5bfeSMarri Devender Rao     {
864d5b5bfeSMarri Devender Rao         log<level::ERR>(
87e0dd7af4SJayanth Othayoth             std::format("Failed to read CurrentHostState property ({})",
884d5b5bfeSMarri Devender Rao                         ex.what())
894d5b5bfeSMarri Devender Rao                 .c_str());
904d5b5bfeSMarri Devender Rao     }
914d5b5bfeSMarri Devender Rao     return false;
924d5b5bfeSMarri Devender Rao }
9387441239SJayanth Othayoth 
getChassisPowerState()9487441239SJayanth Othayoth std::string getChassisPowerState()
9587441239SJayanth Othayoth {
9687441239SJayanth Othayoth     std::string powerState{};
9787441239SJayanth Othayoth     try
9887441239SJayanth Othayoth     {
9987441239SJayanth Othayoth         auto bus = sdbusplus::bus::new_default();
10087441239SJayanth Othayoth         auto properties =
101*969196c3SPatrick Williams             bus.new_method_call("xyz.openbmc_project.State.Chassis0",
10287441239SJayanth Othayoth                                 "/xyz/openbmc_project/state/chassis0",
10387441239SJayanth Othayoth                                 "org.freedesktop.DBus.Properties", "Get");
10487441239SJayanth Othayoth         properties.append("xyz.openbmc_project.State.Chassis");
10587441239SJayanth Othayoth         properties.append("CurrentPowerState");
10687441239SJayanth Othayoth         auto result = bus.call(properties);
10787441239SJayanth Othayoth         std::variant<std::string> val;
10887441239SJayanth Othayoth         result.read(val);
10987441239SJayanth Othayoth         if (auto pVal = std::get_if<std::string>(&val))
11087441239SJayanth Othayoth         {
11187441239SJayanth Othayoth             powerState = *pVal;
11287441239SJayanth Othayoth         }
11387441239SJayanth Othayoth     }
11487441239SJayanth Othayoth 
11587441239SJayanth Othayoth     catch (const std::exception& ex)
11687441239SJayanth Othayoth     {
11787441239SJayanth Othayoth         log<level::ERR>(
118e0dd7af4SJayanth Othayoth             std::format("Failed to read CurrentPowerState property ({})",
11987441239SJayanth Othayoth                         ex.what())
12087441239SJayanth Othayoth                 .c_str());
12187441239SJayanth Othayoth     }
12287441239SJayanth Othayoth 
123e0dd7af4SJayanth Othayoth     log<level::DEBUG>(std::format("Power state is: {} ", powerState).c_str());
12487441239SJayanth Othayoth 
12587441239SJayanth Othayoth     return powerState;
12687441239SJayanth Othayoth }
12787441239SJayanth Othayoth 
128c4831812SJayanth Othayoth } // namespace util
129c4831812SJayanth Othayoth } // namespace openpower
130