fan-sensors: add optional pwm name override

Problem: pwm sensors created by FanSensors have a naming scheme that is
hard coded. There are systems that need to have pwm sensors
named differently.

Solution: In a fan connector configuration the PwmName property will now
attempt to be detected. If this property exists then the name in PwmName
will be used; otherwise the pwm sensor name will be generated using the
old method.

Tested:
Entity-Manager json configuration fragment used:

        {
            "Name": "fan0_connector",
            "Pwm": 0,
            "PwmName": "fan0_pwm",
            "Status": "enabled",
            "Tachs": [0],
            "Type": "FanConnector"
        },
        {
            "Name": "fan1_connector",
            "Pwm": 1,
            "PwmName": "fan1_pwm",
            "Status": "enabled",
            "Tachs": [1],
            "Type": "FanConnector"
        },
        {
            "Name": "fan2_connector",
            "Pwm": 2,
            "Status": "enabled",
            "Tachs": [2],
            "Type": "FanConnector"
        },

results when running ipmitool sdr list on BMC:

Pwm_3            | 99.96 unspecifi   | ok
fan0_pwm         | 99.96 unspecifi   | ok
fan1_pwm         | 99.96 unspecifi   | ok
fan0_tach        | 0 RPM             | ok
fan1_tach        | 0 RPM             | ok
fan2_tach        | 0 RPM             | ok

Signed-off-by: Jason Ling <jasonling@google.com>
Change-Id: I780c4f377e2e4f23567e1196ae420bf3c57c05f2
diff --git a/src/FanMain.cpp b/src/FanMain.cpp
index 9f78fc7..8c47d76 100644
--- a/src/FanMain.cpp
+++ b/src/FanMain.cpp
@@ -104,7 +104,9 @@
                 return;
             }
 
-            std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
+            // 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
@@ -316,10 +318,22 @@
                         std::cerr << "Connector Missing PWM!\n";
                         continue;
                     }
-
                     size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
                                             findPwm->second);
-                    pwmNumbers.emplace_back(pwm, *interfacePath);
+                    /* 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(),
+                                             findOverride->second);
+                    }
+                    else
+                    {
+                        pwmName = "Pwm_" + std::to_string(pwm + 1);
+                    }
+                    pwmNumbers.emplace_back(pwm, *interfacePath, pwmName);
                 }
             }
             std::vector<fs::path> pwms;
@@ -335,12 +349,15 @@
                     continue;
                 }
                 const std::string* path = nullptr;
-                for (const auto& [index, configPath] : pwmNumbers)
+                const std::string* pwmName = nullptr;
+
+                for (const auto& [index, configPath, name] : pwmNumbers)
                 {
                     if (boost::ends_with(pwm.string(),
                                          std::to_string(index + 1)))
                     {
                         path = &configPath;
+                        pwmName = &name;
                         break;
                     }
                 }
@@ -352,12 +369,10 @@
 
                 // only add new elements
                 const std::string& sysPath = pwm.string();
-                const std::string& pwmName =
-                    "Pwm_" + sysPath.substr(sysPath.find_last_of("pwm") + 1);
                 pwmSensors.insert(
                     std::pair<std::string, std::unique_ptr<PwmSensor>>(
                         sysPath, std::make_unique<PwmSensor>(
-                                     pwmName, sysPath, dbusConnection,
+                                     *pwmName, sysPath, dbusConnection,
                                      objectServer, *path, "Fan")));
             }
         }));