Fan: Optimize the pwm sensor creation

Given fan*_input and its corresponding pwm* hwmon sys files are always
under the same diretory, we should use that directory to generate the
pwm sensor rather than deduce it from scratch. This also fixes all
kind of issues that come into play when multiple fan hwmon controllers
exisit on the system.

Tested: the tach and pwm sensors continue to be the same as before.

Change-Id: I54a4059e47190cb9f66e9e11a9f9a373ebf0fc16
Signed-off-by: Yong Zhao <yozhao@google.com>
diff --git a/src/FanMain.cpp b/src/FanMain.cpp
index 4306f16..2e7442d 100644
--- a/src/FanMain.cpp
+++ b/src/FanMain.cpp
@@ -157,14 +157,10 @@
             if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
                            paths))
             {
-                std::cerr << "No temperature sensors in system\n";
+                std::cerr << "No fan sensors in system\n";
                 return;
             }
 
-            // pwm index, sysfs path, pwm name
-            std::vector<std::tuple<uint8_t, std::string, std::string>>
-                pwmNumbers;
-
             // iterate through all found fan sensors, and try to match them with
             // configuration
             for (const auto& path : paths)
@@ -175,8 +171,10 @@
                 std::regex_search(pathStr, match, inputRegex);
                 std::string indexStr = *(match.begin() + 1);
 
-                auto directory = path.parent_path();
+                fs::path directory = path.parent_path();
+                fs::path pwmPath = directory / ("pwm" + indexStr);
                 FanTypes fanType = getFanType(directory);
+
                 size_t bus = 0;
                 size_t address = 0;
                 if (fanType == FanTypes::i2c)
@@ -368,6 +366,7 @@
                     sensorData->find(baseType + std::string(".Connector"));
 
                 std::optional<std::string> led;
+                std::string pwmName;
 
                 if (connector != sensorData->end())
                 {
@@ -380,7 +379,6 @@
                         /* use pwm name override if found in configuration else
                          * use default */
                         auto findOverride = connector->second.find("PwmName");
-                        std::string pwmName;
                         if (findOverride != connector->second.end())
                         {
                             pwmName = std::visit(VariantToStringVisitor(),
@@ -390,7 +388,6 @@
                         {
                             pwmName = "Pwm_" + std::to_string(pwm + 1);
                         }
-                        pwmNumbers.emplace_back(pwm, *interfacePath, pwmName);
                     }
                     else
                     {
@@ -421,47 +418,16 @@
                     std::move(presenceSensor), redundancy, io, sensorName,
                     std::move(sensorThresholds), *interfacePath, limits,
                     powerState, led);
+
+                if (fs::exists(pwmPath) && !pwmSensors.count(pwmPath))
+                {
+                    pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
+                        pwmName, pwmPath, dbusConnection, objectServer,
+                        *interfacePath, "Fan");
+                }
             }
+
             createRedundancySensor(tachSensors, dbusConnection, objectServer);
-            std::vector<fs::path> pwms;
-            if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
-            {
-                std::cerr << "No pwm in system\n";
-                return;
-            }
-            for (const fs::path& pwm : pwms)
-            {
-                if (pwmSensors.find(pwm) != pwmSensors.end())
-                {
-                    continue;
-                }
-                const std::string* path = nullptr;
-                const std::string* pwmName = nullptr;
-
-                for (const auto& [index, configPath, name] : pwmNumbers)
-                {
-                    if (pwm.filename().string() ==
-                        "pwm" + std::to_string(index + 1))
-                    {
-                        path = &configPath;
-                        pwmName = &name;
-                        break;
-                    }
-                }
-
-                if (path == nullptr)
-                {
-                    continue;
-                }
-
-                // only add new elements
-                const std::string& sysPath = pwm.string();
-                pwmSensors.insert(
-                    std::pair<std::string, std::unique_ptr<PwmSensor>>(
-                        sysPath, std::make_unique<PwmSensor>(
-                                     *pwmName, sysPath, dbusConnection,
-                                     objectServer, *path, "Fan")));
-            }
         }));
     getter->getConfiguration(
         std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},