1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/hidraw.h>
/* ------------------------------------------------------------------ */
static const char *tag_map[4][16] = {
{
/* main */
[ 8 ] = "input",
[ 9 ] = "output",
[ 11 ] = "feature",
[ 10 ] = "collection",
[ 12 ] = "end",
},{
/* global */
[ 0 ] = "usage page",
[ 1 ] = "logical min",
[ 2 ] = "logical max",
[ 3 ] = "physical min",
[ 4 ] = "physical max",
[ 5 ] = "unit exponent",
[ 6 ] = "unit",
[ 7 ] = "report size",
[ 8 ] = "report id",
[ 9 ] = "report count",
[ 10 ] = "push",
[ 11 ] = "pop",
},{
/* local */
[ 0 ] = "usage",
[ 1 ] = "usage min",
[ 2 ] = "usage max",
[ 3 ] = "desig index",
[ 4 ] = "desig min",
[ 5 ] = "desig max",
[ 7 ] = "string index",
[ 8 ] = "string min",
[ 9 ] = "string max",
[ 10 ] = "delimiter",
},{
/* reserved */
}
};
static const char *type_map[4] = {
"main", "global", "local", "reserved"
};
static const int size_map[4] = {
0, 1, 2, 4
};
static void print_report(unsigned char *desc, int descsize)
{
unsigned data;
int tag, type, size, bytes;
int i, off = 0;
while (off < descsize) {
if (desc[off] == 0xfe) {
printf("TODO: long item\n");
return;
} else {
tag = (desc[off] >> 4) & 0x0f;
type = (desc[off] >> 2) & 0x03;
size = (desc[off] >> 0) & 0x03;
printf(" %-16s %-6s %d --",
tag_map[type][tag] ?: "?",
type_map[type],
size_map[size]);
data = 0;
for (i = 0; i < size_map[size]; i++)
data |= desc[off + 1 + i] << (8 * i);
printf(" %8x -- ", data);
bytes = size_map[size] + 1;
for (i = 0; i < bytes && off + i < descsize; i++)
printf(" %02x", desc[off+i]);
printf("\n");
off += bytes;
}
}
}
static int usage(char *prog, int error)
{
fprintf(error ? stderr : stdout,
"usage: %s"
" [ -h ]\n",
prog);
exit(error);
}
int main(int argc, char *argv[])
{
struct hidraw_report_descriptor desc;
int i, c, fd, rc, size;
char devname[32];
for (;;) {
if (-1 == (c = getopt(argc, argv, "hv")))
break;
switch (c) {
case 'h':
usage(argv[0], 0);
default:
usage(argv[0], 1);
}
}
for (i = 0; i < HIDRAW_MAX_DEVICES; i++) {
snprintf(devname, sizeof(devname), "/dev/hidraw%d", i);
fd = open(devname, O_RDWR);
if (fd < 0) {
if (errno != ENOENT) {
fprintf(stderr, "%s: %s\n", devname, strerror(errno));
continue;
} else {
break;
}
}
rc = ioctl(fd, HIDIOCGRDESCSIZE, &size);
desc.size = size;
rc = ioctl(fd, HIDIOCGRDESC, &desc);
if (rc < 0) {
fprintf(stderr, "%s: HIDIOCGRDESC: %s\n", devname, strerror(errno));
continue;
}
close(fd);
printf("%s (%d)\n", devname, size);
print_report(desc.value, size);
}
return 0;
}
|