Changes in uspace/lib/usbhid/src/hidparser.c [069b80d:b72efe8] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbhid/src/hidparser.c
r069b80d rb72efe8 52 52 int32_t value); 53 53 54 /*---------------------------------------------------------------------------*/ 55 56 static int usb_pow(int a, int b) 57 { 58 switch (b) { 54 int usb_pow(int a, int b); 55 56 /*---------------------------------------------------------------------------*/ 57 58 // TODO: tohle ma bejt asi jinde 59 int usb_pow(int a, int b) 60 { 61 switch(b) { 59 62 case 0: 60 63 return 1; … … 64 67 break; 65 68 default: 66 return a * usb_pow(a, b -1);69 return a * usb_pow(a, b-1); 67 70 break; 68 71 } … … 78 81 */ 79 82 size_t usb_hid_report_size(usb_hid_report_t *report, uint8_t report_id, 80 usb_hid_report_type_t type)83 usb_hid_report_type_t type) 81 84 { 82 85 usb_hid_report_description_t *report_des; 83 86 84 if 87 if(report == NULL) { 85 88 return 0; 86 89 } 87 90 88 report_des = usb_hid_report_find_description (report, report_id, type);89 if (report_des == NULL){91 report_des = usb_hid_report_find_description (report, report_id, type); 92 if(report_des == NULL){ 90 93 return 0; 91 } else { 94 } 95 else { 92 96 return report_des->item_length; 93 97 } … … 102 106 */ 103 107 size_t usb_hid_report_byte_size(usb_hid_report_t *report, uint8_t report_id, 104 usb_hid_report_type_t type)108 usb_hid_report_type_t type) 105 109 { 106 110 usb_hid_report_description_t *report_des; 107 111 108 if 112 if(report == NULL) { 109 113 return 0; 110 114 } 111 115 112 report_des = usb_hid_report_find_description (report, report_id, type);113 if (report_des == NULL){116 report_des = usb_hid_report_find_description (report, report_id, type); 117 if(report_des == NULL){ 114 118 return 0; 115 } else { 119 } 120 else { 116 121 return ((report_des->bit_length + 7) / 8) ; 117 122 } … … 128 133 */ 129 134 int usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data, 130 135 size_t size, uint8_t *report_id) 131 136 { 132 137 usb_hid_report_field_t *item; … … 135 140 usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT; 136 141 137 if 142 if(report == NULL) { 138 143 return EINVAL; 139 144 } 140 145 141 if 146 if(report->use_report_ids != 0) { 142 147 *report_id = data[0]; 143 } else { 148 } 149 else { 144 150 *report_id = 0; 145 151 } 146 152 153 147 154 report_des = usb_hid_report_find_description(report, *report_id, 148 149 150 if 155 type); 156 157 if(report_des == NULL) { 151 158 return EINVAL; 152 159 } … … 155 162 list_foreach(report_des->report_items, list_item) { 156 163 item = list_get_instance(list_item, usb_hid_report_field_t, 157 158 159 if 164 ritems_link); 165 166 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) { 160 167 161 if (USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) { 162 /* array */ 168 if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0){ 169 170 // array 163 171 item->value = 164 172 usb_hid_translate_data(item, data); 165 173 166 174 item->usage = USB_HID_EXTENDED_USAGE( 167 item->usages[ item->value -168 item-> physical_minimum]);175 item->usages[ 176 item->value - item->physical_minimum]); 169 177 170 178 item->usage_page = 171 179 USB_HID_EXTENDED_USAGE_PAGE( 172 item->usages[ item->value -173 item-> physical_minimum]);174 175 usb_hid_report_set_last_item (180 item->usages[ 181 item->value - item->physical_minimum]); 182 183 usb_hid_report_set_last_item ( 176 184 item->collection_path, 177 185 USB_HID_TAG_CLASS_GLOBAL, 178 186 item->usage_page); 179 187 180 usb_hid_report_set_last_item (188 usb_hid_report_set_last_item ( 181 189 item->collection_path, 182 190 USB_HID_TAG_CLASS_LOCAL, item->usage); 183 } else { 184 /* variable item */ 191 192 } 193 else { 194 // variable item 185 195 item->value = usb_hid_translate_data(item, 186 196 data); … … 190 200 191 201 return EOK; 202 192 203 } 193 204 … … 206 217 int part_size; 207 218 208 int32_t value =0;209 int32_t mask =0;210 const uint8_t *foo =0;211 212 / * now only short tags are allowed */213 if 219 int32_t value=0; 220 int32_t mask=0; 221 const uint8_t *foo=0; 222 223 // now only shot tags are allowed 224 if(item->size > 32) { 214 225 return 0; 215 226 } 216 227 217 if ((item->physical_minimum == 0) && (item->physical_maximum == 0)){228 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){ 218 229 item->physical_minimum = item->logical_minimum; 219 230 item->physical_maximum = item->logical_maximum; … … 221 232 222 233 223 if (item->physical_maximum == item->physical_minimum){234 if(item->physical_maximum == item->physical_minimum){ 224 235 resolution = 1; 225 } else { 236 } 237 else { 226 238 resolution = (item->logical_maximum - item->logical_minimum) / 227 239 ((item->physical_maximum - item->physical_minimum) * 228 (usb_pow(10, 240 (usb_pow(10,(item->unit_exponent)))); 229 241 } 230 242 231 243 offset = item->offset; 232 244 // FIXME 233 if ((size_t) (offset / 8) != (size_t) ((offset+item->size - 1) /8)) {245 if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) { 234 246 235 247 part_size = 0; 236 248 237 size_t i = 0; 238 for (i = (size_t) (offset / 8); 239 i <= (size_t) (offset + item->size - 1) / 8; i++) { 240 if (i == (size_t) (offset / 8)) { 241 /* the higher one */ 249 size_t i=0; 250 for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){ 251 if(i == (size_t)(offset/8)) { 252 // the higher one 242 253 part_size = 8 - (offset % 8); 243 254 foo = data + i; 244 mask = ((1 << (item->size - part_size)) -1);255 mask = ((1 << (item->size-part_size))-1); 245 256 value = (*foo & mask); 246 } else if (i == ((offset + item->size - 1) / 8)) { 247 /* the lower one */ 257 } 258 else if(i == ((offset+item->size-1)/8)){ 259 // the lower one 248 260 foo = data + i; 249 mask = ((1 << (item->size - part_size)) - 1) <<250 261 mask = ((1 << (item->size - part_size)) - 1) 262 << (8 - (item->size - part_size)); 251 263 252 264 value = (((*foo & mask) >> (8 - 253 (item->size - part_size))) << part_size ) +254 value;255 } else {256 value = (*(data + 1) << (part_size + 8)) +257 265 (item->size - part_size))) << part_size ) 266 + value; 267 } 268 else { 269 value = (*(data + 1) << (part_size + 8)) + value; 258 270 part_size += 8; 259 271 } 260 272 } 261 } else {262 foo = data + (offset / 8);263 mask = ((1 << item->size) - 1) <<264 (8 - ((offset % 8) +item->size));265 value = (*foo & mask) >> (8 - ((offset % 8) +item->size));266 } 267 268 if ((item->logical_minimum < 0) || (item->logical_maximum < 0)){273 } 274 else { 275 foo = data+(offset/8); 276 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size)); 277 value = (*foo & mask) >> (8-((offset%8)+item->size)); 278 } 279 280 if((item->logical_minimum < 0) || (item->logical_maximum < 0)){ 269 281 value = USB_HID_UINT32_TO_INT32(value, item->size); 270 282 } 271 283 272 return (int) (((value - item->logical_minimum) / resolution) + 273 item->physical_minimum); 284 return (int)(((value - item->logical_minimum) / resolution) + 285 item->physical_minimum); 286 274 287 } 275 288 … … 286 299 */ 287 300 uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, 288 289 { 290 if 301 uint8_t report_id) 302 { 303 if(report == NULL) { 291 304 *size = 0; 292 305 return NULL; … … 297 310 list_foreach(report->reports, report_it) { 298 311 report_des = list_get_instance(report_it, 299 312 usb_hid_report_description_t, reports_link); 300 313 301 if 302 (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){314 if((report_des->report_id == report_id) && 315 (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){ 303 316 break; 304 317 } 305 318 } 306 319 307 if (report_des == NULL){320 if(report_des == NULL){ 308 321 *size = 0; 309 322 return NULL; 310 } else { 311 *size = (report_des->bit_length + (8 - 1)) / 8; 323 } 324 else { 325 *size = (report_des->bit_length + (8 - 1))/8; 312 326 uint8_t *ret = malloc((*size) * sizeof(uint8_t)); 313 327 memset(ret, 0, (*size) * sizeof(uint8_t)); … … 323 337 */ 324 338 void usb_hid_report_output_free(uint8_t *output) 325 { 326 if (output != NULL) { 327 free(output); 339 340 { 341 if(output != NULL) { 342 free (output); 328 343 } 329 344 } … … 339 354 */ 340 355 int usb_hid_report_output_translate(usb_hid_report_t *report, 341 342 { 343 int32_t value =0;356 uint8_t report_id, uint8_t *buffer, size_t size) 357 { 358 int32_t value=0; 344 359 int offset; 345 360 int length; 346 361 int32_t tmp_value; 347 362 348 if 363 if(report == NULL) { 349 364 return EINVAL; 350 365 } 351 366 352 if 367 if(report->use_report_ids != 0) { 353 368 buffer[0] = report_id; 354 369 } 355 370 356 371 usb_hid_report_description_t *report_des; 357 report_des = usb_hid_report_find_description (report, report_id,358 359 360 if (report_des == NULL){372 report_des = usb_hid_report_find_description (report, report_id, 373 USB_HID_REPORT_TYPE_OUTPUT); 374 375 if(report_des == NULL){ 361 376 return EINVAL; 362 377 } … … 369 384 370 385 value = usb_hid_translate_data_reverse(report_item, 371 386 report_item->value); 372 387 373 388 offset = report_des->bit_length - report_item->offset - 1; … … 376 391 usb_log_debug("\ttranslated value: %x\n", value); 377 392 378 if ((offset / 8) == ((offset + length - 1) / 8)) { 379 if (((size_t) (offset / 8) >= size) || 380 ((size_t) (offset + length - 1) / 8) >= size) { 393 if((offset/8) == ((offset+length-1)/8)) { 394 // je to v jednom bytu 395 if(((size_t)(offset/8) >= size) || 396 ((size_t)(offset+length-1)/8) >= size) { 381 397 break; // TODO ErrorCode 382 398 } 383 size_t shift = 8 - offset %8 - length;399 size_t shift = 8 - offset%8 - length; 384 400 value = value << shift; 385 value = value & (((1 << length) -1) << shift);401 value = value & (((1 << length)-1) << shift); 386 402 387 403 uint8_t mask = 0; 388 404 mask = 0xff - (((1 << length) - 1) << shift); 389 buffer[offset / 8] = (buffer[offset / 8] & mask) |390 value;391 }else {405 buffer[offset/8] = (buffer[offset/8] & mask) | value; 406 } 407 else { 392 408 int i = 0; 393 409 uint8_t mask = 0; 394 for (i = (offset / 8); 395 i <= ((offset + length - 1) / 8); i++) { 396 if (i == (offset / 8)) { 410 for(i = (offset/8); i <= ((offset+length-1)/8); i++) { 411 if(i == (offset/8)) { 397 412 tmp_value = value; 398 413 tmp_value = tmp_value & 399 ((1 << (8 - (offset % 8))) -1);400 401 tmp_value = tmp_value << (offset %8);402 403 mask = ~(((1 << (8 - (offset % 8))) - 1)404 << (offset %8));414 ((1 << (8-(offset%8)))-1); 415 416 tmp_value = tmp_value << (offset%8); 417 418 mask = ~(((1 << (8-(offset%8)))-1) << 419 (offset%8)); 405 420 406 421 buffer[i] = (buffer[i] & mask) | 407 tmp_value; 408 } else if (i == ((offset + length - 1) / 8)) { 422 tmp_value; 423 } 424 else if (i == ((offset + length -1)/8)) { 409 425 410 426 value = value >> (length - 411 427 ((offset + length) % 8)); 412 428 413 429 value = value & ((1 << (length - 414 430 ((offset + length) % 8))) - 1); 415 431 416 432 mask = (1 << (length - 417 433 ((offset + length) % 8))) - 1; 418 434 419 435 buffer[i] = (buffer[i] & mask) | value; 420 } else { 421 buffer[i] = value & (0xff << i); 436 } 437 else { 438 buffer[i] = value & (0xFF << i); 422 439 } 423 440 } 424 441 } 425 442 426 / * reset value */443 // reset value 427 444 report_item->value = 0; 428 445 } … … 439 456 */ 440 457 uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, 441 442 { 443 int ret =0;458 int value) 459 { 460 int ret=0; 444 461 int resolution; 445 462 446 if 463 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) { 447 464 ret = item->logical_minimum; 448 465 } 449 466 450 if ((item->physical_minimum == 0) && (item->physical_maximum == 0)){467 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){ 451 468 item->physical_minimum = item->logical_minimum; 452 469 item->physical_maximum = item->logical_maximum; 453 470 } 454 471 455 / * variable item */456 if (item->physical_maximum == item->physical_minimum){472 // variable item 473 if(item->physical_maximum == item->physical_minimum){ 457 474 resolution = 1; 458 } else { 475 } 476 else { 459 477 resolution = (item->logical_maximum - item->logical_minimum) / 460 478 ((item->physical_maximum - item->physical_minimum) * 461 (usb_pow(10, 479 (usb_pow(10,(item->unit_exponent)))); 462 480 } 463 481 464 482 ret = ((value - item->physical_minimum) * resolution) + 465 466 467 usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), "468 "ret(%x)\n", value, resolution, item->physical_minimum,469 470 471 if ((item->logical_minimum < 0) || (item->logical_maximum < 0)){483 item->logical_minimum; 484 485 usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), \ 486 ret(%x)\n", value, resolution, item->physical_minimum, 487 item->logical_minimum, ret); 488 489 if((item->logical_minimum < 0) || (item->logical_maximum < 0)){ 472 490 return USB_HID_INT32_TO_UINT32(ret, item->size); 473 491 } 474 475 return (int32_t) 0 + ret; 492 return (int32_t)0 + ret; 476 493 } 477 494 … … 484 501 */ 485 502 usb_hid_report_item_t *usb_hid_report_item_clone( 486 503 const usb_hid_report_item_t *item) 487 504 { 488 505 usb_hid_report_item_t *new_report_item; 489 506 490 if 507 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) { 491 508 return NULL; 492 509 } … … 512 529 */ 513 530 usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report, 514 515 531 usb_hid_report_field_t *field, usb_hid_report_path_t *path, int flags, 532 usb_hid_report_type_t type) 516 533 { 517 534 usb_hid_report_description_t *report_des = 518 535 usb_hid_report_find_description(report, path->report_id, type); 519 536 520 537 link_t *field_it; 521 538 522 if (report_des == NULL){539 if(report_des == NULL){ 523 540 return NULL; 524 541 } 525 542 526 if (field == NULL){543 if(field == NULL){ 527 544 field_it = report_des->report_items.head.next; 528 } else { 545 } 546 else { 529 547 field_it = field->ritems_link.next; 530 548 } 531 549 532 while 550 while(field_it != &report_des->report_items.head) { 533 551 field = list_get_instance(field_it, usb_hid_report_field_t, 534 ritems_link); 535 536 if (USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) { 537 usb_hid_report_path_append_item(field->collection_path, 538 field->usage_page, field->usage); 539 540 if (usb_hid_report_compare_usage_path( 541 field->collection_path, path, flags) == EOK) { 552 ritems_link); 553 554 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) { 555 usb_hid_report_path_append_item ( 556 field->collection_path, field->usage_page, 557 field->usage); 558 559 if(usb_hid_report_compare_usage_path( 560 field->collection_path, path, flags) == EOK){ 561 542 562 usb_hid_report_remove_last_item( 543 field->collection_path); 563 field->collection_path); 564 544 565 return field; 545 566 } 546 usb_hid_report_remove_last_item(field->collection_path); 567 usb_hid_report_remove_last_item ( 568 field->collection_path); 547 569 } 548 570 field_it = field_it->next; … … 564 586 * @retval report_id otherwise 565 587 */ 566 uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report, uint8_t report_id,567 568 { 569 if (report == NULL){588 uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report, 589 uint8_t report_id, usb_hid_report_type_t type) 590 { 591 if(report == NULL){ 570 592 return 0; 571 593 } … … 574 596 link_t *report_it; 575 597 576 if 598 if(report_id > 0) { 577 599 report_des = usb_hid_report_find_description(report, report_id, 578 579 if 600 type); 601 if(report_des == NULL) { 580 602 return 0; 581 } else { 603 } 604 else { 582 605 report_it = report_des->reports_link.next; 583 606 } 584 } else { 607 } 608 else { 585 609 report_it = report->reports.head.next; 586 610 } 587 611 588 while 612 while(report_it != &report->reports.head) { 589 613 report_des = list_get_instance(report_it, 590 591 592 if (report_des->type == type){614 usb_hid_report_description_t, reports_link); 615 616 if(report_des->type == type){ 593 617 return report_des->report_id; 594 618 } … … 611 635 void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item) 612 636 { 613 if (report_item == NULL){637 if(report_item == NULL) { 614 638 return; 615 639 } … … 627 651 report_item->string_minimum = 0; 628 652 report_item->string_maximum = 0; 629 } 630 653 654 return; 655 } 631 656 /** 632 657 * @}
Note:
See TracChangeset
for help on using the changeset viewer.