1 #pragma once
2 
3 #include "dump_manager.hpp"
4 
5 #include <fmt/core.h>
6 #include <systemd/sd-event.h>
7 #include <unistd.h>
8 
9 #include <phosphor-logging/elog-errors.hpp>
10 #include <phosphor-logging/elog.hpp>
11 #include <sdbusplus/bus.hpp>
12 #include <xyz/openbmc_project/Common/error.hpp>
13 #include <xyz/openbmc_project/Dump/Create/server.hpp>
14 #include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
15 
16 #include <memory>
17 
18 namespace phosphor
19 {
20 namespace dump
21 {
22 
23 using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
24     Progress::ProgressStages;
25 
26 using namespace phosphor::logging;
27 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
28 
29 /* Need a custom deleter for freeing up sd_event */
30 struct EventDeleter
31 {
32     void operator()(sd_event* event) const
33     {
34         event = sd_event_unref(event);
35     }
36 };
37 using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
38 
39 /** @struct CustomFd
40  *
41  *  RAII wrapper for file descriptor.
42  */
43 struct CustomFd
44 {
45   private:
46     /** @brief File descriptor */
47     int fd = -1;
48 
49   public:
50     CustomFd() = delete;
51     CustomFd(const CustomFd&) = delete;
52     CustomFd& operator=(const CustomFd&) = delete;
53     CustomFd(CustomFd&&) = delete;
54     CustomFd& operator=(CustomFd&&) = delete;
55 
56     /** @brief Saves File descriptor and uses it to do file operation
57      *
58      *  @param[in] fd - File descriptor
59      */
60     CustomFd(int fd) : fd(fd) {}
61 
62     ~CustomFd()
63     {
64         if (fd >= 0)
65         {
66             close(fd);
67         }
68     }
69 
70     int operator()() const
71     {
72         return fd;
73     }
74 };
75 
76 /**
77  * @brief Get the bus service
78  *
79  * @param[in] bus - Bus to attach to.
80  * @param[in] path - D-Bus path name.
81  * @param[in] interface - D-Bus interface name.
82  * @return the bus service as a string
83  **/
84 std::string getService(sdbusplus::bus_t& bus, const std::string& path,
85                        const std::string& interface);
86 
87 /**
88  * @brief Get the host boot progress stage
89  *
90  * @return BootProgress on success
91  *         Throw exception on failure
92  *
93  */
94 BootProgress getBootProgress();
95 
96 /**
97  * @brief Check whether host is running
98  *
99  * @return true if the host running else false.
100  *         Throw exception on failure.
101  */
102 bool isHostRunning();
103 
104 inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params,
105                                         std::string& originatorId,
106                                         originatorTypes& originatorType)
107 {
108     using InvalidArgument =
109         sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
110     using Argument = xyz::openbmc_project::Common::InvalidArgument;
111     using CreateParametersXYZ =
112         sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters;
113 
114     auto iter = params.find(
115         sdbusplus::xyz::openbmc_project::Dump::server::Create::
116             convertCreateParametersToString(CreateParametersXYZ::OriginatorId));
117     if (iter == params.end())
118     {
119         log<level::INFO>("OriginatorId is not provided");
120     }
121     else
122     {
123         try
124         {
125             originatorId = std::get<std::string>(iter->second);
126         }
127         catch (const std::bad_variant_access& e)
128         {
129             // Exception will be raised if the input is not string
130             log<level::ERR>(
131                 fmt::format(
132                     "An invalid  originatorId passed. It should be a string, "
133                     "errormsg({})",
134                     e.what())
135                     .c_str());
136             elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"),
137                                   Argument::ARGUMENT_VALUE("INVALID INPUT"));
138         }
139     }
140 
141     iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create::
142                            convertCreateParametersToString(
143                                CreateParametersXYZ::OriginatorType));
144     if (iter == params.end())
145     {
146         log<level::INFO>("OriginatorType is not provided. Replacing the string "
147                          "with the default value");
148         originatorType = originatorTypes::Internal;
149     }
150     else
151     {
152         try
153         {
154             std::string type = std::get<std::string>(iter->second);
155             originatorType = sdbusplus::xyz::openbmc_project::Common::server::
156                 OriginatedBy::convertOriginatorTypesFromString(type);
157         }
158         catch (const std::bad_variant_access& e)
159         {
160             // Exception will be raised if the input is not string
161             log<level::ERR>(fmt::format("An invalid originatorType passed, "
162                                         "errormsg({})",
163                                         e.what())
164                                 .c_str());
165             elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"),
166                                   Argument::ARGUMENT_VALUE("INVALID INPUT"));
167         }
168     }
169 }
170 
171 } // namespace dump
172 } // namespace phosphor
173