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

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

extended usages implemented

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