Add the BMC code version to error logs

Add the xyz.openbmc_project.Software.Version interface to
the elog entries.  This allows a user to know what BMC code
level was running when the error was created.  The level is
persisted along with the other elog fields.

If this code is flashed on a system that was running older code,
and there were existing error logs, the version property will
be left empty in the restored log entries.

Older code is still able to restore logs created by this code
as the version property is at the end of the serialized data
and so is just ignored by Cereal.

Resolves openbmc/openbmc#3133

Tested:  Check that new error logs have the code level, and that
         restarting phosphor-log-manager preserves that property
         on the existing logs.  Various incantations of running
         the older code with logs created by this code, and running
         this code with logs created by older code.

Change-Id: I682aa3bf97c8352ce6dda05dfdf55d33173de891
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/configure.ac b/configure.ac
index 2e4cdd1..d9c022d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -76,6 +76,8 @@
 AC_DEFINE(CALLOUT_FWD_ASSOCIATION, "callout", [The name of the callout's forward association.])
 AC_DEFINE(CALLOUT_REV_ASSOCIATION, "fault", [The name of the callout's reverse association.])
 AC_DEFINE(BMC_VERSION_FILE, "/etc/os-release", [The file that contains the BMC firmware version])
+AC_DEFINE(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL, "2",
+          [First Cereal class version with the BMC code version persisted])
 
 AC_ARG_VAR(YAML_DIR_TEST, [The path to the test error yaml files.])
 AS_IF([test "x$YAML_DIR_TEST" == "x"], \
@@ -109,7 +111,7 @@
     [Cap on informational (and below) severity errors])
 
 AC_ARG_VAR(CLASS_VERSION, [Class version to register with Cereal])
-AS_IF([test "x$CLASS_VERSION" == "x"], [CLASS_VERSION=1])
+AS_IF([test "x$CLASS_VERSION" == "x"], [CLASS_VERSION=2])
 AC_DEFINE_UNQUOTED([CLASS_VERSION], [$CLASS_VERSION], [Class version to register with Cereal])
 
 AC_CONFIG_HEADERS([config.h])
diff --git a/elog_entry.hpp b/elog_entry.hpp
index 8dcc2a2..5ee8f5f 100644
--- a/elog_entry.hpp
+++ b/elog_entry.hpp
@@ -4,6 +4,7 @@
 #include <sdbusplus/server/object.hpp>
 #include "xyz/openbmc_project/Logging/Entry/server.hpp"
 #include "xyz/openbmc_project/Object/Delete/server.hpp"
+#include "xyz/openbmc_project/Software/Version/server.hpp"
 #include "org/openbmc/Associations/server.hpp"
 
 namespace phosphor
@@ -14,7 +15,8 @@
 using EntryIfaces = sdbusplus::server::object::object<
     sdbusplus::xyz::openbmc_project::Logging::server::Entry,
     sdbusplus::xyz::openbmc_project::Object::server::Delete,
-    sdbusplus::org::openbmc::server::Associations>;
+    sdbusplus::org::openbmc::server::Associations,
+    sdbusplus::xyz::openbmc_project::Software::server::Version>;
 
 using AssociationList =
      std::vector<std::tuple<std::string, std::string, std::string>>;
@@ -50,6 +52,8 @@
          *  @param[in] severityErr - The severity of the error.
          *  @param[in] msgErr - The message of the error.
          *  @param[in] additionalDataErr - The error metadata.
+         *  @param[in] objects - The list of associations.
+         *  @param[in] fwVersion - The BMC code version.
          *  @param[in] parent - The error's parent.
          */
         Entry(sdbusplus::bus::bus& bus,
@@ -60,6 +64,7 @@
               std::string&& msgErr,
               std::vector<std::string>&& additionalDataErr,
               AssociationList&& objects,
+              const std::string& fwVersion,
               internal::Manager& parent) :
               EntryIfaces(bus, path.c_str(), true),
               parent(parent)
@@ -75,6 +80,9 @@
             sdbusplus::xyz::openbmc_project::
                 Logging::server::Entry::resolved(false);
 
+            version(fwVersion);
+            purpose(VersionPurpose::BMC);
+
             // Emit deferred signal.
             this->emit_object_added();
         };
diff --git a/elog_serialize.cpp b/elog_serialize.cpp
index 1af1946..d807a93 100644
--- a/elog_serialize.cpp
+++ b/elog_serialize.cpp
@@ -29,7 +29,8 @@
 void save(Archive& a, const Entry& e, const std::uint32_t version)
 {
     a(e.id(), e.severity(), e.timestamp(),
-      e.message(), e.additionalData(), e.associations(), e.resolved());
+      e.message(), e.additionalData(), e.associations(), e.resolved(),
+      e.version());
 }
 
 /** @brief Function required by Cereal to perform deserialization.
@@ -52,9 +53,18 @@
     std::vector<std::string> additionalData{};
     bool resolved{};
     AssociationList associations{};
+    std::string fwVersion{};
 
-    a(id, severity, timestamp, message,
-      additionalData, associations, resolved);
+    if (version < std::stoul(FIRST_CEREAL_CLASS_VERSION_WITH_FWLEVEL))
+    {
+        a(id, severity, timestamp, message,
+          additionalData, associations, resolved);
+    }
+    else
+    {
+        a(id, severity, timestamp, message,
+          additionalData, associations, resolved, fwVersion);
+    }
 
     e.id(id);
     e.severity(severity);
@@ -64,6 +74,9 @@
     e.sdbusplus::xyz::openbmc_project::
         Logging::server::Entry::resolved(resolved);
     e.associations(associations);
+    e.version(fwVersion);
+    e.purpose(sdbusplus::xyz::openbmc_project::Software::
+        server::Version::VersionPurpose::BMC);
 }
 
 fs::path serialize(const Entry& e, const fs::path& dir)
diff --git a/log_manager.cpp b/log_manager.cpp
index 641ed7b..f808e4b 100644
--- a/log_manager.cpp
+++ b/log_manager.cpp
@@ -201,6 +201,7 @@
                  std::move(errMsg),
                  std::move(additionalData),
                  std::move(objects),
+                 fwVersion,
                  *this);
     serialize(*e);
     entries.insert(std::make_pair(entryId, std::move(e)));
diff --git a/test/serialization_test_properties.cpp b/test/serialization_test_properties.cpp
index 6c8ddf3..be9a7c7 100644
--- a/test/serialization_test_properties.cpp
+++ b/test/serialization_test_properties.cpp
@@ -16,6 +16,7 @@
     std::vector<std::string> testData{"additional", "data"};
     uint64_t timestamp{100};
     std::string message{"test error"};
+    std::string fwLevel{"level42"};
     auto input = std::make_unique<Entry>(
                      bus,
                      std::string(OBJ_ENTRY) + '/' + std::to_string(id),
@@ -25,6 +26,7 @@
                      std::move(message),
                      std::move(testData),
                      std::move(assocations),
+                     fwLevel,
                      manager);
     auto path = serialize(*input, TestSerialization::dir);
 
@@ -44,6 +46,8 @@
     EXPECT_EQ(input->additionalData(), output->additionalData());
     EXPECT_EQ(input->resolved(), output->resolved());
     EXPECT_EQ(input->associations(), output->associations());
+    EXPECT_EQ(input->version(), output->version());
+    EXPECT_EQ(input->purpose(), output->purpose());
 }
 
 } // namespace test