source: mainline/uspace/lib/usb/src/hidparser.c@ 7787dd81

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7787dd81 was 70a71e5, checked in by Matej Klonfar <maklf@…>, 14 years ago

Output API fix

  • Property mode set to 100644
File size: 32.9 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
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
42/** */
43#define USB_HID_NEW_REPORT_ITEM 1
44
45/** */
46#define USB_HID_NO_ACTION 2
47
48/** */
49#define USB_HID_UNKNOWN_TAG -99
50
51/*
52 * Private descriptor parser functions
53 */
54int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
55 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
56int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
57 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
58int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
59 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
60int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
61 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
62
63void usb_hid_descriptor_print_list(link_t *head);
64int usb_hid_report_reset_local_items();
65void usb_hid_free_report_list(link_t *head);
66
67/*
68 * Data translation private functions
69 */
70int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
71inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
72int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j);
73int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int32_t value);
74int usb_pow(int a, int b);
75
76// TODO: tohle ma bejt asi jinde
77int usb_pow(int a, int b)
78{
79 switch(b) {
80 case 0:
81 return 1;
82 break;
83 case 1:
84 return a;
85 break;
86 default:
87 return a * usb_pow(a, b-1);
88 break;
89 }
90}
91
92/**
93 * Initialize the report descriptor parser structure
94 *
95 * @param parser Report descriptor parser structure
96 * @return Error code
97 */
98int usb_hid_parser_init(usb_hid_report_parser_t *parser)
99{
100 if(parser == NULL) {
101 return EINVAL;
102 }
103
104 list_initialize(&(parser->input));
105 list_initialize(&(parser->output));
106 list_initialize(&(parser->feature));
107
108 return EOK;
109}
110
111
112/** Parse HID report descriptor.
113 *
114 * @param parser Opaque HID report parser structure.
115 * @param data Data describing the report.
116 * @return Error code.
117 */
118int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
119 const uint8_t *data, size_t size)
120{
121 size_t i=0;
122 uint8_t tag=0;
123 uint8_t item_size=0;
124 int class=0;
125 int ret;
126 usb_hid_report_item_t *report_item=0;
127 usb_hid_report_item_t *new_report_item;
128 usb_hid_report_path_t *usage_path;
129 usb_hid_report_path_t *tmp_usage_path;
130
131 size_t offset_input=0;
132 size_t offset_output=0;
133 size_t offset_feature=0;
134
135
136 /* parser structure initialization*/
137 if(usb_hid_parser_init(parser) != EOK) {
138 return EINVAL;
139 }
140
141
142 /*report item initialization*/
143 if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
144 return ENOMEM;
145 }
146 memset(report_item, 0, sizeof(usb_hid_report_item_t));
147 list_initialize(&(report_item->link));
148
149 /* usage path context initialization */
150 if(!(usage_path=usb_hid_report_path())){
151 return ENOMEM;
152 }
153
154 while(i<size){
155 if(!USB_HID_ITEM_IS_LONG(data[i])){
156
157 if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
158 return EINVAL; // TODO ERROR CODE
159 }
160
161 tag = USB_HID_ITEM_TAG(data[i]);
162 item_size = USB_HID_ITEM_SIZE(data[i]);
163 class = USB_HID_ITEM_TAG_CLASS(data[i]);
164
165 usb_log_debug2(
166 "i(%u) data(%X) value(%X): TAG %u, class %u, size %u - ", i,
167 data[i], usb_hid_report_tag_data_int32(data+i+1,item_size),
168 tag, class, item_size);
169
170 ret = usb_hid_report_parse_tag(tag,class,data+i+1,
171 item_size,report_item, usage_path);
172 usb_log_debug2("ret: %u\n", ret);
173 switch(ret){
174 case USB_HID_NEW_REPORT_ITEM:
175 // store report item to report and create the new one
176 usb_log_debug("\nNEW REPORT ITEM: %X",ret);
177
178 // store current usage path
179 report_item->usage_path = usage_path;
180
181 // clone path to the new one
182 tmp_usage_path = usb_hid_report_path_clone(usage_path);
183
184 // swap
185 usage_path = tmp_usage_path;
186 tmp_usage_path = NULL;
187
188
189 switch(tag) {
190 case USB_HID_REPORT_TAG_INPUT:
191 report_item->offset = offset_input;
192 offset_input += report_item->count * report_item->size;
193 usb_log_debug(" - INPUT\n");
194 list_append(&(report_item->link), &(parser->input));
195 break;
196 case USB_HID_REPORT_TAG_OUTPUT:
197 report_item->offset = offset_output;
198 offset_output += report_item->count * report_item->size;
199 usb_log_debug(" - OUTPUT\n");
200 list_append(&(report_item->link), &(parser->output));
201
202 break;
203 case USB_HID_REPORT_TAG_FEATURE:
204 report_item->offset = offset_feature;
205 offset_feature += report_item->count * report_item->size;
206 usb_log_debug(" - FEATURE\n");
207 list_append(&(report_item->link), &(parser->feature));
208 break;
209 default:
210 usb_log_debug("\tjump over - tag %X\n", tag);
211 break;
212 }
213
214 /* clone current state table to the new item */
215 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
216 return ENOMEM;
217 }
218 memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));
219 /* reset local items */
220 new_report_item->usage_minimum = 0;
221 new_report_item->usage_maximum = 0;
222
223 link_initialize(&(new_report_item->link));
224 report_item = new_report_item;
225
226 break;
227 case USB_HID_REPORT_TAG_PUSH:
228 // push current state to stack
229 // not yet implemented
230 break;
231 case USB_HID_REPORT_TAG_POP:
232 // restore current state from stack
233 // not yet implemented
234 break;
235
236 default:
237 // nothing special to do
238 break;
239 }
240
241 /* jump over the processed block */
242 i += 1 + USB_HID_ITEM_SIZE(data[i]);
243 }
244 else{
245 // TBD
246 i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
247 }
248
249
250 }
251
252 return EOK;
253}
254
255
256/**
257 * Parse input report.
258 *
259 * @param data Data for report
260 * @param size Size of report
261 * @param callbacks Callbacks for report actions
262 * @param arg Custom arguments
263 *
264 * @return Error code
265 */
266int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
267 const usb_hid_report_in_callbacks_t *callbacks, void *arg)
268{
269 int i;
270 usb_hid_report_item_t item;
271
272 /* fill item due to the boot protocol report descriptor */
273 // modifier keys are in the first byte
274 uint8_t modifiers = data[0];
275
276 item.offset = 2; /* second byte is reserved */
277 item.size = 8;
278 item.count = 6;
279 item.usage_minimum = 0;
280 item.usage_maximum = 255;
281 item.logical_minimum = 0;
282 item.logical_maximum = 255;
283
284 if (size != 8) {
285 return -1; //ERANGE;
286 }
287
288 uint8_t keys[6];
289 for (i = 0; i < item.count; i++) {
290 keys[i] = data[i + item.offset];
291 }
292
293 callbacks->keyboard(keys, 6, modifiers, arg);
294 return EOK;
295}
296
297/**
298 * Makes output report for keyboard boot protocol
299 *
300 * @param leds
301 * @param output Output report data buffer
302 * @param size Size of the output buffer
303 * @return Error code
304 */
305int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
306{
307 if(size != 1){
308 return -1;
309 }
310
311 /* used only first five bits, others are only padding*/
312 *data = leds;
313 return EOK;
314}
315
316/**
317 * Parse one tag of the report descriptor
318 *
319 * @param Tag to parse
320 * @param Report descriptor buffer
321 * @param Size of data belongs to this tag
322 * @param Current report item structe
323 * @return Code of action to be done next
324 */
325int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
326 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
327{
328 int ret;
329
330 switch(class){
331 case USB_HID_TAG_CLASS_MAIN:
332
333 if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
334 return USB_HID_NEW_REPORT_ITEM;
335 }
336 else {
337 /*TODO process the error */
338 return ret;
339 }
340 break;
341
342 case USB_HID_TAG_CLASS_GLOBAL:
343 return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
344 break;
345
346 case USB_HID_TAG_CLASS_LOCAL:
347 return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
348 break;
349 default:
350 return USB_HID_NO_ACTION;
351 }
352}
353
354/**
355 * Parse main tags of report descriptor
356 *
357 * @param Tag identifier
358 * @param Data buffer
359 * @param Length of data buffer
360 * @param Current state table
361 * @return Error code
362 */
363
364int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
365 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
366{
367 switch(tag)
368 {
369 case USB_HID_REPORT_TAG_INPUT:
370 case USB_HID_REPORT_TAG_OUTPUT:
371 case USB_HID_REPORT_TAG_FEATURE:
372 report_item->item_flags = *data;
373 return EOK;
374 break;
375
376 case USB_HID_REPORT_TAG_COLLECTION:
377 usb_hid_report_path_append_item(usage_path, 0, 0);
378
379 return USB_HID_NO_ACTION;
380 break;
381
382 case USB_HID_REPORT_TAG_END_COLLECTION:
383 // TODO
384 // znici posledni uroven ve vsech usage paths
385 // otazka jestli nema nicit dve, respektive novou posledni vynulovat?
386 usb_hid_report_remove_last_item(usage_path);
387 return USB_HID_NO_ACTION;
388 break;
389 default:
390 return USB_HID_NO_ACTION;
391 }
392
393 return EOK;
394}
395
396/**
397 * Parse global tags of report descriptor
398 *
399 * @param Tag identifier
400 * @param Data buffer
401 * @param Length of data buffer
402 * @param Current state table
403 * @return Error code
404 */
405int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
406 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
407{
408 // TODO take care about the bit length of data
409 switch(tag)
410 {
411 case USB_HID_REPORT_TAG_USAGE_PAGE:
412 // zmeni to jenom v poslednim poli aktualni usage path
413 usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL,
414 usb_hid_report_tag_data_int32(data,item_size));
415 break;
416 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
417 report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
418 break;
419 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
420 report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
421 break;
422 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
423 report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
424 break;
425 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
426 report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
427 break;
428 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
429 report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
430 break;
431 case USB_HID_REPORT_TAG_UNIT:
432 report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
433 break;
434 case USB_HID_REPORT_TAG_REPORT_SIZE:
435 report_item->size = usb_hid_report_tag_data_int32(data,item_size);
436 break;
437 case USB_HID_REPORT_TAG_REPORT_COUNT:
438 report_item->count = usb_hid_report_tag_data_int32(data,item_size);
439 break;
440 case USB_HID_REPORT_TAG_REPORT_ID:
441 report_item->id = usb_hid_report_tag_data_int32(data,item_size);
442 break;
443 case USB_HID_REPORT_TAG_PUSH:
444 case USB_HID_REPORT_TAG_POP:
445 return tag;
446 break;
447
448 default:
449 return USB_HID_NO_ACTION;
450 }
451
452 return EOK;
453}
454
455/**
456 * Parse local tags of report descriptor
457 *
458 * @param Tag identifier
459 * @param Data buffer
460 * @param Length of data buffer
461 * @param Current state table
462 * @return Error code
463 */
464int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
465 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
466{
467 switch(tag)
468 {
469 case USB_HID_REPORT_TAG_USAGE:
470 usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL,
471 usb_hid_report_tag_data_int32(data,item_size));
472 break;
473 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
474 report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
475 break;
476 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
477 report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
478 break;
479 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
480 report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
481 break;
482 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
483 report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
484 break;
485 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
486 report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
487 break;
488 case USB_HID_REPORT_TAG_STRING_INDEX:
489 report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
490 break;
491 case USB_HID_REPORT_TAG_STRING_MINIMUM:
492 report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
493 break;
494 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
495 report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
496 break;
497 case USB_HID_REPORT_TAG_DELIMITER:
498 report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
499 break;
500
501 default:
502 return USB_HID_NO_ACTION;
503 }
504
505 return EOK;
506}
507
508/**
509 * Converts raw data to int32 (thats the maximum length of short item data)
510 *
511 * @param Data buffer
512 * @param Size of buffer
513 * @return Converted int32 number
514 */
515int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
516{
517 unsigned int i;
518 int32_t result;
519
520 result = 0;
521 for(i=0; i<size; i++) {
522 result = (result | (data[i]) << (i*8));
523 }
524
525 return result;
526}
527
528
529
530/**
531 * Prints content of given list of report items.
532 *
533 * @param List of report items (usb_hid_report_item_t)
534 * @return void
535 */
536void usb_hid_descriptor_print_list(link_t *head)
537{
538 usb_hid_report_item_t *report_item;
539 usb_hid_report_usage_path_t *path_item;
540 link_t *path;
541 link_t *item;
542
543 if(head == NULL || list_empty(head)) {
544 usb_log_debug("\tempty\n");
545 return;
546 }
547
548 for(item = head->next; item != head; item = item->next) {
549
550 report_item = list_get_instance(item, usb_hid_report_item_t, link);
551
552 usb_log_debug("\tOFFSET: %X\n", report_item->offset);
553 usb_log_debug("\tCOUNT: %X\n", report_item->count);
554 usb_log_debug("\tSIZE: %X\n", report_item->size);
555 usb_log_debug("\tCONSTANT/VAR: %X\n", USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags));
556 usb_log_debug("\tVARIABLE/ARRAY: %X\n", USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags));
557 usb_log_debug("\tUSAGE PATH:\n");
558
559 path = report_item->usage_path->link.next;
560 while(path != &report_item->usage_path->link) {
561 path_item = list_get_instance(path, usb_hid_report_usage_path_t, link);
562 usb_log_debug("\t\tUSAGE PAGE: %X, USAGE: %X\n", path_item->usage_page, path_item->usage);
563 path = path->next;
564 }
565
566 usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum);
567 usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum);
568 usb_log_debug("\tPHYMIN: %X\n", report_item->physical_minimum);
569 usb_log_debug("\tPHYMAX: %X\n", report_item->physical_maximum);
570 usb_log_debug("\tUSAGEMIN: %X\n", report_item->usage_minimum);
571 usb_log_debug("\tUSAGEMAX: %X\n", report_item->usage_maximum);
572
573 usb_log_debug("\n");
574
575 }
576
577
578}
579/**
580 * Prints content of given report descriptor in human readable format.
581 *
582 * @param parser Parsed descriptor to print
583 * @return void
584 */
585void usb_hid_descriptor_print(usb_hid_report_parser_t *parser)
586{
587 if(parser == NULL) {
588 return;
589 }
590
591 usb_log_debug("INPUT:\n");
592 usb_hid_descriptor_print_list(&parser->input);
593
594 usb_log_debug("OUTPUT: \n");
595 usb_hid_descriptor_print_list(&parser->output);
596
597 usb_log_debug("FEATURE:\n");
598 usb_hid_descriptor_print_list(&parser->feature);
599
600}
601
602/**
603 * Releases whole linked list of report items
604 *
605 * @param head Head of list of report descriptor items (usb_hid_report_item_t)
606 * @return void
607 */
608void usb_hid_free_report_list(link_t *head)
609{
610 return;
611
612 usb_hid_report_item_t *report_item;
613 link_t *next;
614
615 if(head == NULL || list_empty(head)) {
616 return;
617 }
618
619 next = head->next;
620 while(next != head) {
621
622 report_item = list_get_instance(next, usb_hid_report_item_t, link);
623
624 while(!list_empty(&report_item->usage_path->link)) {
625 usb_hid_report_remove_last_item(report_item->usage_path);
626 }
627
628
629 next = next->next;
630
631 free(report_item);
632 }
633
634 return;
635
636}
637
638/** Frees the HID report descriptor parser structure
639 *
640 * @param parser Opaque HID report parser structure
641 * @return void
642 */
643void usb_hid_free_report_parser(usb_hid_report_parser_t *parser)
644{
645 if(parser == NULL){
646 return;
647 }
648
649 usb_hid_free_report_list(&parser->input);
650 usb_hid_free_report_list(&parser->output);
651 usb_hid_free_report_list(&parser->feature);
652
653 return;
654}
655
656/** Parse and act upon a HID report.
657 *
658 * @see usb_hid_parse_report_descriptor
659 *
660 * @param parser Opaque HID report parser structure.
661 * @param data Data for the report.
662 * @param callbacks Callbacks for report actions.
663 * @param arg Custom argument (passed through to the callbacks).
664 * @return Error code.
665 */
666int usb_hid_parse_report(const usb_hid_report_parser_t *parser,
667 const uint8_t *data, size_t size,
668 usb_hid_report_path_t *path, int flags,
669 const usb_hid_report_in_callbacks_t *callbacks, void *arg)
670{
671 link_t *list_item;
672 usb_hid_report_item_t *item;
673 uint8_t *keys;
674 uint8_t item_value;
675 size_t key_count=0;
676 size_t i=0;
677 size_t j=0;
678
679 if(parser == NULL) {
680 return EINVAL;
681 }
682
683 /* get the size of result array */
684 key_count = usb_hid_report_input_length(parser, path, flags);
685
686 if(!(keys = malloc(sizeof(uint8_t) * key_count))){
687 return ENOMEM;
688 }
689
690 /* read data */
691 list_item = parser->input.next;
692 while(list_item != &(parser->input)) {
693
694 item = list_get_instance(list_item, usb_hid_report_item_t, link);
695 if(!USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) &&
696 (usb_hid_report_compare_usage_path(item->usage_path, path, flags) == EOK)) {
697 for(j=0; j<(size_t)(item->count); j++) {
698 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) ||
699 ((item->usage_minimum == 0) && (item->usage_maximum == 0))) {
700 // variable item
701 keys[i++] = usb_hid_translate_data(item, data,j);
702 }
703 else {
704 // bitmapa
705 if((item_value = usb_hid_translate_data(item, data, j)) != 0) {
706 keys[i++] = (item->count - 1 - j) + item->usage_minimum;
707 }
708 else {
709 keys[i++] = 0;
710 }
711 }
712 }
713 }
714 list_item = list_item->next;
715 }
716
717 callbacks->keyboard(keys, key_count, 0, arg);
718
719 free(keys);
720 return EOK;
721
722}
723
724/**
725 * Translate data from the report as specified in report descriptor
726 *
727 * @param item Report descriptor item with definition of translation
728 * @param data Data to translate
729 * @param j Index of processed field in report descriptor item
730 * @return Translated data
731 */
732int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j)
733{
734 int resolution;
735 int offset;
736 int part_size;
737
738 int32_t value;
739 int32_t mask;
740 const uint8_t *foo;
741
742 // now only common numbers llowed
743 if(item->size > 32) {
744 return 0;
745 }
746
747 if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
748 item->physical_minimum = item->logical_minimum;
749 item->physical_maximum = item->logical_maximum;
750 }
751
752 if(item->physical_maximum == item->physical_minimum){
753 resolution = 1;
754 }
755 else {
756 resolution = (item->logical_maximum - item->logical_minimum) /
757 ((item->physical_maximum - item->physical_minimum) *
758 (usb_pow(10,(item->unit_exponent))));
759 }
760 offset = item->offset + (j * item->size);
761
762 // FIXME
763 if((offset/8) != ((offset+item->size)/8)) {
764 usb_log_debug2("offset %d\n", offset);
765
766 part_size = ((offset+item->size)%8);
767 usb_log_debug2("part size %d\n",part_size);
768
769 // the higher one
770 foo = data+(offset/8);
771 mask = ((1 << (item->size-part_size))-1);
772 value = (*foo & mask) << part_size;
773
774 usb_log_debug2("hfoo %x\n", *foo);
775 usb_log_debug2("hmaska %x\n", mask);
776 usb_log_debug2("hval %d\n", value);
777
778 // the lower one
779 foo = data+((offset+item->size)/8);
780 mask = ((1 << part_size)-1) << (8-part_size);
781 value += ((*foo & mask) >> (8-part_size));
782
783 usb_log_debug2("lfoo %x\n", *foo);
784 usb_log_debug2("lmaska %x\n", mask);
785 usb_log_debug2("lval %d\n", ((*foo & mask) >> (8-(item->size-part_size))));
786 usb_log_debug2("val %d\n", value);
787
788
789 }
790 else {
791 foo = data+(offset/8);
792 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
793 value = (*foo & mask) >> (8-((offset%8)+item->size));
794
795 usb_log_debug2("offset %d\n", offset);
796
797 usb_log_debug2("foo %x\n", *foo);
798 usb_log_debug2("maska %x\n", mask);
799 usb_log_debug2("val %d\n", value);
800 }
801
802 usb_log_debug2("---\n\n");
803
804 return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
805
806}
807
808/**
809 *
810 *
811 * @param parser
812 * @param path
813 * @param flags
814 * @return
815 */
816size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
817 usb_hid_report_path_t *path, int flags)
818{
819 size_t ret = 0;
820 link_t *item;
821 usb_hid_report_item_t *report_item;
822
823 if(parser == NULL) {
824 return 0;
825 }
826
827 item = parser->input.next;
828 while(&parser->input != item) {
829 report_item = list_get_instance(item, usb_hid_report_item_t, link);
830 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
831 (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
832 ret += report_item->count;
833 }
834
835 item = item->next;
836 }
837
838 return ret;
839}
840
841
842/**
843 *
844 * @param usage_path
845 * @param usage_page
846 * @param usage
847 * @return
848 */
849int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
850 int32_t usage_page, int32_t usage)
851{
852 usb_hid_report_usage_path_t *item;
853
854 if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
855 return ENOMEM;
856 }
857 list_initialize(&item->link);
858
859 item->usage = usage;
860 item->usage_page = usage_page;
861
862 list_append (&usage_path->link, &item->link);
863 usage_path->depth++;
864 return EOK;
865}
866
867/**
868 *
869 * @param usage_path
870 * @return
871 */
872void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
873{
874 usb_hid_report_usage_path_t *item;
875
876 if(!list_empty(&usage_path->link)){
877 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
878 list_remove(usage_path->link.prev);
879 usage_path->depth--;
880 free(item);
881 }
882}
883
884/**
885 *
886 * @param usage_path
887 * @return
888 */
889void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
890{
891 usb_hid_report_usage_path_t *item;
892
893 if(!list_empty(&usage_path->link)){
894 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
895 memset(item, 0, sizeof(usb_hid_report_usage_path_t));
896 }
897}
898
899/**
900 *
901 * @param usage_path
902 * @param tag
903 * @param data
904 * @return
905 */
906void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
907{
908 usb_hid_report_usage_path_t *item;
909
910 if(!list_empty(&usage_path->link)){
911 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
912
913 switch(tag) {
914 case USB_HID_TAG_CLASS_GLOBAL:
915 item->usage_page = data;
916 break;
917 case USB_HID_TAG_CLASS_LOCAL:
918 item->usage = data;
919 break;
920 }
921 }
922
923}
924
925/**
926 *
927 *
928 * @param report_path
929 * @param path
930 * @param flags
931 * @return
932 */
933int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
934 usb_hid_report_path_t *path,
935 int flags)
936{
937 usb_hid_report_usage_path_t *report_item;
938 usb_hid_report_usage_path_t *path_item;
939
940 link_t *report_link;
941 link_t *path_link;
942
943 int only_page;
944
945 if(path->depth == 0){
946 return EOK;
947 }
948
949
950 if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
951 flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
952 }
953
954 switch(flags){
955 /* path must be completly identical */
956 case USB_HID_PATH_COMPARE_STRICT:
957 if(report_path->depth != path->depth){
958 return 1;
959 }
960
961 report_link = report_path->link.next;
962 path_link = path->link.next;
963
964 while((report_link != &report_path->link) && (path_link != &path->link)) {
965 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
966 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
967
968 if((report_item->usage_page != path_item->usage_page) ||
969 ((only_page == 0) && (report_item->usage != path_item->usage))) {
970 return 1;
971 } else {
972 report_link = report_link->next;
973 path_link = path_link->next;
974 }
975
976 }
977
978 if((report_link == &report_path->link) && (path_link == &path->link)) {
979 return EOK;
980 }
981 else {
982 return 1;
983 }
984 break;
985
986 /* compare with only the end of path*/
987 case USB_HID_PATH_COMPARE_END:
988 report_link = report_path->link.prev;
989 path_link = path->link.prev;
990
991 if(list_empty(&path->link)){
992 return EOK;
993 }
994
995 while((report_link != &report_path->link) && (path_link != &path->link)) {
996 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
997 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
998
999 if((report_item->usage_page != path_item->usage_page) ||
1000 ((only_page == 0) && (report_item->usage != path_item->usage))) {
1001 return 1;
1002 } else {
1003 report_link = report_link->prev;
1004 path_link = path_link->prev;
1005 }
1006
1007 }
1008
1009 if(path_link == &path->link) {
1010 return EOK;
1011 }
1012 else {
1013 return 1;
1014 }
1015
1016 break;
1017
1018 default:
1019 return EINVAL;
1020 }
1021
1022
1023
1024
1025}
1026
1027/**
1028 *
1029 * @return
1030 */
1031usb_hid_report_path_t *usb_hid_report_path(void)
1032{
1033 usb_hid_report_path_t *path;
1034 path = malloc(sizeof(usb_hid_report_path_t));
1035 if(!path){
1036 return NULL;
1037 }
1038 else {
1039 path->depth = 0;
1040 list_initialize(&path->link);
1041 return path;
1042 }
1043}
1044
1045/**
1046 *
1047 * @param path
1048 * @return void
1049 */
1050void usb_hid_report_path_free(usb_hid_report_path_t *path)
1051{
1052 while(!list_empty(&path->link)){
1053 usb_hid_report_remove_last_item(path);
1054 }
1055}
1056
1057
1058/**
1059 * Clone content of given usage path to the new one
1060 *
1061 * @param usage_path
1062 * @return
1063 */
1064usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
1065{
1066 usb_hid_report_usage_path_t *path_item;
1067 link_t *path_link;
1068 usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
1069
1070 if(new_usage_path == NULL){
1071 return NULL;
1072 }
1073
1074 if(list_empty(&usage_path->link)){
1075 return new_usage_path;
1076 }
1077
1078 path_link = usage_path->link.next;
1079 while(path_link != &usage_path->link) {
1080 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
1081 usb_hid_report_path_append_item (new_usage_path, path_item->usage_page, path_item->usage);
1082
1083 path_link = path_link->next;
1084 }
1085
1086 return new_usage_path;
1087}
1088
1089
1090/*** OUTPUT API **/
1091
1092/** Allocates output report buffer
1093 *
1094 * @param parser
1095 * @param size
1096 * @return
1097 */
1098uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size)
1099{
1100 if(parser == NULL) {
1101 *size = 0;
1102 return NULL;
1103 }
1104
1105 // read the last output report item
1106 usb_hid_report_item_t *last;
1107 link_t *link;
1108
1109 link = parser->output.prev;
1110 if(link != &parser->output) {
1111 last = list_get_instance(link, usb_hid_report_item_t, link);
1112 *size = (last->offset + (last->size * last->count)) / 8;
1113
1114 uint8_t *buffer = malloc(sizeof(uint8_t) * (*size));
1115 memset(buffer, 0, sizeof(uint8_t) * (*size));
1116 usb_log_debug("output buffer: %s\n", usb_debug_str_buffer(buffer, *size, 0));
1117
1118 return buffer;
1119 }
1120 else {
1121 *size = 0;
1122 return NULL;
1123 }
1124}
1125
1126
1127/** Frees output report buffer
1128 *
1129 * @param output Output report buffer
1130 * @return
1131 */
1132void usb_hid_report_output_free(uint8_t *output)
1133
1134{
1135 if(output != NULL) {
1136 free (output);
1137 }
1138}
1139
1140/** Returns size of output for given usage path
1141 *
1142 * @param parser
1143 * @param path
1144 * @param flags
1145 * @return
1146 */
1147size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
1148 usb_hid_report_path_t *path, int flags)
1149{
1150 size_t ret = 0;
1151 link_t *item;
1152 usb_hid_report_item_t *report_item;
1153
1154 if(parser == NULL) {
1155 return 0;
1156 }
1157
1158 item = parser->output.next;
1159 while(&parser->output != item) {
1160 report_item = list_get_instance(item, usb_hid_report_item_t, link);
1161 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
1162 (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
1163 ret += report_item->count;
1164 }
1165
1166 item = item->next;
1167 }
1168
1169 return ret;
1170
1171}
1172
1173/** Updates the output report buffer by translated given data
1174 *
1175 * @param parser
1176 * @param path
1177 * @param flags
1178 * @param buffer
1179 * @param size
1180 * @param data
1181 * @param data_size
1182 * @return
1183 */
1184int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
1185 usb_hid_report_path_t *path, int flags,
1186 uint8_t *buffer, size_t size,
1187 int32_t *data, size_t data_size)
1188{
1189 usb_hid_report_item_t *report_item;
1190 link_t *item;
1191 size_t idx=0;
1192 int i=0;
1193 int32_t value=0;
1194 int offset;
1195 int length;
1196 int32_t tmp_value;
1197
1198 if(parser == NULL) {
1199 return EINVAL;
1200 }
1201
1202 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
1203 usb_log_debug("OUTPUT DATA[0]: %d, DATA[1]: %d, DATA[2]: %d\n", data[0], data[1], data[2]);
1204
1205 item = parser->output.next;
1206 while(item != &parser->output) {
1207 report_item = list_get_instance(item, usb_hid_report_item_t, link);
1208
1209 for(i=0; i<report_item->count; i++) {
1210
1211 if(idx >= data_size) {
1212 break;
1213 }
1214
1215 if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) ||
1216 ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
1217
1218// // variable item
1219 value = usb_hid_translate_data_reverse(report_item, data[idx++]);
1220 offset = report_item->offset + (i * report_item->size);
1221 length = report_item->size;
1222 }
1223 else {
1224 //bitmap
1225 value += usb_hid_translate_data_reverse(report_item, data[idx++]);
1226 offset = report_item->offset;
1227 length = report_item->size * report_item->count;
1228 }
1229
1230 if((offset/8) == ((offset+length-1)/8)) {
1231 // je to v jednom bytu
1232 if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
1233 break; // TODO ErrorCode
1234 }
1235
1236 size_t shift = offset%8;
1237
1238 value = value << shift;
1239 value = value & (((1 << length)-1) << shift);
1240
1241 uint8_t mask = 0;
1242 mask = 0xff - (((1 << length) - 1) << shift);
1243 buffer[offset/8] = (buffer[offset/8] & mask) | value;
1244 }
1245 else {
1246 // je to ve dvou!! FIXME: melo by to umet delsi jak 2
1247
1248 // konec prvniho -- dolni x bitu
1249 tmp_value = value;
1250 tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);
1251 tmp_value = tmp_value << (offset%8);
1252
1253 uint8_t mask = 0;
1254 mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
1255 buffer[offset/8] = (buffer[offset/8] & mask) | tmp_value;
1256
1257 // a ted druhej -- hornich length-x bitu
1258 value = value >> (8 - (offset % 8));
1259 value = value & ((1 << (length - (8 - (offset % 8)))) - 1);
1260
1261 mask = ((1 << (length - (8 - (offset % 8)))) - 1);
1262 buffer[(offset+length-1)/8] = (buffer[(offset+length-1)/8] & mask) | value;
1263 }
1264
1265 }
1266
1267 item = item->next;
1268 }
1269
1270 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
1271
1272 return EOK;
1273}
1274
1275/**
1276 *
1277 * @param item
1278 * @param value
1279 * @return
1280 */
1281int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int value)
1282{
1283 int ret=0;
1284 int resolution;
1285
1286 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
1287 ret = item->logical_minimum;
1288 }
1289
1290 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
1291
1292 // variable item
1293 if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
1294 item->physical_minimum = item->logical_minimum;
1295 item->physical_maximum = item->logical_maximum;
1296 }
1297
1298 if(item->physical_maximum == item->physical_minimum){
1299 resolution = 1;
1300 }
1301 else {
1302 resolution = (item->logical_maximum - item->logical_minimum) /
1303 ((item->physical_maximum - item->physical_minimum) *
1304 (usb_pow(10,(item->unit_exponent))));
1305 }
1306
1307 ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
1308 }
1309 else {
1310 // bitmapa
1311 if(value == 0) {
1312 ret = 0;
1313 }
1314 else {
1315 size_t bitmap_idx = (value - item->usage_minimum);
1316 ret = 1 << bitmap_idx;
1317 }
1318 }
1319
1320
1321 return ret;
1322}
1323
1324
1325/**
1326 * @}
1327 */
Note: See TracBrowser for help on using the repository browser.