Move base version code to phosphor-bmc-code-mgmt
The version code should handle both the host and the bmc images.
Moving from openpower-pnor-code-mgmt to phosphor-bmc-code-mgmt.
This code has been reviewed before, the only changes are renaming
to "namespace phosphor" and renaming the files to
version from version_host_software_manager. I also remove
setting the Purpose to Host.
Change-Id: I80849ce680beee0d09de686bab8fb53152996476
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/version.cpp b/version.cpp
new file mode 100644
index 0000000..9c86756
--- /dev/null
+++ b/version.cpp
@@ -0,0 +1,77 @@
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <fstream>
+#include <stdexcept>
+#include <phosphor-logging/log.hpp>
+#include "version.hpp"
+
+namespace phosphor
+{
+namespace software
+{
+namespace manager
+{
+
+using namespace phosphor::logging;
+
+std::string Version::getVersion(const std::string& manifestFilePath)
+{
+ constexpr auto versionKey = "version=";
+ constexpr auto versionKeySize = strlen(versionKey);
+
+ if (manifestFilePath.empty())
+ {
+ log<level::ERR>("Error MANIFESTFilePath is empty");
+ throw std::runtime_error("MANIFESTFilePath is empty");
+ }
+
+ std::string version{};
+ std::ifstream efile;
+ std::string line;
+ efile.exceptions(std::ifstream::failbit
+ | std::ifstream::badbit
+ | std::ifstream::eofbit);
+
+ // Too many GCC bugs (53984, 66145) to do this the right way...
+ try
+ {
+ efile.open(manifestFilePath);
+ while (getline(efile, line))
+ {
+ if (line.compare(0, versionKeySize, versionKey) == 0)
+ {
+ version = line.substr(versionKeySize);
+ break;
+ }
+ }
+ efile.close();
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>("Error in reading Host MANIFEST file");
+ }
+
+ return version;
+}
+
+std::string Version::getId(const std::string& version)
+{
+ std::stringstream hexId;
+
+ if (version.empty())
+ {
+ log<level::ERR>("Error Host version is empty");
+ throw std::runtime_error("Host version is empty");
+ }
+
+ // Only want 8 hex digits.
+ hexId << std::hex << ((std::hash<std::string> {}(
+ version)) & 0xFFFFFFFF);
+ return hexId.str();
+}
+
+} // namespace manager
+} // namespace software
+} // namepsace phosphor
+
diff --git a/version.hpp b/version.hpp
new file mode 100755
index 0000000..6c45e0a
--- /dev/null
+++ b/version.hpp
@@ -0,0 +1,60 @@
+#pragma once
+
+#include <sdbusplus/bus.hpp>
+#include "xyz/openbmc_project/Software/Version/server.hpp"
+
+namespace phosphor
+{
+namespace software
+{
+namespace manager
+{
+
+using VersionInherit = sdbusplus::server::object::object<
+ sdbusplus::xyz::openbmc_project::Software::server::Version>;
+
+/** @class Version
+ * @brief OpenBMC version software management implementation.
+ * @details A concrete implementation for xyz.openbmc_project.Software.Version
+ * DBus API.
+ */
+class Version : public VersionInherit
+{
+ public:
+ /** @brief Constructs Version Software Manager
+ *
+ * @param[in] bus - The Dbus bus object
+ * @param[in] objPath - The Dbus object path
+ * @param[in] versionId - The Host version identifier
+ */
+ Version(sdbusplus::bus::bus& bus,
+ const std::string& objPath,
+ const std::string& versionId) : VersionInherit(
+ bus, (objPath).c_str(), true)
+ {
+ // Set properties.
+ version(versionId);
+
+ // Emit deferred signal.
+ emit_object_added();
+ }
+
+ /**
+ * @brief Get the code version identifier.
+ *
+ * @return The version identifier.
+ **/
+ static std::string getVersion(const std::string& manifestFilePath);
+
+ /**
+ * @brief Get the Host Version id.
+ *
+ * @return The id.
+ **/
+ static std::string getId(const std::string& version);
+};
+
+} // namespace manager
+} // namespace software
+} // namespace phosphor
+