ipmi: split dbus get property into separate file

This is a step towards handling getting/setting dbus properties as an
implementation of an interface, de-coupling from dbus.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I81a2c5100833235811d4b7e59152d70585c7abaf
diff --git a/Makefile.am b/Makefile.am
index b2116c9..f62a5f3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -119,7 +119,8 @@
 libmanualcmds_LTLIBRARIES = libmanualcmds.la
 libmanualcmds_la_SOURCES = \
 	ipmi/main_ipmi.cpp \
-	ipmi/manualcmds.cpp
+	ipmi/manualcmds.cpp \
+	ipmi/dbus_mode.cpp
 libmanualcmds_la_LDFLAGS = \
 	$(SYSTEMD_LIBS) \
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS) \
diff --git a/ipmi/dbus_mode.cpp b/ipmi/dbus_mode.cpp
new file mode 100644
index 0000000..76b4eba
--- /dev/null
+++ b/ipmi/dbus_mode.cpp
@@ -0,0 +1,77 @@
+/**
+ * Copyright 2017 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <ipmid/api.h>
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/message.hpp>
+
+#include <cstdint>
+#include <string>
+
+namespace pid_control
+{
+namespace ipmi
+{
+
+static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
+static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
+static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
+static constexpr auto manualProperty = "Manual";
+static constexpr auto failsafeProperty = "FailSafe";
+static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
+
+using Property = std::string;
+using Value = std::variant<bool>;
+using PropertyMap = std::map<Property, Value>;
+
+/* The following was copied directly from my manual thread handler. */
+static std::string getControlPath(int8_t zone)
+{
+    return std::string(objectPath) + std::to_string(zone);
+}
+
+uint8_t getFanCtrlProperty(uint8_t zoneId, bool* value,
+                           const std::string& property)
+{
+    std::string path = getControlPath(zoneId);
+
+    auto propertyReadBus = sdbusplus::bus::new_system();
+    auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
+                                                  propertiesintf, "GetAll");
+    pimMsg.append(intf);
+
+    try
+    {
+        PropertyMap propMap;
+
+        /* a method could error but the call not error. */
+        auto valueResponseMsg = propertyReadBus.call(pimMsg);
+
+        valueResponseMsg.read(propMap);
+
+        *value = std::get<bool>(propMap[property]);
+    }
+    catch (const sdbusplus::exception::SdBusError& ex)
+    {
+        return IPMI_CC_INVALID;
+    }
+
+    return IPMI_CC_OK;
+}
+
+} // namespace ipmi
+} // namespace pid_control
diff --git a/ipmi/dbus_mode.hpp b/ipmi/dbus_mode.hpp
new file mode 100644
index 0000000..05e9734
--- /dev/null
+++ b/ipmi/dbus_mode.hpp
@@ -0,0 +1,28 @@
+#pragma once
+
+#include <cstdint>
+#include <string>
+
+namespace pid_control
+{
+namespace ipmi
+{
+
+/*
+ * busctl call xyz.openbmc_project.State.FanCtrl \
+ *     /xyz/openbmc_project/settings/fanctrl/zone1 \
+ *     org.freedesktop.DBus.Properties \
+ *     GetAll \
+ *     s \
+ *     xyz.openbmc_project.Control.Mode
+ * a{sv} 2 "Manual" b false "FailSafe" b false
+ *
+ * This returns an IPMI code as a uint8_t (which will always be sufficient to
+ * hold the result). NOTE: This does not return the typedef value to avoid
+ * including a header with conflicting types.
+ */
+uint8_t getFanCtrlProperty(uint8_t zoneId, bool* value,
+                           const std::string& property);
+
+} // namespace ipmi
+} // namespace pid_control
diff --git a/ipmi/manualcmds.cpp b/ipmi/manualcmds.cpp
index e11d49a..5dcac09 100644
--- a/ipmi/manualcmds.cpp
+++ b/ipmi/manualcmds.cpp
@@ -16,6 +16,7 @@
 
 #include "manualcmds.hpp"
 
+#include "dbus_mode.hpp"
 #include "manual_messages.hpp"
 
 #include <ipmid/api.h>
@@ -50,45 +51,6 @@
     return std::string(objectPath) + std::to_string(zone);
 }
 
-/*
- * busctl call xyz.openbmc_project.State.FanCtrl \
- *     /xyz/openbmc_project/settings/fanctrl/zone1 \
- *     org.freedesktop.DBus.Properties \
- *     GetAll \
- *     s \
- *     xyz.openbmc_project.Control.Mode
- * a{sv} 2 "Manual" b false "FailSafe" b false
- */
-
-static ipmi_ret_t getFanCtrlProperty(uint8_t zoneId, bool* value,
-                                     const std::string& property)
-{
-    std::string path = getControlPath(zoneId);
-
-    auto propertyReadBus = sdbusplus::bus::new_system();
-    auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
-                                                  propertiesintf, "GetAll");
-    pimMsg.append(intf);
-
-    try
-    {
-        PropertyMap propMap;
-
-        /* a method could error but the call not error. */
-        auto valueResponseMsg = propertyReadBus.call(pimMsg);
-
-        valueResponseMsg.read(propMap);
-
-        *value = std::get<bool>(propMap[property]);
-    }
-    catch (const sdbusplus::exception::SdBusError& ex)
-    {
-        return IPMI_CC_INVALID;
-    }
-
-    return IPMI_CC_OK;
-}
-
 static ipmi_ret_t getFailsafeModeState(const uint8_t* reqBuf, uint8_t* replyBuf,
                                        size_t* dataLen)
 {