Linux-GPIO Archive on lore.kernel.org
 help / color / Atom feed
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Kent Gibson <warthog618@gmail.com>,
	Linus Walleij <linus.walleij@linaro.org>,
	"open list:GPIO SUBSYSTEM" <linux-gpio@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: Re: [PATCH v2 11/11] tools: gpio: implement gpio-watch
Date: Thu, 5 Dec 2019 00:42:39 +0200
Message-ID: <CAHp75VcqYsKUzxGUhn8aHg_u-B=FkqmTU2YS+yyVNfAPu+715w@mail.gmail.com> (raw)
In-Reply-To: <20191204155941.17814-2-brgl@bgdev.pl>

On Wed, Dec 4, 2019 at 7:19 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> Add a simple program that allows to test the new LINECHANGED_FD ioctl().

> --- a/tools/gpio/.gitignore
> +++ b/tools/gpio/.gitignore
> @@ -1,4 +1,5 @@
>  gpio-event-mon
>  gpio-hammer
>  lsgpio
> +gpio-watch

Perhaps keep it sorted?

> +++ b/tools/gpio/gpio-watch.c
> @@ -0,0 +1,112 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * gpio-watch - monitor unrequested lines for property changes using the
> + *              character device
> + *
> + * Copyright (C) 2019 BayLibre SAS
> + * Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> + */
> +
> +#include <ctype.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <linux/gpio.h>
> +#include <poll.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <sys/ioctl.h>
> +#include <unistd.h>
> +
> +static bool isnumber(const char *str)
> +{
> +       size_t sz = strlen(str);
> +       int i;
> +
> +       for (i = 0; i < sz; i++) {
> +               if (!isdigit(str[i]))
> +                       return false;
> +       }
> +
> +       return true;
> +}

strtoul() will do the same.

char *p;
unsigned long dummy; // do we need it?
dummy = strtoul(..., &p);
return *p == '\0';

> +int main(int argc, char **argv)
> +{
> +       struct gpioline_info_changed chg;
> +       struct gpioline_info req;
> +       struct pollfd pfd;
> +       int fd, i, j, ret;
> +       char *event;
> +       ssize_t rd;
> +
> +       if (argc < 3)
> +               goto err_usage;
> +
> +       fd = open(argv[1], O_RDWR | O_CLOEXEC);
> +       if (fd < 0) {
> +               perror("unable to open gpiochip");
> +               return EXIT_FAILURE;
> +       }
> +
> +       for (i = 0, j = 2; i < argc - 2; i++, j++) {
> +               if (!isnumber(argv[j]))
> +                       goto err_usage;
> +
> +               memset(&req, 0, sizeof(req));
> +               req.line_offset = atoi(argv[j]);

Oh, why not to call strtoul() directly?

> +
> +               ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req);
> +               if (ret) {
> +                       perror("unable to set up line watch");

Don't you need to unwatch previously added ones?

> +                       return EXIT_FAILURE;
> +               }
> +       }

> +       for (;;) {
> +               ret = poll(&pfd, 1, 5000);
> +               if (ret < 0) {
> +                       perror("error polling the linechanged fd");
> +                       return EXIT_FAILURE;
> +               } else if (ret > 0) {
> +                       memset(&chg, 0, sizeof(chg));

> +                       rd = read(pfd.fd, &chg, sizeof(chg));
> +                       if (rd < 0 || rd != sizeof(chg)) {
> +                               if (rd != sizeof(chg))
> +                                       errno = EIO;
> +
> +                               perror("error reading line change event");
> +                               return EXIT_FAILURE;
> +                       }

Shouldn't we handle the -EINTR?

> +               }
> +       }
> +
> +       return 0;


-- 
With Best Regards,
Andy Shevchenko

  reply index

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-04 15:59 [PATCH v2 10/11] gpiolib: add new ioctl() for monitoring changes in line info Bartosz Golaszewski
2019-12-04 15:59 ` [PATCH v2 11/11] tools: gpio: implement gpio-watch Bartosz Golaszewski
2019-12-04 22:42   ` Andy Shevchenko [this message]
2019-12-05  9:45     ` Bartosz Golaszewski
2019-12-05  9:44   ` Kent Gibson
2019-12-05  9:48     ` Bartosz Golaszewski
2019-12-04 22:34 ` [PATCH v2 10/11] gpiolib: add new ioctl() for monitoring changes in line info Andy Shevchenko
2019-12-05  9:42   ` Bartosz Golaszewski
2019-12-05 10:27     ` Andy Shevchenko
2019-12-05 13:47       ` Bartosz Golaszewski
2019-12-05 17:02         ` Andy Shevchenko
2019-12-05 10:47 ` Kent Gibson
2019-12-05 10:50   ` Bartosz Golaszewski

Reply instructions:

You may reply publically 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='CAHp75VcqYsKUzxGUhn8aHg_u-B=FkqmTU2YS+yyVNfAPu+715w@mail.gmail.com' \
    --to=andy.shevchenko@gmail.com \
    --cc=bgolaszewski@baylibre.com \
    --cc=brgl@bgdev.pl \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=warthog618@gmail.com \
    /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

Linux-GPIO Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-gpio/0 linux-gpio/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-gpio linux-gpio/ https://lore.kernel.org/linux-gpio \
		linux-gpio@vger.kernel.org
	public-inbox-index linux-gpio

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-gpio


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git