mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH 05/11] commands: gpio: Move argument parsing into a shared function
Date: Fri, 26 Oct 2018 18:31:51 -0700	[thread overview]
Message-ID: <20181027013157.23135-6-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20181027013157.23135-1-andrew.smirnov@gmail.com>

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 commands/gpio.c | 48 +++++++++++++++++++++++++++++-------------------
 1 file changed, 29 insertions(+), 19 deletions(-)

diff --git a/commands/gpio.c b/commands/gpio.c
index 08ecc152d..5a28493d1 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)
-- 
2.17.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2018-10-27  1:32 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-27  1:31 [PATCH 00/11] Allow GPIOs to be referenced by label Andrey Smirnov
2018-10-27  1:31 ` [PATCH 01/11] gpio: VF610: Propagate error code of gpiochip_add() up Andrey Smirnov
2018-10-27  1:31 ` [PATCH 02/11] VF610: Initialize pinctrl driver before gpio Andrey Smirnov
2018-10-27  1:31 ` [PATCH 03/11] linux/ctype.h: Port _tolower() Andrey Smirnov
2018-10-27  1:31 ` [PATCH 04/11] lib: Port kstrtox.c from Linux kernel Andrey Smirnov
2018-10-27  1:31 ` Andrey Smirnov [this message]
2018-10-27  1:31 ` [PATCH 06/11] commands: gpio: Use kstrtoint() instead of simple_strtoul() Andrey Smirnov
2018-10-27  1:31 ` [PATCH 07/11] gpiolib: Introduce gpio_find_by_label() Andrey Smirnov
2018-10-27  1:31 ` [PATCH 08/11] commands: gpio: Allow GPIOs to be specified by label Andrey Smirnov
2018-10-27  1:31 ` [PATCH 09/11] VF610: zii-vf610-dev: Drop switch reset GPIO configuration Andrey Smirnov
2018-10-27  1:31 ` [PATCH 10/11] VF610: zii-vf610-dev: Replace board code with gpio-hog nodes Andrey Smirnov
2018-10-29 11:13   ` Sascha Hauer
2018-10-29 17:14     ` Andrey Smirnov
2018-10-27  1:31 ` [PATCH 11/11] ARM: rdu2: " Andrey Smirnov
2018-11-05 16:25   ` Andrey Smirnov
2018-11-06  8:25     ` Sascha Hauer
2018-10-27  8:42 ` [PATCH 00/11] Allow GPIOs to be referenced by label Sam Ravnborg
2018-10-29 11:17   ` Sascha Hauer
2018-10-29 11:14 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181027013157.23135-6-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox