source: mainline/uspace/lib/usbhid/src/hidparser.c@ aaa3d82b

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

some little changes

  • Property mode set to 100644
File size: 16.6 KB
Line 
1/*
2 * Copyright (c) 2011 Matej Klonfar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libusbhid
30 * @{
31 */
32/** @file
33 * USB HID report data parser implementation.
34 */
35#include <usb/hid/hidparser.h>
36#include <errno.h>
37#include <stdio.h>
38#include <malloc.h>
39#include <mem.h>
40#include <usb/debug.h>
41#include <assert.h>
42
43/*---------------------------------------------------------------------------*/
44/*
45 * Data translation private functions
46 */
47uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
48
49int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
50
51uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
52 int32_t value);
53
54int usb_pow(int a, int b);
55
56/*---------------------------------------------------------------------------*/
57
58// TODO: tohle ma bejt asi jinde
59int usb_pow(int a, int b)
60{
61 switch(b) {
62 case 0:
63 return 1;
64 break;
65 case 1:
66 return a;
67 break;
68 default:
69 return a * usb_pow(a, b-1);
70 break;
71 }
72}
73/*---------------------------------------------------------------------------*/
74
75/** Returns size of report of specified report id and type in items
76 *
77 * @param parser Opaque report parser structure
78 * @param report_id
79 * @param type
80 * @return Number of items in specified report
81 */
82size_t usb_hid_report_size(usb_hid_report_t *report, uint8_t report_id,
83 usb_hid_report_type_t type)
84{
85 usb_hid_report_description_t *report_des;
86
87 if(report == NULL) {
88 return 0;
89 }
90
91 report_des = usb_hid_report_find_description (report, report_id, type);
92 if(report_des == NULL){
93 return 0;
94 }
95 else {
96 return report_des->item_length;
97 }
98}
99
100/** Returns size of report of specified report id and type in bytes
101 *
102 * @param parser Opaque report parser structure
103 * @param report_id
104 * @param type
105 * @return Number of items in specified report
106 */
107size_t usb_hid_report_byte_size(usb_hid_report_t *report, uint8_t report_id,
108 usb_hid_report_type_t type)
109{
110 usb_hid_report_description_t *report_des;
111
112 if(report == NULL) {
113 return 0;
114 }
115
116 report_des = usb_hid_report_find_description (report, report_id, type);
117 if(report_des == NULL){
118 return 0;
119 }
120 else {
121 return ((report_des->bit_length + 7) / 8) ;
122 }
123}
124/*---------------------------------------------------------------------------*/
125
126/** Parse and act upon a HID report.
127 *
128 * @see usb_hid_parse_report_descriptor
129 *
130 * @param parser Opaque HID report parser structure.
131 * @param data Data for the report.
132 * @return Error code.
133 */
134int usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data,
135 size_t size, uint8_t *report_id)
136{
137 link_t *list_item;
138 usb_hid_report_field_t *item;
139
140 usb_hid_report_description_t *report_des;
141 usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
142
143 if(report == NULL) {
144 return EINVAL;
145 }
146
147 if(report->use_report_ids != 0) {
148 *report_id = data[0];
149 }
150 else {
151 *report_id = 0;
152 }
153
154
155 report_des = usb_hid_report_find_description(report, *report_id, type);
156 if(report_des == NULL) {
157 return EINVAL;
158 }
159
160 /* read data */
161 list_item = report_des->report_items.next;
162 while(list_item != &(report_des->report_items)) {
163
164 item = list_get_instance(list_item, usb_hid_report_field_t,
165 link);
166
167 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
168
169 if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
170
171 // array
172 item->value =
173 usb_hid_translate_data(item, data);
174
175 item->usage = USB_HID_EXTENDED_USAGE(
176 item->usages[item->value - item->physical_minimum]);
177
178 item->usage_page = USB_HID_EXTENDED_USAGE_PAGE(
179 item->usages[item->value - item->physical_minimum]);
180
181 usb_hid_report_set_last_item (item->collection_path,
182 USB_HID_TAG_CLASS_GLOBAL, item->usage_page);
183
184 usb_hid_report_set_last_item (item->collection_path,
185 USB_HID_TAG_CLASS_LOCAL, item->usage);
186
187 }
188 else {
189 // variable item
190 item->value = usb_hid_translate_data(item, data);
191 }
192 }
193 list_item = list_item->next;
194 }
195
196 return EOK;
197
198}
199
200/*---------------------------------------------------------------------------*/
201/**
202 * Translate data from the report as specified in report descriptor item
203 *
204 * @param item Report descriptor item with definition of translation
205 * @param data Data to translate
206 * @return Translated data
207 */
208int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
209{
210 int resolution;
211 int offset;
212 int part_size;
213
214 int32_t value=0;
215 int32_t mask;
216 const uint8_t *foo;
217
218 // now only shot tags are allowed
219 if(item->size > 32) {
220 return 0;
221 }
222
223 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
224 item->physical_minimum = item->logical_minimum;
225 item->physical_maximum = item->logical_maximum;
226 }
227
228
229 if(item->physical_maximum == item->physical_minimum){
230 resolution = 1;
231 }
232 else {
233 resolution = (item->logical_maximum - item->logical_minimum) /
234 ((item->physical_maximum - item->physical_minimum) *
235 (usb_pow(10,(item->unit_exponent))));
236 }
237
238 offset = item->offset;
239 // FIXME
240 if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
241
242 part_size = ((offset+item->size)%8);
243
244 size_t i=0;
245 for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
246 if(i == (size_t)(offset/8)) {
247 // the higher one
248 foo = data + i;
249 mask = ((1 << (item->size-part_size))-1);
250 value = (*foo & mask) << part_size;
251 }
252 else if(i == ((offset+item->size-1)/8)){
253 // the lower one
254 foo = data + i;
255 mask = ((1 << part_size)-1) << (8-part_size);
256 value += ((*foo & mask) >> (8-part_size));
257 }
258 else {
259 value = value << 8;
260 value += *(data + 1);
261 }
262 }
263 }
264 else {
265 foo = data+(offset/8);
266 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
267 value = (*foo & mask) >> (8-((offset%8)+item->size));
268 }
269
270 if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
271 value = USB_HID_UINT32_TO_INT32(value, item->size);
272 }
273
274 return (int)(((value - item->logical_minimum) / resolution) +
275 item->physical_minimum);
276
277}
278
279/*---------------------------------------------------------------------------*/
280/* OUTPUT API */
281
282/**
283 * Allocates output report buffer for output report
284 *
285 * @param parser Report parsed structure
286 * @param size Size of returned buffer
287 * @param report_id Report id of created output report
288 * @return Returns allocated output buffer for specified output
289 */
290uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size,
291 uint8_t report_id)
292{
293 if(report == NULL) {
294 *size = 0;
295 return NULL;
296 }
297
298 link_t *report_it = report->reports.next;
299 usb_hid_report_description_t *report_des = NULL;
300 while(report_it != &report->reports) {
301 report_des = list_get_instance(report_it,
302 usb_hid_report_description_t, link);
303
304 if((report_des->report_id == report_id) &&
305 (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
306 break;
307 }
308
309 report_it = report_it->next;
310 }
311
312 if(report_des == NULL){
313 *size = 0;
314 return NULL;
315 }
316 else {
317 *size = (report_des->bit_length + (8 - 1))/8;
318 uint8_t *ret = malloc((*size) * sizeof(uint8_t));
319 memset(ret, 0, (*size) * sizeof(uint8_t));
320 return ret;
321 }
322}
323
324
325/** Frees output report buffer
326 *
327 * @param output Output report buffer
328 * @return void
329 */
330void usb_hid_report_output_free(uint8_t *output)
331
332{
333 if(output != NULL) {
334 free (output);
335 }
336}
337
338/** Makes the output report buffer for data given in the report structure
339 *
340 * @param parser Opaque report parser structure
341 * @param path Usage path specifing which parts of output will be set
342 * @param flags Usage path structure comparison flags
343 * @param buffer Output buffer
344 * @param size Size of output buffer
345 * @return Error code
346 */
347int usb_hid_report_output_translate(usb_hid_report_t *report,
348 uint8_t report_id, uint8_t *buffer, size_t size)
349{
350 link_t *item;
351 int32_t value=0;
352 int offset;
353 int length;
354 int32_t tmp_value;
355
356 if(report == NULL) {
357 return EINVAL;
358 }
359
360 if(report->use_report_ids != 0) {
361 buffer[0] = report_id;
362 }
363
364 usb_hid_report_description_t *report_des;
365 report_des = usb_hid_report_find_description (report, report_id,
366 USB_HID_REPORT_TYPE_OUTPUT);
367
368 if(report_des == NULL){
369 return EINVAL;
370 }
371
372 usb_hid_report_field_t *report_item;
373 item = report_des->report_items.next;
374 while(item != &report_des->report_items) {
375 report_item = list_get_instance(item, usb_hid_report_field_t, link);
376
377 if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
378
379 // array
380 value = usb_hid_translate_data_reverse(report_item,
381 report_item->value);
382
383 offset = report_item->offset;
384 length = report_item->size;
385 }
386 else {
387 // variable item
388 value = usb_hid_translate_data_reverse(report_item,
389 report_item->value);
390
391 offset = report_item->offset;
392 length = report_item->size;
393 }
394
395 usb_log_debug("\ttranslated value: %x\n", value);
396
397 if((offset/8) == ((offset+length-1)/8)) {
398 // je to v jednom bytu
399 if(((size_t)(offset/8) >= size) ||
400 ((size_t)(offset+length-1)/8) >= size) {
401 break; // TODO ErrorCode
402 }
403 size_t shift = 8 - offset%8 - length;
404 value = value << shift;
405 value = value & (((1 << length)-1) << shift);
406
407 uint8_t mask = 0;
408 mask = 0xff - (((1 << length) - 1) << shift);
409 buffer[offset/8] = (buffer[offset/8] & mask) | value;
410 }
411 else {
412 int i = 0;
413 uint8_t mask = 0;
414 for(i = (offset/8); i <= ((offset+length-1)/8); i++) {
415 if(i == (offset/8)) {
416 tmp_value = value;
417 tmp_value = tmp_value &
418 ((1 << (8-(offset%8)))-1);
419
420 tmp_value = tmp_value << (offset%8);
421
422 mask = ~(((1 << (8-(offset%8)))-1) <<
423 (offset%8));
424
425 buffer[i] = (buffer[i] & mask) |
426 tmp_value;
427 }
428 else if (i == ((offset + length -1)/8)) {
429
430 value = value >> (length -
431 ((offset + length) % 8));
432
433 value = value & ((1 << (length -
434 ((offset + length) % 8))) - 1);
435
436 mask = (1 << (length -
437 ((offset + length) % 8))) - 1;
438
439 buffer[i] = (buffer[i] & mask) | value;
440 }
441 else {
442 buffer[i] = value & (0xFF << i);
443 }
444 }
445 }
446
447 // reset value
448 report_item->value = 0;
449
450 item = item->next;
451 }
452
453 return EOK;
454}
455
456/*---------------------------------------------------------------------------*/
457/**
458 * Translate given data for putting them into the outoput report
459 * @param item Report item structure
460 * @param value Value to translate
461 * @return ranslated value
462 */
463uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
464 int value)
465{
466 int ret=0;
467 int resolution;
468
469 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
470 ret = item->logical_minimum;
471 }
472
473 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
474 item->physical_minimum = item->logical_minimum;
475 item->physical_maximum = item->logical_maximum;
476 }
477
478 // variable item
479 if(item->physical_maximum == item->physical_minimum){
480 resolution = 1;
481 }
482 else {
483 resolution = (item->logical_maximum - item->logical_minimum) /
484 ((item->physical_maximum - item->physical_minimum) *
485 (usb_pow(10,(item->unit_exponent))));
486 }
487
488 ret = ((value - item->physical_minimum) * resolution) +
489 item->logical_minimum;
490
491 usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), \
492 ret(%x)\n", value, resolution, item->physical_minimum,
493 item->logical_minimum, ret);
494
495 if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
496 return USB_HID_INT32_TO_UINT32(ret, item->size);
497 }
498 return (int32_t)0 + ret;
499}
500
501/*---------------------------------------------------------------------------*/
502/**
503 * Clones given state table
504 *
505 * @param item State table to clone
506 * @return Pointer to the cloned item
507 */
508usb_hid_report_item_t *usb_hid_report_item_clone(
509 const usb_hid_report_item_t *item)
510{
511 usb_hid_report_item_t *new_report_item;
512
513 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
514 return NULL;
515 }
516 memcpy(new_report_item,item, sizeof(usb_hid_report_item_t));
517 link_initialize(&(new_report_item->link));
518
519 return new_report_item;
520}
521
522/*---------------------------------------------------------------------------*/
523/**
524 * Function for sequence walking through the report. Returns next field in the
525 * report or the first one when no field is given.
526 *
527 * @param report Searched report structure
528 * @param field Current field. If NULL is given, the first one in the report
529 * is returned. Otherwise the next one i nthe list is returned.
530 * @param path Usage path specifying which fields wa are interested in.
531 * @param flags Flags defining mode of usage paths comparison
532 * @param type Type of report we search.
533 * @retval NULL if no field is founded
534 * @retval Pointer to the founded report structure when founded
535 */
536usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
537 usb_hid_report_field_t *field, usb_hid_report_path_t *path, int flags,
538 usb_hid_report_type_t type)
539{
540 usb_hid_report_description_t *report_des =
541 usb_hid_report_find_description(report, path->report_id, type);
542
543 link_t *field_it;
544
545 if(report_des == NULL){
546 return NULL;
547 }
548
549 if(field == NULL){
550 field_it = report_des->report_items.next;
551 }
552 else {
553 field_it = field->link.next;
554 }
555
556 while(field_it != &report_des->report_items) {
557 field = list_get_instance(field_it, usb_hid_report_field_t,
558 link);
559
560 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
561 usb_hid_report_path_append_item (
562 field->collection_path, field->usage_page,
563 field->usage);
564
565 if(usb_hid_report_compare_usage_path(
566 field->collection_path, path, flags) == EOK){
567
568 usb_hid_report_remove_last_item(
569 field->collection_path);
570
571 return field;
572 }
573 usb_hid_report_remove_last_item (
574 field->collection_path);
575 }
576 field_it = field_it->next;
577 }
578
579 return NULL;
580}
581
582/*---------------------------------------------------------------------------*/
583/**
584 * Returns next report_id of report of specified type. If zero is given than
585 * first report_id of specified type is returned (0 is not legal value for
586 * repotr_id)
587 *
588 * @param report_id Current report_id, 0 if there is no current report_id
589 * @param type Type of searched report
590 * @param report Report structure inwhich we search
591 * @retval 0 if report structure is null or there is no specified report
592 * @retval report_id otherwise
593 */
594uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report,
595 uint8_t report_id, usb_hid_report_type_t type)
596{
597 if(report == NULL){
598 return 0;
599 }
600
601 usb_hid_report_description_t *report_des;
602 link_t *report_it;
603
604 if(report_id > 0) {
605 report_it = usb_hid_report_find_description(report, report_id,
606 type)->link.next;
607 }
608 else {
609 report_it = report->reports.next;
610 }
611
612 while(report_it != &report->reports) {
613 report_des = list_get_instance(report_it,
614 usb_hid_report_description_t, link);
615
616 if(report_des->type == type){
617 return report_des->report_id;
618 }
619 }
620
621 return 0;
622}
623
624/*---------------------------------------------------------------------------*/
625/**
626 * Reset all local items in given state table
627 *
628 * @param report_item State table containing current state of report
629 * descriptor parsing
630 *
631 * @return void
632 */
633void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
634{
635 if(report_item == NULL) {
636 return;
637 }
638
639 report_item->usages_count = 0;
640 memset(report_item->usages, 0, USB_HID_MAX_USAGES);
641
642 report_item->extended_usage_page = 0;
643 report_item->usage_minimum = 0;
644 report_item->usage_maximum = 0;
645 report_item->designator_index = 0;
646 report_item->designator_minimum = 0;
647 report_item->designator_maximum = 0;
648 report_item->string_index = 0;
649 report_item->string_minimum = 0;
650 report_item->string_maximum = 0;
651
652 return;
653}
654/**
655 * @}
656 */
Note: See TracBrowser for help on using the repository browser.