[cleanup] mainloop: continue early

Do an early continue if hwmon input attr file is not there, which would
save one nested loop.

Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: I03795240355f96503d62aaf267f168862c298bba
diff --git a/mainloop.cpp b/mainloop.cpp
index d961a10..1e7cd35 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -384,105 +384,104 @@
     for (auto& i : _state)
     {
         auto& attrs = std::get<0>(i.second);
-        if (attrs.find(hwmon::entry::input) != attrs.end())
+        if (attrs.find(hwmon::entry::input) == attrs.end())
         {
-            // Read value from sensor.
-            std::string input = hwmon::entry::cinput;
-            if (i.first.first == "pwm")
+            continue;
+        }
+        // Read value from sensor.
+        std::string input = hwmon::entry::cinput;
+        if (i.first.first == "pwm")
+        {
+            input = "";
+        }
+
+        int64_t value;
+        auto& objInfo = std::get<ObjectInfo>(i.second);
+        auto& obj = std::get<InterfaceMap>(objInfo);
+        std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[i.first];
+
+        auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
+            obj[InterfaceType::STATUS]);
+        // As long as addStatus is called before addValue, statusIface
+        // should never be nullptr.
+        assert(statusIface);
+
+        try
+        {
+            if (sensor->hasFaultFile())
             {
-                input = "";
-            }
-
-            int64_t value;
-            auto& objInfo = std::get<ObjectInfo>(i.second);
-            auto& obj = std::get<InterfaceMap>(objInfo);
-            std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[i.first];
-
-            auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
-                obj[InterfaceType::STATUS]);
-            // As long as addStatus is called before addValue, statusIface
-            // should never be nullptr.
-            assert(statusIface);
-
-            try
-            {
-                if (sensor->hasFaultFile())
+                auto fault = _ioAccess->read(i.first.first, i.first.second,
+                                             hwmon::entry::fault,
+                                             hwmonio::retries, hwmonio::delay);
+                // Skip reading from a sensor with a valid fault file
+                // and set the functional property accordingly
+                if (!statusIface->functional((fault == 0) ? true : false))
                 {
-                    auto fault = _ioAccess->read(
-                        i.first.first, i.first.second, hwmon::entry::fault,
-                        hwmonio::retries, hwmonio::delay);
-                    // Skip reading from a sensor with a valid fault file
-                    // and set the functional property accordingly
-                    if (!statusIface->functional((fault == 0) ? true : false))
-                    {
-                        continue;
-                    }
-                }
-
-                {
-                    // RAII object for GPIO unlock / lock
-                    sensor::GpioLock gpioLock(sensor->getGpio());
-
-                    // Retry for up to a second if device is busy
-                    // or has a transient error.
-                    value =
-                        _ioAccess->read(i.first.first, i.first.second, input,
-                                        hwmonio::retries, hwmonio::delay);
-                    // Set functional property to true if we could read sensor
-                    statusIface->functional(true);
-
-                    value = sensor->adjustValue(value);
-                }
-
-                updateSensorInterfaces(obj, value);
-            }
-            catch (const std::system_error& e)
-            {
-#ifdef UPDATE_FUNCTIONAL_ON_FAIL
-                // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
-                // thrown, set the functional property to false.
-                // We cannot set this with the 'continue' in the lower block
-                // as the code may exit before reaching it.
-                statusIface->functional(false);
-#endif
-                auto file = sysfs::make_sysfs_path(
-                    _ioAccess->path(), i.first.first, i.first.second,
-                    hwmon::entry::cinput);
-
-                // Check sensorAdjusts for sensor removal RCs
-                auto& sAdjusts = _sensorObjects[i.first]->getAdjusts();
-                if (sAdjusts.rmRCs.count(e.code().value()) > 0)
-                {
-                    // Return code found in sensor return code removal list
-                    if (_rmSensors.find(i.first) == _rmSensors.end())
-                    {
-                        // Trace for sensor not already removed from dbus
-                        log<level::INFO>(
-                            "Remove sensor from dbus for read fail",
-                            entry("FILE=%s", file.c_str()),
-                            entry("RC=%d", e.code().value()));
-                        // Mark this sensor to be removed from dbus
-                        _rmSensors[i.first] = std::get<0>(i.second);
-                    }
                     continue;
                 }
-#ifdef UPDATE_FUNCTIONAL_ON_FAIL
-                // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
-                continue;
-#endif
-                using namespace sdbusplus::xyz::openbmc_project::Sensor::
-                    Device::Error;
-                report<ReadFailure>(
-                    xyz::openbmc_project::Sensor::Device::ReadFailure::
-                        CALLOUT_ERRNO(e.code().value()),
-                    xyz::openbmc_project::Sensor::Device::ReadFailure::
-                        CALLOUT_DEVICE_PATH(_devPath.c_str()));
-
-                log<level::INFO>("Logging failing sysfs file",
-                                 entry("FILE=%s", file.c_str()));
-
-                exit(EXIT_FAILURE);
             }
+
+            {
+                // RAII object for GPIO unlock / lock
+                sensor::GpioLock gpioLock(sensor->getGpio());
+
+                // Retry for up to a second if device is busy
+                // or has a transient error.
+                value = _ioAccess->read(i.first.first, i.first.second, input,
+                                        hwmonio::retries, hwmonio::delay);
+                // Set functional property to true if we could read sensor
+                statusIface->functional(true);
+
+                value = sensor->adjustValue(value);
+            }
+
+            updateSensorInterfaces(obj, value);
+        }
+        catch (const std::system_error& e)
+        {
+#ifdef UPDATE_FUNCTIONAL_ON_FAIL
+            // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
+            // thrown, set the functional property to false.
+            // We cannot set this with the 'continue' in the lower block
+            // as the code may exit before reaching it.
+            statusIface->functional(false);
+#endif
+            auto file =
+                sysfs::make_sysfs_path(_ioAccess->path(), i.first.first,
+                                       i.first.second, hwmon::entry::cinput);
+
+            // Check sensorAdjusts for sensor removal RCs
+            auto& sAdjusts = _sensorObjects[i.first]->getAdjusts();
+            if (sAdjusts.rmRCs.count(e.code().value()) > 0)
+            {
+                // Return code found in sensor return code removal list
+                if (_rmSensors.find(i.first) == _rmSensors.end())
+                {
+                    // Trace for sensor not already removed from dbus
+                    log<level::INFO>("Remove sensor from dbus for read fail",
+                                     entry("FILE=%s", file.c_str()),
+                                     entry("RC=%d", e.code().value()));
+                    // Mark this sensor to be removed from dbus
+                    _rmSensors[i.first] = std::get<0>(i.second);
+                }
+                continue;
+            }
+#ifdef UPDATE_FUNCTIONAL_ON_FAIL
+            // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
+            continue;
+#endif
+            using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
+                Error;
+            report<ReadFailure>(
+                xyz::openbmc_project::Sensor::Device::ReadFailure::
+                    CALLOUT_ERRNO(e.code().value()),
+                xyz::openbmc_project::Sensor::Device::ReadFailure::
+                    CALLOUT_DEVICE_PATH(_devPath.c_str()));
+
+            log<level::INFO>("Logging failing sysfs file",
+                             entry("FILE=%s", file.c_str()));
+
+            exit(EXIT_FAILURE);
         }
     }