xref: /openbmc/phosphor-debug-collector/dump_utils.cpp (revision 6a54d9afebfd204ace67b842f7b3c2373451f472)
1d31be2ccSJayanth Othayoth #include "dump_utils.hpp"
2d31be2ccSJayanth Othayoth 
3d31be2ccSJayanth Othayoth #include <phosphor-logging/log.hpp>
4d31be2ccSJayanth Othayoth 
5d31be2ccSJayanth Othayoth namespace phosphor
6d31be2ccSJayanth Othayoth {
7d31be2ccSJayanth Othayoth namespace dump
8d31be2ccSJayanth Othayoth {
9d31be2ccSJayanth Othayoth 
102279386bSRamesh Iyyar using namespace phosphor::logging;
112279386bSRamesh Iyyar 
12d31be2ccSJayanth Othayoth std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
13d31be2ccSJayanth Othayoth                        const std::string& interface)
14d31be2ccSJayanth Othayoth {
15d31be2ccSJayanth Othayoth     constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper";
16d31be2ccSJayanth Othayoth     constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper";
17d31be2ccSJayanth Othayoth 
18d31be2ccSJayanth Othayoth     auto method = bus.new_method_call(objectMapperName, objectMapperPath,
19d31be2ccSJayanth Othayoth                                       objectMapperName, "GetObject");
20d31be2ccSJayanth Othayoth 
21d31be2ccSJayanth Othayoth     method.append(path);
22d31be2ccSJayanth Othayoth     method.append(std::vector<std::string>({interface}));
23d31be2ccSJayanth Othayoth 
24d31be2ccSJayanth Othayoth     std::vector<std::pair<std::string, std::vector<std::string>>> response;
25d31be2ccSJayanth Othayoth 
26d31be2ccSJayanth Othayoth     try
27d31be2ccSJayanth Othayoth     {
28d31be2ccSJayanth Othayoth         auto reply = bus.call(method);
29d31be2ccSJayanth Othayoth         reply.read(response);
30d31be2ccSJayanth Othayoth         if (response.empty())
31d31be2ccSJayanth Othayoth         {
32d31be2ccSJayanth Othayoth             log<level::ERR>("Error in mapper response for getting service name",
33d31be2ccSJayanth Othayoth                             entry("PATH=%s", path.c_str()),
34d31be2ccSJayanth Othayoth                             entry("INTERFACE=%s", interface.c_str()));
35d31be2ccSJayanth Othayoth             return std::string{};
36d31be2ccSJayanth Othayoth         }
37d31be2ccSJayanth Othayoth     }
38d31be2ccSJayanth Othayoth     catch (const sdbusplus::exception::SdBusError& e)
39d31be2ccSJayanth Othayoth     {
40d31be2ccSJayanth Othayoth         log<level::ERR>("Error in mapper method call",
41d31be2ccSJayanth Othayoth                         entry("ERROR=%s", e.what()),
42d31be2ccSJayanth Othayoth                         entry("PATH=%s", path.c_str()),
43d31be2ccSJayanth Othayoth                         entry("INTERFACE=%s", interface.c_str()));
44d31be2ccSJayanth Othayoth         return std::string{};
45d31be2ccSJayanth Othayoth     }
46d31be2ccSJayanth Othayoth     return response[0].first;
47d31be2ccSJayanth Othayoth }
48d31be2ccSJayanth Othayoth 
492279386bSRamesh Iyyar BootProgress getBootProgress()
502279386bSRamesh Iyyar {
512279386bSRamesh Iyyar     constexpr auto bootProgressInterface =
522279386bSRamesh Iyyar         "xyz.openbmc_project.State.Boot.Progress";
532279386bSRamesh Iyyar     // TODO Need to change host instance if multiple instead "0"
542279386bSRamesh Iyyar     constexpr auto hostStateObjPath = "/xyz/openbmc_project/state/host0";
552279386bSRamesh Iyyar 
562279386bSRamesh Iyyar     BootProgress bootProgessStage;
572279386bSRamesh Iyyar 
582279386bSRamesh Iyyar     try
592279386bSRamesh Iyyar     {
602279386bSRamesh Iyyar         auto bus = sdbusplus::bus::new_default();
612279386bSRamesh Iyyar         auto service = getService(bus, hostStateObjPath, bootProgressInterface);
622279386bSRamesh Iyyar 
632279386bSRamesh Iyyar         auto method =
642279386bSRamesh Iyyar             bus.new_method_call(service.c_str(), hostStateObjPath,
652279386bSRamesh Iyyar                                 "org.freedesktop.DBus.Properties", "Get");
662279386bSRamesh Iyyar 
672279386bSRamesh Iyyar         method.append(bootProgressInterface, "BootProgress");
682279386bSRamesh Iyyar 
692279386bSRamesh Iyyar         auto reply = bus.call(method);
702279386bSRamesh Iyyar 
712279386bSRamesh Iyyar         using DBusValue_t =
722279386bSRamesh Iyyar             std::variant<std::string, bool, std::vector<uint8_t>,
732279386bSRamesh Iyyar                          std::vector<std::string>>;
742279386bSRamesh Iyyar         DBusValue_t propertyVal;
752279386bSRamesh Iyyar 
762279386bSRamesh Iyyar         reply.read(propertyVal);
772279386bSRamesh Iyyar 
782279386bSRamesh Iyyar         // BootProgress property type is string
792279386bSRamesh Iyyar         std::string bootPgs(std::get<std::string>(propertyVal));
802279386bSRamesh Iyyar 
812279386bSRamesh Iyyar         bootProgessStage = sdbusplus::xyz::openbmc_project::State::Boot::
822279386bSRamesh Iyyar             server::Progress::convertProgressStagesFromString(bootPgs);
832279386bSRamesh Iyyar     }
842279386bSRamesh Iyyar     catch (const sdbusplus::exception::SdBusError& e)
852279386bSRamesh Iyyar     {
862279386bSRamesh Iyyar         log<level::ERR>("D-Bus call exception",
872279386bSRamesh Iyyar                         entry("OBJPATH=%s", hostStateObjPath),
882279386bSRamesh Iyyar                         entry("INTERFACE=%s", bootProgressInterface),
892279386bSRamesh Iyyar                         entry("EXCEPTION=%s", e.what()));
902279386bSRamesh Iyyar         throw std::runtime_error("Failed to get BootProgress stage");
912279386bSRamesh Iyyar     }
922279386bSRamesh Iyyar     catch (const std::bad_variant_access& e)
932279386bSRamesh Iyyar     {
942279386bSRamesh Iyyar         log<level::ERR>(
952279386bSRamesh Iyyar             "Exception raised while read BootProgress property value",
962279386bSRamesh Iyyar             entry("OBJPATH=%s", hostStateObjPath),
972279386bSRamesh Iyyar             entry("INTERFACE=%s", bootProgressInterface),
982279386bSRamesh Iyyar             entry("EXCEPTION=%s", e.what()));
992279386bSRamesh Iyyar         throw std::runtime_error("Failed to get BootProgress stage");
1002279386bSRamesh Iyyar     }
1012279386bSRamesh Iyyar 
1022279386bSRamesh Iyyar     return bootProgessStage;
1032279386bSRamesh Iyyar }
104*6a54d9afSDhruvaraj Subhashchandran 
105*6a54d9afSDhruvaraj Subhashchandran bool isHostRunning()
106*6a54d9afSDhruvaraj Subhashchandran {
107*6a54d9afSDhruvaraj Subhashchandran     // TODO #ibm-openbmc/dev/2858 Revisit the method for finding whether host
108*6a54d9afSDhruvaraj Subhashchandran     // is running.
109*6a54d9afSDhruvaraj Subhashchandran     BootProgress bootProgressStatus = phosphor::dump::getBootProgress();
110*6a54d9afSDhruvaraj Subhashchandran     if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
111*6a54d9afSDhruvaraj Subhashchandran         (bootProgressStatus == BootProgress::OSStart) ||
112*6a54d9afSDhruvaraj Subhashchandran         (bootProgressStatus == BootProgress::OSRunning))
113*6a54d9afSDhruvaraj Subhashchandran     {
114*6a54d9afSDhruvaraj Subhashchandran         return true;
115*6a54d9afSDhruvaraj Subhashchandran     }
116*6a54d9afSDhruvaraj Subhashchandran     return false;
117*6a54d9afSDhruvaraj Subhashchandran }
118d31be2ccSJayanth Othayoth } // namespace dump
119d31be2ccSJayanth Othayoth } // namespace phosphor
120