blob: 4a11d7723b73577b3d890b01f34317cc733c4e6d [file] [log] [blame]
Matt Spinlerd3335df2019-07-10 11:04:21 -05001#pragma once
Aatir Manzurad0e0472019-10-07 13:18:37 -05002
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +08003#include "registry.hpp"
Matt Spinlerd3335df2019-07-10 11:04:21 -05004#include "section_header.hpp"
5
Aatir Manzurad0e0472019-10-07 13:18:37 -05006#include <optional>
7
Matt Spinlerd3335df2019-07-10 11:04:21 -05008namespace openpower
9{
10namespace pels
11{
Matt Spinlerd3335df2019-07-10 11:04:21 -050012/**
13 * @class Section
14 *
15 * The base class for a PEL section. It contains the SectionHeader
16 * as all sections start with it.
17 *
18 */
19class Section
20{
21 public:
22 Section() = default;
23 virtual ~Section() = default;
24 Section(const Section&) = default;
25 Section& operator=(const Section&) = default;
26 Section(Section&&) = default;
27 Section& operator=(Section&&) = default;
28
29 /**
30 * @brief Returns a reference to the SectionHeader
31 */
Matt Spinler06885452019-11-06 10:35:42 -060032 const SectionHeader& header() const
Matt Spinlerd3335df2019-07-10 11:04:21 -050033 {
34 return _header;
35 }
36
37 /**
38 * @brief Says if the section is valid.
39 */
40 bool valid() const
41 {
42 return _valid;
43 }
44
Matt Spinlercf5a8d02019-09-05 12:58:53 -050045 /**
46 * @brief Flatten the section into the stream
47 *
48 * @param[in] stream - The stream to write to
49 */
Matt Spinler06885452019-11-06 10:35:42 -060050 virtual void flatten(Stream& stream) const = 0;
Matt Spinlercf5a8d02019-09-05 12:58:53 -050051
Aatir Manzurad0e0472019-10-07 13:18:37 -050052 /**
53 * @brief Get section in JSON. Derived classes to override when required to.
54 * @return std::optional<std::string> - If a section comes with a JSON
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +080055 * representation, this would return the string for it.
Aatir Manzurad0e0472019-10-07 13:18:37 -050056 */
57 virtual std::optional<std::string> getJSON() const
58 {
59 return std::nullopt;
60 }
61
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +080062 /**
63 * @brief Get section in JSON. Derived classes to override when required to.
64 * @param[in] registry - Registry object reference
65 * @return std::optional<std::string> - If a section comes with a JSON
66 * representation, this would return the string for it.
67 */
68 virtual std::optional<std::string>
69 getJSON(message::Registry& registry) const
70 {
71 return std::nullopt;
72 }
73
Matt Spinlercbf3c4d2020-03-24 15:34:52 -050074 /**
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +080075 * @brief Get section in JSON. Derived classes to override when required to.
76 * @param[in] creatorID - Creator Subsystem ID from Private Header
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +080077 * @param[in] plugins - Vector of strings of plugins found in filesystem
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +080078 * @return std::optional<std::string> - If a section comes with a JSON
79 * representation, this would return the string for it.
80 */
Harisuddin Mohamed Isa3fdcd4e2020-08-26 11:56:42 +080081 virtual std::optional<std::string>
82 getJSON(uint8_t creatorID,
83 const std::vector<std::string>& plugins) const
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +080084 {
85 return std::nullopt;
86 }
87
88 /**
Matt Spinlercbf3c4d2020-03-24 15:34:52 -050089 * @brief Shrinks a PEL section
90 *
91 * If this is even possible for a section depends on which section
92 * it is. If a section cannot be shrunk, it doesn't need to implement
93 * shrink so it will just return false, meaning no shrinking was done.
94 *
95 * If a section can be shrunk, this function will be overridden in that
96 * class.
97 *
98 * @param[in] newSize - The new size, in bytes, to shrink to
99 *
100 * @return bool - true if successful, false else
101 */
102 virtual bool shrink(size_t newSize)
103 {
104 return false;
105 }
106
Matt Spinler85f61a62020-06-03 16:28:55 -0500107 /**
108 * @brief Returns any debug data stored in the object
109 *
110 * @return std::vector<std::string>& - The debug data
111 */
112 const std::vector<std::string>& getDebugData() const
113 {
114 return _debugData;
115 }
116
Matt Spinlerd3335df2019-07-10 11:04:21 -0500117 protected:
118 /**
119 * @brief Returns the flattened size of the section header
120 */
121 static constexpr size_t flattenedSize()
122 {
123 return SectionHeader::flattenedSize();
124 }
125
126 /**
Matt Spinler85f61a62020-06-03 16:28:55 -0500127 * @brief Adds debug data to the object that may be displayed
128 * in a UserData section in the PEL.
129 *
130 * @param[in] data - The new entry to add to the vector of data.
131 */
132 void addDebugData(const std::string& data)
133 {
134 _debugData.push_back(data);
135 }
136
137 /**
Matt Spinlerd3335df2019-07-10 11:04:21 -0500138 * @brief Used to validate the section.
139 *
140 * Implemented by derived classes.
141 */
142 virtual void validate() = 0;
143
144 /**
145 * @brief The section header structure.
146 *
147 * Filled in by derived classes.
148 */
149 SectionHeader _header;
150
151 /**
152 * @brief The section valid flag.
153 *
154 * This is determined by the derived class.
155 */
156 bool _valid = false;
Matt Spinler85f61a62020-06-03 16:28:55 -0500157
158 /**
159 * @brief Messages that derived classes can add during construction
160 * of a PEL when something happens that would be useful to
161 * store in the PEL. This may get added into a UserData section
162 * in the PEL.
163 */
164 std::vector<std::string> _debugData;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500165};
166} // namespace pels
167} // namespace openpower