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

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

HID parser should better support Usages in Reports fixed

  • Property mode set to 100644
File size: 45.7 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#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 */
46#define USB_HID_NEW_REPORT_ITEM 1
47
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*/
56#define USB_HID_UNKNOWN_TAG -99
57
58/*
59 * Private descriptor parser functions
60 */
61int usb_hid_report_init(usb_hid_report_t *report);
62int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item);
63usb_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);
64int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
65 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
66int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
67 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
68int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
69 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
70int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
71 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
72
73void usb_hid_print_usage_path(usb_hid_report_path_t *path);
74void usb_hid_descriptor_print_list(link_t *head);
75void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item);
76void usb_hid_free_report_list(link_t *head);
77usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item);
78/*
79 * Data translation private functions
80 */
81int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
82inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
83int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
84uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value);
85int usb_pow(int a, int b);
86
87// TODO: tohle ma bejt asi jinde
88int usb_pow(int a, int b)
89{
90 switch(b) {
91 case 0:
92 return 1;
93 break;
94 case 1:
95 return a;
96 break;
97 default:
98 return a * usb_pow(a, b-1);
99 break;
100 }
101}
102
103/**
104 * Initialize the report descriptor parser structure
105 *
106 * @param parser Report descriptor parser structure
107 * @return Error code
108 */
109int usb_hid_report_init(usb_hid_report_t *report)
110{
111 if(report == NULL) {
112 return EINVAL;
113 }
114
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;
120 return EOK;
121}
122
123int 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 for(i=0; i<report_item->usages_count; i++){
147 usb_log_debug("usages (%d) - %x\n", i, report_item->usages[i]);
148 }
149
150
151 for(i=0; i<report_item->count; i++){
152
153 field = malloc(sizeof(usb_hid_report_field_t));
154 memset(field, 0, sizeof(usb_hid_report_field_t));
155 list_initialize(&field->link);
156
157 /* fill the attributes */
158 field->collection_path = path;
159 field->logical_minimum = report_item->logical_minimum;
160 field->logical_maximum = report_item->logical_maximum;
161 field->physical_minimum = report_item->physical_minimum;
162 field->physical_maximum = report_item->physical_maximum;
163
164 field->usage_minimum = report_item->usage_minimum;
165 field->usage_maximum = report_item->usage_maximum;
166 if(report_item->extended_usage_page != 0){
167 field->usage_page = report_item->extended_usage_page;
168 }
169 else {
170 field->usage_page = report_item->usage_page;
171 }
172
173 if(report_item->usages_count > 0 && ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
174 uint32_t usage;
175 if(report_item->type == USB_HID_REPORT_TYPE_INPUT) {
176 if(i < report_item->usages_count){
177 usage = report_item->usages[i];
178 }
179 else {
180 usage = report_item->usages[report_item->usages_count - 1];
181 }
182 }
183 else {
184 if((report_item->count - i - 1) < report_item->usages_count){
185 usage = report_item->usages[(report_item->count - i - 1)];
186 }
187 else {
188 usage = report_item->usages[report_item->usages_count - 1];
189 }
190 }
191
192
193 if((usage & 0xFF00) != 0){
194 field->usage_page = (usage >> 16);
195 field->usage = (usage & 0xFF);
196 }
197 else {
198 field->usage = usage;
199 }
200
201
202 }
203
204 if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) != 0) && (!((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0)))) {
205 if(report_item->type == USB_HID_REPORT_TYPE_INPUT) {
206 field->usage = report_item->usage_maximum - i;
207 }
208 else {
209 field->usage = report_item->usage_minimum + i;
210 }
211
212 }
213
214 field->size = report_item->size;
215 field->offset = report_item->offset + (i * report_item->size);
216 if(report_item->id != 0) {
217 field->offset += 8;
218 report->use_report_ids = 1;
219 }
220 field->item_flags = report_item->item_flags;
221
222 /* find the right report list*/
223 usb_hid_report_description_t *report_des;
224 report_des = usb_hid_report_find_description(report, report_item->id, report_item->type);
225 if(report_des == NULL){
226 report_des = malloc(sizeof(usb_hid_report_description_t));
227 memset(report_des, 0, sizeof(usb_hid_report_description_t));
228
229 report_des->type = report_item->type;
230 report_des->report_id = report_item->id;
231 list_initialize (&report_des->link);
232 list_initialize (&report_des->report_items);
233
234 list_append(&report_des->link, &report->reports);
235 report->report_count++;
236 }
237
238 /* append this field to the end of founded report list */
239 list_append (&field->link, &report_des->report_items);
240
241 /* update the sizes */
242 report_des->bit_length += field->size;
243 report_des->item_length++;
244
245 }
246
247
248 return EOK;
249}
250
251usb_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)
252{
253 link_t *report_it = report->reports.next;
254 usb_hid_report_description_t *report_des = NULL;
255
256 while(report_it != &report->reports) {
257 report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
258
259 if((report_des->report_id == report_id) && (report_des->type == type)){
260 return report_des;
261 }
262
263 report_it = report_it->next;
264 }
265
266 return NULL;
267}
268
269/** Parse HID report descriptor.
270 *
271 * @param parser Opaque HID report parser structure.
272 * @param data Data describing the report.
273 * @return Error code.
274 */
275int usb_hid_parse_report_descriptor(usb_hid_report_t *report,
276 const uint8_t *data, size_t size)
277{
278 size_t i=0;
279 uint8_t tag=0;
280 uint8_t item_size=0;
281 int class=0;
282 int ret;
283 usb_hid_report_item_t *report_item=0;
284 usb_hid_report_item_t *new_report_item;
285 usb_hid_report_path_t *usage_path;
286
287 size_t offset_input=0;
288 size_t offset_output=0;
289 size_t offset_feature=0;
290
291 link_t stack;
292 list_initialize(&stack);
293
294 /* parser structure initialization*/
295 if(usb_hid_report_init(report) != EOK) {
296 return EINVAL;
297 }
298
299 /*report item initialization*/
300 if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
301 return ENOMEM;
302 }
303 memset(report_item, 0, sizeof(usb_hid_report_item_t));
304 list_initialize(&(report_item->link));
305
306 /* usage path context initialization */
307 if(!(usage_path=usb_hid_report_path())){
308 return ENOMEM;
309 }
310
311 while(i<size){
312 if(!USB_HID_ITEM_IS_LONG(data[i])){
313
314 if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
315 return EINVAL;
316 }
317
318 tag = USB_HID_ITEM_TAG(data[i]);
319 item_size = USB_HID_ITEM_SIZE(data[i]);
320 class = USB_HID_ITEM_TAG_CLASS(data[i]);
321
322 ret = usb_hid_report_parse_tag(tag,class,data+i+1,
323 item_size,report_item, usage_path);
324 switch(ret){
325 case USB_HID_NEW_REPORT_ITEM:
326 // store report item to report and create the new one
327 // store current collection path
328 report_item->usage_path = usage_path;
329
330 usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);
331 if(report_item->id != 0){
332 report->use_report_ids = 1;
333 }
334
335 switch(tag) {
336 case USB_HID_REPORT_TAG_INPUT:
337 report_item->type = USB_HID_REPORT_TYPE_INPUT;
338 report_item->offset = offset_input;
339 offset_input += report_item->count * report_item->size;
340 break;
341 case USB_HID_REPORT_TAG_OUTPUT:
342 report_item->type = USB_HID_REPORT_TYPE_OUTPUT;
343 report_item->offset = offset_output;
344 offset_output += report_item->count * report_item->size;
345
346 break;
347 case USB_HID_REPORT_TAG_FEATURE:
348 report_item->type = USB_HID_REPORT_TYPE_FEATURE;
349 report_item->offset = offset_feature;
350 offset_feature += report_item->count * report_item->size;
351 break;
352 default:
353 usb_log_debug("\tjump over - tag %X\n", tag);
354 break;
355 }
356
357 /*
358 * append new fields to the report
359 * structure
360 */
361 usb_hid_report_append_fields(report, report_item);
362
363 /* reset local items */
364 usb_hid_report_reset_local_items (report_item);
365
366 break;
367
368 case USB_HID_RESET_OFFSET:
369 offset_input = 0;
370 offset_output = 0;
371 offset_feature = 0;
372 usb_hid_report_path_set_report_id (usage_path, report_item->id);
373 break;
374
375 case USB_HID_REPORT_TAG_PUSH:
376 // push current state to stack
377 new_report_item = usb_hid_report_item_clone(report_item);
378 usb_hid_report_path_t *tmp_path = usb_hid_report_path_clone(usage_path);
379 new_report_item->usage_path = tmp_path;
380
381 list_prepend (&new_report_item->link, &stack);
382 break;
383 case USB_HID_REPORT_TAG_POP:
384 // restore current state from stack
385 if(list_empty (&stack)) {
386 return EINVAL;
387 }
388 free(report_item);
389
390 report_item = list_get_instance(stack.next, usb_hid_report_item_t, link);
391
392 usb_hid_report_usage_path_t *tmp_usage_path;
393 tmp_usage_path = list_get_instance(report_item->usage_path->link.prev, usb_hid_report_usage_path_t, link);
394
395 usb_hid_report_set_last_item(usage_path, tmp_usage_path->usage_page, tmp_usage_path->usage);
396
397 usb_hid_report_path_free(report_item->usage_path);
398 list_initialize(&report_item->usage_path->link);
399 list_remove (stack.next);
400
401 break;
402
403 default:
404 // nothing special to do
405 break;
406 }
407
408 /* jump over the processed block */
409 i += 1 + USB_HID_ITEM_SIZE(data[i]);
410 }
411 else{
412 // TBD
413 i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
414 }
415
416
417 }
418
419 return EOK;
420}
421
422
423/**
424 * Parse one tag of the report descriptor
425 *
426 * @param Tag to parse
427 * @param Report descriptor buffer
428 * @param Size of data belongs to this tag
429 * @param Current report item structe
430 * @return Code of action to be done next
431 */
432int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
433 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
434{
435 int ret;
436
437 switch(class){
438 case USB_HID_TAG_CLASS_MAIN:
439
440 if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
441 return USB_HID_NEW_REPORT_ITEM;
442 }
443 else {
444 /*TODO process the error */
445 return ret;
446 }
447 break;
448
449 case USB_HID_TAG_CLASS_GLOBAL:
450 return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
451 break;
452
453 case USB_HID_TAG_CLASS_LOCAL:
454 return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
455 break;
456 default:
457 return USB_HID_NO_ACTION;
458 }
459}
460
461/**
462 * Parse main tags of report descriptor
463 *
464 * @param Tag identifier
465 * @param Data buffer
466 * @param Length of data buffer
467 * @param Current state table
468 * @return Error code
469 */
470
471int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
472 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
473{
474 switch(tag)
475 {
476 case USB_HID_REPORT_TAG_INPUT:
477 case USB_HID_REPORT_TAG_OUTPUT:
478 case USB_HID_REPORT_TAG_FEATURE:
479 report_item->item_flags = *data;
480 return EOK;
481 break;
482
483 case USB_HID_REPORT_TAG_COLLECTION:
484 // TODO usage_path->flags = *data;
485 usb_hid_report_path_append_item(usage_path, report_item->usage_page, report_item->usages[report_item->usages_count-1]);
486 usb_hid_report_reset_local_items (report_item);
487 return USB_HID_NO_ACTION;
488 break;
489
490 case USB_HID_REPORT_TAG_END_COLLECTION:
491 usb_hid_report_remove_last_item(usage_path);
492 return USB_HID_NO_ACTION;
493 break;
494 default:
495 return USB_HID_NO_ACTION;
496 }
497
498 return EOK;
499}
500
501/**
502 * Parse global tags of report descriptor
503 *
504 * @param Tag identifier
505 * @param Data buffer
506 * @param Length of data buffer
507 * @param Current state table
508 * @return Error code
509 */
510int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
511 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
512{
513 // TODO take care about the bit length of data
514 switch(tag)
515 {
516 case USB_HID_REPORT_TAG_USAGE_PAGE:
517 report_item->usage_page = usb_hid_report_tag_data_int32(data, item_size);
518 break;
519 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
520 report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
521 break;
522 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
523 report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
524 break;
525 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
526 report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
527 break;
528 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
529 report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
530 break;
531 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
532 report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
533 break;
534 case USB_HID_REPORT_TAG_UNIT:
535 report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
536 break;
537 case USB_HID_REPORT_TAG_REPORT_SIZE:
538 report_item->size = usb_hid_report_tag_data_int32(data,item_size);
539 break;
540 case USB_HID_REPORT_TAG_REPORT_COUNT:
541 report_item->count = usb_hid_report_tag_data_int32(data,item_size);
542 break;
543 case USB_HID_REPORT_TAG_REPORT_ID:
544 report_item->id = usb_hid_report_tag_data_int32(data,item_size);
545 return USB_HID_RESET_OFFSET;
546 break;
547 case USB_HID_REPORT_TAG_PUSH:
548 case USB_HID_REPORT_TAG_POP:
549 /*
550 * stack operations are done in top level parsing
551 * function
552 */
553 return tag;
554 break;
555
556 default:
557 return USB_HID_NO_ACTION;
558 }
559
560 return EOK;
561}
562
563/**
564 * Parse local tags of report descriptor
565 *
566 * @param Tag identifier
567 * @param Data buffer
568 * @param Length of data buffer
569 * @param Current state table
570 * @return Error code
571 */
572int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
573 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
574{
575 switch(tag)
576 {
577 case USB_HID_REPORT_TAG_USAGE:
578 report_item->usages[report_item->usages_count] = usb_hid_report_tag_data_int32(data,item_size);
579 report_item->usages_count++;
580 break;
581 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
582 if (item_size == 3) {
583 // usage extended usages
584 report_item->extended_usage_page = (usb_hid_report_tag_data_int32(data,item_size) & 0xFF00) >> 16;
585 report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size) & 0xFF;
586 }
587 else {
588 report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
589 }
590 break;
591 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
592 if (item_size == 3) {
593 // usage extended usages
594 report_item->extended_usage_page = (usb_hid_report_tag_data_int32(data,item_size) & 0xFF00) >> 16;
595 report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size) & 0xFF;
596 }
597 else {
598 report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
599 }
600 break;
601 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
602 report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
603 break;
604 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
605 report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
606 break;
607 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
608 report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
609 break;
610 case USB_HID_REPORT_TAG_STRING_INDEX:
611 report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
612 break;
613 case USB_HID_REPORT_TAG_STRING_MINIMUM:
614 report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
615 break;
616 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
617 report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
618 break;
619 case USB_HID_REPORT_TAG_DELIMITER:
620 //report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
621 //TODO:
622 // DELIMITER STUFF
623 break;
624
625 default:
626 return USB_HID_NO_ACTION;
627 }
628
629 return EOK;
630}
631
632/**
633 * Converts raw data to int32 (thats the maximum length of short item data)
634 *
635 * @param Data buffer
636 * @param Size of buffer
637 * @return Converted int32 number
638 */
639int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
640{
641 unsigned int i;
642 int32_t result;
643
644 result = 0;
645 for(i=0; i<size; i++) {
646 result = (result | (data[i]) << (i*8));
647 }
648
649 return result;
650}
651
652
653
654/**
655 * Prints content of given list of report items.
656 *
657 * @param List of report items (usb_hid_report_item_t)
658 * @return void
659 */
660void usb_hid_descriptor_print_list(link_t *head)
661{
662 usb_hid_report_field_t *report_item;
663 link_t *item;
664
665
666 if(head == NULL || list_empty(head)) {
667 usb_log_debug("\tempty\n");
668 return;
669 }
670
671 for(item = head->next; item != head; item = item->next) {
672
673 report_item = list_get_instance(item, usb_hid_report_field_t, link);
674
675 usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
676 usb_log_debug("\t\tSIZE: %X\n", report_item->size);
677 usb_log_debug("\t\tLOGMIN: %X\n", report_item->logical_minimum);
678 usb_log_debug("\t\tLOGMAX: %X\n", report_item->logical_maximum);
679 usb_log_debug("\t\tPHYMIN: %X\n", report_item->physical_minimum);
680 usb_log_debug("\t\tPHYMAX: %X\n", report_item->physical_maximum);
681 usb_log_debug("\t\ttUSAGEMIN: %X\n", report_item->usage_minimum);
682 usb_log_debug("\t\tUSAGEMAX: %X\n", report_item->usage_maximum);
683
684 usb_log_debug("\t\tVALUE: %X\n", report_item->value);
685 usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage);
686 usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page);
687
688// usb_log_debug("\n");
689
690 }
691
692
693}
694/**
695 * Prints content of given report descriptor in human readable format.
696 *
697 * @param parser Parsed descriptor to print
698 * @return void
699 */
700void usb_hid_descriptor_print(usb_hid_report_t *report)
701{
702 if(report == NULL) {
703 return;
704 }
705
706 link_t *report_it = report->reports.next;
707 usb_hid_report_description_t *report_des;
708
709 while(report_it != &report->reports) {
710 report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
711 usb_log_debug("Report ID: %d\n", report_des->report_id);
712 usb_log_debug("\tType: %d\n", report_des->type);
713 usb_log_debug("\tLength: %d\n", report_des->bit_length);
714 usb_log_debug("\tItems: %d\n", report_des->item_length);
715
716 usb_hid_descriptor_print_list(&report_des->report_items);
717
718
719 link_t *path_it = report->collection_paths.next;
720 while(path_it != &report->collection_paths) {
721 usb_hid_print_usage_path (list_get_instance(path_it, usb_hid_report_path_t, link));
722 path_it = path_it->next;
723 }
724
725 report_it = report_it->next;
726 }
727}
728
729/**
730 * Releases whole linked list of report items
731 *
732 * @param head Head of list of report descriptor items (usb_hid_report_item_t)
733 * @return void
734 */
735void usb_hid_free_report_list(link_t *head)
736{
737 return;
738
739 usb_hid_report_item_t *report_item;
740 link_t *next;
741
742 if(head == NULL || list_empty(head)) {
743 return;
744 }
745
746 next = head->next;
747 while(next != head) {
748
749 report_item = list_get_instance(next, usb_hid_report_item_t, link);
750
751 while(!list_empty(&report_item->usage_path->link)) {
752 usb_hid_report_remove_last_item(report_item->usage_path);
753 }
754
755
756 next = next->next;
757
758 free(report_item);
759 }
760
761 return;
762
763}
764
765/** Frees the HID report descriptor parser structure
766 *
767 * @param parser Opaque HID report parser structure
768 * @return void
769 */
770void usb_hid_free_report(usb_hid_report_t *report)
771{
772 if(report == NULL){
773 return;
774 }
775
776 // free collection paths
777 usb_hid_report_path_t *path;
778 while(!list_empty(&report->collection_paths)) {
779 path = list_get_instance(report->collection_paths.next, usb_hid_report_path_t, link);
780 usb_hid_report_path_free(path);
781 }
782
783 // free report items
784 usb_hid_report_description_t *report_des;
785 usb_hid_report_field_t *field;
786 while(!list_empty(&report->reports)) {
787 report_des = list_get_instance(report->reports.next, usb_hid_report_description_t, link);
788 list_remove(&report_des->link);
789
790 while(!list_empty(&report_des->report_items)) {
791 field = list_get_instance(report_des->report_items.next, usb_hid_report_field_t, link);
792 list_remove(&field->link);
793
794 free(field);
795 }
796
797 free(report_des);
798 }
799
800 return;
801}
802
803/** Parse and act upon a HID report.
804 *
805 * @see usb_hid_parse_report_descriptor
806 *
807 * @param parser Opaque HID report parser structure.
808 * @param data Data for the report.
809 * @return Error code.
810 */
811int usb_hid_parse_report(const usb_hid_report_t *report,
812 const uint8_t *data, size_t size, uint8_t *report_id)
813{
814 link_t *list_item;
815 usb_hid_report_field_t *item;
816
817 usb_hid_report_description_t *report_des;
818 usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
819
820 if(report == NULL) {
821 return EINVAL;
822 }
823
824 if(report->use_report_ids != 0) {
825 *report_id = data[0];
826 }
827 else {
828 *report_id = 0;
829 }
830
831
832 report_des = usb_hid_report_find_description(report, *report_id, type);
833
834 /* read data */
835 list_item = report_des->report_items.next;
836 while(list_item != &(report_des->report_items)) {
837
838 item = list_get_instance(list_item, usb_hid_report_field_t, link);
839
840 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
841
842 if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
843
844 // array
845 item->value = usb_hid_translate_data(item, data);
846 item->usage = (item->value - item->physical_minimum) + item->usage_minimum;
847 }
848 else {
849 // variable item
850 item->value = usb_hid_translate_data(item, data);
851 }
852 }
853 list_item = list_item->next;
854 }
855
856 return EOK;
857
858}
859
860/**
861 * Translate data from the report as specified in report descriptor item
862 *
863 * @param item Report descriptor item with definition of translation
864 * @param data Data to translate
865 * @param j Index of processed field in report descriptor item
866 * @return Translated data
867 */
868int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
869{
870 int resolution;
871 int offset;
872 int part_size;
873
874 int32_t value=0;
875 int32_t mask;
876 const uint8_t *foo;
877
878 // now only shot tags are allowed
879 if(item->size > 32) {
880 return 0;
881 }
882
883 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
884 item->physical_minimum = item->logical_minimum;
885 item->physical_maximum = item->logical_maximum;
886 }
887
888
889 if(item->physical_maximum == item->physical_minimum){
890 resolution = 1;
891 }
892 else {
893 resolution = (item->logical_maximum - item->logical_minimum) /
894 ((item->physical_maximum - item->physical_minimum) *
895 (usb_pow(10,(item->unit_exponent))));
896 }
897
898 offset = item->offset;
899 // FIXME
900 if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
901
902 part_size = ((offset+item->size)%8);
903
904 size_t i=0;
905 for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
906 if(i == (size_t)(offset/8)) {
907 // the higher one
908 foo = data + i;
909 mask = ((1 << (item->size-part_size))-1);
910 value = (*foo & mask) << part_size;
911 }
912 else if(i == ((offset+item->size-1)/8)){
913 // the lower one
914 foo = data + i;
915 mask = ((1 << part_size)-1) << (8-part_size);
916 value += ((*foo & mask) >> (8-part_size));
917 }
918 else {
919 value = value << 8;
920 value += *(data + 1);
921 }
922 }
923 }
924 else {
925 foo = data+(offset/8);
926 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
927 value = (*foo & mask) >> (8-((offset%8)+item->size));
928 }
929
930 if(!(item->logical_minimum >= 0 && item->logical_maximum >= 0)){
931 value = (int32_t)value;
932 }
933 else {
934 value = (uint32_t)value;
935 }
936
937 return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
938
939}
940
941/**
942 * Returns number of items in input report which are accessible by given usage path
943 *
944 * @param parser Opaque report descriptor structure
945 * @param path Usage path specification
946 * @param flags Usage path comparison flags
947 * @return Number of items in input report
948 */
949size_t usb_hid_report_input_length(const usb_hid_report_t *report,
950 usb_hid_report_path_t *path, int flags)
951{
952
953 size_t ret = 0;
954
955 if(report == NULL) {
956 return 0;
957 }
958
959 usb_hid_report_description_t *report_des;
960 report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_INPUT);
961 if(report_des == NULL) {
962 return 0;
963 }
964
965 link_t *field_it = report_des->report_items.next;
966 usb_hid_report_field_t *field;
967 while(field_it != &report_des->report_items) {
968
969 field = list_get_instance(field_it, usb_hid_report_field_t, link);
970 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
971
972 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
973 if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
974 ret++;
975 }
976 usb_hid_report_remove_last_item (field->collection_path);
977 }
978
979 field_it = field_it->next;
980 }
981
982 return ret;
983 }
984
985
986/**
987 * Appends one item (couple of usage_path and usage) into the usage path
988 * structure
989 *
990 * @param usage_path Usage path structure
991 * @param usage_page Usage page constant
992 * @param usage Usage constant
993 * @return Error code
994 */
995int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
996 int32_t usage_page, int32_t usage)
997{
998 usb_hid_report_usage_path_t *item;
999
1000 if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
1001 return ENOMEM;
1002 }
1003 list_initialize(&item->link);
1004
1005 item->usage = usage;
1006 item->usage_page = usage_page;
1007 item->flags = 0;
1008
1009 list_append (&item->link, &usage_path->head);
1010 usage_path->depth++;
1011 return EOK;
1012}
1013
1014/**
1015 * Removes last item from the usage path structure
1016 * @param usage_path
1017 * @return void
1018 */
1019void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
1020{
1021 usb_hid_report_usage_path_t *item;
1022
1023 if(!list_empty(&usage_path->head)){
1024 item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
1025 list_remove(usage_path->head.prev);
1026 usage_path->depth--;
1027 free(item);
1028 }
1029}
1030
1031/**
1032 * Nulls last item of the usage path structure.
1033 *
1034 * @param usage_path
1035 * @return void
1036 */
1037void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
1038{
1039 usb_hid_report_usage_path_t *item;
1040
1041 if(!list_empty(&usage_path->head)){
1042 item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
1043 memset(item, 0, sizeof(usb_hid_report_usage_path_t));
1044 }
1045}
1046
1047/**
1048 * Modifies last item of usage path structure by given usage page or usage
1049 *
1050 * @param usage_path Opaque usage path structure
1051 * @param tag Class of currently processed tag (Usage page tag falls into Global
1052 * class but Usage tag into the Local)
1053 * @param data Value of the processed tag
1054 * @return void
1055 */
1056void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
1057{
1058 usb_hid_report_usage_path_t *item;
1059
1060 if(!list_empty(&usage_path->head)){
1061 item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
1062
1063 switch(tag) {
1064 case USB_HID_TAG_CLASS_GLOBAL:
1065 item->usage_page = data;
1066 break;
1067 case USB_HID_TAG_CLASS_LOCAL:
1068 item->usage = data;
1069 break;
1070 }
1071 }
1072
1073}
1074
1075
1076void usb_hid_print_usage_path(usb_hid_report_path_t *path)
1077{
1078 usb_log_debug("USAGE_PATH FOR RId(%d):\n", path->report_id);
1079 usb_log_debug("\tLENGTH: %d\n", path->depth);
1080
1081 link_t *item = path->head.next;
1082 usb_hid_report_usage_path_t *path_item;
1083 while(item != &path->head) {
1084
1085 path_item = list_get_instance(item, usb_hid_report_usage_path_t, link);
1086 usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page);
1087 usb_log_debug("\tUSAGE: %X\n", path_item->usage);
1088 usb_log_debug("\tFLAGS: %d\n", path_item->flags);
1089
1090 item = item->next;
1091 }
1092}
1093
1094/**
1095 * Compares two usage paths structures
1096 *
1097 * If USB_HID_PATH_COMPARE_COLLECTION_ONLY flag is given, the last item in report_path structure is forgotten
1098 *
1099 * @param report_path usage path structure to compare
1100 * @param path usage patrh structure to compare
1101 * @param flags Flags determining the mode of comparison
1102 * @return EOK if both paths are identical, non zero number otherwise
1103 */
1104int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
1105 usb_hid_report_path_t *path,
1106 int flags)
1107{
1108 usb_hid_report_usage_path_t *report_item;
1109 usb_hid_report_usage_path_t *path_item;
1110
1111 link_t *report_link;
1112 link_t *path_link;
1113
1114 int only_page;
1115
1116 if(report_path->report_id != path->report_id) {
1117 return 1;
1118 }
1119
1120 if(path->depth == 0){
1121 return EOK;
1122 }
1123
1124
1125 if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
1126 flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
1127 }
1128
1129 switch(flags){
1130 /* path must be completly identical */
1131 case USB_HID_PATH_COMPARE_STRICT:
1132 if(report_path->depth != path->depth){
1133 return 1;
1134 }
1135
1136 report_link = report_path->head.next;
1137 path_link = path->head.next;
1138
1139 while((report_link != &report_path->head) && (path_link != &path->head)) {
1140 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
1141 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
1142
1143 if((report_item->usage_page != path_item->usage_page) ||
1144 ((only_page == 0) && (report_item->usage != path_item->usage))) {
1145 return 1;
1146 } else {
1147 report_link = report_link->next;
1148 path_link = path_link->next;
1149 }
1150
1151 }
1152
1153 if(((report_link == &report_path->head) && (path_link == &path->head)) ||
1154 (((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) && (path_link = &path->head) && (report_link == report_path->head.prev))) {
1155 return EOK;
1156 }
1157 else {
1158 return 1;
1159 }
1160 break;
1161
1162 /* compare with only the end of path*/
1163 case USB_HID_PATH_COMPARE_END:
1164
1165 if((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) {
1166 report_link = report_path->head.prev->prev;
1167 }
1168 else {
1169 report_link = report_path->head.prev;
1170 }
1171 path_link = path->head.prev;
1172
1173 if(list_empty(&path->head)){
1174 return EOK;
1175 }
1176
1177 while((report_link != &report_path->head) && (path_link != &path->head)) {
1178 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
1179 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
1180
1181 if((report_item->usage_page != path_item->usage_page) ||
1182 ((only_page == 0) && (report_item->usage != path_item->usage))) {
1183 return 1;
1184 } else {
1185 report_link = report_link->prev;
1186 path_link = path_link->prev;
1187 }
1188
1189 }
1190
1191 if(path_link == &path->head) {
1192 return EOK;
1193 }
1194 else {
1195 return 1;
1196 }
1197
1198 break;
1199
1200 default:
1201 return EINVAL;
1202 }
1203
1204
1205
1206
1207}
1208
1209/**
1210 * Allocates and initializes new usage path structure.
1211 *
1212 * @return Initialized usage path structure
1213 */
1214usb_hid_report_path_t *usb_hid_report_path(void)
1215{
1216 usb_hid_report_path_t *path;
1217 path = malloc(sizeof(usb_hid_report_path_t));
1218 if(path == NULL){
1219 return NULL;
1220 }
1221 else {
1222 path->depth = 0;
1223 path->report_id = 0;
1224 list_initialize(&path->link);
1225 list_initialize(&path->head);
1226 return path;
1227 }
1228}
1229
1230/**
1231 * Releases given usage path structure.
1232 *
1233 * @param path usage path structure to release
1234 * @return void
1235 */
1236void usb_hid_report_path_free(usb_hid_report_path_t *path)
1237{
1238 while(!list_empty(&path->head)){
1239 usb_hid_report_remove_last_item(path);
1240 }
1241
1242 list_remove(&path->link);
1243 free(path);
1244}
1245
1246
1247/**
1248 * Clone content of given usage path to the new one
1249 *
1250 * @param usage_path Usage path structure to clone
1251 * @return New copy of given usage path structure
1252 */
1253usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
1254{
1255 link_t *path_link;
1256 usb_hid_report_usage_path_t *path_item;
1257 usb_hid_report_usage_path_t *new_path_item;
1258 usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
1259
1260 if(new_usage_path == NULL){
1261 return NULL;
1262 }
1263
1264 new_usage_path->report_id = usage_path->report_id;
1265
1266 if(list_empty(&usage_path->head)){
1267 return new_usage_path;
1268 }
1269
1270 path_link = usage_path->head.next;
1271 while(path_link != &usage_path->head) {
1272 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
1273 new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
1274 if(new_path_item == NULL) {
1275 return NULL;
1276 }
1277
1278 list_initialize (&new_path_item->link);
1279 new_path_item->usage_page = path_item->usage_page;
1280 new_path_item->usage = path_item->usage;
1281 new_path_item->flags = path_item->flags;
1282
1283 list_append(&new_path_item->link, &new_usage_path->head);
1284 new_usage_path->depth++;
1285
1286 path_link = path_link->next;
1287 }
1288
1289 return new_usage_path;
1290}
1291
1292
1293/*** OUTPUT API **/
1294
1295/**
1296 * Allocates output report buffer for output report
1297 *
1298 * @param parser Report parsed structure
1299 * @param size Size of returned buffer
1300 * @param report_id Report id of created output report
1301 * @return Returns allocated output buffer for specified output
1302 */
1303uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id)
1304{
1305 if(report == NULL) {
1306 *size = 0;
1307 return NULL;
1308 }
1309
1310 link_t *report_it = report->reports.next;
1311 usb_hid_report_description_t *report_des = NULL;
1312 while(report_it != &report->reports) {
1313 report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
1314 if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
1315 break;
1316 }
1317
1318 report_it = report_it->next;
1319 }
1320
1321 if(report_des == NULL){
1322 *size = 0;
1323 return NULL;
1324 }
1325 else {
1326 *size = (report_des->bit_length + (8 - 1))/8;
1327 uint8_t *ret = malloc((*size) * sizeof(uint8_t));
1328 memset(ret, 0, (*size) * sizeof(uint8_t));
1329 return ret;
1330 }
1331}
1332
1333
1334/** Frees output report buffer
1335 *
1336 * @param output Output report buffer
1337 * @return void
1338 */
1339void usb_hid_report_output_free(uint8_t *output)
1340
1341{
1342 if(output != NULL) {
1343 free (output);
1344 }
1345}
1346
1347/** Returns size of output for given usage path
1348 *
1349 * @param parser Opaque report parser structure
1350 * @param path Usage path specified which items will be thought for the output
1351 * @param flags Flags of usage path structure comparison
1352 * @return Number of items matching the given usage path
1353 */
1354size_t usb_hid_report_output_size(usb_hid_report_t *report,
1355 usb_hid_report_path_t *path, int flags)
1356{
1357 size_t ret = 0;
1358 usb_hid_report_description_t *report_des;
1359
1360 if(report == NULL) {
1361 return 0;
1362 }
1363
1364 report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_OUTPUT);
1365 if(report_des == NULL){
1366 return 0;
1367 }
1368
1369 link_t *field_it = report_des->report_items.next;
1370 usb_hid_report_field_t *field;
1371 while(field_it != &report_des->report_items) {
1372
1373 field = list_get_instance(field_it, usb_hid_report_field_t, link);
1374 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0){
1375 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
1376 if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
1377 ret++;
1378 }
1379 usb_hid_report_remove_last_item (field->collection_path);
1380 }
1381
1382 field_it = field_it->next;
1383 }
1384
1385 return ret;
1386
1387}
1388
1389/** Makes the output report buffer for data given in the report structure
1390 *
1391 * @param parser Opaque report parser structure
1392 * @param path Usage path specifing which parts of output will be set
1393 * @param flags Usage path structure comparison flags
1394 * @param buffer Output buffer
1395 * @param size Size of output buffer
1396 * @return Error code
1397 */
1398int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
1399 uint8_t *buffer, size_t size)
1400{
1401 link_t *item;
1402 int32_t value=0;
1403 int offset;
1404 int length;
1405 int32_t tmp_value;
1406
1407 if(report == NULL) {
1408 return EINVAL;
1409 }
1410
1411 if(report->use_report_ids != 0) {
1412 buffer[0] = report_id;
1413 }
1414
1415 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
1416
1417 usb_hid_report_description_t *report_des;
1418 report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT);
1419 if(report_des == NULL){
1420 return EINVAL;
1421 }
1422
1423 usb_hid_report_field_t *report_item;
1424 item = report_des->report_items.next;
1425 while(item != &report_des->report_items) {
1426 report_item = list_get_instance(item, usb_hid_report_field_t, link);
1427
1428 if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
1429
1430 // array
1431 value = usb_hid_translate_data_reverse(report_item, report_item->value);
1432 offset = report_item->offset;
1433 length = report_item->size;
1434 }
1435 else {
1436 // variable item
1437 value = usb_hid_translate_data_reverse(report_item, report_item->value);
1438 offset = report_item->offset;
1439 length = report_item->size;
1440 }
1441
1442 if((offset/8) == ((offset+length-1)/8)) {
1443 // je to v jednom bytu
1444 if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
1445 break; // TODO ErrorCode
1446 }
1447
1448 size_t shift = offset%8;
1449
1450 value = value << shift;
1451 value = value & (((1 << length)-1) << shift);
1452
1453 uint8_t mask = 0;
1454 mask = 0xff - (((1 << length) - 1) << shift);
1455 buffer[offset/8] = (buffer[offset/8] & mask) | value;
1456 }
1457 else {
1458 int i = 0;
1459 uint8_t mask = 0;
1460 for(i = (offset/8); i <= ((offset+length-1)/8); i++) {
1461 if(i == (offset/8)) {
1462 tmp_value = value;
1463 tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);
1464 tmp_value = tmp_value << (offset%8);
1465
1466 mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
1467 buffer[i] = (buffer[i] & mask) | tmp_value;
1468 }
1469 else if (i == ((offset + length -1)/8)) {
1470
1471 value = value >> (length - ((offset + length) % 8));
1472 value = value & ((1 << (length - ((offset + length) % 8))) - 1);
1473
1474 mask = (1 << (length - ((offset + length) % 8))) - 1;
1475 buffer[i] = (buffer[i] & mask) | value;
1476 }
1477 else {
1478 buffer[i] = value & (0xFF << i);
1479 }
1480 }
1481 }
1482
1483
1484 // reset value
1485 report_item->value = 0;
1486
1487 item = item->next;
1488 }
1489
1490 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
1491
1492 return EOK;
1493}
1494
1495/**
1496 * Translate given data for putting them into the outoput report
1497 * @param item Report item structure
1498 * @param value Value to translate
1499 * @return ranslated value
1500 */
1501uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value)
1502{
1503 int ret=0;
1504 int resolution;
1505
1506 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
1507 ret = item->logical_minimum;
1508 }
1509
1510 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
1511 item->physical_minimum = item->logical_minimum;
1512 item->physical_maximum = item->logical_maximum;
1513 }
1514
1515
1516 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
1517
1518 // variable item
1519 if(item->physical_maximum == item->physical_minimum){
1520 resolution = 1;
1521 }
1522 else {
1523 resolution = (item->logical_maximum - item->logical_minimum) /
1524 ((item->physical_maximum - item->physical_minimum) *
1525 (usb_pow(10,(item->unit_exponent))));
1526 }
1527
1528 ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
1529 }
1530 else {
1531 // bitmapa
1532 if(value == 0) {
1533 ret = 0;
1534 }
1535 else {
1536 size_t bitmap_idx = (value - item->usage_minimum);
1537 ret = 1 << bitmap_idx;
1538 }
1539 }
1540
1541
1542 return (uint32_t)ret;
1543}
1544
1545/**
1546 * Sets report id in usage path structure
1547 *
1548 * @param path Usage path structure
1549 * @param report_id Report id to set
1550 * @return Error code
1551 */
1552int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id)
1553{
1554 if(path == NULL){
1555 return EINVAL;
1556 }
1557
1558 path->report_id = report_id;
1559 return EOK;
1560}
1561
1562/**
1563 *
1564 *
1565 *
1566 *
1567 *
1568 */
1569int usb_hid_report_output_set_data(usb_hid_report_t *report,
1570 usb_hid_report_path_t *path, int flags,
1571 int *data, size_t data_size)
1572{
1573 size_t data_idx = 0;
1574 if(report == NULL){
1575 return EINVAL;
1576 }
1577
1578 usb_hid_report_description_t *report_des;
1579 report_des = usb_hid_report_find_description (report, path->report_id,
1580 USB_HID_REPORT_TYPE_OUTPUT);
1581 if(report_des == NULL){
1582 return EINVAL;
1583 }
1584
1585 usb_hid_report_field_t *field;
1586 link_t *field_it = report_des->report_items.next;
1587 while(field_it != &report_des->report_items){
1588
1589 field = list_get_instance(field_it, usb_hid_report_field_t, link);
1590 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
1591 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
1592 if(usb_hid_report_compare_usage_path (field->collection_path, path,
1593 flags) == EOK) {
1594 if(data_idx < data_size) {
1595 if((data[data_idx] >= field->physical_minimum) && (data[data_idx] >= field->physical_minimum)) {
1596 field->value = data[data_idx];
1597 }
1598 else {
1599 return ERANGE;
1600 }
1601
1602 data_idx++;
1603 }
1604 else {
1605 field->value = 0;
1606 }
1607 }
1608 usb_hid_report_remove_last_item (field->collection_path);
1609 }
1610
1611 field_it = field_it->next;
1612 }
1613
1614 return EOK;
1615}
1616
1617
1618usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item)
1619{
1620 usb_hid_report_item_t *new_report_item;
1621
1622 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
1623 return NULL;
1624 }
1625 memcpy(new_report_item,item, sizeof(usb_hid_report_item_t));
1626 link_initialize(&(new_report_item->link));
1627
1628 return new_report_item;
1629}
1630
1631
1632usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
1633 usb_hid_report_field_t *field,
1634 usb_hid_report_path_t *path, int flags,
1635 usb_hid_report_type_t type)
1636{
1637 usb_hid_report_description_t *report_des = usb_hid_report_find_description (report, path->report_id, type);
1638 link_t *field_it;
1639
1640 if(report_des == NULL){
1641 return NULL;
1642 }
1643
1644 if(field == NULL){
1645 // vezmu prvni co mathuje podle path!!
1646 field_it = report_des->report_items.next;
1647 }
1648 else {
1649 field_it = field->link.next;
1650 }
1651
1652 while(field_it != &report_des->report_items) {
1653 field = list_get_instance(field_it, usb_hid_report_field_t, link);
1654
1655 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
1656 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
1657 if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK){
1658 usb_hid_report_remove_last_item (field->collection_path);
1659 return field;
1660 }
1661 usb_hid_report_remove_last_item (field->collection_path);
1662 }
1663 field_it = field_it->next;
1664 }
1665
1666 return NULL;
1667}
1668
1669uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
1670{
1671 if(report == NULL){
1672 return 0;
1673 }
1674
1675 usb_hid_report_description_t *report_des;
1676 link_t *report_it;
1677
1678 if(report_id == 0) {
1679 report_it = usb_hid_report_find_description (report, report_id, type)->link.next;
1680 }
1681 else {
1682 report_it = report->reports.next;
1683 }
1684
1685 while(report_it != &report->reports) {
1686 report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
1687 if(report_des->type == type){
1688 return report_des->report_id;
1689 }
1690 }
1691
1692 return 0;
1693}
1694
1695void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
1696{
1697 if(report_item == NULL) {
1698 return;
1699 }
1700
1701 report_item->usages_count = 0;
1702 memset(report_item->usages, 0, USB_HID_MAX_USAGES);
1703
1704 report_item->extended_usage_page = 0;
1705 report_item->usage_minimum = 0;
1706 report_item->usage_maximum = 0;
1707 report_item->designator_index = 0;
1708 report_item->designator_minimum = 0;
1709 report_item->designator_maximum = 0;
1710 report_item->string_index = 0;
1711 report_item->string_minimum = 0;
1712 report_item->string_maximum = 0;
1713
1714 return;
1715}
1716/**
1717 * @}
1718 */
Note: See TracBrowser for help on using the repository browser.