blob: 6e923bc1a9c196d0e22d85377e129c2ea4ff611d [file] [log] [blame]
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05001#pragma once
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -05002#include "dump_manager.hpp"
3
Jayanth Othayothd02153c2017-07-02 22:29:42 -05004#include <systemd/sd-event.h>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05005#include <unistd.h>
6
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -05007#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/elog.hpp>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -05009#include <phosphor-logging/lg2.hpp>
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060010#include <sdbusplus/bus.hpp>
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050011#include <xyz/openbmc_project/Common/error.hpp>
12#include <xyz/openbmc_project/Dump/Create/server.hpp>
Ramesh Iyyar22793862020-12-04 04:03:03 -060013#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050014
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050015#include <memory>
16
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050017namespace phosphor
18{
19namespace dump
20{
21
Ramesh Iyyar22793862020-12-04 04:03:03 -060022using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
23 Progress::ProgressStages;
24
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050025using namespace phosphor::logging;
26using namespace sdbusplus::xyz::openbmc_project::Common::Error;
27
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050028/* Need a custom deleter for freeing up sd_event */
29struct EventDeleter
30{
31 void operator()(sd_event* event) const
32 {
33 event = sd_event_unref(event);
34 }
35};
36using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
37
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050038/** @struct CustomFd
39 *
40 * RAII wrapper for file descriptor.
41 */
42struct CustomFd
43{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050044 private:
45 /** @brief File descriptor */
46 int fd = -1;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050047
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050048 public:
49 CustomFd() = delete;
50 CustomFd(const CustomFd&) = delete;
51 CustomFd& operator=(const CustomFd&) = delete;
52 CustomFd(CustomFd&&) = delete;
53 CustomFd& operator=(CustomFd&&) = delete;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050054
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050055 /** @brief Saves File descriptor and uses it to do file operation
56 *
57 * @param[in] fd - File descriptor
58 */
Patrick Williams78e88402023-05-10 07:50:48 -050059 CustomFd(int fd) : fd(fd) {}
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050060
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050061 ~CustomFd()
62 {
63 if (fd >= 0)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050064 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050065 close(fd);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050066 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050067 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050068
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050069 int operator()() const
70 {
71 return fd;
72 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050073};
74
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060075/**
76 * @brief Get the bus service
77 *
78 * @param[in] bus - Bus to attach to.
79 * @param[in] path - D-Bus path name.
80 * @param[in] interface - D-Bus interface name.
81 * @return the bus service as a string
82 **/
Patrick Williams9b18bf22022-07-22 19:26:55 -050083std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060084 const std::string& interface);
85
Ramesh Iyyar22793862020-12-04 04:03:03 -060086/**
87 * @brief Get the host boot progress stage
88 *
89 * @return BootProgress on success
90 * Throw exception on failure
91 *
92 */
93BootProgress getBootProgress();
94
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -060095/**
96 * @brief Check whether host is running
97 *
98 * @return true if the host running else false.
99 * Throw exception on failure.
100 */
101bool isHostRunning();
102
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500103inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params,
104 std::string& originatorId,
105 originatorTypes& originatorType)
106{
107 using InvalidArgument =
108 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
109 using Argument = xyz::openbmc_project::Common::InvalidArgument;
110 using CreateParametersXYZ =
111 sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters;
112
113 auto iter = params.find(
114 sdbusplus::xyz::openbmc_project::Dump::server::Create::
115 convertCreateParametersToString(CreateParametersXYZ::OriginatorId));
116 if (iter == params.end())
117 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500118 lg2::info("OriginatorId is not provided");
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500119 }
120 else
121 {
122 try
123 {
124 originatorId = std::get<std::string>(iter->second);
125 }
126 catch (const std::bad_variant_access& e)
127 {
128 // Exception will be raised if the input is not string
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500129 lg2::error("An invalid originatorId passed. It should be a string, "
130 "errormsg: {ERROR}",
131 "ERROR", e);
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500132 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"),
133 Argument::ARGUMENT_VALUE("INVALID INPUT"));
134 }
135 }
136
137 iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create::
138 convertCreateParametersToString(
139 CreateParametersXYZ::OriginatorType));
140 if (iter == params.end())
141 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500142 lg2::info("OriginatorType is not provided. Replacing the string "
143 "with the default value");
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500144 originatorType = originatorTypes::Internal;
145 }
146 else
147 {
148 try
149 {
150 std::string type = std::get<std::string>(iter->second);
151 originatorType = sdbusplus::xyz::openbmc_project::Common::server::
152 OriginatedBy::convertOriginatorTypesFromString(type);
153 }
154 catch (const std::bad_variant_access& e)
155 {
156 // Exception will be raised if the input is not string
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500157 lg2::error("An invalid originatorType passed, errormsg: {ERROR}",
158 "ERROR", e);
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500159 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"),
160 Argument::ARGUMENT_VALUE("INVALID INPUT"));
161 }
162 }
163}
164
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500165} // namespace dump
166} // namespace phosphor