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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6283cefb was 6283cefb, checked in by Aearsis <Hlavaty.Ondrej@…>, 8 years ago

usbhid: rewrite the way values are extracted

There was a bug, and I'm not able to find it. After the rewrite, it is
working as expected.

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