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

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

new hid report structure bug fixes

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