blob: 83952a311cdaa96f8fb1e43d04d191029e3df32e [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 <phosphor-logging/elog-errors.hpp>
10#include <phosphor-logging/elog.hpp>
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060011#include <sdbusplus/bus.hpp>
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050012#include <xyz/openbmc_project/Common/error.hpp>
13#include <xyz/openbmc_project/Dump/Create/server.hpp>
Ramesh Iyyar22793862020-12-04 04:03:03 -060014#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050015
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050016#include <memory>
17
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050018namespace phosphor
19{
20namespace dump
21{
22
Ramesh Iyyar22793862020-12-04 04:03:03 -060023using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
24 Progress::ProgressStages;
25
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050026using namespace phosphor::logging;
27using namespace sdbusplus::xyz::openbmc_project::Common::Error;
28
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050029/* Need a custom deleter for freeing up sd_event */
30struct EventDeleter
31{
32 void operator()(sd_event* event) const
33 {
34 event = sd_event_unref(event);
35 }
36};
37using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
38
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050039/** @struct CustomFd
40 *
41 * RAII wrapper for file descriptor.
42 */
43struct CustomFd
44{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050045 private:
46 /** @brief File descriptor */
47 int fd = -1;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050048
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050049 public:
50 CustomFd() = delete;
51 CustomFd(const CustomFd&) = delete;
52 CustomFd& operator=(const CustomFd&) = delete;
53 CustomFd(CustomFd&&) = delete;
54 CustomFd& operator=(CustomFd&&) = delete;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050055
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050056 /** @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)
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050061 {}
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050062
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050063 ~CustomFd()
64 {
65 if (fd >= 0)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050066 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050067 close(fd);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050068 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050069 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050070
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050071 int operator()() const
72 {
73 return fd;
74 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050075};
76
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060077/**
78 * @brief Get the bus service
79 *
80 * @param[in] bus - Bus to attach to.
81 * @param[in] path - D-Bus path name.
82 * @param[in] interface - D-Bus interface name.
83 * @return the bus service as a string
84 **/
Patrick Williams9b18bf22022-07-22 19:26:55 -050085std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060086 const std::string& interface);
87
Ramesh Iyyar22793862020-12-04 04:03:03 -060088/**
89 * @brief Get the host boot progress stage
90 *
91 * @return BootProgress on success
92 * Throw exception on failure
93 *
94 */
95BootProgress getBootProgress();
96
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -060097/**
98 * @brief Check whether host is running
99 *
100 * @return true if the host running else false.
101 * Throw exception on failure.
102 */
103bool isHostRunning();
104
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500105inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params,
106 std::string& originatorId,
107 originatorTypes& originatorType)
108{
109 using InvalidArgument =
110 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
111 using Argument = xyz::openbmc_project::Common::InvalidArgument;
112 using CreateParametersXYZ =
113 sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters;
114
115 auto iter = params.find(
116 sdbusplus::xyz::openbmc_project::Dump::server::Create::
117 convertCreateParametersToString(CreateParametersXYZ::OriginatorId));
118 if (iter == params.end())
119 {
120 log<level::INFO>("OriginatorId is not provided");
121 }
122 else
123 {
124 try
125 {
126 originatorId = std::get<std::string>(iter->second);
127 }
128 catch (const std::bad_variant_access& e)
129 {
130 // Exception will be raised if the input is not string
131 log<level::ERR>(
132 fmt::format(
133 "An invalid originatorId passed. It should be a string, "
134 "errormsg({})",
135 e.what())
136 .c_str());
137 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"),
138 Argument::ARGUMENT_VALUE("INVALID INPUT"));
139 }
140 }
141
142 iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create::
143 convertCreateParametersToString(
144 CreateParametersXYZ::OriginatorType));
145 if (iter == params.end())
146 {
147 log<level::INFO>("OriginatorType is not provided. Replacing the string "
148 "with the default value");
149 originatorType = originatorTypes::Internal;
150 }
151 else
152 {
153 try
154 {
155 std::string type = std::get<std::string>(iter->second);
156 originatorType = sdbusplus::xyz::openbmc_project::Common::server::
157 OriginatedBy::convertOriginatorTypesFromString(type);
158 }
159 catch (const std::bad_variant_access& e)
160 {
161 // Exception will be raised if the input is not string
162 log<level::ERR>(fmt::format("An invalid originatorType passed, "
163 "errormsg({})",
164 e.what())
165 .c_str());
166 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"),
167 Argument::ARGUMENT_VALUE("INVALID INPUT"));
168 }
169 }
170}
171
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500172} // namespace dump
173} // namespace phosphor