NVMeSensorMain: Extract lambda body

Start unwrapping the onion to improve the ability to understand what's
going on.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I62fd5c9103006633e9ee8f0358e688cf92bd8cc7
diff --git a/src/NVMeSensorMain.cpp b/src/NVMeSensorMain.cpp
index 9c44dee..b017bca 100644
--- a/src/NVMeSensorMain.cpp
+++ b/src/NVMeSensorMain.cpp
@@ -34,6 +34,113 @@
     return nvmeDeviceMap;
 }
 
+static void handleSensorConfigurations(
+    boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
+    std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
+    const ManagedObjectType& sensorConfigurations)
+{
+    // todo: it'd be better to only update the ones we care about
+    for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap)
+    {
+        if (nvmeContextPtr)
+        {
+            nvmeContextPtr->close();
+        }
+    }
+    nvmeDeviceMap.clear();
+
+    // iterate through all found configurations
+    for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor :
+         sensorConfigurations)
+    {
+        const SensorData& sensorData = sensor.second;
+        const std::string& interfacePath = sensor.first.str;
+        const std::pair<std::string, boost::container::flat_map<
+                                         std::string, BasicVariantType>>*
+            baseConfiguration = nullptr;
+
+        // find base configuration
+        auto sensorBase = sensor.second.find(sensorType);
+        if (sensorBase != sensor.second.end())
+        {
+            baseConfiguration = &(*sensorBase);
+        }
+
+        if (baseConfiguration == nullptr)
+        {
+            continue;
+        }
+        auto findBus = baseConfiguration->second.find("Bus");
+        if (findBus == baseConfiguration->second.end())
+        {
+            continue;
+        }
+
+        int busNumber = std::visit(VariantToIntVisitor(), findBus->second);
+
+        auto findSensorName = baseConfiguration->second.find("Name");
+        if (findSensorName == baseConfiguration->second.end())
+        {
+            std::cerr << "could not determine configuration name for "
+                      << interfacePath << "\n";
+            continue;
+        }
+        std::string sensorName = std::get<std::string>(findSensorName->second);
+
+        std::vector<thresholds::Threshold> sensorThresholds;
+
+        if (!parseThresholdsFromConfig(sensorData, sensorThresholds))
+        {
+            std::cerr << "error populating thresholds for " << sensorName
+                      << "\n";
+        }
+
+        int rootBus = busNumber;
+
+        std::string muxPath = "/sys/bus/i2c/devices/i2c-" +
+                              std::to_string(busNumber) + "/mux_device";
+
+        if (std::filesystem::is_symlink(muxPath))
+        {
+            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));
+        }
+
+        std::shared_ptr<NVMeContext> context;
+        auto findRoot = nvmeDeviceMap.find(rootBus);
+        if (findRoot != nvmeDeviceMap.end())
+        {
+            context = findRoot->second;
+        }
+        else
+        {
+#if HAVE_NVME_MI_MCTP
+            context = std::make_shared<NVMeMCTPContext>(io, rootBus);
+#else
+            context = std::make_shared<NVMeBasicContext>(io, rootBus);
+#endif
+            nvmeDeviceMap[rootBus] = context;
+        }
+
+        std::shared_ptr<NVMeSensor> sensorPtr = std::make_shared<NVMeSensor>(
+            objectServer, io, dbusConnection, sensorName,
+            std::move(sensorThresholds), interfacePath, busNumber);
+
+        context->addSensor(sensorPtr);
+    }
+    for (const auto& [_, context] : nvmeDeviceMap)
+    {
+        context->pollNVMeDevices();
+    }
+}
+
 void createSensors(boost::asio::io_service& io,
                    sdbusplus::asio::object_server& objectServer,
                    std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
@@ -43,111 +150,8 @@
         dbusConnection,
         std::move([&io, &objectServer, &dbusConnection](
                       const ManagedObjectType& sensorConfigurations) {
-            // todo: it'd be better to only update the ones we care about
-            for (const auto& [_, nvmeContextPtr] : nvmeDeviceMap)
-            {
-                if (nvmeContextPtr)
-                {
-                    nvmeContextPtr->close();
-                }
-            }
-            nvmeDeviceMap.clear();
-
-            // iterate through all found configurations
-            for (const std::pair<sdbusplus::message::object_path, SensorData>&
-                     sensor : sensorConfigurations)
-            {
-                const SensorData& sensorData = sensor.second;
-                const std::string& interfacePath = sensor.first.str;
-                const std::pair<
-                    std::string,
-                    boost::container::flat_map<std::string, BasicVariantType>>*
-                    baseConfiguration = nullptr;
-
-                // find base configuration
-                auto sensorBase = sensor.second.find(sensorType);
-                if (sensorBase != sensor.second.end())
-                {
-                    baseConfiguration = &(*sensorBase);
-                }
-
-                if (baseConfiguration == nullptr)
-                {
-                    continue;
-                }
-                auto findBus = baseConfiguration->second.find("Bus");
-                if (findBus == baseConfiguration->second.end())
-                {
-                    continue;
-                }
-
-                int busNumber =
-                    std::visit(VariantToIntVisitor(), findBus->second);
-
-                auto findSensorName = baseConfiguration->second.find("Name");
-                if (findSensorName == baseConfiguration->second.end())
-                {
-                    std::cerr << "could not determine configuration name for "
-                              << interfacePath << "\n";
-                    continue;
-                }
-                std::string sensorName =
-                    std::get<std::string>(findSensorName->second);
-
-                std::vector<thresholds::Threshold> sensorThresholds;
-
-                if (!parseThresholdsFromConfig(sensorData, sensorThresholds))
-                {
-                    std::cerr << "error populating thresholds for "
-                              << sensorName << "\n";
-                }
-
-                int rootBus = busNumber;
-
-                std::string muxPath = "/sys/bus/i2c/devices/i2c-" +
-                                      std::to_string(busNumber) + "/mux_device";
-
-                if (std::filesystem::is_symlink(muxPath))
-                {
-                    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));
-                }
-
-                std::shared_ptr<NVMeContext> context;
-                auto findRoot = nvmeDeviceMap.find(rootBus);
-                if (findRoot != nvmeDeviceMap.end())
-                {
-                    context = findRoot->second;
-                }
-                else
-                {
-#if HAVE_NVME_MI_MCTP
-                    context = std::make_shared<NVMeMCTPContext>(io, rootBus);
-#else
-                    context = std::make_shared<NVMeBasicContext>(io, rootBus);
-#endif
-                    nvmeDeviceMap[rootBus] = context;
-                }
-
-                std::shared_ptr<NVMeSensor> sensorPtr =
-                    std::make_shared<NVMeSensor>(
-                        objectServer, io, dbusConnection, sensorName,
-                        std::move(sensorThresholds), interfacePath, busNumber);
-
-                context->addSensor(sensorPtr);
-            }
-            for (const auto& [_, context] : nvmeDeviceMap)
-            {
-                context->pollNVMeDevices();
-            }
+            handleSensorConfigurations(io, objectServer, dbusConnection,
+                                       sensorConfigurations);
         }));
     getter->getConfiguration(std::vector<std::string>{sensorType});
 }