From 47e8984b64fff089ed16bf390b5739e62a53d24e Mon Sep 17 00:00:00 2001
From: wangjue <jue.wang2@luxshare-ict.com>
Date: Tue, 26 Nov 2024 21:09:21 +0800
Subject: [PATCH] switch dimmtempsensor from peci to i3c

Signed-off-by: wangjue <jue.wang2@luxshare-ict.com>
---
 src/DIMMTempSensor.cpp     | 78 +++++++++++++++++++++++---------------
 src/DIMMTempSensor.hpp     |  7 ++--
 src/DIMMTempSensorMain.cpp |  2 +-
 3 files changed, 52 insertions(+), 35 deletions(-)

diff --git a/src/DIMMTempSensor.cpp b/src/DIMMTempSensor.cpp
index d290d63..7c4412f 100644
--- a/src/DIMMTempSensor.cpp
+++ b/src/DIMMTempSensor.cpp
@@ -46,6 +46,10 @@ extern "C"
 constexpr const bool debug = false;
 constexpr auto dimmSensorInterface = "xyz.openbmc_project.Sensor.dimmLED";
 constexpr auto dimmSensorPath = "/xyz/openbmc_project/sensors/temperature/";
+constexpr auto dimmSpdReaderService = "xyz.openbmc_project.DimmSpdReader";
+constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
+constexpr auto tempInterface = "xyz.openbmc_project.Dimm.Temperature";
+
 #define PECI_MBX_INDEX_DDR_DIMM_TEMP 0x0E
 
 static void setupDimmLEDMatch(
@@ -132,9 +136,9 @@ DIMMTempSensor::~DIMMTempSensor()
     objectServer.remove_interface(association);
 }
 
-void DIMMTempSensor::init()
+void DIMMTempSensor::init(const std::string& sensorName)
 {
-    read();
+    read(sensorName);
 }
 
 void DIMMTempSensor::checkThresholds(void)
@@ -142,13 +146,7 @@ void DIMMTempSensor::checkThresholds(void)
     thresholds::checkThresholds(this);
 }
 
-int DIMMTempSensor::getDIMMRegsInfoWord(double* ps64data)
-{
-    int ret = -1;
-    ret = getDIMMRegsTemp(ps64data);
-    return ret;
-}
-
+#if 0
 int DIMMTempSensor::getDIMMRegsTemp(double* ps64data)
 {
     int peciFd = -1;
@@ -192,10 +190,47 @@ err:
     return -1;
 }
 
-void DIMMTempSensor::read()
+#endif
+
+void DIMMTempSensor::getDIMMRegsTemp(const std::string& sensorName)
+{
+    std::string path = "/xyz/openbmc_project/dimm/" + sensorName;
+    size_t tempPos = path.find("_Temp");
+    if (tempPos != std::string::npos) {
+        path = path.substr(0, tempPos);
+    }
+
+    std::weak_ptr<DIMMTempSensor> weakRef = weak_from_this();
+    dbusConnection->async_method_call(
+        [weakRef](const boost::system::error_code ec, const std::variant<double> value) {
+            if (ec)
+            {
+              std::cerr << "Error setting DIMM sensor Temp" << "\n";
+              return;
+            }
+            auto self = weakRef.lock();
+            if (!self)
+            {
+                return;
+            }
+            double v = std::get<double>(value);
+            if constexpr (debug)
+            {
+                std::cerr << "Dimm Temp raw reading " << v << "\n";
+            }
+            v += self->calibOffset;
+            self->updateValue(v);
+
+        },
+        dimmSpdReaderService, path, dbusProperties, "Get", tempInterface, "TS0");
+
+    return;
+}
+
+void DIMMTempSensor::read(const std::string& sensorName)
 {
     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
-    waitTimer.async_wait([this](const boost::system::error_code& ec) {
+    waitTimer.async_wait([this, sensorName](const boost::system::error_code& ec) {
         if (ec == boost::asio::error::operation_aborted)
         {
             return; // we're being cancelled
@@ -208,30 +243,13 @@ void DIMMTempSensor::read()
         }
         if (readingStateGood() && dimmLEDState == "ON")
         {
-            double temp = 0;
-            int ret = getDIMMRegsInfoWord(&temp);
-            if (ret == 0)
-            {
-                double v = temp;
-                if constexpr (debug)
-                {
-                    std::cerr << "Value update to " << v << ", raw reading "
-                              << temp << "\n";
-                }
-                v += calibOffset;
-                updateValue(v);
-            }
-            else
-            {
-                std::cerr << "Fail to read DIMM tempture.\n";
-                incrementError();
-            }
+            getDIMMRegsTemp(sensorName);
         }
         else
         {
             updateValue(std::numeric_limits<double>::quiet_NaN());
         }
-        read();
+        read(sensorName);
     });
 }
 
diff --git a/src/DIMMTempSensor.hpp b/src/DIMMTempSensor.hpp
index 774ace2..5e629da 100644
--- a/src/DIMMTempSensor.hpp
+++ b/src/DIMMTempSensor.hpp
@@ -24,15 +24,14 @@ struct DIMMTempSensor : public Sensor, std::enable_shared_from_this<DIMMTempSens
     ~DIMMTempSensor() override;
 
     void checkThresholds(void) override;
-    void read();
-    void init();
+    void read(const std::string& sensorName);
+    void init(const std::string& sensorName);
     void setupMatches(const std::string& sensorName);
     unsigned int sensorPollMs;
     double calibOffset;
 
   private:
-    int getDIMMRegsInfoWord(double* ps64data);
-    int getDIMMRegsTemp(double* ps64data);
+    void getDIMMRegsTemp(const std::string& sensorName);
    // int getDIMMRegsPower(double* ps64data);
 
     sdbusplus::asio::object_server& objectServer;
diff --git a/src/DIMMTempSensorMain.cpp b/src/DIMMTempSensorMain.cpp
index a8fc05c..b8f002b 100644
--- a/src/DIMMTempSensorMain.cpp
+++ b/src/DIMMTempSensorMain.cpp
@@ -143,7 +143,7 @@ void createSensors(
                 }
 
                 sensor->setupMatches(name);
-                sensor->init();
+                sensor->init(name);
             }
         }
     },
-- 
2.34.1

