-
Notifications
You must be signed in to change notification settings - Fork 0
/
hidraw.c
328 lines (295 loc) · 7.87 KB
/
hidraw.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Displays a more human-readable interpretation of the USB data payload
* for Logitech Unifying Receiver.
*
* Example usage: read-dev-usbmon /dev/usbmon0 | hidraw
*
* Copyright (C) 2013 Peter Wu <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
typedef unsigned char u8;
#define SHORT_MSG 0x10
#define SHORT_MSG_LEN 7
#define DJ_SHORT 0x20
#define DJ_SHORT_LEN 15
#define DJ_LONG 0x21
#define DJ_LONG_LEN 32
#define LONG_MSG 0x11
#define LONG_MSG_LEN 20
struct payload_short {
u8 address;
u8 value[3];
};
struct payload_long {
u8 address;
u8 str[16];
};
struct report {
u8 report_id;
u8 device_index;
u8 sub_id;
union {
struct payload_long l;
struct payload_short s;
};
} __attribute__((__packed__));
/* types for HID++ report IDs 0x10 and 0x11 */
static const char * report_types[0x100] = {
[0x00] = "_HIDPP20", // fake type
// 0x00 - 0x3F HID reports
[0x01] = "KEYBOARD",
[0x02] = "MOUSE",
[0x03] = "CONSUMER_CONTROL",
[0x04] = "SYSTEM_CONTROL",
[0x08] = "MEDIA_CENTER",
[0x0E] = "LEDS",
// 0x40 - 0x7F enumerator notifications
[0x40] = "NOTIF_DEVICE_UNPAIRED",
[0x41] = "NOTIF_DEVICE_PAIRED",
[0x42] = "NOTIF_CONNECTION_STATUS",
[0x4A] = "NOTIF_RECV_LOCK_CHANGED",
[0x4B] = "?NOTIF_PAIR_ACCEPTED",
[0x7F] = "NOTIF_ERROR",
// 0x80 - 0xFF enumerator commands; Register Access
[0x80] = "SET_REG", // was CMD_SWITCH
[0x81] = "GET_REG", // was CMD_GET_PAIRED_DEVICES
[0x82] = "SET_LONG_REG",
[0x83] = "GET_LONG_REG",
[0x8F] = "_ERROR_MSG",
[0xFF] = "_HIDPP20_ERROR_MSG",
};
/* types for DJ report IDS 0x20 and 0x21 */
static const char *dj_report_types[0x100] = {
/* 0x00 - 0x3F: RF reports */
[0x01] = "KEYBOARD",
[0x02] = "MOUSE",
[0x03] = "CONSUMER_CONTROL",
[0x04] = "SYSTEM_CONTROL",
[0x08] = "MEDIA_CENTER",
[0x0E] = "KEYBOARD_LEDS",
/* 0x40 - 0x7F: DJ notifications */
[0x40] = "NOTIF_DEVICE_UNPAIRED",
[0x41] = "NOTIF_DEVICE_PAIRED",
[0x42] = "NOTIF_CONNECTION_STATUS",
[0x7F] = "NOTIF_ERROR",
/* 0x80 - 0xFF: DJ commands */
[0x80] = "CMD_SWITCH_N_KEEPALIVE",
[0x81] = "CMD_GET_PAIRED_DEVICES"
};
static const char * error_messages[0x100] = {
// error messages for type=8F (ERROR_MSG)
[0x00] = "SUCCESS",
[0x01] = "INVALID_SUBID",
[0x02] = "INVALID_ADDRESS",
[0x03] = "INVALID_VALUE",
[0x04] = "CONNECT_FAIL",
[0x05] = "TOO_MANY_DEVICES",
[0x06] = "ALREADY_EXISTS",
[0x07] = "BUSY",
[0x08] = "UNKNOWN_DEVICE",
[0x09] = "RESOURCE_ERROR",
[0x0A] = "REQUEST_UNAVAILABLE",
[0x0B] = "INVALID_PARAM_VALUE",
[0x0C] = "WRONG_PIN_CODE",
};
// I don't know the upper bound, perhaps 0x10 is enough
static const char * error_messages_hidpp20[0x100] = {
[0x00] = "NoError",
[0x01] = "Unknown",
[0x02] = "InvalidArgument",
[0x03] = "OutOfRange",
[0x04] = "HWError",
[0x05] = "Logitech internal",
[0x06] = "INVALID_FEATURE_INDEX",
[0x07] = "INVALID_FUNCTION_ID",
[0x08] = "Busy",
[0x09] = "Unsupported",
};
// everything with a '?' is guessed
static const char * registers[0x100] = {
[0x00] = "ENABLED_NOTIFS",
[0x01] = "KBD_HAND_DETECT?",
[0x02] = "CONNECTION_STATE",
[0x07] = "BATTERY?",
[0x09] = "FN_KEY_SWAP?",
[0x17] = "ILLUMINATION_INFO?",
[0xb2] = "DEVICE_PAIRING",
[0xb3] = "DEVICE_ACTIVITY",
[0xb5] = "PAIRING_INFO",
[0xf1] = "VERSION_INFO?",
};
bool report_type_is_hidpp(u8 report_id) {
return report_id == SHORT_MSG || report_id == LONG_MSG;
}
bool report_type_is_dj(u8 report_id) {
return report_id == DJ_SHORT || report_id == DJ_LONG;
}
const char * report_type_str(u8 report_id, u8 type) {
const char *str = NULL;
if (report_type_is_hidpp(report_id))
str = report_types[type];
else if (report_type_is_dj(report_id))
str = dj_report_types[type];
return str ? str : "";
}
const char * device_type_str(u8 type) {
switch (type) {
case 0x01: return "DEV1";
case 0x02: return "DEV2";
case 0x03: return "DEV3";
case 0x04: return "DEV4";
case 0x05: return "DEV5";
case 0x06: return "DEV6";
case 0xFF: return "RECV";
default: return "";
}
}
const char *error_str(u8 er) {
const char * str = error_messages[er];
return str ? str : "";
}
const char *error_str_hidpp20(u8 er) {
const char * str = error_messages_hidpp20[er];
return str ? str : "";
}
const char *register_str(u8 reg) {
const char * str = registers[reg];
return str ? str : "";
}
void process_msg_payload(struct report *r, u8 data_len) {
u8 pos, i;
u8 * bytes = (u8 *) &r->s;
pos = 0; // nothing has been processed
if (report_type_is_hidpp(r->report_id))
switch (r->sub_id) {
case 0x00: // assume HID++ 2.0 request/response for feature IRoot
if (data_len == 4 || data_len == 17) {
printf("func=%X ", bytes[0] >> 4);
printf("swId=%X ", bytes[0] & 0xF);
pos = 1;
}
break;
case 0xFF: // assume HID++ 2.0 error
if (data_len == 17) {
printf("feat=%X ", bytes[0]);
printf("func=%X ", bytes[1] >> 4);
printf("swId=%X ", bytes[1] & 0xF);
printf("err=%02X %s ", bytes[2], error_str_hidpp20(bytes[2]));
pos = 3;
}
break;
case 0x8F: // error
// TODO: length check
printf("SubID=%02X %s ", bytes[0], report_type_str(r->report_id, bytes[0]));
printf("reg=%02X %s ", bytes[1], register_str(bytes[1]));
printf("err=%02X %s ", bytes[2], error_str(bytes[2]));
pos = 4; // everything is processed
break;
case 0x80:
case 0x81:
case 0x82: /* long */
case 0x83: /* long */
printf("reg=%02X %s ", bytes[0], register_str(bytes[0]));
pos = 1;
break;
}
if (pos < data_len) {
printf("params=");
//printf("params(len=%02X)=", data_len);
}
for (i = 0; pos < data_len; pos++, i++) {
printf("%02X ", bytes[pos]);
if (i % 4 == 3 && pos + 1 < data_len) {
putchar(' ');
}
}
}
void process_msg(struct report *report, ssize_t size) {
const char * report_type;
switch (report->report_id) {
case SHORT_MSG:
report_type = "short";
if (size != SHORT_MSG_LEN) {
fprintf(stderr, "Invalid short msg len %zi\n", size);
return;
}
break;
case LONG_MSG:
report_type = "long";
if (size != LONG_MSG_LEN) {
fprintf(stderr, "Invalid long msg len %zi\n", size);
return;
}
break;
case DJ_SHORT:
report_type = "dj_s";
if (size != DJ_SHORT_LEN) {
fprintf(stderr, "Invalid DJ short msg len %zi\n", size);
return;
}
break;
case DJ_LONG:
report_type = "dj_l";
if (size != DJ_LONG_LEN) {
fprintf(stderr, "Invalid DJ long msg len %zi\n", size);
return;
}
break;
default:
report_type = "unkn";
//fprintf(stderr, "Unknown report ID %02x, len=%zi\n", report->report_id, size);
if (size < 3) {
return;
}
break;
}
printf("report_id=%02X %-5s ", report->report_id, report_type);
printf("device=%02X %-4s ", report->device_index,
device_type_str(report->device_index));
printf("type=%02X %-23s ", report->sub_id,
report_type_str(report->report_id, report->sub_id));
if (size > 3) {
process_msg_payload(report, size - 3);
}
putchar('\n');
}
#ifndef NO_MAIN
int main(int argc, char ** argv) {
int fd = STDIN_FILENO;
ssize_t r;
struct report report;
if (argc >= 2 && (fd = open(argv[1], O_RDONLY)) < 0) {
perror(argv[1]);
return 1;
}
do {
memset(&report, 0xCC, sizeof report); // for debugging purposes
r = read(fd, &report, sizeof report);
if (r > 0) {
process_msg(&report, r);
}
} while (r >= 0);
if (r < 0) {
perror("read");
}
close(fd);
return 0;
}
#endif /* ! NO_MAIN */