NVMeSensorMain: Define deriveRootBus()

Continue lifting code from the lambda to simplify its implementation.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Ib125c05a8f8aa10e0a0c1da4036ef134edaeb563
diff --git a/src/NVMeSensorMain.cpp b/src/NVMeSensorMain.cpp
index 2621674..1b48248 100644
--- a/src/NVMeSensorMain.cpp
+++ b/src/NVMeSensorMain.cpp
@@ -70,6 +70,31 @@
            "/mux_device";
 }
 
+static std::optional<int> deriveRootBus(std::optional<int> busNumber)
+{
+    if (!busNumber)
+    {
+        return std::nullopt;
+    }
+
+    std::filesystem::path muxPath = deriveRootBusPath(*busNumber);
+
+    if (!std::filesystem::is_symlink(muxPath))
+    {
+        return *busNumber;
+    }
+
+    std::string rootName = std::filesystem::read_symlink(muxPath).filename();
+    size_t dash = rootName.find('-');
+    if (dash == std::string::npos)
+    {
+        std::cerr << "Error finding root bus for " << rootName << "\n";
+        return std::nullopt;
+    }
+
+    return std::stoi(rootName.substr(0, dash));
+}
+
 static void handleSensorConfigurations(
     boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
     std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
@@ -129,25 +154,14 @@
                       << "\n";
         }
 
-        int rootBus = *busNumber;
-
-        std::filesystem::path muxPath = deriveRootBusPath(*busNumber);
-
-        if (std::filesystem::is_symlink(muxPath))
+        std::optional<int> rootBus = deriveRootBus(busNumber);
+        if (!rootBus)
         {
-            std::string rootName =
-                std::filesystem::read_symlink(muxPath).filename();
-            size_t dash = rootName.find('-');
-            if (dash == std::string::npos)
-            {
-                std::cerr << "Error finding root bus for " << rootName << "\n";
-                continue;
-            }
-            rootBus = std::stoi(rootName.substr(0, dash));
+            continue;
         }
 
         std::shared_ptr<NVMeContext> context;
-        auto findRoot = nvmeDeviceMap.find(rootBus);
+        auto findRoot = nvmeDeviceMap.find(*rootBus);
         if (findRoot != nvmeDeviceMap.end())
         {
             context = findRoot->second;
@@ -155,11 +169,11 @@
         else
         {
 #if HAVE_NVME_MI_MCTP
-            context = std::make_shared<NVMeMCTPContext>(io, rootBus);
+            context = std::make_shared<NVMeMCTPContext>(io, *rootBus);
 #else
-            context = std::make_shared<NVMeBasicContext>(io, rootBus);
+            context = std::make_shared<NVMeBasicContext>(io, *rootBus);
 #endif
-            nvmeDeviceMap[rootBus] = context;
+            nvmeDeviceMap[*rootBus] = context;
         }
 
         std::shared_ptr<NVMeSensor> sensorPtr = std::make_shared<NVMeSensor>(