chiark / gitweb /
hostside: more length for bavarian
[trains.git] / hostside / hidraw-ioctl.c
1 /*
2  * usage:
3  *   .../hidraw-ioctl </dev/hidrawN -a
4  *   .../hidraw-ioctl </dev/hidrawN -[drnp]
5  * where -a means all, and the other letters are:
6  *   -d   descriptors (in hex)
7  *   -D   descriptors length (decimal), descriptors (in hex)
8  *   -i   HIDIOCGRAWINFO: bustype, vendor, product (all in hex)
9  *   -n   HIDIOCGRAWNAME
10  *   -p   HIDIOCGRAWPATHPHYS
11  * exit status:
12  *   0      all ok
13  *   2      at least one ioctl on the device failed
14  *   other  some other problem
15  */
16
17 #include <stdio.h>
18 #include <stdint.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25
26 #include <linux/types.h>
27
28 #include "hidraw.h"
29
30 #include "common.h"
31
32 void die_vprintf_hook(const char *fmt, va_list al) { }
33 void die_hook(void) { }
34 const char *progname= "hidraw-ioctl";
35
36 static void prerror(const char *what) {
37   printf("%s: %s\n", what, strerror(errno));
38 }
39
40 #define ERR(s) do{ prerror(s); e=1; goto next_opt; }while(0)
41
42 int main(int argc, char **argv) {
43   static const char allopts[]= "Dinp";
44   int r, opt, e=0;
45   const char *opts;
46
47   if (!*argv || !(opts=*++argv) || *opts++!='-' || *++argv)
48     badusage("need exactly one argument, containing options");
49   if (!strcmp(opts,"a")) opts= allopts;
50
51   while ((opt= *opts++)) {
52     switch (opt) {
53
54     case 'd': case 'D':;
55       int descsz;
56       r= ioctl(0, HIDIOCGRDESCSIZE, &descsz);
57       if (r) ERR("HIDIOCGRDESCSIZE");
58
59       if (opt=='D') printf("%d\n",descsz);
60
61       {
62         struct {
63           struct hidraw_report_descriptor d;
64           unsigned char buf[descsz];
65         } d;
66         d.d.size = descsz;
67         r= ioctl(0, HIDIOCGRDESC, &d);
68         if (r) ERR("HIDIOCGRDESC");
69
70         dumphex(stdout, d.d.value, d.d.size);
71       }
72       putchar('\n');
73       break;
74
75     case 'i':;
76       struct hidraw_devinfo di;
77       r= ioctl(0, HIDIOCGRAWINFO, &di);
78       if (r) ERR("HIDIOCGRAWINFO");
79       printf("%08"PRIx32" %04"PRIx16" %04"PRIx16"\n",
80              di.bustype, di.vendor, di.product);
81       break;
82       
83     case 'n':;
84       unsigned char buf[PATH_MAX];
85       r= ioctl(0, HIDIOCGRAWNAME(PATH_MAX), buf);
86       if (r<0) ERR("HIDIOCGRAWNAME");
87       printf("%d %.*s\n", r, r,buf);
88       break;
89
90     case 'p':
91       r= ioctl(0, HIDIOCGRAWPHYS(PATH_MAX), buf);
92       if (r<0) ERR("HIDIOCGRAWPHYS");
93       printf("%d %.*s\n", r, r,buf);
94       break;
95
96     default:
97       badusage("unknown option");
98     }
99
100   next_opt:;
101     if (ferror(stdout) || fflush(stdout))
102       diee("write/flush stdout");
103   }
104
105   return e;
106 }