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 DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = {
25 % for item in DUMP_TYPE_TABLE:
26   % for key, values in item.items():
27     {DumpTypes::${values[0].upper()}, "${values[0]}"},
28   % endfor
29 % endfor
30 };
31 
32 std::optional<std::string> dumpTypeToString(const DumpTypes& dumpType)
33 {
34     auto it = dumpTypeToStringMap.find(dumpType);
35     if (it != dumpTypeToStringMap.end())
36     {
37         return it->second;
38     }
39     return std::nullopt;
40 }
41 
42 std::optional<DumpTypes> stringToDumpType(const std::string& str)
43 {
44     auto it = std::ranges::find_if(dumpTypeToStringMap,
45                                    [&str](const auto& pair) {
46         return pair.second == str;
47     });
48 
49     if (it != dumpTypeToStringMap.end())
50     {
51         return it->first;
52     }
53     return std::nullopt;
54 }
55 
56 DumpTypes validateDumpType(const std::string& type, const std::string& category)
57 {
58     using namespace phosphor::logging;
59     using InvalidArgument =
60         sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
61     using Argument = xyz::openbmc_project::Common::InvalidArgument;
62     // Dump type user will be return if type is empty
63     DumpTypes dumpType = DumpTypes::USER;
64     if (type.empty())
65     {
66         return dumpType;
67     }
68 
69     // Find any matching dump collection type for the category
70     auto it = std::find_if(dumpTypeTable.begin(), dumpTypeTable.end(),
71                            [&](const auto& pair) {
72         return pair.first == type && pair.second.second == category;
73     });
74 
75     if (it != dumpTypeTable.end())
76     {
77         dumpType = it->second.first;
78     }
79     else
80     {
81         lg2::error("An invalid dump type: {TYPE} passed", "TYPE", type);
82         elog<InvalidArgument>(Argument::ARGUMENT_NAME("BMC_DUMP_TYPE"),
83                               Argument::ARGUMENT_VALUE(type.c_str()));
84     }
85     return dumpType;
86 }
87 
88 } // namespace dump
89 } // namespace phosphor
90