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

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

HID parser refactoring

  • Property mode set to 100644
File size: 15.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 libusb
30 * @{
31 */
32/** @file
33 * HID report descriptor and report data parser implementation.
34 */
35#include <usb/classes/hidparser.h>
36#include <errno.h>
37#include <stdio.h>
38#include <malloc.h>
39#include <mem.h>
40#include <usb/debug.h>
41#include <assert.h>
42
43
44/*
45 * Data translation private functions
46 */
47uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
48inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
49int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
50uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value);
51int usb_pow(int a, int b);
52
53
54// TODO: tohle ma bejt asi jinde
55int usb_pow(int a, int b)
56{
57 switch(b) {
58 case 0:
59 return 1;
60 break;
61 case 1:
62 return a;
63 break;
64 default:
65 return a * usb_pow(a, b-1);
66 break;
67 }
68}
69
70
71
72
73/** Parse and act upon a HID report.
74 *
75 * @see usb_hid_parse_report_descriptor
76 *
77 * @param parser Opaque HID report parser structure.
78 * @param data Data for the report.
79 * @return Error code.
80 */
81int usb_hid_parse_report(const usb_hid_report_t *report,
82 const uint8_t *data, size_t size, uint8_t *report_id)
83{
84 link_t *list_item;
85 usb_hid_report_field_t *item;
86
87 usb_hid_report_description_t *report_des;
88 usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
89
90 if(report == NULL) {
91 return EINVAL;
92 }
93
94 if(report->use_report_ids != 0) {
95 *report_id = data[0];
96 }
97 else {
98 *report_id = 0;
99 }
100
101
102 report_des = usb_hid_report_find_description(report, *report_id, type);
103
104 /* read data */
105 list_item = report_des->report_items.next;
106 while(list_item != &(report_des->report_items)) {
107
108 item = list_get_instance(list_item, usb_hid_report_field_t, link);
109
110 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
111
112 if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
113
114 // array
115 item->value = usb_hid_translate_data(item, data);
116 item->usage = (item->value - item->physical_minimum) + item->usage_minimum;
117 }
118 else {
119 // variable item
120 item->value = usb_hid_translate_data(item, data);
121 }
122 }
123 list_item = list_item->next;
124 }
125
126 return EOK;
127
128}
129
130/**
131 * Translate data from the report as specified in report descriptor item
132 *
133 * @param item Report descriptor item with definition of translation
134 * @param data Data to translate
135 * @param j Index of processed field in report descriptor item
136 * @return Translated data
137 */
138int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
139{
140 int resolution;
141 int offset;
142 int part_size;
143
144 int32_t value=0;
145 int32_t mask;
146 const uint8_t *foo;
147
148 // now only shot tags are allowed
149 if(item->size > 32) {
150 return 0;
151 }
152
153 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
154 item->physical_minimum = item->logical_minimum;
155 item->physical_maximum = item->logical_maximum;
156 }
157
158
159 if(item->physical_maximum == item->physical_minimum){
160 resolution = 1;
161 }
162 else {
163 resolution = (item->logical_maximum - item->logical_minimum) /
164 ((item->physical_maximum - item->physical_minimum) *
165 (usb_pow(10,(item->unit_exponent))));
166 }
167
168 offset = item->offset;
169 // FIXME
170 if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
171
172 part_size = ((offset+item->size)%8);
173
174 size_t i=0;
175 for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
176 if(i == (size_t)(offset/8)) {
177 // the higher one
178 foo = data + i;
179 mask = ((1 << (item->size-part_size))-1);
180 value = (*foo & mask) << part_size;
181 }
182 else if(i == ((offset+item->size-1)/8)){
183 // the lower one
184 foo = data + i;
185 mask = ((1 << part_size)-1) << (8-part_size);
186 value += ((*foo & mask) >> (8-part_size));
187 }
188 else {
189 value = value << 8;
190 value += *(data + 1);
191 }
192 }
193 }
194 else {
195 foo = data+(offset/8);
196 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
197 value = (*foo & mask) >> (8-((offset%8)+item->size));
198 }
199
200 if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
201 value = USB_HID_UINT32_TO_INT32(value, item->size);
202 }
203
204 return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
205
206}
207
208/**
209 * Returns number of items in input report which are accessible by given usage path
210 *
211 * @param parser Opaque report descriptor structure
212 * @param path Usage path specification
213 * @param flags Usage path comparison flags
214 * @return Number of items in input report
215 */
216size_t usb_hid_report_input_length(const usb_hid_report_t *report,
217 usb_hid_report_path_t *path, int flags)
218{
219
220 size_t ret = 0;
221
222 if(report == NULL) {
223 return 0;
224 }
225
226 usb_hid_report_description_t *report_des;
227 report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_INPUT);
228 if(report_des == NULL) {
229 return 0;
230 }
231
232 link_t *field_it = report_des->report_items.next;
233 usb_hid_report_field_t *field;
234 while(field_it != &report_des->report_items) {
235
236 field = list_get_instance(field_it, usb_hid_report_field_t, link);
237 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
238
239 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
240 if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
241 ret++;
242 }
243 usb_hid_report_remove_last_item (field->collection_path);
244 }
245
246 field_it = field_it->next;
247 }
248
249 return ret;
250 }
251
252/*** OUTPUT API **/
253
254/**
255 * Allocates output report buffer for output report
256 *
257 * @param parser Report parsed structure
258 * @param size Size of returned buffer
259 * @param report_id Report id of created output report
260 * @return Returns allocated output buffer for specified output
261 */
262uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id)
263{
264 if(report == NULL) {
265 *size = 0;
266 return NULL;
267 }
268
269 link_t *report_it = report->reports.next;
270 usb_hid_report_description_t *report_des = NULL;
271 while(report_it != &report->reports) {
272 report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
273 if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
274 break;
275 }
276
277 report_it = report_it->next;
278 }
279
280 if(report_des == NULL){
281 *size = 0;
282 return NULL;
283 }
284 else {
285 *size = (report_des->bit_length + (8 - 1))/8;
286 uint8_t *ret = malloc((*size) * sizeof(uint8_t));
287 memset(ret, 0, (*size) * sizeof(uint8_t));
288 return ret;
289 }
290}
291
292
293/** Frees output report buffer
294 *
295 * @param output Output report buffer
296 * @return void
297 */
298void usb_hid_report_output_free(uint8_t *output)
299
300{
301 if(output != NULL) {
302 free (output);
303 }
304}
305
306/** Returns size of output for given usage path
307 *
308 * @param parser Opaque report parser structure
309 * @param path Usage path specified which items will be thought for the output
310 * @param flags Flags of usage path structure comparison
311 * @return Number of items matching the given usage path
312 */
313size_t usb_hid_report_output_size(usb_hid_report_t *report,
314 usb_hid_report_path_t *path, int flags)
315{
316 size_t ret = 0;
317 usb_hid_report_description_t *report_des;
318
319 if(report == NULL) {
320 return 0;
321 }
322
323 report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_OUTPUT);
324 if(report_des == NULL){
325 return 0;
326 }
327
328 link_t *field_it = report_des->report_items.next;
329 usb_hid_report_field_t *field;
330 while(field_it != &report_des->report_items) {
331
332 field = list_get_instance(field_it, usb_hid_report_field_t, link);
333 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0){
334 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
335 if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
336 ret++;
337 }
338 usb_hid_report_remove_last_item (field->collection_path);
339 }
340
341 field_it = field_it->next;
342 }
343
344 return ret;
345
346}
347
348/** Makes the output report buffer for data given in the report structure
349 *
350 * @param parser Opaque report parser structure
351 * @param path Usage path specifing which parts of output will be set
352 * @param flags Usage path structure comparison flags
353 * @param buffer Output buffer
354 * @param size Size of output buffer
355 * @return Error code
356 */
357int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
358 uint8_t *buffer, size_t size)
359{
360 link_t *item;
361 int32_t value=0;
362 int offset;
363 int length;
364 int32_t tmp_value;
365
366 if(report == NULL) {
367 return EINVAL;
368 }
369
370 if(report->use_report_ids != 0) {
371 buffer[0] = report_id;
372 }
373
374 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
375
376 usb_hid_report_description_t *report_des;
377 report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT);
378 if(report_des == NULL){
379 return EINVAL;
380 }
381
382 usb_hid_report_field_t *report_item;
383 item = report_des->report_items.next;
384 while(item != &report_des->report_items) {
385 report_item = list_get_instance(item, usb_hid_report_field_t, link);
386
387 if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
388
389 // array
390 value = usb_hid_translate_data_reverse(report_item, report_item->value);
391 offset = report_item->offset;
392 length = report_item->size;
393 }
394 else {
395 // variable item
396 value = usb_hid_translate_data_reverse(report_item, report_item->value);
397 offset = report_item->offset;
398 length = report_item->size;
399 }
400
401 if((offset/8) == ((offset+length-1)/8)) {
402 // je to v jednom bytu
403 if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
404 break; // TODO ErrorCode
405 }
406
407 size_t shift = offset%8;
408
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 & ((1 << (8-(offset%8)))-1);
423 tmp_value = tmp_value << (offset%8);
424
425 mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
426 buffer[i] = (buffer[i] & mask) | tmp_value;
427 }
428 else if (i == ((offset + length -1)/8)) {
429
430 value = value >> (length - ((offset + length) % 8));
431 value = value & ((1 << (length - ((offset + length) % 8))) - 1);
432
433 mask = (1 << (length - ((offset + length) % 8))) - 1;
434 buffer[i] = (buffer[i] & mask) | value;
435 }
436 else {
437 buffer[i] = value & (0xFF << i);
438 }
439 }
440 }
441
442
443 // reset value
444 report_item->value = 0;
445
446 item = item->next;
447 }
448
449 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
450
451 return EOK;
452}
453
454/**
455 * Translate given data for putting them into the outoput report
456 * @param item Report item structure
457 * @param value Value to translate
458 * @return ranslated value
459 */
460uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value)
461{
462 int ret=0;
463 int resolution;
464
465 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
466 ret = item->logical_minimum;
467 }
468
469 if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
470 item->physical_minimum = item->logical_minimum;
471 item->physical_maximum = item->logical_maximum;
472 }
473
474
475 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
476
477 // variable item
478 if(item->physical_maximum == item->physical_minimum){
479 resolution = 1;
480 }
481 else {
482 resolution = (item->logical_maximum - item->logical_minimum) /
483 ((item->physical_maximum - item->physical_minimum) *
484 (usb_pow(10,(item->unit_exponent))));
485 }
486
487 ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
488 }
489 else {
490 // bitmapa
491 if(value == 0) {
492 ret = 0;
493 }
494 else {
495 size_t bitmap_idx = (value - item->usage_minimum);
496 ret = 1 << bitmap_idx;
497 }
498 }
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)ret;
504}
505
506usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item)
507{
508 usb_hid_report_item_t *new_report_item;
509
510 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
511 return NULL;
512 }
513 memcpy(new_report_item,item, sizeof(usb_hid_report_item_t));
514 link_initialize(&(new_report_item->link));
515
516 return new_report_item;
517}
518
519
520usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
521 usb_hid_report_field_t *field,
522 usb_hid_report_path_t *path, int flags,
523 usb_hid_report_type_t type)
524{
525 usb_hid_report_description_t *report_des = usb_hid_report_find_description (report, path->report_id, type);
526 link_t *field_it;
527
528 if(report_des == NULL){
529 return NULL;
530 }
531
532 if(field == NULL){
533 // vezmu prvni co mathuje podle path!!
534 field_it = report_des->report_items.next;
535 }
536 else {
537 field_it = field->link.next;
538 }
539
540 while(field_it != &report_des->report_items) {
541 field = list_get_instance(field_it, usb_hid_report_field_t, link);
542
543 if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
544 usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
545 if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK){
546 usb_hid_report_remove_last_item (field->collection_path);
547 return field;
548 }
549 usb_hid_report_remove_last_item (field->collection_path);
550 }
551 field_it = field_it->next;
552 }
553
554 return NULL;
555}
556
557uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
558{
559 if(report == NULL){
560 return 0;
561 }
562
563 usb_hid_report_description_t *report_des;
564 link_t *report_it;
565
566 if(report_id == 0) {
567 report_it = usb_hid_report_find_description (report, report_id, type)->link.next;
568 }
569 else {
570 report_it = report->reports.next;
571 }
572
573 while(report_it != &report->reports) {
574 report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
575 if(report_des->type == type){
576 return report_des->report_id;
577 }
578 }
579
580 return 0;
581}
582
583void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
584{
585 if(report_item == NULL) {
586 return;
587 }
588
589 report_item->usages_count = 0;
590 memset(report_item->usages, 0, USB_HID_MAX_USAGES);
591
592 report_item->extended_usage_page = 0;
593 report_item->usage_minimum = 0;
594 report_item->usage_maximum = 0;
595 report_item->designator_index = 0;
596 report_item->designator_minimum = 0;
597 report_item->designator_maximum = 0;
598 report_item->string_index = 0;
599 report_item->string_minimum = 0;
600 report_item->string_maximum = 0;
601
602 return;
603}
604/**
605 * @}
606 */
Note: See TracBrowser for help on using the repository browser.