chiark / gitweb /
hostside: utils: new dumphex function, moved from hidraw-ioctl.c
[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 static void report_scan(const char *which, int type) {
18   struct hiddev_report_info rinfo;
19   int r, i, j;
20   int fd=0;
21
22   rinfo.report_type = type;
23   rinfo.report_id = HID_REPORT_ID_FIRST;
24
25   for (;;) {
26     r = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
27     if (r && errno==EINVAL) break;
28     if (r) diee("HIDIOCGREPORTINFO");
29
30     printf("%s type=%08"PRIx32" id=%08"PRIx32" num_fields=%08"PRIx32"\n",
31            which, rinfo.report_type, rinfo.report_id, rinfo.num_fields);
32     for (i=0; i < rinfo.num_fields; i++) {
33       struct hiddev_field_info finfo;
34       finfo.report_type= rinfo.report_type;
35       finfo.report_id= rinfo.report_id;
36       finfo.field_index= i;
37       r= ioctl(fd, HIDIOCGFIELDINFO, &finfo);
38       if (r) diee("HIDIOCGFIELDINFO");
39       printf("%s type=%08"PRIx32" id=%08"PRIx32" fi=%08"PRIx32
40              " maxusage=%08"PRIx32" flags=%08"PRIx32
41              " phys=%08"PRIx32"/log=%08"PRIx32"/app=%08"PRIx32
42              " log=%08"PRIx32"..%08"PRIx32
43              " phys=%08"PRIx32"..%08"PRIx32
44              "\n",
45              which, finfo.report_type, finfo.report_id, finfo.field_index,
46              finfo.maxusage, finfo.flags,
47              finfo.physical, finfo.logical, finfo.application,
48              finfo.logical_minimum, finfo.logical_maximum,
49              finfo.physical_minimum, finfo.physical_maximum
50              );
51       for (j=0; j < finfo.maxusage; j++) {
52         struct hiddev_usage_ref uref;
53         uref.report_type= finfo.report_type;
54         uref.report_id= finfo.report_id;
55         uref.field_index= i;
56         uref.usage_index= j;
57         r= ioctl(fd, HIDIOCGUCODE, &uref);
58 //dump(&uref,sizeof(uref));
59         r= ioctl(fd, HIDIOCGUSAGE, &uref);
60 //dump(&uref,sizeof(uref));
61         printf("%s type=%08"PRIx32" id=%08"PRIx32" fi=%08"PRIx32
62                " ui=%08"PRIx32" uc=%08"PRIx32" val=%08"PRIx32"\n",
63                which, uref.report_type, uref.report_id, uref.field_index,
64                uref.usage_index, uref.usage_code, uref.value);
65       }
66     }
67     rinfo.report_id |= HID_REPORT_ID_NEXT;
68   }
69 }
70
71 void die_vprintf_hook(const char *fmt, va_list al) { }
72 void die_hook(void) { }
73 const char *progname= "hiddev-ioctl";
74
75 int main(int argc, char **argv) {
76   int r;
77   if (!argv[0] || !argv[1]) badusage("too few args");
78   const char *act= *++argv;
79   if (!strcmp(act, "scan")) {
80     report_scan("input", HID_REPORT_TYPE_INPUT);
81     report_scan("output", HID_REPORT_TYPE_OUTPUT);
82   } else if (!strcmp(act, "send")) {
83     /* type id field val */
84     if (!argv[1] || !argv[2] || !argv[3] || !argv[4]
85          || !argv[5] || !argv[6] || argv[7])
86       badusage("wrong # args");
87     struct hiddev_usage_ref uref;
88     struct hiddev_report_info rinfo;
89     uref.report_type= strtoul(argv[1],0,0);
90     uref.report_id=   strtoul(argv[2],0,0);
91     uref.field_index= strtoul(argv[3],0,0);
92     uref.usage_index= strtoul(argv[4],0,0);
93     uref.usage_code=  strtoul(argv[5],0,0);
94     uref.value=        strtol(argv[6],0,0);
95     rinfo.report_type= uref.report_type;
96     rinfo.report_id=   uref.report_id;
97     rinfo.num_fields=  0;
98 //    r= ioctl(0, HIDIOCGREPORT, &rinfo);    if (r) diee("HIDIOCGREPORT");
99 //    r= ioctl(0, HIDIOCGUSAGE, &uref);    if (r) diee("HIDIOCGUSAGE");
100 //dump(&uref,sizeof(uref));
101 //    printf("%08"PRIx32"...",uref.value);
102     r= ioctl(0, HIDIOCSUSAGE, &uref);    if (r) diee("HIDIOCSUSAGE");
103     r= ioctl(0, HIDIOCSREPORT, &rinfo);  if (r) diee("HIDIOCSREPORT");
104   } else {
105     badusage("unknown action");
106   }
107   return 0;
108 }