transition dbus-configuration to run-time check

This now checks dbus if there is no json file configuration found.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: Ic8f5b0531b7131a3d333c2127043b7054924c156
diff --git a/Makefile.am b/Makefile.am
index f62a5f3..b859f32 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -81,6 +81,7 @@
 libswampd_la_SOURCES = \
 	notimpl/readonly.cpp \
 	notimpl/writeonly.cpp \
+	dbus/dbusconfiguration.cpp \
 	dbus/dbusutil.cpp \
 	dbus/dbushelper.cpp \
 	dbus/dbuspassiveredundancy.cpp \
@@ -111,10 +112,6 @@
 	build/buildjson.cpp \
 	experiments/drive.cpp
 
-if CONFIGURE_DBUS
-libswampd_la_SOURCES += dbus/dbusconfiguration.cpp
-endif
-
 libmanualcmdsdir = ${libdir}/ipmid-providers
 libmanualcmds_LTLIBRARIES = libmanualcmds.la
 libmanualcmds_la_SOURCES = \
diff --git a/configure.ac b/configure.ac
index 5075c67..82d7d8a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -203,11 +203,6 @@
         [--enable-configure-dbus], [Enable configuring pid from D-Bus.]
     )
 )
-AM_CONDITIONAL(CONFIGURE_DBUS, [test "x$enable_configure_dbus" = "xyes"])
-AS_IF([test "x$enable_configure_dbus" = "xyes"],
-    [AC_DEFINE(CONFIGURE_DBUS, [1], [Read configuration from D-Bus.])],
-    [AC_DEFINE(CONFIGURE_DBUS, [0], [Do not read configuration from D-Bus.])]
-)
 
 AC_ARG_VAR(SYSTEMD_TARGET, "Target for starting this service")
 AS_IF([test "x$SYSTEMD_TARGET" == "x"], [SYSTEMD_TARGET="multi-user.target"])
diff --git a/main.cpp b/main.cpp
index 73e43c8..712d2cf 100644
--- a/main.cpp
+++ b/main.cpp
@@ -18,6 +18,7 @@
 
 #include "build/buildjson.hpp"
 #include "conf.hpp"
+#include "dbus/dbusconfiguration.hpp"
 #include "interfaces.hpp"
 #include "pid/builder.hpp"
 #include "pid/buildjson.hpp"
@@ -36,6 +37,7 @@
 #include <sdbusplus/bus.hpp>
 
 #include <chrono>
+#include <filesystem>
 #include <iostream>
 #include <list>
 #include <map>
@@ -45,10 +47,6 @@
 #include <utility>
 #include <vector>
 
-#if CONFIGURE_DBUS
-#include "dbus/dbusconfiguration.hpp"
-#endif
-
 namespace pid_control
 {
 
@@ -86,34 +84,36 @@
 
     timers.clear();
 
-#if CONFIGURE_DBUS
-
-    static boost::asio::steady_timer reloadTimer(io);
-    if (!dbus_configuration::init(modeControlBus, reloadTimer))
-    {
-        return; // configuration not ready
-    }
-
-#else
     const std::string& path =
         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
 
-    /*
-     * When building the sensors, if any of the dbus passive ones aren't on the
-     * bus, it'll fail immediately.
-     */
-    try
+    if (std::filesystem::exists(path))
     {
-        auto jsonData = parseValidateJson(path);
-        sensorConfig = buildSensorsFromJson(jsonData);
-        std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
+        /*
+         * When building the sensors, if any of the dbus passive ones aren't on
+         * the bus, it'll fail immediately.
+         */
+        try
+        {
+            auto jsonData = parseValidateJson(path);
+            sensorConfig = buildSensorsFromJson(jsonData);
+            std::tie(zoneConfig, zoneDetailsConfig) =
+                buildPIDsFromJson(jsonData);
+        }
+        catch (const std::exception& e)
+        {
+            std::cerr << "Failed during building: " << e.what() << "\n";
+            exit(EXIT_FAILURE); /* fatal error. */
+        }
     }
-    catch (const std::exception& e)
+    else
     {
-        std::cerr << "Failed during building: " << e.what() << "\n";
-        exit(EXIT_FAILURE); /* fatal error. */
+        static boost::asio::steady_timer reloadTimer(io);
+        if (!dbus_configuration::init(modeControlBus, reloadTimer))
+        {
+            return; // configuration not ready
+        }
     }
-#endif
 
     mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
     zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);