source: mainline/uspace/lib/usbhid/src/hiddescriptor.c@ 182487c6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 182487c6 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 24.9 KB
Line 
1/*
2 * Copyright (c) 2011 Matej Klonfar
3 * Copyright (c) 2018 Ondrej Hlavaty
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libusbhid
31 * @{
32 */
33/** @file
34 * HID report descriptor and report data parser implementation.
35 */
36#include <usb/hid/hidparser.h>
37#include <errno.h>
38#include <stdio.h>
39#include <mem.h>
40#include <usb/debug.h>
41#include <assert.h>
42#include <stdlib.h>
43
44/*
45 * Constants defining current parsing mode for correct parsing of the set of
46 * local tags (usage) enclosed in delimter tags.
47 */
48/**
49 * Second delimiter tag was read. The set of local items (usage) ended.
50 */
51#define OUTSIDE_DELIMITER_SET 0
52
53/**
54 * First delimiter tag was read. The set of local items (usage) started.
55 */
56#define START_DELIMITER_SET 1
57
58/**
59 * Parser is in the set of local items.
60 */
61#define INSIDE_DELIMITER_SET 2
62
63/** The new report item flag. Used to determine when the item is completly
64 * configured and should be added to the report structure
65 */
66#define USB_HID_NEW_REPORT_ITEM 1
67
68/** No special action after the report descriptor tag is processed should be
69 * done
70 */
71#define USB_HID_NO_ACTION 2
72
73#define USB_HID_RESET_OFFSET 3
74
75#define USB_HID_INVALID -98
76/** Unknown tag was founded in report descriptor data*/
77#define USB_HID_UNKNOWN_TAG -99
78
79/**
80 * Checks if given collection path is already present in report structure and
81 * inserts it if not.
82 *
83 * @param report Report structure
84 * @param cmp_path The collection path
85 * @return Pointer to the result collection path in report structure.
86 * @retval NULL If some error occurs
87 */
88usb_hid_report_path_t *usb_hid_report_path_try_insert(usb_hid_report_t *report,
89 usb_hid_report_path_t *cmp_path)
90{
91 link_t *path_it = report->collection_paths.head.next;
92 usb_hid_report_path_t *path = NULL;
93
94 if ((report == NULL) || (cmp_path == NULL)) {
95 return NULL;
96 }
97
98 while (path_it != &report->collection_paths.head) {
99 path = list_get_instance(path_it, usb_hid_report_path_t,
100 cpath_link);
101
102 if (usb_hid_report_compare_usage_path(path, cmp_path,
103 USB_HID_PATH_COMPARE_STRICT) == 0) {
104 break;
105 }
106 path_it = path_it->next;
107 }
108 if (path_it == &report->collection_paths.head) {
109 path = usb_hid_report_path_clone(cmp_path);
110 if (path == NULL) {
111 return NULL;
112 }
113 list_append(&path->cpath_link, &report->collection_paths);
114 report->collection_paths_count++;
115
116 return path;
117 } else {
118 return list_get_instance(path_it, usb_hid_report_path_t,
119 cpath_link);
120 }
121}
122
123/**
124 * Initialize the report descriptor parser structure
125 *
126 * @param parser Report descriptor parser structure
127 * @return Error code
128 * @retval EINVAL If no report structure was given
129 * @retval EOK If report structure was successfully initialized
130 */
131errno_t usb_hid_report_init(usb_hid_report_t *report)
132{
133 if (report == NULL) {
134 return EINVAL;
135 }
136
137 memset(report, 0, sizeof(usb_hid_report_t));
138 list_initialize(&report->reports);
139 list_initialize(&report->collection_paths);
140
141 report->use_report_ids = 0;
142 return EOK;
143}
144
145/**
146 *
147 *
148 * @param report Report structure in which the new report items should be
149 * stored
150 * @param report_item Current report descriptor's parsing state table
151 * @return Error code
152 * @retval EOK If all fields were successfully append to report
153 * @retval EINVAL If invalid parameters (NULL) was given
154 * @retval ENOMEM If there is no memmory to store new report description
155 *
156 */
157errno_t usb_hid_report_append_fields(usb_hid_report_t *report,
158 usb_hid_report_item_t *report_item)
159{
160
161 usb_hid_report_field_t *field;
162 int i;
163
164 uint32_t *usages;
165 int usages_used = 0;
166
167 if ((report == NULL) || (report_item == NULL)) {
168 return EINVAL;
169 }
170
171 if (report_item->usages_count > 0) {
172 usages = malloc(sizeof(uint32_t) * report_item->usages_count);
173 memcpy(usages, report_item->usages, sizeof(int32_t) *
174 report_item->usages_count);
175 } else {
176 usages = NULL;
177 }
178
179 usb_hid_report_path_t *path = report_item->usage_path;
180 for (i = 0; i < report_item->count; i++) {
181
182 field = malloc(sizeof(usb_hid_report_field_t));
183 if (field == NULL) {
184 return ENOMEM;
185 }
186
187 memset(field, 0, sizeof(usb_hid_report_field_t));
188 link_initialize(&field->ritems_link);
189
190 /* fill the attributes */
191 field->logical_minimum = report_item->logical_minimum;
192 field->logical_maximum = report_item->logical_maximum;
193 field->physical_minimum = report_item->physical_minimum;
194 field->physical_maximum = report_item->physical_maximum;
195
196 if (USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
197 /*
198 * Store usage array. The Correct Usage Page and Usage is
199 * depending on data in report and will be filled later
200 */
201 field->usage = 0;
202 field->usage_page = 0; //report_item->usage_page;
203
204 field->usages_count = report_item->usages_count;
205 field->usages = usages;
206 usages_used = 1;
207 } else {
208
209 /* Fill the correct Usage and Usage Page */
210 int32_t usage;
211 if (i < report_item->usages_count) {
212 usage = report_item->usages[i];
213 } else {
214 usage = report_item->usages[report_item->usages_count - 1];
215 }
216
217 if (USB_HID_IS_EXTENDED_USAGE(usage)) {
218 field->usage = USB_HID_EXTENDED_USAGE(usage);
219 field->usage_page =
220 USB_HID_EXTENDED_USAGE_PAGE(usage);
221 } else {
222 // should not occur
223 field->usage = usage;
224 field->usage_page = report_item->usage_page;
225 }
226 }
227
228 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL,
229 field->usage_page);
230 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL,
231 field->usage);
232
233 field->collection_path =
234 usb_hid_report_path_try_insert(report, path);
235
236 field->size = report_item->size;
237
238 field->offset = report_item->offset + (i * report_item->size);
239
240 if (report->use_report_ids != 0) {
241 field->offset += 8;
242 report->use_report_ids = 1;
243 }
244
245 field->item_flags = report_item->item_flags;
246
247 /* find the right report list*/
248 usb_hid_report_description_t *report_des;
249 report_des = usb_hid_report_find_description(report,
250 report_item->id, report_item->type);
251
252 if (report_des == NULL) {
253 report_des = malloc(
254 sizeof(usb_hid_report_description_t));
255 if (report_des == NULL) {
256 return ENOMEM;
257 }
258
259 memset(report_des, 0,
260 sizeof(usb_hid_report_description_t));
261
262 report_des->type = report_item->type;
263 report_des->report_id = report_item->id;
264 if (report_des->report_id != 0) {
265 /* set up the bit length by report_id field */
266 report_des->bit_length = 8;
267 }
268
269 link_initialize (&report_des->reports_link);
270 list_initialize (&report_des->report_items);
271
272 list_append(&report_des->reports_link, &report->reports);
273 report->report_count++;
274 }
275
276 /* append this field to the end of founded report list */
277 list_append(&field->ritems_link, &report_des->report_items);
278
279 /* update the sizes */
280 report_des->bit_length += field->size;
281 report_des->item_length++;
282
283 }
284
285 // free only when not used!!!
286 if (usages && usages_used == 0) {
287 free(usages);
288 }
289
290 return EOK;
291}
292
293/**
294 * Finds description of report with given report_id and of given type in
295 * opaque report structure.
296 *
297 * @param report Opaque structure containing the parsed report descriptor
298 * @param report_id ReportId of report we are searching
299 * @param type Type of report we are searching
300 * @return Pointer to the particular report description
301 * @retval NULL If no description is founded
302 */
303usb_hid_report_description_t *usb_hid_report_find_description(
304 const usb_hid_report_t *report, uint8_t report_id,
305 usb_hid_report_type_t type)
306{
307
308 if (report == NULL) {
309 return NULL;
310 }
311
312 list_foreach(report->reports, reports_link,
313 usb_hid_report_description_t, report_des) {
314 // if report id not set, return the first of the type
315 if (((report_des->report_id == report_id) || (report_id == 0)) &&
316 (report_des->type == type)) {
317 return report_des;
318 }
319 }
320
321 return NULL;
322}
323
324/** Parse HID report descriptor.
325 *
326 * @param parser Opaque HID report parser structure.
327 * @param data Data describing the report.
328 * @return Error code.
329 * @retval ENOMEM If no more memmory is available
330 * @retval EINVAL If invalid data are founded
331 * @retval EOK If report descriptor is successfully parsed
332 */
333errno_t usb_hid_parse_report_descriptor(usb_hid_report_t *report,
334 const uint8_t *data, size_t size)
335{
336 size_t i = 0;
337 uint8_t tag = 0;
338 uint8_t item_size = 0;
339 int class = 0;
340 int ret;
341 usb_hid_report_item_t *report_item = 0;
342 usb_hid_report_item_t *new_report_item;
343 usb_hid_report_path_t *usage_path;
344
345 size_t offset_input = 0;
346 size_t offset_output = 0;
347 size_t offset_feature = 0;
348
349 link_t *item_link;
350
351 list_t stack;
352 list_initialize(&stack);
353
354 /* parser structure initialization*/
355 if (usb_hid_report_init(report) != EOK) {
356 return EINVAL;
357 }
358
359 /*report item initialization*/
360 if (!(report_item = malloc(sizeof(usb_hid_report_item_t)))) {
361 return ENOMEM;
362 }
363 memset(report_item, 0, sizeof(usb_hid_report_item_t));
364 link_initialize(&(report_item->link));
365
366 /* usage path context initialization */
367 if (!(usage_path = usb_hid_report_path())) {
368 return ENOMEM;
369 }
370 usb_hid_report_path_append_item(usage_path, 0, 0);
371
372 while (i < size) {
373 if (!USB_HID_ITEM_IS_LONG(data[i])) {
374
375 if ((i + USB_HID_ITEM_SIZE(data[i])) >= size) {
376 return EINVAL;
377 }
378
379 tag = USB_HID_ITEM_TAG(data[i]);
380 item_size = USB_HID_ITEM_SIZE(data[i]);
381 class = USB_HID_ITEM_TAG_CLASS(data[i]);
382
383 ret = usb_hid_report_parse_tag(tag, class, data + i + 1,
384 item_size, report_item, usage_path);
385
386 switch (ret) {
387 case USB_HID_NEW_REPORT_ITEM:
388 /*
389 * store report item to report and create the
390 * new one store current collection path
391 */
392 report_item->usage_path = usage_path;
393
394 usb_hid_report_path_set_report_id(
395 report_item->usage_path, report_item->id);
396
397 if (report_item->id != 0) {
398 report->use_report_ids = 1;
399 }
400
401 switch (tag) {
402 case USB_HID_REPORT_TAG_INPUT:
403 report_item->type =
404 USB_HID_REPORT_TYPE_INPUT;
405
406 report_item->offset = offset_input;
407 offset_input += report_item->count *
408 report_item->size;
409 break;
410
411 case USB_HID_REPORT_TAG_OUTPUT:
412 report_item->type =
413 USB_HID_REPORT_TYPE_OUTPUT;
414
415 report_item->offset = offset_output;
416 offset_output += report_item->count *
417 report_item->size;
418 break;
419
420 case USB_HID_REPORT_TAG_FEATURE:
421 report_item->type =
422 USB_HID_REPORT_TYPE_FEATURE;
423
424 report_item->offset = offset_feature;
425 offset_feature += report_item->count *
426 report_item->size;
427 break;
428
429 default:
430 usb_log_debug2(
431 "\tjump over - tag %X\n", tag);
432 break;
433 }
434
435 /*
436 * append new fields to the report structure
437 */
438 usb_hid_report_append_fields(report,
439 report_item);
440
441 /* reset local items */
442 usb_hid_report_reset_local_items (report_item);
443 break;
444
445 case USB_HID_RESET_OFFSET:
446 offset_input = 0;
447 offset_output = 0;
448 offset_feature = 0;
449 usb_hid_report_path_set_report_id (usage_path,
450 report_item->id);
451 break;
452
453 case USB_HID_REPORT_TAG_PUSH:
454 // push current state to stack
455 new_report_item = usb_hid_report_item_clone(
456 report_item);
457
458 usb_hid_report_path_t *tmp_path =
459 usb_hid_report_path_clone(usage_path);
460
461 new_report_item->usage_path = tmp_path;
462
463 list_prepend (&new_report_item->link, &stack);
464 break;
465 case USB_HID_REPORT_TAG_POP:
466 // restore current state from stack
467 item_link = list_first(&stack);
468 if (item_link == NULL) {
469 return EINVAL;
470 }
471 free(report_item);
472
473 report_item = list_get_instance(item_link,
474 usb_hid_report_item_t, link);
475
476 usb_hid_report_usage_path_t *tmp_usage_path;
477 tmp_usage_path = list_get_instance(
478 report_item->usage_path->cpath_link.prev,
479 usb_hid_report_usage_path_t, rpath_items_link);
480
481 usb_hid_report_set_last_item(usage_path,
482 USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
483
484 usb_hid_report_set_last_item(usage_path,
485 USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
486
487 usb_hid_report_path_free(report_item->usage_path);
488 list_remove (item_link);
489
490 break;
491
492 default:
493 // nothing special to do
494 break;
495 }
496
497 /* jump over the processed block */
498 i += 1 + USB_HID_ITEM_SIZE(data[i]);
499 } else {
500 // TBD
501 i += 3 + USB_HID_ITEM_SIZE(data[i + 1]);
502 }
503
504 }
505
506 return EOK;
507}
508
509/**
510 * Parse one tag of the report descriptor
511 *
512 * @param Tag to parse
513 * @param Report descriptor buffer
514 * @param Size of data belongs to this tag
515 * @param Current report item structe
516 * @return Code of action to be done next
517 */
518int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data,
519 size_t item_size, usb_hid_report_item_t *report_item,
520 usb_hid_report_path_t *usage_path)
521{
522
523 int ret;
524
525 switch (class) {
526 case USB_HID_TAG_CLASS_MAIN:
527
528 if ((ret = usb_hid_report_parse_main_tag(tag, data, item_size,
529 report_item, usage_path)) == 0) {
530
531 return USB_HID_NEW_REPORT_ITEM;
532 } else {
533 return ret;
534 }
535 break;
536
537 case USB_HID_TAG_CLASS_GLOBAL:
538 return usb_hid_report_parse_global_tag(tag, data, item_size,
539 report_item, usage_path);
540 break;
541
542 case USB_HID_TAG_CLASS_LOCAL:
543 return usb_hid_report_parse_local_tag(tag, data, item_size,
544 report_item, usage_path);
545 break;
546
547 default:
548 return USB_HID_NO_ACTION;
549 }
550}
551
552/**
553 * Parse main tags of report descriptor
554 *
555 * @param Tag identifier
556 * @param Data buffer
557 * @param Length of data buffer
558 * @param Current state table
559 * @return 0 or USB_HID_ code
560 */
561
562int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data,
563 size_t item_size, usb_hid_report_item_t *report_item,
564 usb_hid_report_path_t *usage_path)
565{
566 usb_hid_report_usage_path_t *path_item;
567
568 switch (tag) {
569 case USB_HID_REPORT_TAG_INPUT:
570 case USB_HID_REPORT_TAG_OUTPUT:
571 case USB_HID_REPORT_TAG_FEATURE:
572 report_item->item_flags = *data;
573 return 0;
574 break;
575
576 case USB_HID_REPORT_TAG_COLLECTION:
577
578 /* store collection atributes */
579 path_item = list_get_instance(list_first(&usage_path->items),
580 usb_hid_report_usage_path_t, rpath_items_link);
581 path_item->flags = *data;
582
583 /* set last item */
584 usb_hid_report_set_last_item(usage_path,
585 USB_HID_TAG_CLASS_GLOBAL,
586 USB_HID_EXTENDED_USAGE_PAGE(report_item->usages[report_item->usages_count - 1]));
587
588 usb_hid_report_set_last_item(usage_path,
589 USB_HID_TAG_CLASS_LOCAL,
590 USB_HID_EXTENDED_USAGE(report_item->usages[report_item->usages_count - 1]));
591
592 /*
593 * append the new one which will be set by common usage/usage
594 * page
595 */
596 usb_hid_report_path_append_item(usage_path,
597 report_item->usage_page,
598 report_item->usages[report_item->usages_count - 1]);
599
600 usb_hid_report_reset_local_items (report_item);
601 return USB_HID_NO_ACTION;
602 break;
603
604 case USB_HID_REPORT_TAG_END_COLLECTION:
605 usb_hid_report_remove_last_item(usage_path);
606 return USB_HID_NO_ACTION;
607 break;
608
609 default:
610 return USB_HID_NO_ACTION;
611 }
612
613 return 0;
614}
615
616/**
617 * Parse global tags of report descriptor
618 *
619 * @param Tag identifier
620 * @param Data buffer
621 * @param Length of data buffer
622 * @param Current state table
623 * @return 0 or USB_HID_ code
624 */
625int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data,
626 size_t item_size, usb_hid_report_item_t *report_item,
627 usb_hid_report_path_t *usage_path)
628{
629
630 switch (tag) {
631 case USB_HID_REPORT_TAG_USAGE_PAGE:
632 report_item->usage_page =
633 usb_hid_report_tag_data_uint32(data, item_size);
634 break;
635
636 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
637 report_item->logical_minimum = USB_HID_UINT32_TO_INT32(
638 usb_hid_report_tag_data_uint32(data, item_size),
639 item_size * 8);
640 break;
641
642 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
643 report_item->logical_maximum = USB_HID_UINT32_TO_INT32(
644 usb_hid_report_tag_data_uint32(data, item_size),
645 item_size * 8);
646 break;
647
648 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
649 report_item->physical_minimum = USB_HID_UINT32_TO_INT32(
650 usb_hid_report_tag_data_uint32(data, item_size),
651 item_size * 8);
652 break;
653
654 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
655 report_item->physical_maximum = USB_HID_UINT32_TO_INT32(
656 usb_hid_report_tag_data_uint32(data, item_size),
657 item_size * 8);
658 break;
659
660 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
661 report_item->unit_exponent = usb_hid_report_tag_data_uint32(
662 data, item_size);
663 break;
664
665 case USB_HID_REPORT_TAG_UNIT:
666 report_item->unit = usb_hid_report_tag_data_uint32(
667 data, item_size);
668 break;
669
670 case USB_HID_REPORT_TAG_REPORT_SIZE:
671 report_item->size = usb_hid_report_tag_data_uint32(
672 data, item_size);
673 break;
674
675 case USB_HID_REPORT_TAG_REPORT_COUNT:
676 report_item->count = usb_hid_report_tag_data_uint32(
677 data, item_size);
678 break;
679
680 case USB_HID_REPORT_TAG_REPORT_ID:
681 report_item->id = usb_hid_report_tag_data_uint32(data,
682 item_size);
683 return USB_HID_RESET_OFFSET;
684 break;
685
686 case USB_HID_REPORT_TAG_PUSH:
687 case USB_HID_REPORT_TAG_POP:
688 /*
689 * stack operations are done in top level parsing
690 * function
691 */
692 return tag;
693 break;
694
695 default:
696 return USB_HID_NO_ACTION;
697 }
698
699 return 0;
700}
701
702/**
703 * Parse local tags of report descriptor
704 *
705 * @param Tag identifier
706 * @param Data buffer
707 * @param Length of data buffer
708 * @param Current state table
709 * @return 0 or USB_HID_ code
710 */
711int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data,
712 size_t item_size, usb_hid_report_item_t *report_item,
713 usb_hid_report_path_t *usage_path)
714{
715 int32_t extended_usage;
716
717 switch (tag) {
718 case USB_HID_REPORT_TAG_USAGE:
719 switch (report_item->in_delimiter) {
720 case INSIDE_DELIMITER_SET:
721 /*
722 * Nothing to do.
723 * We catch only the first one
724 */
725 break;
726
727 case START_DELIMITER_SET:
728 report_item->in_delimiter = INSIDE_DELIMITER_SET;
729 /* Fallthrough */
730 case OUTSIDE_DELIMITER_SET:
731 extended_usage = ((report_item->usage_page) << 16);
732 extended_usage +=
733 usb_hid_report_tag_data_uint32(data, item_size);
734
735 report_item->usages[report_item->usages_count] =
736 extended_usage;
737
738 report_item->usages_count++;
739 break;
740 }
741 break;
742
743 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
744 if (item_size == 3) {
745 /* Usage extended usages */
746 report_item->extended_usage_page =
747 USB_HID_EXTENDED_USAGE_PAGE(
748 usb_hid_report_tag_data_uint32(data, item_size));
749
750 report_item->usage_minimum =
751 USB_HID_EXTENDED_USAGE(
752 usb_hid_report_tag_data_uint32(data, item_size));
753 } else {
754 report_item->usage_minimum =
755 usb_hid_report_tag_data_uint32(data, item_size);
756 }
757 break;
758
759 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
760 if (item_size == 3) {
761 if (report_item->extended_usage_page !=
762 USB_HID_EXTENDED_USAGE_PAGE(
763 usb_hid_report_tag_data_uint32(data, item_size))) {
764 return USB_HID_INVALID;
765 }
766
767 /* Usage extended usages */
768 report_item->extended_usage_page =
769 USB_HID_EXTENDED_USAGE_PAGE(
770 usb_hid_report_tag_data_uint32(data, item_size));
771
772 report_item->usage_maximum =
773 USB_HID_EXTENDED_USAGE(
774 usb_hid_report_tag_data_uint32(data, item_size));
775 } else {
776 report_item->usage_maximum =
777 usb_hid_report_tag_data_uint32(data, item_size);
778 }
779
780 /* Put the records into the usages array */
781 for (int32_t i = report_item->usage_minimum;
782 i <= report_item->usage_maximum; i++) {
783
784 if (report_item->extended_usage_page) {
785 report_item->usages[report_item->usages_count++] =
786 (report_item->extended_usage_page << 16) + i;
787 } else {
788 report_item->usages[report_item->usages_count++] =
789 (report_item->usage_page << 16) + i;
790 }
791 }
792 report_item->extended_usage_page = 0;
793
794 break;
795
796 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
797 report_item->designator_index =
798 usb_hid_report_tag_data_uint32(data, item_size);
799 break;
800
801 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
802 report_item->designator_minimum =
803 usb_hid_report_tag_data_uint32(data, item_size);
804 break;
805
806 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
807 report_item->designator_maximum =
808 usb_hid_report_tag_data_uint32(data, item_size);
809 break;
810
811 case USB_HID_REPORT_TAG_STRING_INDEX:
812 report_item->string_index =
813 usb_hid_report_tag_data_uint32(data, item_size);
814 break;
815
816 case USB_HID_REPORT_TAG_STRING_MINIMUM:
817 report_item->string_minimum =
818 usb_hid_report_tag_data_uint32(data, item_size);
819 break;
820
821 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
822 report_item->string_maximum =
823 usb_hid_report_tag_data_uint32(data, item_size);
824 break;
825
826 case USB_HID_REPORT_TAG_DELIMITER:
827 report_item->in_delimiter =
828 usb_hid_report_tag_data_uint32(data, item_size);
829 break;
830
831 default:
832 return USB_HID_NO_ACTION;
833 }
834
835 return 0;
836}
837
838/**
839 * Converts raw data to uint32 (thats the maximum length of short item data)
840 *
841 * @param Data buffer
842 * @param Size of buffer
843 * @return Converted int32 number
844 */
845uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size)
846{
847 unsigned int i;
848 uint32_t result;
849
850 result = 0;
851 for (i = 0; i < size; i++) {
852 result = (result | (data[i]) << (i * 8));
853 }
854
855 return result;
856}
857
858/**
859 * Prints content of given list of report items.
860 *
861 * @param List of report items (usb_hid_report_item_t)
862 * @return void
863 */
864void usb_hid_descriptor_print_list(list_t *list)
865{
866 if (list == NULL || list_empty(list)) {
867 usb_log_debug("\tempty");
868 return;
869 }
870
871 list_foreach(*list, ritems_link, usb_hid_report_field_t,
872 report_item) {
873 usb_log_debug("\t\tOFFSET: %u", report_item->offset);
874 usb_log_debug("\t\tSIZE: %zu", report_item->size);
875 usb_log_debug("\t\tLOGMIN: %d",
876 report_item->logical_minimum);
877 usb_log_debug("\t\tLOGMAX: %d",
878 report_item->logical_maximum);
879 usb_log_debug("\t\tPHYMIN: %d",
880 report_item->physical_minimum);
881 usb_log_debug("\t\tPHYMAX: %d",
882 report_item->physical_maximum);
883 usb_log_debug("\t\ttUSAGEMIN: %X",
884 report_item->usage_minimum);
885 usb_log_debug("\t\tUSAGEMAX: %X",
886 report_item->usage_maximum);
887 usb_log_debug("\t\tUSAGES COUNT: %zu",
888 report_item->usages_count);
889
890 usb_log_debug("\t\tVALUE: %X", report_item->value);
891 usb_log_debug("\t\ttUSAGE: %X", report_item->usage);
892 usb_log_debug("\t\tUSAGE PAGE: %X", report_item->usage_page);
893
894 usb_hid_print_usage_path(report_item->collection_path);
895 }
896}
897
898/**
899 * Prints content of given report descriptor in human readable format.
900 *
901 * @param parser Parsed descriptor to print
902 * @return void
903 */
904void usb_hid_descriptor_print(usb_hid_report_t *report)
905{
906 if (report == NULL)
907 return;
908
909 list_foreach(report->reports, reports_link,
910 usb_hid_report_description_t, report_des) {
911 usb_log_debug("Report ID: %d", report_des->report_id);
912 usb_log_debug("\tType: %d", report_des->type);
913 usb_log_debug("\tLength: %zu", report_des->bit_length);
914 usb_log_debug("\tB Size: %zu",
915 usb_hid_report_byte_size(report,
916 report_des->report_id,
917 report_des->type));
918 usb_log_debug("\tItems: %zu", report_des->item_length);
919
920 usb_hid_descriptor_print_list(&report_des->report_items);
921 }
922}
923
924/** Frees the HID report descriptor parser structure
925 *
926 * @param parser Opaque HID report parser structure
927 * @return void
928 */
929void usb_hid_report_deinit(usb_hid_report_t *report)
930{
931 if (report == NULL) {
932 return;
933 }
934
935 // free collection paths
936 link_t *path_link;
937 usb_hid_report_path_t *path;
938 while (!list_empty(&report->collection_paths)) {
939 path_link = list_first(&report->collection_paths);
940 path = list_get_instance(path_link,
941 usb_hid_report_path_t, cpath_link);
942
943 list_remove(path_link);
944 usb_hid_report_path_free(path);
945 }
946
947 // free report items
948 usb_hid_report_description_t *report_des;
949 usb_hid_report_field_t *field;
950 while (!list_empty(&report->reports)) {
951 report_des = list_get_instance(list_first(&report->reports),
952 usb_hid_report_description_t, reports_link);
953
954 list_remove(&report_des->reports_link);
955
956 while (!list_empty(&report_des->report_items)) {
957 field = list_get_instance(
958 list_first(&report_des->report_items),
959 usb_hid_report_field_t, ritems_link);
960
961 list_remove(&field->ritems_link);
962
963 free(field);
964 }
965
966 free(report_des);
967 }
968
969 return;
970}
971
972/**
973 * @}
974 */
Note: See TracBrowser for help on using the repository browser.