Move the sensor utils into their own module

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I22f6a951921095660fd2be502e59a38161565a95
diff --git a/sensors/build_utils.cpp b/sensors/build_utils.cpp
new file mode 100644
index 0000000..c3c7ce1
--- /dev/null
+++ b/sensors/build_utils.cpp
@@ -0,0 +1,73 @@
+/**
+ * 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 "build_utils.hpp"
+
+namespace pid_control
+{
+
+static constexpr auto external_sensor =
+    "/xyz/openbmc_project/extsensors/";                         // type/
+static constexpr auto openbmc_sensor = "/xyz/openbmc_project/"; // type/
+static constexpr auto sysfs = "/sys/";
+
+IOInterfaceType getWriteInterfaceType(const std::string& path)
+{
+    if (path.empty() || "None" == path)
+    {
+        return IOInterfaceType::NONE;
+    }
+
+    if (path.find(sysfs) != std::string::npos)
+    {
+        // A sysfs read sensor.
+        return IOInterfaceType::SYSFS;
+    }
+
+    if (path.find(openbmc_sensor) != std::string::npos)
+    {
+        return IOInterfaceType::DBUSACTIVE;
+    }
+
+    return IOInterfaceType::UNKNOWN;
+}
+
+IOInterfaceType getReadInterfaceType(const std::string& path)
+{
+    if (path.empty() || "None" == path)
+    {
+        return IOInterfaceType::NONE;
+    }
+
+    if (path.find(external_sensor) != std::string::npos)
+    {
+        return IOInterfaceType::EXTERNAL;
+    }
+
+    if (path.find(openbmc_sensor) != std::string::npos)
+    {
+        return IOInterfaceType::DBUSPASSIVE;
+    }
+
+    if (path.find(sysfs) != std::string::npos)
+    {
+        return IOInterfaceType::SYSFS;
+    }
+
+    return IOInterfaceType::UNKNOWN;
+}
+
+} // namespace pid_control
diff --git a/sensors/build_utils.hpp b/sensors/build_utils.hpp
new file mode 100644
index 0000000..bb2b41c
--- /dev/null
+++ b/sensors/build_utils.hpp
@@ -0,0 +1,31 @@
+#pragma once
+
+#include <string>
+
+namespace pid_control
+{
+
+/* This program assumes sensors use the Sensor.Value interface
+ * and for sensor->write() I only implemented sysfs as a type,
+ * but -- how would it know whether to use Control.FanSpeed or Control.FanPwm?
+ *
+ * One could get the interface list for the object and search for Control.*
+ * but, it needs to know the maximum, minimum.  The only sensors it wants to
+ * write in this code base are Fans...
+ */
+enum class IOInterfaceType
+{
+    NONE, // There is no interface.
+    EXTERNAL,
+    DBUSPASSIVE,
+    DBUSACTIVE, // This means for write that it needs to look up the interface.
+    SYSFS,
+    UNKNOWN
+};
+
+/* WriteInterfaceType is different because Dbusactive/passive. how to know... */
+IOInterfaceType getWriteInterfaceType(const std::string& path);
+
+IOInterfaceType getReadInterfaceType(const std::string& path);
+
+} // namespace pid_control
diff --git a/sensors/builder.cpp b/sensors/builder.cpp
index 4da1cf2..1cea61a 100644
--- a/sensors/builder.cpp
+++ b/sensors/builder.cpp
@@ -26,6 +26,7 @@
 #include "interfaces.hpp"
 #include "notimpl/readonly.hpp"
 #include "notimpl/writeonly.hpp"
+#include "sensors/build_utils.hpp"
 #include "sensors/builder.hpp"
 #include "sensors/host.hpp"
 #include "sensors/manager.hpp"
@@ -37,6 +38,10 @@
 static constexpr bool deferSignals = true;
 static DbusHelper helper;
 
+using ::pid_control::getReadInterfaceType;
+using ::pid_control::getWriteInterfaceType;
+using ::pid_control::IOInterfaceType;
+
 SensorManager
     buildSensors(const std::map<std::string, struct conf::SensorConfig>& config,
                  sdbusplus::bus::bus& passive, sdbusplus::bus::bus& host)