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 | */
|
---|
88 | usb_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 | */
|
---|
131 | errno_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 | */
|
---|
157 | errno_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 | */
|
---|
303 | usb_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 | */
|
---|
333 | errno_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 | free(report_item);
|
---|
369 | return ENOMEM;
|
---|
370 | }
|
---|
371 | usb_hid_report_path_append_item(usage_path, 0, 0);
|
---|
372 |
|
---|
373 | while (i < size) {
|
---|
374 | if (!USB_HID_ITEM_IS_LONG(data[i])) {
|
---|
375 |
|
---|
376 | if ((i + USB_HID_ITEM_SIZE(data[i])) >= size) {
|
---|
377 | return EINVAL;
|
---|
378 | }
|
---|
379 |
|
---|
380 | tag = USB_HID_ITEM_TAG(data[i]);
|
---|
381 | item_size = USB_HID_ITEM_SIZE(data[i]);
|
---|
382 | class = USB_HID_ITEM_TAG_CLASS(data[i]);
|
---|
383 |
|
---|
384 | ret = usb_hid_report_parse_tag(tag, class, data + i + 1,
|
---|
385 | item_size, report_item, usage_path);
|
---|
386 |
|
---|
387 | switch (ret) {
|
---|
388 | case USB_HID_NEW_REPORT_ITEM:
|
---|
389 | /*
|
---|
390 | * store report item to report and create the
|
---|
391 | * new one store current collection path
|
---|
392 | */
|
---|
393 | report_item->usage_path = usage_path;
|
---|
394 |
|
---|
395 | usb_hid_report_path_set_report_id(
|
---|
396 | report_item->usage_path, report_item->id);
|
---|
397 |
|
---|
398 | if (report_item->id != 0) {
|
---|
399 | report->use_report_ids = 1;
|
---|
400 | }
|
---|
401 |
|
---|
402 | switch (tag) {
|
---|
403 | case USB_HID_REPORT_TAG_INPUT:
|
---|
404 | report_item->type =
|
---|
405 | USB_HID_REPORT_TYPE_INPUT;
|
---|
406 |
|
---|
407 | report_item->offset = offset_input;
|
---|
408 | offset_input += report_item->count *
|
---|
409 | report_item->size;
|
---|
410 | break;
|
---|
411 |
|
---|
412 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
413 | report_item->type =
|
---|
414 | USB_HID_REPORT_TYPE_OUTPUT;
|
---|
415 |
|
---|
416 | report_item->offset = offset_output;
|
---|
417 | offset_output += report_item->count *
|
---|
418 | report_item->size;
|
---|
419 | break;
|
---|
420 |
|
---|
421 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
422 | report_item->type =
|
---|
423 | USB_HID_REPORT_TYPE_FEATURE;
|
---|
424 |
|
---|
425 | report_item->offset = offset_feature;
|
---|
426 | offset_feature += report_item->count *
|
---|
427 | report_item->size;
|
---|
428 | break;
|
---|
429 |
|
---|
430 | default:
|
---|
431 | usb_log_debug2(
|
---|
432 | "\tjump over - tag %X\n", tag);
|
---|
433 | break;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * append new fields to the report structure
|
---|
438 | */
|
---|
439 | usb_hid_report_append_fields(report,
|
---|
440 | report_item);
|
---|
441 |
|
---|
442 | /* reset local items */
|
---|
443 | usb_hid_report_reset_local_items (report_item);
|
---|
444 | break;
|
---|
445 |
|
---|
446 | case USB_HID_RESET_OFFSET:
|
---|
447 | offset_input = 0;
|
---|
448 | offset_output = 0;
|
---|
449 | offset_feature = 0;
|
---|
450 | usb_hid_report_path_set_report_id (usage_path,
|
---|
451 | report_item->id);
|
---|
452 | break;
|
---|
453 |
|
---|
454 | case USB_HID_REPORT_TAG_PUSH:
|
---|
455 | // push current state to stack
|
---|
456 | new_report_item = usb_hid_report_item_clone(
|
---|
457 | report_item);
|
---|
458 |
|
---|
459 | usb_hid_report_path_t *tmp_path =
|
---|
460 | usb_hid_report_path_clone(usage_path);
|
---|
461 |
|
---|
462 | new_report_item->usage_path = tmp_path;
|
---|
463 |
|
---|
464 | list_prepend (&new_report_item->link, &stack);
|
---|
465 | break;
|
---|
466 | case USB_HID_REPORT_TAG_POP:
|
---|
467 | // restore current state from stack
|
---|
468 | item_link = list_first(&stack);
|
---|
469 | if (item_link == NULL) {
|
---|
470 | return EINVAL;
|
---|
471 | }
|
---|
472 | free(report_item);
|
---|
473 |
|
---|
474 | report_item = list_get_instance(item_link,
|
---|
475 | usb_hid_report_item_t, link);
|
---|
476 |
|
---|
477 | usb_hid_report_usage_path_t *tmp_usage_path;
|
---|
478 | tmp_usage_path = list_get_instance(
|
---|
479 | report_item->usage_path->cpath_link.prev,
|
---|
480 | usb_hid_report_usage_path_t, rpath_items_link);
|
---|
481 |
|
---|
482 | usb_hid_report_set_last_item(usage_path,
|
---|
483 | USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
|
---|
484 |
|
---|
485 | usb_hid_report_set_last_item(usage_path,
|
---|
486 | USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
|
---|
487 |
|
---|
488 | usb_hid_report_path_free(report_item->usage_path);
|
---|
489 | list_remove (item_link);
|
---|
490 |
|
---|
491 | break;
|
---|
492 |
|
---|
493 | default:
|
---|
494 | // nothing special to do
|
---|
495 | break;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /* jump over the processed block */
|
---|
499 | i += 1 + USB_HID_ITEM_SIZE(data[i]);
|
---|
500 | } else {
|
---|
501 | // TBD
|
---|
502 | i += 3 + USB_HID_ITEM_SIZE(data[i + 1]);
|
---|
503 | }
|
---|
504 |
|
---|
505 | }
|
---|
506 |
|
---|
507 | return EOK;
|
---|
508 | }
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Parse one tag of the report descriptor
|
---|
512 | *
|
---|
513 | * @param Tag to parse
|
---|
514 | * @param Report descriptor buffer
|
---|
515 | * @param Size of data belongs to this tag
|
---|
516 | * @param Current report item structe
|
---|
517 | * @return Code of action to be done next
|
---|
518 | */
|
---|
519 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data,
|
---|
520 | size_t item_size, usb_hid_report_item_t *report_item,
|
---|
521 | usb_hid_report_path_t *usage_path)
|
---|
522 | {
|
---|
523 |
|
---|
524 | int ret;
|
---|
525 |
|
---|
526 | switch (class) {
|
---|
527 | case USB_HID_TAG_CLASS_MAIN:
|
---|
528 |
|
---|
529 | if ((ret = usb_hid_report_parse_main_tag(tag, data, item_size,
|
---|
530 | report_item, usage_path)) == 0) {
|
---|
531 |
|
---|
532 | return USB_HID_NEW_REPORT_ITEM;
|
---|
533 | } else {
|
---|
534 | return ret;
|
---|
535 | }
|
---|
536 | break;
|
---|
537 |
|
---|
538 | case USB_HID_TAG_CLASS_GLOBAL:
|
---|
539 | return usb_hid_report_parse_global_tag(tag, data, item_size,
|
---|
540 | report_item, usage_path);
|
---|
541 | break;
|
---|
542 |
|
---|
543 | case USB_HID_TAG_CLASS_LOCAL:
|
---|
544 | return usb_hid_report_parse_local_tag(tag, data, item_size,
|
---|
545 | report_item, usage_path);
|
---|
546 | break;
|
---|
547 |
|
---|
548 | default:
|
---|
549 | return USB_HID_NO_ACTION;
|
---|
550 | }
|
---|
551 | }
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * Parse main tags of report descriptor
|
---|
555 | *
|
---|
556 | * @param Tag identifier
|
---|
557 | * @param Data buffer
|
---|
558 | * @param Length of data buffer
|
---|
559 | * @param Current state table
|
---|
560 | * @return 0 or USB_HID_ code
|
---|
561 | */
|
---|
562 |
|
---|
563 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data,
|
---|
564 | size_t item_size, usb_hid_report_item_t *report_item,
|
---|
565 | usb_hid_report_path_t *usage_path)
|
---|
566 | {
|
---|
567 | usb_hid_report_usage_path_t *path_item;
|
---|
568 |
|
---|
569 | switch (tag) {
|
---|
570 | case USB_HID_REPORT_TAG_INPUT:
|
---|
571 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
572 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
573 | report_item->item_flags = *data;
|
---|
574 | return 0;
|
---|
575 | break;
|
---|
576 |
|
---|
577 | case USB_HID_REPORT_TAG_COLLECTION:
|
---|
578 |
|
---|
579 | /* store collection atributes */
|
---|
580 | path_item = list_get_instance(list_first(&usage_path->items),
|
---|
581 | usb_hid_report_usage_path_t, rpath_items_link);
|
---|
582 | path_item->flags = *data;
|
---|
583 |
|
---|
584 | /* set last item */
|
---|
585 | usb_hid_report_set_last_item(usage_path,
|
---|
586 | USB_HID_TAG_CLASS_GLOBAL,
|
---|
587 | USB_HID_EXTENDED_USAGE_PAGE(report_item->usages[report_item->usages_count - 1]));
|
---|
588 |
|
---|
589 | usb_hid_report_set_last_item(usage_path,
|
---|
590 | USB_HID_TAG_CLASS_LOCAL,
|
---|
591 | USB_HID_EXTENDED_USAGE(report_item->usages[report_item->usages_count - 1]));
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * append the new one which will be set by common usage/usage
|
---|
595 | * page
|
---|
596 | */
|
---|
597 | usb_hid_report_path_append_item(usage_path,
|
---|
598 | report_item->usage_page,
|
---|
599 | report_item->usages[report_item->usages_count - 1]);
|
---|
600 |
|
---|
601 | usb_hid_report_reset_local_items (report_item);
|
---|
602 | return USB_HID_NO_ACTION;
|
---|
603 | break;
|
---|
604 |
|
---|
605 | case USB_HID_REPORT_TAG_END_COLLECTION:
|
---|
606 | usb_hid_report_remove_last_item(usage_path);
|
---|
607 | return USB_HID_NO_ACTION;
|
---|
608 | break;
|
---|
609 |
|
---|
610 | default:
|
---|
611 | return USB_HID_NO_ACTION;
|
---|
612 | }
|
---|
613 |
|
---|
614 | return 0;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * Parse global tags of report descriptor
|
---|
619 | *
|
---|
620 | * @param Tag identifier
|
---|
621 | * @param Data buffer
|
---|
622 | * @param Length of data buffer
|
---|
623 | * @param Current state table
|
---|
624 | * @return 0 or USB_HID_ code
|
---|
625 | */
|
---|
626 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data,
|
---|
627 | size_t item_size, usb_hid_report_item_t *report_item,
|
---|
628 | usb_hid_report_path_t *usage_path)
|
---|
629 | {
|
---|
630 |
|
---|
631 | switch (tag) {
|
---|
632 | case USB_HID_REPORT_TAG_USAGE_PAGE:
|
---|
633 | report_item->usage_page =
|
---|
634 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
635 | break;
|
---|
636 |
|
---|
637 | case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
|
---|
638 | report_item->logical_minimum = USB_HID_UINT32_TO_INT32(
|
---|
639 | usb_hid_report_tag_data_uint32(data, item_size),
|
---|
640 | item_size * 8);
|
---|
641 | break;
|
---|
642 |
|
---|
643 | case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
|
---|
644 | report_item->logical_maximum = USB_HID_UINT32_TO_INT32(
|
---|
645 | usb_hid_report_tag_data_uint32(data, item_size),
|
---|
646 | item_size * 8);
|
---|
647 | break;
|
---|
648 |
|
---|
649 | case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
|
---|
650 | report_item->physical_minimum = USB_HID_UINT32_TO_INT32(
|
---|
651 | usb_hid_report_tag_data_uint32(data, item_size),
|
---|
652 | item_size * 8);
|
---|
653 | break;
|
---|
654 |
|
---|
655 | case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
|
---|
656 | report_item->physical_maximum = USB_HID_UINT32_TO_INT32(
|
---|
657 | usb_hid_report_tag_data_uint32(data, item_size),
|
---|
658 | item_size * 8);
|
---|
659 | break;
|
---|
660 |
|
---|
661 | case USB_HID_REPORT_TAG_UNIT_EXPONENT:
|
---|
662 | report_item->unit_exponent = usb_hid_report_tag_data_uint32(
|
---|
663 | data, item_size);
|
---|
664 | break;
|
---|
665 |
|
---|
666 | case USB_HID_REPORT_TAG_UNIT:
|
---|
667 | report_item->unit = usb_hid_report_tag_data_uint32(
|
---|
668 | data, item_size);
|
---|
669 | break;
|
---|
670 |
|
---|
671 | case USB_HID_REPORT_TAG_REPORT_SIZE:
|
---|
672 | report_item->size = usb_hid_report_tag_data_uint32(
|
---|
673 | data, item_size);
|
---|
674 | break;
|
---|
675 |
|
---|
676 | case USB_HID_REPORT_TAG_REPORT_COUNT:
|
---|
677 | report_item->count = usb_hid_report_tag_data_uint32(
|
---|
678 | data, item_size);
|
---|
679 | break;
|
---|
680 |
|
---|
681 | case USB_HID_REPORT_TAG_REPORT_ID:
|
---|
682 | report_item->id = usb_hid_report_tag_data_uint32(data,
|
---|
683 | item_size);
|
---|
684 | return USB_HID_RESET_OFFSET;
|
---|
685 | break;
|
---|
686 |
|
---|
687 | case USB_HID_REPORT_TAG_PUSH:
|
---|
688 | case USB_HID_REPORT_TAG_POP:
|
---|
689 | /*
|
---|
690 | * stack operations are done in top level parsing
|
---|
691 | * function
|
---|
692 | */
|
---|
693 | return tag;
|
---|
694 | break;
|
---|
695 |
|
---|
696 | default:
|
---|
697 | return USB_HID_NO_ACTION;
|
---|
698 | }
|
---|
699 |
|
---|
700 | return 0;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * Parse local tags of report descriptor
|
---|
705 | *
|
---|
706 | * @param Tag identifier
|
---|
707 | * @param Data buffer
|
---|
708 | * @param Length of data buffer
|
---|
709 | * @param Current state table
|
---|
710 | * @return 0 or USB_HID_ code
|
---|
711 | */
|
---|
712 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data,
|
---|
713 | size_t item_size, usb_hid_report_item_t *report_item,
|
---|
714 | usb_hid_report_path_t *usage_path)
|
---|
715 | {
|
---|
716 | int32_t extended_usage;
|
---|
717 |
|
---|
718 | switch (tag) {
|
---|
719 | case USB_HID_REPORT_TAG_USAGE:
|
---|
720 | switch (report_item->in_delimiter) {
|
---|
721 | case INSIDE_DELIMITER_SET:
|
---|
722 | /*
|
---|
723 | * Nothing to do.
|
---|
724 | * We catch only the first one
|
---|
725 | */
|
---|
726 | break;
|
---|
727 |
|
---|
728 | case START_DELIMITER_SET:
|
---|
729 | report_item->in_delimiter = INSIDE_DELIMITER_SET;
|
---|
730 | /* Fallthrough */
|
---|
731 | case OUTSIDE_DELIMITER_SET:
|
---|
732 | extended_usage = ((report_item->usage_page) << 16);
|
---|
733 | extended_usage +=
|
---|
734 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
735 |
|
---|
736 | report_item->usages[report_item->usages_count] =
|
---|
737 | extended_usage;
|
---|
738 |
|
---|
739 | report_item->usages_count++;
|
---|
740 | break;
|
---|
741 | }
|
---|
742 | break;
|
---|
743 |
|
---|
744 | case USB_HID_REPORT_TAG_USAGE_MINIMUM:
|
---|
745 | if (item_size == 3) {
|
---|
746 | /* Usage extended usages */
|
---|
747 | report_item->extended_usage_page =
|
---|
748 | USB_HID_EXTENDED_USAGE_PAGE(
|
---|
749 | usb_hid_report_tag_data_uint32(data, item_size));
|
---|
750 |
|
---|
751 | report_item->usage_minimum =
|
---|
752 | USB_HID_EXTENDED_USAGE(
|
---|
753 | usb_hid_report_tag_data_uint32(data, item_size));
|
---|
754 | } else {
|
---|
755 | report_item->usage_minimum =
|
---|
756 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
757 | }
|
---|
758 | break;
|
---|
759 |
|
---|
760 | case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
|
---|
761 | if (item_size == 3) {
|
---|
762 | if (report_item->extended_usage_page !=
|
---|
763 | USB_HID_EXTENDED_USAGE_PAGE(
|
---|
764 | usb_hid_report_tag_data_uint32(data, item_size))) {
|
---|
765 | return USB_HID_INVALID;
|
---|
766 | }
|
---|
767 |
|
---|
768 | /* Usage extended usages */
|
---|
769 | report_item->extended_usage_page =
|
---|
770 | USB_HID_EXTENDED_USAGE_PAGE(
|
---|
771 | usb_hid_report_tag_data_uint32(data, item_size));
|
---|
772 |
|
---|
773 | report_item->usage_maximum =
|
---|
774 | USB_HID_EXTENDED_USAGE(
|
---|
775 | usb_hid_report_tag_data_uint32(data, item_size));
|
---|
776 | } else {
|
---|
777 | report_item->usage_maximum =
|
---|
778 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /* Put the records into the usages array */
|
---|
782 | for (int32_t i = report_item->usage_minimum;
|
---|
783 | i <= report_item->usage_maximum; i++) {
|
---|
784 |
|
---|
785 | if (report_item->extended_usage_page) {
|
---|
786 | report_item->usages[report_item->usages_count++] =
|
---|
787 | (report_item->extended_usage_page << 16) + i;
|
---|
788 | } else {
|
---|
789 | report_item->usages[report_item->usages_count++] =
|
---|
790 | (report_item->usage_page << 16) + i;
|
---|
791 | }
|
---|
792 | }
|
---|
793 | report_item->extended_usage_page = 0;
|
---|
794 |
|
---|
795 | break;
|
---|
796 |
|
---|
797 | case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
|
---|
798 | report_item->designator_index =
|
---|
799 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
800 | break;
|
---|
801 |
|
---|
802 | case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
|
---|
803 | report_item->designator_minimum =
|
---|
804 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
805 | break;
|
---|
806 |
|
---|
807 | case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
|
---|
808 | report_item->designator_maximum =
|
---|
809 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
810 | break;
|
---|
811 |
|
---|
812 | case USB_HID_REPORT_TAG_STRING_INDEX:
|
---|
813 | report_item->string_index =
|
---|
814 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
815 | break;
|
---|
816 |
|
---|
817 | case USB_HID_REPORT_TAG_STRING_MINIMUM:
|
---|
818 | report_item->string_minimum =
|
---|
819 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
820 | break;
|
---|
821 |
|
---|
822 | case USB_HID_REPORT_TAG_STRING_MAXIMUM:
|
---|
823 | report_item->string_maximum =
|
---|
824 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
825 | break;
|
---|
826 |
|
---|
827 | case USB_HID_REPORT_TAG_DELIMITER:
|
---|
828 | report_item->in_delimiter =
|
---|
829 | usb_hid_report_tag_data_uint32(data, item_size);
|
---|
830 | break;
|
---|
831 |
|
---|
832 | default:
|
---|
833 | return USB_HID_NO_ACTION;
|
---|
834 | }
|
---|
835 |
|
---|
836 | return 0;
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Converts raw data to uint32 (thats the maximum length of short item data)
|
---|
841 | *
|
---|
842 | * @param Data buffer
|
---|
843 | * @param Size of buffer
|
---|
844 | * @return Converted int32 number
|
---|
845 | */
|
---|
846 | uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size)
|
---|
847 | {
|
---|
848 | unsigned int i;
|
---|
849 | uint32_t result;
|
---|
850 |
|
---|
851 | result = 0;
|
---|
852 | for (i = 0; i < size; i++) {
|
---|
853 | result = (result | (data[i]) << (i * 8));
|
---|
854 | }
|
---|
855 |
|
---|
856 | return result;
|
---|
857 | }
|
---|
858 |
|
---|
859 | /**
|
---|
860 | * Prints content of given list of report items.
|
---|
861 | *
|
---|
862 | * @param List of report items (usb_hid_report_item_t)
|
---|
863 | * @return void
|
---|
864 | */
|
---|
865 | void usb_hid_descriptor_print_list(list_t *list)
|
---|
866 | {
|
---|
867 | if (list == NULL || list_empty(list)) {
|
---|
868 | usb_log_debug("\tempty");
|
---|
869 | return;
|
---|
870 | }
|
---|
871 |
|
---|
872 | list_foreach(*list, ritems_link, usb_hid_report_field_t,
|
---|
873 | report_item) {
|
---|
874 | usb_log_debug("\t\tOFFSET: %u", report_item->offset);
|
---|
875 | usb_log_debug("\t\tSIZE: %zu", report_item->size);
|
---|
876 | usb_log_debug("\t\tLOGMIN: %d",
|
---|
877 | report_item->logical_minimum);
|
---|
878 | usb_log_debug("\t\tLOGMAX: %d",
|
---|
879 | report_item->logical_maximum);
|
---|
880 | usb_log_debug("\t\tPHYMIN: %d",
|
---|
881 | report_item->physical_minimum);
|
---|
882 | usb_log_debug("\t\tPHYMAX: %d",
|
---|
883 | report_item->physical_maximum);
|
---|
884 | usb_log_debug("\t\ttUSAGEMIN: %X",
|
---|
885 | report_item->usage_minimum);
|
---|
886 | usb_log_debug("\t\tUSAGEMAX: %X",
|
---|
887 | report_item->usage_maximum);
|
---|
888 | usb_log_debug("\t\tUSAGES COUNT: %zu",
|
---|
889 | report_item->usages_count);
|
---|
890 |
|
---|
891 | usb_log_debug("\t\tVALUE: %X", report_item->value);
|
---|
892 | usb_log_debug("\t\ttUSAGE: %X", report_item->usage);
|
---|
893 | usb_log_debug("\t\tUSAGE PAGE: %X", report_item->usage_page);
|
---|
894 |
|
---|
895 | usb_hid_print_usage_path(report_item->collection_path);
|
---|
896 | }
|
---|
897 | }
|
---|
898 |
|
---|
899 | /**
|
---|
900 | * Prints content of given report descriptor in human readable format.
|
---|
901 | *
|
---|
902 | * @param parser Parsed descriptor to print
|
---|
903 | * @return void
|
---|
904 | */
|
---|
905 | void usb_hid_descriptor_print(usb_hid_report_t *report)
|
---|
906 | {
|
---|
907 | if (report == NULL)
|
---|
908 | return;
|
---|
909 |
|
---|
910 | list_foreach(report->reports, reports_link,
|
---|
911 | usb_hid_report_description_t, report_des) {
|
---|
912 | usb_log_debug("Report ID: %d", report_des->report_id);
|
---|
913 | usb_log_debug("\tType: %d", report_des->type);
|
---|
914 | usb_log_debug("\tLength: %zu", report_des->bit_length);
|
---|
915 | usb_log_debug("\tB Size: %zu",
|
---|
916 | usb_hid_report_byte_size(report,
|
---|
917 | report_des->report_id,
|
---|
918 | report_des->type));
|
---|
919 | usb_log_debug("\tItems: %zu", report_des->item_length);
|
---|
920 |
|
---|
921 | usb_hid_descriptor_print_list(&report_des->report_items);
|
---|
922 | }
|
---|
923 | }
|
---|
924 |
|
---|
925 | /** Frees the HID report descriptor parser structure
|
---|
926 | *
|
---|
927 | * @param parser Opaque HID report parser structure
|
---|
928 | * @return void
|
---|
929 | */
|
---|
930 | void usb_hid_report_deinit(usb_hid_report_t *report)
|
---|
931 | {
|
---|
932 | if (report == NULL) {
|
---|
933 | return;
|
---|
934 | }
|
---|
935 |
|
---|
936 | // free collection paths
|
---|
937 | link_t *path_link;
|
---|
938 | usb_hid_report_path_t *path;
|
---|
939 | while (!list_empty(&report->collection_paths)) {
|
---|
940 | path_link = list_first(&report->collection_paths);
|
---|
941 | path = list_get_instance(path_link,
|
---|
942 | usb_hid_report_path_t, cpath_link);
|
---|
943 |
|
---|
944 | list_remove(path_link);
|
---|
945 | usb_hid_report_path_free(path);
|
---|
946 | }
|
---|
947 |
|
---|
948 | // free report items
|
---|
949 | usb_hid_report_description_t *report_des;
|
---|
950 | usb_hid_report_field_t *field;
|
---|
951 | while (!list_empty(&report->reports)) {
|
---|
952 | report_des = list_get_instance(list_first(&report->reports),
|
---|
953 | usb_hid_report_description_t, reports_link);
|
---|
954 |
|
---|
955 | list_remove(&report_des->reports_link);
|
---|
956 |
|
---|
957 | while (!list_empty(&report_des->report_items)) {
|
---|
958 | field = list_get_instance(
|
---|
959 | list_first(&report_des->report_items),
|
---|
960 | usb_hid_report_field_t, ritems_link);
|
---|
961 |
|
---|
962 | list_remove(&field->ritems_link);
|
---|
963 |
|
---|
964 | free(field);
|
---|
965 | }
|
---|
966 |
|
---|
967 | free(report_des);
|
---|
968 | }
|
---|
969 |
|
---|
970 | return;
|
---|
971 | }
|
---|
972 |
|
---|
973 | /**
|
---|
974 | * @}
|
---|
975 | */
|
---|