psusensor: simplify the labelMatch table

It might be more efficient to simplify the labelMatch table by not
listing all labels. PSUProperty of the same type are mostly the same,
except for fan properties. Listing all labels can limit flexibility
and necessitate table modifications when adding new properties.
Simplifying the table could help alleviate these constraints.

Tested:
(1) Sensors defined in "Labels" (e.g. tempN, powerN, fanN, ...) can
be created successfully.
(2) PWM sensors (/sys/class/hwmon/hwmonX/fanN_target) can be created
with the names, "Pwm_PSUNAME_Fan_1", "Pwm_PSUNAME_Fan_2" ...
(3) FanFault PSUSubEvents can be asserted.

Change-Id: I789cdd5c5d0c5c92cca45700bd2095c166bdc564
Signed-off-by: Cosmo Chou <cosmo.chou@quantatw.com>
diff --git a/src/PSUSensorMain.cpp b/src/PSUSensorMain.cpp
index 85ca833..cd49ee4 100644
--- a/src/PSUSensorMain.cpp
+++ b/src/PSUSensorMain.cpp
@@ -155,9 +155,7 @@
     pwmSensors;
 static boost::container::flat_map<std::string, std::string> sensorTable;
 static boost::container::flat_map<std::string, PSUProperty> labelMatch;
-static boost::container::flat_map<std::string, std::string> pwmTable;
 static EventPathList eventMatch;
-static GroupEventPathList groupEventMatch;
 static EventPathList limitEventMatch;
 
 static boost::container::flat_map<size_t, bool> cpuPresence;
@@ -193,34 +191,21 @@
 // Check Group Events which contains more than one targets in each combine
 // events.
 void checkGroupEvent(const std::string& directory,
-                     const GroupEventPathList& groupEventMatch,
                      GroupEventPathList& groupEventPathList)
 {
-    for (const auto& match : groupEventMatch)
+    EventPathList pathList;
+    std::vector<fs::path> eventPaths;
+    if (!findFiles(fs::path(directory), R"(fan\d+_(alarm|fault))", eventPaths))
     {
-        const std::string& groupEventName = match.first;
-        const EventPathList events = match.second;
-        EventPathList pathList;
-        for (const auto& match : events)
-        {
-            const std::string& eventName = match.first;
-            const std::vector<std::string>& eventAttrs = match.second;
-            for (const auto& eventAttr : eventAttrs)
-            {
-                std::string eventPath = directory;
-                eventPath += "/";
-                eventPath += eventAttr;
-                std::ifstream eventFile(eventPath);
-                if (!eventFile.good())
-                {
-                    continue;
-                }
-
-                pathList[eventName].push_back(eventPath);
-            }
-        }
-        groupEventPathList[groupEventName] = pathList;
+        return;
     }
+
+    for (const auto& eventPath : eventPaths)
+    {
+        std::string attrName = eventPath.filename();
+        pathList[attrName.substr(0, attrName.find('_'))].push_back(eventPath);
+    }
+    groupEventPathList["FanFault"] = pathList;
 }
 
 // Function checkEventLimits will check all the psu related xxx_input attributes
@@ -270,40 +255,38 @@
                    sdbusplus::asio::object_server& objectServer,
                    const std::string& psuName)
 {
-    for (const auto& [pwmLabel, pwmName] : pwmTable)
+    if (!labelHead.starts_with("fan"))
     {
-        if (pwmLabel != labelHead)
-        {
-            continue;
-        }
-
-        const std::string& sensorPathStr = sensorPath.string();
-        const std::string& pwmPathStr =
-            boost::replace_all_copy(sensorPathStr, "input", "target");
-        std::ifstream pwmFile(pwmPathStr);
-        if (!pwmFile.good())
-        {
-            continue;
-        }
-
-        auto findPWMSensor = pwmSensors.find(psuName + labelHead);
-        if (findPWMSensor != pwmSensors.end())
-        {
-            continue;
-        }
-
-        std::string name = "Pwm_";
-        name += psuName;
-        name += "_";
-        name += pwmName;
-
-        std::string objPath = interfacePath;
-        objPath += "_";
-        objPath += pwmName;
-
-        pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
-            name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
+        return;
     }
+    std::string labelHeadIndex = labelHead.substr(3);
+
+    const std::string& sensorPathStr = sensorPath.string();
+    const std::string& pwmPathStr = boost::replace_all_copy(sensorPathStr,
+                                                            "input", "target");
+    std::ifstream pwmFile(pwmPathStr);
+    if (!pwmFile.good())
+    {
+        return;
+    }
+
+    auto findPWMSensor = pwmSensors.find(psuName + labelHead);
+    if (findPWMSensor != pwmSensors.end())
+    {
+        return;
+    }
+
+    std::string name = "Pwm_";
+    name += psuName;
+    name += "_Fan_";
+    name += labelHeadIndex;
+
+    std::string objPath = interfacePath;
+    objPath += "_Fan_";
+    objPath += labelHeadIndex;
+
+    pwmSensors[psuName + labelHead] = std::make_unique<PwmSensor>(
+        name, pwmPathStr, dbusConnection, objectServer, objPath, "PSU");
 }
 
 static void createSensorsCallback(
@@ -513,8 +496,7 @@
             sensorsChanged->erase(it);
         }
         checkEvent(directory.string(), eventMatch, eventPathList);
