Add associations for pwm

Pwm doesn't inheirt from Sensor as it is special, so
it needs it to be added seperate.

Tested: Saw associations on dbus

Change-Id: I2178e9346dcae845b47a013a2cc15f62e9ed9b31
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/PwmSensor.hpp b/include/PwmSensor.hpp
index 0a712fb..67aff56 100644
--- a/include/PwmSensor.hpp
+++ b/include/PwmSensor.hpp
@@ -6,7 +6,8 @@
 {
   public:
     PwmSensor(const std::string& sysPath,
-              sdbusplus::asio::object_server& objectServer);
+              sdbusplus::asio::object_server& objectServer,
+              const std::string& sensorConfiguration);
     ~PwmSensor();
 
   private:
@@ -15,6 +16,7 @@
     std::string name;
     std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
     std::shared_ptr<sdbusplus::asio::dbus_interface> controlInterface;
+    std::shared_ptr<sdbusplus::asio::dbus_interface> association;
     void setValue(uint32_t value);
     uint32_t getValue(bool errThrow = true);
 };
diff --git a/include/Utils.hpp b/include/Utils.hpp
index a1c9d37..edaccc0 100644
--- a/include/Utils.hpp
+++ b/include/Utils.hpp
@@ -6,6 +6,7 @@
 #include <iostream>
 #include <regex>
 #include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/asio/object_server.hpp>
 #include <sdbusplus/message/types.hpp>
 
 const constexpr char* jsonStore = "/var/configuration/flattened.json";
@@ -41,6 +42,10 @@
     const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
     ManagedObjectType& resp, bool useCache = false);
 
+void createAssociation(
+    std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
+    const std::string& path);
+
 // replaces limits if MinReading and MaxReading are found.
 void findLimits(std::pair<double, double>& limits,
                 const SensorBaseConfiguration* data);
diff --git a/include/sensor.hpp b/include/sensor.hpp
index a6bf2b1..607eb3d 100644
--- a/include/sensor.hpp
+++ b/include/sensor.hpp
@@ -50,16 +50,7 @@
     void
         setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
     {
-        if (association)
-        {
-            using Association =
-                std::tuple<std::string, std::string, std::string>;
-            std::vector<Association> associations;
-            associations.push_back(
-                Association("inventory", "sensors", configurationPath));
-            association->register_property("associations", associations);
-            association->initialize();
-        }
+        createAssociation(association, configurationPath);
 
         sensorInterface->register_property("MaxValue", maxValue);
         sensorInterface->register_property("MinValue", minValue);
diff --git a/src/FanMain.cpp b/src/FanMain.cpp
index 244c104..701edba 100644
--- a/src/FanMain.cpp
+++ b/src/FanMain.cpp
@@ -92,7 +92,7 @@
         return;
     }
 
-    std::vector<uint8_t> pwmNumbers;
+    std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
 
     // iterate through all found fan sensors, and try to match them with
     // configuration
@@ -284,7 +284,7 @@
 
             size_t pwm =
                 std::visit(VariantToUnsignedIntVisitor(), findPwm->second);
-            pwmNumbers.emplace_back(pwm);
+            pwmNumbers.emplace_back(pwm, *interfacePath);
         }
     }
     std::vector<fs::path> pwms;
@@ -299,17 +299,17 @@
         {
             continue;
         }
-        bool inConfig = false;
-        for (uint8_t index : pwmNumbers)
+        const std::string* path = nullptr;
+        for (const auto& [index, configPath] : pwmNumbers)
         {
             if (boost::ends_with(pwm.string(), std::to_string(index + 1)))
             {
-                inConfig = true;
+                path = &configPath;
                 break;
             }
         }
 
-        if (!inConfig)
+        if (path == nullptr)
         {
             continue;
         }
@@ -317,7 +317,7 @@
         // only add new elements
         pwmSensors.insert(std::pair<std::string, std::unique_ptr<PwmSensor>>(
             pwm.string(),
-            std::make_unique<PwmSensor>(pwm.string(), objectServer)));
+            std::make_unique<PwmSensor>(pwm.string(), objectServer, *path)));
     }
 }
 
diff --git a/src/PwmSensor.cpp b/src/PwmSensor.cpp
index 047d5ea..b157d52 100644
--- a/src/PwmSensor.cpp
+++ b/src/PwmSensor.cpp
@@ -13,7 +13,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 */
-#include <PwmSensor.hpp>
+#include "PwmSensor.hpp"
+
+#include "Utils.hpp"
+
 #include <fstream>
 #include <iostream>
 #include <sdbusplus/asio/object_server.hpp>
@@ -22,7 +25,8 @@
 static constexpr size_t pwmMin = 0;
 
 PwmSensor::PwmSensor(const std::string& sysPath,
-                     sdbusplus::asio::object_server& objectServer) :
+                     sdbusplus::asio::object_server& objectServer,
+                     const std::string& sensorConfiguration) :
     sysPath(sysPath),
     objectServer(objectServer)
 {
@@ -106,6 +110,11 @@
         });
     sensorInterface->initialize();
     controlInterface->initialize();
+
+    association = objectServer.add_interface(
+        "/xyz/openbmc_project/sensors/fan_pwm/" + name,
+        "org.openbmc.Associations");
+    createAssociation(association, sensorConfiguration);
 }
 PwmSensor::~PwmSensor()
 {
diff --git a/src/Utils.cpp b/src/Utils.cpp
index ff06428..86afb09 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -21,6 +21,7 @@
 #include <fstream>
 #include <regex>
 #include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/asio/object_server.hpp>
 #include <sdbusplus/bus/match.hpp>
 
 namespace fs = std::filesystem;
@@ -203,3 +204,17 @@
         limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
     }
 }
+
+void createAssociation(
+    std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
+    const std::string& path)
+{
+    if (association)
+    {
+        using Association = std::tuple<std::string, std::string, std::string>;
+        std::vector<Association> associations;
+        associations.push_back(Association("inventory", "sensors", path));
+        association->register_property("associations", associations);
+        association->initialize();
+    }
+}
\ No newline at end of file