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

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

Development changes merge
Correct parsing of usages in case of array items

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