-        checkGroupEvent(directory.string(), groupEventMatch,
-                        groupEventPathList);
+        checkGroupEvent(directory.string(), groupEventPathList);
 
         PowerState readState = getPowerState(*baseConfig);
 
@@ -674,13 +656,13 @@
                 }
             }
 
-            auto findProperty = labelMatch.find(labelHead);
+            auto findProperty = labelMatch.find(sensorNameSubStr);
             if (findProperty == labelMatch.end())
             {
                 if constexpr (debug)
                 {
                     std::cerr << "Could not find matching default property for "
-                              << labelHead << "\n";
+                              << sensorNameSubStr << "\n";
                 }
                 continue;
             }
@@ -809,6 +791,7 @@
             // then prefix/suffix composition becomes not necessary,
             // and in fact not wanted, because it gets in the way.
             std::string psuNameFromIndex;
+            std::string nameIndexStr = "1";
             if (!customizedName)
             {
                 /* Find out sensor name index for this label */
@@ -816,7 +799,8 @@
                 size_t nameIndex{0};
                 if (std::regex_search(labelHead, matches, rgx))
                 {
-                    nameIndex = std::stoi(matches[1]);
+                    nameIndexStr = matches[1];
+                    nameIndex = std::stoi(nameIndexStr);
 
                     // Decrement to preserve alignment, because hwmon
                     // human-readable filenames and labels use 1-based
@@ -927,6 +911,13 @@
                 // Sensor name not customized, do prefix/suffix composition,
                 // preserving default behavior by using psuNameFromIndex.
                 sensorName = psuNameFromIndex + " " + psuProperty.labelTypeName;
+
+                // The labelTypeName of a fan can be:
+                // "Fan Speed 1", "Fan Speed 2", "Fan Speed 3" ...
+                if (labelHead == "fan" + nameIndexStr)
+                {
+                    sensorName += nameIndexStr;
+                }
             }
 
             if constexpr (debug)
@@ -1083,103 +1074,22 @@
 
     labelMatch = {
         {"pin", PSUProperty("Input Power", 3000, 0, 6, 0)},
-        {"pin1", PSUProperty("Input Power", 3000, 0, 6, 0)},
-        {"pin2", PSUProperty("Input Power", 3000, 0, 6, 0)},
-        {"pout1", PSUProperty("Output Power", 3000, 0, 6, 0)},
-        {"pout2", PSUProperty("Output Power", 3000, 0, 6, 0)},
-        {"pout3", PSUProperty("Output Power", 3000, 0, 6, 0)},
-        {"power1", PSUProperty("Output Power", 3000, 0, 6, 0)},
-        {"power2", PSUProperty("Output Power", 3000, 0, 6, 0)},
-        {"power3", PSUProperty("Output Power", 3000, 0, 6, 0)},
-        {"power4", PSUProperty("Output Power", 3000, 0, 6, 0)},
+        {"pout", PSUProperty("Output Power", 3000, 0, 6, 0)},
+        {"power", PSUProperty("Output Power", 3000, 0, 6, 0)},
         {"maxpin", PSUProperty("Max Input Power", 3000, 0, 6, 0)},
         {"vin", PSUProperty("Input Voltage", 300, 0, 3, 0)},
-        {"vin1", PSUProperty("Input Voltage", 300, 0, 3, 0)},
-        {"vin2", PSUProperty("Input Voltage", 300, 0, 3, 0)},
         {"maxvin", PSUProperty("Max Input Voltage", 300, 0, 3, 0)},
-        {"in_voltage0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in_voltage1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in_voltage2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in_voltage3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout8", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout9", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout10", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout11", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout12", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout13", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout14", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout15", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout16", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout17", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout18", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout19", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout20", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout21", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout22", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout23", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout24", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout25", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout26", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout27", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout28", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout29", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout30", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout31", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"vout32", PSUProperty("Output Voltage", 255, 0, 3, 0)},
+        {"in_voltage", PSUProperty("Output Voltage", 255, 0, 3, 0)},
+        {"vout", PSUProperty("Output Voltage", 255, 0, 3, 0)},
         {"vmon", PSUProperty("Auxiliary Input Voltage", 255, 0, 3, 0)},
-        {"in0", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in1", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in2", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in3", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in4", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in5", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in6", PSUProperty("Output Voltage", 255, 0, 3, 0)},
-        {"in7", PSUProperty("Output Voltage", 255, 0, 3, 0)},
+        {"in", PSUProperty("Output Voltage", 255, 0, 3, 0)},
         {"iin", PSUProperty("Input Current", 20, 0, 3, 0)},
-        {"iin1", PSUProperty("Input Current", 20, 0, 3, 0)},
-        {"iin2", PSUProperty("Input Current", 20, 0, 3, 0)},
-        {"iout1", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout2", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout3", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout4", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout5", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout6", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout7", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout8", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout9", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout10", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout11", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout12", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout13", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"iout14", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"curr1", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"curr2", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"curr3", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"curr4", PSUProperty("Output Current", 255, 0, 3, 0)},
-        {"maxiout1", PSUProperty("Max Output Current", 255, 0, 3, 0)},
-        {"temp1", PSUProperty("Temperature", 127, -128, 3, 0)},
-        {"temp2", PSUProperty("Temperature", 127, -128, 3, 0)},
-        {"temp3", PSUProperty("Temperature", 127, -128, 3, 0)},
-        {"temp4", PSUProperty("Temperature", 127, -128, 3, 0)},
-        {"temp5", PSUProperty("Temperature", 127, -128, 3, 0)},
-        {"temp6", PSUProperty("Temperature", 127, -128, 3, 0)},
-        {"maxtemp1", PSUProperty("Max Temperature", 127, -128, 3, 0)},
-        {"fan1", PSUProperty("Fan Speed 1", 30000, 0, 0, 0)},
-        {"fan2", PSUProperty("Fan Speed 2", 30000, 0, 0, 0)},
-        {"fan3", PSUProperty("Fan Speed 3", 30000, 0, 0, 0)},
-        {"fan4", PSUProperty("Fan Speed 4", 30000, 0, 0, 0)}};
-
-    pwmTable = {{"fan1", "Fan_1"},
-                {"fan2", "Fan_2"},
-                {"fan3", "Fan_3"},
-                {"fan4", "Fan_4"}};
+        {"iout", PSUProperty("Output Current", 255, 0, 3, 0)},
+        {"curr", PSUProperty("Output Current", 255, 0, 3, 0)},
+        {"maxiout", PSUProperty("Max Output Current", 255, 0, 3, 0)},
+        {"temp", PSUProperty("Temperature", 127, -128, 3, 0)},
+        {"maxtemp", PSUProperty("Max Temperature", 127, -128, 3, 0)},
+        {"fan", PSUProperty("Fan Speed ", 30000, 0, 0, 0)}};
 
     limitEventMatch = {{"PredictiveFailure", {"max_alarm", "min_alarm"}},
                        {"Failure", {"crit_alarm", "lcrit_alarm"}}};
@@ -1189,12 +1099,6 @@
                   {"ACLost", {"in1_beep"}},
                   {"ConfigureError", {"in1_fault"}}};
 
-    groupEventMatch = {{"FanFault",
-                        {{"fan1", {"fan1_alarm", "fan1_fault"}},
-                         {"fan2", {"fan2_alarm", "fan2_fault"}},
-                         {"fan3", {"fan3_alarm", "fan3_fault"}},
-                         {"fan4", {"fan4_alarm", "fan4_fault"}}}}};
-
     devParamMap = {
         {DevTypes::HWMON, {1, R"(\w\d+_input$)", "([A-Za-z]+)[0-9]*_"}},
         {DevTypes::IIO,