adc: Only process CPUReq sensors when CPUs change

The adcsensor application watches for the Present property to change on
CPUs so that it can check if any ADC sensors that have the 'CPURequired'
configuration field need changes.

However, in this case it was also deleting and recreating non-CPU
related sensors, such as ones that measure the RTC battery voltage. This
is a problem because if that sensor is below threshold, it will behave
as if the threshold were newly asserted by emitting propertiesChanged
signals on the threshold alarm properties as well as emitting the
ThresholdAsserted signals again.

Fix this by changing the code to only process sensors that have the
CPURequired field when handling CPU presence changes.

Tested:
Check D-Bus signals when CPU presence changes versus when a threshold
configuration property changes and see the expected handling.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I8f00726e71c85e70203a5cb6ee5dd99b4fe9e9c1
diff --git a/src/ADCSensorMain.cpp b/src/ADCSensorMain.cpp
index fd61680..401da12 100644
--- a/src/ADCSensorMain.cpp
+++ b/src/ADCSensorMain.cpp
@@ -46,6 +46,12 @@
 
 static boost::container::flat_map<size_t, bool> cpuPresence;
 
+enum class UpdateType
+{
+    init,
+    cpuPresenceChange
+};
+
 // filter out adc from any other voltage sensor
 bool isAdc(const fs::path& parentPath)
 {
@@ -70,12 +76,13 @@
         sensors,
     std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
     const std::shared_ptr<boost::container::flat_set<std::string>>&
-        sensorsChanged)
+        sensorsChanged,
+    UpdateType updateType)
 {
     auto getter = std::make_shared<GetSensorConfiguration>(
         dbusConnection,
-        [&io, &objectServer, &sensors, &dbusConnection,
-         sensorsChanged](const ManagedObjectType& sensorConfigurations) {
+        [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
+         updateType](const ManagedObjectType& sensorConfigurations) {
             bool firstScan = sensorsChanged == nullptr;
             std::vector<fs::path> paths;
             if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)",
@@ -195,6 +202,27 @@
                         continue;
                     }
                 }
+
+                auto findCPU = baseConfiguration->second.find("CPURequired");
+                if (findCPU != baseConfiguration->second.end())
+                {
+                    size_t index =
+                        std::visit(VariantToIntVisitor(), findCPU->second);
+                    auto presenceFind = cpuPresence.find(index);
+                    if (presenceFind == cpuPresence.end())
+                    {
+                        continue; // no such cpu
+                    }
+                    if (!presenceFind->second)
+                    {
+                        continue; // cpu not installed
+                    }
+                }
+                else if (updateType == UpdateType::cpuPresenceChange)
+                {
+                    continue;
+                }
+
                 std::vector<thresholds::Threshold> sensorThresholds;
                 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
                 {
@@ -237,22 +265,6 @@
                     setReadState(powerState, readState);
                 }
 
-                auto findCPU = baseConfiguration->second.find("CPURequired");
-                if (findCPU != baseConfiguration->second.end())
-                {
-                    size_t index =
-                        std::visit(VariantToIntVisitor(), findCPU->second);
-                    auto presenceFind = cpuPresence.find(index);
-                    if (presenceFind == cpuPresence.end())
-                    {
-                        continue; // no such cpu
-                    }
-                    if (!presenceFind->second)
-                    {
-                        continue; // cpu not installed
-                    }
-                }
-
                 auto& sensor = sensors[sensorName];
                 sensor = nullptr;
 
@@ -322,7 +334,8 @@
         std::make_shared<boost::container::flat_set<std::string>>();
 
     io.post([&]() {
-        createSensors(io, objectServer, sensors, systemBus, nullptr);
+        createSensors(io, objectServer, sensors, systemBus, nullptr,
+                      UpdateType::init);
     });
 
     boost::asio::deadline_timer filterTimer(io);
@@ -349,7 +362,7 @@
                     return;
                 }
                 createSensors(io, objectServer, sensors, systemBus,
-                              sensorsChanged);
+                              sensorsChanged, UpdateType::init);
             });
         };
 
@@ -396,7 +409,8 @@
                     std::cerr << "timer error\n";
                     return;
                 }
-                createSensors(io, objectServer, sensors, systemBus, nullptr);
+                createSensors(io, objectServer, sensors, systemBus, nullptr,
+                              UpdateType::cpuPresenceChange);
             });
         };