Rewrite parts of gpioctl(8) to use the gpio(3) library.

This commit is contained in:
Rui Paulo 2014-12-02 06:11:32 +00:00
parent 2dfcd0ae9d
commit 244c371d25
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=275395
2 changed files with 50 additions and 61 deletions

View File

@ -3,4 +3,9 @@
PROG= gpioctl PROG= gpioctl
MAN= gpioctl.8 MAN= gpioctl.8
CFLAGS+= -I${.CURDIR}/../../lib/libgpio
DPADD+= ${LIBGPIO}
LDADD+= -lgpio
.include <bsd.prog.mk> .include <bsd.prog.mk>

View File

@ -1,5 +1,6 @@
/*- /*-
* Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org> * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
* Copyright (c) 2014, Rui Paulo <rpaulo@FreeBSD.org>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -37,7 +38,7 @@ __FBSDID("$FreeBSD$");
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/gpio.h> #include <libgpio.h>
struct flag_desc { struct flag_desc {
const char *name; const char *name;
@ -100,7 +101,7 @@ str2cap(const char *str)
/* /*
* Our handmade function for converting string to number * Our handmade function for converting string to number
*/ */
static int static int
str2int(const char *s, int *ok) str2int(const char *s, int *ok)
{ {
char *endptr; char *endptr;
@ -132,44 +133,35 @@ print_caps(int caps)
} }
static void static void
dump_pins(int fd, int verbose) dump_pins(gpio_handle_t handle, int verbose)
{ {
int i, maxpin; int i, maxpin, pinv;
struct gpio_pin pin; gpio_config_t *cfgs;
struct gpio_req req; gpio_config_t *pin;
if (ioctl(fd, GPIOMAXPIN, &maxpin) < 0) { maxpin = gpio_pin_list(handle, &cfgs);
perror("ioctl(GPIOMAXPIN)"); if (maxpin < 0) {
perror("gpio_pin_list");
exit(1); exit(1);
} }
for (i = 0; i <= maxpin; i++) { for (i = 0; i <= maxpin; i++) {
pin.gp_pin = i; pin = cfgs + i;
if (ioctl(fd, GPIOGETCONFIG, &pin) < 0) pinv = gpio_pin_get(handle, pin->g_pin);
/* For some reason this pin is inaccessible */ printf("pin %02d:\t%d\t%s", pin->g_pin, pinv,
continue; pin->g_name);
req.gp_pin = i; print_caps(pin->g_flags);
if (ioctl(fd, GPIOGET, &req) < 0) {
/* Now, that's wrong */
perror("ioctl(GPIOGET)");
exit(1);
}
printf("pin %02d:\t%d\t%s", pin.gp_pin, req.gp_value,
pin.gp_name);
print_caps(pin.gp_flags);
if (verbose) { if (verbose) {
printf(", caps:"); printf(", caps:");
print_caps(pin.gp_caps); print_caps(pin->g_caps);
} }
printf("\n"); printf("\n");
} }
} }
static void static void
fail(const char *fmt, ...) fail(const char *fmt, ...)
{ {
va_list ap; va_list ap;
@ -180,15 +172,14 @@ fail(const char *fmt, ...)
exit(1); exit(1);
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int i; int i;
struct gpio_pin pin; gpio_config_t pin;
struct gpio_req req; gpio_handle_t handle;
char defctlfile[] = _PATH_DEVGPIOC "0";
char *ctlfile = NULL; char *ctlfile = NULL;
int pinn, pinv, fd, ch; int pinn, pinv, ch;
int flags, flag, ok; int flags, flag, ok;
int config, toggle, verbose, list; int config, toggle, verbose, list;
@ -228,17 +219,17 @@ main(int argc, char **argv)
printf("%d/%s\n", i, argv[i]); printf("%d/%s\n", i, argv[i]);
if (ctlfile == NULL) if (ctlfile == NULL)
ctlfile = defctlfile; handle = gpio_open(0);
else
fd = open(ctlfile, O_RDONLY); handle = gpio_open_device(ctlfile);
if (fd < 0) { if (handle == GPIO_INVALID_HANDLE) {
perror("open"); perror("gpio_open");
exit(1); exit(1);
} }
if (list) { if (list) {
dump_pins(fd, verbose); dump_pins(handle, verbose);
close(fd); gpio_close(handle);
exit(0); exit(0);
} }
@ -246,19 +237,16 @@ main(int argc, char **argv)
/* /*
* -t pin assumes no additional arguments * -t pin assumes no additional arguments
*/ */
if(argc > 0) { if (argc > 0) {
usage(); usage();
exit(1); exit(1);
} }
if (gpio_pin_toggle(handle, pinn) < 0) {
req.gp_pin = pinn; perror("gpio_pin_toggle");
if (ioctl(fd, GPIOTOGGLE, &req) < 0) {
perror("ioctl(GPIOTOGGLE)");
exit(1); exit(1);
} }
gpio_close(handle);
close(fd); exit(0);
exit (0);
} }
if (config) { if (config) {
@ -269,14 +257,12 @@ main(int argc, char **argv)
fail("Invalid flag: %s\n", argv[i]); fail("Invalid flag: %s\n", argv[i]);
flags |= flag; flags |= flag;
} }
pin.g_pin = pinn;
pin.gp_pin = pinn; pin.g_flags = flags;
pin.gp_flags = flags; if (gpio_pin_set_flags(handle, &pin) < 0) {
if (ioctl(fd, GPIOSETCONFIG, &pin) < 0) { perror("gpio_pin_set_flags");
perror("ioctl(GPIOSETCONFIG)");
exit(1); exit(1);
} }
exit(0); exit(0);
} }
@ -296,13 +282,13 @@ main(int argc, char **argv)
* Read pin value * Read pin value
*/ */
if (argc == 1) { if (argc == 1) {
req.gp_pin = pinn; pinv = gpio_pin_get(handle, pinn);
if (ioctl(fd, GPIOGET, &req) < 0) { if (pinv < 0) {
perror("ioctl(GPIOGET)"); perror("gpio_pin_get");
exit(1); exit(1);
} }
printf("%d\n", req.gp_value); printf("%d\n", pinv);
exit (0); exit(0);
} }
/* Is it valid number (0 or 1) ? */ /* Is it valid number (0 or 1) ? */
@ -313,13 +299,11 @@ main(int argc, char **argv)
/* /*
* Set pin value * Set pin value
*/ */
req.gp_pin = pinn; if (gpio_pin_set(handle, pinn, pinv) < 0) {
req.gp_value = pinv; perror("gpio_pin_set");
if (ioctl(fd, GPIOSET, &req) < 0) {
perror("ioctl(GPIOSET)");
exit(1); exit(1);
} }
close(fd); gpio_close(handle);
exit(0); exit(0);
} }