dbus: transition to interface for testing

To make testing easier, the dbus objects now receive a
helper interface to use instead of calling into a helper module.

Tested: Ran on quanta-q71l board and it behaved as expected.

Change-Id: I2521d9c75aec76e92d2e26dc044d01011e44d552
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/dbus/dbusactiveread.cpp b/dbus/dbusactiveread.cpp
index 5e046a4..e41ba7a 100644
--- a/dbus/dbusactiveread.cpp
+++ b/dbus/dbusactiveread.cpp
@@ -27,7 +27,7 @@
     struct SensorProperties settings;
     double value;
 
-    GetProperties(_bus, _service, _path, &settings);
+    _helper->GetProperties(_bus, _service, _path, &settings);
 
     value = settings.value * pow(10, settings.scale);
 
diff --git a/dbus/dbusactiveread.hpp b/dbus/dbusactiveread.hpp
index a9146b9..85380fb 100644
--- a/dbus/dbusactiveread.hpp
+++ b/dbus/dbusactiveread.hpp
@@ -1,13 +1,12 @@
 #pragma once
 
 #include <memory>
+#include <sdbusplus/bus.hpp>
 #include <string>
 
-#include <sdbusplus/bus.hpp>
-
+#include "dbus/util.hpp"
 #include "interfaces.hpp"
 
-
 /*
  * This ReadInterface will actively reach out over dbus upon calling read to
  * get the value from whomever owns the associated dbus path.
@@ -17,11 +16,13 @@
     public:
         DbusActiveRead(sdbusplus::bus::bus& bus,
                        const std::string& path,
-                       const std::string& service)
+                       const std::string& service,
+                       DbusHelperInterface *helper)
             : ReadInterface(),
               _bus(bus),
               _path(path),
-              _service(service)
+              _service(service),
+              _helper(helper)
         { }
 
         ReadReturn read(void) override;
@@ -30,4 +31,5 @@
         sdbusplus::bus::bus& _bus;
         const std::string _path;
         const std::string _service; // the sensor service.
+        DbusHelperInterface *_helper;
 };
diff --git a/dbus/dbuspassive.cpp b/dbus/dbuspassive.cpp
index 038fc5c..6b4ea5f 100644
--- a/dbus/dbuspassive.cpp
+++ b/dbus/dbuspassive.cpp
@@ -23,20 +23,22 @@
 DbusPassive::DbusPassive(
     sdbusplus::bus::bus& bus,
     const std::string& type,
-    const std::string& id)
+    const std::string& id,
+    DbusHelperInterface *helper)
     : ReadInterface(),
       _bus(bus),
       _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this),
-      _id(id)
+      _id(id),
+      _helper(helper)
 {
     /* Need to get the scale and initial value */
     auto tempBus = sdbusplus::bus::new_default();
     /* service == busname */
     std::string path = GetSensorPath(type, id);
-    std::string service = GetService(tempBus, sensorintf, path);
+    std::string service = _helper->GetService(tempBus, sensorintf, path);
 
     struct SensorProperties settings;
-    GetProperties(tempBus, service, path, &settings);
+    _helper->GetProperties(tempBus, service, path, &settings);
 
     _scale = settings.scale;
     _value = settings.value * pow(10, _scale);
diff --git a/dbus/dbuspassive.hpp b/dbus/dbuspassive.hpp
index 36e0ac1..8ecfe71 100644
--- a/dbus/dbuspassive.hpp
+++ b/dbus/dbuspassive.hpp
@@ -10,7 +10,6 @@
 #include <tuple>
 #include <vector>
 
-
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/message.hpp>
 #include <sdbusplus/server.hpp>
@@ -36,7 +35,8 @@
     public:
         DbusPassive(sdbusplus::bus::bus& bus,
                     const std::string& type,
-                    const std::string& id);
+                    const std::string& id,
+                    DbusHelperInterface *helper);
 
         ReadReturn read(void) override;
 
@@ -49,6 +49,7 @@
         sdbusplus::server::match::match _signal;
         int64_t _scale;
         std::string _id; // for debug identification
+        DbusHelperInterface *_helper;
 
         std::mutex _lock;
         double _value = 0;
@@ -56,3 +57,4 @@
         std::chrono::high_resolution_clock::time_point _updated;
 };
 
+int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner);
diff --git a/dbus/util.cpp b/dbus/util.cpp
index 7e1f9a5..f0ffffb 100644
--- a/dbus/util.cpp
+++ b/dbus/util.cpp
@@ -9,9 +9,9 @@
 /* TODO(venture): Basically all phosphor apps need this, maybe it should be a
  * part of sdbusplus.  There is an old version in libmapper.
  */
