summaryrefslogtreecommitdiffstats
path: root/gpio-util
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2017-05-25 11:15:43 -0500
committerMatt Spinler <spinler@us.ibm.com>2017-05-30 15:25:31 -0500
commite3b859ca45f17386257fa4e506acd266a18f1aa3 (patch)
tree0859b2fc086ab55311268c3e3d9573f0621bd691 /gpio-util
parent36df1979f5c11d4e4dbe144b63f42390b6088f76 (diff)
downloadphosphor-gpio-monitor-e3b859ca45f17386257fa4e506acd266a18f1aa3.tar.gz
phosphor-gpio-monitor-e3b859ca45f17386257fa4e506acd266a18f1aa3.zip
Fill in gpio-util functionality
Process the command line arguments to know which GPIO to set and what to set it to. Then create the GPIO object and call the appropriate functions on it. Change-Id: Ib8da78e5ae92e5ae0716901aa71243226668be10 Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Diffstat (limited to 'gpio-util')
-rw-r--r--gpio-util/Makefile.am1
-rw-r--r--gpio-util/argument.cpp95
-rw-r--r--gpio-util/argument.hpp64
-rw-r--r--gpio-util/main.cpp180
4 files changed, 340 insertions, 0 deletions
diff --git a/gpio-util/Makefile.am b/gpio-util/Makefile.am
index 9bdc42d..241cb19 100644
--- a/gpio-util/Makefile.am
+++ b/gpio-util/Makefile.am
@@ -5,6 +5,7 @@ sbin_PROGRAMS = \
phosphor-gpio-util
phosphor_gpio_util_SOURCES = \
+ argument.cpp \
gpio.cpp \
main.cpp
diff --git a/gpio-util/argument.cpp b/gpio-util/argument.cpp
new file mode 100644
index 0000000..222b7d3
--- /dev/null
+++ b/gpio-util/argument.cpp
@@ -0,0 +1,95 @@
+/**
+ * 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 "argument.hpp"
+
+namespace phosphor
+{
+namespace gpio
+{
+
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+ int 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 : trueString);
+ }
+ }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+ auto i = arguments.find(opt);
+ if (i == arguments.end())
+ {
+ return emptyString;
+ }
+ else
+ {
+ return i->second;
+ }
+}
+
+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 << " --gpio=<gpio> The GPIO number. Example: 1\n";
+ std::cerr << " --path=<path> The path to the GPIO device."
+ " Example: /dev/gpiochip0\n";
+ std::cerr << " --delay=<delay> The delay in ms in between a toggle."
+ " Example: 5\n";
+ std::cerr << " --action=<action> The action to do.\n";
+ std::cerr << " Valid actions: low, high, low_high, "
+ "high_low\n";
+ std::cerr << std::flush;
+}
+
+const option ArgumentParser::options[] =
+{
+ { "action", required_argument, NULL, 'a' },
+ { "gpio", required_argument, NULL, 'g' },
+ { "delay", required_argument, NULL, 'd' },
+ { "path", required_argument, NULL, 'p' },
+ { "help", no_argument, NULL, 'h' },
+ { 0, 0, 0, 0},
+};
+
+const char* ArgumentParser::optionStr = "a:g:d:p:h?";
+
+const std::string ArgumentParser::trueString = "true";
+const std::string ArgumentParser::emptyString = "";
+
+}
+}
diff --git a/gpio-util/argument.hpp b/gpio-util/argument.hpp
new file mode 100644
index 0000000..a6cccc7
--- /dev/null
+++ b/gpio-util/argument.hpp
@@ -0,0 +1,64 @@
+#pragma once
+
+#include <getopt.h>
+#include <map>
+#include <string>
+
+namespace phosphor
+{
+namespace gpio
+{
+/** @brief Class - Encapsulates parsing command line options and
+ * populating arguments
+ */
+class ArgumentParser
+{
+ public:
+ ArgumentParser() = delete;
+ ~ArgumentParser() = default;
+ ArgumentParser(const ArgumentParser&) = delete;
+ ArgumentParser& operator=(const ArgumentParser&) = delete;
+ ArgumentParser(ArgumentParser&&) = default;
+ ArgumentParser& operator=(ArgumentParser&&) = default;
+
+ /** @brief Contructs Argument object
+ *
+ * @param argc - the main function's argc passed as is
+ * @param argv - the main function's argv passed as is
+ * @return Object constructed
+ */
+ ArgumentParser(int argc, char** argv);
+
+ /** @brief Given an option, returns its argument(optarg)
+ *
+ * @param opt - command line option string
+ *
+ * @return argument which is a standard optarg
+ */
+ const std::string& operator[](const std::string& opt);
+
+ /** @brief Displays usage
+ *
+ * @param argv - the main function's argv passed as is
+ */
+ static void usage(char** argv);
+
+ /** @brief Set to 'true' when an option is passed */
+ static const std::string trueString;
+
+ /** @brief Set to '' when an option is not passed */
+ static const std::string emptyString;
+
+ private:
+ /** @brief Option to argument mapping */
+ std::map<const std::string, std::string> arguments;
+
+ /** @brief Array of struct options as needed by getopt_long */
+ static const option options[];
+
+ /** @brief optstring as needed by getopt_long */
+ static const char* optionStr;
+};
+
+} // namespace gpio
+} // namespace phosphor
diff --git a/gpio-util/main.cpp b/gpio-util/main.cpp
index b8801aa..fb56e66 100644
--- a/gpio-util/main.cpp
+++ b/gpio-util/main.cpp
@@ -14,7 +14,187 @@
* limitations under the License.
*/
+/**
+ * This program is a utility for accessing GPIOs.
+ * Actions:
+ * low: Set a GPIO low
+ * high: Set a GPIO high
+ * low_high: Set a GPIO low, delay if requested, set it high
+ * high_low: Set a GPIO high, delay if requested, set it low
+ */
+
+#include <algorithm>
+#include <chrono>
+#include <iostream>
+#include <map>
+#include <thread>
+#include <phosphor-logging/log.hpp>
+#include "argument.hpp"
+#include "gpio.hpp"
+
+using namespace phosphor::gpio;
+using namespace phosphor::logging;
+
+typedef void (*gpioFunction)(GPIO&, unsigned int);
+using gpioFunctionMap = std::map<std::string, gpioFunction>;
+
+/**
+ * Sets a GPIO low
+ *
+ * @param[in] gpio - the GPIO object
+ * @param[in] delayInMS - Unused in this function
+ */
+void low(GPIO& gpio, unsigned int delayInMS)
+{
+ gpio.set(GPIO::Value::low);
+}
+
+
+/**
+ * Sets a GPIO high
+ *
+ * @param[in] gpio - the GPIO object
+ * @param[in] delayInMS - Unused in this function
+ */
+void high(GPIO& gpio, unsigned int delayInMS)
+{
+ gpio.set(GPIO::Value::high);
+}
+
+
+/**
+ * Sets a GPIO high, then delays, then sets it low
+ *
+ * @param[in] gpio - the GPIO object
+ * @param[in] delayInMS - The delay in between the sets
+ */
+void highLow(GPIO& gpio, unsigned int delayInMS)
+{
+ gpio.set(GPIO::Value::high);
+
+ std::chrono::milliseconds delay{delayInMS};
+ std::this_thread::sleep_for(delay);
+
+ gpio.set(GPIO::Value::low);
+}
+
+
+/**
+ * Sets a GPIO low, then delays, then sets it high
+ *
+ * @param[in] gpio - the GPIO to write
+ * @param[in] delayInMS - The delay in between the sets
+ */
+void lowHigh(GPIO& gpio, unsigned int delayInMS)
+{
+ gpio.set(GPIO::Value::low);
+
+ std::chrono::milliseconds delay{delayInMS};
+ std::this_thread::sleep_for(delay);
+
+ gpio.set(GPIO::Value::high);
+}
+
+
+/**
+ * The actions supported by this program
+ */
+static const gpioFunctionMap functions
+{
+ {"low", low},
+ {"high", high},
+ {"low_high", lowHigh},
+ {"high_low", highLow}
+};
+
+
+/**
+ * Prints usage and exits the program
+ *
+ * @param[in] err - the error message to print
+ * @param[in] argv - argv from main()
+ */
+void exitWithError(const char* err, char** argv)
+{
+ std::cerr << "ERROR: " << err << "\n";
+ ArgumentParser::usage(argv);
+ exit(EXIT_FAILURE);
+}
+
+
+/**
+ * Returns the number value of the argument passed in.
+ *
+ * @param[in] name - the argument name
+ * @param[in] parser - the argument parser
+ * @param[in] argv - arv from main()
+ */
+template<typename T>
+T getValueFromArg(const char* name,
+ ArgumentParser& parser,
+ char** argv)
+{
+ char* p = NULL;
+ auto val = strtol(parser[name].c_str(), &p, 10);
+
+ //strol sets p on error, also we don't allow negative values
+ if (*p || (val < 0))
+ {
+ using namespace std::string_literals;
+ std::string msg = "Invalid "s + name + " value passed in";
+ exitWithError(msg.c_str(), argv);
+ }
+ return static_cast<T>(val);
+}
+
+
int main(int argc, char** argv)
{
+ ArgumentParser args(argc, argv);
+
+ auto path = args["path"];
+ if (path == ArgumentParser::emptyString)
+ {
+ exitWithError("GPIO device path not specified", argv);
+ }
+
+ auto action = args["action"];
+ if (action == ArgumentParser::emptyString)
+ {
+ exitWithError("Action not specified", argv);
+ }
+
+ if (args["gpio"] == ArgumentParser::emptyString)
+ {
+ exitWithError("GPIO not specified", argv);
+ }
+
+ auto gpioNum = getValueFromArg<gpioNum_t>("gpio", args, argv);
+
+ //Not all actions require a delay, so not required
+ unsigned int delay = 0;
+ if (args["delay"] != ArgumentParser::emptyString)
+ {
+ delay = getValueFromArg<decltype(delay)>("delay", args, argv);
+ }
+
+ auto function = functions.find(action);
+ if (function == functions.end())
+ {
+ exitWithError("Invalid action value passed in", argv);
+ }
+
+ GPIO gpio{path, gpioNum, GPIO::Direction::output};
+
+ try
+ {
+ function->second(gpio, delay);
+ }
+ catch (std::runtime_error& e)
+ {
+ std::cerr << e.what();
+ return -1;
+ }
+
return 0;
}
OpenPOWER on IntegriCloud