Add input history collection support to main()

Will call enableHistory() on the PowerSupply instance
to start collection, and also start the D-Bus server
to host the objects that hold the history.

Change-Id: If3d95bae1ce4a773bcdba10496d904ecc32c9102
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/configure.ac b/configure.ac
index 2ec466c..144afac 100644
--- a/configure.ac
+++ b/configure.ac
@@ -75,6 +75,20 @@
       AC_DEFINE_UNQUOTED([UCD90160_DEVICE_ACCESS], ["$UCD90160_DEVICE_ACCESS"], [Turn off UCD90160 hardware access])
 )
 
+AC_ARG_VAR(INPUT_HISTORY_BUSNAME_ROOT, [The D-Bus busname root for the PS input history.])
+AS_IF([test "x$INPUT_HISTORY_BUSNAME_ROOT" == "x"],
+      [INPUT_HISTORY_BUSNAME_ROOT="org.open_power.powersupply"])
+AC_DEFINE_UNQUOTED([INPUT_HISTORY_BUSNAME_ROOT],
+                   ["$INPUT_HISTORY_BUSNAME_ROOT"],
+                   [The D-Bus busname root for the PS input history.])
+
+AC_ARG_VAR(INPUT_HISTORY_SENSOR_ROOT, [The D-Bus power sensors namespace root])
+AS_IF([test "x$INPUT_HISTORY_SENSOR_ROOT" == "x"],
+      [INPUT_HISTORY_SENSOR_ROOT="/org/open_power/sensors/aggregation/per_30s"])
+AC_DEFINE_UNQUOTED([INPUT_HISTORY_SENSOR_ROOT],
+                   ["$INPUT_HISTORY_SENSOR_ROOT"],
+                   [The D-Bus power sensors namespace root])
+
 # Create configured output
 AC_CONFIG_FILES([Makefile power-sequencer/Makefile power-supply/Makefile test/Makefile power-supply/test/Makefile])
 AC_OUTPUT
diff --git a/power-supply/main.cpp b/power-supply/main.cpp
index dc13ef9..ee93f74 100644
--- a/power-supply/main.cpp
+++ b/power-supply/main.cpp
@@ -17,6 +17,7 @@
 #include <phosphor-logging/log.hpp>
 #include <systemd/sd-daemon.h>
 #include "argument.hpp"
+#include "config.h"
 #include "event.hpp"
 #include "power_supply.hpp"
 #include "device_monitor.hpp"
@@ -97,6 +98,58 @@
                                                         powerOnDelay,
                                                         presentDelay);
 
+    // Get the number of input power history records to keep in D-Bus.
+    long int numRecords = 0;
+    auto records = (options)["num-history-records"];
+    if (records != ArgumentParser::emptyString)
+    {
+        numRecords = std::stol(records);
+        if (numRecords < 0)
+        {
+            std::cerr << "Invalid number of history records specified.\n";
+            return -6;
+        }
+    }
+
+    if (numRecords != 0)
+    {
+        // Get the GPIO information for controlling the SYNC signal.
+        // If one is there, they both must be.
+        auto syncGPIOPath = (options)["sync-gpio-path"];
+        auto syncGPIONum = (options)["sync-gpio-num"];
+
+        if (((syncGPIOPath == ArgumentParser::emptyString) &&
+            (syncGPIONum != ArgumentParser::emptyString)) ||
+            ((syncGPIOPath != ArgumentParser::emptyString) &&
+            (syncGPIONum == ArgumentParser::emptyString)))
+        {
+            std::cerr << "Invalid sync GPIO number or path\n";
+            return -7;
+        }
+
+        size_t gpioNum = 0;
+        if (syncGPIONum != ArgumentParser::emptyString)
+        {
+            gpioNum = stoul(syncGPIONum);
+        }
+
+        std::string name{"ps" + instnum + "_input_power"};
+        std::string basePath =
+            std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
+
+        psuDevice->enableHistory(basePath,
+                                 numRecords,
+                                 syncGPIOPath,
+                                 gpioNum);
+
+        // Systemd object manager
+        sdbusplus::server::manager::manager objManager{bus, basePath.c_str()};
+
+        std::string busName =
+                std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name;
+        bus.request_name(busName.c_str());
+    }
+
     auto pollInterval = std::chrono::milliseconds(1000);
     DeviceMonitor mainloop(std::move(psuDevice), eventPtr, pollInterval);
     mainloop.run();