Determine pcap value to send to occ

Change-Id: Ie60aac151f5fd8ce091020ce756834e4877cbc93
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
diff --git a/.gitignore b/.gitignore
index ea5e824..6c72e31 100644
--- a/.gitignore
+++ b/.gitignore
@@ -46,3 +46,7 @@
 *-libtool
 /org/open_power/OCC/PassThrough/
 /openpower-occ-control
+/test/utest
+/test/utest-utest.o
+/test/*.log
+/test/*.trs
diff --git a/Makefile.am b/Makefile.am
index d26ed99..25e1358 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -36,3 +36,5 @@
 org/open_power/OCC/PassThrough/error.cpp: ${top_srcdir}/org/open_power/OCC/PassThrough.errors.yaml
 	@mkdir -p `dirname $@`
 	$(SDBUSPLUSPLUS) -r $(top_srcdir) error exception-cpp org.open_power.OCC.PassThrough > $@
+
+SUBDIRS = . test
\ No newline at end of file
diff --git a/configure.ac b/configure.ac
index 693c864..efc29d8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -13,6 +13,30 @@
 # Surpress the --with-libtool-sysroot error
 LT_INIT
 
+# gtest
+# Check/set gtest specific functions.
+AX_PTHREAD([GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=1"],[GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=0"])
+AC_SUBST(GTEST_CPPFLAGS)
+
+# Test cases require SDK so only build if we're told to (and SDK is available)
+AC_ARG_ENABLE([oe-sdk],
+    AS_HELP_STRING([--enable-oe-sdk], [Link testcases absolutely against OE SDK so they can be ran within it.])
+)
+AC_ARG_VAR(OECORE_TARGET_SYSROOT,
+    [Path to the OE SDK SYSROOT])
+AS_IF([test "x$enable_oe_sdk" == "xyes"],
+    AS_IF([test "x$OECORE_TARGET_SYSROOT" == "x"],
+          AC_MSG_ERROR([OECORE_TARGET_SYSROOT must be set with --enable-oe-sdk])
+    )
+    AC_MSG_NOTICE([Enabling OE-SDK at $OECORE_TARGET_SYSROOT])
+    [
+        testcase_flags="-Wl,-rpath,\${OECORE_TARGET_SYSROOT}/lib"
+        testcase_flags="${testcase_flags} -Wl,-rpath,\${OECORE_TARGET_SYSROOT}/usr/lib"
+        testcase_flags="${testcase_flags} -Wl,-dynamic-linker,`find \${OECORE_TARGET_SYSROOT}/lib/ld-*.so | sort -r -n | head -n1`"
+    ]
+    AC_SUBST([OESDK_TESTCASE_FLAGS], [$testcase_flags])
+)
+
 PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus],,\
     AC_MSG_ERROR(["Requires sdbusplus package."]))
 PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging],,\
@@ -63,6 +87,10 @@
 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])
 
+AC_ARG_VAR(PS_DERATING_FACTOR, [The power supply derating factor])
+AS_IF([test "x$PS_DERATING_FACTOR" == "x"], [PS_DERATING_FACTOR=90])
+AC_DEFINE_UNQUOTED([PS_DERATING_FACTOR], [$PS_DERATING_FACTOR], [The power supply derating factor])
+
 AC_CONFIG_HEADERS([config.h])
-AC_CONFIG_FILES([Makefile])
+AC_CONFIG_FILES([Makefile test/Makefile])
 AC_OUTPUT
diff --git a/powercap.cpp b/powercap.cpp
index 2ecad0c..7e8c0db 100644
--- a/powercap.cpp
+++ b/powercap.cpp
@@ -54,6 +54,19 @@
     return mapperResponse.begin()->first;
 }
 
+uint32_t PowerCap::getOccInput(uint32_t pcap, bool pcapEnabled)
+{
+    if (!pcapEnabled)
+    {
+        // Pcap disabled, return 0 to indicate disabled
+        return 0;
+    }
+
+    // If pcap is not disabled then just return the pcap with the derating
+    // factor applied.
+    return( (static_cast<uint64_t>(pcap) * PS_DERATING_FACTOR) /100);
+}
+
 uint32_t PowerCap::getPcap()
 {
     auto settingService = getService(PCAP_PATH,PCAP_INTERFACE);
@@ -144,7 +157,9 @@
                      entry("PCAP_ENABLED=%u",pcapEnabled));
 
     // Determine desired action to write to occ
-    // TODO
+    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
diff --git a/powercap.hpp b/powercap.hpp
index ff457f8..efd0729 100644
--- a/powercap.hpp
+++ b/powercap.hpp
@@ -49,6 +49,15 @@
                           this, std::placeholders::_1))
     {};
 
+    /** @brief Return the appropriate value to write to the OCC
+     *
+     * @param[in]  pcap        - Current user power cap setting
+     * @param[in]  pcapEnabled - Current power cap enable setting
+     *
+     * @return The value to write to the occ user pcap
+     */
+    uint32_t getOccInput(uint32_t pcap, bool pcapEnabled);
+
 private:
 
     /** @brief Callback for pcap setting changes
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..6022c65
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,20 @@
+AM_CPPFLAGS = -I$(top_srcdir)
+check_PROGRAMS = utest
+
+# Run all 'check' test programs
+TESTS = $(check_PROGRAMS)
+
+# Build/add utest to test suite
+utest_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS)
+utest_CXXFLAGS = $(PTHREAD_CFLAGS)
+utest_LDFLAGS = -lgtest_main -lgtest \
+                 $(PTHREAD_LIBS) \
+                 $(OESDK_TESTCASE_FLAGS) \
+                 $(SYSTEMD_LIBS) \
+                 ${SDBUSPLUS_LIBS} \
+                 $(OPENPOWER_DBUS_INTERFACES_LIBS) \
+                 -lstdc++fs
+utest_SOURCES = utest.cpp
+utest_LDADD = $(top_builddir)/powercap.o \
+              $(top_builddir)/occ_status.o \
+              $(top_builddir)/occ_device.o
diff --git a/test/utest.cpp b/test/utest.cpp
new file mode 100644
index 0000000..e99d996
--- /dev/null
+++ b/test/utest.cpp
@@ -0,0 +1,30 @@
+#include <gtest/gtest.h>
+#include "powercap.hpp"
+
+using namespace open_power::occ;
+
+class VerifyOccInput : public ::testing::Test
+{
+    public:
+        VerifyOccInput() :
+            bus(sdbusplus::bus::new_default()),
+            occStatus(bus,"/test/path"),
+            pcap(bus,occStatus)
+        {}
+        ~VerifyOccInput()
+        {}
+
+        sdbusplus::bus::bus bus;
+        Status occStatus;
+        powercap::PowerCap pcap;
+};
+
+TEST_F(VerifyOccInput, PcapDisabled) {
+    uint32_t occInput = pcap.getOccInput(100,false);
+    EXPECT_EQ(occInput, 0);
+}
+
+TEST_F(VerifyOccInput, PcapEnabled) {
+    uint32_t occInput = pcap.getOccInput(100,true);
+    EXPECT_EQ(occInput, 90);
+}