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> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 15 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace dump |
| 21 | { |
| 22 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 23 | using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server:: |
| 24 | Progress::ProgressStages; |
| 25 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 26 | using namespace phosphor::logging; |
| 27 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 28 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 29 | /* Need a custom deleter for freeing up sd_event */ |
| 30 | struct EventDeleter |
| 31 | { |
| 32 | void operator()(sd_event* event) const |
| 33 | { |
| 34 | event = sd_event_unref(event); |
| 35 | } |
| 36 | }; |
| 37 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 38 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 39 | /** @struct CustomFd |
| 40 | * |
| 41 | * RAII wrapper for file descriptor. |
| 42 | */ |
| 43 | struct CustomFd |
| 44 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 45 | private: |
| 46 | /** @brief File descriptor */ |
| 47 | int fd = -1; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 48 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 49 | 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 Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 55 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 56 | /** @brief Saves File descriptor and uses it to do file operation |
| 57 | * |
| 58 | * @param[in] fd - File descriptor |
| 59 | */ |
| 60 | CustomFd(int fd) : fd(fd) |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 61 | {} |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 62 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 63 | ~CustomFd() |
| 64 | { |
| 65 | if (fd >= 0) |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 66 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 67 | close(fd); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 68 | } |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 69 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 70 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 71 | int operator()() const |
| 72 | { |
| 73 | return fd; |
| 74 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 77 | /** |
| 78 | * @brief Get the bus service |
| 79 | * |
| 80 | * @param[in] bus - Bus to attach to. |
| 81 | * @param[in] path - D-Bus path name. |
| 82 | * @param[in] interface - D-Bus interface name. |
| 83 | * @return the bus service as a string |
| 84 | **/ |
Patrick Williams | 9b18bf2 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 85 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 86 | const std::string& interface); |
| 87 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 88 | /** |
| 89 | * @brief Get the host boot progress stage |
| 90 | * |
| 91 | * @return BootProgress on success |
| 92 | * Throw exception on failure |
| 93 | * |
| 94 | */ |
| 95 | BootProgress getBootProgress(); |
| 96 | |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 97 | /** |
| 98 | * @brief Check whether host is running |
| 99 | * |
| 100 | * @return true if the host running else false. |
| 101 | * Throw exception on failure. |
| 102 | */ |
| 103 | bool isHostRunning(); |
| 104 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 105 | inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params, |
| 106 | std::string& originatorId, |
| 107 | originatorTypes& originatorType) |
| 108 | { |
| 109 | using InvalidArgument = |
| 110 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 111 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 112 | using CreateParametersXYZ = |
| 113 | sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters; |
| 114 | |
| 115 | auto iter = params.find( |
| 116 | sdbusplus::xyz::openbmc_project::Dump::server::Create:: |
| 117 | convertCreateParametersToString(CreateParametersXYZ::OriginatorId)); |
| 118 | if (iter == params.end()) |
| 119 | { |
| 120 | log<level::INFO>("OriginatorId is not provided"); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | try |
| 125 | { |
| 126 | originatorId = std::get<std::string>(iter->second); |
| 127 | } |
| 128 | catch (const std::bad_variant_access& e) |
| 129 | { |
| 130 | // Exception will be raised if the input is not string |
| 131 | log<level::ERR>( |
| 132 | fmt::format( |
| 133 | "An invalid originatorId passed. It should be a string, " |
| 134 | "errormsg({})", |
| 135 | e.what()) |
| 136 | .c_str()); |
| 137 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"), |
| 138 | Argument::ARGUMENT_VALUE("INVALID INPUT")); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create:: |
| 143 | convertCreateParametersToString( |
| 144 | CreateParametersXYZ::OriginatorType)); |
| 145 | if (iter == params.end()) |
| 146 | { |
| 147 | log<level::INFO>("OriginatorType is not provided. Replacing the string " |
| 148 | "with the default value"); |
| 149 | originatorType = originatorTypes::Internal; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | try |
| 154 | { |
| 155 | std::string type = std::get<std::string>(iter->second); |
| 156 | originatorType = sdbusplus::xyz::openbmc_project::Common::server:: |
| 157 | OriginatedBy::convertOriginatorTypesFromString(type); |
| 158 | } |
| 159 | catch (const std::bad_variant_access& e) |
| 160 | { |
| 161 | // Exception will be raised if the input is not string |
| 162 | log<level::ERR>(fmt::format("An invalid originatorType passed, " |
| 163 | "errormsg({})", |
| 164 | e.what()) |
| 165 | .c_str()); |
| 166 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"), |
| 167 | Argument::ARGUMENT_VALUE("INVALID INPUT")); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 172 | } // namespace dump |
| 173 | } // namespace phosphor |