chiark / gitweb /
hostside: wip test programs etc. for Joytech Neo S gamepad
[trains.git] / hostside / hiddev-ioctl.c
1 /**/
2
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <inttypes.h>
9 #include <sys/ioctl.h>
10 #include <sys/types.h>
11
12 #include <linux/types.h>
13 #include <linux/hiddev.h>
14
15 #include "common.h"
16
17 #if 0
18 static void dump(const void *pu, int l) {
19   const uint8_t *p= pu;
20   while (l>0) {
21     printf("%02x",*p++);
22     l--;
23   }
24   putchar(' ');
25 }
26 #endif
27
28 static void report_scan(const char *which, int type) {
29   struct hiddev_report_info rinfo;
30   int r, i, j;
31   int fd=0;
32
33   rinfo.report_type = type;
34   rinfo.report_id = HID_REPORT_ID_FIRST;
35
36   for (;;) {
37     r = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
38     if (r && errno==EINVAL) break;
39     if (r) diee("HIDIOCGREPORTINFO");
40
41     printf("%s type=%08"PRIx32" id=%08"PRIx32" num_fields=%08"PRIx32"\n",
42            which, rinfo.report_type, rinfo.report_id, rinfo.num_fields);
43     for (i=0; i < rinfo.num_fields; i++) {
44       struct hiddev_field_info finfo;
45       finfo.report_type= rinfo.report_type;
46       finfo.report_id= rinfo.report_id;
47       finfo.field_index= i;
48       r= ioctl(fd, HIDIOCGFIELDINFO, &finfo);
49       if (r) diee("HIDIOCGFIELDINFO");
50       printf("%s type=%08"PRIx32" id=%08"PRIx32" fi=%08"PRIx32
51              " maxusage=%08"PRIx32" flags=%08"PRIx32
52              " phys=%08"PRIx32"/log=%08"PRIx32"/app=%08"PRIx32
53              " log=%08"PRIx32"..%08"PRIx32
54              " phys=%08"PRIx32"..%08"PRIx32
55              "\n",
56              which, finfo.report_type, finfo.report_id, finfo.field_index,
57              finfo.maxusage, finfo.flags,
58              finfo.physical, finfo.logical, finfo.application,
59              finfo.logical_minimum, finfo.logical_maximum,
60              finfo.physical_minimum, finfo.physical_maximum
61              );
62       for (j=0; j < finfo.maxusage; j++) {
63         struct hiddev_usage_ref uref;
64         uref.report_type= finfo.report_type;
65         uref.report_id= finfo.report_id;
66         uref.field_index= i;
67         uref.usage_index= j;
68         r= ioctl(fd, HIDIOCGUCODE, &uref);
69 //dump(&uref,sizeof(uref));
70         r= ioctl(fd, HIDIOCGUSAGE, &uref);
71 //dump(&uref,sizeof(uref));
72         printf("%s type=%08"PRIx32" id=%08"PRIx32" fi=%08"PRIx32
73                " ui=%08"PRIx32" uc=%08"PRIx32" val=%08"PRIx32"\n",
74                which, uref.report_type, uref.report_id, uref.field_index,
75                uref.usage_index, uref.usage_code, uref.value);
76       }
77     }
78     rinfo.report_id |= HID_REPORT_ID_NEXT;
79   }
80 }
81
82 void die_vprintf_hook(const char *fmt, va_list al) { }
83 void die_hook(void) { }
84 const char *progname= "hiddev-ioctl";
85
86 int main(int argc, char **argv) {
87   int r;
88   if (!argv[0] || !argv[1]) badusage("too few args");
89   const char *act= *++argv;
90   if (!strcmp(act, "scan")) {
91     report_scan("input", HID_REPORT_TYPE_INPUT);
92     report_scan("output", HID_REPORT_TYPE_OUTPUT);
93   } else if (!strcmp(act, "send")) {
94     /* type id field val */
95     if (!argv[1] || !argv[2] || !argv[3] || !argv[4]
96          || !argv[5] || !argv[6] || argv[7])
97       badusage("wrong # args");
98     struct hiddev_usage_ref uref;
99     struct hiddev_report_info rinfo;
100     uref.report_type= strtoul(argv[1],0,0);
101     uref.report_id=   strtoul(argv[2],0,0);
102     uref.field_index= strtoul(argv[3],0,0);
103     uref.usage_index= strtoul(argv[4],0,0);
104     uref.usage_code=  strtoul(argv[5],0,0);
105     uref.value=        strtol(argv[6],0,0);
106     rinfo.report_type= uref.report_type;
107     rinfo.report_id=   uref.report_id;
108     rinfo.num_fields=  0;
109 //    r= ioctl(0, HIDIOCGREPORT, &rinfo);    if (r) diee("HIDIOCGREPORT");
110 //    r= ioctl(0, HIDIOCGUSAGE, &uref);    if (r) diee("HIDIOCGUSAGE");
111 //dump(&uref,sizeof(uref));
112 //    printf("%08"PRIx32"...",uref.value);
113     r= ioctl(0, HIDIOCSUSAGE, &uref);    if (r) diee("HIDIOCSUSAGE");
114     r= ioctl(0, HIDIOCSREPORT, &rinfo);  if (r) diee("HIDIOCSREPORT");
115   } else {
116     badusage("unknown action");
117   }
118   return 0;
119 }