diff options
-rw-r--r-- | Makefile.am | 6 | ||||
-rw-r--r-- | argument.cpp | 83 | ||||
-rw-r--r-- | argument.hpp | 58 | ||||
-rwxr-xr-x | bootstrap.sh | 18 | ||||
-rw-r--r-- | configure.ac | 38 | ||||
-rw-r--r-- | controller.cpp | 49 |
6 files changed, 252 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..fdf4020 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,6 @@ +sbin_PROGRAMS = phosphor-ledcontroller + +phosphor_ledcontroller_SOURCES = controller.cpp argument.cpp + +phosphor_ledcontroller_LDFLAGS = $(SYSTEMD_LIBS) +phosphor_ledcontroller_CFLAGS = $(SYSTEMD_CFLAGS) diff --git a/argument.cpp b/argument.cpp new file mode 100644 index 0000000..3df84e7 --- /dev/null +++ b/argument.cpp @@ -0,0 +1,83 @@ +/** + * Copyright © 2016 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 led +{ + +const std::string ArgumentParser::true_string = "true"; +const std::string ArgumentParser::empty_string = ""; + +const char* ArgumentParser::optionstr = "n:p:?h"; +const option ArgumentParser::options[] = +{ + { "name", required_argument, nullptr, 'n' }, + { "path", required_argument, nullptr, 'p' }, + { "help", no_argument, nullptr, 'h' }, + { 0, 0, 0, 0}, +}; + +ArgumentParser::ArgumentParser(int argc, char** argv) +{ + int option = 0; + while(-1 != (option = getopt_long(argc, argv, optionstr, options, nullptr))) + { + 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; + } +} + +void ArgumentParser::usage(char** argv) +{ + std::cerr << "Usage: " << argv[0] << " [options]" << std::endl; + std::cerr << "Options:" << std::endl; + std::cerr << " --help Print this menu" << std::endl; + std::cerr << " --name=<name> Name of the LED" + << std::endl; + std::cerr << " --path=<path> sysfs path like /sys/class/leds" + << std::endl; +} +} // namespace led +} // namespace phosphor diff --git a/argument.hpp b/argument.hpp new file mode 100644 index 0000000..28b2966 --- /dev/null +++ b/argument.hpp @@ -0,0 +1,58 @@ +#ifndef __ARGUMENT_H +#define __ARGUMENT_H +#include <getopt.h> +#include <map> +#include <string> + +namespace phosphor +{ +namespace led +{ +/** @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 a option, returns its argument(optarg) */ + const std::string& operator[](const std::string& opt); + + /** @brief Displays usage */ + static void usage(char** argv); + + /** @brief Set to 'true' when an option is passed */ + static const std::string true_string; + + /** @brief Set to '' when an option is not passed */ + static const std::string empty_string; + + 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 led +} // namespace phosphor + +#endif diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100755 index 0000000..50b75b7 --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +AUTOCONF_FILES="Makefile.in aclocal.m4 ar-lib autom4te.cache compile \ + config.guess config.h.in config.sub configure depcomp install-sh \ + ltmain.sh missing *libtool test-driver" + +case $1 in + clean) + test -f Makefile && make maintainer-clean + for file in ${AUTOCONF_FILES}; do + find -name "$file" | xargs -r rm -rf + done + exit 0 + ;; +esac + +autoreconf -i +echo 'Run "./configure ${CONFIGURE_FLAGS} && make"' diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..b38f165 --- /dev/null +++ b/configure.ac @@ -0,0 +1,38 @@ +# Initialization +AC_PREREQ([2.69]) +AC_INIT([phosphor-led-sysfs], [1.0], [https://github.com/openbmc/phosphor-led-sysfs/issues]) +AC_LANG([C++]) +AC_CONFIG_HEADERS([config.h]) +AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz]) +AM_SILENT_RULES([yes]) + +# Checks for programs. +AC_PROG_CXX +AC_PROG_INSTALL +AC_PROG_MAKE_SET + +# Checks for libraries. +PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221]) + +# Checks for typedefs, structures, and compiler characteristics. +AX_CXX_COMPILE_STDCXX_14([noext]) +AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS]) + +# Checks for header files. +AC_CHECK_HEADER(systemd/sd-bus.h, ,[AC_MSG_ERROR([Could not find systemd/sd-bus.h...systemd developement package required])]) +AC_CHECK_HEADER(sdbusplus/server.hpp, ,[AC_MSG_ERROR([Could not find server.hpp...openbmc/sdbusplus package required])]) + +# Check/set gtest specific functions. +AX_PTHREAD([GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=1"],[GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=0"]) +AC_SUBST(GTEST_CPPFLAGS) + +AC_ARG_VAR(BUSNAME, [The Dbus busname to own]) +AS_IF([test "x$BUSNAME" == "x"], [BUSNAME="xyz.openbmc_project.led.controller"]) +AC_DEFINE_UNQUOTED([BUSNAME], ["$BUSNAME"], [The Dbus busname to own]) +AC_ARG_VAR(OBJPATH, [The Ledmanager Dbus root]) +AS_IF([test "x$OBJPATH" == "x"], [OBJPATH="/xyz/openbmc_project/led/controller"]) +AC_DEFINE_UNQUOTED([OBJPATH], ["$OBJPATH"], [The Led controller Dbus root]) + +# Create configured output +AC_CONFIG_FILES([Makefile]) +AC_OUTPUT diff --git a/controller.cpp b/controller.cpp new file mode 100644 index 0000000..8db607c --- /dev/null +++ b/controller.cpp @@ -0,0 +1,49 @@ +/** + * Copyright © 2016 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 <memory> +#include "argument.hpp" + +static void ExitWithError(const char* err, char** argv) +{ + phosphor::led::ArgumentParser::usage(argv); + std::cerr << std::endl; + std::cerr << "ERROR: " << err << std::endl; + exit(-1); +} + +int main(int argc, char** argv) +{ + // Read arguments. + auto options = phosphor::led::ArgumentParser(argc, argv); + + // Parse out Name argument. + auto name = std::move((options)["name"]); + if (name == phosphor::led::ArgumentParser::empty_string) + { + ExitWithError("Name not specified.", argv); + } + + // Parse out Name argument. + auto path = std::move((options)["path"]); + if (path == phosphor::led::ArgumentParser::empty_string) + { + ExitWithError("Path not specified.", argv); + } + + return 0; +} |