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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e2d387 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 15.2 KB
Line 
1/*
2 * Copyright (c) 2011 Matej Klonfar
3 * Copyright (c) 2018 Ondrej Hlavaty
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libusbhid
31 * @{
32 */
33/** @file
34 * USB HID report data parser implementation.
35 */
36#include <usb/hid/hidparser.h>
37#include <errno.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <mem.h>
41#include <usb/debug.h>
42#include <assert.h>
43#include <bitops.h>
44#include <macros.h>
45
46
47/*
48 * Data translation private functions
49 */
50uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
51
52int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
53
54uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
55 int32_t value);
56
57
58
59static int 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 } else {
95 return report_des->item_length;
96 }
97}
98
99/** Returns size of report of specified report id and type in bytes
100 *
101 * @param parser Opaque report parser structure
102 * @param report_id
103 * @param type
104 * @return Number of items in specified report
105 */
106size_t usb_hid_report_byte_size(usb_hid_report_t *report, uint8_t report_id,
107 usb_hid_report_type_t type)
108{
109 usb_hid_report_description_t *report_des;
110
111 if (report == NULL) {
112 return 0;
113 }
114
115 report_des = usb_hid_report_find_description(report, report_id, type);
116 if (report_des == NULL) {
117 return 0;
118 } else {
119 return ((report_des->bit_length + 7) / 8);
120 }
121}
122
123
124/** Parse and act upon a HID report.
125 *
126 * @see usb_hid_parse_report_descriptor
127 *
128 * @param parser Opaque HID report parser structure.
129 * @param data Data for the report.
130 * @return Error code.
131 */
132errno_t usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data,
133 size_t size, uint8_t *report_id)
134{
135 usb_hid_report_description_t *report_des;
136 usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
137
138 if (report == NULL) {
139 return EINVAL;
140 }
141
142 if (report->use_report_ids != 0) {
143 *report_id = data[0];
144 } else {
145 *report_id = 0;
146 }
147
148 report_des = usb_hid_report_find_description(report, *report_id,
149 type);
150
151 if (report_des == NULL) {
152 return EINVAL;
153 }
154
155 /* read data */
156 list_foreach(report_des->report_items, ritems_link,
157 usb_hid_report_field_t, item) {
158
159 if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
160
161 if (USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
162 /* array */
163 item->value =
164 usb_hid_translate_data(item, data);
165
166 item->usage = USB_HID_EXTENDED_USAGE(
167 item->usages[item->value -
168 item->physical_minimum]);
169
170 item->usage_page =
171 USB_HID_EXTENDED_USAGE_PAGE(
172 item->usages[item->value -
173 item->physical_minimum]);
174
175 usb_hid_report_set_last_item(
176 item->collection_path,
177 USB_HID_TAG_CLASS_GLOBAL,
178 item->usage_page);
179
180 usb_hid_report_set_last_item(
181 item->collection_path,
182 USB_HID_TAG_CLASS_LOCAL, item->usage);
183 } else {
184 /* variable item */
185 item->value = usb_hid_translate_data(item,
186 data);
187 }
188 }
189 }
190
191 return EOK;
192}
193
194
195/**
196 * Translate data from the report as specified in report descriptor item
197 *
198 * @param item Report descriptor item with definition of translation
199 * @param data Data to translate
200 * @return Translated data
201 */
202int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
203{
204 /* now only short tags are allowed */
205 if (item->size > 32) {
206 return 0;
207 }
208
209 if ((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
210 item->physical_minimum = item->logical_minimum;
211 item->physical_maximum = item->logical_maximum;
212 }
213
214 int resolution;
215 if (item->physical_maximum == item->physical_minimum) {
216 resolution = 1;
217 } else {
218 resolution = (item->logical_maximum - item->logical_minimum) /
219 ((item->physical_maximum - item->physical_minimum) *
220 (usb_pow(10, (item->unit_exponent))));
221 }
222
223 int32_t value = 0;
224
225 /* First, skip all bytes we don't care */
226 data += item->offset / 8;
227
228 int bits = item->size;
229 int taken = 0;
230
231 /* Than we take the higher bits from the LSB */
232 const unsigned bit_offset = item->offset % 8;
233 const int lsb_bits = min(bits, 8);
234
235 value |= (*data >> bit_offset) & BIT_RRANGE(uint8_t, lsb_bits);
236 bits -= lsb_bits;
237 taken += lsb_bits;
238 data++;
239
240 /* Then there may be bytes, which we take as a whole. */
241 while (bits > 8) {
242 value |= *data << taken;
243 taken += 8;
244 bits -= 8;
245 data++;
246 }
247
248 /* And, finally, lower bits from HSB. */
249 if (bits > 0) {
250 value |= (*data & BIT_RRANGE(uint8_t, bits)) << taken;
251 }
252
253 if ((item->logical_minimum < 0) || (item->logical_maximum < 0)) {
254 value = USB_HID_UINT32_TO_INT32(value, item->size);
255 }
256
257 return (int) (((value - item->logical_minimum) / resolution) +
258 item->physical_minimum);
259}
260
261
262/* OUTPUT API */
263
264/**
265 * Allocates output report buffer for output report
266 *
267 * @param parser Report parsed structure
268 * @param size Size of returned buffer
269 * @param report_id Report id of created output report
270 * @return Returns allocated output buffer for specified output
271 */
272uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size,
273 uint8_t report_id)
274{
275 if (report == NULL) {
276 *size = 0;
277 return NULL;
278 }
279
280 usb_hid_report_description_t *report_des = NULL;
281
282 list_foreach(report->reports, reports_link,
283 usb_hid_report_description_t, rdes) {
284 if ((rdes->report_id == report_id) &&
285 (rdes->type == USB_HID_REPORT_TYPE_OUTPUT)) {
286 report_des = rdes;
287 break;
288 }
289 }
290
291 if (report_des == NULL) {
292 *size = 0;
293 return NULL;
294 } else {
295 *size = (report_des->bit_length + (8 - 1)) / 8;
296 uint8_t *ret = malloc((*size) * sizeof(uint8_t));
297 memset(ret, 0, (*size) * sizeof(uint8_t));
298 return ret;
299 }
300}
301
302
303/** Frees output report buffer
304 *
305 * @param output Output report buffer
306 * @return void
307 */
308void usb_hid_report_output_free(uint8_t *output)
309{
310 if (output != NULL) {
311 free(output);
312 }
313}
314
315/** Makes the output report buffer for data given in the report structure
316 *
317 * @param parser Opaque report parser structure
318 * @param path Usage path specifing which parts of output will be set
319 * @param flags Usage path structure comparison flags
320 * @param buffer Output buffer
321 * @param size Size of output buffer
322 * @return Error code
323 */
324errno_t usb_hid_report_output_translate(usb_hid_report_t *report,
325 uint8_t report_id, uint8_t *buffer, size_t size)
326{
327 int32_t value = 0;
328 int offset;
329 int length;
330 int32_t tmp_value;
331
332 if (report == NULL) {
333 return EINVAL;
334 }
335
336 if (report->use_report_ids != 0) {
337 buffer[0] = report_id;
338 }
339
340 usb_hid_report_description_t *report_des;
341 report_des = usb_hid_report_find_description(report, report_id,
342 USB_HID_REPORT_TYPE_OUTPUT);
343
344 if (report_des == NULL) {
345 return EINVAL;
346 }
347
348 list_foreach(report_des->report_items, ritems_link,
349 usb_hid_report_field_t, report_item) {
350 value = usb_hid_translate_data_reverse(report_item,
351 report_item->value);
352
353 offset = report_des->bit_length - report_item->offset - 1;
354 length = report_item->size;
355
356 usb_log_debug("\ttranslated value: %x", value);
357
358 if ((offset / 8) == ((offset + length - 1) / 8)) {
359 if (((size_t) (offset / 8) >= size) ||
360 ((size_t) (offset + length - 1) / 8) >= size) {
361 break; // TODO ErrorCode
362 }
363 size_t shift = 8 - offset % 8 - length;
364 value = value << shift;
365 value = value & (((1 << length) - 1) << shift);
366
367 uint8_t mask = 0;
368 mask = 0xff - (((1 << length) - 1) << shift);
369 buffer[offset / 8] = (buffer[offset / 8] & mask) |
370 value;
371 } else {
372 int i = 0;
373 uint8_t mask = 0;
374 for (i = (offset / 8);
375 i <= ((offset + length - 1) / 8); i++) {
376 if (i == (offset / 8)) {
377 tmp_value = value;
378 tmp_value = tmp_value &
379 ((1 << (8 - (offset % 8))) - 1);
380
381 tmp_value = tmp_value << (offset % 8);
382
383 mask = ~(((1 << (8 - (offset % 8))) - 1) << (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 return 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) == 0) {
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, sizeof(report_item->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.