blob: 93e8878ab6db87684c479861c5540c5541b66d4b [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>
Dhruvaraj Subhashchandran416c8ae2022-05-06 06:21:11 -05006#include <fmt/format.h>
Jayanth Othayothd02153c2017-07-02 22:29:42 -05007#include <systemd/sd-event.h>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05008#include <unistd.h>
9
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050010#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/elog.hpp>
Dhruvaraj Subhashchandran416c8ae2022-05-06 06:21:11 -050012#include <phosphor-logging/log.hpp>
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060013#include <sdbusplus/bus.hpp>
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050014#include <xyz/openbmc_project/Common/error.hpp>
15#include <xyz/openbmc_project/Dump/Create/server.hpp>
Ramesh Iyyar22793862020-12-04 04:03:03 -060016#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050017#include <xyz/openbmc_project/State/Host/server.hpp>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050018
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050019#include <memory>
20
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050021namespace phosphor
22{
23namespace dump
24{
25
Ramesh Iyyar22793862020-12-04 04:03:03 -060026using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
27 Progress::ProgressStages;
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050028using HostState =
29 sdbusplus::xyz::openbmc_project::State::server::Host::HostState;
Ramesh Iyyar22793862020-12-04 04:03:03 -060030
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050031using namespace phosphor::logging;
32using namespace sdbusplus::xyz::openbmc_project::Common::Error;
33
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050034/* Need a custom deleter for freeing up sd_event */
35struct EventDeleter
36{
37 void operator()(sd_event* event) const
38 {
39 event = sd_event_unref(event);
40 }
41};
42using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
43
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050044/** @struct CustomFd
45 *
46 * RAII wrapper for file descriptor.
47 */
48struct CustomFd
49{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050050 private:
51 /** @brief File descriptor */
52 int fd = -1;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050053
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050054 public:
55 CustomFd() = delete;
56 CustomFd(const CustomFd&) = delete;
57 CustomFd& operator=(const CustomFd&) = delete;
58 CustomFd(CustomFd&&) = delete;
59 CustomFd& operator=(CustomFd&&) = delete;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050060
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050061 /** @brief Saves File descriptor and uses it to do file operation
62 *
63 * @param[in] fd - File descriptor
64 */
65 CustomFd(int fd) : fd(fd)
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050066 {}
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050067
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050068 ~CustomFd()
69 {
70 if (fd >= 0)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050071 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050072 close(fd);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050073 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050074 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050075
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050076 int operator()() const
77 {
78 return fd;
79 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050080};
81
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060082/**
83 * @brief Get the bus service
84 *
85 * @param[in] bus - Bus to attach to.
86 * @param[in] path - D-Bus path name.
87 * @param[in] interface - D-Bus interface name.
88 * @return the bus service as a string
89 **/
Patrick Williams9b18bf22022-07-22 19:26:55 -050090std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060091 const std::string& interface);
92
Ramesh Iyyar22793862020-12-04 04:03:03 -060093/**
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -050094 * @brief Get the host state
95 *
96 * @return HostState on success
97 * Throw exception on failure
98 *
99 */
100HostState getHostState();
101
102/**
Ramesh Iyyar22793862020-12-04 04:03:03 -0600103 * @brief Get the host boot progress stage
104 *
105 * @return BootProgress on success
106 * Throw exception on failure
107 *
108 */
109BootProgress getBootProgress();
110
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600111/**
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500112 * @brief Get the host state value
113 *
114 * @param[in] intf - Interface to get the value
115 * @param[in] objPath - Object path of the service
116 * @param[in] state - State name to get
117 *
118 * @return The state value on success
119 * Throw exception on failure
120 */
121std::string getStateValue(const std::string& intf, const std::string& objPath,
122 const std::string& state);
123
124/**
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600125 * @brief Check whether host is running
126 *
127 * @return true if the host running else false.
128 * Throw exception on failure.
129 */
130bool isHostRunning();
131
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500132inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params,
133 std::string& originatorId,
134 originatorTypes& originatorType)
135{
136 using InvalidArgument =
137 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
138 using Argument = xyz::openbmc_project::Common::InvalidArgument;
139 using CreateParametersXYZ =
140 sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters;
141
142 auto iter = params.find(
143 sdbusplus::xyz::openbmc_project::Dump::server::Create::
144 convertCreateParametersToString(CreateParametersXYZ::OriginatorId));
145 if (iter == params.end())
146 {
147 log<level::INFO>("OriginatorId is not provided");
148 }
149 else
150 {
151 try
152 {
153 originatorId = std::get<std::string>(iter->second);
154 }
155 catch (const std::bad_variant_access& e)
156 {
157 // Exception will be raised if the input is not string
158 log<level::ERR>(
159 fmt::format(
160 "An invalid originatorId passed. It should be a string, "
161 "errormsg({})",
162 e.what())
163 .c_str());
164 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"),
165 Argument::ARGUMENT_VALUE("INVALID INPUT"));
166 }
167 }
168
169 iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create::
170 convertCreateParametersToString(
171 CreateParametersXYZ::OriginatorType));
172 if (iter == params.end())
173 {
174 log<level::INFO>("OriginatorType is not provided. Replacing the string "
175 "with the default value");
176 originatorType = originatorTypes::Internal;
177 }
178 else
179 {
180 try
181 {
182 std::string type = std::get<std::string>(iter->second);
183 originatorType = sdbusplus::xyz::openbmc_project::Common::server::
184 OriginatedBy::convertOriginatorTypesFromString(type);
185 }
186 catch (const std::bad_variant_access& e)
187 {
188 // Exception will be raised if the input is not string
189 log<level::ERR>(fmt::format("An invalid originatorType passed, "
190 "errormsg({})",
191 e.what())
192 .c_str());
193 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"),
194 Argument::ARGUMENT_VALUE("INVALID INPUT"));
195 }
196 }
197}
198
Dhruvaraj Subhashchandrana91cc842022-03-22 08:06:20 -0500199/**
200 * @brief Check whether host is quiesced
201 *
202 * @return true if the host is quiesced else false.
203 * Throw exception on failure.
204 */
205bool isHostQuiesced();
206
Dhruvaraj Subhashchandran416c8ae2022-05-06 06:21:11 -0500207/**
208 * @brief Read property value from the specified object and interface
209 * @param[in] bus D-Bus handle
210 * @param[in] service service which has implemented the interface
211 * @param[in] object object having has implemented the interface
212 * @param[in] intf interface having the property
213 * @param[in] prop name of the property to read
214 * @return property value
215 */
216template <typename T>
217T readDBusProperty(sdbusplus::bus::bus& bus, const std::string& service,
218 const std::string& object, const std::string& intf,
219 const std::string& prop)
220{
221 using ::phosphor::logging::level;
222 using ::phosphor::logging::log;
223 T retVal{};
224 try
225 {
226 auto properties =
227 bus.new_method_call(service.c_str(), object.c_str(),
228 "org.freedesktop.DBus.Properties", "Get");
229 properties.append(intf);
230 properties.append(prop);
231 auto result = bus.call(properties);
232 result.read(retVal);
233 }
234 catch (const std::exception& ex)
235 {
236 log<level::ERR>(
237 fmt::format("Failed to get the property ({}) interface ({}) "
238 "object path ({}) error ({}) ",
239 prop.c_str(), intf.c_str(), object.c_str(), ex.what())
240 .c_str());
241 throw;
242 }
243 return retVal;
244}
245
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500246} // namespace dump
247} // namespace phosphor