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

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

doxygen and coding style update

  • Property mode set to 100644
File size: 16.7 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 if(report_id == 0) {
122 return ((report_des->bit_length + 7) / 8) ;
123 }
124 else {
125 return 1 + ((report_des->bit_length + 7) / 8);
126 }
127 }
128}
129/*---------------------------------------------------------------------------*/
130
131/** Parse and act upon a HID report.
132 *
133 * @see usb_hid_parse_report_descriptor
134 *
135 * @param parser Opaque HID report parser structure.
136 * @param data Data for the report.
137 * @return Error code.
138 */
139int usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data,
140 size_t size, uint8_t *report_id)
141{
142 link_t *list_item;
143 usb_hid_report_field_t *item;
144
145 usb_hid_report_description_t *report_des;
146 usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
147
148 if(report == NULL) {
149 return EINVAL;
150 }
151
152 if(report->use_report_ids != 0) {
153 *report_id = data[0];
154 }
155 else {
156 *report_id = 0;
157 }
158
159
160 report_des = usb_hid_report_find_description(report, *report_id, type);
161 if(report_des == NULL) {
162 return EINVAL;
163 }
164
165 /* read data */
166 list_item = report_des->report_items.next;
167 while(list_item != &(report_des->report_items)) {
168
169 item = list_get_instance(list_item, usb_hid_report_field_t,
170 link);
171
172 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
173
174 if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
175
176 // array
177 item->value =
178 usb_hid_translate_data(item, data);
179
180 item->usage = USB_HID_EXTENDED_USAGE(
181 item->usages[item->value - item->physical_minimum]);
182
183 item->usage_page = USB_HID_EXTENDED_USAGE_PAGE(
184 item->usages[item->value - item->physical_minimum]);
185
186 usb_hid_report_set_last_item (item->collection_path,
187 USB_HID_TAG_CLASS_GLOBAL, item->usage_page);
188
189 usb_hid_report_set_last_item (item->collection_path,
190 USB_HID_TAG_CLASS_LOCAL, item->usage);
191
192 }
193 else {
194 // variable item
195 item->value = usb_hid_translate_data(item, data);
196 }
197 }
198 list_item = list_item->next;
199 }
200
201 return EOK;
202
203}
204
205/*---------------------------------------------------------------------------*/
206/**
207 * Translate data from the report as specified in report descriptor item
208 *
209 * @param item Report descriptor item with definition of translation
210 * @param data Data to translate
211 * @return Translated data
212 */
213int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
214{
215 int resolution;
216 int offset;
217 int part_size;
218
219 int32_t value=0;
220 int32_t mask;
221 const uint8_t *foo;
222
223 // now only shot tags are allowed
224 if(item->size > 32) {
225 return 0;
226 }
227
228 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
229 item->physical_minimum = item->logical_minimum;
230 item->physical_maximum = item->logical_maximum;
231 }
232
233
234 if(item->physical_maximum == item->physical_minimum){
235 resolution = 1;
236 }
237 else {
238 resolution = (item->logical_maximum - item->logical_minimum) /
239 ((item->physical_maximum - item->physical_minimum) *
240 (usb_pow(10,(item->unit_exponent))));
241 }
242
243 offset = item->offset;
244 // FIXME
245 if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
246
247 part_size = ((offset+item->size)%8);
248
249 size_t i=0;
250 for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
251 if(i == (size_t)(offset/8)) {
252 // the higher one
253 foo = data + i;
254 mask = ((1 << (item->size-part_size))-1);
255 value = (*foo & mask) << part_size;
256 }
257 else if(i == ((offset+item->size-1)/8)){
258 // the lower one
259 foo = data + i;
260 mask = ((1 << part_size)-1) << (8-part_size);
261 value += ((*foo & mask) >> (8-part_size));
262 }
263 else {
264 value = value << 8;
265 value += *(data + 1);
266 }
267 }
268 }
269 else {
270 foo = data+(offset/8);
271 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
272 value = (*foo & mask) >> (8-((offset%8)+item->size));
273 }
274
275 if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
276 value = USB_HID_UINT32_TO_INT32(value, item->size);
277 }
278
279 return (int)(((value - item->logical_minimum) / resolution) +
280 item->physical_minimum);
281
282}
283
284/*---------------------------------------------------------------------------*/
285/* OUTPUT API */
286
287/**
288 * Allocates output report buffer for output report
289 *
290 * @param parser Report parsed structure
291 * @param size Size of returned buffer
292 * @param report_id Report id of created output report
293 * @return Returns allocated output buffer for specified output
294 */
295uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size,
296 uint8_t report_id)
297{
298 if(report == NULL) {
299 *size = 0;
300 return NULL;
301 }
302
303 link_t *report_it = report->reports.next;
304 usb_hid_report_description_t *report_des = NULL;
305 while(report_it != &report->reports) {
306 report_des = list_get_instance(report_it,
307 usb_hid_report_description_t, link);
308
309 if((report_des->report_id == report_id) &&
310 (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
311 break;
312 }
313
314 report_it = report_it->next;
315 }
316
317 if(report_des == NULL){
318 *size = 0;
319 return NULL;
320 }
321 else {
322 *size = (report_des->bit_length + (8 - 1))/8;
323 uint8_t *ret = malloc((*size) * sizeof(uint8_t));
324 memset(ret, 0, (*size) * sizeof(uint8_t));
325 return ret;
326 }
327}
328
329
330/** Frees output report buffer
331 *
332 * @param output Output report buffer
333 * @return void
334 */
335void usb_hid_report_output_free(uint8_t *output)
336
337{
338 if(output != NULL) {
339 free (output);
340 }
341}
342
343/** Makes the output report buffer for data given in the report structure
344 *
345 * @param parser Opaque report parser structure
346 * @param path Usage path specifing which parts of output will be set
347 * @param flags Usage path structure comparison flags
348 * @param buffer Output buffer
349 * @param size Size of output buffer
350 * @return Error code
351 */
352int usb_hid_report_output_translate(usb_hid_report_t *report,
353 uint8_t report_id, uint8_t *buffer, size_t size)
354{
355 link_t *item;
356 int32_t value=0;
357 int offset;
358 int length;
359 int32_t tmp_value;
360
361 if(report == NULL) {
362 return EINVAL;
363 }
364
365 if(report->use_report_ids != 0) {
366 buffer[0] = report_id;
367 }
368
369 usb_hid_report_description_t *report_des;
370 report_des = usb_hid_report_find_description (report, report_id,
371 USB_HID_REPORT_TYPE_OUTPUT);
372
373 if(report_des == NULL){
374 return EINVAL;
375 }
376
377 usb_hid_report_field_t *report_item;
378 item = report_des->report_items.next;
379 while(item != &report_des->report_items) {
380 report_item = list_get_instance(item, usb_hid_report_field_t, link);
381
382 if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
383
384 // array
385 value = usb_hid_translate_data_reverse(report_item,
386 report_item->value);
387
388 offset = report_item->offset;
389 length = report_item->size;
390 }
391 else {
392 // variable item
393 value = usb_hid_translate_data_reverse(report_item,
394 report_item->value);
395
396 offset = report_item->offset;
397 length = report_item->size;
398 }
399
400 usb_log_debug("\ttranslated value: %x\n", value);
401
402 if((offset/8) == ((offset+length-1)/8)) {
403 // je to v jednom bytu
404 if(((size_t)(offset/8) >= size) ||
405 ((size_t)(offset+length-1)/8) >= size) {
406 break; // TODO ErrorCode
407 }
408 size_t shift = 8 - offset%8 - length;
409 value = value << shift;
410 value = value & (((1 << length)-1) << shift);
411
412 uint8_t mask = 0;
413 mask = 0xff - (((1 << length) - 1) << shift);
414 buffer[offset/8] = (buffer[offset/8] & mask) | value;
415 }
416 else {
417 int i = 0;
418 uint8_t mask = 0;
419 for(i = (offset/8); i <= ((offset+length-1)/8); i++) {
420 if(i == (offset/8)) {
421 tmp_value = value;
422 tmp_value = tmp_value &
423 ((1 << (8-(offset%8)))-1);
424
425 tmp_value = tmp_value << (offset%8);
426
427 mask = ~(((1 << (8-(offset%8)))-1) <<
428 (offset%8));
429
430 buffer[i] = (buffer[i] & mask) |
431 tmp_value;
432 }
433 else if (i == ((offset + length -1)/8)) {
434
435 value = value >> (length -
436 ((offset + length) % 8));
437
438 value = value & ((1 << (length -
439 ((offset + length) % 8))) - 1);
440
441 mask = (1 << (length -
442 ((offset + length) % 8))) - 1;
443
444 buffer[i] = (buffer[i] & mask) | value;
445 }
446 else {
447 buffer[i] = value & (0xFF << i);
448 }
449 }
450 }
451
452 // reset value
453 report_item->value = 0;
454
455 item = item->next;
456 }
457
458 return EOK;
459}
460
461/*---------------------------------------------------------------------------*/
462/**
463 * Translate given data for putting them into the outoput report
464 * @param item Report item structure
465 * @param value Value to translate
466 * @return ranslated value
467 */
468uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
469 int value)
470{
471 int ret=0;
472 int resolution;
473
474 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
475 ret = item->logical_minimum;
476 }
477
478 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
479 item->physical_minimum = item->logical_minimum;
480 item->physical_maximum = item->logical_maximum;
481 }
482
483 // variable item
484 if(item->physical_maximum == item->physical_minimum){
485 resolution = 1;
486 }
487 else {
488 resolution = (item->logical_maximum - item->logical_minimum) /
489 ((item->physical_maximum - item->physical_minimum) *
490 (usb_pow(10,(item->unit_exponent))));
491 }
492
493 ret = ((value - item->physical_minimum) * resolution) +
494 item->logical_minimum;
495
496 usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), \
497 ret(%x)\n", value, resolution, item->physical_minimum,
498 item->logical_minimum, ret);
499
500 if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
501 return USB_HID_INT32_TO_UINT32(ret, item->size);
502 }
503 return (int32_t)0 + ret;
504}
505
506/*---------------------------------------------------------------------------*/
507/**
508 * Clones given state table
509 *
510 * @param item State table to clone
511 * @return Pointer to the cloned item
512 */
513usb_hid_report_item_t *usb_hid_report_item_clone(
514 const usb_hid_report_item_t *item)
515{
516 usb_hid_report_item_t *new_report_item;
517
518 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
519 return NULL;
520 }
521 memcpy(new_report_item,item, sizeof(usb_hid_report_item_t));
522 link_initialize(&(new_report_item->link));
523
524 return new_report_item;
525}
526
527/*---------------------------------------------------------------------------*/
528/**
529 * Function for sequence walking through the report. Returns next field in the
530 * report or the first one when no field is given.
531 *
532 * @param report Searched report structure
533 * @param field Current field. If NULL is given, the first one in the report
534 * is returned. Otherwise the next one i nthe list is returned.
535 * @param path Usage path specifying which fields wa are interested in.
536 * @param flags Flags defining mode of usage paths comparison
537 * @param type Type of report we search.
538 * @retval NULL if no field is founded
539 * @retval Pointer to the founded report structure when founded
540 */
541usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
542 usb_hid_report_field_t *field, usb_hid_report_path_t *path, int flags,
543 usb_hid_report_type_t type)
544{
545 usb_hid_report_description_t *report_des =
546 usb_hid_report_find_description(report, path->report_id, type);
547
548 link_t *field_it;
549
550 if(report_des == NULL){
551 return NULL;
552 }
553
554 if(field == NULL){
555 field_it = report_des->report_items.next;
556 }
557 else {
558 field_it = field->link.next;
559 }
560
561 while(field_it != &report_des->report_items) {
562 field = list_get_instance(field_it, usb_hid_report_field_t,
563 link);
564
565 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
566 usb_hid_report_path_append_item (
567 field->collection_path, field->usage_page,
568 field->usage);
569
570 if(usb_hid_report_compare_usage_path(
571 field->collection_path, path, flags) == EOK){
572
573 usb_hid_report_remove_last_item(
574 field->collection_path);
575
576 return field;
577 }
578 usb_hid_report_remove_last_item (
579 field->collection_path);
580 }
581 field_it = field_it->next;
582 }
583
584 return NULL;
585}
586
587/*---------------------------------------------------------------------------*/
588/**
589 * Returns next report_id of report of specified type. If zero is given than
590 * first report_id of specified type is returned (0 is not legal value for
591 * repotr_id)
592 *
593 * @param report_id Current report_id, 0 if there is no current report_id
594 * @param type Type of searched report
595 * @param report Report structure inwhich we search
596 * @retval 0 if report structure is null or there is no specified report
597 * @retval report_id otherwise
598 */
599uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report,
600 uint8_t report_id, usb_hid_report_type_t type)
601{
602 if(report == NULL){
603 return 0;
604 }
605
606 usb_hid_report_description_t *report_des;
607 link_t *report_it;
608
609 if(report_id > 0) {
610 report_it = usb_hid_report_find_description(report, report_id,
611 type)->link.next;
612 }
613 else {
614 report_it = report->reports.next;
615 }
616
617 while(report_it != &report->reports) {
618 report_des = list_get_instance(report_it,
619 usb_hid_report_description_t, link);
620
621 if(report_des->type == type){
622 return report_des->report_id;
623 }
624 }
625
626 return 0;
627}
628
629/*---------------------------------------------------------------------------*/
630/**
631 * Reset all local items in given state table
632 *
633 * @param report_item State table containing current state of report
634 * descriptor parsing
635 *
636 * @return void
637 */
638void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
639{
640 if(report_item == NULL) {
641 return;
642 }
643
644 report_item->usages_count = 0;
645 memset(report_item->usages, 0, USB_HID_MAX_USAGES);
646
647 report_item->extended_usage_page = 0;
648 report_item->usage_minimum = 0;
649 report_item->usage_maximum = 0;
650 report_item->designator_index = 0;
651 report_item->designator_minimum = 0;
652 report_item->designator_maximum = 0;
653 report_item->string_index = 0;
654 report_item->string_minimum = 0;
655 report_item->string_maximum = 0;
656
657 return;
658}
659/**
660 * @}
661 */
Note: See TracBrowser for help on using the repository browser.