blob: 969bfff4f3d37d08f7fd86a7f62308ab60431cf8 [file] [log] [blame]
Dhruvaraj Subhashchandranaa0937f2023-07-22 23:50:40 -05001## This file is a template.The comment below is emitted
Dhruvaraj Subhashchandranc1f5ed62023-07-09 04:31:28 -05002## into the rendered file; feel free to edit this file.
Dhruvaraj Subhashchandranaa0937f2023-07-22 23:50:40 -05003// !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -05004#include "dump_types.hpp"
5
6#include <phosphor-logging/elog-errors.hpp>
7#include <phosphor-logging/elog.hpp>
8#include <phosphor-logging/lg2.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
10
11namespace phosphor
12{
13namespace dump
14{
Dhruvaraj Subhashchandranc1f5ed62023-07-09 04:31:28 -050015
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050016DUMP_TYPE_TABLE dumpTypeTable = {
Dhruvaraj Subhashchandranc1f5ed62023-07-09 04:31:28 -050017% for item in DUMP_TYPE_TABLE:
18 % for key, values in item.items():
19 {"${key}", {DumpTypes::${values[0].upper()}, "${values[1]}"}},
20 % endfor
21% endfor
22};
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050023
Dhruvaraj Subhashchandranaa0937f2023-07-22 23:50:40 -050024<%
25map_keys = set()
26%>
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050027DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = {
Dhruvaraj Subhashchandranc1f5ed62023-07-09 04:31:28 -050028% for item in DUMP_TYPE_TABLE:
29 % for key, values in item.items():
Dhruvaraj Subhashchandranaa0937f2023-07-22 23:50:40 -050030 % if values[0].upper() not in map_keys:
31 {DumpTypes::${values[0].upper()}, "${values[0]}"},
32 <% map_keys.add(values[0].upper()) %>
33 % endif
Dhruvaraj Subhashchandranc1f5ed62023-07-09 04:31:28 -050034 % endfor
35% endfor
Dhruvaraj Subhashchandranaa0937f2023-07-22 23:50:40 -050036% for key, values in ERROR_TYPE_DICT.items():
37 % if key.upper() not in map_keys:
38 {DumpTypes::${key.upper()}, "${key}"},
39 <% map_keys.add(key.upper()) %>
40 % endif
41% endfor
42};
43
44const ErrorMap errorMap = {
45% for key, errors in ERROR_TYPE_DICT.items():
46 {"${key}", {
47 % for error in errors:
48 "${error}",
49 % endfor
50 }},
51% endfor
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050052};
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -050053
54std::optional<std::string> dumpTypeToString(const DumpTypes& dumpType)
55{
56 auto it = dumpTypeToStringMap.find(dumpType);
57 if (it != dumpTypeToStringMap.end())
58 {
59 return it->second;
60 }
61 return std::nullopt;
62}
63
64std::optional<DumpTypes> stringToDumpType(const std::string& str)
65{
66 auto it = std::ranges::find_if(dumpTypeToStringMap,
67 [&str](const auto& pair) {
68 return pair.second == str;
69 });
70
71 if (it != dumpTypeToStringMap.end())
72 {
73 return it->first;
74 }
75 return std::nullopt;
76}
77
78DumpTypes validateDumpType(const std::string& type, const std::string& category)
79{
80 using namespace phosphor::logging;
81 using InvalidArgument =
82 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
83 using Argument = xyz::openbmc_project::Common::InvalidArgument;
84 // Dump type user will be return if type is empty
85 DumpTypes dumpType = DumpTypes::USER;
86 if (type.empty())
87 {
88 return dumpType;
89 }
90
91 // Find any matching dump collection type for the category
92 auto it = std::find_if(dumpTypeTable.begin(), dumpTypeTable.end(),
93 [&](const auto& pair) {
94 return pair.first == type && pair.second.second == category;
95 });
96
97 if (it != dumpTypeTable.end())
98 {
99 dumpType = it->second.first;
100 }
101 else
102 {
103 lg2::error("An invalid dump type: {TYPE} passed", "TYPE", type);
104 elog<InvalidArgument>(Argument::ARGUMENT_NAME("BMC_DUMP_TYPE"),
105 Argument::ARGUMENT_VALUE(type.c_str()));
106 }
107 return dumpType;
108}
109
Dhruvaraj Subhashchandranaa0937f2023-07-22 23:50:40 -0500110bool isErrorTypeValid(const std::string& errorType)
111{
112 const auto it = errorMap.find(errorType);
113 return it != errorMap.end();
114}
115
116std::optional<ErrorType> findErrorType(const std::string& errString)
117{
118 for (const auto& [type, errorList] : errorMap)
119 {
120 auto error = std::find(errorList.begin(), errorList.end(), errString);
121 if (error != errorList.end())
122 {
123 return type;
124 }
125 }
126 return std::nullopt;
127}
128
Dhruvaraj Subhashchandran36047102023-06-29 03:46:25 -0500129} // namespace dump
130} // namespace phosphor