Changes in uspace/lib/usb/src/hidparser.c [1cbb4b7:ef354b6] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usb/src/hidparser.c
r1cbb4b7 ref354b6 39 39 #include <mem.h> 40 40 #include <usb/debug.h> 41 42 /** */ 41 #include <assert.h> 42 43 /** The new report item flag. Used to determine when the item is completly 44 * configured and should be added to the report structure 45 */ 43 46 #define USB_HID_NEW_REPORT_ITEM 1 44 47 45 /** */ 46 #define USB_HID_NO_ACTION 2 47 48 /** */ 48 /** No special action after the report descriptor tag is processed should be 49 * done 50 */ 51 #define USB_HID_NO_ACTION 2 52 53 #define USB_HID_RESET_OFFSET 3 54 55 /** Unknown tag was founded in report descriptor data*/ 49 56 #define USB_HID_UNKNOWN_TAG -99 50 57 … … 52 59 * Private descriptor parser functions 53 60 */ 61 int usb_hid_report_init(usb_hid_report_t *report); 62 int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item); 63 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); 54 64 int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size, 55 65 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path); … … 61 71 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path); 62 72 73 void usb_hid_print_usage_path(usb_hid_report_path_t *path); 63 74 void usb_hid_descriptor_print_list(link_t *head); 64 75 int usb_hid_report_reset_local_items(); … … 70 81 int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size); 71 82 inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset); 72 int usb_hid_translate_data(usb_hid_report_ item_t *item, const uint8_t *data, size_t j);73 int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int32_t value);83 int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data, size_t j); 84 uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value); 74 85 int usb_pow(int a, int b); 75 86 … … 96 107 * @return Error code 97 108 */ 98 int usb_hid_ parser_init(usb_hid_report_parser_t *parser)99 { 100 if( parser== NULL) {109 int usb_hid_report_init(usb_hid_report_t *report) 110 { 111 if(report == NULL) { 101 112 return EINVAL; 102 113 } 103 114 104 list_initialize(&(parser->input)); 105 list_initialize(&(parser->output)); 106 list_initialize(&(parser->feature)); 107 108 list_initialize(&(parser->stack)); 109 110 parser->use_report_id = 0; 115 memset(report, 0, sizeof(usb_hid_report_t)); 116 list_initialize(&report->reports); 117 list_initialize(&report->collection_paths); 118 119 report->use_report_ids = 0; 111 120 return EOK; 112 121 } 113 122 123 int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item) 124 { 125 usb_hid_report_field_t *field; 126 int i; 127 128 129 /* find or append current collection path to the list */ 130 link_t *path_it = report->collection_paths.next; 131 usb_hid_report_path_t *path = NULL; 132 while(path_it != &report->collection_paths) { 133 path = list_get_instance(path_it, usb_hid_report_path_t, link); 134 135 if(usb_hid_report_compare_usage_path(path, report_item->usage_path, USB_HID_PATH_COMPARE_STRICT) == EOK){ 136 break; 137 } 138 path_it = path_it->next; 139 } 140 if(path_it == &report->collection_paths) { 141 path = usb_hid_report_path_clone(report_item->usage_path); 142 list_append(&path->link, &report->collection_paths); 143 report->collection_paths_count++; 144 } 145 146 147 for(i=0; i<report_item->count; i++){ 148 149 field = malloc(sizeof(usb_hid_report_field_t)); 150 memset(field, 0, sizeof(usb_hid_report_field_t)); 151 list_initialize(&field->link); 152 153 /* fill the attributes */ 154 field->collection_path = path; 155 field->logical_minimum = report_item->logical_minimum; 156 field->logical_maximum = report_item->logical_maximum; 157 field->physical_minimum = report_item->physical_minimum; 158 field->physical_maximum = report_item->physical_maximum; 159 field->usage_minimum = report_item->usage_minimum; 160 field->usage_maximum = report_item->usage_maximum; 161 if(report_item->extended_usage_page != 0){ 162 field->usage_page = report_item->extended_usage_page; 163 } 164 else { 165 field->usage_page = report_item->usage_page; 166 } 167 168 if(report_item->usages_count > 0 && ((report_item->usage_minimum = 0) && (report_item->usage_maximum = 0))) { 169 if(i < report_item->usages_count){ 170 if((report_item->usages[i] & 0xFF00) > 0){ 171 field->usage_page = (report_item->usages[i] >> 16); 172 field->usage = (report_item->usages[i] & 0xFF); 173 } 174 else { 175 field->usage = report_item->usages[i]; 176 } 177 } 178 else { 179 field->usage = report_item->usages[report_item->usages_count - 1]; 180 } 181 } 182 183 field->size = report_item->size; 184 field->offset = report_item->offset + (i * report_item->size); 185 if(report_item->id != 0) { 186 field->offset += 8; 187 report->use_report_ids = 1; 188 } 189 field->item_flags = report_item->item_flags; 190 191 /* find the right report list*/ 192 usb_hid_report_description_t *report_des; 193 report_des = usb_hid_report_find_description(report, report_item->id, report_item->type); 194 if(report_des == NULL){ 195 report_des = malloc(sizeof(usb_hid_report_description_t)); 196 memset(report_des, 0, sizeof(usb_hid_report_description_t)); 197 198 report_des->type = report_item->type; 199 report_des->report_id = report_item->id; 200 list_initialize (&report_des->link); 201 list_initialize (&report_des->report_items); 202 203 list_append(&report_des->link, &report->reports); 204 report->report_count++; 205 } 206 207 /* append this field to the end of founded report list */ 208 list_append (&field->link, &report_des->report_items); 209 210 /* update the sizes */ 211 report_des->bit_length += field->size; 212 report_des->item_length++; 213 214 } 215 216 217 return EOK; 218 } 219 220 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) 221 { 222 link_t *report_it = report->reports.next; 223 usb_hid_report_description_t *report_des = NULL; 224 225 while(report_it != &report->reports) { 226 report_des = list_get_instance(report_it, usb_hid_report_description_t, link); 227 228 if((report_des->report_id == report_id) && (report_des->type == type)){ 229 return report_des; 230 } 231 232 report_it = report_it->next; 233 } 234 235 return NULL; 236 } 114 237 115 238 /** Parse HID report descriptor. … … 119 242 * @return Error code. 120 243 */ 121 int usb_hid_parse_report_descriptor(usb_hid_report_ parser_t *parser,244 int usb_hid_parse_report_descriptor(usb_hid_report_t *report, 122 245 const uint8_t *data, size_t size) 123 246 { … … 130 253 usb_hid_report_item_t *new_report_item; 131 254 usb_hid_report_path_t *usage_path; 132 usb_hid_report_path_t *tmp_usage_path;133 255 134 256 size_t offset_input=0; 135 257 size_t offset_output=0; 136 258 size_t offset_feature=0; 137 259 260 link_t stack; 261 list_initialize(&stack); 138 262 139 263 /* parser structure initialization*/ 140 if(usb_hid_ parser_init(parser) != EOK) {264 if(usb_hid_report_init(report) != EOK) { 141 265 return EINVAL; 142 266 } 143 267 144 145 268 /*report item initialization*/ 146 269 if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){ … … 159 282 160 283 if((i+USB_HID_ITEM_SIZE(data[i]))>= size){ 161 return EINVAL; // TODO ERROR CODE284 return EINVAL; 162 285 } 163 286 … … 165 288 item_size = USB_HID_ITEM_SIZE(data[i]); 166 289 class = USB_HID_ITEM_TAG_CLASS(data[i]); 167 168 usb_log_debug2(169 "i(%u) data(%X) value(%X): TAG %u, class %u, size %u - ", i,170 data[i], usb_hid_report_tag_data_int32(data+i+1,item_size),171 tag, class, item_size);172 290 173 291 ret = usb_hid_report_parse_tag(tag,class,data+i+1, 174 292 item_size,report_item, usage_path); 175 usb_log_debug2("ret: %u\n", ret);176 293 switch(ret){ 177 294 case USB_HID_NEW_REPORT_ITEM: 178 295 // store report item to report and create the new one 179 usb_log_debug("\nNEW REPORT ITEM: %X",ret); 180 181 // store current usage path 296 // store current collection path 182 297 report_item->usage_path = usage_path; 183 298 184 // clone path to the new one185 tmp_usage_path = usb_hid_report_path_clone(usage_path);186 187 // swap188 usage_path = tmp_usage_path;189 tmp_usage_path = NULL;190 191 299 usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id); 192 300 if(report_item->id != 0){ 193 parser->use_report_id= 1;301 report->use_report_ids = 1; 194 302 } 195 303 196 304 switch(tag) { 197 305 case USB_HID_REPORT_TAG_INPUT: 306 report_item->type = USB_HID_REPORT_TYPE_INPUT; 198 307 report_item->offset = offset_input; 199 308 offset_input += report_item->count * report_item->size; 200 usb_log_debug(" - INPUT\n");201 list_append(&(report_item->link), &(parser->input));202 309 break; 203 310 case USB_HID_REPORT_TAG_OUTPUT: 311 report_item->type = USB_HID_REPORT_TYPE_OUTPUT; 204 312 report_item->offset = offset_output; 205 313 offset_output += report_item->count * report_item->size; 206 usb_log_debug(" - OUTPUT\n");207 list_append(&(report_item->link), &(parser->output));208 314 209 315 break; 210 316 case USB_HID_REPORT_TAG_FEATURE: 317 report_item->type = USB_HID_REPORT_TYPE_FEATURE; 211 318 report_item->offset = offset_feature; 212 319 offset_feature += report_item->count * report_item->size; 213 usb_log_debug(" - FEATURE\n");214 list_append(&(report_item->link), &(parser->feature));215 320 break; 216 321 default: … … 218 323 break; 219 324 } 220 221 /* clone current state table to the new item */222 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {223 return ENOMEM;224 }225 memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));226 link_initialize(&(new_report_item->link));227 325 326 /* 327 * append new fields to the report 328 * structure 329 */ 330 usb_hid_report_append_fields(report, report_item); 331 228 332 /* reset local items */ 229 new_report_item->usage_minimum = 0; 230 new_report_item->usage_maximum = 0; 231 new_report_item->designator_index = 0; 232 new_report_item->designator_minimum = 0; 233 new_report_item->designator_maximum = 0; 234 new_report_item->string_index = 0; 235 new_report_item->string_minimum = 0; 236 new_report_item->string_maximum = 0; 237 238 /* reset usage from current usage path */ 239 usb_hid_report_usage_path_t *path = list_get_instance(&usage_path->link, usb_hid_report_usage_path_t, link); 240 path->usage = 0; 241 242 report_item = new_report_item; 243 333 while(report_item->usages_count > 0){ 334 report_item->usages[--(report_item->usages_count)] = 0; 335 } 336 337 report_item->extended_usage_page = 0; 338 report_item->usage_minimum = 0; 339 report_item->usage_maximum = 0; 340 report_item->designator_index = 0; 341 report_item->designator_minimum = 0; 342 report_item->designator_maximum = 0; 343 report_item->string_index = 0; 344 report_item->string_minimum = 0; 345 report_item->string_maximum = 0; 346 244 347 break; 348 349 case USB_HID_RESET_OFFSET: 350 offset_input = 0; 351 offset_output = 0; 352 offset_feature = 0; 353 break; 354 245 355 case USB_HID_REPORT_TAG_PUSH: 246 356 // push current state to stack 247 357 new_report_item = usb_hid_report_item_clone(report_item); 248 list_prepend (&parser->stack, &new_report_item->link); 249 358 usb_hid_report_path_t *tmp_path = usb_hid_report_path_clone(usage_path); 359 new_report_item->usage_path = tmp_path; 360 361 list_prepend (&new_report_item->link, &stack); 250 362 break; 251 363 case USB_HID_REPORT_TAG_POP: 252 364 // restore current state from stack 253 if(list_empty (& parser->stack)) {365 if(list_empty (&stack)) { 254 366 return EINVAL; 255 367 } 368 free(report_item); 369 370 report_item = list_get_instance(stack.next, usb_hid_report_item_t, link); 256 371 257 report_item = list_get_instance(&parser->stack, usb_hid_report_item_t, link); 258 list_remove (parser->stack.next); 372 usb_hid_report_usage_path_t *tmp_usage_path; 373 tmp_usage_path = list_get_instance(report_item->usage_path->link.prev, usb_hid_report_usage_path_t, link); 374 375 usb_hid_report_set_last_item(usage_path, tmp_usage_path->usage_page, tmp_usage_path->usage); 376 377 usb_hid_report_path_free(report_item->usage_path); 378 list_initialize(&report_item->usage_path->link); 379 list_remove (stack.next); 259 380 260 381 break; … … 279 400 } 280 401 281 282 /**283 * Parse input report.284 *285 * @param data Data for report286 * @param size Size of report287 * @param callbacks Callbacks for report actions288 * @param arg Custom arguments289 *290 * @return Error code291 */292 int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,293 const usb_hid_report_in_callbacks_t *callbacks, void *arg)294 {295 int i;296 usb_hid_report_item_t item;297 298 /* fill item due to the boot protocol report descriptor */299 // modifier keys are in the first byte300 uint8_t modifiers = data[0];301 302 item.offset = 2; /* second byte is reserved */303 item.size = 8;304 item.count = 6;305 item.usage_minimum = 0;306 item.usage_maximum = 255;307 item.logical_minimum = 0;308 item.logical_maximum = 255;309 310 if (size != 8) {311 return -1; //ERANGE;312 }313 314 uint8_t keys[6];315 for (i = 0; i < item.count; i++) {316 keys[i] = data[i + item.offset];317 }318 319 callbacks->keyboard(keys, 6, modifiers, arg);320 return EOK;321 }322 323 /**324 * Makes output report for keyboard boot protocol325 *326 * @param leds327 * @param output Output report data buffer328 * @param size Size of the output buffer329 * @return Error code330 */331 int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)332 {333 if(size != 1){334 return -1;335 }336 337 /* used only first five bits, others are only padding*/338 *data = leds;339 return EOK;340 }341 402 342 403 /** … … 401 462 402 463 case USB_HID_REPORT_TAG_COLLECTION: 403 usb_hid_report_path_append_item(usage_path, 0, 0);404 464 // TODO usage_path->flags = *data; 465 usb_hid_report_path_append_item(usage_path, report_item->usage_page, report_item->usages[report_item->usages_count-1]); 405 466 return USB_HID_NO_ACTION; 406 467 break; 407 468 408 469 case USB_HID_REPORT_TAG_END_COLLECTION: 409 // TODO410 // znici posledni uroven ve vsech usage paths411 // otazka jestli nema nicit dve, respektive novou posledni vynulovat?412 470 usb_hid_report_remove_last_item(usage_path); 413 471 return USB_HID_NO_ACTION; … … 436 494 { 437 495 case USB_HID_REPORT_TAG_USAGE_PAGE: 438 // zmeni to jenom v poslednim poli aktualni usage path 439 usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL, 440 usb_hid_report_tag_data_int32(data,item_size)); 496 report_item->usage_page = usb_hid_report_tag_data_int32(data, item_size); 441 497 break; 442 498 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM: … … 466 522 case USB_HID_REPORT_TAG_REPORT_ID: 467 523 report_item->id = usb_hid_report_tag_data_int32(data,item_size); 524 return USB_HID_RESET_OFFSET; 468 525 break; 469 526 case USB_HID_REPORT_TAG_PUSH: 470 527 case USB_HID_REPORT_TAG_POP: 528 /* 529 * stack operations are done in top level parsing 530 * function 531 */ 471 532 return tag; 472 533 break; … … 494 555 { 495 556 case USB_HID_REPORT_TAG_USAGE: 496 usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL, 497 usb_hid_report_tag_data_int32(data,item_size)); 557 report_item->usages[report_item->usages_count++] = usb_hid_report_tag_data_int32(data,item_size); 498 558 break; 499 559 case USB_HID_REPORT_TAG_USAGE_MINIMUM: 500 report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size); 560 if (item_size == 3) { 561 // usage extended usages 562 report_item->extended_usage_page = (usb_hid_report_tag_data_int32(data,item_size) & 0xFF00) >> 16; 563 report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size) & 0xFF; 564 } 565 else { 566 report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size); 567 } 501 568 break; 502 569 case USB_HID_REPORT_TAG_USAGE_MAXIMUM: 503 report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size); 570 if (item_size == 3) { 571 // usage extended usages 572 report_item->extended_usage_page = (usb_hid_report_tag_data_int32(data,item_size) & 0xFF00) >> 16; 573 report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size) & 0xFF; 574 } 575 else { 576 report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size); 577 } 504 578 break; 505 579 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX: … … 522 596 break; 523 597 case USB_HID_REPORT_TAG_DELIMITER: 524 report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size); 598 //report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size); 599 //TODO: 600 // DELIMITER STUFF 525 601 break; 526 602 … … 562 638 void usb_hid_descriptor_print_list(link_t *head) 563 639 { 564 usb_hid_report_item_t *report_item; 565 usb_hid_report_usage_path_t *path_item; 566 link_t *path; 640 usb_hid_report_field_t *report_item; 567 641 link_t *item; 568 642 643 569 644 if(head == NULL || list_empty(head)) { 570 645 usb_log_debug("\tempty\n"); … … 574 649 for(item = head->next; item != head; item = item->next) { 575 650 576 report_item = list_get_instance(item, usb_hid_report_item_t, link); 577 578 usb_log_debug("\tOFFSET: %X\n", report_item->offset); 579 usb_log_debug("\tCOUNT: %X\n", report_item->count); 580 usb_log_debug("\tSIZE: %X\n", report_item->size); 581 usb_log_debug("\tCONSTANT/VAR: %X\n", USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags)); 582 usb_log_debug("\tVARIABLE/ARRAY: %X\n", USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags)); 583 usb_log_debug("\tUSAGE PATH:\n"); 584 585 path = report_item->usage_path->link.next; 586 while(path != &report_item->usage_path->link) { 587 path_item = list_get_instance(path, usb_hid_report_usage_path_t, link); 588 usb_log_debug("\t\tUSAGE PAGE: %X, USAGE: %X\n", path_item->usage_page, path_item->usage); 589 path = path->next; 590 } 591 592 usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum); 593 usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum); 594 usb_log_debug("\tPHYMIN: %X\n", report_item->physical_minimum); 595 usb_log_debug("\tPHYMAX: %X\n", report_item->physical_maximum); 596 usb_log_debug("\tUSAGEMIN: %X\n", report_item->usage_minimum); 597 usb_log_debug("\tUSAGEMAX: %X\n", report_item->usage_maximum); 598 599 usb_log_debug("\n"); 651 report_item = list_get_instance(item, usb_hid_report_field_t, link); 652 653 usb_log_debug("\t\tOFFSET: %X\n", report_item->offset); 654 usb_log_debug("\t\tSIZE: %X\n", report_item->size); 655 usb_log_debug("\t\tLOGMIN: %X\n", report_item->logical_minimum); 656 usb_log_debug("\t\tLOGMAX: %X\n", report_item->logical_maximum); 657 usb_log_debug("\t\tPHYMIN: %X\n", report_item->physical_minimum); 658 usb_log_debug("\t\tPHYMAX: %X\n", report_item->physical_maximum); 659 usb_log_debug("\t\ttUSAGEMIN: %X\n", report_item->usage_minimum); 660 usb_log_debug("\t\tUSAGEMAX: %X\n", report_item->usage_maximum); 661 662 usb_log_debug("\t\tVALUE: %X\n", report_item->value); 663 usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage); 664 usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page); 665 666 // usb_log_debug("\n"); 600 667 601 668 } … … 609 676 * @return void 610 677 */ 611 void usb_hid_descriptor_print(usb_hid_report_ parser_t *parser)612 { 613 if( parser== NULL) {678 void usb_hid_descriptor_print(usb_hid_report_t *report) 679 { 680 if(report == NULL) { 614 681 return; 615 682 } 616 617 usb_log_debug("INPUT:\n"); 618 usb_hid_descriptor_print_list(&parser->input); 619 620 usb_log_debug("OUTPUT: \n"); 621 usb_hid_descriptor_print_list(&parser->output); 622 623 usb_log_debug("FEATURE:\n"); 624 usb_hid_descriptor_print_list(&parser->feature); 625 683 684 link_t *report_it = report->reports.next; 685 usb_hid_report_description_t *report_des; 686 687 while(report_it != &report->reports) { 688 report_des = list_get_instance(report_it, usb_hid_report_description_t, link); 689 usb_log_debug("Report ID: %d\n", report_des->report_id); 690 usb_log_debug("\tType: %d\n", report_des->type); 691 usb_log_debug("\tLength: %d\n", report_des->bit_length); 692 usb_log_debug("\tItems: %d\n", report_des->item_length); 693 694 usb_hid_descriptor_print_list(&report_des->report_items); 695 696 697 link_t *path_it = report->collection_paths.next; 698 while(path_it != &report->collection_paths) { 699 usb_hid_print_usage_path (list_get_instance(path_it, usb_hid_report_path_t, link)); 700 path_it = path_it->next; 701 } 702 703 report_it = report_it->next; 704 } 626 705 } 627 706 … … 667 746 * @return void 668 747 */ 669 void usb_hid_free_report _parser(usb_hid_report_parser_t *parser)670 { 671 if( parser== NULL){748 void usb_hid_free_report(usb_hid_report_t *report) 749 { 750 if(report == NULL){ 672 751 return; 673 752 } 674 753 675 parser->use_report_id = 0; 676 677 usb_hid_free_report_list(&parser->input); 678 usb_hid_free_report_list(&parser->output); 679 usb_hid_free_report_list(&parser->feature); 680 754 // free collection paths 755 usb_hid_report_path_t *path; 756 while(!list_empty(&report->collection_paths)) { 757 path = list_get_instance(report->collection_paths.next, usb_hid_report_path_t, link); 758 usb_hid_report_path_free(path); 759 } 760 761 // free report items 762 usb_hid_report_description_t *report_des; 763 usb_hid_report_field_t *field; 764 while(!list_empty(&report->reports)) { 765 report_des = list_get_instance(report->reports.next, usb_hid_report_description_t, link); 766 list_remove(&report_des->link); 767 768 while(!list_empty(&report_des->report_items)) { 769 field = list_get_instance(report_des->report_items.next, usb_hid_report_field_t, link); 770 list_remove(&field->link); 771 772 free(field); 773 } 774 775 free(report_des); 776 } 777 681 778 return; 682 779 } … … 688 785 * @param parser Opaque HID report parser structure. 689 786 * @param data Data for the report. 690 * @param callbacks Callbacks for report actions.691 * @param arg Custom argument (passed through to the callbacks).692 787 * @return Error code. 693 788 */ 694 int usb_hid_parse_report(const usb_hid_report_parser_t *parser, 695 const uint8_t *data, size_t size, 696 usb_hid_report_path_t *path, int flags, 697 const usb_hid_report_in_callbacks_t *callbacks, void *arg) 789 int usb_hid_parse_report(const usb_hid_report_t *report, 790 const uint8_t *data, size_t size) 698 791 { 699 792 link_t *list_item; 700 usb_hid_report_item_t *item; 701 uint8_t *keys; 702 uint8_t item_value; 703 size_t key_count=0; 704 size_t i=0; 705 size_t j=0; 793 usb_hid_report_field_t *item; 794 706 795 uint8_t report_id = 0; 707 708 if(parser == NULL) { 796 usb_hid_report_description_t *report_des; 797 usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT; 798 799 if(report == NULL) { 709 800 return EINVAL; 710 801 } 711 712 /* get the size of result array */ 713 key_count = usb_hid_report_input_length(parser, path, flags); 714 715 if(!(keys = malloc(sizeof(uint8_t) * key_count))){ 716 return ENOMEM; 717 } 718 719 if(parser->use_report_id != 0) { 802 803 if(report->use_report_ids != 0) { 720 804 report_id = data[0]; 721 usb_hid_report_path_set_report_id(path, report_id); 722 } 805 } 806 807 report_des = usb_hid_report_find_description(report, report_id, type); 723 808 724 809 /* read data */ 725 list_item = parser->input.next; 726 while(list_item != &(parser->input)) { 727 728 item = list_get_instance(list_item, usb_hid_report_item_t, link); 729 730 if(!USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) && 731 (usb_hid_report_compare_usage_path(item->usage_path, path, flags) == EOK)) { 732 for(j=0; j<(size_t)(item->count); j++) { 733 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) || 734 ((item->usage_minimum == 0) && (item->usage_maximum == 0))) { 735 // variable item 736 keys[i++] = usb_hid_translate_data(item, data,j); 810 list_item = report_des->report_items.next; 811 while(list_item != &(report_des->report_items)) { 812 813 item = list_get_instance(list_item, usb_hid_report_field_t, link); 814 815 if(!USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) { 816 817 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) || 818 ((item->usage_minimum == 0) && (item->usage_maximum == 0))) { 819 820 // variable item 821 item->value = usb_hid_translate_data(item, data,0); 822 823 // array item ??? 824 if(!((item->usage_minimum == 0) && (item->usage_maximum == 0))) { 825 item->usage = item->value + item->usage_minimum; 737 826 } 738 else { 739 // bitmapa 740 if((item_value = usb_hid_translate_data(item, data, j)) != 0) { 741 keys[i++] = (item->count - 1 - j) + item->usage_minimum; 742 } 743 else { 744 keys[i++] = 0; 745 } 746 } 747 } 827 } 828 else { 829 // bitmapa 830 // TODO: overit jestli vraci hodnoty podle phy min/max 831 item->value = usb_hid_translate_data(item, data, 0); 832 } 748 833 } 749 834 list_item = list_item->next; 750 835 } 751 752 callbacks->keyboard(keys, key_count, report_id, arg);753 836 754 free(keys);755 837 return EOK; 756 838 … … 758 840 759 841 /** 760 * Translate data from the report as specified in report descriptor 842 * Translate data from the report as specified in report descriptor item 761 843 * 762 844 * @param item Report descriptor item with definition of translation … … 765 847 * @return Translated data 766 848 */ 767 int usb_hid_translate_data(usb_hid_report_ item_t *item, const uint8_t *data, size_t j)849 int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data, size_t j) 768 850 { 769 851 int resolution; … … 771 853 int part_size; 772 854 773 int32_t value ;855 int32_t value=0; 774 856 int32_t mask; 775 857 const uint8_t *foo; 776 858 777 // now only common numbersllowed859 // now only shot tags are allowed 778 860 if(item->size > 32) { 779 861 return 0; … … 795 877 796 878 offset = item->offset + (j * item->size); 797 if(item->id != 0) {798 offset += 8;799 usb_log_debug("MOVED OFFSET BY 1Byte, REPORT_ID(%d)\n", item->id);800 }801 879 802 880 // FIXME 803 if((offset/8) != ((offset+item->size)/8)) { 804 usb_log_debug2("offset %d\n", offset); 881 if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) { 805 882 806 883 part_size = ((offset+item->size)%8); 807 usb_log_debug2("part size %d\n",part_size); 808 809 // the higher one 810 foo = data+(offset/8); 811 mask = ((1 << (item->size-part_size))-1); 812 value = (*foo & mask) << part_size; 813 814 usb_log_debug2("hfoo %x\n", *foo); 815 usb_log_debug2("hmaska %x\n", mask); 816 usb_log_debug2("hval %d\n", value); 817 818 // the lower one 819 foo = data+((offset+item->size)/8); 820 mask = ((1 << part_size)-1) << (8-part_size); 821 value += ((*foo & mask) >> (8-part_size)); 822 823 usb_log_debug2("lfoo %x\n", *foo); 824 usb_log_debug2("lmaska %x\n", mask); 825 usb_log_debug2("lval %d\n", ((*foo & mask) >> (8-(item->size-part_size)))); 826 usb_log_debug2("val %d\n", value); 827 828 884 885 size_t i=0; 886 for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){ 887 if(i == (size_t)(offset/8)) { 888 // the higher one 889 foo = data + i; 890 mask = ((1 << (item->size-part_size))-1); 891 value = (*foo & mask) << part_size; 892 } 893 else if(i == ((offset+item->size-1)/8)){ 894 // the lower one 895 foo = data + i; 896 mask = ((1 << part_size)-1) << (8-part_size); 897 value += ((*foo & mask) >> (8-part_size)); 898 } 899 else { 900 value = value << 8; 901 value += *(data + 1); 902 } 903 } 829 904 } 830 905 else { … … 832 907 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size)); 833 908 value = (*foo & mask) >> (8-((offset%8)+item->size)); 834 835 usb_log_debug2("offset %d\n", offset); 836 837 usb_log_debug2("foo %x\n", *foo);838 usb_log_debug2("maska %x\n", mask);839 usb_log_debug2("val %d\n", value);840 }841 842 usb_log_debug2("---\n\n"); 909 } 910 911 if(!(item->logical_minimum >= 0 && item->logical_maximum >= 0)){ 912 value = (int32_t)value; 913 } 914 else { 915 value = (uint32_t)value; 916 } 917 843 918 844 919 return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum); … … 847 922 848 923 /** 849 * 850 * 851 * @param parser 852 * @param path 853 * @param flags 854 * @return 855 */ 856 size_t usb_hid_report_input_length(const usb_hid_report_ parser_t *parser,924 * Returns number of items in input report which are accessible by given usage path 925 * 926 * @param parser Opaque report descriptor structure 927 * @param path Usage path specification 928 * @param flags Usage path comparison flags 929 * @return Number of items in input report 930 */ 931 size_t usb_hid_report_input_length(const usb_hid_report_t *report, 857 932 usb_hid_report_path_t *path, int flags) 858 933 { 934 859 935 size_t ret = 0; 860 link_t *item; 861 usb_hid_report_item_t *report_item; 862 863 if(parser == NULL) { 936 937 if(report == NULL) { 864 938 return 0; 865 939 } 866 867 item = parser->input.next; 868 while(&parser->input != item) { 869 report_item = list_get_instance(item, usb_hid_report_item_t, link); 870 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) && 871 (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) { 872 ret += report_item->count; 873 } 874 875 item = item->next; 876 } 940 941 usb_hid_report_description_t *report_des; 942 report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_INPUT); 943 if(report_des == NULL) { 944 return 0; 945 } 946 947 link_t *field_it = report_des->report_items.next; 948 usb_hid_report_field_t *field; 949 while(field_it != &report_des->report_items) { 950 951 field = list_get_instance(field_it, usb_hid_report_field_t, link); 952 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) { 953 954 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage); 955 if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) { 956 ret++; 957 } 958 usb_hid_report_remove_last_item (field->collection_path); 959 } 960 961 field_it = field_it->next; 962 } 877 963 878 964 return ret; 879 } 880 881 882 /** 883 * 884 * @param usage_path 885 * @param usage_page 886 * @param usage 887 * @return 965 } 966 967 968 /** 969 * Appends one item (couple of usage_path and usage) into the usage path 970 * structure 971 * 972 * @param usage_path Usage path structure 973 * @param usage_page Usage page constant 974 * @param usage Usage constant 975 * @return Error code 888 976 */ 889 977 int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, … … 899 987 item->usage = usage; 900 988 item->usage_page = usage_page; 901 902 usb_log_debug("Appending usage %d, usage page %d\n", usage, usage_page); 903 904 list_append (&usage_path->link, &item->link); 989 item->flags = 0; 990 991 list_append (&item->link, &usage_path->head); 905 992 usage_path->depth++; 906 993 return EOK; … … 908 995 909 996 /** 910 * 911 * @param usage_path 912 * @return 997 * Removes last item from the usage path structure 998 * @param usage_path 999 * @return void 913 1000 */ 914 1001 void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path) … … 916 1003 usb_hid_report_usage_path_t *item; 917 1004 918 if(!list_empty(&usage_path-> link)){919 item = list_get_instance(usage_path-> link.prev, usb_hid_report_usage_path_t, link);920 list_remove(usage_path-> link.prev);1005 if(!list_empty(&usage_path->head)){ 1006 item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link); 1007 list_remove(usage_path->head.prev); 921 1008 usage_path->depth--; 922 1009 free(item); … … 925 1012 926 1013 /** 1014 * Nulls last item of the usage path structure. 927 1015 * 928 1016 * @param usage_path 929 * @return 1017 * @return void 930 1018 */ 931 1019 void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path) … … 933 1021 usb_hid_report_usage_path_t *item; 934 1022 935 if(!list_empty(&usage_path-> link)){936 item = list_get_instance(usage_path-> link.prev, usb_hid_report_usage_path_t, link);1023 if(!list_empty(&usage_path->head)){ 1024 item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link); 937 1025 memset(item, 0, sizeof(usb_hid_report_usage_path_t)); 938 1026 } … … 940 1028 941 1029 /** 942 * 943 * @param usage_path 944 * @param tag 945 * @param data 946 * @return 1030 * Modifies last item of usage path structure by given usage page or usage 1031 * 1032 * @param usage_path Opaque usage path structure 1033 * @param tag Class of currently processed tag (Usage page tag falls into Global 1034 * class but Usage tag into the Local) 1035 * @param data Value of the processed tag 1036 * @return void 947 1037 */ 948 1038 void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data) … … 950 1040 usb_hid_report_usage_path_t *item; 951 1041 952 if(!list_empty(&usage_path-> link)){953 item = list_get_instance(usage_path-> link.prev, usb_hid_report_usage_path_t, link);1042 if(!list_empty(&usage_path->head)){ 1043 item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link); 954 1044 955 1045 switch(tag) { … … 965 1055 } 966 1056 967 /** 968 * 969 * 970 * @param report_path 971 * @param path 972 * @param flags 973 * @return 1057 1058 void usb_hid_print_usage_path(usb_hid_report_path_t *path) 1059 { 1060 usb_log_debug("USAGE_PATH FOR RId(%d):\n", path->report_id); 1061 usb_log_debug("\tLENGTH: %d\n", path->depth); 1062 1063 link_t *item = path->head.next; 1064 usb_hid_report_usage_path_t *path_item; 1065 while(item != &path->head) { 1066 1067 path_item = list_get_instance(item, usb_hid_report_usage_path_t, link); 1068 usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page); 1069 usb_log_debug("\tUSAGE: %X\n", path_item->usage); 1070 usb_log_debug("\tFLAGS: %d\n", path_item->flags); 1071 1072 item = item->next; 1073 } 1074 } 1075 1076 /** 1077 * Compares two usage paths structures 1078 * 1079 * If USB_HID_PATH_COMPARE_COLLECTION_ONLY flag is given, the last item in report_path structure is forgotten 1080 * 1081 * @param report_path usage path structure to compare 1082 * @param path usage patrh structure to compare 1083 * @param flags Flags determining the mode of comparison 1084 * @return EOK if both paths are identical, non zero number otherwise 974 1085 */ 975 1086 int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, … … 1005 1116 } 1006 1117 1007 report_link = report_path-> link.next;1008 path_link = path-> link.next;1118 report_link = report_path->head.next; 1119 path_link = path->head.next; 1009 1120 1010 while((report_link != &report_path-> link) && (path_link != &path->link)) {1121 while((report_link != &report_path->head) && (path_link != &path->head)) { 1011 1122 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link); 1012 1123 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link); … … 1022 1133 } 1023 1134 1024 if((report_link == &report_path->link) && (path_link == &path->link)) { 1135 if(((report_link == &report_path->head) && (path_link == &path->head)) || 1136 (((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) && (path_link = &path->head) && (report_link == report_path->head.prev))) { 1025 1137 return EOK; 1026 1138 } … … 1032 1144 /* compare with only the end of path*/ 1033 1145 case USB_HID_PATH_COMPARE_END: 1034 report_link = report_path->link.prev; 1035 path_link = path->link.prev; 1036 1037 if(list_empty(&path->link)){ 1146 1147 if((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) { 1148 report_link = report_path->head.prev->prev; 1149 } 1150 else { 1151 report_link = report_path->head.prev; 1152 } 1153 path_link = path->head.prev; 1154 1155 if(list_empty(&path->head)){ 1038 1156 return EOK; 1039 1157 } 1040 1158 1041 while((report_link != &report_path-> link) && (path_link != &path->link)) {1159 while((report_link != &report_path->head) && (path_link != &path->head)) { 1042 1160 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link); 1043 1161 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link); … … 1053 1171 } 1054 1172 1055 if(path_link == &path-> link) {1173 if(path_link == &path->head) { 1056 1174 return EOK; 1057 1175 } … … 1072 1190 1073 1191 /** 1074 * 1075 * @return 1192 * Allocates and initializes new usage path structure. 1193 * 1194 * @return Initialized usage path structure 1076 1195 */ 1077 1196 usb_hid_report_path_t *usb_hid_report_path(void) … … 1079 1198 usb_hid_report_path_t *path; 1080 1199 path = malloc(sizeof(usb_hid_report_path_t)); 1081 if( !path){1200 if(path == NULL){ 1082 1201 return NULL; 1083 1202 } … … 1086 1205 path->report_id = 0; 1087 1206 list_initialize(&path->link); 1207 list_initialize(&path->head); 1088 1208 return path; 1089 1209 } … … 1091 1211 1092 1212 /** 1093 * 1094 * @param path 1213 * Releases given usage path structure. 1214 * 1215 * @param path usage path structure to release 1095 1216 * @return void 1096 1217 */ 1097 1218 void usb_hid_report_path_free(usb_hid_report_path_t *path) 1098 1219 { 1099 while(!list_empty(&path-> link)){1220 while(!list_empty(&path->head)){ 1100 1221 usb_hid_report_remove_last_item(path); 1101 1222 } 1223 1224 list_remove(&path->link); 1225 free(path); 1102 1226 } 1103 1227 … … 1106 1230 * Clone content of given usage path to the new one 1107 1231 * 1108 * @param usage_path 1109 * @return 1232 * @param usage_path Usage path structure to clone 1233 * @return New copy of given usage path structure 1110 1234 */ 1111 1235 usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path) 1112 1236 { 1237 link_t *path_link; 1113 1238 usb_hid_report_usage_path_t *path_item; 1114 link_t *path_link;1239 usb_hid_report_usage_path_t *new_path_item; 1115 1240 usb_hid_report_path_t *new_usage_path = usb_hid_report_path (); 1116 1241 … … 1119 1244 } 1120 1245 1121 if(list_empty(&usage_path-> link)){1246 if(list_empty(&usage_path->head)){ 1122 1247 return new_usage_path; 1123 1248 } 1124 1249 1125 path_link = usage_path-> link.next;1126 while(path_link != &usage_path-> link) {1250 path_link = usage_path->head.next; 1251 while(path_link != &usage_path->head) { 1127 1252 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link); 1128 usb_hid_report_path_append_item (new_usage_path, path_item->usage_page, path_item->usage); 1253 new_path_item = malloc(sizeof(usb_hid_report_usage_path_t)); 1254 if(new_path_item == NULL) { 1255 return NULL; 1256 } 1257 1258 list_initialize (&new_path_item->link); 1259 new_path_item->usage_page = path_item->usage_page; 1260 new_path_item->usage = path_item->usage; 1261 new_path_item->flags = path_item->flags; 1262 1263 list_append(&new_path_item->link, &new_usage_path->head); 1264 new_usage_path->depth++; 1129 1265 1130 1266 path_link = path_link->next; … … 1137 1273 /*** OUTPUT API **/ 1138 1274 1139 /** Allocates output report buffer 1140 * 1141 * @param parser 1142 * @param size 1143 * @return 1144 */ 1145 uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size) 1146 { 1147 if(parser == NULL) { 1275 /** 1276 * Allocates output report buffer for output report 1277 * 1278 * @param parser Report parsed structure 1279 * @param size Size of returned buffer 1280 * @param report_id Report id of created output report 1281 * @return Returns allocated output buffer for specified output 1282 */ 1283 uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id) 1284 { 1285 if(report == NULL) { 1148 1286 *size = 0; 1149 1287 return NULL; 1150 1288 } 1151 1152 // read the last output report item1153 usb_hid_report_ item_t *last;1154 link_t *link;1155 1156 link = parser->output.prev;1157 if(link != &parser->output) {1158 last = list_get_instance(link, usb_hid_report_item_t, link);1159 *size = (last->offset + (last->size * last->count)) / 8; 1160 1161 uint8_t *buffer = malloc(sizeof(uint8_t) * (*size));1162 memset(buffer, 0, sizeof(uint8_t) * (*size)); 1163 usb_log_debug("output buffer: %s\n", usb_debug_str_buffer(buffer, *size, 0));1164 1165 return buffer;1289 1290 link_t *report_it = report->reports.next; 1291 usb_hid_report_description_t *report_des = NULL; 1292 while(report_it != &report->reports) { 1293 report_des = list_get_instance(report_it, usb_hid_report_description_t, link); 1294 if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){ 1295 break; 1296 } 1297 1298 report_it = report_it->next; 1299 } 1300 1301 if(report_des == NULL){ 1302 *size = 0; 1303 return NULL; 1166 1304 } 1167 1305 else { 1168 *size = 0; 1169 return NULL; 1306 *size = (report_des->bit_length + (8 - 1))/8; 1307 uint8_t *ret = malloc((*size) * sizeof(uint8_t)); 1308 memset(ret, 0, (*size) * sizeof(uint8_t)); 1309 return ret; 1170 1310 } 1171 1311 } … … 1175 1315 * 1176 1316 * @param output Output report buffer 1177 * @return 1317 * @return void 1178 1318 */ 1179 1319 void usb_hid_report_output_free(uint8_t *output) … … 1187 1327 /** Returns size of output for given usage path 1188 1328 * 1189 * @param parser 1190 * @param path 1191 * @param flags 1192 * @return 1193 */ 1194 size_t usb_hid_report_output_size(usb_hid_report_ parser_t *parser,1329 * @param parser Opaque report parser structure 1330 * @param path Usage path specified which items will be thought for the output 1331 * @param flags Flags of usage path structure comparison 1332 * @return Number of items matching the given usage path 1333 */ 1334 size_t usb_hid_report_output_size(usb_hid_report_t *report, 1195 1335 usb_hid_report_path_t *path, int flags) 1196 1336 { 1197 size_t ret = 0; 1198 link_t *item; 1199 usb_hid_report_item_t *report_item; 1200 1201 if(parser == NULL) { 1337 size_t ret = 0; 1338 usb_hid_report_description_t *report_des; 1339 1340 if(report == NULL) { 1202 1341 return 0; 1203 1342 } 1204 1343 1205 item = parser->output.next; 1206 while(&parser->output != item) { 1207 report_item = list_get_instance(item, usb_hid_report_item_t, link); 1208 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) && 1209 (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) { 1210 ret += report_item->count; 1211 } 1212 1213 item = item->next; 1214 } 1344 report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_OUTPUT); 1345 if(report_des == NULL){ 1346 return 0; 1347 } 1348 1349 link_t *field_it = report_des->report_items.next; 1350 usb_hid_report_field_t *field; 1351 while(field_it != &report_des->report_items) { 1352 1353 field = list_get_instance(field_it, usb_hid_report_field_t, link); 1354 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage); 1355 if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) { 1356 ret++; 1357 } 1358 usb_hid_report_remove_last_item (field->collection_path); 1359 1360 field_it = field_it->next; 1361 } 1215 1362 1216 1363 return ret; … … 1218 1365 } 1219 1366 1220 /** Updates the output report buffer by translated given data 1221 * 1222 * @param parser 1223 * @param path 1224 * @param flags 1225 * @param buffer 1226 * @param size 1227 * @param data 1228 * @param data_size 1229 * @return 1230 */ 1231 int usb_hid_report_output_translate(usb_hid_report_parser_t *parser, 1232 usb_hid_report_path_t *path, int flags, 1233 uint8_t *buffer, size_t size, 1234 int32_t *data, size_t data_size) 1235 { 1236 usb_hid_report_item_t *report_item; 1367 /** Makes the output report buffer for data given in the report structure 1368 * 1369 * @param parser Opaque report parser structure 1370 * @param path Usage path specifing which parts of output will be set 1371 * @param flags Usage path structure comparison flags 1372 * @param buffer Output buffer 1373 * @param size Size of output buffer 1374 * @return Error code 1375 */ 1376 int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id, 1377 uint8_t *buffer, size_t size) 1378 { 1237 1379 link_t *item; 1238 size_t idx=0;1239 int i=0;1240 1380 int32_t value=0; 1241 1381 int offset; 1242 1382 int length; 1243 1383 int32_t tmp_value; 1244 size_t offset_prefix = 0; 1245 1246 if(parser == NULL) { 1384 1385 if(report == NULL) { 1247 1386 return EINVAL; 1248 1387 } 1249 1388 1250 if(parser->use_report_id != 0) { 1251 buffer[0] = path->report_id; 1252 offset_prefix = 8; 1389 if(report->use_report_ids != 0) { 1390 buffer[0] = report_id; 1253 1391 } 1254 1392 1255 1393 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0)); 1256 usb_log_debug("OUTPUT DATA[0]: %d, DATA[1]: %d, DATA[2]: %d\n", data[0], data[1], data[2]); 1257 1258 item = parser->output.next;1259 while(item != &parser->output){1260 re port_item = list_get_instance(item, usb_hid_report_item_t, link);1261 1262 for(i=0; i<report_item->count; i++) { 1263 1264 if(idx >= data_size) {1265 break;1266 }1394 1395 usb_hid_report_description_t *report_des; 1396 report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT); 1397 if(report_des == NULL){ 1398 return EINVAL; 1399 } 1400 1401 usb_hid_report_field_t *report_item; 1402 item = report_des->report_items.next; 1403 while(item != &report_des->report_items) { 1404 report_item = list_get_instance(item, usb_hid_report_field_t, link); 1267 1405 1268 1406 if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) || 1269 1407 ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) { 1270 1408 1271 //// variable item1272 value = usb_hid_translate_data_reverse(report_item, data[idx++]);1273 offset = report_item->offset + (i * report_item->size) + offset_prefix;1409 // variable item 1410 value = usb_hid_translate_data_reverse(report_item, report_item->value); 1411 offset = report_item->offset; 1274 1412 length = report_item->size; 1275 1413 } 1276 1414 else { 1277 1415 //bitmap 1278 value += usb_hid_translate_data_reverse(report_item, data[idx++]);1279 offset = report_item->offset + offset_prefix;1280 length = report_item->size * report_item->count;1416 value += usb_hid_translate_data_reverse(report_item, report_item->value); 1417 offset = report_item->offset; 1418 length = report_item->size; 1281 1419 } 1282 1420 … … 1297 1435 } 1298 1436 else { 1299 // je to ve dvou!! FIXME: melo by to umet delsi jak 2 1300 1301 // konec prvniho -- dolni x bitu 1302 tmp_value = value; 1303 tmp_value = tmp_value & ((1 << (8-(offset%8)))-1); 1304 tmp_value = tmp_value << (offset%8); 1305 1437 int i = 0; 1306 1438 uint8_t mask = 0; 1307 mask = ~(((1 << (8-(offset%8)))-1) << (offset%8)); 1308 buffer[offset/8] = (buffer[offset/8] & mask) | tmp_value; 1309 1310 // a ted druhej -- hornich length-x bitu 1311 value = value >> (8 - (offset % 8)); 1312 value = value & ((1 << (length - (8 - (offset % 8)))) - 1); 1439 for(i = (offset/8); i <= ((offset+length-1)/8); i++) { 1440 if(i == (offset/8)) { 1441 tmp_value = value; 1442 tmp_value = tmp_value & ((1 << (8-(offset%8)))-1); 1443 tmp_value = tmp_value << (offset%8); 1444 1445 mask = ~(((1 << (8-(offset%8)))-1) << (offset%8)); 1446 buffer[i] = (buffer[i] & mask) | tmp_value; 1447 } 1448 else if (i == ((offset + length -1)/8)) { 1449 1450 value = value >> (length - ((offset + length) % 8)); 1451 value = value & ((1 << (length - ((offset + length) % 8))) - 1); 1313 1452 1314 mask = ((1 << (length - (8 - (offset % 8)))) - 1); 1315 buffer[(offset+length-1)/8] = (buffer[(offset+length-1)/8] & mask) | value; 1316 } 1317 1318 } 1453 mask = (1 << (length - ((offset + length) % 8))) - 1; 1454 buffer[i] = (buffer[i] & mask) | value; 1455 } 1456 else { 1457 buffer[i] = value & (0xFF << i); 1458 } 1459 } 1460 } 1461 1319 1462 1320 1463 item = item->next; … … 1327 1470 1328 1471 /** 1329 * 1330 * @param item 1331 * @param value 1332 * @return 1333 */ 1334 int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int value)1472 * Translate given data for putting them into the outoput report 1473 * @param item Report item structure 1474 * @param value Value to translate 1475 * @return ranslated value 1476 */ 1477 uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value) 1335 1478 { 1336 1479 int ret=0; … … 1372 1515 1373 1516 1374 return ret; 1375 } 1376 1377 1517 return (uint32_t)ret; 1518 } 1519 1520 /** 1521 * Sets report id in usage path structure 1522 * 1523 * @param path Usage path structure 1524 * @param report_id Report id to set 1525 * @return Error code 1526 */ 1378 1527 int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id) 1379 1528 { … … 1386 1535 } 1387 1536 1388 1537 /** 1538 * Clones given report item structure and returns the new one 1539 * 1540 * @param item Report item structure to clone 1541 * @return Clonned item 1542 */ 1389 1543 usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item) 1390 1544 { … … 1400 1554 } 1401 1555 1556 1557 /** 1558 * 1559 * 1560 * 1561 * 1562 * 1563 */ 1564 int usb_hid_report_output_set_data(usb_hid_report_t *report, 1565 usb_hid_report_path_t *path, int flags, 1566 int *data, size_t data_size) 1567 { 1568 size_t data_idx = 0; 1569 if(report == NULL){ 1570 return EINVAL; 1571 } 1572 1573 usb_hid_report_description_t *report_des; 1574 report_des = usb_hid_report_find_description (report, path->report_id, 1575 USB_HID_REPORT_TYPE_OUTPUT); 1576 if(report_des == NULL){ 1577 return EINVAL; 1578 } 1579 1580 usb_hid_report_field_t *field; 1581 link_t *field_it = report_des->report_items.next; 1582 while(field_it != &report_des->report_items){ 1583 1584 field = list_get_instance(field_it, usb_hid_report_field_t, link); 1585 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) { 1586 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage); 1587 if(usb_hid_report_compare_usage_path (field->collection_path, path, 1588 flags) == EOK) { 1589 1590 if(data_idx < data_size) { 1591 field->value = data[data_idx++]; 1592 } 1593 else { 1594 field->value = 0; 1595 } 1596 } 1597 usb_hid_report_remove_last_item (field->collection_path); 1598 } 1599 1600 field_it = field_it->next; 1601 } 1602 1603 return EOK; 1604 } 1605 1402 1606 /** 1403 1607 * @}
Note:
See TracChangeset
for help on using the changeset viewer.