summaryrefslogtreecommitdiffstats
path: root/gpio-util
diff options
context:
space:
mode:
authorPatrick Venture <venture@google.com>2018-11-01 16:52:10 -0700
committerPatrick Venture <venture@google.com>2018-11-01 16:59:54 -0700
commitdace680fa404d7c02a68db105fd7b958b4681442 (patch)
tree6d447ced41a2d1ef9bbd8206d52099609cd973d1 /gpio-util
parent3c4a23e7764c7785d18cb30340a6d185022a797a (diff)
downloadphosphor-gpio-monitor-dace680fa404d7c02a68db105fd7b958b4681442.tar.gz
phosphor-gpio-monitor-dace680fa404d7c02a68db105fd7b958b4681442.zip
Add clang-format to repo
Add clang-format to repo. Change-Id: I3f8704d80dd0bdde0706ab189c68cffe6d347995 Signed-off-by: Patrick Venture <venture@google.com>
Diffstat (limited to 'gpio-util')
-rw-r--r--gpio-util/argument.cpp30
-rw-r--r--gpio-util/argument.hpp75
-rw-r--r--gpio-util/gpio.cpp30
-rw-r--r--gpio-util/gpio.hpp170
-rw-r--r--gpio-util/main.cpp33
5 files changed, 161 insertions, 177 deletions
diff --git a/gpio-util/argument.cpp b/gpio-util/argument.cpp
index 222b7d3..913313c 100644
--- a/gpio-util/argument.cpp
+++ b/gpio-util/argument.cpp
@@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include "argument.hpp"
+
+#include <algorithm>
#include <iostream>
#include <iterator>
-#include <algorithm>
-#include "argument.hpp"
namespace phosphor
{
@@ -67,23 +68,22 @@ void ArgumentParser::usage(char** argv)
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";
+ " Example: /dev/gpiochip0\n";
std::cerr << " --delay=<delay> The delay in ms in between a toggle."
- " Example: 5\n";
+ " Example: 5\n";
std::cerr << " --action=<action> The action to do.\n";
std::cerr << " Valid actions: low, high, low_high, "
- "high_low\n";
+ "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 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?";
@@ -91,5 +91,5 @@ const char* ArgumentParser::optionStr = "a:g:d:p:h?";
const std::string ArgumentParser::trueString = "true";
const std::string ArgumentParser::emptyString = "";
-}
-}
+} // namespace gpio
+} // namespace phosphor
diff --git a/gpio-util/argument.hpp b/gpio-util/argument.hpp
index 3debea5..7917b8a 100644
--- a/gpio-util/argument.hpp
+++ b/gpio-util/argument.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <getopt.h>
+
#include <map>
#include <string>
@@ -13,51 +14,51 @@ namespace gpio
*/
class ArgumentParser
{
- public:
- ArgumentParser() = delete;
- ~ArgumentParser() = default;
- ArgumentParser(const ArgumentParser&) = delete;
- ArgumentParser& operator=(const ArgumentParser&) = delete;
- ArgumentParser(ArgumentParser&&) = default;
- ArgumentParser& operator=(ArgumentParser&&) = default;
+ public:
+ ArgumentParser() = delete;
+ ~ArgumentParser() = default;
+ ArgumentParser(const ArgumentParser&) = delete;
+ ArgumentParser& operator=(const ArgumentParser&) = delete;
+ ArgumentParser(ArgumentParser&&) = default;
+ ArgumentParser& operator=(ArgumentParser&&) = default;
- /** @brief Constructs 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 Constructs 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 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 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 '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;
+ /** @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;
+ 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 Array of struct options as needed by getopt_long */
+ static const option options[];
- /** @brief optstring as needed by getopt_long */
- static const char* optionStr;
+ /** @brief optstring as needed by getopt_long */
+ static const char* optionStr;
};
} // namespace gpio
diff --git a/gpio-util/gpio.cpp b/gpio-util/gpio.cpp
index 080ab99..da5801e 100644
--- a/gpio-util/gpio.cpp
+++ b/gpio-util/gpio.cpp
@@ -13,10 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include "gpio.hpp"
+
#include <fcntl.h>
-#include <phosphor-logging/log.hpp>
#include <sys/ioctl.h>
-#include "gpio.hpp"
+
+#include <phosphor-logging/log.hpp>
namespace phosphor
{
@@ -38,16 +40,14 @@ void GPIO::set(Value value)
if (rc == -1)
{
auto e = errno;
- log<level::ERR>("Failed SET_LINE_VALUES ioctl",
- entry("ERRNO=%d", e));
+ log<level::ERR>("Failed SET_LINE_VALUES ioctl", entry("ERRNO=%d", e));
throw std::runtime_error("Failed SET_LINE_VALUES ioctl");
}
}
-
void GPIO::requestLine(Value defaultValue)
{
- //Only need to do this once
+ // Only need to do this once
if (lineFD)
{
return;
@@ -63,15 +63,14 @@ void GPIO::requestLine(Value defaultValue)
throw std::runtime_error("Failed opening GPIO device");
}
- //Make an ioctl call to request the GPIO line, which will
- //return the descriptor to use to access it.
+ // Make an ioctl call to request the GPIO line, which will
+ // return the descriptor to use to access it.
gpiohandle_request request{};
- strncpy(request.consumer_label,
- "phosphor-gpio-util",
+ strncpy(request.consumer_label, "phosphor-gpio-util",
sizeof(request.consumer_label));
- request.flags = (direction == Direction::output) ?
- GPIOHANDLE_REQUEST_OUTPUT : GPIOHANDLE_REQUEST_INPUT;
+ request.flags = (direction == Direction::output) ? GPIOHANDLE_REQUEST_OUTPUT
+ : GPIOHANDLE_REQUEST_INPUT;
request.lineoffsets[0] = gpio;
request.lines = 1;
@@ -85,8 +84,7 @@ void GPIO::requestLine(Value defaultValue)
if (rc == -1)
{
auto e = errno;
- log<level::ERR>("Failed GET_LINEHANDLE ioctl",
- entry("GPIO=%d", gpio),
+ log<level::ERR>("Failed GET_LINEHANDLE ioctl", entry("GPIO=%d", gpio),
entry("ERRNO=%d", e));
throw std::runtime_error("Failed GET_LINEHANDLE ioctl");
}
@@ -94,5 +92,5 @@ void GPIO::requestLine(Value defaultValue)
lineFD.set(request.fd);
}
-}
-}
+} // namespace gpio
+} // namespace phosphor
diff --git a/gpio-util/gpio.hpp b/gpio-util/gpio.hpp
index ba8cce3..d4ac47b 100644
--- a/gpio-util/gpio.hpp
+++ b/gpio-util/gpio.hpp
@@ -1,18 +1,22 @@
#pragma once
-#include <linux/gpio.h>
#include "file.hpp"
+#include <linux/gpio.h>
+
+#include <string>
+#include <type_traits>
+
namespace phosphor
{
namespace gpio
{
- typedef std::remove_reference<decltype(
- gpiohandle_request::lineoffsets[0])>::type gpioNum_t;
+typedef std::remove_reference<decltype(
+ gpiohandle_request::lineoffsets[0])>::type gpioNum_t;
- typedef std::remove_reference<decltype(
- gpiohandle_data::values[0])>::type gpioValue_t;
+typedef std::remove_reference<decltype(gpiohandle_data::values[0])>::type
+ gpioValue_t;
/**
* Represents a GPIO.
@@ -23,86 +27,80 @@ namespace gpio
*/
class GPIO
{
- public:
-
- /**
- * If the GPIO is an input or output
- */
- enum class Direction
- {
- input,
- output
- };
-
- /**
- * The possible values - low or high
- */
- enum class Value
- {
- low,
- high
- };
-
- GPIO() = delete;
- GPIO(const GPIO&) = delete;
- GPIO(GPIO&&) = default;
- GPIO& operator=(const GPIO&) = delete;
- GPIO& operator=(GPIO&&) = default;
- ~GPIO() = default;
-
- /**
- * Constructor
- *
- * @param[in] device - the GPIO device file
- * @param[in] gpio - the GPIO number
- * @param[in] direction - the GPIO direction
- */
- GPIO(const std::string& device,
- gpioNum_t gpio,
- Direction direction) :
- device(device),
- gpio(gpio),
- direction(direction)
- {
- }
-
- /**
- * Sets the GPIO value
- *
- * Requests the GPIO line if it hasn't been done already.
- */
- void set(Value value);
-
- private:
-
- /**
- * Requests a GPIO line from the GPIO device
- *
- * @param[in] defaultValue - The default value, required for
- * output GPIOs only.
- */
- void requestLine(Value defaultValue = Value::high);
-
- /**
- * The GPIO device name, like /dev/gpiochip0
- */
- const std::string device;
-
- /**
- * The GPIO number
- */
- const gpioNum_t gpio;
-
- /**
- * The GPIO direction
- */
- const Direction direction;
-
- /**
- * File descriptor for the GPIO line
- */
- FileDescriptor lineFD;
+ public:
+ /**
+ * If the GPIO is an input or output
+ */
+ enum class Direction
+ {
+ input,
+ output
+ };
+
+ /**
+ * The possible values - low or high
+ */
+ enum class Value
+ {
+ low,
+ high
+ };
+
+ GPIO() = delete;
+ GPIO(const GPIO&) = delete;
+ GPIO(GPIO&&) = default;
+ GPIO& operator=(const GPIO&) = delete;
+ GPIO& operator=(GPIO&&) = default;
+ ~GPIO() = default;
+
+ /**
+ * Constructor
+ *
+ * @param[in] device - the GPIO device file
+ * @param[in] gpio - the GPIO number
+ * @param[in] direction - the GPIO direction
+ */
+ GPIO(const std::string& device, gpioNum_t gpio, Direction direction) :
+ device(device), gpio(gpio), direction(direction)
+ {
+ }
+
+ /**
+ * Sets the GPIO value
+ *
+ * Requests the GPIO line if it hasn't been done already.
+ */
+ void set(Value value);
+
+ private:
+ /**
+ * Requests a GPIO line from the GPIO device
+ *
+ * @param[in] defaultValue - The default value, required for
+ * output GPIOs only.
+ */
+ void requestLine(Value defaultValue = Value::high);
+
+ /**
+ * The GPIO device name, like /dev/gpiochip0
+ */
+ const std::string device;
+
+ /**
+ * The GPIO number
+ */
+ const gpioNum_t gpio;
+
+ /**
+ * The GPIO direction
+ */
+ const Direction direction;
+
+ /**
+ * File descriptor for the GPIO line
+ */
+ FileDescriptor lineFD;
};
-}
-}
+} // namespace gpio
+} // namespace phosphor
diff --git a/gpio-util/main.cpp b/gpio-util/main.cpp
index fb56e66..89e61f9 100644
--- a/gpio-util/main.cpp
+++ b/gpio-util/main.cpp
@@ -23,14 +23,15 @@
* high_low: Set a GPIO high, delay if requested, set it low
*/
+#include "argument.hpp"
+#include "gpio.hpp"
+
#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
-#include <thread>
#include <phosphor-logging/log.hpp>
-#include "argument.hpp"
-#include "gpio.hpp"
+#include <thread>
using namespace phosphor::gpio;
using namespace phosphor::logging;
@@ -49,7 +50,6 @@ void low(GPIO& gpio, unsigned int delayInMS)
gpio.set(GPIO::Value::low);
}
-
/**
* Sets a GPIO high
*
@@ -61,7 +61,6 @@ void high(GPIO& gpio, unsigned int delayInMS)
gpio.set(GPIO::Value::high);
}
-
/**
* Sets a GPIO high, then delays, then sets it low
*
@@ -78,7 +77,6 @@ void highLow(GPIO& gpio, unsigned int delayInMS)
gpio.set(GPIO::Value::low);
}
-
/**
* Sets a GPIO low, then delays, then sets it high
*
@@ -95,18 +93,11 @@ void lowHigh(GPIO& gpio, unsigned int delayInMS)
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}
-};
-
+static const gpioFunctionMap functions{
+ {"low", low}, {"high", high}, {"low_high", lowHigh}, {"high_low", highLow}};
/**
* Prints usage and exits the program
@@ -121,7 +112,6 @@ void exitWithError(const char* err, char** argv)
exit(EXIT_FAILURE);
}
-
/**
* Returns the number value of the argument passed in.
*
@@ -129,15 +119,13 @@ void exitWithError(const char* err, char** argv)
* @param[in] parser - the argument parser
* @param[in] argv - arv from main()
*/
-template<typename T>
-T getValueFromArg(const char* name,
- ArgumentParser& parser,
- char** argv)
+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
+ // strol sets p on error, also we don't allow negative values
if (*p || (val < 0))
{
using namespace std::string_literals;
@@ -147,7 +135,6 @@ T getValueFromArg(const char* name,
return static_cast<T>(val);
}
-
int main(int argc, char** argv)
{
ArgumentParser args(argc, argv);
@@ -171,7 +158,7 @@ int main(int argc, char** argv)
auto gpioNum = getValueFromArg<gpioNum_t>("gpio", args, argv);
- //Not all actions require a delay, so not required
+ // Not all actions require a delay, so not required
unsigned int delay = 0;
if (args["delay"] != ArgumentParser::emptyString)
{
OpenPOWER on IntegriCloud