From 2a4ceaf845fd605e4f44740e08bd92ddb868c87a Mon Sep 17 00:00:00 2001
From: "Wang.Bin" <Bin-B.Wang@luxshare-ict.com>
Date: Mon, 11 Nov 2024 13:07:48 +0800
Subject: [PATCH 6/6] Add PWM sensors

---
 src/FanMain.cpp   |  6 ++----
 src/PwmSensor.cpp | 15 +++++++++++++++
 src/PwmSensor.hpp |  2 ++
 3 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/FanMain.cpp b/src/FanMain.cpp
index 8c6991e..fc81a93 100644
--- a/src/FanMain.cpp
+++ b/src/FanMain.cpp
@@ -161,7 +161,7 @@ bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
     std::error_code ec;
 
     /* Assuming PWM file is appeared in the same directory as fanX_input */
-    auto path = directory / ("pwm" + std::to_string(pwm + 1));
+    auto path = directory / ("pwm" + std::to_string(pwm));
     bool exists = fs::exists(path, ec);
 
     if (ec || !exists)
@@ -484,12 +484,10 @@ void createSensors(
                                   << " no pwm channel found!\n";
                         continue;
                     }
-
-                    fs::path pwmEnableFile = "pwm" + std::to_string(pwm + 1) +
+                    fs::path pwmEnableFile = "pwm" + std::to_string(pwm) +
                                              "_enable";
                     fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
                     enablePwm(enablePath);
-
                     /* use pwm name override if found in configuration else
                      * use default */
                     auto findOverride = connector->second.find("PwmName");
diff --git a/src/PwmSensor.cpp b/src/PwmSensor.cpp
index ad54dfe..eadf1bc 100644
--- a/src/PwmSensor.cpp
+++ b/src/PwmSensor.cpp
@@ -43,6 +43,19 @@ PwmSensor::PwmSensor(const std::string& name, const std::string& sysPath,
     sensorInterface = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/fan_pwm/" + name,
         "xyz.openbmc_project.Sensor.Value");
+    aInterface = objectServer.add_interface(
+        "/xyz/openbmc_project/sensors/fan_pwm/" + name,
+        "xyz.openbmc_project.State.Decorator.Availability");
+    fInterface = objectServer.add_interface(
+        "/xyz/openbmc_project/sensors/fan_pwm/" + name,
+        "xyz.openbmc_project.State.Decorator.OperationalStatus");
+
+
+    aInterface->register_property("Available", true,[](const bool &req, bool &res)
+                                {res = req;return true; });
+    fInterface->register_property("Functional", true,[](const bool &req, bool &res)
+                                {res = req;return true; });
+
     uint32_t pwmValue = getValue(false);
     if (sensorType == "PSU")
     {
@@ -150,6 +163,8 @@ PwmSensor::PwmSensor(const std::string& name, const std::string& sysPath,
     });
 
     sensorInterface->initialize();
+    aInterface->initialize();
+    fInterface->initialize();
     controlInterface->initialize();
 
     if (isValueMutable)
diff --git a/src/PwmSensor.hpp b/src/PwmSensor.hpp
index f70079b..a1f2277 100644
--- a/src/PwmSensor.hpp
+++ b/src/PwmSensor.hpp
@@ -22,6 +22,8 @@ class PwmSensor
     sdbusplus::asio::object_server& objectServer;
     std::string name;
     std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
+    std::shared_ptr<sdbusplus::asio::dbus_interface> aInterface;
+    std::shared_ptr<sdbusplus::asio::dbus_interface> fInterface;
     std::shared_ptr<sdbusplus::asio::dbus_interface> controlInterface;
     std::shared_ptr<sdbusplus::asio::dbus_interface> association;
     std::shared_ptr<sdbusplus::asio::dbus_interface> valueMutabilityInterface;
-- 
2.25.1

