xref: /openbmc/phosphor-debug-collector/dump_utils.cpp (revision d1f670fe7ea219c643f8b630e8c2d7b333f16e35)
1d31be2ccSJayanth Othayoth #include "dump_utils.hpp"
2d31be2ccSJayanth Othayoth 
3*d1f670feSDhruvaraj Subhashchandran #include <phosphor-logging/lg2.hpp>
4d31be2ccSJayanth Othayoth #include <phosphor-logging/log.hpp>
5d31be2ccSJayanth Othayoth 
6d31be2ccSJayanth Othayoth namespace phosphor
7d31be2ccSJayanth Othayoth {
8d31be2ccSJayanth Othayoth namespace dump
9d31be2ccSJayanth Othayoth {
10d31be2ccSJayanth Othayoth 
112279386bSRamesh Iyyar using namespace phosphor::logging;
122279386bSRamesh Iyyar 
139b18bf2dSPatrick Williams std::string getService(sdbusplus::bus_t& bus, const std::string& path,
14d31be2ccSJayanth Othayoth                        const std::string& interface)
15d31be2ccSJayanth Othayoth {
16d31be2ccSJayanth Othayoth     constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper";
17d31be2ccSJayanth Othayoth     constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper";
18d31be2ccSJayanth Othayoth 
19d31be2ccSJayanth Othayoth     auto method = bus.new_method_call(objectMapperName, objectMapperPath,
20d31be2ccSJayanth Othayoth                                       objectMapperName, "GetObject");
21d31be2ccSJayanth Othayoth 
22d31be2ccSJayanth Othayoth     method.append(path);
23d31be2ccSJayanth Othayoth     method.append(std::vector<std::string>({interface}));
24d31be2ccSJayanth Othayoth 
25d31be2ccSJayanth Othayoth     std::vector<std::pair<std::string, std::vector<std::string>>> response;
26d31be2ccSJayanth Othayoth 
27d31be2ccSJayanth Othayoth     try
28d31be2ccSJayanth Othayoth     {
29d31be2ccSJayanth Othayoth         auto reply = bus.call(method);
30d31be2ccSJayanth Othayoth         reply.read(response);
31d31be2ccSJayanth Othayoth         if (response.empty())
32d31be2ccSJayanth Othayoth         {
33*d1f670feSDhruvaraj Subhashchandran             lg2::error(
34*d1f670feSDhruvaraj Subhashchandran                 "Error in mapper response for getting service name, PATH: "
35*d1f670feSDhruvaraj Subhashchandran                 "{PATH}, INTERFACE: {INTERFACE}",
36*d1f670feSDhruvaraj Subhashchandran                 "PATH", path, "INTERFACE", interface);
37d31be2ccSJayanth Othayoth             return std::string{};
38d31be2ccSJayanth Othayoth         }
39d31be2ccSJayanth Othayoth     }
409b18bf2dSPatrick Williams     catch (const sdbusplus::exception_t& e)
41d31be2ccSJayanth Othayoth     {
42*d1f670feSDhruvaraj Subhashchandran         lg2::error("Error in mapper method call, errormsg: {ERROR}, "
43*d1f670feSDhruvaraj Subhashchandran                    "PATH: {PATH}, INTERFACE: {INTERFACE}",
44*d1f670feSDhruvaraj Subhashchandran                    "ERROR", e, "PATH", path, "INTERFACE", interface);
45d31be2ccSJayanth Othayoth         return std::string{};
46d31be2ccSJayanth Othayoth     }
47d31be2ccSJayanth Othayoth     return response[0].first;
48d31be2ccSJayanth Othayoth }
49d31be2ccSJayanth Othayoth 
502279386bSRamesh Iyyar BootProgress getBootProgress()
512279386bSRamesh Iyyar {
522279386bSRamesh Iyyar     constexpr auto bootProgressInterface =
532279386bSRamesh Iyyar         "xyz.openbmc_project.State.Boot.Progress";
542279386bSRamesh Iyyar     // TODO Need to change host instance if multiple instead "0"
552279386bSRamesh Iyyar     constexpr auto hostStateObjPath = "/xyz/openbmc_project/state/host0";
562279386bSRamesh Iyyar 
572279386bSRamesh Iyyar     BootProgress bootProgessStage;
582279386bSRamesh Iyyar 
592279386bSRamesh Iyyar     try
602279386bSRamesh Iyyar     {
612279386bSRamesh Iyyar         auto bus = sdbusplus::bus::new_default();
622279386bSRamesh Iyyar         auto service = getService(bus, hostStateObjPath, bootProgressInterface);
632279386bSRamesh Iyyar 
6478e88402SPatrick Williams         auto method = bus.new_method_call(service.c_str(), hostStateObjPath,
6578e88402SPatrick Williams                                           "org.freedesktop.DBus.Properties",
6678e88402SPatrick Williams                                           "Get");
672279386bSRamesh Iyyar 
682279386bSRamesh Iyyar         method.append(bootProgressInterface, "BootProgress");
692279386bSRamesh Iyyar 
702279386bSRamesh Iyyar         auto reply = bus.call(method);
712279386bSRamesh Iyyar 
722279386bSRamesh Iyyar         using DBusValue_t =
732279386bSRamesh Iyyar             std::variant<std::string, bool, std::vector<uint8_t>,
742279386bSRamesh Iyyar                          std::vector<std::string>>;
752279386bSRamesh Iyyar         DBusValue_t propertyVal;
762279386bSRamesh Iyyar 
772279386bSRamesh Iyyar         reply.read(propertyVal);
782279386bSRamesh Iyyar 
792279386bSRamesh Iyyar         // BootProgress property type is string
802279386bSRamesh Iyyar         std::string bootPgs(std::get<std::string>(propertyVal));
812279386bSRamesh Iyyar 
822279386bSRamesh Iyyar         bootProgessStage = sdbusplus::xyz::openbmc_project::State::Boot::
832279386bSRamesh Iyyar             server::Progress::convertProgressStagesFromString(bootPgs);
842279386bSRamesh Iyyar     }
859b18bf2dSPatrick Williams     catch (const sdbusplus::exception_t& e)
862279386bSRamesh Iyyar     {
87*d1f670feSDhruvaraj Subhashchandran         lg2::error("D-Bus call exception, OBJPATH: {OBJ_PATH}, "
88*d1f670feSDhruvaraj Subhashchandran                    "INTERFACE: {INTERFACE}, EXCEPTION: {ERROR}",
89*d1f670feSDhruvaraj Subhashchandran                    "OBJ_PATH", hostStateObjPath, "INTERFACE",
90*d1f670feSDhruvaraj Subhashchandran                    bootProgressInterface, "ERROR", e);
912279386bSRamesh Iyyar         throw std::runtime_error("Failed to get BootProgress stage");
922279386bSRamesh Iyyar     }
932279386bSRamesh Iyyar     catch (const std::bad_variant_access& e)
942279386bSRamesh Iyyar     {
95*d1f670feSDhruvaraj Subhashchandran         lg2::error("Exception raised while read BootProgress property value, "
96*d1f670feSDhruvaraj Subhashchandran                    "OBJPATH: {OBJ_PATH}, INTERFACE: {INTERFACE}, "
97*d1f670feSDhruvaraj Subhashchandran                    "EXCEPTION: {ERROR}",
98*d1f670feSDhruvaraj Subhashchandran                    "OBJ_PATH", hostStateObjPath, "INTERFACE",
99*d1f670feSDhruvaraj Subhashchandran                    bootProgressInterface, "ERROR", e);
1002279386bSRamesh Iyyar         throw std::runtime_error("Failed to get BootProgress stage");
1012279386bSRamesh Iyyar     }
1022279386bSRamesh Iyyar 
1032279386bSRamesh Iyyar     return bootProgessStage;
1042279386bSRamesh Iyyar }
1056a54d9afSDhruvaraj Subhashchandran 
1066a54d9afSDhruvaraj Subhashchandran bool isHostRunning()
1076a54d9afSDhruvaraj Subhashchandran {
1086a54d9afSDhruvaraj Subhashchandran     // TODO #ibm-openbmc/dev/2858 Revisit the method for finding whether host
1096a54d9afSDhruvaraj Subhashchandran     // is running.
1106a54d9afSDhruvaraj Subhashchandran     BootProgress bootProgressStatus = phosphor::dump::getBootProgress();
1116a54d9afSDhruvaraj Subhashchandran     if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
1128a9736b3SRavi Teja         (bootProgressStatus == BootProgress::SystemSetup) ||
1136a54d9afSDhruvaraj Subhashchandran         (bootProgressStatus == BootProgress::OSStart) ||
114706ae1b5SDhruvaraj Subhashchandran         (bootProgressStatus == BootProgress::OSRunning) ||
115706ae1b5SDhruvaraj Subhashchandran         (bootProgressStatus == BootProgress::PCIInit))
1166a54d9afSDhruvaraj Subhashchandran     {
1176a54d9afSDhruvaraj Subhashchandran         return true;
1186a54d9afSDhruvaraj Subhashchandran     }
1196a54d9afSDhruvaraj Subhashchandran     return false;
1206a54d9afSDhruvaraj Subhashchandran }
121d31be2ccSJayanth Othayoth } // namespace dump
122d31be2ccSJayanth Othayoth } // namespace phosphor
123