Notify OCC of pcap setting changes

Resolves openbmc/openbmc#948

Change-Id: I03193b07ddaf380468bd0c0e62a41220bdeaecce
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
diff --git a/configure.ac b/configure.ac
index efc29d8..daff49a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -83,6 +83,10 @@
 AS_IF([test "x$OCC_NAME" == "x"], [OCC_NAME="occ"])
 AC_DEFINE_UNQUOTED([OCC_NAME], ["$OCC_NAME"], [The OCC object name])
 
+AC_ARG_VAR(OCC_MASTER_NAME, [The OCC master object name])
+AS_IF([test "x$OCC_MASTER_NAME" == "x"], [OCC_MASTER_NAME="occ-dev0"])
+AC_DEFINE_UNQUOTED([OCC_MASTER_NAME], ["$OCC_MASTER_NAME"], [The OCC master object name])
+
 AC_ARG_VAR(OCC_HWMON_PATH, [The OCC hwmon path])
 AS_IF([test "x$OCC_HWMON_PATH" == "x"], [OCC_HWMON_PATH="/sys/bus/platform/drivers/occ-hwmon/"])
 AC_DEFINE_UNQUOTED([OCC_HWMON_PATH], ["$OCC_HWMON_PATH"], [The OCC hwmon path])
diff --git a/powercap.cpp b/powercap.cpp
index 7e8c0db..0aca3e4 100644
--- a/powercap.cpp
+++ b/powercap.cpp
@@ -113,6 +113,34 @@
     return pcapEnabled.get<bool>();
 }
 
+void PowerCap::writeOcc(uint32_t pcapValue)
+{
+    // Create path out to master occ hwmon entry
+    std::unique_ptr<fs::path> fileName =
+            std::make_unique<fs::path>(OCC_HWMON_PATH);
+    *fileName /= OCC_MASTER_NAME;
+    *fileName /= "/hwmon/";
+
+    // Need to get the hwmonXX directory name, there better only be 1 dir
+    assert(std::distance(fs::directory_iterator(*fileName),
+                         fs::directory_iterator{}) == 1);
+    // Now set our path to this full path, including this hwmonXX directory
+    fileName = std::make_unique<fs::path>(*fs::directory_iterator(*fileName));
+    // Append on the hwmon string where we write the user power cap
+    *fileName /= "/caps1_user";
+
+    auto pcapString {std::to_string(pcapValue)};
+
+    log<level::INFO>("Writing pcap value to hwmon",
+                     entry("PCAP_PATH=%s",*fileName),
+                     entry("PCAP_VALUE=%s",pcapString.c_str()));
+    // Open the hwmon file and write the power cap
+    std::ofstream file(*fileName, std::ios::out);
+    file << pcapString;
+    file.close();
+    return;
+}
+
 void PowerCap::pcapChanged(sdbusplus::message::message& msg)
 {
     if (!occStatus.occActive())
@@ -157,12 +185,11 @@
                      entry("PCAP_ENABLED=%u",pcapEnabled));
 
     // Determine desired action to write to occ
+
     auto occInput = getOccInput(pcap, pcapEnabled);
-    log<level::DEBUG>("Writing new power cap setting to OCC",
-                     entry("OCC_PCAP_VAL=%u",occInput));
 
     // Write action to occ
-    // TODO
+    writeOcc(occInput);
 
     return;
 }
diff --git a/powercap.hpp b/powercap.hpp
index efd0729..ddf2a74 100644
--- a/powercap.hpp
+++ b/powercap.hpp
@@ -91,6 +91,12 @@
      */
     bool getPcapEnabled();
 
+    /** @brief Write the input power cap to the occ hwmon entry
+     *
+     * @param[in]  pcapValue - Power cap value to write to OCC
+     */
+    void writeOcc(uint32_t pcapValue);
+
     /** @brief Reference to sdbus **/
     sdbusplus::bus::bus& bus;