From c049cf37710acb91b0361267048f8a71600677c1 Mon Sep 17 00:00:00 2001
From: wangjue <jue.wang2@luxshare-ict.com>
Date: Thu, 24 Oct 2024 16:40:34 +0800
Subject: [PATCH] Add dimmLEDInterface and dimmLEDState property

Signed-off-by: wangjue <jue.wang2@luxshare-ict.com>
---
 src/DIMMTempSensor.cpp     | 51 ++++++++++++++++++++++++++++++++------
 src/DIMMTempSensor.hpp     |  7 ++++--
 src/DIMMTempSensorMain.cpp |  1 +
 src/sensor.hpp             |  1 +
 4 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/src/DIMMTempSensor.cpp b/src/DIMMTempSensor.cpp
index 6fba752..94843d2 100644
--- a/src/DIMMTempSensor.cpp
+++ b/src/DIMMTempSensor.cpp
@@ -60,7 +60,7 @@ DIMMTempSensor::DIMMTempSensor(
            std::move(thresholdData), sensorConfiguration, "", false, false,
            maxReading, minReading, conn, readState),
     sensorPollMs(static_cast<unsigned int>(pollRate * 1000)), peciBus(peciBus),
-    peciAddr(peciAddr), dimmRank(rank), calibOffset(calibOffset),
+    peciAddr(peciAddr), dimmRank(rank), calibOffset(calibOffset), dimmLEDState("OFF"),
     objectServer(objectServer), waitTimer(io)
 {
     std::string interfacePath = "/xyz/openbmc_project/sensors/" + sensorType +
@@ -69,6 +69,16 @@ DIMMTempSensor::DIMMTempSensor(
     sensorInterface = objectServer.add_interface(
         interfacePath, "xyz.openbmc_project.Sensor.Value");
 
+    dimmLEDInterface = objectServer.add_interface(
+        interfacePath, "xyz.openbmc_project.Sensor.dimmLED");
+
+    dimmLEDInterface->register_property(
+                "LEDState", dimmLEDState, [this](const std::string& newValue, std::string& oldValue) {
+                    oldValue = newValue; return true;
+                });
+
+    dimmLEDInterface->initialize();
+
     for (const auto& threshold : thresholds)
     {
         std::string interface = thresholds::getInterface(threshold.level);
@@ -95,7 +105,7 @@ DIMMTempSensor::~DIMMTempSensor()
     objectServer.remove_interface(association);
 }
 
-void DIMMTempSensor::init(void)
+void DIMMTempSensor::init()
 {
     read();
 }
@@ -124,7 +134,7 @@ int DIMMTempSensor::getDIMMRegsTemp(double* ps64data)
     {
         std::cerr << "unable to open " << peciDevPath << " "
                     << std::strerror(errno) << "\n";
-        return -1;
+        goto err;
     }
 
     if (peci_Ping(peciAddr) == PECI_CC_SUCCESS)
@@ -137,20 +147,25 @@ int DIMMTempSensor::getDIMMRegsTemp(double* ps64data)
                                 &cc) != PECI_CC_SUCCESS)
         {
             std::cerr << "PECI RdPkgConfig returns failure" << "\n";
-            return -1;
+            goto err;
         }
 
         *ps64data = (double)pkgConfig[0];
 
     } else {
         std::cerr << "PECI Ping returns failure" << "\n";
-        return -1;
+        goto err;
     }
+
     peci_Unlock(peciFd);
     return 0;
+
+err:
+    peci_Unlock(peciFd);
+    return -1;
 }
 
-void DIMMTempSensor::read(void)
+void DIMMTempSensor::read()
 {
     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
     waitTimer.async_wait([this](const boost::system::error_code& ec) {
@@ -177,18 +192,40 @@ void DIMMTempSensor::read(void)
                               << temp << "\n";
                 }
                 v += calibOffset;
+
+
+                updateDIMMLedState(dimmLEDInterface, dimmLEDState, "ON", "LEDState");
                 updateValue(v);
             }
             else
             {
-                std::cerr << "Invalid read getDIMMRegsInfoWord.\n";
+                std::cerr << "Fail to read DIMM tempture.\n";
+                updateDIMMLedState(dimmLEDInterface, dimmLEDState, "OFF", "LEDState");
                 incrementError();
             }
         }
         else
         {
             updateValue(std::numeric_limits<double>::quiet_NaN());
+            updateDIMMLedState(dimmLEDInterface, dimmLEDState, "OFF", "LEDState");
         }
         read();
     });
 }
+
+void DIMMTempSensor::updateDIMMLedState(std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
+        std::string& oldState, const std::string& newState, const char* dbusPropertyName)
+{
+    if(oldState == newState)
+        return;
+    oldState = newState;
+
+    if (interface &&
+        !(interface->set_property(dbusPropertyName, newState)))
+    {
+        std::cerr << "error setting property " << dbusPropertyName
+                    << " to " << newState << "\n";
+    }
+
+    return;
+}
diff --git a/src/DIMMTempSensor.hpp b/src/DIMMTempSensor.hpp
index dd52053..9aea017 100644
--- a/src/DIMMTempSensor.hpp
+++ b/src/DIMMTempSensor.hpp
@@ -24,8 +24,8 @@ struct DIMMTempSensor : public Sensor
     ~DIMMTempSensor() override;
 
     void checkThresholds(void) override;
-    void read(void);
-    void init(void);
+    void read();
+    void init();
 
     unsigned int sensorPollMs;
 
@@ -33,10 +33,13 @@ struct DIMMTempSensor : public Sensor
     uint8_t peciAddr;
     uint8_t dimmRank;
     double calibOffset;
+    std::string dimmLEDState;
 
   private:
     int getDIMMRegsInfoWord(double* ps64data);
     int getDIMMRegsTemp(double* ps64data);
+    void updateDIMMLedState(std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
+        std::string& oldState, const std::string& newState, const char* dbusPropertyName);
    // int getDIMMRegsPower(double* ps64data);
     sdbusplus::asio::object_server& objectServer;
     boost::asio::steady_timer waitTimer;
diff --git a/src/DIMMTempSensorMain.cpp b/src/DIMMTempSensorMain.cpp
index 042659f..5c3573c 100644
--- a/src/DIMMTempSensorMain.cpp
+++ b/src/DIMMTempSensorMain.cpp
@@ -87,6 +87,7 @@ void createSensors(
                 uint8_t peciBus = loadVariant<uint8_t>(entry.second, "Bus");
                 uint8_t peciAddr = loadVariant<uint8_t>(entry.second, "Address");
                 uint8_t dimmRank = loadVariant<uint8_t>(entry.second, "Rank");
+                //std::string LEDState = loadVariant<std::string>(entry.second, "LEDState");
 
                 /* calibration offset */
                 auto findCalibOffset = entry.second.find("Offset");
diff --git a/src/sensor.hpp b/src/sensor.hpp
index e48036a..2c46771 100644
--- a/src/sensor.hpp
+++ b/src/sensor.hpp
@@ -97,6 +97,7 @@ struct Sensor
     std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface;
     std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface;
     std::shared_ptr<sdbusplus::asio::dbus_interface> valueMutabilityInterface;
+    std::shared_ptr<sdbusplus::asio::dbus_interface> dimmLEDInterface;
     double value = std::numeric_limits<double>::quiet_NaN();
     double rawValue = std::numeric_limits<double>::quiet_NaN();
     bool overriddenState = false;
-- 
2.34.1

