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