Determine the host software id

The id is determined by hashing the PNOR version.
For now, assume the pnor partitions are located at
/tmp/pnor. Part of openbmc/openbmc#1276. The PNOR
version is read from pnor.toc

Change-Id: Iddab7bb4ae5c350c60abf93057ddd997c97fa84c
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/version_host_software_manager.cpp b/version_host_software_manager.cpp
old mode 100755
new mode 100644
index 1fc562e..7719270
--- a/version_host_software_manager.cpp
+++ b/version_host_software_manager.cpp
@@ -1,3 +1,9 @@
+#include <iostream>
+#include <string>
+#include <sstream>
+#include <fstream>
+#include <stdexcept>
+#include <phosphor-logging/log.hpp>
 #include "version_host_software_manager.hpp"
 
 namespace openpower
@@ -7,6 +13,64 @@
 namespace manager
 {
 
+using namespace phosphor::logging;
+
+std::string Version::getVersion(const std::string& tocFilePath)
+{
+    constexpr auto versionKey = "version=";
+    constexpr auto versionKeySize = strlen(versionKey);
+
+    if (tocFilePath.empty())
+    {
+        log<level::ERR>("Error TocFilePath is empty");
+        throw std::runtime_error("TocFilePath 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(tocFilePath);
+        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 pnor.toc 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 openpower