| 1 |
#include "hidparse.h" |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <string.h> |
| 5 |
#include <assert.h> |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
static uint8_t size_to_bytes(uint8_t sz) |
| 9 |
{ |
| 10 |
switch (sz) |
| 11 |
{ |
| 12 |
case 0: return 0; |
| 13 |
case 1: return 1; |
| 14 |
case 2: return 2; |
| 15 |
case 3: return 4; |
| 16 |
default: return 0; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
static int32_t read_int(uint8_t sz, uint8_t* raw) |
| 21 |
{ |
| 22 |
switch (sz) |
| 23 |
{ |
| 24 |
case 1: return *((int8_t*) raw); |
| 25 |
case 2: return *((int16_t*) raw); |
| 26 |
case 3: return *((int32_t*) raw); |
| 27 |
} |
| 28 |
|
| 29 |
assert(0); |
| 30 |
return 0; |
| 31 |
} |
| 32 |
|
| 33 |
static uint32_t read_uint(uint8_t sz, uint8_t* raw) |
| 34 |
{ |
| 35 |
switch (sz) |
| 36 |
{ |
| 37 |
case 1: return *raw; |
| 38 |
case 2: return *((uint16_t*) raw); |
| 39 |
case 3: return *((uint32_t*) raw); |
| 40 |
} |
| 41 |
|
| 42 |
assert(0); |
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
const uint8_t HID_LONG_ITEM = 0xFE; |
| 48 |
|
| 49 |
|
| 50 |
#define HID_COLLECTION_ITEM 0xA |
| 51 |
#define HID_END_COLLECTION_ITEM 0xC |
| 52 |
#define HID_INPUT_ITEM 0x8 |
| 53 |
#define HID_OUTPUT_ITEM 0x9 |
| 54 |
#define HID_FEATURE_ITEM 0xB |
| 55 |
|
| 56 |
#define HID_USAGE_PAGE_ITEM 0x0 |
| 57 |
#define HID_LOGICAL_MINIMUM_ITEM 0x1 |
| 58 |
#define HID_LOGICAL_MAXIMUM_ITEM 0x2 |
| 59 |
#define HID_PHYSICAL_MINIMUM_ITEM 0x3 |
| 60 |
#define HID_PHYSICAL_MAXIMUM_ITEM 0x4 |
| 61 |
#define HID_EXPONENT_ITEM 0x5 |
| 62 |
#define HID_UNIT_ITEM 0x6 |
| 63 |
#define HID_REPORT_SIZE_ITEM 0x7 |
| 64 |
#define HID_REPORT_ID_ITEM 0x8 |
| 65 |
#define HID_REPORT_COUNT_ITEM 0x9 |
| 66 |
#define HID_PUSH_ITEM 0xA |
| 67 |
#define HID_POP_ITEM 0xB |
| 68 |
|
| 69 |
|
| 70 |
#define HID_USAGE_ITEM 0x0 |
| 71 |
#define HID_USAGE_MINIMUM_ITEM 0x1 |
| 72 |
#define HID_USAGE_MAXIMUM_ITEM 0x2 |
| 73 |
#define HID_DESIGNATOR_INDEX_ITEM 0x3 |
| 74 |
#define HID_DESIGNATOR_MINIMUM_ITEM 0x4 |
| 75 |
#define HID_DESIGNATOR_MAXIMUM_ITEM 0x5 |
| 76 |
#define HID_STRING_INDEX_ITEM 0x7 |
| 77 |
#define HID_STRING_MINIMUM_ITEM 0x8 |
| 78 |
#define HID_STRING_MAXIMUM_ITEM 0x9 |
| 79 |
#define HID_DELIMITER_ITEM 0xA |
| 80 |
|
| 81 |
#define MAX_ITEM_STACK 4 |
| 82 |
|
| 83 |
hid_item item_stack[MAX_ITEM_STACK]; |
| 84 |
uint8_t item_stack_size = 0; |
| 85 |
|
| 86 |
uint32_t usage_array[16]; |
| 87 |
uint8_t usage_count; |
| 88 |
|
| 89 |
void init_local_data(); |
| 90 |
void clear_local_data(); |
| 91 |
|
| 92 |
void set_local_minimum(uint8_t tag, uint32_t data); |
| 93 |
void set_local_maximum(uint8_t tag, uint32_t data); |
| 94 |
|
| 95 |
void push_local_item(uint8_t tag, uint32_t data); |
| 96 |
|
| 97 |
uint32_t pop_local_item(uint8_t tag); |
| 98 |
|
| 99 |
hid_item* build_item(uint8_t tag, uint8_t flags, hid_item* global_state) |
| 100 |
{ |
| 101 |
hid_item* item = (hid_item*) calloc(sizeof(hid_item), 1); |
| 102 |
if (item == NULL) |
| 103 |
return NULL; |
| 104 |
|
| 105 |
item->flags = flags; |
| 106 |
item->usage = pop_local_item(HID_USAGE_ITEM); |
| 107 |
item->type = tag; |
| 108 |
|
| 109 |
|
| 110 |
item->report_size = global_state->report_size; |
| 111 |
item->logical_min = global_state->logical_min; |
| 112 |
item->logical_max = global_state->logical_max; |
| 113 |
item->physical_min = global_state->physical_min; |
| 114 |
item->physical_max = global_state->physical_max; |
| 115 |
item->unit = global_state->unit; |
| 116 |
item->unit_exponent = global_state->unit_exponent; |
| 117 |
item->report_id = global_state->report_id; |
| 118 |
|
| 119 |
return item; |
| 120 |
} |
| 121 |
|
| 122 |
hid_item* build_collection(uint8_t collectionType) |
| 123 |
{ |
| 124 |
hid_item* col = (hid_item*) calloc(sizeof(hid_item), 1); |
| 125 |
assert(col); |
| 126 |
col->type = collectionType; |
| 127 |
col->usage = pop_local_item(HID_USAGE_ITEM); |
| 128 |
return col; |
| 129 |
} |
| 130 |
|
| 131 |
void append_item_with_head(hid_item* head, hid_item* i) |
| 132 |
{ |
| 133 |
assert(head); |
| 134 |
for (; head->next; head = head->next) {} |
| 135 |
head->next = i; |
| 136 |
} |
| 137 |
|
| 138 |
void append_item(hid_item* col, hid_item* i) |
| 139 |
{ |
| 140 |
assert(col); |
| 141 |
assert(i); |
| 142 |
assert(i->next == NULL); |
| 143 |
assert(i->parent == NULL); |
| 144 |
|
| 145 |
i->parent = col; |
| 146 |
hid_item* last = col->collection; |
| 147 |
if (!last) |
| 148 |
{ |
| 149 |
col->collection = i; |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
append_item_with_head(last, i); |
| 154 |
} |
| 155 |
|
| 156 |
int hid_parse_reportdesc(uint8_t* rdesc_buf, uint32_t rdesc_size, hid_item** item) |
| 157 |
{ |
| 158 |
hid_item current_state; |
| 159 |
uint8_t report_id = 0; |
| 160 |
uint8_t report_count = 1; |
| 161 |
uint8_t *p = rdesc_buf; |
| 162 |
uint8_t* pEnd = p + rdesc_size; |
| 163 |
|
| 164 |
hid_item* root_collection = NULL; |
| 165 |
hid_item* current_collection = NULL; |
| 166 |
|
| 167 |
|
| 168 |
memset(item_stack, 0, sizeof(hid_item) * MAX_ITEM_STACK); |
| 169 |
item_stack_size = 0; |
| 170 |
|
| 171 |
memset(¤t_state, 0, sizeof(hid_item)); |
| 172 |
usage_count = 0; |
| 173 |
|
| 174 |
init_local_data(); |
| 175 |
|
| 176 |
while (p < pEnd) |
| 177 |
{ |
| 178 |
|
| 179 |
uint8_t pfx = *p++; |
| 180 |
if (pfx == HID_LONG_ITEM) |
| 181 |
{ |
| 182 |
printf("encountered long item\n"); |
| 183 |
} |
| 184 |
|
| 185 |
uint8_t size = pfx & 0x3; |
| 186 |
uint8_t bytes = size_to_bytes(size); |
| 187 |
uint8_t type = (pfx >> 2) & 0x3; |
| 188 |
uint8_t tag = pfx >> 4; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
if (type == 0) |
| 195 |
{ |
| 196 |
switch (tag) |
| 197 |
{ |
| 198 |
case HID_COLLECTION_ITEM: |
| 199 |
{ |
| 200 |
|
| 201 |
uint8_t flags = read_uint(size, p); |
| 202 |
hid_item* col = build_collection(flags); |
| 203 |
if (!current_collection) { |
| 204 |
if (root_collection) { |
| 205 |
append_item_with_head(root_collection, col); |
| 206 |
} else { |
| 207 |
root_collection = col; |
| 208 |
} |
| 209 |
current_collection = col; |
| 210 |
} else { |
| 211 |
append_item(current_collection, col); |
| 212 |
current_collection = col; |
| 213 |
} |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
case HID_END_COLLECTION_ITEM: |
| 218 |
|
| 219 |
assert(current_collection); |
| 220 |
current_collection = current_collection->parent; |
| 221 |
break; |
| 222 |
|
| 223 |
case HID_INPUT_ITEM: |
| 224 |
case HID_OUTPUT_ITEM: |
| 225 |
case HID_FEATURE_ITEM: |
| 226 |
{ |
| 227 |
int i; |
| 228 |
uint8_t flags = read_uint(size, p); |
| 229 |
|
| 230 |
for (i=0; i<report_count; ++i) { |
| 231 |
hid_item* item = build_item(tag, flags, ¤t_state); |
| 232 |
|
| 233 |
|
| 234 |
append_item(current_collection, item); |
| 235 |
} |
| 236 |
break; |
| 237 |
} |
| 238 |
|
| 239 |
default: |
| 240 |
fprintf(stderr, "bad item %d\n", tag); |
| 241 |
} |
| 242 |
|
| 243 |
clear_local_data(); |
| 244 |
} |
| 245 |
|
| 246 |
else if (type == 1) |
| 247 |
{ |
| 248 |
switch (tag) |
| 249 |
{ |
| 250 |
case HID_USAGE_PAGE_ITEM: |
| 251 |
current_state.usage = read_uint(size, p) << 16; |
| 252 |
break; |
| 253 |
|
| 254 |
case HID_LOGICAL_MINIMUM_ITEM: |
| 255 |
current_state.logical_min = read_int(size, p); |
| 256 |
break; |
| 257 |
|
| 258 |
case HID_LOGICAL_MAXIMUM_ITEM: |
| 259 |
current_state.logical_max = read_int(size, p); |
| 260 |
break; |
| 261 |
|
| 262 |
case HID_UNIT_ITEM: |
| 263 |
current_state.unit = read_uint(size, p); |
| 264 |
break; |
| 265 |
|
| 266 |
case HID_EXPONENT_ITEM: |
| 267 |
current_state.unit_exponent = read_uint(size, p); |
| 268 |
break; |
| 269 |
|
| 270 |
case HID_REPORT_ID_ITEM: |
| 271 |
report_id = read_uint(size, p); |
| 272 |
|
| 273 |
current_state.report_id = report_id; |
| 274 |
break; |
| 275 |
|
| 276 |
case HID_REPORT_SIZE_ITEM: |
| 277 |
current_state.report_size = read_uint(size, p); |
| 278 |
|
| 279 |
break; |
| 280 |
|
| 281 |
case HID_REPORT_COUNT_ITEM: |
| 282 |
report_count = read_uint(size, p); |
| 283 |
|
| 284 |
break; |
| 285 |
|
| 286 |
case HID_POP_ITEM: |
| 287 |
assert(item_stack_size > 0); |
| 288 |
current_state = item_stack[--item_stack_size]; |
| 289 |
break; |
| 290 |
|
| 291 |
case HID_PUSH_ITEM: |
| 292 |
item_stack[item_stack_size++] = current_state; |
| 293 |
break; |
| 294 |
} |
| 295 |
} else if (type == 2) { |
| 296 |
|
| 297 |
uint32_t value = read_uint(size, p); |
| 298 |
switch (tag) |
| 299 |
{ |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
case HID_USAGE_ITEM: |
| 304 |
if (size < sizeof(uint32_t)) |
| 305 |
value |= current_state.usage; |
| 306 |
push_local_item(tag, value); |
| 307 |
break; |
| 308 |
|
| 309 |
case HID_USAGE_MINIMUM_ITEM: |
| 310 |
set_local_minimum(HID_USAGE_ITEM, value | current_state.usage); |
| 311 |
break; |
| 312 |
|
| 313 |
case HID_USAGE_MAXIMUM_ITEM: |
| 314 |
set_local_maximum(HID_USAGE_ITEM, value | current_state.usage); |
| 315 |
break; |
| 316 |
|
| 317 |
case HID_DESIGNATOR_MINIMUM_ITEM: |
| 318 |
set_local_minimum(HID_DESIGNATOR_INDEX_ITEM, value); |
| 319 |
break; |
| 320 |
|
| 321 |
case HID_DESIGNATOR_MAXIMUM_ITEM: |
| 322 |
set_local_maximum(HID_DESIGNATOR_INDEX_ITEM, value); |
| 323 |
break; |
| 324 |
|
| 325 |
case HID_STRING_MINIMUM_ITEM: |
| 326 |
set_local_minimum(HID_STRING_INDEX_ITEM, value); |
| 327 |
break; |
| 328 |
|
| 329 |
case HID_STRING_MAXIMUM_ITEM: |
| 330 |
set_local_maximum(HID_STRING_INDEX_ITEM, value); |
| 331 |
break; |
| 332 |
|
| 333 |
default: |
| 334 |
push_local_item(tag, value); |
| 335 |
} |
| 336 |
} else { |
| 337 |
fprintf(stderr, "bad type value: %d\n", type); |
| 338 |
} |
| 339 |
|
| 340 |
p += bytes; |
| 341 |
} |
| 342 |
|
| 343 |
clear_local_data(); |
| 344 |
*item = root_collection; |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
void hid_free_reportdesc(hid_item* root) |
| 349 |
{ |
| 350 |
free(root); |
| 351 |
} |
| 352 |
|
| 353 |
int hid_parse_is_relative(hid_item* item) |
| 354 |
{ |
| 355 |
return item->flags & 0x04; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
typedef struct { |
| 360 |
uint8_t allocated, count; |
| 361 |
uint32_t* d; |
| 362 |
uint32_t minimum, maximum, step; |
| 363 |
} local_data_array; |
| 364 |
|
| 365 |
local_data_array local_data_store[0xf]; |
| 366 |
|
| 367 |
void init_local_data() |
| 368 |
{ |
| 369 |
int i; |
| 370 |
for (i=0; i<0xf; ++i) { |
| 371 |
memset(&local_data_store[i], 0, sizeof(local_data_array)); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
void clear_local_data() |
| 376 |
{ |
| 377 |
int i; |
| 378 |
for (i=0; i<0xf; ++i) { |
| 379 |
if (local_data_store[i].d != NULL) { |
| 380 |
free(local_data_store[i].d); |
| 381 |
} |
| 382 |
|
| 383 |
memset(&local_data_store[i], 0, sizeof(local_data_array)); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
void reallocate_local_item(uint8_t tag) |
| 388 |
{ |
| 389 |
local_data_array* arr = &local_data_store[tag >> 4]; |
| 390 |
arr->allocated = (arr->allocated == 0) ? 4 : arr->allocated << 1; |
| 391 |
arr->d = realloc(arr->d, sizeof(uint32_t) * arr->allocated); |
| 392 |
} |
| 393 |
|
| 394 |
void push_local_item(uint8_t tag, uint32_t data) |
| 395 |
{ |
| 396 |
local_data_array* arr = &local_data_store[tag >> 4]; |
| 397 |
if (arr->allocated == arr->count) |
| 398 |
reallocate_local_item(tag); |
| 399 |
|
| 400 |
arr->d[arr->count++] = data; |
| 401 |
} |
| 402 |
|
| 403 |
uint32_t pop_local_item(uint8_t tag) |
| 404 |
{ |
| 405 |
local_data_array* arr = &local_data_store[tag >> 4]; |
| 406 |
if (arr->minimum) |
| 407 |
{ |
| 408 |
uint32_t result = arr->minimum + arr->step++; |
| 409 |
if (arr->maximum && (result > arr->maximum)) { |
| 410 |
fprintf(stderr, "hidparse.c:pop_local_item: range exceeded for tag %d", tag); |
| 411 |
} |
| 412 |
|
| 413 |
return result; |
| 414 |
} |
| 415 |
|
| 416 |
if (arr->count == 0) |
| 417 |
return 0; |
| 418 |
|
| 419 |
uint32_t result = arr->d[arr->count - 1]; |
| 420 |
if (arr->count > 1) |
| 421 |
--arr->count; |
| 422 |
return result; |
| 423 |
} |
| 424 |
|
| 425 |
void set_local_minimum(uint8_t tag, uint32_t data) |
| 426 |
{ |
| 427 |
local_data_array* arr = &local_data_store[tag >> 4]; |
| 428 |
arr->minimum = data; |
| 429 |
} |
| 430 |
|
| 431 |
void set_local_maximum(uint8_t tag, uint32_t data) |
| 432 |
{ |
| 433 |
local_data_array* arr = &local_data_store[tag >> 4]; |
| 434 |
arr->maximum = data; |
| 435 |
} |