utils: Add function to get the latest version

This repo does not know which PSU version is the latest, and it lets the
vendor tool to decide which one is the latest.

This commit defines PSU_VERSION_COMPARE_UTIL which is expected to be a
tool that accepts arguments of a list of versions, and returns the
latest version string.

Tested: Put and configure to use the example get_latest_version on
        Witherspoon, verify the versionId with a larger version string
        is returned.

Signed-off-by: Lei YU <mine260309@gmail.com>
Change-Id: Ic5a10d3724cf6a98b3979486c72d54f8deac7038
diff --git a/src/item_updater.cpp b/src/item_updater.cpp
index 6472fe1..13e6bf4 100644
--- a/src/item_updater.cpp
+++ b/src/item_updater.cpp
@@ -4,6 +4,7 @@
 
 #include "utils.hpp"
 
+#include <cassert>
 #include <filesystem>
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
@@ -271,6 +272,7 @@
     sdbusplus::xyz::openbmc_project::Software::server::Version::VersionPurpose
         versionPurpose)
 {
+    versionStrings.insert(versionString);
     auto version = std::make_unique<Version>(
         bus, objPath, versionId, versionString, versionPurpose,
         std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
@@ -412,6 +414,27 @@
     }
 }
 
+std::optional<std::string> ItemUpdater::getLatestVersionId()
+{
+    auto latestVersion = utils::getLatestVersion(versionStrings);
+    if (latestVersion.empty())
+    {
+        return {};
+    }
+
+    std::optional<std::string> versionId;
+    for (const auto& v : versions)
+    {
+        if (v.second->version() == latestVersion)
+        {
+            versionId = v.first;
+            break;
+        }
+    }
+    assert(versionId.has_value());
+    return versionId;
+}
+
 } // namespace updater
 } // namespace software
 } // namespace phosphor