PEL: Add PEL section header structure

A PEL is made up of sections, and every section has an 8B
section header.  This commit adds a SectionHeader structure
that will represent that header.  It will then be included in
all upcoming PEL sections.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Ia5356560f49707e21aebca28f4a0b525aa24158d
diff --git a/extensions/openpower-pels/section_header.hpp b/extensions/openpower-pels/section_header.hpp
new file mode 100644
index 0000000..d87e4e5
--- /dev/null
+++ b/extensions/openpower-pels/section_header.hpp
@@ -0,0 +1,109 @@
+#pragma once
+
+#include "stream.hpp"
+
+#include <cstdint>
+
+namespace openpower
+{
+namespace pels
+{
+
+/**
+ * @class SectionHeader
+ *
+ * This header is at the start of every PEL section.  It has a size
+ * of 8 bytes.
+ */
+struct SectionHeader
+{
+  public:
+    /**
+     * @brief Constructor
+     */
+    SectionHeader() : id(0), size(0), version(0), subType(0), componentID(0)
+    {
+    }
+
+    /**
+     * @brief Constructor
+     *
+     * @param[in] id - the ID field
+     * @param[in] size - the size field
+     * @param[in] version - the version field
+     * @param[in] subType - the sub-type field
+     * @param[in] componentID - the component ID field
+     */
+    SectionHeader(uint16_t id, uint16_t size, uint8_t version, uint8_t subType,
+                  uint16_t componentID) :
+        id(id),
+        size(size), version(version), subType(subType), componentID(componentID)
+    {
+    }
+
+    /**
+     * @brief A two character ASCII field which identifies the section type.
+     */
+    uint16_t id;
+
+    /**
+     * @brief The size of the section in bytes, including this section header.
+     */
+    uint16_t size;
+
+    /**
+     * @brief The section format version.
+     */
+    uint8_t version;
+
+    /**
+     * @brief The section sub-type.
+     */
+    uint8_t subType;
+
+    /**
+     * @brief The component ID, which has various meanings depending on the
+     * section.
+     */
+    uint16_t componentID;
+
+    /**
+     * @brief Returns the size of header when flattened into a PEL.
+     *
+     * @return size_t - the size of the header
+     */
+    static constexpr size_t flattenedSize()
+    {
+        return sizeof(id) + sizeof(size) + sizeof(version) + sizeof(subType) +
+               sizeof(componentID);
+    }
+};
+
+/**
+ * @brief Stream extraction operator for the SectionHeader
+ *
+ * @param[in] s - the stream
+ * @param[out] header - the SectionHeader object
+ */
+inline Stream& operator>>(Stream& s, SectionHeader& header)
+{
+    s >> header.id >> header.size >> header.version >> header.subType >>
+        header.componentID;
+    return s;
+}
+
+/**
+ * @brief Stream insertion operator for the section header
+ *
+ * @param[out] s - the stream
+ * @param[in] header - the SectionHeader object
+ */
+inline Stream& operator<<(Stream& s, SectionHeader& header)
+{
+    s << header.id << header.size << header.version << header.subType
+      << header.componentID;
+    return s;
+}
+
+} // namespace pels
+} // namespace openpower
diff --git a/test/openpower-pels/Makefile.include b/test/openpower-pels/Makefile.include
index a68b229..07e596d 100644
--- a/test/openpower-pels/Makefile.include
+++ b/test/openpower-pels/Makefile.include
@@ -3,6 +3,7 @@
 check_PROGRAMS += \
 	additional_data_test \
 	bcd_time_test \
+	section_header_test \
 	stream_test
 
 additional_data_test_SOURCES = %reldir%/additional_data_test.cpp
@@ -24,4 +25,11 @@
 bcd_time_test_LDADD = \
 	$(test_ldadd) \
 	$(top_builddir)/extensions/openpower-pels/bcd_time.o
-bcd_time_test_LDFLAGS = $(test_ldflags)
\ No newline at end of file
+bcd_time_test_LDFLAGS = $(test_ldflags)
+
+section_header_test_SOURCES = \
+	%reldir%/section_header_test.cpp
+section_header_test_CPPFLAGS = $(test_cppflags)
+section_header_test_CXXFLAGS = $(test_cxxflags)
+section_header_test_LDADD = $(test_ldadd)
+section_header_test_LDFLAGS = $(test_ldflags)
\ No newline at end of file
diff --git a/test/openpower-pels/section_header_test.cpp b/test/openpower-pels/section_header_test.cpp
new file mode 100644
index 0000000..2d5827c
--- /dev/null
+++ b/test/openpower-pels/section_header_test.cpp
@@ -0,0 +1,40 @@
+
+#include "extensions/openpower-pels/section_header.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace openpower::pels;
+
+TEST(SectionHeaderTest, SizeTest)
+{
+    EXPECT_EQ(SectionHeader::flattenedSize(), 8);
+}
+
+TEST(SectionHeaderTest, UnflattenTest)
+{
+    std::vector<uint8_t> data{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
+    Stream reader{data};
+    SectionHeader header;
+
+    reader >> header;
+
+    EXPECT_EQ(header.id, 0x1122);
+    EXPECT_EQ(header.size, 0x3344);
+    EXPECT_EQ(header.version, 0x55);
+    EXPECT_EQ(header.subType, 0x66);
+    EXPECT_EQ(header.componentID, 0x7788);
+}
+
+TEST(SectionHeaderTest, FlattenTest)
+{
+    SectionHeader header{0xAABB, 0xCCDD, 0xEE, 0xFF, 0xA0A0};
+
+    std::vector<uint8_t> data;
+    Stream writer{data};
+
+    writer << header;
+
+    std::vector<uint8_t> expected{0xAA, 0xBB, 0xCC, 0xDD,
+                                  0xEE, 0xFF, 0xA0, 0xA0};
+    EXPECT_EQ(data, expected);
+}