xref: /openbmc/phosphor-logging/extensions/openpower-pels/registry.hpp (revision 367144cfa0eb857f4cb9ba786f43933469e19e34)
1 #pragma once
2 #include <filesystem>
3 #include <nlohmann/json.hpp>
4 #include <optional>
5 #include <string>
6 #include <vector>
7 
8 namespace openpower
9 {
10 namespace pels
11 {
12 namespace message
13 {
14 
15 constexpr auto registryFileName = "message_registry.json";
16 
17 /**
18  * @brief Represents a message registry entry, which is used for creating a
19  *        PEL from an OpenBMC event log.
20  */
21 struct Entry
22 {
23     /**
24      * @brief The error name, like "xyz.openbmc_project.Error.Foo".
25      */
26     std::string name;
27 
28     /**
29      * @brief The PEL subsystem field.
30      */
31     uint8_t subsystem;
32 
33     /**
34      * @brief The optional PEL severity field.  If not specified, the PEL
35      *        will use the severity of the OpenBMC event log.
36      */
37     std::optional<uint8_t> severity;
38 
39     /**
40      * @brief The optional severity field to use when in manufacturing tolerance
41      *        mode.
42      */
43     std::optional<uint8_t> mfgSeverity;
44 
45     /**
46      * @brief The PEL action flags field.
47      */
48     uint16_t actionFlags;
49 
50     /**
51      * @brief  The optional action flags to use instead when in manufacturing
52      * tolerance mode.
53      */
54     std::optional<uint16_t> mfgActionFlags;
55 
56     /**
57      * @brief The PEL event type field.  If not specified, higher level code
58      *        will decide the value.
59      */
60     std::optional<uint8_t> eventType;
61 
62     /**
63      * @brief The PEL event scope field.  If not specified, higher level code
64      *        will decide the value.
65      */
66     std::optional<uint8_t> eventScope;
67 
68     // TODO: SRC related fields
69 };
70 
71 /**
72  * @class Registry
73  *
74  * This class wraps the message registry JSON data and allows one to find
75  * the message registry entry pertaining to the error name.
76  *
77  * So that new registry files can easily be tested, the code will look for
78  * /etc/phosphor-logging/message_registry.json before looking for the real
79  * path.
80  */
81 class Registry
82 {
83   public:
84     Registry() = delete;
85     ~Registry() = default;
86     Registry(const Registry&) = default;
87     Registry& operator=(const Registry&) = default;
88     Registry(Registry&&) = default;
89     Registry& operator=(Registry&&) = default;
90 
91     /**
92      * @brief Constructor
93      * @param[in] registryFile - The path to the file.
94      */
95     explicit Registry(const std::filesystem::path& registryFile) :
96         _registryFile(registryFile)
97     {
98     }
99 
100     /**
101      * @brief Find a registry entry based on its error name.
102      *
103      * This function does do some basic sanity checking on the JSON contents,
104      * but there is also an external program that enforces a schema on the
105      * registry JSON that should catch all of these problems ahead of time.
106      *
107      * @param[in] name - The error name, like xyz.openbmc_project.Error.Foo
108      *
109      * @return optional<Entry> A filled in message registry structure if
110      *                         found, otherwise an empty optional object.
111      */
112     std::optional<Entry> lookup(const std::string& name);
113 
114   private:
115     /**
116      * @brief The path to the registry JSON file.
117      */
118     std::filesystem::path _registryFile;
119 };
120 
121 namespace helper
122 {
123 
124 /**
125  * @brief A helper function to get the PEL subsystem value based on
126  *        the registry subsystem name.
127  *
128  * @param[in] subsystemName - The registry name for the subsystem
129  *
130  * @return uint8_t The PEL subsystem value
131  */
132 uint8_t getSubsystem(const std::string& subsystemName);
133 
134 /**
135  * @brief A helper function to get the PEL severity value based on
136  *        the registry severity name.
137  *
138  * @param[in] severityName - The registry name for the severity
139  *
140  * @return uint8_t The PEL severity value
141  */
142 uint8_t getSeverity(const std::string& severityName);
143 
144 /**
145  * @brief A helper function to get the action flags value based on
146  *        the action flag names used in the registry.
147  *
148  * @param[in] flags - The list of flag names from the registry.
149  *
150  * @return uint16_t - The bitfield of flags used in the PEL.
151  */
152 uint16_t getActionFlags(const std::vector<std::string>& flags);
153 
154 /**
155  * @brief A helper function to get the PEL event type value based on
156  *        the registry event type name.
157  *
158  * @param[in] eventTypeName - The registry name for the event type
159  *
160  * @return uint8_t The PEL event type value
161  */
162 uint8_t getEventType(const std::string& eventTypeName);
163 
164 /**
165  * @brief A helper function to get the PEL event scope value based on
166  *        the registry event scope name.
167  *
168  * @param[in] eventScopeName - The registry name for the event scope
169  *
170  * @return uint8_t The PEL event scope value
171  */
172 uint8_t getEventScope(const std::string& eventScopeName);
173 
174 } // namespace helper
175 
176 } // namespace message
177 
178 } // namespace pels
179 } // namespace openpower
180