blob: 82fff10c8992bfc2dbf7f483cc2b0aab44a6a6fe [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>
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050015#include <xyz/openbmc_project/State/Host/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;
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050026using HostState =
27 sdbusplus::xyz::openbmc_project::State::server::Host::HostState;
Ramesh Iyyar22793862020-12-04 04:03:03 -060028
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050029using namespace phosphor::logging;
30using namespace sdbusplus::xyz::openbmc_project::Common::Error;
31
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050032/* Need a custom deleter for freeing up sd_event */
33struct EventDeleter
34{
35 void operator()(sd_event* event) const
36 {
37 event = sd_event_unref(event);
38 }
39};
40using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
41
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050042/** @struct CustomFd
43 *
44 * RAII wrapper for file descriptor.
45 */
46struct CustomFd
47{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050048 private:
49 /** @brief File descriptor */
50 int fd = -1;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050051
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050052 public:
53 CustomFd() = delete;
54 CustomFd(const CustomFd&) = delete;
55 CustomFd& operator=(const CustomFd&) = delete;
56 CustomFd(CustomFd&&) = delete;
57 CustomFd& operator=(CustomFd&&) = delete;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050058
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050059 /** @brief Saves File descriptor and uses it to do file operation
60 *
61 * @param[in] fd - File descriptor
62 */
63 CustomFd(int fd) : fd(fd)
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050064 {}
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050065
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050066 ~CustomFd()
67 {
68 if (fd >= 0)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050069 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050070 close(fd);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050071 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050072 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050073
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050074 int operator()() const
75 {
76 return fd;
77 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050078};
79
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060080/**
81 * @brief Get the bus service
82 *
83 * @param[in] bus - Bus to attach to.
84 * @param[in] path - D-Bus path name.
85 * @param[in] interface - D-Bus interface name.
86 * @return the bus service as a string
87 **/
Patrick Williams9b18bf22022-07-22 19:26:55 -050088std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060089 const std::string& interface);
90
Ramesh Iyyar22793862020-12-04 04:03:03 -060091/**
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050092 * @brief Get the host state
93 *
94 * @return HostState on success
95 * Throw exception on failure
96 *
97 */
98HostState getHostState();
99
100/**
Ramesh Iyyar22793862020-12-04 04:03:03 -0600101 * @brief Get the host boot progress stage
102 *
103 * @return BootProgress on success
104 * Throw exception on failure
105 *
106 */
107BootProgress getBootProgress();
108
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600109/**
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500110 * @brief Get the host state value
111 *
112 * @param[in] intf - Interface to get the value
113 * @param[in] objPath - Object path of the service
114 * @param[in] state - State name to get
115 *
116 * @return The state value on success
117 * Throw exception on failure
118 */
119std::string getStateValue(const std::string& intf, const std::string& objPath,
120 const std::string& state);
121
122/**
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600123 * @brief Check whether host is running
124 *
125 * @return true if the host running else false.
126 * Throw exception on failure.
127 */
128bool isHostRunning();
129
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500130inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params,
131 std::string& originatorId,
132 originatorTypes& originatorType)
133{
134 using InvalidArgument =
135 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
136 using Argument = xyz::openbmc_project::Common::InvalidArgument;
137 using CreateParametersXYZ =
138 sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters;
139
140 auto iter = params.find(
141 sdbusplus::xyz::openbmc_project::Dump::server::Create::
142 convertCreateParametersToString(CreateParametersXYZ::OriginatorId));
143 if (iter == params.end())
144 {
145 log<level::INFO>("OriginatorId is not provided");
146 }
147 else
148 {
149 try
150 {
151 originatorId = std::get<std::string>(iter->second);
152 }
153 catch (const std::bad_variant_access& e)
154 {
155 // Exception will be raised if the input is not string
156 log<level::ERR>(
157 fmt::format(
158 "An invalid originatorId passed. It should be a string, "
159 "errormsg({})",
160 e.what())
161 .c_str());
162 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"),
163 Argument::ARGUMENT_VALUE("INVALID INPUT"));
164 }
165 }
166
167 iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create::
168 convertCreateParametersToString(
169 CreateParametersXYZ::OriginatorType));
170 if (iter == params.end())
171 {
172 log<level::INFO>("OriginatorType is not provided. Replacing the string "
173 "with the default value");
174 originatorType = originatorTypes::Internal;
175 }
176 else
177 {
178 try
179 {
180 std::string type = std::get<std::string>(iter->second);
181 originatorType = sdbusplus::xyz::openbmc_project::Common::server::
182 OriginatedBy::convertOriginatorTypesFromString(type);
183 }
184 catch (const std::bad_variant_access& e)
185 {
186 // Exception will be raised if the input is not string
187 log<level::ERR>(fmt::format("An invalid originatorType passed, "
188 "errormsg({})",
189 e.what())
190 .c_str());
191 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"),
192 Argument::ARGUMENT_VALUE("INVALID INPUT"));
193 }
194 }
195}
196
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500197/**
198 * @brief Check whether host is quiesced
199 *
200 * @return true if the host is quiesced else false.
201 * Throw exception on failure.
202 */
203bool isHostQuiesced();
204
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500205} // namespace dump
206} // namespace phosphor