blob: 4cdbd1d400b6ff64c36adf7cb26c82e5b283f414 [file] [log] [blame]
Matt Spinlercb6b0592019-07-16 15:58:51 -05001#pragma once
2
Matt Spinlerb8323632019-09-20 15:11:04 -05003#include "additional_data.hpp"
Matt Spinleraa659472019-10-23 09:26:48 -05004#include "data_interface.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -05005#include "private_header.hpp"
Matt Spinlerb8323632019-09-20 15:11:04 -05006#include "registry.hpp"
Matt Spinlerbd716f02019-10-15 10:54:11 -05007#include "src.hpp"
Matt Spinlerafa857c2019-10-24 13:03:46 -05008#include "user_data.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -05009#include "user_header.hpp"
10
11#include <memory>
12#include <vector>
13
14namespace openpower
15{
16namespace pels
17{
18
19/** @class PEL
20 *
21 * @brief This class represents a specific event log format referred to as a
22 * Platform Event Log.
23 *
24 * Every field in a PEL are in structures call sections, of which there are
25 * several types. Some sections are required, and some are optional. In some
26 * cases there may be more than one instance of a section type.
27 *
28 * The only two required sections for every type of PEL are the Private Header
29 * section and User Header section, which must be in the first and second
30 * positions, respectively.
31 *
32 * Every section starts with an 8 byte section header, which has the section
33 * size and type, among other things.
34 *
35 * This class represents all sections with objects.
36 *
Matt Spinlerbd716f02019-10-15 10:54:11 -050037 * The class can be constructed:
38 * - From a full formed flattened PEL.
39 * - From scratch based on an OpenBMC event and its corresponding PEL message
40 * registry entry.
Matt Spinlerb8323632019-09-20 15:11:04 -050041 *
Matt Spinlercb6b0592019-07-16 15:58:51 -050042 * The data() method allows one to retrieve the PEL as a vector<uint8_t>. This
43 * is the format in which it is stored and transmitted.
44 */
45class PEL
46{
47 public:
48 PEL() = delete;
49 ~PEL() = default;
50 PEL(const PEL&) = delete;
51 PEL& operator=(const PEL&) = delete;
52 PEL(PEL&&) = delete;
53 PEL& operator=(PEL&&) = delete;
54
55 /**
56 * @brief Constructor
57 *
58 * Build a PEL from raw data.
59 *
Matt Spinler07eefc52019-09-26 11:18:26 -050060 * Note: Neither this nor the following constructor can take a const vector&
61 * because the Stream class that is used to read from the vector cannot take
62 * a const. The alternative is to make a copy of the data, but as PELs can
63 * be up to 16KB that is undesireable.
64 *
Matt Spinlercb6b0592019-07-16 15:58:51 -050065 * @param[in] data - The PEL data
66 */
Matt Spinler07eefc52019-09-26 11:18:26 -050067 PEL(std::vector<uint8_t>& data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050068
69 /**
70 * @brief Constructor
71 *
72 * Build a PEL from the raw data.
73 *
74 * @param[in] data - the PEL data
75 * @param[in] obmcLogID - the corresponding OpenBMC event log ID
76 */
Matt Spinler07eefc52019-09-26 11:18:26 -050077 PEL(std::vector<uint8_t>& data, uint32_t obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -050078
79 /**
Matt Spinlerb8323632019-09-20 15:11:04 -050080 * @brief Constructor
81 *
82 * Creates a PEL from an OpenBMC event log and its message
83 * registry entry.
84 *
85 * @param[in] entry - The message registry entry for this error
86 * @param[in] obmcLogID - ID of corresponding OpenBMC event log
87 * @param[in] timestamp - Timestamp from the event log
88 * @param[in] severity - Severity from the event log
Matt Spinlerbd716f02019-10-15 10:54:11 -050089 * @param[in] additionalData - The AdditionalData contents
Matt Spinleraa659472019-10-23 09:26:48 -050090 * @param[in] dataIface - The data interface object
Matt Spinlerb8323632019-09-20 15:11:04 -050091 */
92 PEL(const openpower::pels::message::Entry& entry, uint32_t obmcLogID,
Matt Spinlerbd716f02019-10-15 10:54:11 -050093 uint64_t timestamp, phosphor::logging::Entry::Level severity,
Matt Spinleraa659472019-10-23 09:26:48 -050094 const AdditionalData& additionalData,
95 const DataInterfaceBase& dataIface);
Matt Spinlerb8323632019-09-20 15:11:04 -050096
97 /**
Matt Spinlercb6b0592019-07-16 15:58:51 -050098 * @brief Convenience function to return the log ID field from the
99 * Private Header section.
100 *
101 * @return uint32_t - the ID
102 */
103 uint32_t id() const
104 {
105 return _ph->id();
106 }
107
108 /**
109 * @brief Convenience function to return the PLID field from the
110 * Private Header section.
111 *
112 * @return uint32_t - the PLID
113 */
114 uint32_t plid() const
115 {
116 return _ph->plid();
117 }
118
119 /**
120 * @brief Convenience function to return the OpenBMC event log ID field
121 * from the Private Header section.
122 *
123 * @return uint32_t - the OpenBMC event log ID
124 */
125 uint32_t obmcLogID() const
126 {
127 return _ph->obmcLogID();
128 }
129
130 /**
131 * @brief Convenience function to return the commit time field from
132 * the Private Header section.
133 *
134 * @return BCDTime - the timestamp
135 */
136 BCDTime commitTime() const
137 {
138 return _ph->commitTimestamp();
139 }
140
141 /**
142 * @brief Convenience function to return the create time field from
143 * the Private Header section.
144 *
145 * @return BCDTime - the timestamp
146 */
147 BCDTime createTime() const
148 {
149 return _ph->createTimestamp();
150 }
151
152 /**
153 * @brief Gives access to the Private Header section class
154 *
Matt Spinler97d19b42019-10-29 11:34:03 -0500155 * @return const PrivateHeader& - the private header
Matt Spinlercb6b0592019-07-16 15:58:51 -0500156 */
Matt Spinler97d19b42019-10-29 11:34:03 -0500157 const PrivateHeader& privateHeader() const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500158 {
Matt Spinler97d19b42019-10-29 11:34:03 -0500159 return *_ph;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500160 }
161
162 /**
163 * @brief Gives access to the User Header section class
164 *
Matt Spinler97d19b42019-10-29 11:34:03 -0500165 * @return const UserHeader& - the user header
Matt Spinlercb6b0592019-07-16 15:58:51 -0500166 */
Matt Spinler97d19b42019-10-29 11:34:03 -0500167 const UserHeader& userHeader() const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500168 {
Matt Spinler97d19b42019-10-29 11:34:03 -0500169 return *_uh;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500170 }
171
172 /**
Matt Spinlerbd716f02019-10-15 10:54:11 -0500173 * @brief Gives access to the primary SRC's section class
174 *
175 * This is technically an optional section, so the return
176 * value is an std::optional<SRC*>.
177 *
178 * @return std::optional<SRC*> - the SRC section object
179 */
180 std::optional<SRC*> primarySRC() const;
181
182 /**
Matt Spinler131870c2019-09-25 13:29:04 -0500183 * @brief Returns the optional sections, which is everything but
184 * the Private and User Headers.
185 *
186 * @return const std::vector<std::unique_ptr<Section>>&
187 */
188 const std::vector<std::unique_ptr<Section>>& optionalSections() const
189 {
190 return _optionalSections;
191 }
192
193 /**
Matt Spinlercb6b0592019-07-16 15:58:51 -0500194 * @brief Returns the PEL data.
195 *
196 * @return std::vector<uint8_t> - the raw PEL data
197 */
198 std::vector<uint8_t> data();
199
200 /**
201 * @brief Says if the PEL is valid (the sections are all valid)
202 *
203 * @return bool - if the PEL is valid
204 */
205 bool valid() const;
206
207 /**
208 * @brief Sets the commit timestamp to the current time
209 */
210 void setCommitTime();
211
212 /**
213 * @brief Sets the error log ID field to a unique ID.
214 */
215 void assignID();
216
Aatir186ce8c2019-10-20 15:13:39 -0500217 /**
218 * @brief Output a PEL in JSON.
219 */
220 void toJSON();
221
Matt Spinlercb6b0592019-07-16 15:58:51 -0500222 private:
223 /**
224 * @brief Builds the section objects from a PEL data buffer
225 *
Matt Spinler07eefc52019-09-26 11:18:26 -0500226 * Note: The data parameter cannot be const for the same reasons
227 * as listed in the constructor.
228 *
229 * @param[in] data - The PEL data
Matt Spinlercb6b0592019-07-16 15:58:51 -0500230 * @param[in] obmcLogID - The OpenBMC event log ID to use for that
231 * field in the Private Header.
232 */
Matt Spinler07eefc52019-09-26 11:18:26 -0500233 void populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500234
235 /**
236 * @brief Flattens the PEL objects into the buffer
237 *
238 * @param[out] pelBuffer - What the data will be written to
239 */
240 void flatten(std::vector<uint8_t>& pelBuffer);
241
242 /**
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500243 * @brief Check that the PEL fields that need to be in agreement
244 * with each other are, and fix them up if necessary.
245 */
246 void checkRulesAndFix();
247
248 /**
Matt Spinlercb6b0592019-07-16 15:58:51 -0500249 * @brief The PEL Private Header section
250 */
251 std::unique_ptr<PrivateHeader> _ph;
252
253 /**
254 * @brief The PEL User Header section
255 */
256 std::unique_ptr<UserHeader> _uh;
257
258 /**
Matt Spinler131870c2019-09-25 13:29:04 -0500259 * @brief Holds all sections by the PH and UH.
260 */
261 std::vector<std::unique_ptr<Section>> _optionalSections;
Aatir186ce8c2019-10-20 15:13:39 -0500262
263 /**
264 * @brief helper function for printing PELs.
265 * @param[in] Section& - section object reference
266 * @param[in] std::string - PEL string
267 */
268 void printSectionInJSON(Section& section, std::string& buf) const;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500269};
270
Matt Spinlerafa857c2019-10-24 13:03:46 -0500271namespace util
272{
273
274/**
275 * @brief Create a UserData section containing the AdditionalData
276 * contents as a JSON string.
277 *
278 * @param[in] ad - The AdditionalData contents
279 *
280 * @return std::unique_ptr<UserData> - The section
281 */
282std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad);
283
284} // namespace util
285
Matt Spinlercb6b0592019-07-16 15:58:51 -0500286} // namespace pels
287} // namespace openpower