common: fix Software::getPurpose error path

'version' cannot be dereferenced if it is not initialized.

Change the API to return an std::optional and handle in the caller.

Change-Id: Iac7badf145dad64e21ede5e81f0b4b761ff6c89d
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/common/include/software.hpp b/common/include/software.hpp
index a8477bf..2993707 100644
--- a/common/include/software.hpp
+++ b/common/include/software.hpp
@@ -58,9 +58,6 @@
                     SoftwareVersion::VersionPurpose versionPurpose =
                         SoftwareVersion::VersionPurpose::Unknown);
 
-    // Return the version purpose
-    SoftwareVersion::VersionPurpose getPurpose();
-
     // This should populate 'softwareAssociationDefinitions'
     // @param isRunning             if the software version is currently running
     // on the device. Otherwise the software is assumed to be activating (not
@@ -88,6 +85,10 @@
     static long int getRandomId();
 
   protected:
+    // @returns the version purpose
+    // @returns std::nullopt in case the version has not been set
+    std::optional<SoftwareVersion::VersionPurpose> getPurpose();
+
     // @returns        a random software id (swid) for that device
     static std::string getRandomSoftwareId(device::Device& parent);
 
@@ -118,6 +119,7 @@
     sdbusplus::async::context& ctx;
 
     friend update::SoftwareUpdate;
+    friend device::Device;
 };
 
 }; // namespace phosphor::software