platform-mc: Terminus: Add getSensorNames function

`addNumericSensor` and `addCompactNumericSensor` functions currently try
to construct the sensor name it self. This causes code duplication.

This commit separates that work into `getSensorNames` function, which
returns a vector of sensor name strings that might enlist Sensor
Auxiliary Names PDRs. Depending on the existence of the mentioned PDR,
and the sensor count that the PDR supports, the size of the returned
vector might vary. At this time, callers only get the first element of
the vector, but this function can cover future implementations of
Compact Numeric Sensor.

Tested: There's no functional change applied, so the behavior is the
same after this commit.

Change-Id: I02844f5d0820b0dea2d16d1695350e92ef0383d2
Signed-off-by: Chau Ly <chaul@amperecomputing.com>
diff --git a/platform-mc/terminus.cpp b/platform-mc/terminus.cpp
index d59ac26..ba299be 100644
--- a/platform-mc/terminus.cpp
+++ b/platform-mc/terminus.cpp
@@ -291,7 +291,7 @@
             std::fill(std::begin(alignedBuffer), std::end(alignedBuffer), 0);
             if (u16NameStringLen > PLDM_STR_UTF_16_MAX_LEN)
             {
-                lg2::error("Sensor name to long.");
+                lg2::error("Sensor name too long.");
                 return nullptr;
             }
             memcpy(alignedBuffer, ptr, u16NameStringLen * sizeof(uint16_t));
@@ -391,7 +391,6 @@
 void Terminus::addNumericSensor(
     const std::shared_ptr<pldm_numeric_sensor_value_pdr> pdr)
 {
-    uint16_t sensorId = pdr->sensor_id;
     if (terminusName.empty())
     {
         lg2::error(
@@ -399,29 +398,28 @@
             "TID", tid);
         return;
     }
-    std::string sensorName =
-        terminusName + "_" + "Sensor_" + std::to_string(pdr->sensor_id);
 
-    if (pdr->sensor_auxiliary_names_pdr)
+    if (!pdr)
     {
-        auto sensorAuxiliaryNames = getSensorAuxiliaryNames(sensorId);
-        if (sensorAuxiliaryNames)
-        {
-            const auto& [sensorId, sensorCnt, sensorNames] =
-                *sensorAuxiliaryNames;
-            if (sensorCnt == 1)
-            {
-                for (const auto& [languageTag, name] : sensorNames[0])
-                {
-                    if (languageTag == "en" && !name.empty())
-                    {
-                        sensorName = terminusName + "_" + name;
-                    }
-                }
-            }
-        }
+        lg2::error(
+            "Terminus ID {TID}: Skip adding Numeric Sensor - invalid pointer to PDR.",
+            "TID", tid);
+        return;
     }
 
+    auto sensorId = pdr->sensor_id;
+    auto sensorNames = getSensorNames(sensorId);
+
+    if (sensorNames.empty())
+    {
+        lg2::error(
+            "Terminus ID {TID}: Failed to get name for Numeric Sensor {SID}",
+            "TID", tid, "SID", sensorId);
+        return;
+    }
+
+    std::string sensorName = sensorNames.front();
+
     try
     {
         auto sensor = std::make_shared<NumericSensor>(
@@ -505,7 +503,6 @@
 void Terminus::addCompactNumericSensor(
     const std::shared_ptr<pldm_compact_numeric_sensor_pdr> pdr)
 {
-    uint16_t sensorId = pdr->sensor_id;
     if (terminusName.empty())
     {
         lg2::error(
@@ -513,25 +510,28 @@
             "TID", tid);
         return;
     }
-    std::string sensorName =
-        terminusName + "_" + "Sensor_" + std::to_string(pdr->sensor_id);
 
-    auto sensorAuxiliaryNames = getSensorAuxiliaryNames(sensorId);
-    if (sensorAuxiliaryNames)
+    if (!pdr)
     {
-        const auto& [sensorId, sensorCnt, sensorNames] = *sensorAuxiliaryNames;
-        if (sensorCnt == 1)
-        {
-            for (const auto& [languageTag, name] : sensorNames[0])
-            {
-                if (languageTag == "en" && !name.empty())
-                {
-                    sensorName = terminusName + "_" + name;
-                }
-            }
-        }
+        lg2::error(
+            "Terminus ID {TID}: Skip adding Compact Numeric Sensor - invalid pointer to PDR.",
+            "TID", tid);
+        return;
     }
 
+    auto sensorId = pdr->sensor_id;
+    auto sensorNames = getSensorNames(sensorId);
+
+    if (sensorNames.empty())
+    {
+        lg2::error(
+            "Terminus ID {TID}: Failed to get name for Compact Numeric Sensor {SID}",
+            "TID", tid, "SID", sensorId);
+        return;
+    }
+
+    std::string sensorName = sensorNames.front();
+
     try
     {
         auto sensor = std::make_shared<NumericSensor>(
@@ -707,5 +707,51 @@
     }
 }
 
+std::vector<std::string> Terminus::getSensorNames(const SensorId& sensorId)
+{
+    std::vector<std::string> sensorNames;
+    std::string defaultName =
+        std::format("{}_Sensor_{}", terminusName, unsigned(sensorId));
+    // To ensure there's always a default name at offset 0
+    sensorNames.emplace_back(defaultName);
+
+    auto sensorAuxiliaryNames = getSensorAuxiliaryNames(sensorId);
+    if (!sensorAuxiliaryNames)
+    {
+        return sensorNames;
+    }
+
+    const auto& [id, sensorCount, nameMap] = *sensorAuxiliaryNames;
+    for (const unsigned int& i :
+         std::views::iota(0, static_cast<int>(sensorCount)))
+    {
+        auto sensorName = defaultName;
+        if (i > 0)
+        {
+            // Sensor name at offset 0 will be the default name
+            sensorName += "_" + std::to_string(i);
+        }
+
+        for (const auto& [languageTag, name] : nameMap[i])
+        {
+            if (languageTag == "en" && !name.empty())
+            {
+                sensorName = std::format("{}_{}", terminusName, name);
+            }
+        }
+
+        if (i >= sensorNames.size())
+        {
+            sensorNames.emplace_back(sensorName);
+        }
+        else
+        {
+            sensorNames[i] = sensorName;
+        }
+    }
+
+    return sensorNames;
+}
+
 } // namespace platform_mc
 } // namespace pldm