-std::string GetService(sdbusplus::bus::bus& bus,
-                       const std::string& intf,
-                       const std::string& path)
+std::string DbusHelper::GetService(sdbusplus::bus::bus& bus,
+                                   const std::string& intf,
+                                   const std::string& path)
 {
     auto mapper = bus.new_method_call(
                       "xyz.openbmc_project.ObjectMapper",
@@ -39,12 +39,11 @@
     return response.begin()->first;
 }
 
-void GetProperties(sdbusplus::bus::bus& bus,
-                   const std::string& service,
-                   const std::string& path,
-                   struct SensorProperties* prop)
+void DbusHelper::GetProperties(sdbusplus::bus::bus& bus,
+                               const std::string& service,
+                               const std::string& path,
+                               struct SensorProperties* prop)
 {
-
     auto pimMsg = bus.new_method_call(service.c_str(),
                                       path.c_str(),
                                       propertiesintf.c_str(),
@@ -69,9 +68,12 @@
     valueResponseMsg.read(propMap);
 
     // If no error was set, the values should all be there.
-    prop->unit = sdbusplus::message::variant_ns::get<std::string>(propMap["Unit"]);
-    prop->scale = sdbusplus::message::variant_ns::get<int64_t>(propMap["Scale"]);
-    prop->value = sdbusplus::message::variant_ns::get<int64_t>(propMap["Value"]);
+    prop->unit = sdbusplus::message::variant_ns::get<std::string>(
+        propMap["Unit"]);
+    prop->scale = sdbusplus::message::variant_ns::get<int64_t>(
+        propMap["Scale"]);
+    prop->value = sdbusplus::message::variant_ns::get<int64_t>(
+        propMap["Value"]);
 
     return;
 }
diff --git a/dbus/util.hpp b/dbus/util.hpp
index 3ff2424..e858c0c 100644
--- a/dbus/util.hpp
+++ b/dbus/util.hpp
@@ -9,21 +9,52 @@
     std::string unit;
 };
 
-/*
- * Retrieve the dbus bus (or service) for the given object and interface.
- */
-std::string GetService(sdbusplus::bus::bus& bus,
-                       const std::string& intf,
-                       const std::string& path);
-
-void GetProperties(sdbusplus::bus::bus& bus,
-                   const std::string& service,
-                   const std::string& path,
-                   struct SensorProperties* prop);
-
-std::string GetSensorPath(const std::string& type, const std::string& id);
-std::string GetMatch(const std::string& type, const std::string& id);
-
 const std::string sensorintf = "xyz.openbmc_project.Sensor.Value";
 const std::string propertiesintf = "org.freedesktop.DBus.Properties";
 
+class DbusHelperInterface
+{
+    public:
+        virtual ~DbusHelperInterface() = default;
+
+        /** @brief Get the service providing the interface for the path.
+         */
+        virtual std::string GetService(sdbusplus::bus::bus& bus,
+                                       const std::string& intf,
+                                       const std::string& path) = 0;
+
+        /** @brief Get all Sensor.Value properties for a service and path.
+         *
+         * @param[in] bus - A bus to use for the call.
+         * @param[in] service - The service providing the interface.
+         * @param[in] path - The dbus path.
+         * @param[out] prop - A pointer to a properties struct to fill out.
+         */
+        virtual void GetProperties(sdbusplus::bus::bus& bus,
+                                   const std::string& service,
+                                   const std::string& path,
+                                   struct SensorProperties* prop) = 0;
+};
+
+class DbusHelper : public DbusHelperInterface
+{
+    public:
+        DbusHelper() = default;
+        ~DbusHelper() = default;
+        DbusHelper(const DbusHelper&) = default;
+        DbusHelper& operator=(const DbusHelper&) = default;
+        DbusHelper(DbusHelper&&) = default;
+        DbusHelper& operator=(DbusHelper&&) = default;
+
+        std::string GetService(sdbusplus::bus::bus& bus,
+                               const std::string& intf,
+                               const std::string& path) override;
+
+        void GetProperties(sdbusplus::bus::bus& bus,
+                           const std::string& service,
+                           const std::string& path,
+                           struct SensorProperties* prop) override;
+};
+
+std::string GetSensorPath(const std::string& type, const std::string& id);
+std::string GetMatch(const std::string& type, const std::string& id);
diff --git a/sensors/builder.cpp b/sensors/builder.cpp
index 4654e51..9ab6a8c 100644
--- a/sensors/builder.cpp
+++ b/sensors/builder.cpp
@@ -34,6 +34,7 @@
 #include "util.hpp"
 
 static constexpr bool deferSignals = true;
+static DbusHelper helper;
 
 SensorManager BuildSensors(
     const std::map<std::string, struct sensor>& config)
@@ -68,7 +69,8 @@
                 ri = std::make_unique<DbusPassive>(
                          PassiveListeningBus,
                          info->type,
-                         name);
+                         name,
+                         &helper);
                 break;
             case IOInterfaceType::EXTERNAL:
                 // These are a special case for read-only.
diff --git a/test/dbushelper_mock.hpp b/test/dbushelper_mock.hpp
new file mode 100644
index 0000000..23dd81c
--- /dev/null
+++ b/test/dbushelper_mock.hpp
@@ -0,0 +1,21 @@
+#pragma once
+
+#include <gmock/gmock.h>
+#include <sdbusplus/bus.hpp>
+#include <string>
+
+#include "dbus/util.hpp"
+
+class DbusHelperMock : public DbusHelperInterface
+{
+    public:
+        virtual ~DbusHelperMock() = default;
+
+        MOCK_METHOD3(GetService, std::string(sdbusplus::bus::bus&,
+                                             const std::string&,
+                                             const std::string&));
+        MOCK_METHOD4(GetProperties, void(sdbusplus::bus::bus&,
+                                         const std::string&,
+                                         const std::string&,
+                                         struct SensorProperties*));
+};