1 ## This file is a template.The comment below is emitted
2 ## into the rendered file; feel free to edit this file.
3 //  !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
4 #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 
11 namespace phosphor
12 {
13 namespace dump
14 {
15 
16 DUMP_TYPE_TABLE dumpTypeTable = {
17 % 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 };
23 
24 <%
25 map_keys = set()
26 %>
27 DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = {
28 % for item in DUMP_TYPE_TABLE:
29   % for key, values in item.items():
30     % if values[0].upper() not in map_keys:
31         {DumpTypes::${values[0].upper()}, "${values[0]}"},
32         <% map_keys.add(values[0].upper()) %>
33     % endif
34   % endfor
35 % endfor
36 % 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 
44 const 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
52 };
53 
dumpTypeToString(const DumpTypes & dumpType)54 std::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 
stringToDumpType(const std::string & str)64 std::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 
validateDumpType(const std::string & type,const std::string & category)78 DumpTypes 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 
isErrorTypeValid(const std::string & errorType)110 bool isErrorTypeValid(const std::string& errorType)
111 {
112     const auto it = errorMap.find(errorType);
113     return it != errorMap.end();
114 }
115 
findErrorType(const std::string & errString)116 std::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 
129 } // namespace dump
130 } // namespace phosphor
131