-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuclogic-decode.c
361 lines (316 loc) · 9.86 KB
/
uclogic-decode.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright (C) 2013 Nikolai Kondrashov
*
* This file is part of uclogic-tools.
*
* Uclogic-tools 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 2 of the License, or
* (at your option) any later version.
*
* Uclogic-tools 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 uclogic-tools; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Nikolai Kondrashov <spbnick@gmail.com>
*/
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#define GENERIC_ERROR(_fmt, _args...) \
fprintf(stderr, _fmt "\n", ##_args)
#define GENERIC_FAILURE(_fmt, _args...) \
GENERIC_ERROR("Failed to " _fmt, ##_args)
#define ERROR_CLEANUP(_fmt, _args...) \
do { \
GENERIC_ERROR(_fmt, ##_args); \
goto cleanup; \
} while (0)
#define FAILURE_CLEANUP(_fmt, _args...) \
do { \
GENERIC_FAILURE(_fmt, ##_args); \
goto cleanup; \
} while (0)
/** Format string for a field header */
#define FIELD_HEAD_FMT " | %22s: "
/** Format string for a field header with hex index */
#define IDX_FIELD_HEAD_FMT " %02x | %22s: "
/** Format string for a sub-field header */
#define SUB_FIELD_HEAD_FMT " | %30s: "
/** Blank output line string */
#define BLANK_LINE_STR " |"
struct decoder {
/** Decoder ID */
uint8_t id;
/**
* Decoder function.
*
* @param idx Index of the decoded data (e.g. descriptor number).
* @param ptr Pointer to the decoded data.
* @param len Length of the decoded data.
*
* @return Zero if decoded successfully, non-zero otherwise.
*/
int (*decode)(uint8_t idx, const uint8_t *ptr, int len);
};
/**
* Print a 16-bit unsigned sub-field.
* Assumes "ptr" and "len" variables are in scope.
*
* @param _offset Field offset from ptr, bytes.
* @param _label Field label string literal.
*/
#define PRINT_SUB_FIELD_U16(_offset, _label) \
do { \
printf(SUB_FIELD_HEAD_FMT, _label); \
if ((_offset) + 1 < len) { \
printf("%u\n", \
ptr[_offset] | \
((unsigned int)ptr[(_offset) + 1] << 8)); \
} else { \
printf("N/A\n"); \
} \
} while (0)
/**
* Print a 24-bit unsigned sub-field.
* Assumes "ptr" and "len" variables are in scope.
*
* @param _offset Field offset from ptr, bytes.
* @param _label Field label string literal.
*/
#define PRINT_SUB_FIELD_U24(_offset, _label) \
do { \
printf(SUB_FIELD_HEAD_FMT, _label); \
if ((_offset) + 2 < len) { \
printf("%u\n", \
ptr[_offset] | \
((unsigned int)ptr[(_offset) + 1] << 8) | \
((unsigned int)ptr[(_offset) + 2] << 16)); \
} else { \
printf("N/A\n"); \
} \
} while (0)
static void
print_unicode(const uint8_t *ptr, int len)
{
uint16_t c;
for (; len > 1; ptr += 2, len -= 2) {
c = ptr[0] | (ptr[1] << 8);
putchar((c >= 0x20 && c <= 0x7E) ? c : '?');
}
}
static void
print_field_unicode(const char *name, const uint8_t *ptr, int len)
{
printf(FIELD_HEAD_FMT, name);
print_unicode(ptr, len);
putchar('\n');
}
static void
print_idx_field_unicode(uint8_t idx, const char *name,
const uint8_t *ptr, int len)
{
printf(IDX_FIELD_HEAD_FMT, idx, name);
print_unicode(ptr, len);
putchar('\n');
}
static int
decode_params1(uint8_t idx, const uint8_t *ptr, int len)
{
print_idx_field_unicode(idx, "Params block #1", ptr + 2, len - 2);
PRINT_SUB_FIELD_U16(2, "Max X");
PRINT_SUB_FIELD_U16(4, "Max Y");
PRINT_SUB_FIELD_U16(8, "Max pressure");
PRINT_SUB_FIELD_U16(10, "Resolution");
puts(BLANK_LINE_STR);
return 0;
}
static int
decode_internal_model(uint8_t idx, const uint8_t *ptr, int len)
{
print_idx_field_unicode(idx, "Internal model", ptr + 2, len - 2);
return 0;
}
static int
decode_buttons_status(uint8_t idx, const uint8_t *ptr, int len)
{
print_idx_field_unicode(idx, "Buttons status", ptr + 2, len - 2);
return 0;
}
static int
decode_params2(uint8_t idx, const uint8_t *ptr, int len)
{
print_idx_field_unicode(idx, "Params block #2", ptr + 2, len - 2);
PRINT_SUB_FIELD_U24(2, "Max X");
PRINT_SUB_FIELD_U24(5, "Max Y");
PRINT_SUB_FIELD_U16(8, "Max pressure");
PRINT_SUB_FIELD_U16(10, "Resolution");
puts(BLANK_LINE_STR);
return 0;
}
static int
decode_firmware_version(uint8_t idx, const uint8_t *ptr, int len)
{
print_idx_field_unicode(idx, "Firmware version", ptr + 2, len - 2);
return 0;
}
static int
decode_internal_manufacturer(uint8_t idx, const uint8_t *ptr, int len)
{
print_idx_field_unicode(idx, "Internal manufacturer", ptr + 2, len - 2);
return 0;
}
/* List of string descriptor decoders */
static const struct decoder desc_list[] = {
{0x64, decode_params1},
{0x79, decode_internal_model},
{0x7b, decode_buttons_status},
{0xc8, decode_params2},
{0xc9, decode_firmware_version},
{0xca, decode_internal_manufacturer},
{0x00, NULL}
};
static int
decode_desc(uint8_t idx, const uint8_t *buf, int len)
{
const struct decoder *d;
(void)idx;
if (len == 0) {
GENERIC_ERROR("String descriptor without index");
return 1;
}
for (d = desc_list; d->decode != NULL; d++) {
if (d->id == *buf)
return d->decode(d->id, buf + 1, len - 1);
}
return 0;
}
static int
decode_manufacturer(uint8_t idx, const uint8_t *buf, int len)
{
(void)idx;
print_field_unicode("Manufacturer", buf, len);
return 0;
}
static int
decode_product(uint8_t idx, const uint8_t *buf, int len)
{
(void)idx;
print_field_unicode("Product", buf, len);
return 0;
}
/* List of chunk decoders */
static const struct decoder chunk_list[] = {
{'M', decode_manufacturer},
{'P', decode_product},
{'S', decode_desc},
{'\0', NULL}
};
static int
decode_chunk(const uint8_t *buf, int len)
{
const struct decoder *d;
assert(len > 0);
for (d = chunk_list; d->decode != NULL; d++) {
if (d->id == *buf)
return d->decode(d->id, buf + 1, len - 1);
}
return 0;
}
static int
decode(FILE *input)
{
int result = 1;
char *word = NULL;
char *end;
/*
* Chunk type byte +
* string descriptor index byte +
* maximum descriptor length
*/
uint8_t buf[258];
uint8_t *p = buf;
unsigned long n;
assert(sizeof(buf) > 0);
/* For each "word" (a run of non-whitespace characters) on stdin */
while (fscanf(input, "%ms", &word) == 1) {
/* If it's a single character chunk type */
if (word[0] != '\0' && !isxdigit(word[0]) && word[1] == '\0') {
/* If we read a chunk before and it is failing decoding */
if (p != buf && decode_chunk(buf, p - buf) != 0)
FAILURE_CLEANUP("decode chunk");
/* Start a new chunk */
p = buf;
n = word[0];
/* Otherwise it is some other word */
} else {
/* If there was nothing else for this chunk (i.e. no type) */
if (p == buf)
ERROR_CLEANUP("Expecting chunk type indicator");
/* Try to decode the word as a hex byte */
errno = 0;
n = strtoul(word, &end, 16);
if (*end != '\0' || errno != 0 || n > UINT8_MAX)
ERROR_CLEANUP("Invalid byte \"%s\"", word);
/* If there's no space in the buffer */
if (p >= buf + sizeof(buf))
ERROR_CLEANUP("Descriptor too long");
}
/* Write the byte into the buffer */
*p++ = n;
/* Discard the read word */
free(word);
word = NULL;
};
if (p != buf && decode_chunk(buf, p - buf) != 0)
FAILURE_CLEANUP("decode chunk");
result = 0;
cleanup:
free(word);
return result;
}
static void
usage(FILE *file, const char *name)
{
fprintf(file,
"Usage: %s [PROBE_OUTPUT]\n"
"Decode a UC-Logic tablet probe dump.\n"
"\n",
name);
}
int
main(int argc, char **argv)
{
const char *name;
FILE *input;
name = rindex(argv[0], '/');
if (name == NULL)
name = argv[0];
else
name++;
if (argc > 2) {
fprintf(stderr, "Invalid number of arguments\n");
usage(stderr, name);
exit(1);
} else if (argc > 1) {
input = fopen(argv[1], "r");
if (input == NULL) {
fprintf(stderr, "Failed opening %s: %s\n", argv[1],
strerror(errno));
}
} else {
input = stdin;
}
setlinebuf(stdout);
return decode(input);
}