1d3335dfaSMatt Spinler #pragma once
2ad0e0476SAatir Manzur 
3a214ed30SHarisuddin Mohamed Isa #include "registry.hpp"
4d3335dfaSMatt Spinler #include "section_header.hpp"
5d3335dfaSMatt Spinler 
6ad0e0476SAatir Manzur #include <optional>
7ad0e0476SAatir Manzur 
8d3335dfaSMatt Spinler namespace openpower
9d3335dfaSMatt Spinler {
10d3335dfaSMatt Spinler namespace pels
11d3335dfaSMatt Spinler {
12d3335dfaSMatt Spinler /**
13d3335dfaSMatt Spinler  * @class Section
14d3335dfaSMatt Spinler  *
15d3335dfaSMatt Spinler  * The base class for a PEL section.  It contains the SectionHeader
16d3335dfaSMatt Spinler  * as all sections start with it.
17d3335dfaSMatt Spinler  *
18d3335dfaSMatt Spinler  */
19d3335dfaSMatt Spinler class Section
20d3335dfaSMatt Spinler {
21d3335dfaSMatt Spinler   public:
22d3335dfaSMatt Spinler     Section() = default;
23d3335dfaSMatt Spinler     virtual ~Section() = default;
24d3335dfaSMatt Spinler     Section(const Section&) = default;
25d3335dfaSMatt Spinler     Section& operator=(const Section&) = default;
26d3335dfaSMatt Spinler     Section(Section&&) = default;
27d3335dfaSMatt Spinler     Section& operator=(Section&&) = default;
28d3335dfaSMatt Spinler 
29d3335dfaSMatt Spinler     /**
30d3335dfaSMatt Spinler      * @brief Returns a reference to the SectionHeader
31d3335dfaSMatt Spinler      */
header() const320688545bSMatt Spinler     const SectionHeader& header() const
33d3335dfaSMatt Spinler     {
34d3335dfaSMatt Spinler         return _header;
35d3335dfaSMatt Spinler     }
36d3335dfaSMatt Spinler 
37d3335dfaSMatt Spinler     /**
38d3335dfaSMatt Spinler      * @brief Says if the section is valid.
39d3335dfaSMatt Spinler      */
valid() const40d3335dfaSMatt Spinler     bool valid() const
41d3335dfaSMatt Spinler     {
42d3335dfaSMatt Spinler         return _valid;
43d3335dfaSMatt Spinler     }
44d3335dfaSMatt Spinler 
45cf5a8d0fSMatt Spinler     /**
46cf5a8d0fSMatt Spinler      * @brief Flatten the section into the stream
47cf5a8d0fSMatt Spinler      *
48cf5a8d0fSMatt Spinler      * @param[in] stream - The stream to write to
49cf5a8d0fSMatt Spinler      */
500688545bSMatt Spinler     virtual void flatten(Stream& stream) const = 0;
51cf5a8d0fSMatt Spinler 
52ad0e0476SAatir Manzur     /**
53ad0e0476SAatir Manzur      * @brief Get section in JSON. Derived classes to override when required to.
54*b832aa5eSMatt Spinler      *
55*b832aa5eSMatt Spinler      * @param[in] creatorID - The creator ID for the PEL
56*b832aa5eSMatt Spinler      *
57ad0e0476SAatir Manzur      * @return std::optional<std::string> - If a section comes with a JSON
58a214ed30SHarisuddin Mohamed Isa      * representation, this would return the string for it.
59ad0e0476SAatir Manzur      */
getJSON(uint8_t) const60*b832aa5eSMatt Spinler     virtual std::optional<std::string> getJSON(uint8_t /* creatorID*/) const
61ad0e0476SAatir Manzur     {
62ad0e0476SAatir Manzur         return std::nullopt;
63ad0e0476SAatir Manzur     }
64ad0e0476SAatir Manzur 
65a214ed30SHarisuddin Mohamed Isa     /**
66a214ed30SHarisuddin Mohamed Isa      * @brief Get section in JSON. Derived classes to override when required to.
67a214ed30SHarisuddin Mohamed Isa      * @param[in] registry - Registry object reference
68c8d6cc61SHarisuddin Mohamed Isa      * @param[in] plugins - Vector of strings of plugins found in filesystem
69c8d6cc61SHarisuddin Mohamed Isa      * @param[in] creatorID - Creator Subsystem ID from Private Header
70a214ed30SHarisuddin Mohamed Isa      * @return std::optional<std::string> - If a section comes with a JSON
71a214ed30SHarisuddin Mohamed Isa      * representation, this would return the string for it.
72a214ed30SHarisuddin Mohamed Isa      */
73a214ed30SHarisuddin Mohamed Isa     virtual std::optional<std::string>
getJSON(message::Registry &,const std::vector<std::string> &,uint8_t) const74d26fa3e7SPatrick Williams         getJSON(message::Registry& /*registry*/,
75d26fa3e7SPatrick Williams                 const std::vector<std::string>& /*plugins*/,
76d26fa3e7SPatrick Williams                 uint8_t /*creatorID*/) const
77a214ed30SHarisuddin Mohamed Isa     {
78a214ed30SHarisuddin Mohamed Isa         return std::nullopt;
79a214ed30SHarisuddin Mohamed Isa     }
80a214ed30SHarisuddin Mohamed Isa 
81cbf3c4d4SMatt Spinler     /**
82f67bafd0SHarisuddin Mohamed Isa      * @brief Get section in JSON. Derived classes to override when required to.
83f67bafd0SHarisuddin Mohamed Isa      * @param[in] creatorID - Creator Subsystem ID from Private Header
843fdcd4e8SHarisuddin Mohamed Isa      * @param[in] plugins - Vector of strings of plugins found in filesystem
85f67bafd0SHarisuddin Mohamed Isa      * @return std::optional<std::string> - If a section comes with a JSON
86f67bafd0SHarisuddin Mohamed Isa      * representation, this would return the string for it.
87f67bafd0SHarisuddin Mohamed Isa      */
883fdcd4e8SHarisuddin Mohamed Isa     virtual std::optional<std::string>
getJSON(uint8_t,const std::vector<std::string> &) const89d26fa3e7SPatrick Williams         getJSON(uint8_t /*creatorID*/,
90d26fa3e7SPatrick Williams                 const std::vector<std::string>& /*plugins*/) const
91f67bafd0SHarisuddin Mohamed Isa     {
92f67bafd0SHarisuddin Mohamed Isa         return std::nullopt;
93f67bafd0SHarisuddin Mohamed Isa     }
94f67bafd0SHarisuddin Mohamed Isa 
95f67bafd0SHarisuddin Mohamed Isa     /**
96cbf3c4d4SMatt Spinler      * @brief Shrinks a PEL section
97cbf3c4d4SMatt Spinler      *
98cbf3c4d4SMatt Spinler      * If this is even possible for a section depends on which section
99cbf3c4d4SMatt Spinler      * it is.  If a section cannot be shrunk, it doesn't need to implement
100cbf3c4d4SMatt Spinler      * shrink so it will just return false, meaning no shrinking was done.
101cbf3c4d4SMatt Spinler      *
102cbf3c4d4SMatt Spinler      * If a section can be shrunk, this function will be overridden in that
103cbf3c4d4SMatt Spinler      * class.
104cbf3c4d4SMatt Spinler      *
105cbf3c4d4SMatt Spinler      * @param[in] newSize - The new size, in bytes, to shrink to
106cbf3c4d4SMatt Spinler      *
107cbf3c4d4SMatt Spinler      * @return bool - true if successful, false else
108cbf3c4d4SMatt Spinler      */
shrink(size_t)109d26fa3e7SPatrick Williams     virtual bool shrink(size_t /*newSize*/)
110cbf3c4d4SMatt Spinler     {
111cbf3c4d4SMatt Spinler         return false;
112cbf3c4d4SMatt Spinler     }
113cbf3c4d4SMatt Spinler 
11485f61a63SMatt Spinler     /**
11585f61a63SMatt Spinler      * @brief Returns any debug data stored in the object
11685f61a63SMatt Spinler      *
11785f61a63SMatt Spinler      * @return std::vector<std::string>& - The debug data
11885f61a63SMatt Spinler      */
getDebugData() const11985f61a63SMatt Spinler     const std::vector<std::string>& getDebugData() const
12085f61a63SMatt Spinler     {
12185f61a63SMatt Spinler         return _debugData;
12285f61a63SMatt Spinler     }
12385f61a63SMatt Spinler 
124d3335dfaSMatt Spinler   protected:
125d3335dfaSMatt Spinler     /**
126d3335dfaSMatt Spinler      * @brief Returns the flattened size of the section header
127d3335dfaSMatt Spinler      */
flattenedSize()128d3335dfaSMatt Spinler     static constexpr size_t flattenedSize()
129d3335dfaSMatt Spinler     {
130d3335dfaSMatt Spinler         return SectionHeader::flattenedSize();
131d3335dfaSMatt Spinler     }
132d3335dfaSMatt Spinler 
133d3335dfaSMatt Spinler     /**
13485f61a63SMatt Spinler      * @brief Adds debug data to the object that may be displayed
13585f61a63SMatt Spinler      *        in a UserData section in the PEL.
13685f61a63SMatt Spinler      *
13785f61a63SMatt Spinler      * @param[in] data - The new entry to add to the vector of data.
13885f61a63SMatt Spinler      */
addDebugData(const std::string & data)13985f61a63SMatt Spinler     void addDebugData(const std::string& data)
14085f61a63SMatt Spinler     {
14185f61a63SMatt Spinler         _debugData.push_back(data);
14285f61a63SMatt Spinler     }
14385f61a63SMatt Spinler 
14485f61a63SMatt Spinler     /**
145d3335dfaSMatt Spinler      * @brief Used to validate the section.
146d3335dfaSMatt Spinler      *
147d3335dfaSMatt Spinler      * Implemented by derived classes.
148d3335dfaSMatt Spinler      */
149d3335dfaSMatt Spinler     virtual void validate() = 0;
150d3335dfaSMatt Spinler 
151d3335dfaSMatt Spinler     /**
152d3335dfaSMatt Spinler      * @brief The section header structure.
153d3335dfaSMatt Spinler      *
154d3335dfaSMatt Spinler      * Filled in by derived classes.
155d3335dfaSMatt Spinler      */
156d3335dfaSMatt Spinler     SectionHeader _header;
157d3335dfaSMatt Spinler 
158d3335dfaSMatt Spinler     /**
159d3335dfaSMatt Spinler      * @brief The section valid flag.
160d3335dfaSMatt Spinler      *
161d3335dfaSMatt Spinler      * This is determined by the derived class.
162d3335dfaSMatt Spinler      */
163d3335dfaSMatt Spinler     bool _valid = false;
16485f61a63SMatt Spinler 
16585f61a63SMatt Spinler     /**
16685f61a63SMatt Spinler      * @brief Messages that derived classes can add during construction
16785f61a63SMatt Spinler      *        of a PEL when something happens that would be useful to
16885f61a63SMatt Spinler      *        store in the PEL.  This may get added into a UserData section
16985f61a63SMatt Spinler      *        in the PEL.
17085f61a63SMatt Spinler      */
17185f61a63SMatt Spinler     std::vector<std::string> _debugData;
172d3335dfaSMatt Spinler };
173d3335dfaSMatt Spinler } // namespace pels
174d3335dfaSMatt Spinler } // namespace openpower
175