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

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

Report IDs support

  • Property mode set to 100644
File size: 33.8 KB
RevLine 
[bf2063e9]1/*
2 * Copyright (c) 2010 Vojtech Horky
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>
[976f546]41
[57d9c05e]42/** */
[b7d9606]43#define USB_HID_NEW_REPORT_ITEM 1
[57d9c05e]44
45/** */
[b7d9606]46#define USB_HID_NO_ACTION 2
[976f546]47
[57d9c05e]48/** */
49#define USB_HID_UNKNOWN_TAG -99
[976f546]50
[57d9c05e]51/*
52 * Private descriptor parser functions
53 */
[b7d9606]54int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
[b53d3b7]55 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
[c7a2e7e]56int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
[b53d3b7]57 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
[c7a2e7e]58int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
[b53d3b7]59 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
[c7a2e7e]60int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
[b53d3b7]61 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
[976f546]62
[e24e7b1]63void usb_hid_descriptor_print_list(link_t *head);
[976f546]64int usb_hid_report_reset_local_items();
[e24e7b1]65void usb_hid_free_report_list(link_t *head);
[57d9c05e]66
67/*
68 * Data translation private functions
69 */
[c7a2e7e]70int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
[b7d9606]71inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
[fad14d7]72int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j);
[841e6e5]73int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int32_t value);
[fad14d7]74int usb_pow(int a, int b);
75
[57d9c05e]76// TODO: tohle ma bejt asi jinde
[fad14d7]77int usb_pow(int a, int b)
78{
79 switch(b) {
80 case 0:
81 return 1;
82 break;
83 case 1:
84 return a;
85 break;
86 default:
87 return a * usb_pow(a, b-1);
88 break;
89 }
90}
91
[976f546]92/**
[57d9c05e]93 * Initialize the report descriptor parser structure
[976f546]94 *
[57d9c05e]95 * @param parser Report descriptor parser structure
96 * @return Error code
[976f546]97 */
98int usb_hid_parser_init(usb_hid_report_parser_t *parser)
99{
[57d9c05e]100 if(parser == NULL) {
101 return EINVAL;
102 }
[976f546]103
[57d9c05e]104 list_initialize(&(parser->input));
[976f546]105 list_initialize(&(parser->output));
106 list_initialize(&(parser->feature));
[da3965e]107
[bda06a3]108 parser->use_report_id = 0;
[da3965e]109 return EOK;
[976f546]110}
111
[bf2063e9]112
113/** Parse HID report descriptor.
114 *
115 * @param parser Opaque HID report parser structure.
116 * @param data Data describing the report.
117 * @return Error code.
118 */
119int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
[b7d9606]120 const uint8_t *data, size_t size)
[bf2063e9]121{
[976f546]122 size_t i=0;
123 uint8_t tag=0;
124 uint8_t item_size=0;
125 int class=0;
126 int ret;
127 usb_hid_report_item_t *report_item=0;
[b53d3b7]128 usb_hid_report_item_t *new_report_item;
129 usb_hid_report_path_t *usage_path;
130 usb_hid_report_path_t *tmp_usage_path;
[c7a2e7e]131
[60a228f]132 size_t offset_input=0;
133 size_t offset_output=0;
134 size_t offset_feature=0;
[b53d3b7]135
[55e388a1]136
[b53d3b7]137 /* parser structure initialization*/
[55e388a1]138 if(usb_hid_parser_init(parser) != EOK) {
139 return EINVAL;
140 }
[976f546]141
142
[b53d3b7]143 /*report item initialization*/
[976f546]144 if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
145 return ENOMEM;
146 }
[60a228f]147 memset(report_item, 0, sizeof(usb_hid_report_item_t));
[b53d3b7]148 list_initialize(&(report_item->link));
[8bec4d1]149
[b53d3b7]150 /* usage path context initialization */
151 if(!(usage_path=usb_hid_report_path())){
152 return ENOMEM;
153 }
154
[b7d9606]155 while(i<size){
[976f546]156 if(!USB_HID_ITEM_IS_LONG(data[i])){
[b7d9606]157
[8bec4d1]158 if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
[55e388a1]159 return EINVAL; // TODO ERROR CODE
[b7d9606]160 }
161
[976f546]162 tag = USB_HID_ITEM_TAG(data[i]);
163 item_size = USB_HID_ITEM_SIZE(data[i]);
164 class = USB_HID_ITEM_TAG_CLASS(data[i]);
[b7d9606]165
166 usb_log_debug2(
167 "i(%u) data(%X) value(%X): TAG %u, class %u, size %u - ", i,
168 data[i], usb_hid_report_tag_data_int32(data+i+1,item_size),
169 tag, class, item_size);
170
171 ret = usb_hid_report_parse_tag(tag,class,data+i+1,
[b53d3b7]172 item_size,report_item, usage_path);
[fad14d7]173 usb_log_debug2("ret: %u\n", ret);
[976f546]174 switch(ret){
175 case USB_HID_NEW_REPORT_ITEM:
176 // store report item to report and create the new one
[b53d3b7]177 usb_log_debug("\nNEW REPORT ITEM: %X",ret);
178
179 // store current usage path
180 report_item->usage_path = usage_path;
181
[96bfe76]182 // clone path to the new one
183 tmp_usage_path = usb_hid_report_path_clone(usage_path);
[b53d3b7]184
185 // swap
186 usage_path = tmp_usage_path;
187 tmp_usage_path = NULL;
188
[bda06a3]189 usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);
190 if(report_item->id != 0){
191 parser->use_report_id = 1;
192 }
[fad14d7]193
[976f546]194 switch(tag) {
195 case USB_HID_REPORT_TAG_INPUT:
[60a228f]196 report_item->offset = offset_input;
197 offset_input += report_item->count * report_item->size;
[b7d9606]198 usb_log_debug(" - INPUT\n");
[976f546]199 list_append(&(report_item->link), &(parser->input));
200 break;
201 case USB_HID_REPORT_TAG_OUTPUT:
[60a228f]202 report_item->offset = offset_output;
203 offset_output += report_item->count * report_item->size;
[b7d9606]204 usb_log_debug(" - OUTPUT\n");
[976f546]205 list_append(&(report_item->link), &(parser->output));
206
207 break;
208 case USB_HID_REPORT_TAG_FEATURE:
[60a228f]209 report_item->offset = offset_feature;
210 offset_feature += report_item->count * report_item->size;
[b7d9606]211 usb_log_debug(" - FEATURE\n");
[976f546]212 list_append(&(report_item->link), &(parser->feature));
213 break;
214 default:
[b7d9606]215 usb_log_debug("\tjump over - tag %X\n", tag);
[976f546]216 break;
217 }
218
219 /* clone current state table to the new item */
220 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
221 return ENOMEM;
222 }
223 memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));
[c32688d]224 /* reset local items */
225 new_report_item->usage_minimum = 0;
226 new_report_item->usage_maximum = 0;
227
[976f546]228 link_initialize(&(new_report_item->link));
229 report_item = new_report_item;
[b53d3b7]230
[976f546]231 break;
232 case USB_HID_REPORT_TAG_PUSH:
233 // push current state to stack
234 // not yet implemented
235 break;
236 case USB_HID_REPORT_TAG_POP:
237 // restore current state from stack
238 // not yet implemented
239 break;
240
241 default:
[b7d9606]242 // nothing special to do
[976f546]243 break;
244 }
245
246 /* jump over the processed block */
247 i += 1 + USB_HID_ITEM_SIZE(data[i]);
248 }
249 else{
250 // TBD
251 i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
252 }
253
254
255 }
256
257 return EOK;
[bf2063e9]258}
259
[2f60e57d]260
261/**
262 * Parse input report.
263 *
264 * @param data Data for report
265 * @param size Size of report
266 * @param callbacks Callbacks for report actions
267 * @param arg Custom arguments
268 *
269 * @return Error code
270 */
[b7d9606]271int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
[2f60e57d]272 const usb_hid_report_in_callbacks_t *callbacks, void *arg)
273{
274 int i;
275 usb_hid_report_item_t item;
276
277 /* fill item due to the boot protocol report descriptor */
278 // modifier keys are in the first byte
279 uint8_t modifiers = data[0];
280
281 item.offset = 2; /* second byte is reserved */
282 item.size = 8;
283 item.count = 6;
[976f546]284 item.usage_minimum = 0;
285 item.usage_maximum = 255;
286 item.logical_minimum = 0;
287 item.logical_maximum = 255;
[2f60e57d]288
[976f546]289 if (size != 8) {
[e24e7b1]290 return -1; //ERANGE;
[2f60e57d]291 }
292
293 uint8_t keys[6];
[976f546]294 for (i = 0; i < item.count; i++) {
295 keys[i] = data[i + item.offset];
[2f60e57d]296 }
297
298 callbacks->keyboard(keys, 6, modifiers, arg);
[e7726a4]299 return EOK;
[bf2063e9]300}
301
[2f60e57d]302/**
303 * Makes output report for keyboard boot protocol
304 *
305 * @param leds
306 * @param output Output report data buffer
307 * @param size Size of the output buffer
308 * @return Error code
309 */
310int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
311{
312 if(size != 1){
313 return -1;
314 }
315
316 /* used only first five bits, others are only padding*/
317 *data = leds;
318 return EOK;
319}
[bf2063e9]320
[976f546]321/**
[57d9c05e]322 * Parse one tag of the report descriptor
[976f546]323 *
324 * @param Tag to parse
325 * @param Report descriptor buffer
326 * @param Size of data belongs to this tag
327 * @param Current report item structe
328 * @return Code of action to be done next
329 */
[b7d9606]330int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
[b53d3b7]331 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
[976f546]332{
[b7d9606]333 int ret;
334
[976f546]335 switch(class){
336 case USB_HID_TAG_CLASS_MAIN:
337
[b53d3b7]338 if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
[976f546]339 return USB_HID_NEW_REPORT_ITEM;
340 }
341 else {
342 /*TODO process the error */
[b7d9606]343 return ret;
[976f546]344 }
345 break;
346
347 case USB_HID_TAG_CLASS_GLOBAL:
[b53d3b7]348 return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
[976f546]349 break;
350
351 case USB_HID_TAG_CLASS_LOCAL:
[b53d3b7]352 return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
[976f546]353 break;
354 default:
[b7d9606]355 return USB_HID_NO_ACTION;
[976f546]356 }
357}
358
359/**
360 * Parse main tags of report descriptor
361 *
362 * @param Tag identifier
363 * @param Data buffer
364 * @param Length of data buffer
365 * @param Current state table
366 * @return Error code
367 */
368
[c7a2e7e]369int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
[b53d3b7]370 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
371{
[976f546]372 switch(tag)
373 {
374 case USB_HID_REPORT_TAG_INPUT:
375 case USB_HID_REPORT_TAG_OUTPUT:
376 case USB_HID_REPORT_TAG_FEATURE:
[b7d9606]377 report_item->item_flags = *data;
378 return EOK;
[976f546]379 break;
380
381 case USB_HID_REPORT_TAG_COLLECTION:
[b53d3b7]382 usb_hid_report_path_append_item(usage_path, 0, 0);
383
[8bec4d1]384 return USB_HID_NO_ACTION;
[976f546]385 break;
386
387 case USB_HID_REPORT_TAG_END_COLLECTION:
[b53d3b7]388 // TODO
389 // znici posledni uroven ve vsech usage paths
390 // otazka jestli nema nicit dve, respektive novou posledni vynulovat?
391 usb_hid_report_remove_last_item(usage_path);
[8bec4d1]392 return USB_HID_NO_ACTION;
[976f546]393 break;
394 default:
[b7d9606]395 return USB_HID_NO_ACTION;
[976f546]396 }
397
[8bec4d1]398 return EOK;
[976f546]399}
400
401/**
402 * Parse global tags of report descriptor
403 *
404 * @param Tag identifier
405 * @param Data buffer
406 * @param Length of data buffer
407 * @param Current state table
408 * @return Error code
409 */
[c7a2e7e]410int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
[b53d3b7]411 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
[976f546]412{
413 // TODO take care about the bit length of data
414 switch(tag)
415 {
416 case USB_HID_REPORT_TAG_USAGE_PAGE:
[b53d3b7]417 // zmeni to jenom v poslednim poli aktualni usage path
418 usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL,
419 usb_hid_report_tag_data_int32(data,item_size));
[976f546]420 break;
421 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
422 report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
423 break;
424 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
425 report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
426 break;
427 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
428 report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
429 break;
430 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
431 report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
432 break;
433 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
434 report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
435 break;
436 case USB_HID_REPORT_TAG_UNIT:
437 report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
438 break;
439 case USB_HID_REPORT_TAG_REPORT_SIZE:
440 report_item->size = usb_hid_report_tag_data_int32(data,item_size);
441 break;
442 case USB_HID_REPORT_TAG_REPORT_COUNT:
443 report_item->count = usb_hid_report_tag_data_int32(data,item_size);
444 break;
445 case USB_HID_REPORT_TAG_REPORT_ID:
446 report_item->id = usb_hid_report_tag_data_int32(data,item_size);
447 break;
448 case USB_HID_REPORT_TAG_PUSH:
449 case USB_HID_REPORT_TAG_POP:
450 return tag;
451 break;
452
453 default:
[b7d9606]454 return USB_HID_NO_ACTION;
[976f546]455 }
[da3965e]456
457 return EOK;
[976f546]458}
459
460/**
461 * Parse local tags of report descriptor
462 *
463 * @param Tag identifier
464 * @param Data buffer
465 * @param Length of data buffer
466 * @param Current state table
467 * @return Error code
468 */
[c7a2e7e]469int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
[b53d3b7]470 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
[976f546]471{
472 switch(tag)
473 {
474 case USB_HID_REPORT_TAG_USAGE:
[b53d3b7]475 usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL,
476 usb_hid_report_tag_data_int32(data,item_size));
[976f546]477 break;
478 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
479 report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
480 break;
481 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
482 report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
483 break;
484 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
485 report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
486 break;
487 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
488 report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
489 break;
490 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
491 report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
492 break;
493 case USB_HID_REPORT_TAG_STRING_INDEX:
494 report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
495 break;
496 case USB_HID_REPORT_TAG_STRING_MINIMUM:
497 report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
498 break;
499 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
500 report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
[3de529c]501 break;
[976f546]502 case USB_HID_REPORT_TAG_DELIMITER:
503 report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
504 break;
[3de529c]505
[976f546]506 default:
[b7d9606]507 return USB_HID_NO_ACTION;
[976f546]508 }
[da3965e]509
510 return EOK;
[976f546]511}
512
513/**
514 * Converts raw data to int32 (thats the maximum length of short item data)
515 *
516 * @param Data buffer
517 * @param Size of buffer
518 * @return Converted int32 number
519 */
[c7a2e7e]520int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
[976f546]521{
[da3965e]522 unsigned int i;
[976f546]523 int32_t result;
524
525 result = 0;
526 for(i=0; i<size; i++) {
527 result = (result | (data[i]) << (i*8));
528 }
529
530 return result;
531}
532
533
534
535/**
536 * Prints content of given list of report items.
537 *
[57d9c05e]538 * @param List of report items (usb_hid_report_item_t)
[976f546]539 * @return void
540 */
541void usb_hid_descriptor_print_list(link_t *head)
542{
543 usb_hid_report_item_t *report_item;
[b53d3b7]544 usb_hid_report_usage_path_t *path_item;
545 link_t *path;
[976f546]546 link_t *item;
547
548 if(head == NULL || list_empty(head)) {
[60a228f]549 usb_log_debug("\tempty\n");
[976f546]550 return;
551 }
[b7d9606]552
[976f546]553 for(item = head->next; item != head; item = item->next) {
554
555 report_item = list_get_instance(item, usb_hid_report_item_t, link);
556
[60a228f]557 usb_log_debug("\tOFFSET: %X\n", report_item->offset);
558 usb_log_debug("\tCOUNT: %X\n", report_item->count);
559 usb_log_debug("\tSIZE: %X\n", report_item->size);
[c32688d]560 usb_log_debug("\tCONSTANT/VAR: %X\n", USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags));
561 usb_log_debug("\tVARIABLE/ARRAY: %X\n", USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags));
[b53d3b7]562 usb_log_debug("\tUSAGE PATH:\n");
563
564 path = report_item->usage_path->link.next;
565 while(path != &report_item->usage_path->link) {
566 path_item = list_get_instance(path, usb_hid_report_usage_path_t, link);
567 usb_log_debug("\t\tUSAGE PAGE: %X, USAGE: %X\n", path_item->usage_page, path_item->usage);
568 path = path->next;
569 }
[57d9c05e]570
[60a228f]571 usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum);
572 usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum);
573 usb_log_debug("\tPHYMIN: %X\n", report_item->physical_minimum);
574 usb_log_debug("\tPHYMAX: %X\n", report_item->physical_maximum);
[c32688d]575 usb_log_debug("\tUSAGEMIN: %X\n", report_item->usage_minimum);
576 usb_log_debug("\tUSAGEMAX: %X\n", report_item->usage_maximum);
577
[60a228f]578 usb_log_debug("\n");
[976f546]579
580 }
581
582
583}
584/**
[57d9c05e]585 * Prints content of given report descriptor in human readable format.
[976f546]586 *
[57d9c05e]587 * @param parser Parsed descriptor to print
[976f546]588 * @return void
589 */
590void usb_hid_descriptor_print(usb_hid_report_parser_t *parser)
591{
[55e388a1]592 if(parser == NULL) {
593 return;
594 }
595
[60a228f]596 usb_log_debug("INPUT:\n");
[976f546]597 usb_hid_descriptor_print_list(&parser->input);
598
[60a228f]599 usb_log_debug("OUTPUT: \n");
[976f546]600 usb_hid_descriptor_print_list(&parser->output);
601
[60a228f]602 usb_log_debug("FEATURE:\n");
[976f546]603 usb_hid_descriptor_print_list(&parser->feature);
604
605}
606
607/**
608 * Releases whole linked list of report items
609 *
[57d9c05e]610 * @param head Head of list of report descriptor items (usb_hid_report_item_t)
611 * @return void
[976f546]612 */
[e24e7b1]613void usb_hid_free_report_list(link_t *head)
[976f546]614{
[e24e7b1]615 return;
[e259d95]616
[976f546]617 usb_hid_report_item_t *report_item;
[e259d95]618 link_t *next;
[976f546]619
620 if(head == NULL || list_empty(head)) {
[e24e7b1]621 return;
[976f546]622 }
[e259d95]623
624 next = head->next;
625 while(next != head) {
626
627 report_item = list_get_instance(next, usb_hid_report_item_t, link);
[b53d3b7]628
629 while(!list_empty(&report_item->usage_path->link)) {
630 usb_hid_report_remove_last_item(report_item->usage_path);
631 }
632
633
[e259d95]634 next = next->next;
[976f546]635
[e259d95]636 free(report_item);
[976f546]637 }
[e259d95]638
[e24e7b1]639 return;
[e259d95]640
[976f546]641}
642
[57d9c05e]643/** Frees the HID report descriptor parser structure
[19a1800]644 *
645 * @param parser Opaque HID report parser structure
[57d9c05e]646 * @return void
[976f546]647 */
648void usb_hid_free_report_parser(usb_hid_report_parser_t *parser)
649{
650 if(parser == NULL){
651 return;
652 }
653
[bda06a3]654 parser->use_report_id = 0;
655
[976f546]656 usb_hid_free_report_list(&parser->input);
657 usb_hid_free_report_list(&parser->output);
658 usb_hid_free_report_list(&parser->feature);
659
660 return;
661}
[c7a2e7e]662
[fad14d7]663/** Parse and act upon a HID report.
664 *
665 * @see usb_hid_parse_report_descriptor
666 *
667 * @param parser Opaque HID report parser structure.
668 * @param data Data for the report.
669 * @param callbacks Callbacks for report actions.
670 * @param arg Custom argument (passed through to the callbacks).
671 * @return Error code.
672 */
673int usb_hid_parse_report(const usb_hid_report_parser_t *parser,
674 const uint8_t *data, size_t size,
[b53d3b7]675 usb_hid_report_path_t *path, int flags,
[fad14d7]676 const usb_hid_report_in_callbacks_t *callbacks, void *arg)
677{
678 link_t *list_item;
679 usb_hid_report_item_t *item;
680 uint8_t *keys;
[c32688d]681 uint8_t item_value;
[fad14d7]682 size_t key_count=0;
683 size_t i=0;
684 size_t j=0;
[bda06a3]685 uint8_t report_id = 0;
[fad14d7]686
[55e388a1]687 if(parser == NULL) {
688 return EINVAL;
689 }
690
[57d9c05e]691 /* get the size of result array */
[b53d3b7]692 key_count = usb_hid_report_input_length(parser, path, flags);
[fad14d7]693
694 if(!(keys = malloc(sizeof(uint8_t) * key_count))){
695 return ENOMEM;
696 }
697
[bda06a3]698 if(parser->use_report_id != 0) {
699 report_id = data[0];
700 usb_hid_report_path_set_report_id(path, report_id);
701 }
702
[57d9c05e]703 /* read data */
[fad14d7]704 list_item = parser->input.next;
705 while(list_item != &(parser->input)) {
706
707 item = list_get_instance(list_item, usb_hid_report_item_t, link);
[bda06a3]708
[b53d3b7]709 if(!USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) &&
710 (usb_hid_report_compare_usage_path(item->usage_path, path, flags) == EOK)) {
[fad14d7]711 for(j=0; j<(size_t)(item->count); j++) {
[c32688d]712 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) ||
713 ((item->usage_minimum == 0) && (item->usage_maximum == 0))) {
714 // variable item
715 keys[i++] = usb_hid_translate_data(item, data,j);
716 }
717 else {
718 // bitmapa
719 if((item_value = usb_hid_translate_data(item, data, j)) != 0) {
[b53d3b7]720 keys[i++] = (item->count - 1 - j) + item->usage_minimum;
[c32688d]721 }
722 else {
723 keys[i++] = 0;
724 }
725 }
[fad14d7]726 }
727 }
728 list_item = list_item->next;
729 }
730
[bda06a3]731 callbacks->keyboard(keys, key_count, report_id, arg);
[fad14d7]732
733 free(keys);
734 return EOK;
735
736}
737
[57d9c05e]738/**
739 * Translate data from the report as specified in report descriptor
740 *
741 * @param item Report descriptor item with definition of translation
742 * @param data Data to translate
743 * @param j Index of processed field in report descriptor item
744 * @return Translated data
745 */
[fad14d7]746int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j)
[c7a2e7e]747{
[fad14d7]748 int resolution;
749 int offset;
750 int part_size;
751
752 int32_t value;
753 int32_t mask;
754 const uint8_t *foo;
[bda06a3]755
[fad14d7]756 // now only common numbers llowed
757 if(item->size > 32) {
758 return 0;
759 }
760
[60a228f]761 if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
[fad14d7]762 item->physical_minimum = item->logical_minimum;
763 item->physical_maximum = item->logical_maximum;
764 }
765
[60a228f]766 if(item->physical_maximum == item->physical_minimum){
767 resolution = 1;
768 }
769 else {
770 resolution = (item->logical_maximum - item->logical_minimum) /
771 ((item->physical_maximum - item->physical_minimum) *
772 (usb_pow(10,(item->unit_exponent))));
773 }
[bda06a3]774
[fad14d7]775 offset = item->offset + (j * item->size);
[bda06a3]776 if(item->id != 0) {
777 offset += 8;
778 usb_log_debug("MOVED OFFSET BY 1Byte, REPORT_ID(%d)\n", item->id);
779 }
[fad14d7]780
781 // FIXME
782 if((offset/8) != ((offset+item->size)/8)) {
783 usb_log_debug2("offset %d\n", offset);
784
785 part_size = ((offset+item->size)%8);
786 usb_log_debug2("part size %d\n",part_size);
787
788 // the higher one
789 foo = data+(offset/8);
790 mask = ((1 << (item->size-part_size))-1);
791 value = (*foo & mask) << part_size;
792
793 usb_log_debug2("hfoo %x\n", *foo);
794 usb_log_debug2("hmaska %x\n", mask);
795 usb_log_debug2("hval %d\n", value);
796
797 // the lower one
798 foo = data+((offset+item->size)/8);
799 mask = ((1 << part_size)-1) << (8-part_size);
800 value += ((*foo & mask) >> (8-part_size));
801
802 usb_log_debug2("lfoo %x\n", *foo);
803 usb_log_debug2("lmaska %x\n", mask);
804 usb_log_debug2("lval %d\n", ((*foo & mask) >> (8-(item->size-part_size))));
805 usb_log_debug2("val %d\n", value);
806
807
808 }
809 else {
810 foo = data+(offset/8);
811 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
812 value = (*foo & mask) >> (8-((offset%8)+item->size));
813
814 usb_log_debug2("offset %d\n", offset);
[60a228f]815
[fad14d7]816 usb_log_debug2("foo %x\n", *foo);
817 usb_log_debug2("maska %x\n", mask);
818 usb_log_debug2("val %d\n", value);
819 }
820
[60a228f]821 usb_log_debug2("---\n\n");
[fad14d7]822
[767da0a]823 return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
[fad14d7]824
[c7a2e7e]825}
[0bd4810c]826
[57d9c05e]827/**
828 *
829 *
830 * @param parser
831 * @param path
832 * @param flags
833 * @return
834 */
[96bfe76]835size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
[b53d3b7]836 usb_hid_report_path_t *path, int flags)
[55e388a1]837{
[57d9c05e]838 size_t ret = 0;
[0bd4810c]839 link_t *item;
840 usb_hid_report_item_t *report_item;
841
[55e388a1]842 if(parser == NULL) {
[57d9c05e]843 return 0;
[55e388a1]844 }
845
[57d9c05e]846 item = parser->input.next;
[0bd4810c]847 while(&parser->input != item) {
848 report_item = list_get_instance(item, usb_hid_report_item_t, link);
[33382a9]849 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
[b53d3b7]850 (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
[0bd4810c]851 ret += report_item->count;
852 }
853
854 item = item->next;
855 }
856
857 return ret;
858}
859
860
[b53d3b7]861/**
862 *
[57d9c05e]863 * @param usage_path
864 * @param usage_page
865 * @param usage
866 * @return
[b53d3b7]867 */
868int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
869 int32_t usage_page, int32_t usage)
870{
871 usb_hid_report_usage_path_t *item;
872
873 if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
874 return ENOMEM;
875 }
876 list_initialize(&item->link);
877
878 item->usage = usage;
879 item->usage_page = usage_page;
880
881 list_append (&usage_path->link, &item->link);
882 usage_path->depth++;
883 return EOK;
884}
885
886/**
887 *
[57d9c05e]888 * @param usage_path
889 * @return
[b53d3b7]890 */
891void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
892{
893 usb_hid_report_usage_path_t *item;
894
895 if(!list_empty(&usage_path->link)){
896 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
897 list_remove(usage_path->link.prev);
898 usage_path->depth--;
899 free(item);
900 }
901}
902
903/**
904 *
[57d9c05e]905 * @param usage_path
906 * @return
[b53d3b7]907 */
908void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
909{
910 usb_hid_report_usage_path_t *item;
911
912 if(!list_empty(&usage_path->link)){
913 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
914 memset(item, 0, sizeof(usb_hid_report_usage_path_t));
915 }
916}
917
918/**
919 *
[57d9c05e]920 * @param usage_path
921 * @param tag
922 * @param data
923 * @return
[b53d3b7]924 */
925void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
926{
927 usb_hid_report_usage_path_t *item;
928
929 if(!list_empty(&usage_path->link)){
930 item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
931
932 switch(tag) {
933 case USB_HID_TAG_CLASS_GLOBAL:
934 item->usage_page = data;
935 break;
936 case USB_HID_TAG_CLASS_LOCAL:
937 item->usage = data;
938 break;
939 }
940 }
941
942}
943
944/**
[57d9c05e]945 *
[b53d3b7]946 *
[57d9c05e]947 * @param report_path
948 * @param path
949 * @param flags
950 * @return
[b53d3b7]951 */
952int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
953 usb_hid_report_path_t *path,
954 int flags)
955{
956 usb_hid_report_usage_path_t *report_item;
957 usb_hid_report_usage_path_t *path_item;
958
959 link_t *report_link;
960 link_t *path_link;
961
962 int only_page;
963
[bda06a3]964 if(report_path->report_id != path->report_id) {
965 return 1;
966 }
967
[b53d3b7]968 if(path->depth == 0){
969 return EOK;
970 }
971
972
973 if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
974 flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
975 }
976
977 switch(flags){
978 /* path must be completly identical */
979 case USB_HID_PATH_COMPARE_STRICT:
980 if(report_path->depth != path->depth){
981 return 1;
982 }
983
984 report_link = report_path->link.next;
985 path_link = path->link.next;
986
987 while((report_link != &report_path->link) && (path_link != &path->link)) {
988 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
989 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
990
991 if((report_item->usage_page != path_item->usage_page) ||
992 ((only_page == 0) && (report_item->usage != path_item->usage))) {
993 return 1;
994 } else {
995 report_link = report_link->next;
996 path_link = path_link->next;
997 }
998
999 }
1000
1001 if((report_link == &report_path->link) && (path_link == &path->link)) {
1002 return EOK;
1003 }
1004 else {
1005 return 1;
1006 }
1007 break;
1008
[96bfe76]1009 /* compare with only the end of path*/
[b53d3b7]1010 case USB_HID_PATH_COMPARE_END:
1011 report_link = report_path->link.prev;
1012 path_link = path->link.prev;
1013
1014 if(list_empty(&path->link)){
1015 return EOK;
1016 }
1017
1018 while((report_link != &report_path->link) && (path_link != &path->link)) {
1019 report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
1020 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
1021
1022 if((report_item->usage_page != path_item->usage_page) ||
1023 ((only_page == 0) && (report_item->usage != path_item->usage))) {
1024 return 1;
1025 } else {
1026 report_link = report_link->prev;
1027 path_link = path_link->prev;
1028 }
1029
1030 }
1031
1032 if(path_link == &path->link) {
1033 return EOK;
1034 }
1035 else {
1036 return 1;
1037 }
1038
1039 break;
1040
1041 default:
1042 return EINVAL;
1043 }
1044
1045
1046
1047
1048}
1049
1050/**
1051 *
[57d9c05e]1052 * @return
[b53d3b7]1053 */
1054usb_hid_report_path_t *usb_hid_report_path(void)
1055{
1056 usb_hid_report_path_t *path;
1057 path = malloc(sizeof(usb_hid_report_path_t));
1058 if(!path){
1059 return NULL;
1060 }
1061 else {
1062 path->depth = 0;
[bda06a3]1063 path->report_id = 0;
[b53d3b7]1064 list_initialize(&path->link);
1065 return path;
1066 }
1067}
1068
1069/**
1070 *
[57d9c05e]1071 * @param path
1072 * @return void
[b53d3b7]1073 */
1074void usb_hid_report_path_free(usb_hid_report_path_t *path)
1075{
1076 while(!list_empty(&path->link)){
1077 usb_hid_report_remove_last_item(path);
1078 }
1079}
1080
1081
1082/**
[96bfe76]1083 * Clone content of given usage path to the new one
[b53d3b7]1084 *
[57d9c05e]1085 * @param usage_path
1086 * @return
[b53d3b7]1087 */
[96bfe76]1088usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
[b53d3b7]1089{
1090 usb_hid_report_usage_path_t *path_item;
1091 link_t *path_link;
[96bfe76]1092 usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
[b53d3b7]1093
[96bfe76]1094 if(new_usage_path == NULL){
1095 return NULL;
1096 }
[b53d3b7]1097
1098 if(list_empty(&usage_path->link)){
[96bfe76]1099 return new_usage_path;
[b53d3b7]1100 }
1101
1102 path_link = usage_path->link.next;
1103 while(path_link != &usage_path->link) {
1104 path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
1105 usb_hid_report_path_append_item (new_usage_path, path_item->usage_page, path_item->usage);
1106
1107 path_link = path_link->next;
1108 }
1109
[96bfe76]1110 return new_usage_path;
[b53d3b7]1111}
1112
[0bd4810c]1113
[57d9c05e]1114/*** OUTPUT API **/
1115
1116/** Allocates output report buffer
1117 *
1118 * @param parser
1119 * @param size
1120 * @return
1121 */
1122uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size)
1123{
1124 if(parser == NULL) {
1125 *size = 0;
1126 return NULL;
1127 }
1128
[841e6e5]1129 // read the last output report item
[57d9c05e]1130 usb_hid_report_item_t *last;
1131 link_t *link;
1132
1133 link = parser->output.prev;
1134 if(link != &parser->output) {
1135 last = list_get_instance(link, usb_hid_report_item_t, link);
1136 *size = (last->offset + (last->size * last->count)) / 8;
1137
1138 uint8_t *buffer = malloc(sizeof(uint8_t) * (*size));
[841e6e5]1139 memset(buffer, 0, sizeof(uint8_t) * (*size));
1140 usb_log_debug("output buffer: %s\n", usb_debug_str_buffer(buffer, *size, 0));
1141
[57d9c05e]1142 return buffer;
1143 }
1144 else {
1145 *size = 0;
1146 return NULL;
1147 }
1148}
1149
1150
1151/** Frees output report buffer
1152 *
1153 * @param output Output report buffer
1154 * @return
1155 */
1156void usb_hid_report_output_free(uint8_t *output)
[841e6e5]1157
[57d9c05e]1158{
1159 if(output != NULL) {
1160 free (output);
1161 }
1162}
1163
1164/** Returns size of output for given usage path
1165 *
1166 * @param parser
1167 * @param path
1168 * @param flags
1169 * @return
1170 */
1171size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
1172 usb_hid_report_path_t *path, int flags)
1173{
1174 size_t ret = 0;
1175 link_t *item;
1176 usb_hid_report_item_t *report_item;
1177
1178 if(parser == NULL) {
1179 return 0;
1180 }
[bda06a3]1181
[57d9c05e]1182 item = parser->output.next;
[841e6e5]1183 while(&parser->output != item) {
[57d9c05e]1184 report_item = list_get_instance(item, usb_hid_report_item_t, link);
1185 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
1186 (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
1187 ret += report_item->count;
1188 }
1189
1190 item = item->next;
1191 }
1192
1193 return ret;
1194
1195}
1196
1197/** Updates the output report buffer by translated given data
1198 *
1199 * @param parser
1200 * @param path
1201 * @param flags
1202 * @param buffer
1203 * @param size
1204 * @param data
1205 * @param data_size
1206 * @return
1207 */
1208int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
1209 usb_hid_report_path_t *path, int flags,
1210 uint8_t *buffer, size_t size,
1211 int32_t *data, size_t data_size)
1212{
1213 usb_hid_report_item_t *report_item;
1214 link_t *item;
1215 size_t idx=0;
1216 int i=0;
1217 int32_t value=0;
[841e6e5]1218 int offset;
1219 int length;
1220 int32_t tmp_value;
[bda06a3]1221 size_t offset_prefix = 0;
[57d9c05e]1222
1223 if(parser == NULL) {
1224 return EINVAL;
1225 }
1226
[bda06a3]1227 if(parser->use_report_id != 0) {
1228 buffer[0] = path->report_id;
1229 offset_prefix = 8;
1230 }
1231
[70a71e5]1232 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
1233 usb_log_debug("OUTPUT DATA[0]: %d, DATA[1]: %d, DATA[2]: %d\n", data[0], data[1], data[2]);
1234
[57d9c05e]1235 item = parser->output.next;
1236 while(item != &parser->output) {
[841e6e5]1237 report_item = list_get_instance(item, usb_hid_report_item_t, link);
[57d9c05e]1238
[d012590]1239 for(i=0; i<report_item->count; i++) {
[841e6e5]1240
1241 if(idx >= data_size) {
1242 break;
1243 }
1244
1245 if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) ||
1246 ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
[d012590]1247
[841e6e5]1248// // variable item
[70a71e5]1249 value = usb_hid_translate_data_reverse(report_item, data[idx++]);
[bda06a3]1250 offset = report_item->offset + (i * report_item->size) + offset_prefix;
[841e6e5]1251 length = report_item->size;
1252 }
1253 else {
1254 //bitmap
[70a71e5]1255 value += usb_hid_translate_data_reverse(report_item, data[idx++]);
[bda06a3]1256 offset = report_item->offset + offset_prefix;
[841e6e5]1257 length = report_item->size * report_item->count;
1258 }
1259
[d012590]1260 if((offset/8) == ((offset+length-1)/8)) {
[841e6e5]1261 // je to v jednom bytu
[d012590]1262 if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
[841e6e5]1263 break; // TODO ErrorCode
1264 }
1265
[2f4b3a4]1266 size_t shift = offset%8;
[841e6e5]1267
[d012590]1268 value = value << shift;
1269 value = value & (((1 << length)-1) << shift);
[70a71e5]1270
1271 uint8_t mask = 0;
1272 mask = 0xff - (((1 << length) - 1) << shift);
1273 buffer[offset/8] = (buffer[offset/8] & mask) | value;
[841e6e5]1274 }
1275 else {
1276 // je to ve dvou!! FIXME: melo by to umet delsi jak 2
1277
[2f4b3a4]1278 // konec prvniho -- dolni x bitu
[841e6e5]1279 tmp_value = value;
[d012590]1280 tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);
[2f4b3a4]1281 tmp_value = tmp_value << (offset%8);
[d012590]1282
[70a71e5]1283 uint8_t mask = 0;
1284 mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
1285 buffer[offset/8] = (buffer[offset/8] & mask) | tmp_value;
[841e6e5]1286
[2f4b3a4]1287 // a ted druhej -- hornich length-x bitu
1288 value = value >> (8 - (offset % 8));
1289 value = value & ((1 << (length - (8 - (offset % 8)))) - 1);
[d012590]1290
[70a71e5]1291 mask = ((1 << (length - (8 - (offset % 8)))) - 1);
1292 buffer[(offset+length-1)/8] = (buffer[(offset+length-1)/8] & mask) | value;
[841e6e5]1293 }
[57d9c05e]1294
1295 }
[841e6e5]1296
1297 item = item->next;
[57d9c05e]1298 }
1299
[70a71e5]1300 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
[841e6e5]1301
[57d9c05e]1302 return EOK;
1303}
1304
[841e6e5]1305/**
1306 *
1307 * @param item
1308 * @param value
1309 * @return
1310 */
1311int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int value)
1312{
1313 int ret=0;
1314 int resolution;
1315
[70a71e5]1316 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
1317 ret = item->logical_minimum;
1318 }
1319
[d012590]1320 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
[841e6e5]1321
1322 // variable item
1323 if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
1324 item->physical_minimum = item->logical_minimum;
1325 item->physical_maximum = item->logical_maximum;
1326 }
1327
1328 if(item->physical_maximum == item->physical_minimum){
1329 resolution = 1;
1330 }
1331 else {
1332 resolution = (item->logical_maximum - item->logical_minimum) /
1333 ((item->physical_maximum - item->physical_minimum) *
1334 (usb_pow(10,(item->unit_exponent))));
1335 }
1336
1337 ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
1338 }
1339 else {
1340 // bitmapa
1341 if(value == 0) {
1342 ret = 0;
1343 }
1344 else {
1345 size_t bitmap_idx = (value - item->usage_minimum);
1346 ret = 1 << bitmap_idx;
1347 }
1348 }
1349
1350
1351 return ret;
1352}
1353
[57d9c05e]1354
[bda06a3]1355int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id)
1356{
1357 if(path == NULL){
1358 return EINVAL;
1359 }
1360
1361 path->report_id = report_id;
1362 return EOK;
1363}
1364
[976f546]1365/**
1366 * @}
[c7a2e7e]1367 */
Note: See TracBrowser for help on using the repository browser.