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> |
Dhruvaraj Subhashchandran | 416c8ae | 2022-05-06 06:21:11 -0500 | [diff] [blame] | 6 | #include <fmt/format.h> |
Jayanth Othayoth | d02153c | 2017-07-02 22:29:42 -0500 | [diff] [blame] | 7 | #include <systemd/sd-event.h> |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
| 11 | #include <phosphor-logging/elog.hpp> |
Dhruvaraj Subhashchandran | 416c8ae | 2022-05-06 06:21:11 -0500 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 13 | #include <sdbusplus/bus.hpp> |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Common/error.hpp> |
| 15 | #include <xyz/openbmc_project/Dump/Create/server.hpp> |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 16 | #include <xyz/openbmc_project/State/Boot/Progress/server.hpp> |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 17 | #include <xyz/openbmc_project/State/Host/server.hpp> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 18 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 21 | namespace phosphor |
| 22 | { |
| 23 | namespace dump |
| 24 | { |
| 25 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 26 | using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server:: |
| 27 | Progress::ProgressStages; |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 28 | using HostState = |
| 29 | sdbusplus::xyz::openbmc_project::State::server::Host::HostState; |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 30 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 31 | using namespace phosphor::logging; |
| 32 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 33 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 34 | /* Need a custom deleter for freeing up sd_event */ |
| 35 | struct EventDeleter |
| 36 | { |
| 37 | void operator()(sd_event* event) const |
| 38 | { |
| 39 | event = sd_event_unref(event); |
| 40 | } |
| 41 | }; |
| 42 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 43 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 44 | /** @struct CustomFd |
| 45 | * |
| 46 | * RAII wrapper for file descriptor. |
| 47 | */ |
| 48 | struct CustomFd |
| 49 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 50 | private: |
| 51 | /** @brief File descriptor */ |
| 52 | int fd = -1; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 53 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 54 | public: |
| 55 | CustomFd() = delete; |
| 56 | CustomFd(const CustomFd&) = delete; |
| 57 | CustomFd& operator=(const CustomFd&) = delete; |
| 58 | CustomFd(CustomFd&&) = delete; |
| 59 | CustomFd& operator=(CustomFd&&) = delete; |
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 | /** @brief Saves File descriptor and uses it to do file operation |
| 62 | * |
| 63 | * @param[in] fd - File descriptor |
| 64 | */ |
| 65 | CustomFd(int fd) : fd(fd) |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 66 | {} |
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 | ~CustomFd() |
| 69 | { |
| 70 | if (fd >= 0) |
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 | close(fd); |
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 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 75 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 76 | int operator()() const |
| 77 | { |
| 78 | return fd; |
| 79 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 80 | }; |
| 81 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 82 | /** |
| 83 | * @brief Get the bus service |
| 84 | * |
| 85 | * @param[in] bus - Bus to attach to. |
| 86 | * @param[in] path - D-Bus path name. |
| 87 | * @param[in] interface - D-Bus interface name. |
| 88 | * @return the bus service as a string |
| 89 | **/ |
Patrick Williams | 9b18bf2 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 90 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 91 | const std::string& interface); |
| 92 | |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 93 | /** |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 94 | * @brief Get the host state |
| 95 | * |
| 96 | * @return HostState on success |
| 97 | * Throw exception on failure |
| 98 | * |
| 99 | */ |
| 100 | HostState getHostState(); |
| 101 | |
| 102 | /** |
Ramesh Iyyar | 2279386 | 2020-12-04 04:03:03 -0600 | [diff] [blame] | 103 | * @brief Get the host boot progress stage |
| 104 | * |
| 105 | * @return BootProgress on success |
| 106 | * Throw exception on failure |
| 107 | * |
| 108 | */ |
| 109 | BootProgress getBootProgress(); |
| 110 | |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 111 | /** |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 112 | * @brief Get the host state value |
| 113 | * |
| 114 | * @param[in] intf - Interface to get the value |
| 115 | * @param[in] objPath - Object path of the service |
| 116 | * @param[in] state - State name to get |
| 117 | * |
| 118 | * @return The state value on success |
| 119 | * Throw exception on failure |
| 120 | */ |
| 121 | std::string getStateValue(const std::string& intf, const std::string& objPath, |
| 122 | const std::string& state); |
| 123 | |
| 124 | /** |
Dhruvaraj Subhashchandran | 6a54d9a | 2020-12-17 22:24:37 -0600 | [diff] [blame] | 125 | * @brief Check whether host is running |
| 126 | * |
| 127 | * @return true if the host running else false. |
| 128 | * Throw exception on failure. |
| 129 | */ |
| 130 | bool isHostRunning(); |
| 131 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 132 | inline void extractOriginatorProperties(phosphor::dump::DumpCreateParams params, |
| 133 | std::string& originatorId, |
| 134 | originatorTypes& originatorType) |
| 135 | { |
| 136 | using InvalidArgument = |
| 137 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 138 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 139 | using CreateParametersXYZ = |
| 140 | sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters; |
| 141 | |
| 142 | auto iter = params.find( |
| 143 | sdbusplus::xyz::openbmc_project::Dump::server::Create:: |
| 144 | convertCreateParametersToString(CreateParametersXYZ::OriginatorId)); |
| 145 | if (iter == params.end()) |
| 146 | { |
| 147 | log<level::INFO>("OriginatorId is not provided"); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | try |
| 152 | { |
| 153 | originatorId = std::get<std::string>(iter->second); |
| 154 | } |
| 155 | catch (const std::bad_variant_access& e) |
| 156 | { |
| 157 | // Exception will be raised if the input is not string |
| 158 | log<level::ERR>( |
| 159 | fmt::format( |
| 160 | "An invalid originatorId passed. It should be a string, " |
| 161 | "errormsg({})", |
| 162 | e.what()) |
| 163 | .c_str()); |
| 164 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_ID"), |
| 165 | Argument::ARGUMENT_VALUE("INVALID INPUT")); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | iter = params.find(sdbusplus::xyz::openbmc_project::Dump::server::Create:: |
| 170 | convertCreateParametersToString( |
| 171 | CreateParametersXYZ::OriginatorType)); |
| 172 | if (iter == params.end()) |
| 173 | { |
| 174 | log<level::INFO>("OriginatorType is not provided. Replacing the string " |
| 175 | "with the default value"); |
| 176 | originatorType = originatorTypes::Internal; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | try |
| 181 | { |
| 182 | std::string type = std::get<std::string>(iter->second); |
| 183 | originatorType = sdbusplus::xyz::openbmc_project::Common::server:: |
| 184 | OriginatedBy::convertOriginatorTypesFromString(type); |
| 185 | } |
| 186 | catch (const std::bad_variant_access& e) |
| 187 | { |
| 188 | // Exception will be raised if the input is not string |
| 189 | log<level::ERR>(fmt::format("An invalid originatorType passed, " |
| 190 | "errormsg({})", |
| 191 | e.what()) |
| 192 | .c_str()); |
| 193 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ORIGINATOR_TYPE"), |
| 194 | Argument::ARGUMENT_VALUE("INVALID INPUT")); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
Dhruvaraj Subhashchandran | a91cc84 | 2022-03-22 08:06:20 -0500 | [diff] [blame] | 199 | /** |
| 200 | * @brief Check whether host is quiesced |
| 201 | * |
| 202 | * @return true if the host is quiesced else false. |
| 203 | * Throw exception on failure. |
| 204 | */ |
| 205 | bool isHostQuiesced(); |
| 206 | |
Dhruvaraj Subhashchandran | 416c8ae | 2022-05-06 06:21:11 -0500 | [diff] [blame] | 207 | /** |
| 208 | * @brief Read property value from the specified object and interface |
| 209 | * @param[in] bus D-Bus handle |
| 210 | * @param[in] service service which has implemented the interface |
| 211 | * @param[in] object object having has implemented the interface |
| 212 | * @param[in] intf interface having the property |
| 213 | * @param[in] prop name of the property to read |
| 214 | * @return property value |
| 215 | */ |
| 216 | template <typename T> |
| 217 | T readDBusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 218 | const std::string& object, const std::string& intf, |
| 219 | const std::string& prop) |
| 220 | { |
| 221 | using ::phosphor::logging::level; |
| 222 | using ::phosphor::logging::log; |
| 223 | T retVal{}; |
| 224 | try |
| 225 | { |
| 226 | auto properties = |
| 227 | bus.new_method_call(service.c_str(), object.c_str(), |
| 228 | "org.freedesktop.DBus.Properties", "Get"); |
| 229 | properties.append(intf); |
| 230 | properties.append(prop); |
| 231 | auto result = bus.call(properties); |
| 232 | result.read(retVal); |
| 233 | } |
| 234 | catch (const std::exception& ex) |
| 235 | { |
| 236 | log<level::ERR>( |
| 237 | fmt::format("Failed to get the property ({}) interface ({}) " |
| 238 | "object path ({}) error ({}) ", |
| 239 | prop.c_str(), intf.c_str(), object.c_str(), ex.what()) |
| 240 | .c_str()); |
| 241 | throw; |
| 242 | } |
| 243 | return retVal; |
| 244 | } |
| 245 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 246 | } // namespace dump |
| 247 | } // namespace phosphor |