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