blob: 8dcdf151eaf29226d0c44e2d4926610797910e5a [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",
16 {DumpTypes::CORE, "BMC_DUMP"}}};
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050017
Dhruvaraj Subhashchandran247159b2023-06-29 05:17:35 -050018DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = {{DumpTypes::USER, "user"},
19 {DumpTypes::CORE, "core"}};
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050020
21std::optional<std::string> dumpTypeToString(const DumpTypes& dumpType)
22{
23 auto it = dumpTypeToStringMap.find(dumpType);
24 if (it != dumpTypeToStringMap.end())
25 {
26 return it->second;
27 }
28 return std::nullopt;
29}
30
31std::optional<DumpTypes> stringToDumpType(const std::string& str)
32{
33 auto it = std::ranges::find_if(dumpTypeToStringMap,
34 [&str](const auto& pair) {
35 return pair.second == str;
36 });
37
38 if (it != dumpTypeToStringMap.end())
39 {
40 return it->first;
41 }
42 return std::nullopt;
43}
44
45DumpTypes validateDumpType(const std::string& type, const std::string& category)
46{
47 using namespace phosphor::logging;
48 using InvalidArgument =
49 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
50 using Argument = xyz::openbmc_project::Common::InvalidArgument;
51 // Dump type user will be return if type is empty
52 DumpTypes dumpType = DumpTypes::USER;
53 if (type.empty())
54 {
55 return dumpType;
56 }
57
58 // Find any matching dump collection type for the category
59 auto it = std::find_if(dumpTypeTable.begin(), dumpTypeTable.end(),
60 [&](const auto& pair) {
61 return pair.first == type && pair.second.second == category;
62 });
63
64 if (it != dumpTypeTable.end())
65 {
66 dumpType = it->second.first;
67 }
68 else
69 {
70 lg2::error("An invalid dump type: {TYPE} passed", "TYPE", type);
71 elog<InvalidArgument>(Argument::ARGUMENT_NAME("BMC_DUMP_TYPE"),
72 Argument::ARGUMENT_VALUE(type.c_str()));
73 }
74 return dumpType;
75}
76
77} // namespace dump
78} // namespace phosphor