blob: ac3b31cb89f1b2d47cc61ca194f622a4b35966c1 [file] [log] [blame]
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -05001#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
8namespace phosphor
9{
10namespace dump
11{
12DUMP_TYPE_TABLE dumpTypeTable = {
13 {"xyz.openbmc_project.Dump.Create.DumpType.UserRequested",
Dhruvaraj Subhashchandran247159b2023-06-29 05:17:35 -050014 {DumpTypes::USER, "BMC_DUMP"}},
15 {"xyz.openbmc_project.Dump.Create.DumpType.ApplicationCored",
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050016 {DumpTypes::CORE, "BMC_DUMP"}},
17 {"xyz.openbmc_project.Dump.Create.DumpType.Ramoops",
18 {DumpTypes::RAMOOPS, "BMC_DUMP"}}};
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050019
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050020DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = {
21 {DumpTypes::USER, "user"},
22 {DumpTypes::CORE, "core"},
23 {DumpTypes::RAMOOPS, "ramoops"},
24};
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050025
26std::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
36std::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
50DumpTypes 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