1 #pragma once
2 
3 #include "dump_utils.hpp"
4 #include "sbe_consts.hpp"
5 
6 #include <sys/wait.h>
7 #include <unistd.h>
8 
9 #include <phosphor-logging/lg2.hpp>
10 #include <sdbusplus/bus.hpp>
11 #include <sdbusplus/bus/match.hpp>
12 #include <xyz/openbmc_project/Common/Progress/common.hpp>
13 
14 #include <iostream>
15 #include <string>
16 #include <variant>
17 
18 namespace openpower::dump
19 {
20 
21 using PropertyMap = std::map<std::string, std::variant<uint32_t, std::string>>;
22 using InterfaceMap = std::map<std::string, PropertyMap>;
23 /**
24  * @class DumpMonitor
25  * @brief Monitors DBus signals for dump creation and handles them.
26  */
27 class DumpMonitor
28 {
29   public:
30     /**
31      * @brief Constructor for DumpMonitor.
32      * Initializes the DBus connection and signal match for monitoring dump
33      * creation.
34      */
35     DumpMonitor() :
36         bus(sdbusplus::bus::new_default()),
37         match(bus,
38               sdbusplus::bus::match::rules::interfacesAdded(
39                   "/xyz/openbmc_project/dump") +
40                   sdbusplus::bus::match::rules::sender(
41                       "xyz.openbmc_project.Dump.Manager"),
42               [this](sdbusplus::message_t& msg) { handleDBusSignal(msg); })
43     {}
44 
45     /**
46      * @brief Runs the monitor to continuously listen for DBus signals.
47      */
48     void run()
49     {
50         bus.process_loop();
51     }
52 
53   private:
54     /* @brief sdbusplus handler for a bus to use */
55     sdbusplus::bus_t bus;
56 
57     /* @brief Monitores dump interfaces */
58     const std::vector<std::string> monitoredInterfaces = {
59         "com.ibm.Dump.Entry.Hardware", "com.ibm.Dump.Entry.Hostboot",
60         "com.ibm.Dump.Entry.SBE"};
61 
62     /* @brief InterfaceAdded match */
63     sdbusplus::bus::match_t match;
64 
65     /**
66      * @brief Handles the received DBus signal for dump creation.
67      * @param[in] msg - The DBus message received.
68      */
69     void handleDBusSignal(sdbusplus::message::message& msg);
70 
71     /**
72      * @brief Checks if the dump creation is in progress.
73      * @param[in] interfaces - The map of interfaces and their properties.
74      * @return True if the dump is in progress, false otherwise.
75      */
76     inline bool isInProgress(const InterfaceMap& interfaces)
77     {
78         using namespace sdbusplus::common::xyz::openbmc_project::common;
79         auto progressIt = interfaces.find(Progress::interface);
80         if (progressIt != interfaces.end())
81         {
82             auto statusIt = progressIt->second.find("Status");
83             if (statusIt != progressIt->second.end())
84             {
85                 std::string status = std::get<std::string>(statusIt->second);
86                 return status == Progress::convertOperationStatusToString(
87                                      Progress::OperationStatus::InProgress);
88             }
89         }
90         return false;
91     }
92 
93     /**
94      * @brief Executes the script to collect the dump.
95      * @param[in] path - The object path of the dump entry.
96      * @param[in] properties - The properties of the dump entry.
97      */
98     void executeCollectionScript(const sdbusplus::message::object_path& path,
99                                  const PropertyMap& properties);
100 
101     /**
102      * @brief Updates the progress status of the dump.
103      * @param[in] path - The object path of the dump entry.
104      * @param[in] status - The status to be set.
105      */
106     void updateProgressStatus(const std::string& path,
107                               const std::string& status);
108 
109     /**
110      * @brief Gets the dump type from the dump ID.
111      * @param[in] id - The dump ID.
112      * @return The dump type.
113      */
114     inline uint32_t getDumpTypeFromId(uint32_t id)
115     {
116         using namespace openpower::dump::SBE;
117         uint8_t type = (id >> 28) & 0xF;
118         if (type == 0)
119         {
120             return SBE_DUMP_TYPE_HARDWARE;
121         }
122         else if (type == 2)
123         {
124             return SBE_DUMP_TYPE_HOSTBOOT;
125         }
126         else if (type == 3)
127         {
128             return SBE_DUMP_TYPE_SBE;
129         }
130         return 0;
131     }
132 };
133 
134 } // namespace openpower::dump
135