PEL: Add PEL class

This class represents a Platform Event Log.

A PEL consists of sections, and this commit just adds support for the
only required sections - the Private Header and User Header, by
including those specific objects.  More will be added in the future.

The only constructor provided in this commit is to construct the object
from an existing flattened PEL buffer.  This is for use in the case when
a PEL is received from off the BMC, such as from the host.  Future
commits will add support for creating PELs from OpenBMC event logs.

Since there aren't objects yet for every PEL section, the class cannot
make a full flattened PEL without still keeping around the original PEL
data it received in the constructor as mentioned above.  So for now it
will keep that data and just overlay the sections it does support when
flattening.  In the future, a fully formed PEL will be able to be
constructed from just flattening the section objects in the correct
order.

This commit provides a few public methods of note:

* data() - returns a flattened PEL
* assignID() - sets a unique ID in the log ID field in the Private
               Header
* setCommitTime() - Sets the commit timestamp in the Private Header
                    to the current time
* valid() - Says if the PEL is properly formed

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I2a9d82df9cd096ce77ecca7b2f73b097b8368aa2
diff --git a/extensions/openpower-pels/pel.cpp b/extensions/openpower-pels/pel.cpp
new file mode 100644
index 0000000..3000864
--- /dev/null
+++ b/extensions/openpower-pels/pel.cpp
@@ -0,0 +1,85 @@
+#include "pel.hpp"
+
+#include "bcd_time.hpp"
+#include "log_id.hpp"
+#include "stream.hpp"
+
+namespace openpower
+{
+namespace pels
+{
+
+PEL::PEL(const std::vector<uint8_t>& data) : PEL(data, 0)
+{
+}
+
+PEL::PEL(const std::vector<uint8_t>& data, uint32_t obmcLogID) : _rawPEL(data)
+{
+    populateFromRawData(obmcLogID);
+}
+
+void PEL::populateFromRawData(uint32_t obmcLogID)
+{
+    Stream pelData{_rawPEL};
+    _ph = std::make_unique<PrivateHeader>(pelData);
+    if (obmcLogID != 0)
+    {
+        _ph->obmcLogID() = obmcLogID;
+    }
+
+    _uh = std::make_unique<UserHeader>(pelData);
+}
+
+bool PEL::valid() const
+{
+    bool valid = _ph->valid();
+
+    if (valid)
+    {
+        valid = _uh->valid();
+    }
+
+    return valid;
+}
+
+void PEL::setCommitTime()
+{
+    auto now = std::chrono::system_clock::now();
+    _ph->commitTimestamp() = getBCDTime(now);
+}
+
+void PEL::assignID()
+{
+    _ph->id() = generatePELID();
+}
+
+void PEL::flatten(std::vector<uint8_t>& pelBuffer)
+{
+    Stream pelData{pelBuffer};
+    if (_ph->valid())
+    {
+        pelData << *_ph;
+    }
+    else
+    {
+        return;
+    }
+
+    if (_uh->valid())
+    {
+        pelData << *_uh;
+    }
+}
+
+std::vector<uint8_t> PEL::data()
+{
+    // Until we can recreate a complete PEL from objects, need to just flatten
+    // on top of the original PEL data which we need to keep around for this
+    // reason.
+
+    flatten(_rawPEL);
+    return _rawPEL;
+}
+
+} // namespace pels
+} // namespace openpower