Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 3 | #include "dump_manager.hpp" |
| 4 | |
| 5 | #include <fmt/core.h> |
Jayanth Othayoth | d02153c | 2017-07-02 22:29:42 -0500 | [diff] [blame] | 6 | #include <systemd/sd-event.h> |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <phosphor-logging/elog.hpp> |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
| 13 | #include <xyz/openbmc_project/Dump/Create/server.hpp> |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 14 | #include <xyz/openbmc_project/State/Boot/Progress/server.hpp> |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 15 | #include <xyz/openbmc_project/State/Host/server.hpp> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 16 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 19 | namespace phosphor |
| 20 | { |
| 21 | namespace dump |
| 22 | { |
| 23 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 24 | using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server:: |
| 25 | Progress::ProgressStages; |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 26 | using HostState = |
| 27 | sdbusplus::xyz::openbmc_project::State::server::Host::HostState; |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 28 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 29 | using namespace phosphor::logging; |
| 30 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 31 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 32 | /* Need a custom deleter for freeing up sd_event */ |
| 33 | struct EventDeleter |
| 34 | { |
| 35 | void operator()(sd_event* event) const |
| 36 | { |
| 37 | event = sd_event_unref(event); |
| 38 | } |
| 39 | }; |
| 40 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 41 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 42 | /** @struct CustomFd |
| 43 | * |
| 44 | * RAII wrapper for file descriptor. |
| 45 | */ |
| 46 | struct CustomFd |
| 47 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 48 | private: |
| 49 | /** @brief File descriptor */ |
| 50 | int fd = -1; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 51 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 52 | 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 Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 58 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 59 | /** @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 Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 64 | {} |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 65 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 66 | ~CustomFd() |
| 67 | { |
| 68 | if (fd >= 0) |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 69 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 70 | close(fd); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 71 | } |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 72 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 73 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 74 | int operator()() const |
| 75 | { |
| 76 | return fd; |
| 77 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 80 | /** |
| 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 Williams | 9b18bf2 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 88 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 89 | const std::string& interface); |
| 90 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 91 | /** |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 92 | * @brief Get the host state |
| 93 | * |
| 94 | * @return HostState on success |
| 95 | * Throw exception on failure |
| 96 | * |
| 97 | */ |
| 98 | HostState getHostState(); |
| 99 | |
| 100 | /** |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 101 | * @brief Get the host boot progress stage |
| 102 | * |
| 103 | * @return BootProgress on success |
| 104 | * Throw exception on failure |
| 105 | * |
| 106 | */ |
| 107 | BootProgress getBootProgress(); |
| 108 | |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 109 | /** |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 110 | * @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 | */ |
| 119 | std::string getStateValue(const std::string& intf, const std::string& objPath, |
| 120 | const std::string& state); |
| 121 | |
| 122 | /** |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 123 | * @brief Check whether host is running |
| 124 | * |
| 125 | * @return true if the host running else false. |
| 126 | * Throw exception on failure. |
| 127 | */ |
| 128 | bool isHostRunning(); |
| 129 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 130 | inline 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 Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 197 | /** |
| 198 | * @brief Check whether host is quiesced |
| 199 | * |
| 200 | * @return true if the host is quiesced else false. |
| 201 | * Throw exception on failure. |
| 202 | */ |
| 203 | bool isHostQuiesced(); |
| 204 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 205 | } // namespace dump |
| 206 | } // namespace phosphor |