Fill in shell functions/files for cooling type.

Change-Id: I8d18d3141e955adeffdd8122ebb17549c88edd77
Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
diff --git a/Makefile.am b/Makefile.am
index 443dab7..73d8e41 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,7 +19,7 @@
 SUBDIRS += control
 endif
 if WANT_COOLING_TYPE
-SUBDIRS += chassis-cooling-type
+SUBDIRS += cooling-type
 endif
 if WANT_MONITOR
 SUBDIRS += monitor
diff --git a/chassis-cooling-type/Makefile.am b/chassis-cooling-type/Makefile.am
deleted file mode 100644
index 7a31d77..0000000
--- a/chassis-cooling-type/Makefile.am
+++ /dev/null
@@ -1,8 +0,0 @@
-AM_DEFAULT_SOURCE_EXT = .cpp
-
-sbin_PROGRAMS = \
-	phosphor-cooling-type
-
-phosphor_cooling_type_SOURCES = \
-	cooling_type.cpp
-
diff --git a/chassis-cooling-type/cooling_type.cpp b/chassis-cooling-type/cooling_type.cpp
deleted file mode 100644
index d382a0b..0000000
--- a/chassis-cooling-type/cooling_type.cpp
+++ /dev/null
@@ -1,10 +0,0 @@
-#include "cooling_type.hpp"
-
-int main(int argc, char* argv[])
-{
-    int rc = -1;
-
-    rc = 0;
-
-    return rc;
-}
diff --git a/chassis-cooling-type/cooling_type.hpp b/chassis-cooling-type/cooling_type.hpp
deleted file mode 100644
index ff9e34d..0000000
--- a/chassis-cooling-type/cooling_type.hpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#pragma once
-
-namespace phosphor
-{
-namespace chassis
-{
-namespace cooling
-{
-
-}
-}
-}
diff --git a/configure.ac b/configure.ac
index 2152b03..c3e18bb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -114,7 +114,7 @@
 AS_IF([test "x$enable_cooling-type" != "xno"], [
        PKG_CHECK_MODULES([LIBEVDEV], [libevdev], ,
                          [AC_MSG_ERROR([The libevdev package is required])])
-       AC_CONFIG_FILES([chassis-cooling-type/Makefile])
+       AC_CONFIG_FILES([cooling-type/Makefile])
 ])
 AS_IF([test "x$enable_monitor" != "xno"], [
 
diff --git a/cooling-type/Makefile.am b/cooling-type/Makefile.am
new file mode 100644
index 0000000..8be1c3e
--- /dev/null
+++ b/cooling-type/Makefile.am
@@ -0,0 +1,13 @@
+AM_DEFAULT_SOURCE_EXT = .cpp
+
+sbin_PROGRAMS = \
+	phosphor-cooling-type
+
+phosphor_cooling_type_SOURCES = \
+	main.cpp \
+	argument.cpp \
+	cooling_type.cpp
+
+phosphor_cooling_type_LDADD = $(SDBUSPLUS_LIBS)
+
+# vim: tabstop=8 noexpandtab
diff --git a/cooling-type/argument.cpp b/cooling-type/argument.cpp
new file mode 100644
index 0000000..da1e5a3
--- /dev/null
+++ b/cooling-type/argument.cpp
@@ -0,0 +1,99 @@
+/**
+ * Copyright © 2017 IBM Corporation
+ *
+ * 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 <iostream>
+#include <iterator>
+#include <algorithm>
+#include <cassert>
+#include "argument.hpp"
+
+namespace phosphor
+{
+namespace cooling
+{
+namespace type
+{
+
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+    auto option = 0;
+    while (-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
+    {
+        if ((option == '?') || (option == 'h'))
+        {
+            usage(argv);
+            exit(-1);
+        }
+
+        auto i = &options[0];
+        while ((i->val != option) && (i->val != 0))
+        {
+            ++i;
+        }
+
+        if (i->val)
+        {
+            arguments[i->name] = (i->has_arg ? optarg : true_string);
+        }
+    }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+    auto i = arguments.find(opt);
+    if (i == arguments.end())
+    {
+        return empty_string;
+    }
+    else
+    {
+        return i->second;
+    }
+}
+
+// TODO openbmc/phosphor-fan-presence#6
+//      gpio parameter need something to indicate 0=water & air, 1=air?
+void ArgumentParser::usage(char** argv)
+{
+    std::cerr << "Usage: " << argv[0] << " [options]\n";
+    std::cerr << "Options:\n";
+    std::cerr << "    --help               print this menu\n";
+    std::cerr << "    --air                Indicate air cooled is set\n";
+    std::cerr << "    --water              Indicate water cooled is set\n";
+    std::cerr << "    --gpio=<pin>         GPIO pin to read\n";
+    std::cerr <<
+              "    --path=<objpath>     Object path under inventory to have "
+              "CoolingType updated\n";
+    std::cerr << std::flush;
+}
+
+const option ArgumentParser::options[] =
+{
+    { "path",   required_argument,  NULL,   'p' },
+    { "gpio",   required_argument,  NULL,   'g' },
+    { "air",    no_argument,        NULL,   'a' },
+    { "water",  no_argument,        NULL,   'w' },
+    { "help",   no_argument,        NULL,   'h' },
+    { 0, 0, 0, 0},
+};
+
+const char* ArgumentParser::optionstr = "p:g:aw?h";
+
+const std::string ArgumentParser::empty_string = "";
+
+}
+}
+}
+// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/cooling-type/argument.hpp b/cooling-type/argument.hpp
new file mode 100644
index 0000000..cc64891
--- /dev/null
+++ b/cooling-type/argument.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <getopt.h>
+#include <map>
+#include <string>
+
+namespace phosphor
+{
+namespace cooling
+{
+namespace type
+{
+
+class ArgumentParser
+{
+    public:
+        ArgumentParser(int argc, char** argv);
+        ArgumentParser() = delete;
+        ArgumentParser(const ArgumentParser&) = delete;
+        ArgumentParser(ArgumentParser&&) = default;
+        ArgumentParser& operator=(const ArgumentParser&) = delete;
+        ArgumentParser& operator=(ArgumentParser&&) = default;
+        ~ArgumentParser() = default;
+        const std::string& operator[](const std::string& opt);
+
+        static void usage(char** argv);
+
+        static constexpr auto true_string = "true";
+        static const std::string empty_string;
+
+    private:
+        std::map<const std::string, std::string> arguments;
+
+        static const option options[];
+        static const char* optionstr;
+};
+
+}
+}
+}
+// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/cooling-type/cooling_type.cpp b/cooling-type/cooling_type.cpp
new file mode 100644
index 0000000..66d9ac9
--- /dev/null
+++ b/cooling-type/cooling_type.cpp
@@ -0,0 +1,59 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <sdbusplus/bus.hpp>
+#include <phosphor-logging/log.hpp>
+#include "cooling_type.hpp"
+
+namespace phosphor
+{
+namespace cooling
+{
+namespace type
+{
+
+void CoolingType::setAirCooled()
+{
+    airCooled = true;
+}
+
+void CoolingType::setWaterCooled()
+{
+    waterCooled = true;
+}
+
+void CoolingType::setupGpio(const std::string& gpioPath)
+{
+    using namespace phosphor::logging;
+
+    gpioFd = open(gpioPath.c_str(), O_RDONLY);
+    if (gpioFd.is_open())
+    {
+        auto rc = 0;//libevdev_new_from_fd(gpiofd, &gpioDev);//FIXME
+        if (rc < 0)
+        {
+            throw std::runtime_error("Failed to get libevdev from " +
+                                     gpioPath + " rc = " +
+                                     std::to_string(rc));
+        }
+
+        //TODO - more to go here?
+    }
+    else
+    {
+        throw std::runtime_error("Failed to open GPIO file device");
+    }
+
+}
+
+void CoolingType::updateInventory(const std::string& objpath)
+{
+    //TODO
+    //     setProperty(bus, ..., "AirCooled");
+    //     setProperty(bus, ..., "WaterCooled");
+}
+
+}
+}
+}
+
+// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/cooling-type/cooling_type.hpp b/cooling-type/cooling_type.hpp
new file mode 100644
index 0000000..e0240bd
--- /dev/null
+++ b/cooling-type/cooling_type.hpp
@@ -0,0 +1,65 @@
+#pragma once
+#include "utility.hpp"
+
+namespace phosphor
+{
+namespace cooling
+{
+namespace type
+{
+
+constexpr auto INVENTORY_PATH = "/xyz/openbmc_project/inventory";
+constexpr auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager";
+
+class CoolingType
+{
+    public:
+        CoolingType() = delete;
+        ~CoolingType() = default;
+        CoolingType(const CoolingType&) = delete;
+        CoolingType(CoolingType&&) = default;
+        CoolingType& operator=(const CoolingType&) = delete;
+        CoolingType& operator=(CoolingType&&) = default;
+
+        /**
+         * @brief Constructs Cooling Type Object
+         *
+         * @param[in] bus - Dbus bus object
+         */
+        CoolingType(sdbusplus::bus::bus& bus)
+        {
+        }
+
+        /**
+         * @brief Sets airCooled to true.
+         */
+        void setAirCooled();
+        /**
+         * @brief Sets waterCooled to true.
+         */
+        void setWaterCooled();
+        /**
+         * @brief Updates the inventory properties for CoolingType.
+         *
+         * @param[in] path - D-Bus path
+         */
+        void updateInventory(const std::string&);
+        /**
+         * @brief Setup the GPIO device for reading cooling type.
+         *
+         * @param[in] std::string - Path to the GPIO device file to read
+         */
+        void setupGpio(const std::string&);
+
+    private:
+        // File descriptor for the GPIO file we are going to read.
+        phosphor::fan::util::FileDescriptor gpioFd = -1;
+        bool airCooled = false;
+        bool waterCooled = false;
+};
+
+}
+}
+}
+
+// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/cooling-type/main.cpp b/cooling-type/main.cpp
new file mode 100644
index 0000000..cddbe31
--- /dev/null
+++ b/cooling-type/main.cpp
@@ -0,0 +1,75 @@
+#include <iostream>
+#include <memory> //make_unique
+#include <sdbusplus/bus.hpp>
+#include <phosphor-logging/log.hpp>
+#include "argument.hpp"
+#include "cooling_type.hpp"
+
+using namespace phosphor::cooling::type;
+using namespace phosphor::logging;
+
+// Utility function to find the device string for a given pin name.
+std::string findGpio(std::string pinName)
+{
+    std::string path = "/dev/null";
+    //TODO
+    return path;
+}
+
+int main(int argc, char* argv[])
+{
+    auto rc = -1;
+    auto options = ArgumentParser(argc, argv);
+
+    auto objpath = (options)["path"];
+    if (argc < 2)
+    {
+        std::cerr << std::endl << "Too few arguments" << std::endl;
+        log<level::ERR>("Too few arguments");
+        options.usage(argv);
+    }
+    else if (objpath == ArgumentParser::empty_string)
+    {
+        log<level::ERR>("Bus path argument required");
+    }
+    else
+    {
+        auto bus = sdbusplus::bus::new_default();
+        CoolingType coolingType(bus);
+
+        auto gpiopin = (options)["gpio"];
+        if (gpiopin != ArgumentParser::empty_string)
+        {
+            try
+            {
+                auto gpiopath = findGpio(gpiopin);
+                coolingType.setupGpio(gpiopath);
+            }
+            catch (std::exception& err)
+            {
+                rc = -1;
+                log<phosphor::logging::level::ERR>(err.what());
+            }
+        }
+
+        auto air = (options)["air"];
+        if (air != ArgumentParser::empty_string)
+        {
+            coolingType.setAirCooled();
+        }
+
+        auto water = (options)["water"];
+        if (water != ArgumentParser::empty_string)
+        {
+            coolingType.setWaterCooled();
+        }
+
+        coolingType.updateInventory(objpath);
+
+        rc = 0;
+    }
+
+    return rc;
+}
+
+// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
diff --git a/utility.hpp b/utility.hpp
index b37987d..05fe60c 100644
--- a/utility.hpp
+++ b/utility.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <sdbusplus/bus.hpp>
+#include <unistd.h>
 
 namespace phosphor
 {
@@ -9,6 +10,37 @@
 namespace util
 {
 
+class FileDescriptor
+{
+    public:
+        FileDescriptor() = delete;
+        FileDescriptor(const FileDescriptor&) = delete;
+        FileDescriptor(FileDescriptor&&) = default;
+        FileDescriptor& operator=(const FileDescriptor&) = delete;
+        FileDescriptor& operator=(FileDescriptor&&) = default;
+
+        FileDescriptor(int fd) : fd(fd)
+        {
+        }
+
+        ~FileDescriptor()
+        {
+            if (fd != -1)
+            {
+                close(fd);
+            }
+        }
+
+        bool is_open()
+        {
+            return fd != -1;
+        }
+
+    private:
+        int fd = -1;
+
+};
+
 /**
  * @brief Get the inventory service name from the mapper object
  *