1 | /*
|
---|
2 | * Copyright (c) 2011 Matej Klonfar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libusb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * HID report descriptor and report data parser implementation.
|
---|
34 | */
|
---|
35 | #include <usb/classes/hidparser.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <malloc.h>
|
---|
39 | #include <mem.h>
|
---|
40 | #include <usb/debug.h>
|
---|
41 | #include <assert.h>
|
---|
42 |
|
---|
43 | /** The new report item flag. Used to determine when the item is completly
|
---|
44 | * configured and should be added to the report structure
|
---|
45 | */
|
---|
46 | #define USB_HID_NEW_REPORT_ITEM 1
|
---|
47 |
|
---|
48 | /** No special action after the report descriptor tag is processed should be
|
---|
49 | * done
|
---|
50 | */
|
---|
51 | #define USB_HID_NO_ACTION 2
|
---|
52 |
|
---|
53 | #define USB_HID_RESET_OFFSET 3
|
---|
54 |
|
---|
55 | /** Unknown tag was founded in report descriptor data*/
|
---|
56 | #define USB_HID_UNKNOWN_TAG -99
|
---|
57 |
|
---|
58 | usb_hid_report_path_t *usb_hid_report_path_try_insert(usb_hid_report_t *report, usb_hid_report_path_t *cmp_path)
|
---|
59 | {
|
---|
60 | /* find or append current collection path to the list */
|
---|
61 | link_t *path_it = report->collection_paths.next;
|
---|
62 | usb_hid_report_path_t *path = NULL;
|
---|
63 | while(path_it != &report->collection_paths) {
|
---|
64 | path = list_get_instance(path_it, usb_hid_report_path_t, link);
|
---|
65 |
|
---|
66 | if(usb_hid_report_compare_usage_path(path, cmp_path, USB_HID_PATH_COMPARE_STRICT) == EOK){
|
---|
67 | break;
|
---|
68 | }
|
---|
69 | path_it = path_it->next;
|
---|
70 | }
|
---|
71 | if(path_it == &report->collection_paths) {
|
---|
72 | path = usb_hid_report_path_clone(cmp_path);
|
---|
73 | list_append(&path->link, &report->collection_paths);
|
---|
74 | report->collection_paths_count++;
|
---|
75 |
|
---|
76 | return path;
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | return list_get_instance(path_it, usb_hid_report_path_t, link);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Initialize the report descriptor parser structure
|
---|
85 | *
|
---|
86 | * @param parser Report descriptor parser structure
|
---|
87 | * @return Error code
|
---|
88 | */
|
---|
89 | int usb_hid_report_init(usb_hid_report_t *report)
|
---|
90 | {
|
---|
91 | if(report == NULL) {
|
---|
92 | return EINVAL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | memset(report, 0, sizeof(usb_hid_report_t));
|
---|
96 | list_initialize(&report->reports);
|
---|
97 | list_initialize(&report->collection_paths);
|
---|
98 |
|
---|
99 | report->use_report_ids = 0;
|
---|
100 | return EOK;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /*
|
---|
105 | *
|
---|
106 | *
|
---|
107 | */
|
---|
108 | int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item)
|
---|
109 | {
|
---|
110 | usb_hid_report_field_t *field;
|
---|
111 | int i;
|
---|
112 |
|
---|
113 | for(i=0; i<report_item->usages_count; i++){
|
---|
114 | usb_log_debug("usages (%d) - %x\n", i, report_item->usages[i]);
|
---|
115 | }
|
---|
116 |
|
---|
117 | usb_hid_report_path_t *path = report_item->usage_path;
|
---|
118 | for(i=0; i<report_item->count; i++){
|
---|
119 |
|
---|
120 | field = malloc(sizeof(usb_hid_report_field_t));
|
---|
121 | memset(field, 0, sizeof(usb_hid_report_field_t));
|
---|
122 | list_initialize(&field->link);
|
---|
123 |
|
---|
124 | /* fill the attributes */
|
---|
125 | field->logical_minimum = report_item->logical_minimum;
|
---|
126 | field->logical_maximum = report_item->logical_maximum;
|
---|
127 | field->physical_minimum = report_item->physical_minimum;
|
---|
128 | field->physical_maximum = report_item->physical_maximum;
|
---|
129 |
|
---|
130 | field->usage_minimum = report_item->usage_minimum;
|
---|
131 | field->usage_maximum = report_item->usage_maximum;
|
---|
132 | if(report_item->extended_usage_page != 0){
|
---|
133 | field->usage_page = report_item->extended_usage_page;
|
---|
134 | }
|
---|
135 | else {
|
---|
136 | field->usage_page = report_item->usage_page;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if(report_item->usages_count > 0 && ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
|
---|
140 | uint32_t usage;
|
---|
141 | if(i < report_item->usages_count){
|
---|
142 | usage = report_item->usages[i];
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | usage = report_item->usages[report_item->usages_count - 1];
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | if((usage & 0xFFFF0000) != 0){
|
---|
150 | field->usage_page = (usage >> 16);
|
---|
151 | field->usage = (usage & 0xFFFF);
|
---|
152 | }
|
---|
153 | else {
|
---|
154 | field->usage = usage;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | }
|
---|
159 |
|
---|
160 | if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) != 0) && (!((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0)))) {
|
---|
161 | field->usage = report_item->usage_minimum + i;
|
---|
162 | }
|
---|
163 |
|
---|
164 | usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL, field->usage_page);
|
---|
165 | usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL, field->usage);
|
---|
166 |
|
---|
167 | field->collection_path = usb_hid_report_path_try_insert(report, path);
|
---|
168 |
|
---|
169 | field->size = report_item->size;
|
---|
170 |
|
---|
171 | size_t offset_byte = (report_item->offset + (i * report_item->size)) / 8;
|
---|
172 | size_t offset_bit = 8 - ((report_item->offset + (i * report_item->size)) % 8) - report_item->size;
|
---|
173 |
|
---|
174 | field->offset = 8 * offset_byte + offset_bit;
|
---|
175 | if(report_item->id != 0) {
|
---|
176 | field->offset += 8;
|
---|
177 | report->use_report_ids = 1;
|
---|
178 | }
|
---|
179 | field->item_flags = report_item->item_flags;
|
---|
180 |
|
---|
181 | /* find the right report list*/
|
---|
182 | usb_hid_report_description_t *report_des;
|
---|
183 | report_des = usb_hid_report_find_description(report, report_item->id, report_item->type);
|
---|
184 | if(report_des == NULL){
|
---|
185 | report_des = malloc(sizeof(usb_hid_report_description_t));
|
---|
186 | memset(report_des, 0, sizeof(usb_hid_report_description_t));
|
---|
187 |
|
---|
188 | report_des->type = report_item->type;
|
---|
189 | report_des->report_id = report_item->id;
|
---|
190 | list_initialize (&report_des->link);
|
---|
191 | list_initialize (&report_des->report_items);
|
---|
192 |
|
---|
193 | list_append(&report_des->link, &report->reports);
|
---|
194 | report->report_count++;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* append this field to the end of founded report list */
|
---|
198 | list_append (&field->link, &report_des->report_items);
|
---|
199 |
|
---|
200 | /* update the sizes */
|
---|
201 | report_des->bit_length += field->size;
|
---|
202 | report_des->item_length++;
|
---|
203 |
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | return EOK;
|
---|
208 | }
|
---|
209 |
|
---|
210 | usb_hid_report_description_t * usb_hid_report_find_description(const usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
|
---|
211 | {
|
---|
212 | link_t *report_it = report->reports.next;
|
---|
213 | usb_hid_report_description_t *report_des = NULL;
|
---|
214 |
|
---|
215 | while(report_it != &report->reports) {
|
---|
216 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
217 |
|
---|
218 | if((report_des->report_id == report_id) && (report_des->type == type)){
|
---|
219 | return report_des;
|
---|
220 | }
|
---|
221 |
|
---|
222 | report_it = report_it->next;
|
---|
223 | }
|
---|
224 |
|
---|
225 | return NULL;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /** Parse HID report descriptor.
|
---|
229 | *
|
---|
230 | * @param parser Opaque HID report parser structure.
|
---|
231 | * @param data Data describing the report.
|
---|
232 | * @return Error code.
|
---|
233 | */
|
---|
234 | int usb_hid_parse_report_descriptor(usb_hid_report_t *report,
|
---|
235 | const uint8_t *data, size_t size)
|
---|
236 | {
|
---|
237 | size_t i=0;
|
---|
238 | uint8_t tag=0;
|
---|
239 | uint8_t item_size=0;
|
---|
240 | int class=0;
|
---|
241 | int ret;
|
---|
242 | usb_hid_report_item_t *report_item=0;
|
---|
243 | usb_hid_report_item_t *new_report_item;
|
---|
244 | usb_hid_report_path_t *usage_path;
|
---|
245 |
|
---|
246 | size_t offset_input=0;
|
---|
247 | size_t offset_output=0;
|
---|
248 | size_t offset_feature=0;
|
---|
249 |
|
---|
250 | link_t stack;
|
---|
251 | list_initialize(&stack);
|
---|
252 |
|
---|
253 | /* parser structure initialization*/
|
---|
254 | if(usb_hid_report_init(report) != EOK) {
|
---|
255 | return EINVAL;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /*report item initialization*/
|
---|
259 | if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
|
---|
260 | return ENOMEM;
|
---|
261 | }
|
---|
262 | memset(report_item, 0, sizeof(usb_hid_report_item_t));
|
---|
263 | list_initialize(&(report_item->link));
|
---|
264 |
|
---|
265 | /* usage path context initialization */
|
---|
266 | if(!(usage_path=usb_hid_report_path())){
|
---|
267 | return ENOMEM;
|
---|
268 | }
|
---|
269 |
|
---|
270 | while(i<size){
|
---|
271 | if(!USB_HID_ITEM_IS_LONG(data[i])){
|
---|
272 |
|
---|
273 | if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
|
---|
274 | return EINVAL;
|
---|
275 | }
|
---|
276 |
|
---|
277 | tag = USB_HID_ITEM_TAG(data[i]);
|
---|
278 | item_size = USB_HID_ITEM_SIZE(data[i]);
|
---|
279 | class = USB_HID_ITEM_TAG_CLASS(data[i]);
|
---|
280 |
|
---|
281 | ret = usb_hid_report_parse_tag(tag,class,data+i+1,
|
---|
282 | item_size,report_item, usage_path);
|
---|
283 | switch(ret){
|
---|
284 | case USB_HID_NEW_REPORT_ITEM:
|
---|
285 | // store report item to report and create the new one
|
---|
286 | // store current collection path
|
---|
287 | report_item->usage_path = usage_path;
|
---|
288 |
|
---|
289 | usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);
|
---|
290 | if(report_item->id != 0){
|
---|
291 | report->use_report_ids = 1;
|
---|
292 | }
|
---|
293 |
|
---|
294 | switch(tag) {
|
---|
295 | case USB_HID_REPORT_TAG_INPUT:
|
---|
296 | report_item->type = USB_HID_REPORT_TYPE_INPUT;
|
---|
297 | report_item->offset = offset_input;
|
---|
298 | offset_input += report_item->count * report_item->size;
|
---|
299 | break;
|
---|
300 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
301 | report_item->type = USB_HID_REPORT_TYPE_OUTPUT;
|
---|
302 | report_item->offset = offset_output;
|
---|
303 | offset_output += report_item->count * report_item->size;
|
---|
304 |
|
---|
305 | break;
|
---|
306 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
307 | report_item->type = USB_HID_REPORT_TYPE_FEATURE;
|
---|
308 | report_item->offset = offset_feature;
|
---|
309 | offset_feature += report_item->count * report_item->size;
|
---|
310 | break;
|
---|
311 | default:
|
---|
312 | usb_log_debug("\tjump over - tag %X\n", tag);
|
---|
313 | break;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * append new fields to the report
|
---|
318 | * structure
|
---|
319 | */
|
---|
320 | usb_hid_report_append_fields(report, report_item);
|
---|
321 |
|
---|
322 | /* reset local items */
|
---|
323 | usb_hid_report_reset_local_items (report_item);
|
---|
324 |
|
---|
325 | break;
|
---|
326 |
|
---|
327 | case USB_HID_RESET_OFFSET:
|
---|
328 | offset_input = 0;
|
---|
329 | offset_output = 0;
|
---|
330 | offset_feature = 0;
|
---|
331 | usb_hid_report_path_set_report_id (usage_path, report_item->id);
|
---|
332 | break;
|
---|
333 |
|
---|
334 | case USB_HID_REPORT_TAG_PUSH:
|
---|
335 | // push current state to stack
|
---|
336 | new_report_item = usb_hid_report_item_clone(report_item);
|
---|
337 | usb_hid_report_path_t *tmp_path = usb_hid_report_path_clone(usage_path);
|
---|
338 | new_report_item->usage_path = tmp_path;
|
---|
339 |
|
---|
340 | list_prepend (&new_report_item->link, &stack);
|
---|
341 | break;
|
---|
342 | case USB_HID_REPORT_TAG_POP:
|
---|
343 | // restore current state from stack
|
---|
344 | if(list_empty (&stack)) {
|
---|
345 | return EINVAL;
|
---|
346 | }
|
---|
347 | free(report_item);
|
---|
348 |
|
---|
349 | report_item = list_get_instance(stack.next, usb_hid_report_item_t, link);
|
---|
350 |
|
---|
351 | usb_hid_report_usage_path_t *tmp_usage_path;
|
---|
352 | tmp_usage_path = list_get_instance(report_item->usage_path->link.prev, usb_hid_report_usage_path_t, link);
|
---|
353 |
|
---|
354 | usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
|
---|
355 | usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
|
---|
356 |
|
---|
357 | usb_hid_report_path_free(report_item->usage_path);
|
---|
358 | list_initialize(&report_item->usage_path->link);
|
---|
359 | list_remove (stack.next);
|
---|
360 |
|
---|
361 | break;
|
---|
362 |
|
---|
363 | default:
|
---|
364 | // nothing special to do
|
---|
365 | break;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /* jump over the processed block */
|
---|
369 | i += 1 + USB_HID_ITEM_SIZE(data[i]);
|
---|
370 | }
|
---|
371 | else{
|
---|
372 | // TBD
|
---|
373 | i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | }
|
---|
378 |
|
---|
379 | return EOK;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Parse one tag of the report descriptor
|
---|
385 | *
|
---|
386 | * @param Tag to parse
|
---|
387 | * @param Report descriptor buffer
|
---|
388 | * @param Size of data belongs to this tag
|
---|
389 | * @param Current report item structe
|
---|
390 | * @return Code of action to be done next
|
---|
391 | */
|
---|
392 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
---|
393 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
394 | {
|
---|
395 | int ret;
|
---|
396 |
|
---|
397 | switch(class){
|
---|
398 | case USB_HID_TAG_CLASS_MAIN:
|
---|
399 |
|
---|
400 | if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
|
---|
401 | return USB_HID_NEW_REPORT_ITEM;
|
---|
402 | }
|
---|
403 | else {
|
---|
404 | /*TODO process the error */
|
---|
405 | return ret;
|
---|
406 | }
|
---|
407 | break;
|
---|
408 |
|
---|
409 | case USB_HID_TAG_CLASS_GLOBAL:
|
---|
410 | return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
|
---|
411 | break;
|
---|
412 |
|
---|
413 | case USB_HID_TAG_CLASS_LOCAL:
|
---|
414 | return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
|
---|
415 | break;
|
---|
416 | default:
|
---|
417 | return USB_HID_NO_ACTION;
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Parse main tags of report descriptor
|
---|
423 | *
|
---|
424 | * @param Tag identifier
|
---|
425 | * @param Data buffer
|
---|
426 | * @param Length of data buffer
|
---|
427 | * @param Current state table
|
---|
428 | * @return Error code
|
---|
429 | */
|
---|
430 |
|
---|
431 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
432 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
433 | {
|
---|
434 | switch(tag)
|
---|
435 | {
|
---|
436 | case USB_HID_REPORT_TAG_INPUT:
|
---|
437 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
438 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
439 | report_item->item_flags = *data;
|
---|
440 | return EOK;
|
---|
441 | break;
|
---|
442 |
|
---|
443 | case USB_HID_REPORT_TAG_COLLECTION:
|
---|
444 | // TODO usage_path->flags = *data;
|
---|
445 | usb_hid_report_path_append_item(usage_path, report_item->usage_page, report_item->usages[report_item->usages_count-1]);
|
---|
446 | usb_hid_report_reset_local_items (report_item);
|
---|
447 | return USB_HID_NO_ACTION;
|
---|
448 | break;
|
---|
449 |
|
---|
450 | case USB_HID_REPORT_TAG_END_COLLECTION:
|
---|
451 | usb_hid_report_remove_last_item(usage_path);
|
---|
452 | return USB_HID_NO_ACTION;
|
---|
453 | break;
|
---|
454 | default:
|
---|
455 | return USB_HID_NO_ACTION;
|
---|
456 | }
|
---|
457 |
|
---|
458 | return EOK;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Parse global tags of report descriptor
|
---|
463 | *
|
---|
464 | * @param Tag identifier
|
---|
465 | * @param Data buffer
|
---|
466 | * @param Length of data buffer
|
---|
467 | * @param Current state table
|
---|
468 | * @return Error code
|
---|
469 | */
|
---|
470 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
471 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
472 | {
|
---|
473 | // TODO take care about the bit length of data
|
---|
474 | switch(tag)
|
---|
475 | {
|
---|
476 | case USB_HID_REPORT_TAG_USAGE_PAGE:
|
---|
477 | report_item->usage_page = usb_hid_report_tag_data_uint32(data, item_size);
|
---|
478 | break;
|
---|
479 | case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
|
---|
480 | report_item->logical_minimum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
|
---|
481 | break;
|
---|
482 | case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
|
---|
483 | report_item->logical_maximum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
|
---|
484 | break;
|
---|
485 | case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
|
---|
486 | report_item->physical_minimum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
|
---|
487 | break;
|
---|
488 | case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
|
---|
489 | report_item->physical_maximum = USB_HID_UINT32_TO_INT32(usb_hid_report_tag_data_uint32(data,item_size), item_size * 8);
|
---|
490 |
|
---|
491 | break;
|
---|
492 | case USB_HID_REPORT_TAG_UNIT_EXPONENT:
|
---|
493 | report_item->unit_exponent = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
494 | break;
|
---|
495 | case USB_HID_REPORT_TAG_UNIT:
|
---|
496 | report_item->unit = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
497 | break;
|
---|
498 | case USB_HID_REPORT_TAG_REPORT_SIZE:
|
---|
499 | report_item->size = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
500 | break;
|
---|
501 | case USB_HID_REPORT_TAG_REPORT_COUNT:
|
---|
502 | report_item->count = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
503 | break;
|
---|
504 | case USB_HID_REPORT_TAG_REPORT_ID:
|
---|
505 | report_item->id = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
506 | return USB_HID_RESET_OFFSET;
|
---|
507 | break;
|
---|
508 | case USB_HID_REPORT_TAG_PUSH:
|
---|
509 | case USB_HID_REPORT_TAG_POP:
|
---|
510 | /*
|
---|
511 | * stack operations are done in top level parsing
|
---|
512 | * function
|
---|
513 | */
|
---|
514 | return tag;
|
---|
515 | break;
|
---|
516 |
|
---|
517 | default:
|
---|
518 | return USB_HID_NO_ACTION;
|
---|
519 | }
|
---|
520 |
|
---|
521 | return EOK;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * Parse local tags of report descriptor
|
---|
526 | *
|
---|
527 | * @param Tag identifier
|
---|
528 | * @param Data buffer
|
---|
529 | * @param Length of data buffer
|
---|
530 | * @param Current state table
|
---|
531 | * @return Error code
|
---|
532 | */
|
---|
533 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
534 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
535 | {
|
---|
536 | switch(tag)
|
---|
537 | {
|
---|
538 | case USB_HID_REPORT_TAG_USAGE:
|
---|
539 | report_item->usages[report_item->usages_count] = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
540 | report_item->usages_count++;
|
---|
541 | break;
|
---|
542 | case USB_HID_REPORT_TAG_USAGE_MINIMUM:
|
---|
543 | if (item_size == 3) {
|
---|
544 | // usage extended usages
|
---|
545 | report_item->extended_usage_page = (usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16;
|
---|
546 | report_item->usage_minimum = usb_hid_report_tag_data_uint32(data,item_size) & 0xFF;
|
---|
547 | }
|
---|
548 | else {
|
---|
549 | report_item->usage_minimum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
550 | }
|
---|
551 | break;
|
---|
552 | case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
|
---|
553 | if (item_size == 3) {
|
---|
554 | // usage extended usages
|
---|
555 | report_item->extended_usage_page = (usb_hid_report_tag_data_uint32(data,item_size) & 0xFF00) >> 16;
|
---|
556 | report_item->usage_maximum = usb_hid_report_tag_data_uint32(data,item_size) & 0xFF;
|
---|
557 | }
|
---|
558 | else {
|
---|
559 | report_item->usage_maximum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
560 | }
|
---|
561 | break;
|
---|
562 | case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
|
---|
563 | report_item->designator_index = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
564 | break;
|
---|
565 | case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
|
---|
566 | report_item->designator_minimum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
567 | break;
|
---|
568 | case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
|
---|
569 | report_item->designator_maximum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
570 | break;
|
---|
571 | case USB_HID_REPORT_TAG_STRING_INDEX:
|
---|
572 | report_item->string_index = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
573 | break;
|
---|
574 | case USB_HID_REPORT_TAG_STRING_MINIMUM:
|
---|
575 | report_item->string_minimum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
576 | break;
|
---|
577 | case USB_HID_REPORT_TAG_STRING_MAXIMUM:
|
---|
578 | report_item->string_maximum = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
579 | break;
|
---|
580 | case USB_HID_REPORT_TAG_DELIMITER:
|
---|
581 | //report_item->delimiter = usb_hid_report_tag_data_uint32(data,item_size);
|
---|
582 | //TODO:
|
---|
583 | // DELIMITER STUFF
|
---|
584 | break;
|
---|
585 |
|
---|
586 | default:
|
---|
587 | return USB_HID_NO_ACTION;
|
---|
588 | }
|
---|
589 |
|
---|
590 | return EOK;
|
---|
591 | }
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Converts raw data to uint32 (thats the maximum length of short item data)
|
---|
595 | *
|
---|
596 | * @param Data buffer
|
---|
597 | * @param Size of buffer
|
---|
598 | * @return Converted int32 number
|
---|
599 | */
|
---|
600 | uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size)
|
---|
601 | {
|
---|
602 | unsigned int i;
|
---|
603 | uint32_t result;
|
---|
604 |
|
---|
605 | result = 0;
|
---|
606 | for(i=0; i<size; i++) {
|
---|
607 | result = (result | (data[i]) << (i*8));
|
---|
608 | }
|
---|
609 |
|
---|
610 | return result;
|
---|
611 | }
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Prints content of given list of report items.
|
---|
615 | *
|
---|
616 | * @param List of report items (usb_hid_report_item_t)
|
---|
617 | * @return void
|
---|
618 | */
|
---|
619 | void usb_hid_descriptor_print_list(link_t *head)
|
---|
620 | {
|
---|
621 | usb_hid_report_field_t *report_item;
|
---|
622 | link_t *item;
|
---|
623 |
|
---|
624 |
|
---|
625 | if(head == NULL || list_empty(head)) {
|
---|
626 | usb_log_debug("\tempty\n");
|
---|
627 | return;
|
---|
628 | }
|
---|
629 |
|
---|
630 | for(item = head->next; item != head; item = item->next) {
|
---|
631 |
|
---|
632 | report_item = list_get_instance(item, usb_hid_report_field_t, link);
|
---|
633 |
|
---|
634 | usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
|
---|
635 | usb_log_debug("\t\tSIZE: %X\n", report_item->size);
|
---|
636 | usb_log_debug("\t\tLOGMIN: %d\n", report_item->logical_minimum);
|
---|
637 | usb_log_debug("\t\tLOGMAX: %d\n", report_item->logical_maximum);
|
---|
638 | usb_log_debug("\t\tPHYMIN: %d\n", report_item->physical_minimum);
|
---|
639 | usb_log_debug("\t\tPHYMAX: %d\n", report_item->physical_maximum);
|
---|
640 | usb_log_debug("\t\ttUSAGEMIN: %X\n", report_item->usage_minimum);
|
---|
641 | usb_log_debug("\t\tUSAGEMAX: %X\n", report_item->usage_maximum);
|
---|
642 |
|
---|
643 | usb_log_debug("\t\tVALUE: %X\n", report_item->value);
|
---|
644 | usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage);
|
---|
645 | usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page);
|
---|
646 |
|
---|
647 | //usb_hid_print_usage_path(report_item->collection_path);
|
---|
648 |
|
---|
649 | usb_log_debug("\n");
|
---|
650 |
|
---|
651 | }
|
---|
652 |
|
---|
653 |
|
---|
654 | }
|
---|
655 | /**
|
---|
656 | * Prints content of given report descriptor in human readable format.
|
---|
657 | *
|
---|
658 | * @param parser Parsed descriptor to print
|
---|
659 | * @return void
|
---|
660 | */
|
---|
661 | void usb_hid_descriptor_print(usb_hid_report_t *report)
|
---|
662 | {
|
---|
663 | if(report == NULL) {
|
---|
664 | return;
|
---|
665 | }
|
---|
666 |
|
---|
667 | link_t *report_it = report->reports.next;
|
---|
668 | usb_hid_report_description_t *report_des;
|
---|
669 |
|
---|
670 | while(report_it != &report->reports) {
|
---|
671 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
672 | usb_log_debug("Report ID: %d\n", report_des->report_id);
|
---|
673 | usb_log_debug("\tType: %d\n", report_des->type);
|
---|
674 | usb_log_debug("\tLength: %d\n", report_des->bit_length);
|
---|
675 | usb_log_debug("\tItems: %d\n", report_des->item_length);
|
---|
676 |
|
---|
677 | usb_hid_descriptor_print_list(&report_des->report_items);
|
---|
678 |
|
---|
679 |
|
---|
680 | link_t *path_it = report->collection_paths.next;
|
---|
681 | while(path_it != &report->collection_paths) {
|
---|
682 | usb_hid_print_usage_path (list_get_instance(path_it, usb_hid_report_path_t, link));
|
---|
683 | path_it = path_it->next;
|
---|
684 | }
|
---|
685 |
|
---|
686 | report_it = report_it->next;
|
---|
687 | }
|
---|
688 | }
|
---|
689 |
|
---|
690 | /**
|
---|
691 | * Releases whole linked list of report items
|
---|
692 | *
|
---|
693 | * @param head Head of list of report descriptor items (usb_hid_report_item_t)
|
---|
694 | * @return void
|
---|
695 | */
|
---|
696 | void usb_hid_free_report_list(link_t *head)
|
---|
697 | {
|
---|
698 | return;
|
---|
699 |
|
---|
700 | usb_hid_report_item_t *report_item;
|
---|
701 | link_t *next;
|
---|
702 |
|
---|
703 | if(head == NULL || list_empty(head)) {
|
---|
704 | return;
|
---|
705 | }
|
---|
706 |
|
---|
707 | next = head->next;
|
---|
708 | while(next != head) {
|
---|
709 |
|
---|
710 | report_item = list_get_instance(next, usb_hid_report_item_t, link);
|
---|
711 |
|
---|
712 | while(!list_empty(&report_item->usage_path->link)) {
|
---|
713 | usb_hid_report_remove_last_item(report_item->usage_path);
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | next = next->next;
|
---|
718 |
|
---|
719 | free(report_item);
|
---|
720 | }
|
---|
721 |
|
---|
722 | return;
|
---|
723 |
|
---|
724 | }
|
---|
725 |
|
---|
726 | /** Frees the HID report descriptor parser structure
|
---|
727 | *
|
---|
728 | * @param parser Opaque HID report parser structure
|
---|
729 | * @return void
|
---|
730 | */
|
---|
731 | void usb_hid_free_report(usb_hid_report_t *report)
|
---|
732 | {
|
---|
733 | if(report == NULL){
|
---|
734 | return;
|
---|
735 | }
|
---|
736 |
|
---|
737 | // free collection paths
|
---|
738 | usb_hid_report_path_t *path;
|
---|
739 | while(!list_empty(&report->collection_paths)) {
|
---|
740 | path = list_get_instance(report->collection_paths.next, usb_hid_report_path_t, link);
|
---|
741 | usb_hid_report_path_free(path);
|
---|
742 | }
|
---|
743 |
|
---|
744 | // free report items
|
---|
745 | usb_hid_report_description_t *report_des;
|
---|
746 | usb_hid_report_field_t *field;
|
---|
747 | while(!list_empty(&report->reports)) {
|
---|
748 | report_des = list_get_instance(report->reports.next, usb_hid_report_description_t, link);
|
---|
749 | list_remove(&report_des->link);
|
---|
750 |
|
---|
751 | while(!list_empty(&report_des->report_items)) {
|
---|
752 | field = list_get_instance(report_des->report_items.next, usb_hid_report_field_t, link);
|
---|
753 | list_remove(&field->link);
|
---|
754 |
|
---|
755 | free(field);
|
---|
756 | }
|
---|
757 |
|
---|
758 | free(report_des);
|
---|
759 | }
|
---|
760 |
|
---|
761 | return;
|
---|
762 | }
|
---|
763 |
|
---|
764 | /**
|
---|
765 | * @}
|
---|
766 | */
|
---|