From 301fd47f7f21a780b06c4403a657d31e967934bf Mon Sep 17 00:00:00 2001
From: wangjue <jue.wang2@luxshare-ict.com>
Date: Fri, 3 Jan 2025 18:57:29 +0800
Subject: [PATCH] Trigger to read dimm temp by vwgpio1

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

%% original patch: 0014-Trigger-to-read-dimm-temp-by-vwgpio1.patch
---
 src/DIMMTempSensor.cpp | 107 +++++++++++++++++++++++++++++++++++++++--
 src/DIMMTempSensor.hpp |   2 +
 2 files changed, 106 insertions(+), 3 deletions(-)

diff --git a/src/DIMMTempSensor.cpp b/src/DIMMTempSensor.cpp
index a0c9d76..852810b 100644
--- a/src/DIMMTempSensor.cpp
+++ b/src/DIMMTempSensor.cpp
@@ -49,6 +49,9 @@ 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";
+constexpr auto HostMiscDbusName = "xyz.openbmc_project.Host.Misc.Manager";
+constexpr auto platformStatePath = "/xyz/openbmc_project/misc/platform_state";
+constexpr auto platformStateInterface = "xyz.openbmc_project.State.Host.Misc";
 
 #define PECI_MBX_INDEX_DDR_DIMM_TEMP 0x0E
 
@@ -78,6 +81,60 @@ static void setupDimmLEDMatch(
                         std::move(eventHandler));
 }
 
+static bool getDbusMsgState(sdbusplus::message_t& msg, bool& value)
+{
+    std::string pltStateInterface;
+    std::string event;
+    boost::container::flat_map<std::string, std::variant<bool>>
+        propertiesChanged;
+    try
+    {
+        msg.read(pltStateInterface, propertiesChanged);
+        if (propertiesChanged.empty())
+        {
+            return false;
+        }
+
+        std::string property = propertiesChanged.begin()->first;
+
+        if (property.empty() || property != "dimmI3cSwitch")
+        {
+            return false;
+        }
+
+        value = std::get<bool>(propertiesChanged.begin()->second);
+        return true;
+    }
+    catch (const std::exception& e)
+    {
+        std::cerr << "exception while reading dbus property dimmI3cSwitch" << "\n";
+        return false;
+    }
+}
+
+static void dimmI3cSwitchMatcher(
+    std::vector<sdbusplus::bus::match_t>& matches, sdbusplus::bus_t& connection,
+    std::function<void(bool)>&& onMatch)
+{
+    auto pulseEventMatcherCallback =
+        [onMatch{std::move(onMatch)}](sdbusplus::message_t& msg) {
+        bool value = false;
+        if (!getDbusMsgState(msg, value))
+        {
+            return;
+        }
+        onMatch(value);
+    };
+
+    matches.emplace_back(
+        connection,
+        "type='signal',interface='org.freedesktop.DBus.Properties',member='"
+        "PropertiesChanged',arg0='" +
+        std::string(platformStateInterface) + "'",
+        std::move(pulseEventMatcherCallback));
+}
+
+
 DIMMTempSensor::DIMMTempSensor(
     std::shared_ptr<sdbusplus::asio::connection>& conn,
     boost::asio::io_context& io, const std::string& sensorName,
@@ -121,6 +178,8 @@ DIMMTempSensor::DIMMTempSensor(
                                              association::interface);
 
     setInitialProperties(units);
+
+    dimmI3cSwitchState = getdimmI3cSwitchState(io);
 }
 
 DIMMTempSensor::~DIMMTempSensor()
@@ -202,10 +261,10 @@ void DIMMTempSensor::getDIMMRegsTemp(const std::string& sensorName)
 
     std::weak_ptr<DIMMTempSensor> weakRef = weak_from_this();
     dbusConnection->async_method_call(
-        [weakRef](const boost::system::error_code ec, const std::variant<double> value) {
+        [weakRef, sensorName](const boost::system::error_code ec, const std::variant<double> value) {
             if (ec)
             {
-              std::cerr << "Error setting DIMM sensor Temp" << "\n";
+              std::cerr << "Error getting DIMM sensor " << sensorName << " Temp" << "\n";
               return;
             }
             auto self = weakRef.lock();
@@ -241,7 +300,7 @@ void DIMMTempSensor::read(const std::string& sensorName)
             std::cerr << "timer error\n";
             return;
         }
-        if (readingStateGood() && dimmLEDState == "ON")
+        if (readingStateGood() && dimmI3cSwitchState)
         {
             getDIMMRegsTemp(sensorName);
         }
@@ -257,6 +316,7 @@ void DIMMTempSensor::read(const std::string& sensorName)
 void DIMMTempSensor::setupMatches(const std::string& sensorName)
 {
     std::weak_ptr<DIMMTempSensor> weakRef = weak_from_this();
+    std::shared_ptr<DIMMTempSensor> sharedRef = weakRef.lock();
     setupDimmLEDMatch(matches, *dbusConnection, sensorName,
         [weakRef, sensorName](const std::string& value) {
             auto self = weakRef.lock();
@@ -272,4 +332,45 @@ void DIMMTempSensor::setupMatches(const std::string& sensorName)
             }
         }
     );
+
+    dimmI3cSwitchMatcher(matches, *dbusConnection, [sharedRef](bool state)
+    {
+        if (!sharedRef)
+        {
+            return;
+        }
+        if (!state)
+        {
+            sharedRef->dimmI3cSwitchState = false;
+        }
+        else
+        {
+            sharedRef->dimmI3cSwitchState = true;
+        }
+        std::cout << "dimmI3cSwitchState: " << (int)sharedRef->dimmI3cSwitchState << std::endl;
+    });
+}
+
+bool DIMMTempSensor::getdimmI3cSwitchState(boost::asio::io_context& io)
+{
+    auto conn = std::make_shared<sdbusplus::asio::connection>(io);
+    auto mesg = conn->new_method_call(HostMiscDbusName, platformStatePath,
+                                      "org.freedesktop.DBus.Properties", "Get");
+    mesg.append(platformStateInterface, "dimmI3cSwitch");
+
+    bool state = false;
+    try
+    {
+        auto resp = conn->call(mesg);
+        std::variant<bool> value;
+        resp.read(value);
+        state = std::get<bool>(value);
+        std::cout << " get dimmI3cSwitch State: " << (int)state << std::endl;
+    }
+    catch (std::exception& e)
+    {
+        std::cerr << "auto shutdown: failed to read node id";
+    }
+
+    return state;
 }
diff --git a/src/DIMMTempSensor.hpp b/src/DIMMTempSensor.hpp
index 5e629da..fc51928 100644
--- a/src/DIMMTempSensor.hpp
+++ b/src/DIMMTempSensor.hpp
@@ -27,6 +27,7 @@ struct DIMMTempSensor : public Sensor, std::enable_shared_from_this<DIMMTempSens
     void read(const std::string& sensorName);
     void init(const std::string& sensorName);
     void setupMatches(const std::string& sensorName);
+    bool getdimmI3cSwitchState(boost::asio::io_context& io);
     unsigned int sensorPollMs;
     double calibOffset;
 
@@ -41,5 +42,6 @@ struct DIMMTempSensor : public Sensor, std::enable_shared_from_this<DIMMTempSens
     uint8_t dimmRank;
     std::string dimmSensorName;
     std::string dimmLEDState;
+    bool dimmI3cSwitchState;
     std::vector<sdbusplus::bus::match_t> matches;
 };
-- 
2.34.1

