From: Ian Jackson Date: Sun, 30 Jan 2011 17:55:50 +0000 (+0000) Subject: hostside: hidraw-ioctl: much improved, now a useful utility X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ijackson/git?a=commitdiff_plain;h=c1c1010050c3e8d513e0c2a3efd88039fc4ed7d1;p=trains.git hostside: hidraw-ioctl: much improved, now a useful utility --- diff --git a/hostside/Makefile b/hostside/Makefile index 64fa270..7429f51 100644 --- a/hostside/Makefile +++ b/hostside/Makefile @@ -39,7 +39,7 @@ topology-dump: topology-dump.o utils.o $(LAYOUT_DATA).o evdev-manip: evdev-manip.o utils.o $(LIBOOP_OBJS) -hidraw-ioctl: hidraw-ioctl.o +hidraw-ioctl: hidraw-ioctl.o utils.o hiddev-ioctl: hiddev-ioctl.o utils.o kdled-ioctl: kdled-ioctl.o diff --git a/hostside/hidraw-ioctl.c b/hostside/hidraw-ioctl.c index 027e953..4da6171 100644 --- a/hostside/hidraw-ioctl.c +++ b/hostside/hidraw-ioctl.c @@ -1,4 +1,18 @@ -/**/ +/* + * usage: + * .../hidraw-ioctl #include @@ -15,41 +29,78 @@ #include "common.h" +void die_vprintf_hook(const char *fmt, va_list al) { } +void die_hook(void) { } +const char *progname= "hidraw-ioctl"; + +static void prerror(const char *what) { + printf("%s: %s\n", what, strerror(errno)); +} + +#define ERR(s) do{ prerror(s); e=1; goto next_opt; }while(0) + int main(int argc, char **argv) { - int r, descsz; - - r= ioctl(0, HIDIOCGRDESCSIZE, &descsz); - if (r) { - perror("HIDIOCGRDESCSIZE"); - } else { - printf("%d\n",descsz); - struct { - struct hidraw_report_descriptor d; - unsigned char buf[descsz]; - } d; - d.d.size = descsz; - r= ioctl(0, HIDIOCGRDESC, &d); - if (r) perror("HIDIOCGRDESC"); - else { dump(d.d.value, d.d.size); putchar('\n'); } - } + static const char allopts[]= "Dinp"; + int r, opt, e=0; + const char *opts; - struct hidraw_devinfo di; - r= ioctl(0, HIDIOCGRAWINFO, &di); - if (r) perror("HIDIOCGRAWINFO"); - else { - printf("%08"PRIx32" %04"PRIx16" %04"PRIx16"\n", - di.bustype, di.vendor, di.product); - } + if (!*argv || !(opts=*++argv) || *opts++!='-' || *++argv) + badusage("need exactly one argument, containing options"); + if (!strcmp(opts,"a")) opts= allopts; + + while ((opt= *opts++)) { + switch (opt) { - unsigned char buf[PATH_MAX]; + case 'd': case 'D':; + int descsz; + r= ioctl(0, HIDIOCGRDESCSIZE, &descsz); + if (r) ERR("HIDIOCGRDESCSIZE"); - r= ioctl(0, HIDIOCGRAWNAME(PATH_MAX), buf); - if (r<0) perror("HIDIOCGRAWNAME"); - else printf("%d %.*s\n", r, r,buf); + if (opt=='D') printf("%d\n",descsz); - r= ioctl(0, HIDIOCGRAWPHYS(PATH_MAX), buf); - if (r<0) perror("HIDIOCGRAWPHYS"); - else printf("%d %.*s\n", r, r,buf); + { + struct { + struct hidraw_report_descriptor d; + unsigned char buf[descsz]; + } d; + d.d.size = descsz; + r= ioctl(0, HIDIOCGRDESC, &d); + if (r) ERR("HIDIOCGRDESC"); + + dumphex(stdout, d.d.value, d.d.size); + } + putchar('\n'); + break; + + case 'i':; + struct hidraw_devinfo di; + r= ioctl(0, HIDIOCGRAWINFO, &di); + if (r) ERR("HIDIOCGRAWINFO"); + printf("%08"PRIx32" %04"PRIx16" %04"PRIx16"\n", + di.bustype, di.vendor, di.product); + break; + + case 'n':; + unsigned char buf[PATH_MAX]; + r= ioctl(0, HIDIOCGRAWNAME(PATH_MAX), buf); + if (r<0) ERR("HIDIOCGRAWNAME"); + printf("%d %.*s\n", r, r,buf); + break; + + case 'p': + r= ioctl(0, HIDIOCGRAWPHYS(PATH_MAX), buf); + if (r<0) ERR("HIDIOCGRAWPHYS"); + printf("%d %.*s\n", r, r,buf); + break; + + default: + badusage("unknown option"); + } + + next_opt:; + if (ferror(stdout) || fflush(stdout)) + diee("write/flush stdout"); + } - return 0; + return e; }