Grab DBus connection

Connect to DBus and own a busname.
Create a freedesktop ObjectManager.

Change-Id: I186dadc5a5172f44edf9cfc8d0a338677636de04
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/mainloop.cpp b/mainloop.cpp
index 8d34b18..746e032 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -15,23 +15,33 @@
  */
 #include <iostream>
 #include <memory>
-#include <thread>
+#include <cstring>
+#include <cstdlib>
+#include <chrono>
 #include "sensorset.hpp"
 #include "sensorcache.hpp"
 #include "hwmon.hpp"
 #include "sysfs.hpp"
 #include "mainloop.hpp"
 
+using namespace std::literals::chrono_literals;
+
 MainLoop::MainLoop(
+    sdbusplus::bus::bus&& bus,
     const std::string& path,
     const char* prefix,
     const char* root)
-    : _shutdown(false),
+    : _bus(std::move(bus)),
+      _manager(sdbusplus::server::manager::manager(_bus, root)),
+      _shutdown(false),
       _path(path),
       _prefix(prefix),
       _root(root)
 {
-
+    if (_path.back() == '/')
+    {
+        _path.pop_back();
+    }
 }
 
 void MainLoop::shutdown() noexcept
@@ -45,6 +55,20 @@
     auto sensors = std::make_unique<SensorSet>(_path);
     auto sensor_cache = std::make_unique<SensorCache>();
 
+    {
+        struct Free
+        {
+            void operator()(char* ptr) const
+            {
+                free(ptr);
+            }
+        };
+
+        auto copy = std::unique_ptr<char, Free>(strdup(_path.c_str()));
+        auto busname = std::string(_prefix) + '.' + basename(copy.get());
+        _bus.request_name(busname.c_str());
+    }
+
     // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
     //       ensure the objects all exist?
 
@@ -73,13 +97,13 @@
             }
         }
 
+        // Respond to DBus
+        _bus.process_discard();
+
         // Sleep until next interval.
         // TODO: Issue#5 - Make this configurable.
         // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
-        {
-            using namespace std::literals::chrono_literals;
-            std::this_thread::sleep_for(1s);
-        }
+        _bus.wait((1000000us).count());
 
         // TODO: Issue#7 - Should probably periodically check the SensorSet
         //       for new entries.
diff --git a/mainloop.hpp b/mainloop.hpp
index 2d2cea2..23ee683 100644
--- a/mainloop.hpp
+++ b/mainloop.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <string>
+#include <sdbusplus/server.hpp>
 
 /** @class MainLoop
  *  @brief hwmon-readd main application loop.
@@ -17,6 +18,7 @@
 
         /** @brief Constructor
          *
+         *  @param[in] bus - sdbusplus bus client connection.
          *  @param[in] path - hwmon sysfs instance to manage
          *  @param[in] prefix - DBus busname prefix.
          *  @param[in] root - DBus sensors namespace root.
@@ -28,6 +30,7 @@
          *  the format <prefix>.hwmon<n>.
          */
         MainLoop(
+            sdbusplus::bus::bus&& bus,
             const std::string& path,
             const char* prefix,
             const char* root);
@@ -42,7 +45,10 @@
         void shutdown() noexcept;
 
     private:
-
+        /** @brief sdbusplus bus client connection. */
+        sdbusplus::bus::bus _bus;
+        /** @brief sdbusplus freedesktop.ObjectManager storage. */
+        sdbusplus::server::manager::manager _manager;
         /** @brief Shutdown requested. */
         volatile bool _shutdown;
         /** @brief Path to hwmon sysfs instance. */
diff --git a/readd.cpp b/readd.cpp
index d8ad098..1cc2894 100644
--- a/readd.cpp
+++ b/readd.cpp
@@ -43,6 +43,7 @@
     options.reset();
 
     MainLoop loop(
+        sdbusplus::bus::new_default(),
         path,
         BUSNAME_PREFIX,
         SENSOR_ROOT);
diff --git a/test/test.cpp b/test/test.cpp
index df9ed22..c5c8a1d 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -44,6 +44,7 @@
     f << "1234";
 
     auto loop = MainLoop(
+                    sdbusplus::bus::new_default(),
                     dir,
                     "xyz.openbmc_project.Testing", "/testing");
     auto t = std::thread(server_thread, &loop);