platform-mc: Defer adding sensor objects from PDRs

After all PDRs retrieved from the other terminus are parsed into pdr
structs. They will be processed further to create sensor objects (e.g
via `addNumericSensor` function for Numeric Sensors).

During this phase for one sensor, sensor name is achieved (may enlist
Sensor Aux Name PDRs), and NumericSensor object is constructed.
Sensor object construction involves parsing pdr struct elements into
sensor class variables, D-Bus interface initialization and many calls
to set D-Bus object properties.

They are actually not blocking actions, but as it continuously loops
through sensor PDRs in `parseTerminusPDRs` to add sensors, they take
too much time and prevent BMC from processing events coming from other
termini. Not to mention the adding of new sensor types in the future
and the increase in sensor numbers, the total time will be large, while
this is not a primary task in the initialization phase of a terminus.

This commit defers `addNumericSensor` and `addCompactNumericSensor`
calls using sdeventplus::source::Defer event source, instead of
continuously calling them while looping through the sensor PDRs, to let
events coming from other termini break in and be processed by BMC.

Tested:

While BMC is getting, parsing sensor PDRs and creating sensor objects
from those PDRs, events can still be received from the other termini.

Change-Id: I4a3bacd4139b51c302e36614757fa1b5e5105f21
Signed-off-by: Chau Ly <chaul@amperecomputing.com>
diff --git a/platform-mc/sensor_manager.cpp b/platform-mc/sensor_manager.cpp
index 2f9eaa5..46e0786 100644
--- a/platform-mc/sensor_manager.cpp
+++ b/platform-mc/sensor_manager.cpp
@@ -33,21 +33,11 @@
                   tid);
         return;
     }
-    // numeric sensor
-    auto terminus = termini[tid];
-    for (auto& sensor : terminus->numericSensors)
-    {
-        roundRobinSensors[tid].push(sensor);
-    }
+
+    roundRobinSensorItMap[tid] = 0;
 
     updateAvailableState(tid, true);
 
-    if (!roundRobinSensors[tid].size())
-    {
-        lg2::info("Terminus ID {TID}: no sensors to poll.", "TID", tid);
-        return;
-    }
-
     sensorPollTimers[tid] = std::make_unique<sdbusplus::Timer>(
         event.get(),
         std::bind_front(&SensorManager::doSensorPolling, this, tid));
@@ -85,6 +75,11 @@
 
     // numeric sensor
     auto terminus = termini[tid];
+    if (!terminus)
+    {
+        return;
+    }
+
     for (auto& sensor : terminus->numericSensors)
     {
         sensor->updateReading(true, false,
@@ -101,7 +96,7 @@
         sensorPollTimers.erase(tid);
     }
 
-    roundRobinSensors.erase(tid);
+    roundRobinSensorItMap.erase(tid);
 
     if (doSensorPollingTaskHandles.contains(tid))
     {
@@ -203,6 +198,13 @@
         }
 
         auto& terminus = termini[tid];
+        if (!terminus)
+        {
+            lg2::info(
+                "Terminus ID {TID} does not have a valid Terminus object {NOW}.",
+                "TID", tid, "NOW", pldm::utils::getCurrentSystemTime());
+            co_return PLDM_ERROR;
+        }
 
         if (manager && terminus->pollEvent)
         {
@@ -216,7 +218,19 @@
         }
 
         sd_event_now(event.get(), CLOCK_MONOTONIC, &t1);
-        auto toBeUpdated = roundRobinSensors[tid].size();
+
+        auto& numericSensors = terminus->numericSensors;
+        auto toBeUpdated = numericSensors.size();
+
+        if (!roundRobinSensorItMap.contains(tid))
+        {
+            lg2::info(
+                "Terminus ID {TID} does not have a round robin sensor iteration {NOW}.",
+                "TID", tid, "NOW", pldm::utils::getCurrentSystemTime());
+            co_return PLDM_ERROR;
+        }
+        auto& sensorIt = roundRobinSensorItMap[tid];
+
         while (((t1 - t0) < pollingTimeInUsec) && (toBeUpdated > 0))
         {
             if (!getAvailableState(tid))
@@ -227,7 +241,12 @@
                 co_await stdexec::just_stopped();
             }
 
-            auto sensor = roundRobinSensors[tid].front();
+            if (sensorIt >= numericSensors.size())
+            {
+                sensorIt = 0;
+            }
+
+            auto sensor = numericSensors[sensorIt];
 
             sd_event_now(event.get(), CLOCK_MONOTONIC, &t1);
             elapsed = t1 - sensor->timeStamp;
@@ -255,11 +274,8 @@
             }
 
             toBeUpdated--;
-            if (roundRobinSensors.contains(tid))
-            {
-                roundRobinSensors[tid].pop();
-                roundRobinSensors[tid].push(std::move(sensor));
-            }
+            sensorIt++;
+
             sd_event_now(event.get(), CLOCK_MONOTONIC, &t1);
         }