From 1bb10963f47654a6fed6826bb3131401508d86cc Mon Sep 17 00:00:00 2001
From: wangjue <jue.wang2@luxshare-ict.com>
Date: Thu, 23 Jan 2025 19:07:55 +0800
Subject: [PATCH] Add retry in case unable to open intelcpu sensor file node
 and initialize dimm LED to "Orange"

Signed-off-by: wangjue <jue.wang2@luxshare-ict.com>
---
 src/DIMMTempSensor.cpp |  2 +-
 src/IntelCPUSensor.cpp | 18 ++++++++++++++----
 src/IntelCPUSensor.hpp |  1 +
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/DIMMTempSensor.cpp b/src/DIMMTempSensor.cpp
index 0fe9b83..d2c2f65 100644
--- a/src/DIMMTempSensor.cpp
+++ b/src/DIMMTempSensor.cpp
@@ -152,7 +152,7 @@ DIMMTempSensor::DIMMTempSensor(
            maxReading, minReading, conn, readState),
     sensorPollMs(static_cast<unsigned int>(pollRate * 1000)), calibOffset(calibOffset),
     objectServer(objectServer), waitTimer(io), peciBus(peciBus), peciAddr(peciAddr),
-    dimmRank(rank), dimmSensorName(sensorName), dimmLEDState("OFF")
+    dimmRank(rank), dimmSensorName(sensorName), dimmLEDState("NA")
 {
     std::string interfacePath = "/xyz/openbmc_project/sensors/" + sensorType +
                                 "/" + name;
diff --git a/src/IntelCPUSensor.cpp b/src/IntelCPUSensor.cpp
index abde76f..260ea77 100644
--- a/src/IntelCPUSensor.cpp
+++ b/src/IntelCPUSensor.cpp
@@ -47,7 +47,7 @@ IntelCPUSensor::IntelCPUSensor(
     objServer(objectServer), inputDev(io), waitTimer(io),
     nameTcontrol("Tcontrol CPU" + std::to_string(cpuId)), path(path),
     privTcontrol(std::numeric_limits<double>::quiet_NaN()),
-    dtsOffset(dtsOffset), show(show), pollTime(IntelCPUSensor::sensorPollMs)
+    dtsOffset(dtsOffset), show(show), pollTime(IntelCPUSensor::sensorPollMs), retry(10)
 
 {
     if (show)
@@ -140,10 +140,20 @@ void IntelCPUSensor::setupRead(void)
         fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
         if (fd < 0)
         {
-            std::cerr << name << " unable to open fd!\n";
-            return;
+            std::cerr << name << " unable to open fd! Error code: " << errno
+                  << " (" << strerror(errno) << ")\n";
+            if(retry > 0)
+            {
+                std::cout << "Call restartRead to retry opening fd, retry " << (int)retry << std::endl;
+                retry--;
+                restartRead();
+            }
+            else
+            {
+                std::cout << "Still can't open fd after retry 10 times" << std::endl;
+                return;
+            }
         }
-
         inputDev.assign(fd);
     }
     else
diff --git a/src/IntelCPUSensor.hpp b/src/IntelCPUSensor.hpp
index b928894..37e5694 100644
--- a/src/IntelCPUSensor.hpp
+++ b/src/IntelCPUSensor.hpp
@@ -62,6 +62,7 @@ class IntelCPUSensor :
     void checkThresholds(void) override;
     void updateMinMaxValues(void);
     void restartRead(void);
+    uint8_t retry;
 };
 
 extern boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
-- 
2.34.1

