linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zev Weiss <zweiss@equinix.com>
To: Joel Stanley <joel@jms.id.au>
Cc: Bartosz Golaszewski <brgl@bgdev.pl>,
	"linux-gpio@vger.kernel.org" <linux-gpio@vger.kernel.org>,
	Andrew Jeffery <andrew@aj.id.au>,
	"openbmc@lists.ozlabs.org" <openbmc@lists.ozlabs.org>
Subject: Re: [libgpiod PATCH 5/7] tools: gpioset: Add by-name support
Date: Thu, 3 Feb 2022 08:37:55 +0000	[thread overview]
Message-ID: <20220203083755.GP5754@packtop> (raw)
In-Reply-To: <20220203042134.68425-6-joel@jms.id.au>

On Wed, Feb 02, 2022 at 08:21:32PM PST, Joel Stanley wrote:
>Allow users to set the values of gpios by passing the gpio name. The
>gpipchip is not specified, instead it is discovered using the same
>method as gpiofind.
>
> $ gpioset  --mode=wait --by-name cfam-reset=1
>
>Signed-off-by: Joel Stanley <joel@jms.id.au>
>---
> tools/gpioset.c | 68 ++++++++++++++++++++++++++++++++++---------------
> 1 file changed, 48 insertions(+), 20 deletions(-)
>
>diff --git a/tools/gpioset.c b/tools/gpioset.c
>index 7882b53bab41..fe3e1c246c89 100644
>--- a/tools/gpioset.c
>+++ b/tools/gpioset.c
>@@ -23,15 +23,18 @@ static const struct option longopts[] = {
> 	{ "sec",		required_argument,	NULL,	's' },
> 	{ "usec",		required_argument,	NULL,	'u' },
> 	{ "background",		no_argument,		NULL,	'b' },
>+	{ "by-name",		no_argument,		NULL,	'N' },
> 	{ GETOPT_NULL_LONGOPT },
> };
>
>-static const char *const shortopts = "+hvlB:D:m:s:u:b";
>+static const char *const shortopts = "+hvlB:D:m:s:u:bN";
>
> static void print_help(void)
> {
> 	printf("Usage: %s [OPTIONS] <chip name/number> <offset1>=<value1> <offset2>=<value2> ...\n",
> 	       get_progname());
>+	printf("       %s [OPTIONS] -L <line name1> <line name2> ...\n",

s/-L/-N/ here again, I think...

>+	       get_progname());
> 	printf("\n");
> 	printf("Set GPIO line values of a GPIO chip and maintain the state until the process exits\n");
> 	printf("\n");
>@@ -48,6 +51,7 @@ static void print_help(void)
> 	printf("  -s, --sec=SEC:\tspecify the number of seconds to wait (only valid for --mode=time)\n");
> 	printf("  -u, --usec=USEC:\tspecify the number of microseconds to wait (only valid for --mode=time)\n");
> 	printf("  -b, --background:\tafter setting values: detach from the controlling terminal\n");
>+	printf("  -N, --by-name:\tset line by name. All lines must be from the same gpiochip\n");
> 	printf("\n");
> 	print_bias_help();
> 	printf("\n");
>@@ -195,7 +199,8 @@ int main(int argc, char **argv)
> 	struct gpiod_line_bulk *lines;
> 	struct callback_data cbdata;
> 	struct gpiod_chip *chip;
>-	char *device, *end;
>+	bool by_name = false;
>+	char *end;
>
> 	memset(&cbdata, 0, sizeof(cbdata));
>
>@@ -239,6 +244,9 @@ int main(int argc, char **argv)
> 		case 'b':
> 			cbdata.daemonize = true;
> 			break;
>+		case 'N':
>+			by_name = true;
>+			break;
> 		case '?':
> 			die("try %s --help", get_progname());
> 		default:
>@@ -257,37 +265,57 @@ int main(int argc, char **argv)
> 	    cbdata.daemonize)
> 		die("can't daemonize in this mode");
>
>-	if (argc < 1)
>-		die("gpiochip must be specified");
>+	if (by_name) {
>+		char *line_name;
>+
>+		if (argc < 1)
>+			die("at least one line name must be specified");
>+
>+		line_name = split_line(argv[0]);
>+		chip = chip_by_line_name(line_name);
>+		if (!chip)
>+			die("unable to find '%s' on a gpiochip", line_name);
>+
>+		free(line_name);
>
>-	if (argc < 2)
>-		die("at least one GPIO line offset to value mapping must be specified");
>+		num_lines = argc;
>+	} else {
>+		if (argc < 1)
>+			die("gpiochip must be specified");
>
>-	device = argv[0];
>+		if (argc < 2)
>+			die("at least one GPIO line offset to value mapping must be specified");
>
>-	num_lines = argc - 1;
>+
>+		chip = chip_open_lookup(argv[0]);
>+		if (!chip)
>+			die_perror("unable to open %s", argv[0]);
>+
>+		num_lines = argc - 1;
>+		argv++;
>+	}
>
> 	offsets = malloc(sizeof(*offsets) * num_lines);
> 	values = malloc(sizeof(*values) * num_lines);
> 	if (!values || !offsets)
> 		die("out of memory");
>
>-	for (i = 0; i < num_lines; i++) {
>-		rv = sscanf(argv[i + 1], "%u=%d", &offsets[i], &values[i]);
>-		if (rv != 2)
>-			die("invalid offset<->value mapping: %s", argv[i + 1]);
>+	if (by_name) {
>+		line_names_to_offsets(chip, argv, offsets, values, num_lines);
>+	} else {
>+		for (i = 0; i < num_lines; i++) {
>+			rv = sscanf(argv[i], "%u=%d", &offsets[i], &values[i]);
>+			if (rv != 2)
>+				die("invalid offset<->value mapping: %s", argv[i + 1]);
>
>-		if (values[i] != 0 && values[i] != 1)
>-			die("value must be 0 or 1: %s", argv[i + 1]);
>+			if (values[i] != 0 && values[i] != 1)
>+				die("value must be 0 or 1: %s", argv[i]);
>
>-		if (offsets[i] > INT_MAX)
>-			die("invalid offset: %s", argv[i + 1]);
>+			if (offsets[i] > INT_MAX)
>+				die("invalid offset: %s", argv[i]);
>+		}
> 	}
>
>-	chip = chip_open_lookup(device);
>-	if (!chip)
>-		die_perror("unable to open %s", device);
>-
> 	lines = gpiod_chip_get_lines(chip, offsets, num_lines);
> 	if (!lines)
> 		die_perror("unable to retrieve GPIO lines from chip");
>-- 
>2.34.1
>

  reply	other threads:[~2022-02-03  8:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-03  4:21 [libgpiod PATCH 0/7] tools: Add by-name support Joel Stanley
2022-02-03  4:21 ` [libgpiod PATCH 1/7] tools: Clean up scandir memory allocations Joel Stanley
2022-02-08 11:21   ` Bartosz Golaszewski
2022-02-03  4:21 ` [libgpiod PATCH 2/7] tools: Add line name to offset lookup helper Joel Stanley
2022-02-03  4:21 ` [libgpiod PATCH 3/7] tools: Add value support to line name lookup Joel Stanley
2022-02-03  8:37   ` Zev Weiss
2022-02-03  4:21 ` [libgpiod PATCH 4/7] tools: gpioget: Add by-name support Joel Stanley
2022-02-03  8:37   ` Zev Weiss
2022-02-03  4:21 ` [libgpiod PATCH 5/7] tools: gpioset: " Joel Stanley
2022-02-03  8:37   ` Zev Weiss [this message]
2022-02-03  4:21 ` [libgpiod PATCH 6/7] gpio-tools-test: Add gpioset --by-name tests Joel Stanley
2022-02-03  4:21 ` [libgpiod PATCH 7/7] gpio-tools-test: Add gpioget " Joel Stanley

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=20220203083755.GP5754@packtop \
    --to=zweiss@equinix.com \
    --cc=andrew@aj.id.au \
    --cc=brgl@bgdev.pl \
    --cc=joel@jms.id.au \
    --cc=linux-gpio@vger.kernel.org \
    --cc=openbmc@lists.ozlabs.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;
as well as URLs for NNTP newsgroup(s).