blob: 6661bd43333146cecfa9754cf17acdb1913c2471 [file] [log] [blame]
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05001#pragma once
2
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -05003#include "dump_manager.hpp"
4
5#include <fmt/core.h>
Jayanth Othayothd02153c2017-07-02 22:29:42 -05006#include <systemd/sd-event.h>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05007#include <unistd.h>
8
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -05009#include <com/ibm/Dump/Create/server.hpp>
10#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/elog.hpp>
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060012#include <sdbusplus/bus.hpp>
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050013#include <xyz/openbmc_project/Common/error.hpp>
14#include <xyz/openbmc_project/Dump/Create/server.hpp>
Ramesh Iyyar22793862020-12-04 04:03:03 -060015#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050016
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050017#include <memory>
18
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050019namespace phosphor
20{
21namespace dump
22{
23
Ramesh Iyyar22793862020-12-04 04:03:03 -060024using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
25 Progress::ProgressStages;
26
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050027using namespace phosphor::logging;
28using namespace sdbusplus::xyz::openbmc_project::Common::Error;
29
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050030/* Need a custom deleter for freeing up sd_event */
31struct EventDeleter
32{
33 void operator()(sd_event* event) const
34 {
35 event = sd_event_unref(event);
36 }
37};
38using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
39
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050040/** @struct CustomFd
41 *
42 * RAII wrapper for file descriptor.
43 */
44struct CustomFd
45{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050046 private:
47 /** @brief File descriptor */
48 int fd = -1;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050049
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050050 public:
51 CustomFd() = delete;
52 CustomFd(const CustomFd&) = delete;
53 CustomFd& operator=(const CustomFd&) = delete;
54 CustomFd(CustomFd&&) = delete;
55 CustomFd& operator=(CustomFd&&) = delete;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050056
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050057 /** @brief Saves File descriptor and uses it to do file operation
58 *
59 * @param[in] fd - File descriptor
60 */
61 CustomFd(int fd) : fd(fd)
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050062 {}
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050063
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050064 ~CustomFd()
65 {
66 if (fd >= 0)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050067 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050068 close(fd);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050069 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050070 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050071
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050072 int operator()() const
73 {
74 return fd;
75 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050076};
77
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060078/**
79 * @brief Get the bus service
80 *
81 * @param[in] bus - Bus to attach to.
82 * @param[in] path - D-Bus path name.
83 * @param[in] interface - D-Bus interface name.
84 * @return the bus service as a string
85 **/
Patrick Williams9b18bf22022-07-22 19:26:55 -050086std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060087 const std::string& interface);
88
Ramesh Iyyar22793862020-12-04 04:03:03 -060089/**
90 * @brief Get the host boot progress stage
91 *
92 * @return BootProgress on success
93 * Throw exception on failure
94 *
95 */
96BootProgress getBootProgress();
97
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -060098/**
99 * @brief Check whether host is running
100 *
101 * @return true if the host running else false.
102 * Throw exception on failure.
103 */
104bool isHostRunning();
105
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500106inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params,
107 std::string& originatorId,
108 originatorTypes& originatorType)
109{
110 using InvalidArgument =
111 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
112 using Argument = xyz::openbmc_project::Common::InvalidArgument;
113 using CreateParametersXYZ =
114 sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters;
115
116 auto iter = params.find(
117 sdbusplus::xyz::openbmc_project::Dump::server::Create::
118 convertCreateParametersToString(CreateParametersXYZ::OriginatorId));
119 if (iter == params.end())
120 {
121 log<level::INFO>("OriginatorId is not provided");
122 }
123 else
124 {
125 try
126 {
127 originatorId = std::get<std::string>(iter->second);
128 }
129 catch (const std::bad_variant_access& e)
130 {
131 // Exception will be raised if the input is not string
132 log<level::ERR>(
133 fmt::format(
134 "An invalid originatorId passed. It should be a string, "
135 "errormsg({})",
136 e.what())
137 .c_str());
138 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"),
139 Argument::ARGUMENT_VALUE("INVALID INPUT"));
140 }
141 }
142
143 iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create::
144 convertCreateParametersToString(
145 CreateParametersXYZ::OriginatorType));
146 if (iter == params.end())
147 {
148 log<level::INFO>("OriginatorType is not provided. Replacing the string "
149 "with the default value");
150 originatorType = originatorTypes::Internal;
151 }
152 else
153 {
154 try
155 {
156 std::string type = std::get<std::string>(iter->second);
157 originatorType = sdbusplus::xyz::openbmc_project::Common::server::
158 OriginatedBy::convertOriginatorTypesFromString(type);
159 }
160 catch (const std::bad_variant_access& e)
161 {
162 // Exception will be raised if the input is not string
163 log<level::ERR>(fmt::format("An invalid originatorType passed, "
164 "errormsg({})",
165 e.what())
166 .c_str());
167 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"),
168 Argument::ARGUMENT_VALUE("INVALID INPUT"));
169 }
170 }
171}
172
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500173} // namespace dump
174} // namespace phosphor