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