source: mainline/uspace/lib/usbhid/src/hiddescriptor.c@ 92cb73f

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

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 24.9 KB
RevLine 
[9d05599]1/*
2 * Copyright (c) 2011 Matej Klonfar
[e0a5d4c]3 * Copyright (c) 2018 Ondrej Hlavaty
[9d05599]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
[160b75e]30/** @addtogroup libusbhid
[9d05599]31 * @{
32 */
33/** @file
34 * HID report descriptor and report data parser implementation.
35 */
[faa44e58]36#include <usb/hid/hidparser.h>
[9d05599]37#include <errno.h>
38#include <stdio.h>
39#include <mem.h>
40#include <usb/debug.h>
41#include <assert.h>
[38d150e]42#include <stdlib.h>
[9d05599]43
[a76b01b4]44
[f3b39b4]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 */
[22ded10]52#define OUTSIDE_DELIMITER_SET 0
[f3b39b4]53
54/**
55 * First delimiter tag was read. The set of local items (usage) started.
56 */
[22ded10]57#define START_DELIMITER_SET 1
[f3b39b4]58
59/**
60 * Parser is in the set of local items.
61 */
[22ded10]62#define INSIDE_DELIMITER_SET 2
[f3b39b4]63
[a76b01b4]64
[a35b458]65
[9d05599]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
[3ca4ae9]78#define USB_HID_INVALID -98
[9d05599]79/** Unknown tag was founded in report descriptor data*/
80#define USB_HID_UNKNOWN_TAG -99
81
[a76b01b4]82
[f3b39b4]83/**
84 * Checks if given collection path is already present in report structure and
85 * inserts it if not.
86 *
[1b20da0]87 * @param report Report structure
88 * @param cmp_path The collection path
[f3b39b4]89 * @return Pointer to the result collection path in report structure.
90 * @retval NULL If some error occurs
91 */
[b72efe8]92usb_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;
[1519b91]96 usb_hid_report_path_t *path = NULL;
[a35b458]97
[3bacee1]98 if ((report == NULL) || (cmp_path == NULL)) {
[f3b39b4]99 return NULL;
100 }
[a35b458]101
[3bacee1]102 while (path_it != &report->collection_paths.head) {
[f3b39b4]103 path = list_get_instance(path_it, usb_hid_report_path_t,
[3bacee1]104 cpath_link);
[a35b458]105
[3bacee1]106 if (usb_hid_report_compare_usage_path(path, cmp_path,
107 USB_HID_PATH_COMPARE_STRICT) == 0) {
[1519b91]108 break;
[b72efe8]109 }
[1519b91]110 path_it = path_it->next;
111 }
[3bacee1]112 if (path_it == &report->collection_paths.head) {
[f3b39b4]113 path = usb_hid_report_path_clone(cmp_path);
[3bacee1]114 if (path == NULL) {
[f3b39b4]115 return NULL;
116 }
[b72efe8]117 list_append(&path->cpath_link, &report->collection_paths);
[1519b91]118 report->collection_paths_count++;
[b9d7965]119
120 return path;
[3bacee1]121 } else {
[f3b39b4]122 return list_get_instance(path_it, usb_hid_report_path_t,
[3bacee1]123 cpath_link);
[1519b91]124 }
125}
[9d05599]126
[a76b01b4]127
[9d05599]128/**
129 * Initialize the report descriptor parser structure
130 *
131 * @param parser Report descriptor parser structure
132 * @return Error code
[f3b39b4]133 * @retval EINVAL If no report structure was given
134 * @retval EOK If report structure was successfully initialized
[9d05599]135 */
[5a6cc679]136errno_t usb_hid_report_init(usb_hid_report_t *report)
[9d05599]137{
[8c62a71]138 if (report == NULL) {
[9d05599]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;
[3bacee1]147 return EOK;
[9d05599]148}
149
[a76b01b4]150
[3b5d5b9d]151
[f3b39b4]152/**
[3b5d5b9d]153 *
154 *
[f3b39b4]155 * @param report Report structure in which the new report items should be
156 * stored
[1b20da0]157 * @param report_item Current report descriptor's parsing state table
[f3b39b4]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 *
[3b5d5b9d]163 */
[5a6cc679]164errno_t usb_hid_report_append_fields(usb_hid_report_t *report,
[3bacee1]165 usb_hid_report_item_t *report_item)
166{
[f3b39b4]167
[9d05599]168 usb_hid_report_field_t *field;
169 int i;
170
[3a6e423]171 uint32_t *usages;
[3bacee1]172 int usages_used = 0;
[f3b39b4]173
[3bacee1]174 if ((report == NULL) || (report_item == NULL)) {
[f3b39b4]175 return EINVAL;
176 }
177
[3bacee1]178 if (report_item->usages_count > 0) {
[806a779]179 usages = malloc(sizeof(uint32_t) * report_item->usages_count);
[f3b39b4]180 memcpy(usages, report_item->usages, sizeof(int32_t) *
[3bacee1]181 report_item->usages_count);
182 } else {
[3a6e423]183 usages = NULL;
184 }
[a35b458]185
[1b20da0]186 usb_hid_report_path_t *path = report_item->usage_path;
[3bacee1]187 for (i = 0; i < report_item->count; i++) {
[9d05599]188
189 field = malloc(sizeof(usb_hid_report_field_t));
[3bacee1]190 if (field == NULL) {
[574f276]191 return ENOMEM;
192 }
193
[9d05599]194 memset(field, 0, sizeof(usb_hid_report_field_t));
[b72efe8]195 link_initialize(&field->ritems_link);
[9d05599]196
[1b20da0]197 /* fill the attributes */
[9d05599]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
[3bacee1]203 if (USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
[1b20da0]204 /*
[47e00b83]205 * Store usage array. The Correct Usage Page and Usage is
206 * depending on data in report and will be filled later
207 */
[3a6e423]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;
[3bacee1]214 } else {
[9d05599]215
[3a6e423]216 /* Fill the correct Usage and Usage Page */
217 int32_t usage;
[3bacee1]218 if (i < report_item->usages_count) {
[3b5d5b9d]219 usage = report_item->usages[i];
[3bacee1]220 } else {
221 usage = report_item->usages[report_item->usages_count - 1];
[9d05599]222 }
223
[3bacee1]224 if (USB_HID_IS_EXTENDED_USAGE(usage)) {
[3a6e423]225 field->usage = USB_HID_EXTENDED_USAGE(usage);
[1b20da0]226 field->usage_page =
[3bacee1]227 USB_HID_EXTENDED_USAGE_PAGE(usage);
228 } else {
[3a6e423]229 // should not occur
[9d05599]230 field->usage = usage;
[3a6e423]231 field->usage_page = report_item->usage_page;
[9d05599]232 }
233 }
[a35b458]234
[f3b39b4]235 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL,
[3bacee1]236 field->usage_page);
[f3b39b4]237 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL,
[3bacee1]238 field->usage);
[1519b91]239
[f3b39b4]240 field->collection_path =
[3bacee1]241 usb_hid_report_path_try_insert(report, path);
[1519b91]242
[9d05599]243 field->size = report_item->size;
[ee7e7c93]244
[6283cefb]245 field->offset = report_item->offset + (i * report_item->size);
[ee7e7c93]246
[3bacee1]247 if (report->use_report_ids != 0) {
[9d05599]248 field->offset += 8;
249 report->use_report_ids = 1;
250 }
[ee7e7c93]251
[9d05599]252 field->item_flags = report_item->item_flags;
253
254 /* find the right report list*/
255 usb_hid_report_description_t *report_des;
[f3b39b4]256 report_des = usb_hid_report_find_description(report,
[3bacee1]257 report_item->id, report_item->type);
[a35b458]258
[3bacee1]259 if (report_des == NULL) {
[f3b39b4]260 report_des = malloc(
[3bacee1]261 sizeof(usb_hid_report_description_t));
262 if (report_des == NULL) {
[f3b39b4]263 return ENOMEM;
264 }
265
266 memset(report_des, 0,
[3bacee1]267 sizeof(usb_hid_report_description_t));
[9d05599]268
269 report_des->type = report_item->type;
270 report_des->report_id = report_item->id;
[3bacee1]271 if (report_des->report_id != 0) {
[1432fcf3]272 /* set up the bit length by report_id field */
273 report_des->bit_length = 8;
274 }
275
[b72efe8]276 link_initialize (&report_des->reports_link);
[9d05599]277 list_initialize (&report_des->report_items);
278
[b72efe8]279 list_append(&report_des->reports_link, &report->reports);
[9d05599]280 report->report_count++;
281 }
282
283 /* append this field to the end of founded report list */
[b72efe8]284 list_append(&field->ritems_link, &report_des->report_items);
[a35b458]285
[9d05599]286 /* update the sizes */
287 report_des->bit_length += field->size;
288 report_des->item_length++;
289
290 }
291
[3a6e423]292 // free only when not used!!!
[3bacee1]293 if (usages && usages_used == 0) {
[3a6e423]294 free(usages);
295 }
[9d05599]296
297 return EOK;
298}
[a76b01b4]299
[f3b39b4]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 */
[3bacee1]310usb_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{
[9d05599]314
[3bacee1]315 if (report == NULL) {
[0dd3e49]316 return NULL;
317 }
318
[feeac0d]319 list_foreach(report->reports, reports_link,
320 usb_hid_report_description_t, report_des) {
[dcb7d7cd]321 // if report id not set, return the first of the type
[3bacee1]322 if (((report_des->report_id == report_id) || (report_id == 0)) &&
323 (report_des->type == type)) {
[9d05599]324 return report_des;
325 }
326 }
327
328 return NULL;
329}
[a76b01b4]330
[9d05599]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.
[f3b39b4]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
[9d05599]340 */
[1b20da0]341errno_t usb_hid_parse_report_descriptor(usb_hid_report_t *report,
[9d05599]342 const uint8_t *data, size_t size)
343{
[3bacee1]344 size_t i = 0;
345 uint8_t tag = 0;
346 uint8_t item_size = 0;
347 int class = 0;
[9d05599]348 int ret;
[3bacee1]349 usb_hid_report_item_t *report_item = 0;
[1b20da0]350 usb_hid_report_item_t *new_report_item;
[9d05599]351 usb_hid_report_path_t *usage_path;
352
[3bacee1]353 size_t offset_input = 0;
354 size_t offset_output = 0;
355 size_t offset_feature = 0;
[a35b458]356
[b72efe8]357 link_t *item_link;
[9d05599]358
[b72efe8]359 list_t stack;
360 list_initialize(&stack);
[9d05599]361
362 /* parser structure initialization*/
[3bacee1]363 if (usb_hid_report_init(report) != EOK) {
[9d05599]364 return EINVAL;
365 }
[a35b458]366
[9d05599]367 /*report item initialization*/
[3bacee1]368 if (!(report_item = malloc(sizeof(usb_hid_report_item_t)))) {
[9d05599]369 return ENOMEM;
370 }
371 memset(report_item, 0, sizeof(usb_hid_report_item_t));
[b72efe8]372 link_initialize(&(report_item->link));
[9d05599]373
374 /* usage path context initialization */
[3bacee1]375 if (!(usage_path = usb_hid_report_path())) {
[9d05599]376 return ENOMEM;
377 }
[1b20da0]378 usb_hid_report_path_append_item(usage_path, 0, 0);
[a35b458]379
[3bacee1]380 while (i < size) {
381 if (!USB_HID_ITEM_IS_LONG(data[i])) {
[9d05599]382
[3bacee1]383 if ((i + USB_HID_ITEM_SIZE(data[i])) >= size) {
[9d05599]384 return EINVAL;
385 }
[a35b458]386
[9d05599]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]);
[a35b458]390
[3bacee1]391 ret = usb_hid_report_parse_tag(tag, class, data + i + 1,
392 item_size, report_item, usage_path);
[f3b39b4]393
[3bacee1]394 switch (ret) {
[f3b39b4]395 case USB_HID_NEW_REPORT_ITEM:
[7c3fb9b]396 /*
397 * store report item to report and create the
[f3b39b4]398 * new one store current collection path
399 */
400 report_item->usage_path = usage_path;
[a35b458]401
[f3b39b4]402 usb_hid_report_path_set_report_id(
[3bacee1]403 report_item->usage_path, report_item->id);
[a35b458]404
[3bacee1]405 if (report_item->id != 0) {
[f3b39b4]406 report->use_report_ids = 1;
407 }
[a35b458]408
[3bacee1]409 switch (tag) {
[f3b39b4]410 case USB_HID_REPORT_TAG_INPUT:
[1b20da0]411 report_item->type =
[f3b39b4]412 USB_HID_REPORT_TYPE_INPUT;
413
414 report_item->offset = offset_input;
[1b20da0]415 offset_input += report_item->count *
[f3b39b4]416 report_item->size;
417 break;
[a35b458]418
[f3b39b4]419 case USB_HID_REPORT_TAG_OUTPUT:
[1b20da0]420 report_item->type =
[f3b39b4]421 USB_HID_REPORT_TYPE_OUTPUT;
[a35b458]422
[f3b39b4]423 report_item->offset = offset_output;
[1b20da0]424 offset_output += report_item->count *
[f3b39b4]425 report_item->size;
[9d05599]426 break;
[a35b458]427
[f3b39b4]428 case USB_HID_REPORT_TAG_FEATURE:
[1b20da0]429 report_item->type =
[f3b39b4]430 USB_HID_REPORT_TYPE_FEATURE;
[9d05599]431
[f3b39b4]432 report_item->offset = offset_feature;
[1b20da0]433 offset_feature += report_item->count *
[3bacee1]434 report_item->size;
[9d05599]435 break;
[a35b458]436
[f3b39b4]437 default:
438 usb_log_debug2(
439 "\tjump over - tag %X\n", tag);
[3bacee1]440 break;
[f3b39b4]441 }
[a35b458]442
[1b20da0]443 /*
444 * append new fields to the report structure
[f3b39b4]445 */
[1b20da0]446 usb_hid_report_append_fields(report,
[f3b39b4]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;
[1b20da0]457 usb_hid_report_path_set_report_id (usage_path,
[f3b39b4]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);
[a35b458]465
[1b20da0]466 usb_hid_report_path_t *tmp_path =
[f3b39b4]467 usb_hid_report_path_clone(usage_path);
[9d05599]468
[1b20da0]469 new_report_item->usage_path = tmp_path;
[9d05599]470
[f3b39b4]471 list_prepend (&new_report_item->link, &stack);
472 break;
473 case USB_HID_REPORT_TAG_POP:
474 // restore current state from stack
[b72efe8]475 item_link = list_first(&stack);
476 if (item_link == NULL) {
[f3b39b4]477 return EINVAL;
478 }
479 free(report_item);
[a35b458]480
[b72efe8]481 report_item = list_get_instance(item_link,
[f3b39b4]482 usb_hid_report_item_t, link);
[a35b458]483
[f3b39b4]484 usb_hid_report_usage_path_t *tmp_usage_path;
485 tmp_usage_path = list_get_instance(
[b72efe8]486 report_item->usage_path->cpath_link.prev,
487 usb_hid_report_usage_path_t, rpath_items_link);
[a35b458]488
[1b20da0]489 usb_hid_report_set_last_item(usage_path,
[f3b39b4]490 USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
[a35b458]491
[1b20da0]492 usb_hid_report_set_last_item(usage_path,
[f3b39b4]493 USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
[9d05599]494
[f3b39b4]495 usb_hid_report_path_free(report_item->usage_path);
[b72efe8]496 list_remove (item_link);
[a35b458]497
[f3b39b4]498 break;
[a35b458]499
[f3b39b4]500 default:
[1b20da0]501 // nothing special to do
[f3b39b4]502 break;
[9d05599]503 }
504
505 /* jump over the processed block */
506 i += 1 + USB_HID_ITEM_SIZE(data[i]);
[3bacee1]507 } else {
[9d05599]508 // TBD
[3bacee1]509 i += 3 + USB_HID_ITEM_SIZE(data[i + 1]);
[9d05599]510 }
[a35b458]511
[9d05599]512
513 }
[a35b458]514
[9d05599]515 return EOK;
516}
517
[a76b01b4]518
[9d05599]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 */
[f3b39b4]529int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data,
[3bacee1]530 size_t item_size, usb_hid_report_item_t *report_item,
531 usb_hid_report_path_t *usage_path)
532{
[a35b458]533
[9d05599]534 int ret;
[a35b458]535
[3bacee1]536 switch (class) {
[f3b39b4]537 case USB_HID_TAG_CLASS_MAIN:
[9d05599]538
[3bacee1]539 if ((ret = usb_hid_report_parse_main_tag(tag, data, item_size,
540 report_item, usage_path)) == 0) {
[9d05599]541
[f3b39b4]542 return USB_HID_NEW_REPORT_ITEM;
[3bacee1]543 } else {
[f3b39b4]544 return ret;
545 }
546 break;
[9d05599]547
[1b20da0]548 case USB_HID_TAG_CLASS_GLOBAL:
[f3b39b4]549 return usb_hid_report_parse_global_tag(tag, data, item_size,
[3bacee1]550 report_item, usage_path);
[f3b39b4]551 break;
552
[1b20da0]553 case USB_HID_TAG_CLASS_LOCAL:
[f3b39b4]554 return usb_hid_report_parse_local_tag(tag, data, item_size,
[3bacee1]555 report_item, usage_path);
[f3b39b4]556 break;
[a35b458]557
[f3b39b4]558 default:
559 return USB_HID_NO_ACTION;
[9d05599]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
[3ca4ae9]570 * @return 0 or USB_HID_ code
[9d05599]571 */
572
[1b20da0]573int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data,
[3bacee1]574 size_t item_size, usb_hid_report_item_t *report_item,
575 usb_hid_report_path_t *usage_path)
[674cf89]576{
577 usb_hid_report_usage_path_t *path_item;
[a35b458]578
[3bacee1]579 switch (tag) {
[f3b39b4]580 case USB_HID_REPORT_TAG_INPUT:
581 case USB_HID_REPORT_TAG_OUTPUT:
582 case USB_HID_REPORT_TAG_FEATURE:
[1b20da0]583 report_item->item_flags = *data;
584 return 0;
[f3b39b4]585 break;
[a35b458]586
[f3b39b4]587 case USB_HID_REPORT_TAG_COLLECTION:
[3a6e423]588
[f3b39b4]589 /* store collection atributes */
[b72efe8]590 path_item = list_get_instance(list_first(&usage_path->items),
[3bacee1]591 usb_hid_report_usage_path_t, rpath_items_link);
[b72efe8]592 path_item->flags = *data;
[a35b458]593
[f3b39b4]594 /* set last item */
[1b20da0]595 usb_hid_report_set_last_item(usage_path,
[3bacee1]596 USB_HID_TAG_CLASS_GLOBAL,
597 USB_HID_EXTENDED_USAGE_PAGE(report_item->usages[report_item->usages_count - 1]));
[f3b39b4]598
[1b20da0]599 usb_hid_report_set_last_item(usage_path,
[3bacee1]600 USB_HID_TAG_CLASS_LOCAL,
601 USB_HID_EXTENDED_USAGE(report_item->usages[report_item->usages_count - 1]));
[a35b458]602
[7c3fb9b]603 /*
604 * append the new one which will be set by common usage/usage
605 * page
606 */
[1b20da0]607 usb_hid_report_path_append_item(usage_path,
[3bacee1]608 report_item->usage_page,
609 report_item->usages[report_item->usages_count - 1]);
[f3b39b4]610
611 usb_hid_report_reset_local_items (report_item);
612 return USB_HID_NO_ACTION;
613 break;
[a35b458]614
[f3b39b4]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;
[9d05599]622 }
623
[3ca4ae9]624 return 0;
[9d05599]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
[3ca4ae9]634 * @return 0 or USB_HID_ code
[9d05599]635 */
[1b20da0]636int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data,
[3bacee1]637 size_t item_size, usb_hid_report_item_t *report_item,
638 usb_hid_report_path_t *usage_path)
639{
[a35b458]640
[3bacee1]641 switch (tag) {
[f3b39b4]642 case USB_HID_REPORT_TAG_USAGE_PAGE:
[1b20da0]643 report_item->usage_page =
[3bacee1]644 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]645 break;
646
647 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
648 report_item->logical_minimum = USB_HID_UINT32_TO_INT32(
[3bacee1]649 usb_hid_report_tag_data_uint32(data, item_size),
650 item_size * 8);
[f3b39b4]651 break;
652
653 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
654 report_item->logical_maximum = USB_HID_UINT32_TO_INT32(
[3bacee1]655 usb_hid_report_tag_data_uint32(data, item_size),
656 item_size * 8);
[f3b39b4]657 break;
658
659 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
660 report_item->physical_minimum = USB_HID_UINT32_TO_INT32(
[3bacee1]661 usb_hid_report_tag_data_uint32(data, item_size),
662 item_size * 8);
[1b20da0]663 break;
[f3b39b4]664
665 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
666 report_item->physical_maximum = USB_HID_UINT32_TO_INT32(
[3bacee1]667 usb_hid_report_tag_data_uint32(data, item_size),
668 item_size * 8);
[f3b39b4]669 break;
670
671 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
672 report_item->unit_exponent = usb_hid_report_tag_data_uint32(
[3bacee1]673 data, item_size);
[f3b39b4]674 break;
675
676 case USB_HID_REPORT_TAG_UNIT:
677 report_item->unit = usb_hid_report_tag_data_uint32(
[3bacee1]678 data, item_size);
[f3b39b4]679 break;
680
681 case USB_HID_REPORT_TAG_REPORT_SIZE:
682 report_item->size = usb_hid_report_tag_data_uint32(
[3bacee1]683 data, item_size);
[f3b39b4]684 break;
685
686 case USB_HID_REPORT_TAG_REPORT_COUNT:
687 report_item->count = usb_hid_report_tag_data_uint32(
[3bacee1]688 data, item_size);
[f3b39b4]689 break;
690
691 case USB_HID_REPORT_TAG_REPORT_ID:
[1b20da0]692 report_item->id = usb_hid_report_tag_data_uint32(data,
[3bacee1]693 item_size);
[f3b39b4]694 return USB_HID_RESET_OFFSET;
695 break;
[a35b458]696
[f3b39b4]697 case USB_HID_REPORT_TAG_PUSH:
698 case USB_HID_REPORT_TAG_POP:
[1b20da0]699 /*
[f3b39b4]700 * stack operations are done in top level parsing
701 * function
702 */
703 return tag;
704 break;
[a35b458]705
[f3b39b4]706 default:
707 return USB_HID_NO_ACTION;
[9d05599]708 }
709
[3ca4ae9]710 return 0;
[9d05599]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
[3ca4ae9]720 * @return 0 or USB_HID_ code
[9d05599]721 */
[e141281]722int 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)
[9d05599]725{
[3a6e423]726 int32_t extended_usage;
[a35b458]727
[e141281]728 switch (tag) {
[f3b39b4]729 case USB_HID_REPORT_TAG_USAGE:
[e141281]730 switch (report_item->in_delimiter) {
[f3b39b4]731 case INSIDE_DELIMITER_SET:
[e141281]732 /*
733 * Nothing to do.
734 * We catch only the first one
[f3b39b4]735 */
[1be39e9]736 break;
[a35b458]737
[f3b39b4]738 case START_DELIMITER_SET:
739 report_item->in_delimiter = INSIDE_DELIMITER_SET;
[dc12262]740 /* Fallthrough */
[f3b39b4]741 case OUTSIDE_DELIMITER_SET:
742 extended_usage = ((report_item->usage_page) << 16);
[e141281]743 extended_usage +=
744 usb_hid_report_tag_data_uint32(data, item_size);
[a35b458]745
[e141281]746 report_item->usages[report_item->usages_count] =
747 extended_usage;
[a35b458]748
[f3b39b4]749 report_item->usages_count++;
750 break;
751 }
752 break;
[a35b458]753
[e141281]754 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
[f3b39b4]755 if (item_size == 3) {
[e141281]756 /* Usage extended usages */
757 report_item->extended_usage_page =
[f3b39b4]758 USB_HID_EXTENDED_USAGE_PAGE(
[e141281]759 usb_hid_report_tag_data_uint32(data, item_size));
[a35b458]760
761
[e141281]762 report_item->usage_minimum =
[f3b39b4]763 USB_HID_EXTENDED_USAGE(
[e141281]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);
[f3b39b4]768 }
769 break;
[a35b458]770
[f3b39b4]771 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
772 if (item_size == 3) {
[e141281]773 if (report_item->extended_usage_page !=
774 USB_HID_EXTENDED_USAGE_PAGE(
775 usb_hid_report_tag_data_uint32(data, item_size))) {
[3ca4ae9]776 return USB_HID_INVALID;
[1be39e9]777 }
[a35b458]778
[e141281]779 /* Usage extended usages */
780 report_item->extended_usage_page =
781 USB_HID_EXTENDED_USAGE_PAGE(
[3bacee1]782 usb_hid_report_tag_data_uint32(data, item_size));
[a35b458]783
[e141281]784 report_item->usage_maximum =
785 USB_HID_EXTENDED_USAGE(
[3bacee1]786 usb_hid_report_tag_data_uint32(data, item_size));
[e141281]787 } else {
788 report_item->usage_maximum =
[3bacee1]789 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]790 }
[a35b458]791
[e141281]792 /* Put the records into the usages array */
793 for (int32_t i = report_item->usage_minimum;
[f3b39b4]794 i <= report_item->usage_maximum; i++) {
[a35b458]795
[e141281]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;
[f3b39b4]802 }
803 }
804 report_item->extended_usage_page = 0;
[a35b458]805
[f3b39b4]806 break;
[a35b458]807
[f3b39b4]808 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
[e141281]809 report_item->designator_index =
810 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]811 break;
[a35b458]812
[f3b39b4]813 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
[e141281]814 report_item->designator_minimum =
815 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]816 break;
[a35b458]817
[f3b39b4]818 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
[e141281]819 report_item->designator_maximum =
820 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]821 break;
[a35b458]822
[f3b39b4]823 case USB_HID_REPORT_TAG_STRING_INDEX:
[e141281]824 report_item->string_index =
825 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]826 break;
[a35b458]827
[f3b39b4]828 case USB_HID_REPORT_TAG_STRING_MINIMUM:
[e141281]829 report_item->string_minimum =
830 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]831 break;
[a35b458]832
[f3b39b4]833 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
[e141281]834 report_item->string_maximum =
835 usb_hid_report_tag_data_uint32(data, item_size);
836 break;
[a35b458]837
[f3b39b4]838 case USB_HID_REPORT_TAG_DELIMITER:
[e141281]839 report_item->in_delimiter =
840 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]841 break;
[a35b458]842
[f3b39b4]843 default:
844 return USB_HID_NO_ACTION;
[9d05599]845 }
[a35b458]846
[3ca4ae9]847 return 0;
[9d05599]848}
[a76b01b4]849
[9d05599]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 */
857uint32_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;
[3bacee1]863 for (i = 0; i < size; i++) {
864 result = (result | (data[i]) << (i * 8));
[9d05599]865 }
866
867 return result;
868}
[a76b01b4]869
[9d05599]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 */
[b72efe8]877void usb_hid_descriptor_print_list(list_t *list)
[9d05599]878{
[3bacee1]879 if (list == NULL || list_empty(list)) {
880 usb_log_debug("\tempty");
881 return;
[9d05599]882 }
[b72efe8]883
[3bacee1]884 list_foreach(*list, ritems_link, usb_hid_report_field_t,
885 report_item) {
[a1732929]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",
[3bacee1]889 report_item->logical_minimum);
[a1732929]890 usb_log_debug("\t\tLOGMAX: %d",
[3bacee1]891 report_item->logical_maximum);
[a1732929]892 usb_log_debug("\t\tPHYMIN: %d",
[3bacee1]893 report_item->physical_minimum);
[a1732929]894 usb_log_debug("\t\tPHYMAX: %d",
[3bacee1]895 report_item->physical_maximum);
[a1732929]896 usb_log_debug("\t\ttUSAGEMIN: %X",
[3bacee1]897 report_item->usage_minimum);
[a1732929]898 usb_log_debug("\t\tUSAGEMAX: %X",
[3bacee1]899 report_item->usage_maximum);
[a1732929]900 usb_log_debug("\t\tUSAGES COUNT: %zu",
[3bacee1]901 report_item->usages_count);
[9d05599]902
[a1732929]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);
[b72efe8]906
[3a6e423]907 usb_hid_print_usage_path(report_item->collection_path);
[9d05599]908 }
909}
[a76b01b4]910
[f3b39b4]911
[9d05599]912/**
913 * Prints content of given report descriptor in human readable format.
914 *
915 * @param parser Parsed descriptor to print
916 * @return void
917 */
918void usb_hid_descriptor_print(usb_hid_report_t *report)
919{
[feeac0d]920 if (report == NULL)
[9d05599]921 return;
922
[feeac0d]923 list_foreach(report->reports, reports_link,
924 usb_hid_report_description_t, report_des) {
[a1732929]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",
[3bacee1]929 usb_hid_report_byte_size(report,
930 report_des->report_id,
931 report_des->type));
[a1732929]932 usb_log_debug("\tItems: %zu", report_des->item_length);
[9d05599]933
934 usb_hid_descriptor_print_list(&report_des->report_items);
935 }
936}
[a76b01b4]937
[9d05599]938
939
[1b20da0]940/** Frees the HID report descriptor parser structure
[9d05599]941 *
942 * @param parser Opaque HID report parser structure
943 * @return void
944 */
[a8c4e871]945void usb_hid_report_deinit(usb_hid_report_t *report)
[9d05599]946{
[3bacee1]947 if (report == NULL) {
[9d05599]948 return;
949 }
950
951 // free collection paths
[b72efe8]952 link_t *path_link;
[9d05599]953 usb_hid_report_path_t *path;
[3bacee1]954 while (!list_empty(&report->collection_paths)) {
[b72efe8]955 path_link = list_first(&report->collection_paths);
956 path = list_get_instance(path_link,
957 usb_hid_report_path_t, cpath_link);
[f3b39b4]958
[b72efe8]959 list_remove(path_link);
960 usb_hid_report_path_free(path);
[9d05599]961 }
[a35b458]962
[9d05599]963 // free report items
964 usb_hid_report_description_t *report_des;
965 usb_hid_report_field_t *field;
[3bacee1]966 while (!list_empty(&report->reports)) {
[b72efe8]967 report_des = list_get_instance(list_first(&report->reports),
[3bacee1]968 usb_hid_report_description_t, reports_link);
[f3b39b4]969
[b72efe8]970 list_remove(&report_des->reports_link);
[a35b458]971
[3bacee1]972 while (!list_empty(&report_des->report_items)) {
[f3b39b4]973 field = list_get_instance(
[b72efe8]974 list_first(&report_des->report_items),
975 usb_hid_report_field_t, ritems_link);
[f3b39b4]976
[b72efe8]977 list_remove(&field->ritems_link);
[9d05599]978
979 free(field);
980 }
[a35b458]981
[9d05599]982 free(report_des);
983 }
[a35b458]984
[9d05599]985 return;
986}
[a76b01b4]987
[9d05599]988
989/**
990 * @}
991 */
Note: See TracBrowser for help on using the repository browser.