Create functional association on startup

Look at /var/lib/phosphor-software-manager/pnor/ro
to determine if there is a funcitonal image, if so
create a functional association.

Resolves openbmc/openbmc#2279

Change-Id: I6e576e2e21a592a98d843eb45cdab8bba8f55192
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index 261b9d6..b451932 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -250,6 +250,13 @@
                                      *this)));
         }
     }
+
+    // Look at the RO symlink to determine if there is a functional image
+    auto id = determineId(PNOR_RO_ACTIVE_PATH);
+    if (!id.empty())
+    {
+        updateFunctionalAssociation(std::string{SOFTWARE_OBJPATH} + '/' + id);
+    }
     return;
 }
 
@@ -559,6 +566,26 @@
     }
 }
 
+std::string ItemUpdater::determineId(const std::string& symlinkPath)
+{
+    if (!fs::exists(symlinkPath))
+    {
+        return {};
+    }
+
+    auto target = fs::canonical(symlinkPath).string();
+
+    // check to make sure the target really exists
+    if (!fs::is_regular_file(target + "/" + PNOR_TOC_FILE))
+    {
+        return {};
+    }
+    // Get the image <id> from the symlink target
+    // for example /media/ro-2a1022fe
+    static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
+    return target.substr(PNOR_RO_PREFIX_LEN);
+}
+
 } // namespace updater
 } // namespace software
 } // namespace openpower
diff --git a/item_updater.hpp b/item_updater.hpp
index 7f9a88b..1c65a1c 100755
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -91,6 +91,14 @@
          */
         void freeSpace();
 
+        /** @brief Determine the software version id
+         *         from the symlink target (e.g. /media/ro-2a1022fe).
+         *
+         * @param[in] symlinkPath - The path of the symlink.
+         * @param[out] id - The version id as a string.
+         */
+        static std::string determineId(const std::string& symlinkPath);
+
         /** @brief Creates an active association to the
          *  newly active software image
          *
diff --git a/watch.cpp b/watch.cpp
index da06bbc..2212e01 100644
--- a/watch.cpp
+++ b/watch.cpp
@@ -8,6 +8,7 @@
 #include <phosphor-logging/log.hpp>
 #include "config.h"
 #include "watch.hpp"
+#include "item_updater.hpp"
 
 namespace openpower
 {
@@ -97,12 +98,7 @@
         path /= event->name;
         if (fs::equivalent(path, PNOR_RO_ACTIVE_PATH))
         {
-            auto target = fs::canonical(path).string();
-
-            // Get the image <id> from the symlink target
-            // for example /media/ro-2a1022fe
-            static const auto PNOR_RO_PREFIX_LEN = strlen(PNOR_RO_PREFIX);
-            auto id = target.substr(PNOR_RO_PREFIX_LEN);
+            auto id = ItemUpdater::determineId(path);
             auto objPath = std::string{SOFTWARE_OBJPATH} + '/' + id;
 
             static_cast<Watch*>(userdata)->functionalCallback(objPath);