Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 1 | #pragma once |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 2 | #include "dump_manager.hpp" |
| 3 | |
Jayanth Othayoth | d02153c | 2017-07-02 22:29:42 -0500 | [diff] [blame] | 4 | #include <systemd/sd-event.h> |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <phosphor-logging/elog.hpp> |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 10 | #include <sdbusplus/bus.hpp> |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
| 12 | #include <xyz/openbmc_project/Dump/Create/server.hpp> |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 13 | #include <xyz/openbmc_project/State/Boot/Progress/server.hpp> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 14 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace dump |
| 20 | { |
| 21 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 22 | using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server:: |
| 23 | Progress::ProgressStages; |
| 24 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 25 | using namespace phosphor::logging; |
| 26 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 27 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 28 | /* Need a custom deleter for freeing up sd_event */ |
| 29 | struct EventDeleter |
| 30 | { |
| 31 | void operator()(sd_event* event) const |
| 32 | { |
| 33 | event = sd_event_unref(event); |
| 34 | } |
| 35 | }; |
| 36 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 37 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 38 | /** @struct CustomFd |
| 39 | * |
| 40 | * RAII wrapper for file descriptor. |
| 41 | */ |
| 42 | struct CustomFd |
| 43 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 44 | private: |
| 45 | /** @brief File descriptor */ |
| 46 | int fd = -1; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 47 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 48 | 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 Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 54 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 55 | /** @brief Saves File descriptor and uses it to do file operation |
| 56 | * |
| 57 | * @param[in] fd - File descriptor |
| 58 | */ |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 59 | CustomFd(int fd) : fd(fd) {} |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 60 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 61 | ~CustomFd() |
| 62 | { |
| 63 | if (fd >= 0) |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 64 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 65 | close(fd); |
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 | } |
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 | int operator()() const |
| 70 | { |
| 71 | return fd; |
| 72 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 75 | /** |
| 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 Williams | 9b18bf2 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 83 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 84 | const std::string& interface); |
| 85 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 86 | /** |
| 87 | * @brief Get the host boot progress stage |
| 88 | * |
| 89 | * @return BootProgress on success |
| 90 | * Throw exception on failure |
| 91 | * |
| 92 | */ |
| 93 | BootProgress getBootProgress(); |
| 94 | |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 95 | /** |
| 96 | * @brief Check whether host is running |
| 97 | * |
| 98 | * @return true if the host running else false. |
| 99 | * Throw exception on failure. |
| 100 | */ |
| 101 | bool isHostRunning(); |
| 102 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 103 | inline 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 Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 118 | lg2::info("OriginatorId is not provided"); |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 119 | } |
| 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 Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 129 | lg2::error("An invalid originatorId passed. It should be a string, " |
| 130 | "errormsg: {ERROR}", |
| 131 | "ERROR", e); |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 132 | 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 Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 142 | lg2::info("OriginatorType is not provided. Replacing the string " |
| 143 | "with the default value"); |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 144 | 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 Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 157 | lg2::error("An invalid originatorType passed, errormsg: {ERROR}", |
| 158 | "ERROR", e); |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 159 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"), |
| 160 | Argument::ARGUMENT_VALUE("INVALID INPUT")); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 165 | } // namespace dump |
| 166 | } // namespace phosphor |