summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorAndrey Smirnov <andrew.smirnov@gmail.com>2018-10-26 18:31:51 -0700
committerSascha Hauer <s.hauer@pengutronix.de>2018-10-29 09:10:38 +0100
commit4f3713adb0bc973122a977205a305813b38f63ba (patch)
treed2a9799755e487dba6a1df161f9190f9dd6521d5 /commands
parente8e26a40956a4bd6847e3c341cda4d34d4086a01 (diff)
downloadbarebox-4f3713adb0bc973122a977205a305813b38f63ba.tar.gz
commands: gpio: Move argument parsing into a shared function
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands')
-rw-r--r--commands/gpio.c48
1 files changed, 29 insertions, 19 deletions
diff --git a/commands/gpio.c b/commands/gpio.c
index 08ecc15..5a28493 100644
--- a/commands/gpio.c
+++ b/commands/gpio.c
@@ -16,14 +16,29 @@
#include <errno.h>
#include <gpio.h>
-static int do_gpio_get_value(int argc, char *argv[])
+static int get_gpio_and_value(int argc, char *argv[],
+ int *gpio, int *value)
{
- int gpio, value;
+ const int count = value ? 3 : 2;
- if (argc < 2)
+ if (argc < count)
return COMMAND_ERROR_USAGE;
- gpio = simple_strtoul(argv[1], NULL, 0);
+ *gpio = simple_strtoul(argv[1], NULL, 0);
+
+ if (value)
+ *value = simple_strtoul(argv[2], NULL, 0);
+
+ return 0;
+}
+
+static int do_gpio_get_value(int argc, char *argv[])
+{
+ int gpio, value, ret;
+
+ ret = get_gpio_and_value(argc, argv, &gpio, NULL);
+ if (ret)
+ return ret;
value = gpio_get_value(gpio);
if (value < 0)
@@ -41,13 +56,11 @@ BAREBOX_CMD_END
static int do_gpio_set_value(int argc, char *argv[])
{
- int gpio, value;
-
- if (argc < 3)
- return COMMAND_ERROR_USAGE;
+ int gpio, value, ret;
- gpio = simple_strtoul(argv[1], NULL, 0);
- value = simple_strtoul(argv[2], NULL, 0);
+ ret = get_gpio_and_value(argc, argv, &gpio, &value);
+ if (ret)
+ return ret;
gpio_set_value(gpio, value);
@@ -65,10 +78,9 @@ static int do_gpio_direction_input(int argc, char *argv[])
{
int gpio, ret;
- if (argc < 2)
- return COMMAND_ERROR_USAGE;
-
- gpio = simple_strtoul(argv[1], NULL, 0);
+ ret = get_gpio_and_value(argc, argv, &gpio, NULL);
+ if (ret)
+ return ret;
ret = gpio_direction_input(gpio);
if (ret)
@@ -88,11 +100,9 @@ static int do_gpio_direction_output(int argc, char *argv[])
{
int gpio, value, ret;
- if (argc < 3)
- return COMMAND_ERROR_USAGE;
-
- gpio = simple_strtoul(argv[1], NULL, 0);
- value = simple_strtoul(argv[2], NULL, 0);
+ ret = get_gpio_and_value(argc, argv, &gpio, &value);
+ if (ret)
+ return ret;
ret = gpio_direction_output(gpio, value);
if (ret)