blob: 9f683adc647e186434847c243e42251f1bea6706 [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 */
Patrick Williams78e88402023-05-10 07:50:48 -050060 CustomFd(int fd) : fd(fd) {}
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050061
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050062 ~CustomFd()
63 {
64 if (fd >= 0)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050065 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050066 close(fd);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050067 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050068 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050069
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050070 int operator()() const
71 {
72 return fd;
73 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050074};
75
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060076/**
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 **/
Patrick Williams9b18bf22022-07-22 19:26:55 -050084std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060085 const std::string& interface);
86
Ramesh Iyyar22793862020-12-04 04:03:03 -060087/**
88 * @brief Get the host boot progress stage
89 *
90 * @return BootProgress on success
91 * Throw exception on failure
92 *
93 */
94BootProgress getBootProgress();
95
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -060096/**
97 * @brief Check whether host is running
98 *
99 * @return true if the host running else false.
100 * Throw exception on failure.
101 */
102bool isHostRunning();
103
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500104inline 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
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500171} // namespace dump
172} // namespace phosphor