source: mainline/uspace/lib/usbhid/src/hiddescriptor.c

Last change on this file was d1582b50, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Fix spacing in single-line comments using latest ccheck

This found incorrectly formatted section comments (with blocks of
asterisks or dashes). I strongly believe against using section comments
but I am not simply removing them since that would probably be
controversial.

  • 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
[f3b39b4]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 */
[22ded10]51#define OUTSIDE_DELIMITER_SET 0
[f3b39b4]52
53/**
54 * First delimiter tag was read. The set of local items (usage) started.
55 */
[22ded10]56#define START_DELIMITER_SET 1
[f3b39b4]57
58/**
59 * Parser is in the set of local items.
60 */
[22ded10]61#define INSIDE_DELIMITER_SET 2
[f3b39b4]62
[9d05599]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
[3ca4ae9]75#define USB_HID_INVALID -98
[d1582b50]76/** Unknown tag was founded in report descriptor data */
[9d05599]77#define USB_HID_UNKNOWN_TAG -99
78
[f3b39b4]79/**
80 * Checks if given collection path is already present in report structure and
81 * inserts it if not.
82 *
[1b20da0]83 * @param report Report structure
84 * @param cmp_path The collection path
[f3b39b4]85 * @return Pointer to the result collection path in report structure.
86 * @retval NULL If some error occurs
87 */
[b72efe8]88usb_hid_report_path_t *usb_hid_report_path_try_insert(usb_hid_report_t *report,
89 usb_hid_report_path_t *cmp_path)
90{
91 link_t *path_it = report->collection_paths.head.next;
[1519b91]92 usb_hid_report_path_t *path = NULL;
[a35b458]93
[3bacee1]94 if ((report == NULL) || (cmp_path == NULL)) {
[f3b39b4]95 return NULL;
96 }
[a35b458]97
[3bacee1]98 while (path_it != &report->collection_paths.head) {
[f3b39b4]99 path = list_get_instance(path_it, usb_hid_report_path_t,
[3bacee1]100 cpath_link);
[a35b458]101
[3bacee1]102 if (usb_hid_report_compare_usage_path(path, cmp_path,
103 USB_HID_PATH_COMPARE_STRICT) == 0) {
[1519b91]104 break;
[b72efe8]105 }
[1519b91]106 path_it = path_it->next;
107 }
[3bacee1]108 if (path_it == &report->collection_paths.head) {
[f3b39b4]109 path = usb_hid_report_path_clone(cmp_path);
[3bacee1]110 if (path == NULL) {
[f3b39b4]111 return NULL;
112 }
[b72efe8]113 list_append(&path->cpath_link, &report->collection_paths);
[1519b91]114 report->collection_paths_count++;
[b9d7965]115
116 return path;
[3bacee1]117 } else {
[f3b39b4]118 return list_get_instance(path_it, usb_hid_report_path_t,
[3bacee1]119 cpath_link);
[1519b91]120 }
121}
[9d05599]122
123/**
124 * Initialize the report descriptor parser structure
125 *
126 * @param parser Report descriptor parser structure
127 * @return Error code
[f3b39b4]128 * @retval EINVAL If no report structure was given
129 * @retval EOK If report structure was successfully initialized
[9d05599]130 */
[5a6cc679]131errno_t usb_hid_report_init(usb_hid_report_t *report)
[9d05599]132{
[8c62a71]133 if (report == NULL) {
[9d05599]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;
[3bacee1]142 return EOK;
[9d05599]143}
144
[f3b39b4]145/**
[3b5d5b9d]146 *
147 *
[f3b39b4]148 * @param report Report structure in which the new report items should be
149 * stored
[1b20da0]150 * @param report_item Current report descriptor's parsing state table
[f3b39b4]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 *
[3b5d5b9d]156 */
[5a6cc679]157errno_t usb_hid_report_append_fields(usb_hid_report_t *report,
[3bacee1]158 usb_hid_report_item_t *report_item)
159{
[f3b39b4]160
[9d05599]161 usb_hid_report_field_t *field;
162 int i;
163
[3a6e423]164 uint32_t *usages;
[3bacee1]165 int usages_used = 0;
[f3b39b4]166
[3bacee1]167 if ((report == NULL) || (report_item == NULL)) {
[f3b39b4]168 return EINVAL;
169 }
170
[3bacee1]171 if (report_item->usages_count > 0) {
[806a779]172 usages = malloc(sizeof(uint32_t) * report_item->usages_count);
[f3b39b4]173 memcpy(usages, report_item->usages, sizeof(int32_t) *
[3bacee1]174 report_item->usages_count);
175 } else {
[3a6e423]176 usages = NULL;
177 }
[a35b458]178
[1b20da0]179 usb_hid_report_path_t *path = report_item->usage_path;
[3bacee1]180 for (i = 0; i < report_item->count; i++) {
[9d05599]181
182 field = malloc(sizeof(usb_hid_report_field_t));
[3bacee1]183 if (field == NULL) {
[574f276]184 return ENOMEM;
185 }
186
[9d05599]187 memset(field, 0, sizeof(usb_hid_report_field_t));
[b72efe8]188 link_initialize(&field->ritems_link);
[9d05599]189
[1b20da0]190 /* fill the attributes */
[9d05599]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
[3bacee1]196 if (USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
[1b20da0]197 /*
[47e00b83]198 * Store usage array. The Correct Usage Page and Usage is
199 * depending on data in report and will be filled later
200 */
[3a6e423]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;
[3bacee1]207 } else {
[9d05599]208
[3a6e423]209 /* Fill the correct Usage and Usage Page */
210 int32_t usage;
[3bacee1]211 if (i < report_item->usages_count) {
[3b5d5b9d]212 usage = report_item->usages[i];
[3bacee1]213 } else {
214 usage = report_item->usages[report_item->usages_count - 1];
[9d05599]215 }
216
[3bacee1]217 if (USB_HID_IS_EXTENDED_USAGE(usage)) {
[3a6e423]218 field->usage = USB_HID_EXTENDED_USAGE(usage);
[1b20da0]219 field->usage_page =
[3bacee1]220 USB_HID_EXTENDED_USAGE_PAGE(usage);
221 } else {
[3a6e423]222 // should not occur
[9d05599]223 field->usage = usage;
[3a6e423]224 field->usage_page = report_item->usage_page;
[9d05599]225 }
226 }
[a35b458]227
[f3b39b4]228 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL,
[3bacee1]229 field->usage_page);
[f3b39b4]230 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL,
[3bacee1]231 field->usage);
[1519b91]232
[f3b39b4]233 field->collection_path =
[3bacee1]234 usb_hid_report_path_try_insert(report, path);
[1519b91]235
[9d05599]236 field->size = report_item->size;
[ee7e7c93]237
[6283cefb]238 field->offset = report_item->offset + (i * report_item->size);
[ee7e7c93]239
[3bacee1]240 if (report->use_report_ids != 0) {
[9d05599]241 field->offset += 8;
242 report->use_report_ids = 1;
243 }
[ee7e7c93]244
[9d05599]245 field->item_flags = report_item->item_flags;
246
[d1582b50]247 /* find the right report list */
[9d05599]248 usb_hid_report_description_t *report_des;
[f3b39b4]249 report_des = usb_hid_report_find_description(report,
[3bacee1]250 report_item->id, report_item->type);
[a35b458]251
[3bacee1]252 if (report_des == NULL) {
[f3b39b4]253 report_des = malloc(
[3bacee1]254 sizeof(usb_hid_report_description_t));
255 if (report_des == NULL) {
[f3b39b4]256 return ENOMEM;
257 }
258
259 memset(report_des, 0,
[3bacee1]260 sizeof(usb_hid_report_description_t));
[9d05599]261
262 report_des->type = report_item->type;
263 report_des->report_id = report_item->id;
[3bacee1]264 if (report_des->report_id != 0) {
[1432fcf3]265 /* set up the bit length by report_id field */
266 report_des->bit_length = 8;
267 }
268
[b72efe8]269 link_initialize (&report_des->reports_link);
[9d05599]270 list_initialize (&report_des->report_items);
271
[b72efe8]272 list_append(&report_des->reports_link, &report->reports);
[9d05599]273 report->report_count++;
274 }
275
276 /* append this field to the end of founded report list */
[b72efe8]277 list_append(&field->ritems_link, &report_des->report_items);
[a35b458]278
[9d05599]279 /* update the sizes */
280 report_des->bit_length += field->size;
281 report_des->item_length++;
282
283 }
284
[3a6e423]285 // free only when not used!!!
[3bacee1]286 if (usages && usages_used == 0) {
[3a6e423]287 free(usages);
288 }
[9d05599]289
290 return EOK;
291}
[a76b01b4]292
[f3b39b4]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 */
[3bacee1]303usb_hid_report_description_t *usb_hid_report_find_description(
304 const usb_hid_report_t *report, uint8_t report_id,
305 usb_hid_report_type_t type)
306{
[9d05599]307
[3bacee1]308 if (report == NULL) {
[0dd3e49]309 return NULL;
310 }
311
[feeac0d]312 list_foreach(report->reports, reports_link,
313 usb_hid_report_description_t, report_des) {
[dcb7d7cd]314 // if report id not set, return the first of the type
[3bacee1]315 if (((report_des->report_id == report_id) || (report_id == 0)) &&
316 (report_des->type == type)) {
[9d05599]317 return report_des;
318 }
319 }
320
321 return NULL;
322}
[a76b01b4]323
[9d05599]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.
[f3b39b4]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
[9d05599]332 */
[1b20da0]333errno_t usb_hid_parse_report_descriptor(usb_hid_report_t *report,
[9d05599]334 const uint8_t *data, size_t size)
335{
[3bacee1]336 size_t i = 0;
337 uint8_t tag = 0;
338 uint8_t item_size = 0;
339 int class = 0;
[9d05599]340 int ret;
[3bacee1]341 usb_hid_report_item_t *report_item = 0;
[1b20da0]342 usb_hid_report_item_t *new_report_item;
[9d05599]343 usb_hid_report_path_t *usage_path;
344
[3bacee1]345 size_t offset_input = 0;
346 size_t offset_output = 0;
347 size_t offset_feature = 0;
[a35b458]348
[b72efe8]349 link_t *item_link;
[9d05599]350
[b72efe8]351 list_t stack;
352 list_initialize(&stack);
[9d05599]353
[d1582b50]354 /* parser structure initialization */
[3bacee1]355 if (usb_hid_report_init(report) != EOK) {
[9d05599]356 return EINVAL;
357 }
[a35b458]358
[d1582b50]359 /* report item initialization */
[3bacee1]360 if (!(report_item = malloc(sizeof(usb_hid_report_item_t)))) {
[9d05599]361 return ENOMEM;
362 }
363 memset(report_item, 0, sizeof(usb_hid_report_item_t));
[b72efe8]364 link_initialize(&(report_item->link));
[9d05599]365
366 /* usage path context initialization */
[3bacee1]367 if (!(usage_path = usb_hid_report_path())) {
[4453a12a]368 free(report_item);
[9d05599]369 return ENOMEM;
370 }
[1b20da0]371 usb_hid_report_path_append_item(usage_path, 0, 0);
[a35b458]372
[3bacee1]373 while (i < size) {
374 if (!USB_HID_ITEM_IS_LONG(data[i])) {
[9d05599]375
[3bacee1]376 if ((i + USB_HID_ITEM_SIZE(data[i])) >= size) {
[9d05599]377 return EINVAL;
378 }
[a35b458]379
[9d05599]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]);
[a35b458]383
[3bacee1]384 ret = usb_hid_report_parse_tag(tag, class, data + i + 1,
385 item_size, report_item, usage_path);
[f3b39b4]386
[3bacee1]387 switch (ret) {
[f3b39b4]388 case USB_HID_NEW_REPORT_ITEM:
[7c3fb9b]389 /*
390 * store report item to report and create the
[f3b39b4]391 * new one store current collection path
392 */
393 report_item->usage_path = usage_path;
[a35b458]394
[f3b39b4]395 usb_hid_report_path_set_report_id(
[3bacee1]396 report_item->usage_path, report_item->id);
[a35b458]397
[3bacee1]398 if (report_item->id != 0) {
[f3b39b4]399 report->use_report_ids = 1;
400 }
[a35b458]401
[3bacee1]402 switch (tag) {
[f3b39b4]403 case USB_HID_REPORT_TAG_INPUT:
[1b20da0]404 report_item->type =
[f3b39b4]405 USB_HID_REPORT_TYPE_INPUT;
406
407 report_item->offset = offset_input;
[1b20da0]408 offset_input += report_item->count *
[f3b39b4]409 report_item->size;
410 break;
[a35b458]411
[f3b39b4]412 case USB_HID_REPORT_TAG_OUTPUT:
[1b20da0]413 report_item->type =
[f3b39b4]414 USB_HID_REPORT_TYPE_OUTPUT;
[a35b458]415
[f3b39b4]416 report_item->offset = offset_output;
[1b20da0]417 offset_output += report_item->count *
[f3b39b4]418 report_item->size;
[9d05599]419 break;
[a35b458]420
[f3b39b4]421 case USB_HID_REPORT_TAG_FEATURE:
[1b20da0]422 report_item->type =
[f3b39b4]423 USB_HID_REPORT_TYPE_FEATURE;
[9d05599]424
[f3b39b4]425 report_item->offset = offset_feature;
[1b20da0]426 offset_feature += report_item->count *
[3bacee1]427 report_item->size;
[9d05599]428 break;
[a35b458]429
[f3b39b4]430 default:
431 usb_log_debug2(
432 "\tjump over - tag %X\n", tag);
[3bacee1]433 break;
[f3b39b4]434 }
[a35b458]435
[1b20da0]436 /*
437 * append new fields to the report structure
[f3b39b4]438 */
[1b20da0]439 usb_hid_report_append_fields(report,
[f3b39b4]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;
[1b20da0]450 usb_hid_report_path_set_report_id (usage_path,
[f3b39b4]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);
[a35b458]458
[1b20da0]459 usb_hid_report_path_t *tmp_path =
[f3b39b4]460 usb_hid_report_path_clone(usage_path);
[9d05599]461
[1b20da0]462 new_report_item->usage_path = tmp_path;
[9d05599]463
[f3b39b4]464 list_prepend (&new_report_item->link, &stack);
465 break;
466 case USB_HID_REPORT_TAG_POP:
467 // restore current state from stack
[b72efe8]468 item_link = list_first(&stack);
469 if (item_link == NULL) {
[f3b39b4]470 return EINVAL;
471 }
472 free(report_item);
[a35b458]473
[b72efe8]474 report_item = list_get_instance(item_link,
[f3b39b4]475 usb_hid_report_item_t, link);
[a35b458]476
[f3b39b4]477 usb_hid_report_usage_path_t *tmp_usage_path;
478 tmp_usage_path = list_get_instance(
[b72efe8]479 report_item->usage_path->cpath_link.prev,
480 usb_hid_report_usage_path_t, rpath_items_link);
[a35b458]481
[1b20da0]482 usb_hid_report_set_last_item(usage_path,
[f3b39b4]483 USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
[a35b458]484
[1b20da0]485 usb_hid_report_set_last_item(usage_path,
[f3b39b4]486 USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
[9d05599]487
[f3b39b4]488 usb_hid_report_path_free(report_item->usage_path);
[b72efe8]489 list_remove (item_link);
[a35b458]490
[f3b39b4]491 break;
[a35b458]492
[f3b39b4]493 default:
[1b20da0]494 // nothing special to do
[f3b39b4]495 break;
[9d05599]496 }
497
498 /* jump over the processed block */
499 i += 1 + USB_HID_ITEM_SIZE(data[i]);
[3bacee1]500 } else {
[9d05599]501 // TBD
[3bacee1]502 i += 3 + USB_HID_ITEM_SIZE(data[i + 1]);
[9d05599]503 }
[a35b458]504
[9d05599]505 }
[a35b458]506
[9d05599]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 */
[f3b39b4]519int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data,
[3bacee1]520 size_t item_size, usb_hid_report_item_t *report_item,
521 usb_hid_report_path_t *usage_path)
522{
[a35b458]523
[9d05599]524 int ret;
[a35b458]525
[3bacee1]526 switch (class) {
[f3b39b4]527 case USB_HID_TAG_CLASS_MAIN:
[9d05599]528
[3bacee1]529 if ((ret = usb_hid_report_parse_main_tag(tag, data, item_size,
530 report_item, usage_path)) == 0) {
[9d05599]531
[f3b39b4]532 return USB_HID_NEW_REPORT_ITEM;
[3bacee1]533 } else {
[f3b39b4]534 return ret;
535 }
536 break;
[9d05599]537
[1b20da0]538 case USB_HID_TAG_CLASS_GLOBAL:
[f3b39b4]539 return usb_hid_report_parse_global_tag(tag, data, item_size,
[3bacee1]540 report_item, usage_path);
[f3b39b4]541 break;
542
[1b20da0]543 case USB_HID_TAG_CLASS_LOCAL:
[f3b39b4]544 return usb_hid_report_parse_local_tag(tag, data, item_size,
[3bacee1]545 report_item, usage_path);
[f3b39b4]546 break;
[a35b458]547
[f3b39b4]548 default:
549 return USB_HID_NO_ACTION;
[9d05599]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
[3ca4ae9]560 * @return 0 or USB_HID_ code
[9d05599]561 */
562
[1b20da0]563int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data,
[3bacee1]564 size_t item_size, usb_hid_report_item_t *report_item,
565 usb_hid_report_path_t *usage_path)
[674cf89]566{
567 usb_hid_report_usage_path_t *path_item;
[a35b458]568
[3bacee1]569 switch (tag) {
[f3b39b4]570 case USB_HID_REPORT_TAG_INPUT:
571 case USB_HID_REPORT_TAG_OUTPUT:
572 case USB_HID_REPORT_TAG_FEATURE:
[1b20da0]573 report_item->item_flags = *data;
574 return 0;
[f3b39b4]575 break;
[a35b458]576
[f3b39b4]577 case USB_HID_REPORT_TAG_COLLECTION:
[3a6e423]578
[f3b39b4]579 /* store collection atributes */
[b72efe8]580 path_item = list_get_instance(list_first(&usage_path->items),
[3bacee1]581 usb_hid_report_usage_path_t, rpath_items_link);
[b72efe8]582 path_item->flags = *data;
[a35b458]583
[f3b39b4]584 /* set last item */
[1b20da0]585 usb_hid_report_set_last_item(usage_path,
[3bacee1]586 USB_HID_TAG_CLASS_GLOBAL,
587 USB_HID_EXTENDED_USAGE_PAGE(report_item->usages[report_item->usages_count - 1]));
[f3b39b4]588
[1b20da0]589 usb_hid_report_set_last_item(usage_path,
[3bacee1]590 USB_HID_TAG_CLASS_LOCAL,
591 USB_HID_EXTENDED_USAGE(report_item->usages[report_item->usages_count - 1]));
[a35b458]592
[7c3fb9b]593 /*
594 * append the new one which will be set by common usage/usage
595 * page
596 */
[1b20da0]597 usb_hid_report_path_append_item(usage_path,
[3bacee1]598 report_item->usage_page,
599 report_item->usages[report_item->usages_count - 1]);
[f3b39b4]600
601 usb_hid_report_reset_local_items (report_item);
602 return USB_HID_NO_ACTION;
603 break;
[a35b458]604
[f3b39b4]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;
[9d05599]612 }
613
[3ca4ae9]614 return 0;
[9d05599]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
[3ca4ae9]624 * @return 0 or USB_HID_ code
[9d05599]625 */
[1b20da0]626int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data,
[3bacee1]627 size_t item_size, usb_hid_report_item_t *report_item,
628 usb_hid_report_path_t *usage_path)
629{
[a35b458]630
[3bacee1]631 switch (tag) {
[f3b39b4]632 case USB_HID_REPORT_TAG_USAGE_PAGE:
[1b20da0]633 report_item->usage_page =
[3bacee1]634 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]635 break;
636
637 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
638 report_item->logical_minimum = USB_HID_UINT32_TO_INT32(
[3bacee1]639 usb_hid_report_tag_data_uint32(data, item_size),
640 item_size * 8);
[f3b39b4]641 break;
642
643 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
644 report_item->logical_maximum = USB_HID_UINT32_TO_INT32(
[3bacee1]645 usb_hid_report_tag_data_uint32(data, item_size),
646 item_size * 8);
[f3b39b4]647 break;
648
649 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
650 report_item->physical_minimum = USB_HID_UINT32_TO_INT32(
[3bacee1]651 usb_hid_report_tag_data_uint32(data, item_size),
652 item_size * 8);
[1b20da0]653 break;
[f3b39b4]654
655 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
656 report_item->physical_maximum = USB_HID_UINT32_TO_INT32(
[3bacee1]657 usb_hid_report_tag_data_uint32(data, item_size),
658 item_size * 8);
[f3b39b4]659 break;
660
661 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
662 report_item->unit_exponent = usb_hid_report_tag_data_uint32(
[3bacee1]663 data, item_size);
[f3b39b4]664 break;
665
666 case USB_HID_REPORT_TAG_UNIT:
667 report_item->unit = usb_hid_report_tag_data_uint32(
[3bacee1]668 data, item_size);
[f3b39b4]669 break;
670
671 case USB_HID_REPORT_TAG_REPORT_SIZE:
672 report_item->size = usb_hid_report_tag_data_uint32(
[3bacee1]673 data, item_size);
[f3b39b4]674 break;
675
676 case USB_HID_REPORT_TAG_REPORT_COUNT:
677 report_item->count = usb_hid_report_tag_data_uint32(
[3bacee1]678 data, item_size);
[f3b39b4]679 break;
680
681 case USB_HID_REPORT_TAG_REPORT_ID:
[1b20da0]682 report_item->id = usb_hid_report_tag_data_uint32(data,
[3bacee1]683 item_size);
[f3b39b4]684 return USB_HID_RESET_OFFSET;
685 break;
[a35b458]686
[f3b39b4]687 case USB_HID_REPORT_TAG_PUSH:
688 case USB_HID_REPORT_TAG_POP:
[1b20da0]689 /*
[f3b39b4]690 * stack operations are done in top level parsing
691 * function
692 */
693 return tag;
694 break;
[a35b458]695
[f3b39b4]696 default:
697 return USB_HID_NO_ACTION;
[9d05599]698 }
699
[3ca4ae9]700 return 0;
[9d05599]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
[3ca4ae9]710 * @return 0 or USB_HID_ code
[9d05599]711 */
[e141281]712int 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)
[9d05599]715{
[3a6e423]716 int32_t extended_usage;
[a35b458]717
[e141281]718 switch (tag) {
[f3b39b4]719 case USB_HID_REPORT_TAG_USAGE:
[e141281]720 switch (report_item->in_delimiter) {
[f3b39b4]721 case INSIDE_DELIMITER_SET:
[e141281]722 /*
723 * Nothing to do.
724 * We catch only the first one
[f3b39b4]725 */
[1be39e9]726 break;
[a35b458]727
[f3b39b4]728 case START_DELIMITER_SET:
729 report_item->in_delimiter = INSIDE_DELIMITER_SET;
[dc12262]730 /* Fallthrough */
[f3b39b4]731 case OUTSIDE_DELIMITER_SET:
732 extended_usage = ((report_item->usage_page) << 16);
[e141281]733 extended_usage +=
734 usb_hid_report_tag_data_uint32(data, item_size);
[a35b458]735
[e141281]736 report_item->usages[report_item->usages_count] =
737 extended_usage;
[a35b458]738
[f3b39b4]739 report_item->usages_count++;
740 break;
741 }
742 break;
[a35b458]743
[e141281]744 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
[f3b39b4]745 if (item_size == 3) {
[e141281]746 /* Usage extended usages */
747 report_item->extended_usage_page =
[f3b39b4]748 USB_HID_EXTENDED_USAGE_PAGE(
[e141281]749 usb_hid_report_tag_data_uint32(data, item_size));
[a35b458]750
[e141281]751 report_item->usage_minimum =
[f3b39b4]752 USB_HID_EXTENDED_USAGE(
[e141281]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);
[f3b39b4]757 }
758 break;
[a35b458]759
[f3b39b4]760 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
761 if (item_size == 3) {
[e141281]762 if (report_item->extended_usage_page !=
763 USB_HID_EXTENDED_USAGE_PAGE(
764 usb_hid_report_tag_data_uint32(data, item_size))) {
[3ca4ae9]765 return USB_HID_INVALID;
[1be39e9]766 }
[a35b458]767
[e141281]768 /* Usage extended usages */
769 report_item->extended_usage_page =
770 USB_HID_EXTENDED_USAGE_PAGE(
[3bacee1]771 usb_hid_report_tag_data_uint32(data, item_size));
[a35b458]772
[e141281]773 report_item->usage_maximum =
774 USB_HID_EXTENDED_USAGE(
[3bacee1]775 usb_hid_report_tag_data_uint32(data, item_size));
[e141281]776 } else {
777 report_item->usage_maximum =
[3bacee1]778 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]779 }
[a35b458]780
[e141281]781 /* Put the records into the usages array */
782 for (int32_t i = report_item->usage_minimum;
[f3b39b4]783 i <= report_item->usage_maximum; i++) {
[a35b458]784
[e141281]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;
[f3b39b4]791 }
792 }
793 report_item->extended_usage_page = 0;
[a35b458]794
[f3b39b4]795 break;
[a35b458]796
[f3b39b4]797 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
[e141281]798 report_item->designator_index =
799 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]800 break;
[a35b458]801
[f3b39b4]802 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
[e141281]803 report_item->designator_minimum =
804 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]805 break;
[a35b458]806
[f3b39b4]807 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
[e141281]808 report_item->designator_maximum =
809 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]810 break;
[a35b458]811
[f3b39b4]812 case USB_HID_REPORT_TAG_STRING_INDEX:
[e141281]813 report_item->string_index =
814 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]815 break;
[a35b458]816
[f3b39b4]817 case USB_HID_REPORT_TAG_STRING_MINIMUM:
[e141281]818 report_item->string_minimum =
819 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]820 break;
[a35b458]821
[f3b39b4]822 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
[e141281]823 report_item->string_maximum =
824 usb_hid_report_tag_data_uint32(data, item_size);
825 break;
[a35b458]826
[f3b39b4]827 case USB_HID_REPORT_TAG_DELIMITER:
[e141281]828 report_item->in_delimiter =
829 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]830 break;
[a35b458]831
[f3b39b4]832 default:
833 return USB_HID_NO_ACTION;
[9d05599]834 }
[a35b458]835
[3ca4ae9]836 return 0;
[9d05599]837}
[a76b01b4]838
[9d05599]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 */
846uint32_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;
[3bacee1]852 for (i = 0; i < size; i++) {
853 result = (result | (data[i]) << (i * 8));
[9d05599]854 }
855
856 return result;
857}
[a76b01b4]858
[9d05599]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 */
[b72efe8]865void usb_hid_descriptor_print_list(list_t *list)
[9d05599]866{
[3bacee1]867 if (list == NULL || list_empty(list)) {
868 usb_log_debug("\tempty");
869 return;
[9d05599]870 }
[b72efe8]871
[3bacee1]872 list_foreach(*list, ritems_link, usb_hid_report_field_t,
873 report_item) {
[a1732929]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",
[3bacee1]877 report_item->logical_minimum);
[a1732929]878 usb_log_debug("\t\tLOGMAX: %d",
[3bacee1]879 report_item->logical_maximum);
[a1732929]880 usb_log_debug("\t\tPHYMIN: %d",
[3bacee1]881 report_item->physical_minimum);
[a1732929]882 usb_log_debug("\t\tPHYMAX: %d",
[3bacee1]883 report_item->physical_maximum);
[a1732929]884 usb_log_debug("\t\ttUSAGEMIN: %X",
[3bacee1]885 report_item->usage_minimum);
[a1732929]886 usb_log_debug("\t\tUSAGEMAX: %X",
[3bacee1]887 report_item->usage_maximum);
[a1732929]888 usb_log_debug("\t\tUSAGES COUNT: %zu",
[3bacee1]889 report_item->usages_count);
[9d05599]890
[a1732929]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);
[b72efe8]894
[3a6e423]895 usb_hid_print_usage_path(report_item->collection_path);
[9d05599]896 }
897}
[a76b01b4]898
[9d05599]899/**
900 * Prints content of given report descriptor in human readable format.
901 *
902 * @param parser Parsed descriptor to print
903 * @return void
904 */
905void usb_hid_descriptor_print(usb_hid_report_t *report)
906{
[feeac0d]907 if (report == NULL)
[9d05599]908 return;
909
[feeac0d]910 list_foreach(report->reports, reports_link,
911 usb_hid_report_description_t, report_des) {
[a1732929]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",
[3bacee1]916 usb_hid_report_byte_size(report,
917 report_des->report_id,
918 report_des->type));
[a1732929]919 usb_log_debug("\tItems: %zu", report_des->item_length);
[9d05599]920
921 usb_hid_descriptor_print_list(&report_des->report_items);
922 }
923}
[a76b01b4]924
[1b20da0]925/** Frees the HID report descriptor parser structure
[9d05599]926 *
927 * @param parser Opaque HID report parser structure
928 * @return void
929 */
[a8c4e871]930void usb_hid_report_deinit(usb_hid_report_t *report)
[9d05599]931{
[3bacee1]932 if (report == NULL) {
[9d05599]933 return;
934 }
935
936 // free collection paths
[b72efe8]937 link_t *path_link;
[9d05599]938 usb_hid_report_path_t *path;
[3bacee1]939 while (!list_empty(&report->collection_paths)) {
[b72efe8]940 path_link = list_first(&report->collection_paths);
941 path = list_get_instance(path_link,
942 usb_hid_report_path_t, cpath_link);
[f3b39b4]943
[b72efe8]944 list_remove(path_link);
945 usb_hid_report_path_free(path);
[9d05599]946 }
[a35b458]947
[9d05599]948 // free report items
949 usb_hid_report_description_t *report_des;
950 usb_hid_report_field_t *field;
[3bacee1]951 while (!list_empty(&report->reports)) {
[b72efe8]952 report_des = list_get_instance(list_first(&report->reports),
[3bacee1]953 usb_hid_report_description_t, reports_link);
[f3b39b4]954
[b72efe8]955 list_remove(&report_des->reports_link);
[a35b458]956
[3bacee1]957 while (!list_empty(&report_des->report_items)) {
[f3b39b4]958 field = list_get_instance(
[b72efe8]959 list_first(&report_des->report_items),
960 usb_hid_report_field_t, ritems_link);
[f3b39b4]961
[b72efe8]962 list_remove(&field->ritems_link);
[9d05599]963
964 free(field);
965 }
[a35b458]966
[9d05599]967 free(report_des);
968 }
[a35b458]969
[9d05599]970 return;
971}
[a76b01b4]972
[9d05599]973/**
974 * @}
975 */
Note: See TracBrowser for help on using the repository browser.