From b7052285e077a632bd3cc123e1327fb58540c30c Mon Sep 17 00:00:00 2001
From: wangjue <jue.wang2@luxshare-ict.com>
Date: Wed, 5 Mar 2025 19:30:42 +0800
Subject: [PATCH] Fix incorrect config state for intel cpu sensors

Signed-off-by: wangjue <jue.wang2@luxshare-ict.com>

%% original patch: 0018-Fix-incorrect-config-state-for-intel-cpu-sensors.patch
---
 src/IntelCPUSensorMain.cpp | 47 +++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 19 deletions(-)

diff --git a/src/IntelCPUSensorMain.cpp b/src/IntelCPUSensorMain.cpp
index b6dc544..bdb6d25 100644
--- a/src/IntelCPUSensorMain.cpp
+++ b/src/IntelCPUSensorMain.cpp
@@ -571,7 +571,6 @@ void detectCpu(boost::asio::steady_timer& pingTimer,
         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
         if (peci_Ping(config.addr) == PECI_CC_SUCCESS)
         {
-            bool dimmReady = false;
             for (unsigned int rank = 0; rank < rankNumMax; rank++)
             {
                 std::array<uint8_t, 8> pkgConfig{};
@@ -585,7 +584,7 @@ void detectCpu(boost::asio::steady_timer& pingTimer,
                     if ((pkgConfig[0] != 0U) || (pkgConfig[1] != 0U) ||
                         (pkgConfig[2] != 0U))
                     {
-                        dimmReady = true;
+                        //pkgConfig for DIMM_TEMP will return 0 if i3c is switched on BMC side.
                         break;
                     }
                 }
@@ -595,14 +594,7 @@ void detectCpu(boost::asio::steady_timer& pingTimer,
                 }
             }
 
-            if (dimmReady)
-            {
-                newState = State::READY;
-            }
-            else
-            {
-                newState = State::ON;
-            }
+            newState = State::ON;
         }
 
         if (config.state != newState)
@@ -634,29 +626,46 @@ void detectCpu(boost::asio::steady_timer& pingTimer,
                 {
                     rescanDelaySeconds = 3;
                 }
-                else if (newState == State::READY)
-                {
-                    rescanDelaySeconds = 5;
-                    std::cout << "DIMM(s) on " << config.name
-                              << " is/are detected\n";
-                }
             }
 
             config.state = newState;
         }
 
-        if (config.state != State::READY)
+        std::vector<fs::path> peciDimmDevPaths;
+        std::ostringstream searchDimmPath;
+        searchDimmPath << std::hex << "peci-" << config.bus << "/" << config.bus
+                << "-" << config.addr;
+
+        std::string peciDevDir = "/sys/bus/peci/devices/";
+        findFiles(fs::path(peciDevDir + searchDimmPath.str()),
+                R"(peci-dimmpower.+/hwmon/hwmon\d+/name$)", peciDimmDevPaths,
+                3);
+        if (!peciDimmDevPaths.empty())
         {
-            keepPinging = true;
+            config.state = State::READY;
         }
 
         if (debug)
         {
-            std::cout << config.name << ", state: " << config.state << "\n";
+            std::cout << config.name << ", state: " << config.state << std::endl;
         }
+
         peci_Unlock(peciFd);
     }
 
+    for (CPUConfig& config : cpuConfigs)
+    {
+        if(config.state == State::READY)
+        {
+            keepPinging = false;
+        }
+        else
+        {
+            keepPinging = true;
+            break;
+        }
+    }
+
     if (rescanDelaySeconds != 0U)
     {
         creationTimer.expires_after(std::chrono::seconds(rescanDelaySeconds));
-- 
2.34.1

