Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 1 | #include "dump_types.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/elog-errors.hpp> |
| 4 | #include <phosphor-logging/elog.hpp> |
| 5 | #include <phosphor-logging/lg2.hpp> |
| 6 | #include <xyz/openbmc_project/Common/error.hpp> |
| 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace dump |
| 11 | { |
| 12 | DUMP_TYPE_TABLE dumpTypeTable = { |
| 13 | {"xyz.openbmc_project.Dump.Create.DumpType.UserRequested", |
Dhruvaraj Subhashchandran | 247159b | 2023-06-29 05:17:35 -0500 | [diff] [blame] | 14 | {DumpTypes::USER, "BMC_DUMP"}}, |
| 15 | {"xyz.openbmc_project.Dump.Create.DumpType.ApplicationCored", |
Dhruvaraj Subhashchandran | 7d7e001 | 2023-06-29 05:35:07 -0500 | [diff] [blame] | 16 | {DumpTypes::CORE, "BMC_DUMP"}}, |
| 17 | {"xyz.openbmc_project.Dump.Create.DumpType.Ramoops", |
| 18 | {DumpTypes::RAMOOPS, "BMC_DUMP"}}}; |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 19 | |
Dhruvaraj Subhashchandran | 7d7e001 | 2023-06-29 05:35:07 -0500 | [diff] [blame] | 20 | DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = { |
| 21 | {DumpTypes::USER, "user"}, |
| 22 | {DumpTypes::CORE, "core"}, |
| 23 | {DumpTypes::RAMOOPS, "ramoops"}, |
| 24 | }; |
Dhruvaraj Subhashchandran | 3604710 | 2023-06-29 03:46:25 -0500 | [diff] [blame] | 25 | |
| 26 | std::optional<std::string> dumpTypeToString(const DumpTypes& dumpType) |
| 27 | { |
| 28 | auto it = dumpTypeToStringMap.find(dumpType); |
| 29 | if (it != dumpTypeToStringMap.end()) |
| 30 | { |
| 31 | return it->second; |
| 32 | } |
| 33 | return std::nullopt; |
| 34 | } |
| 35 | |
| 36 | std::optional<DumpTypes> stringToDumpType(const std::string& str) |
| 37 | { |
| 38 | auto it = std::ranges::find_if(dumpTypeToStringMap, |
| 39 | [&str](const auto& pair) { |
| 40 | return pair.second == str; |
| 41 | }); |
| 42 | |
| 43 | if (it != dumpTypeToStringMap.end()) |
| 44 | { |
| 45 | return it->first; |
| 46 | } |
| 47 | return std::nullopt; |
| 48 | } |
| 49 | |
| 50 | DumpTypes validateDumpType(const std::string& type, const std::string& category) |
| 51 | { |
| 52 | using namespace phosphor::logging; |
| 53 | using InvalidArgument = |
| 54 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 55 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 56 | // Dump type user will be return if type is empty |
| 57 | DumpTypes dumpType = DumpTypes::USER; |
| 58 | if (type.empty()) |
| 59 | { |
| 60 | return dumpType; |
| 61 | } |
| 62 | |
| 63 | // Find any matching dump collection type for the category |
| 64 | auto it = std::find_if(dumpTypeTable.begin(), dumpTypeTable.end(), |
| 65 | [&](const auto& pair) { |
| 66 | return pair.first == type && pair.second.second == category; |
| 67 | }); |
| 68 | |
| 69 | if (it != dumpTypeTable.end()) |
| 70 | { |
| 71 | dumpType = it->second.first; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | lg2::error("An invalid dump type: {TYPE} passed", "TYPE", type); |
| 76 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("BMC_DUMP_TYPE"), |
| 77 | Argument::ARGUMENT_VALUE(type.c_str())); |
| 78 | } |
| 79 | return dumpType; |
| 80 | } |
| 81 | |
| 82 | } // namespace dump |
| 83 | } // namespace phosphor |