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