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 |
|
---|
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 | /*
|
---|
59 | * Private descriptor parser functions
|
---|
60 | */
|
---|
61 | int usb_hid_report_init(usb_hid_report_t *report);
|
---|
62 | int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item);
|
---|
63 | 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);
|
---|
64 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
---|
65 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
---|
66 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
67 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
---|
68 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
69 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
---|
70 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
71 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
---|
72 |
|
---|
73 | void usb_hid_print_usage_path(usb_hid_report_path_t *path);
|
---|
74 | void usb_hid_descriptor_print_list(link_t *head);
|
---|
75 | int usb_hid_report_reset_local_items();
|
---|
76 | void usb_hid_free_report_list(link_t *head);
|
---|
77 | usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item);
|
---|
78 | /*
|
---|
79 | * Data translation private functions
|
---|
80 | */
|
---|
81 | int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
|
---|
82 | inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
|
---|
83 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
|
---|
84 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value);
|
---|
85 | int usb_pow(int a, int b);
|
---|
86 |
|
---|
87 | // TODO: tohle ma bejt asi jinde
|
---|
88 | int usb_pow(int a, int b)
|
---|
89 | {
|
---|
90 | switch(b) {
|
---|
91 | case 0:
|
---|
92 | return 1;
|
---|
93 | break;
|
---|
94 | case 1:
|
---|
95 | return a;
|
---|
96 | break;
|
---|
97 | default:
|
---|
98 | return a * usb_pow(a, b-1);
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Initialize the report descriptor parser structure
|
---|
105 | *
|
---|
106 | * @param parser Report descriptor parser structure
|
---|
107 | * @return Error code
|
---|
108 | */
|
---|
109 | int usb_hid_report_init(usb_hid_report_t *report)
|
---|
110 | {
|
---|
111 | if(report == NULL) {
|
---|
112 | return EINVAL;
|
---|
113 | }
|
---|
114 |
|
---|
115 | memset(report, 0, sizeof(usb_hid_report_t));
|
---|
116 | list_initialize(&report->reports);
|
---|
117 | list_initialize(&report->collection_paths);
|
---|
118 |
|
---|
119 | report->use_report_ids = 0;
|
---|
120 | return EOK;
|
---|
121 | }
|
---|
122 |
|
---|
123 | int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item)
|
---|
124 | {
|
---|
125 | usb_hid_report_field_t *field;
|
---|
126 | int i;
|
---|
127 |
|
---|
128 |
|
---|
129 | /* find or append current collection path to the list */
|
---|
130 | link_t *path_it = report->collection_paths.next;
|
---|
131 | usb_hid_report_path_t *path = NULL;
|
---|
132 | while(path_it != &report->collection_paths) {
|
---|
133 | path = list_get_instance(path_it, usb_hid_report_path_t, link);
|
---|
134 |
|
---|
135 | if(usb_hid_report_compare_usage_path(path, report_item->usage_path, USB_HID_PATH_COMPARE_STRICT) == EOK){
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | path_it = path_it->next;
|
---|
139 | }
|
---|
140 | if(path_it == &report->collection_paths) {
|
---|
141 | path = usb_hid_report_path_clone(report_item->usage_path);
|
---|
142 | list_append(&path->link, &report->collection_paths);
|
---|
143 | report->collection_paths_count++;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | for(i=0; i<report_item->count; i++){
|
---|
148 |
|
---|
149 | field = malloc(sizeof(usb_hid_report_field_t));
|
---|
150 | memset(field, 0, sizeof(usb_hid_report_field_t));
|
---|
151 | list_initialize(&field->link);
|
---|
152 |
|
---|
153 | /* fill the attributes */
|
---|
154 | field->collection_path = path;
|
---|
155 | field->logical_minimum = report_item->logical_minimum;
|
---|
156 | field->logical_maximum = report_item->logical_maximum;
|
---|
157 | field->physical_minimum = report_item->physical_minimum;
|
---|
158 | field->physical_maximum = report_item->physical_maximum;
|
---|
159 |
|
---|
160 | field->usage_minimum = report_item->usage_minimum;
|
---|
161 | field->usage_maximum = report_item->usage_maximum;
|
---|
162 | if(report_item->extended_usage_page != 0){
|
---|
163 | field->usage_page = report_item->extended_usage_page;
|
---|
164 | }
|
---|
165 | else {
|
---|
166 | field->usage_page = report_item->usage_page;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if(report_item->usages_count > 0 && ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
|
---|
170 | if(i < report_item->usages_count){
|
---|
171 | if((report_item->usages[i] & 0xFF00) != 0){
|
---|
172 | field->usage_page = (report_item->usages[i] >> 16);
|
---|
173 | field->usage = (report_item->usages[i] & 0xFF);
|
---|
174 | }
|
---|
175 | else {
|
---|
176 | field->usage = report_item->usages[i];
|
---|
177 | }
|
---|
178 | }
|
---|
179 | else {
|
---|
180 | field->usage = report_item->usages[report_item->usages_count - 1];
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) != 0) && (!((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0)))) {
|
---|
185 | if(report_item->type == USB_HID_REPORT_TYPE_INPUT) {
|
---|
186 | field->usage = report_item->usage_maximum - i;
|
---|
187 | }
|
---|
188 | else {
|
---|
189 | field->usage = report_item->usage_minimum + i;
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|
193 |
|
---|
194 | field->size = report_item->size;
|
---|
195 | field->offset = report_item->offset + (i * report_item->size);
|
---|
196 | if(report_item->id != 0) {
|
---|
197 | field->offset += 8;
|
---|
198 | report->use_report_ids = 1;
|
---|
199 | }
|
---|
200 | field->item_flags = report_item->item_flags;
|
---|
201 |
|
---|
202 | /* find the right report list*/
|
---|
203 | usb_hid_report_description_t *report_des;
|
---|
204 | report_des = usb_hid_report_find_description(report, report_item->id, report_item->type);
|
---|
205 | if(report_des == NULL){
|
---|
206 | report_des = malloc(sizeof(usb_hid_report_description_t));
|
---|
207 | memset(report_des, 0, sizeof(usb_hid_report_description_t));
|
---|
208 |
|
---|
209 | report_des->type = report_item->type;
|
---|
210 | report_des->report_id = report_item->id;
|
---|
211 | list_initialize (&report_des->link);
|
---|
212 | list_initialize (&report_des->report_items);
|
---|
213 |
|
---|
214 | list_append(&report_des->link, &report->reports);
|
---|
215 | report->report_count++;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /* append this field to the end of founded report list */
|
---|
219 | list_append (&field->link, &report_des->report_items);
|
---|
220 |
|
---|
221 | /* update the sizes */
|
---|
222 | report_des->bit_length += field->size;
|
---|
223 | report_des->item_length++;
|
---|
224 |
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | return EOK;
|
---|
229 | }
|
---|
230 |
|
---|
231 | 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)
|
---|
232 | {
|
---|
233 | link_t *report_it = report->reports.next;
|
---|
234 | usb_hid_report_description_t *report_des = NULL;
|
---|
235 |
|
---|
236 | while(report_it != &report->reports) {
|
---|
237 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
238 |
|
---|
239 | if((report_des->report_id == report_id) && (report_des->type == type)){
|
---|
240 | return report_des;
|
---|
241 | }
|
---|
242 |
|
---|
243 | report_it = report_it->next;
|
---|
244 | }
|
---|
245 |
|
---|
246 | return NULL;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** Parse HID report descriptor.
|
---|
250 | *
|
---|
251 | * @param parser Opaque HID report parser structure.
|
---|
252 | * @param data Data describing the report.
|
---|
253 | * @return Error code.
|
---|
254 | */
|
---|
255 | int usb_hid_parse_report_descriptor(usb_hid_report_t *report,
|
---|
256 | const uint8_t *data, size_t size)
|
---|
257 | {
|
---|
258 | size_t i=0;
|
---|
259 | uint8_t tag=0;
|
---|
260 | uint8_t item_size=0;
|
---|
261 | int class=0;
|
---|
262 | int ret;
|
---|
263 | usb_hid_report_item_t *report_item=0;
|
---|
264 | usb_hid_report_item_t *new_report_item;
|
---|
265 | usb_hid_report_path_t *usage_path;
|
---|
266 |
|
---|
267 | size_t offset_input=0;
|
---|
268 | size_t offset_output=0;
|
---|
269 | size_t offset_feature=0;
|
---|
270 |
|
---|
271 | link_t stack;
|
---|
272 | list_initialize(&stack);
|
---|
273 |
|
---|
274 | /* parser structure initialization*/
|
---|
275 | if(usb_hid_report_init(report) != EOK) {
|
---|
276 | return EINVAL;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /*report item initialization*/
|
---|
280 | if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
|
---|
281 | return ENOMEM;
|
---|
282 | }
|
---|
283 | memset(report_item, 0, sizeof(usb_hid_report_item_t));
|
---|
284 | list_initialize(&(report_item->link));
|
---|
285 |
|
---|
286 | /* usage path context initialization */
|
---|
287 | if(!(usage_path=usb_hid_report_path())){
|
---|
288 | return ENOMEM;
|
---|
289 | }
|
---|
290 |
|
---|
291 | while(i<size){
|
---|
292 | if(!USB_HID_ITEM_IS_LONG(data[i])){
|
---|
293 |
|
---|
294 | if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
|
---|
295 | return EINVAL;
|
---|
296 | }
|
---|
297 |
|
---|
298 | tag = USB_HID_ITEM_TAG(data[i]);
|
---|
299 | item_size = USB_HID_ITEM_SIZE(data[i]);
|
---|
300 | class = USB_HID_ITEM_TAG_CLASS(data[i]);
|
---|
301 |
|
---|
302 | ret = usb_hid_report_parse_tag(tag,class,data+i+1,
|
---|
303 | item_size,report_item, usage_path);
|
---|
304 | switch(ret){
|
---|
305 | case USB_HID_NEW_REPORT_ITEM:
|
---|
306 | // store report item to report and create the new one
|
---|
307 | // store current collection path
|
---|
308 | report_item->usage_path = usage_path;
|
---|
309 |
|
---|
310 | usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);
|
---|
311 | if(report_item->id != 0){
|
---|
312 | report->use_report_ids = 1;
|
---|
313 | }
|
---|
314 |
|
---|
315 | switch(tag) {
|
---|
316 | case USB_HID_REPORT_TAG_INPUT:
|
---|
317 | report_item->type = USB_HID_REPORT_TYPE_INPUT;
|
---|
318 | report_item->offset = offset_input;
|
---|
319 | offset_input += report_item->count * report_item->size;
|
---|
320 | break;
|
---|
321 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
322 | report_item->type = USB_HID_REPORT_TYPE_OUTPUT;
|
---|
323 | report_item->offset = offset_output;
|
---|
324 | offset_output += report_item->count * report_item->size;
|
---|
325 |
|
---|
326 | break;
|
---|
327 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
328 | report_item->type = USB_HID_REPORT_TYPE_FEATURE;
|
---|
329 | report_item->offset = offset_feature;
|
---|
330 | offset_feature += report_item->count * report_item->size;
|
---|
331 | break;
|
---|
332 | default:
|
---|
333 | usb_log_debug("\tjump over - tag %X\n", tag);
|
---|
334 | break;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /*
|
---|
338 | * append new fields to the report
|
---|
339 | * structure
|
---|
340 | */
|
---|
341 | usb_hid_report_append_fields(report, report_item);
|
---|
342 |
|
---|
343 | /* reset local items */
|
---|
344 | while(report_item->usages_count > 0){
|
---|
345 | report_item->usages[--(report_item->usages_count)] = 0;
|
---|
346 | }
|
---|
347 |
|
---|
348 | report_item->extended_usage_page = 0;
|
---|
349 | report_item->usage_minimum = 0;
|
---|
350 | report_item->usage_maximum = 0;
|
---|
351 | report_item->designator_index = 0;
|
---|
352 | report_item->designator_minimum = 0;
|
---|
353 | report_item->designator_maximum = 0;
|
---|
354 | report_item->string_index = 0;
|
---|
355 | report_item->string_minimum = 0;
|
---|
356 | report_item->string_maximum = 0;
|
---|
357 |
|
---|
358 | break;
|
---|
359 |
|
---|
360 | case USB_HID_RESET_OFFSET:
|
---|
361 | offset_input = 0;
|
---|
362 | offset_output = 0;
|
---|
363 | offset_feature = 0;
|
---|
364 | break;
|
---|
365 |
|
---|
366 | case USB_HID_REPORT_TAG_PUSH:
|
---|
367 | // push current state to stack
|
---|
368 | new_report_item = usb_hid_report_item_clone(report_item);
|
---|
369 | usb_hid_report_path_t *tmp_path = usb_hid_report_path_clone(usage_path);
|
---|
370 | new_report_item->usage_path = tmp_path;
|
---|
371 |
|
---|
372 | list_prepend (&new_report_item->link, &stack);
|
---|
373 | break;
|
---|
374 | case USB_HID_REPORT_TAG_POP:
|
---|
375 | // restore current state from stack
|
---|
376 | if(list_empty (&stack)) {
|
---|
377 | return EINVAL;
|
---|
378 | }
|
---|
379 | free(report_item);
|
---|
380 |
|
---|
381 | report_item = list_get_instance(stack.next, usb_hid_report_item_t, link);
|
---|
382 |
|
---|
383 | usb_hid_report_usage_path_t *tmp_usage_path;
|
---|
384 | tmp_usage_path = list_get_instance(report_item->usage_path->link.prev, usb_hid_report_usage_path_t, link);
|
---|
385 |
|
---|
386 | usb_hid_report_set_last_item(usage_path, tmp_usage_path->usage_page, tmp_usage_path->usage);
|
---|
387 |
|
---|
388 | usb_hid_report_path_free(report_item->usage_path);
|
---|
389 | list_initialize(&report_item->usage_path->link);
|
---|
390 | list_remove (stack.next);
|
---|
391 |
|
---|
392 | break;
|
---|
393 |
|
---|
394 | default:
|
---|
395 | // nothing special to do
|
---|
396 | break;
|
---|
397 | }
|
---|
398 |
|
---|
399 | /* jump over the processed block */
|
---|
400 | i += 1 + USB_HID_ITEM_SIZE(data[i]);
|
---|
401 | }
|
---|
402 | else{
|
---|
403 | // TBD
|
---|
404 | i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | }
|
---|
409 |
|
---|
410 | return EOK;
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Parse one tag of the report descriptor
|
---|
416 | *
|
---|
417 | * @param Tag to parse
|
---|
418 | * @param Report descriptor buffer
|
---|
419 | * @param Size of data belongs to this tag
|
---|
420 | * @param Current report item structe
|
---|
421 | * @return Code of action to be done next
|
---|
422 | */
|
---|
423 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
---|
424 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
425 | {
|
---|
426 | int ret;
|
---|
427 |
|
---|
428 | switch(class){
|
---|
429 | case USB_HID_TAG_CLASS_MAIN:
|
---|
430 |
|
---|
431 | if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
|
---|
432 | return USB_HID_NEW_REPORT_ITEM;
|
---|
433 | }
|
---|
434 | else {
|
---|
435 | /*TODO process the error */
|
---|
436 | return ret;
|
---|
437 | }
|
---|
438 | break;
|
---|
439 |
|
---|
440 | case USB_HID_TAG_CLASS_GLOBAL:
|
---|
441 | return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
|
---|
442 | break;
|
---|
443 |
|
---|
444 | case USB_HID_TAG_CLASS_LOCAL:
|
---|
445 | return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
|
---|
446 | break;
|
---|
447 | default:
|
---|
448 | return USB_HID_NO_ACTION;
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * Parse main tags of report descriptor
|
---|
454 | *
|
---|
455 | * @param Tag identifier
|
---|
456 | * @param Data buffer
|
---|
457 | * @param Length of data buffer
|
---|
458 | * @param Current state table
|
---|
459 | * @return Error code
|
---|
460 | */
|
---|
461 |
|
---|
462 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
463 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
464 | {
|
---|
465 | switch(tag)
|
---|
466 | {
|
---|
467 | case USB_HID_REPORT_TAG_INPUT:
|
---|
468 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
469 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
470 | report_item->item_flags = *data;
|
---|
471 | return EOK;
|
---|
472 | break;
|
---|
473 |
|
---|
474 | case USB_HID_REPORT_TAG_COLLECTION:
|
---|
475 | // TODO usage_path->flags = *data;
|
---|
476 | usb_hid_report_path_append_item(usage_path, report_item->usage_page, report_item->usages[report_item->usages_count-1]);
|
---|
477 | return USB_HID_NO_ACTION;
|
---|
478 | break;
|
---|
479 |
|
---|
480 | case USB_HID_REPORT_TAG_END_COLLECTION:
|
---|
481 | usb_hid_report_remove_last_item(usage_path);
|
---|
482 | return USB_HID_NO_ACTION;
|
---|
483 | break;
|
---|
484 | default:
|
---|
485 | return USB_HID_NO_ACTION;
|
---|
486 | }
|
---|
487 |
|
---|
488 | return EOK;
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Parse global tags of report descriptor
|
---|
493 | *
|
---|
494 | * @param Tag identifier
|
---|
495 | * @param Data buffer
|
---|
496 | * @param Length of data buffer
|
---|
497 | * @param Current state table
|
---|
498 | * @return Error code
|
---|
499 | */
|
---|
500 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
501 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
502 | {
|
---|
503 | // TODO take care about the bit length of data
|
---|
504 | switch(tag)
|
---|
505 | {
|
---|
506 | case USB_HID_REPORT_TAG_USAGE_PAGE:
|
---|
507 | report_item->usage_page = usb_hid_report_tag_data_int32(data, item_size);
|
---|
508 | break;
|
---|
509 | case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
|
---|
510 | report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
511 | break;
|
---|
512 | case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
|
---|
513 | report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
514 | break;
|
---|
515 | case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
|
---|
516 | report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
517 | break;
|
---|
518 | case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
|
---|
519 | report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
520 | break;
|
---|
521 | case USB_HID_REPORT_TAG_UNIT_EXPONENT:
|
---|
522 | report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
|
---|
523 | break;
|
---|
524 | case USB_HID_REPORT_TAG_UNIT:
|
---|
525 | report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
|
---|
526 | break;
|
---|
527 | case USB_HID_REPORT_TAG_REPORT_SIZE:
|
---|
528 | report_item->size = usb_hid_report_tag_data_int32(data,item_size);
|
---|
529 | break;
|
---|
530 | case USB_HID_REPORT_TAG_REPORT_COUNT:
|
---|
531 | report_item->count = usb_hid_report_tag_data_int32(data,item_size);
|
---|
532 | break;
|
---|
533 | case USB_HID_REPORT_TAG_REPORT_ID:
|
---|
534 | report_item->id = usb_hid_report_tag_data_int32(data,item_size);
|
---|
535 | return USB_HID_RESET_OFFSET;
|
---|
536 | break;
|
---|
537 | case USB_HID_REPORT_TAG_PUSH:
|
---|
538 | case USB_HID_REPORT_TAG_POP:
|
---|
539 | /*
|
---|
540 | * stack operations are done in top level parsing
|
---|
541 | * function
|
---|
542 | */
|
---|
543 | return tag;
|
---|
544 | break;
|
---|
545 |
|
---|
546 | default:
|
---|
547 | return USB_HID_NO_ACTION;
|
---|
548 | }
|
---|
549 |
|
---|
550 | return EOK;
|
---|
551 | }
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * Parse local tags of report descriptor
|
---|
555 | *
|
---|
556 | * @param Tag identifier
|
---|
557 | * @param Data buffer
|
---|
558 | * @param Length of data buffer
|
---|
559 | * @param Current state table
|
---|
560 | * @return Error code
|
---|
561 | */
|
---|
562 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
563 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
564 | {
|
---|
565 | switch(tag)
|
---|
566 | {
|
---|
567 | case USB_HID_REPORT_TAG_USAGE:
|
---|
568 | report_item->usages[report_item->usages_count++] = usb_hid_report_tag_data_int32(data,item_size);
|
---|
569 | break;
|
---|
570 | case USB_HID_REPORT_TAG_USAGE_MINIMUM:
|
---|
571 | if (item_size == 3) {
|
---|
572 | // usage extended usages
|
---|
573 | report_item->extended_usage_page = (usb_hid_report_tag_data_int32(data,item_size) & 0xFF00) >> 16;
|
---|
574 | report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size) & 0xFF;
|
---|
575 | }
|
---|
576 | else {
|
---|
577 | report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
578 | }
|
---|
579 | break;
|
---|
580 | case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
|
---|
581 | if (item_size == 3) {
|
---|
582 | // usage extended usages
|
---|
583 | report_item->extended_usage_page = (usb_hid_report_tag_data_int32(data,item_size) & 0xFF00) >> 16;
|
---|
584 | report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size) & 0xFF;
|
---|
585 | }
|
---|
586 | else {
|
---|
587 | report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
588 | }
|
---|
589 | break;
|
---|
590 | case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
|
---|
591 | report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
|
---|
592 | break;
|
---|
593 | case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
|
---|
594 | report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
595 | break;
|
---|
596 | case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
|
---|
597 | report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
598 | break;
|
---|
599 | case USB_HID_REPORT_TAG_STRING_INDEX:
|
---|
600 | report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
|
---|
601 | break;
|
---|
602 | case USB_HID_REPORT_TAG_STRING_MINIMUM:
|
---|
603 | report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
604 | break;
|
---|
605 | case USB_HID_REPORT_TAG_STRING_MAXIMUM:
|
---|
606 | report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
---|
607 | break;
|
---|
608 | case USB_HID_REPORT_TAG_DELIMITER:
|
---|
609 | //report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
|
---|
610 | //TODO:
|
---|
611 | // DELIMITER STUFF
|
---|
612 | break;
|
---|
613 |
|
---|
614 | default:
|
---|
615 | return USB_HID_NO_ACTION;
|
---|
616 | }
|
---|
617 |
|
---|
618 | return EOK;
|
---|
619 | }
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * Converts raw data to int32 (thats the maximum length of short item data)
|
---|
623 | *
|
---|
624 | * @param Data buffer
|
---|
625 | * @param Size of buffer
|
---|
626 | * @return Converted int32 number
|
---|
627 | */
|
---|
628 | int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
|
---|
629 | {
|
---|
630 | unsigned int i;
|
---|
631 | int32_t result;
|
---|
632 |
|
---|
633 | result = 0;
|
---|
634 | for(i=0; i<size; i++) {
|
---|
635 | result = (result | (data[i]) << (i*8));
|
---|
636 | }
|
---|
637 |
|
---|
638 | return result;
|
---|
639 | }
|
---|
640 |
|
---|
641 |
|
---|
642 |
|
---|
643 | /**
|
---|
644 | * Prints content of given list of report items.
|
---|
645 | *
|
---|
646 | * @param List of report items (usb_hid_report_item_t)
|
---|
647 | * @return void
|
---|
648 | */
|
---|
649 | void usb_hid_descriptor_print_list(link_t *head)
|
---|
650 | {
|
---|
651 | usb_hid_report_field_t *report_item;
|
---|
652 | link_t *item;
|
---|
653 |
|
---|
654 |
|
---|
655 | if(head == NULL || list_empty(head)) {
|
---|
656 | usb_log_debug("\tempty\n");
|
---|
657 | return;
|
---|
658 | }
|
---|
659 |
|
---|
660 | for(item = head->next; item != head; item = item->next) {
|
---|
661 |
|
---|
662 | report_item = list_get_instance(item, usb_hid_report_field_t, link);
|
---|
663 |
|
---|
664 | usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
|
---|
665 | usb_log_debug("\t\tSIZE: %X\n", report_item->size);
|
---|
666 | usb_log_debug("\t\tLOGMIN: %X\n", report_item->logical_minimum);
|
---|
667 | usb_log_debug("\t\tLOGMAX: %X\n", report_item->logical_maximum);
|
---|
668 | usb_log_debug("\t\tPHYMIN: %X\n", report_item->physical_minimum);
|
---|
669 | usb_log_debug("\t\tPHYMAX: %X\n", report_item->physical_maximum);
|
---|
670 | usb_log_debug("\t\ttUSAGEMIN: %X\n", report_item->usage_minimum);
|
---|
671 | usb_log_debug("\t\tUSAGEMAX: %X\n", report_item->usage_maximum);
|
---|
672 |
|
---|
673 | usb_log_debug("\t\tVALUE: %X\n", report_item->value);
|
---|
674 | usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage);
|
---|
675 | usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page);
|
---|
676 |
|
---|
677 | // usb_log_debug("\n");
|
---|
678 |
|
---|
679 | }
|
---|
680 |
|
---|
681 |
|
---|
682 | }
|
---|
683 | /**
|
---|
684 | * Prints content of given report descriptor in human readable format.
|
---|
685 | *
|
---|
686 | * @param parser Parsed descriptor to print
|
---|
687 | * @return void
|
---|
688 | */
|
---|
689 | void usb_hid_descriptor_print(usb_hid_report_t *report)
|
---|
690 | {
|
---|
691 | if(report == NULL) {
|
---|
692 | return;
|
---|
693 | }
|
---|
694 |
|
---|
695 | link_t *report_it = report->reports.next;
|
---|
696 | usb_hid_report_description_t *report_des;
|
---|
697 |
|
---|
698 | while(report_it != &report->reports) {
|
---|
699 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
700 | usb_log_debug("Report ID: %d\n", report_des->report_id);
|
---|
701 | usb_log_debug("\tType: %d\n", report_des->type);
|
---|
702 | usb_log_debug("\tLength: %d\n", report_des->bit_length);
|
---|
703 | usb_log_debug("\tItems: %d\n", report_des->item_length);
|
---|
704 |
|
---|
705 | usb_hid_descriptor_print_list(&report_des->report_items);
|
---|
706 |
|
---|
707 |
|
---|
708 | link_t *path_it = report->collection_paths.next;
|
---|
709 | while(path_it != &report->collection_paths) {
|
---|
710 | usb_hid_print_usage_path (list_get_instance(path_it, usb_hid_report_path_t, link));
|
---|
711 | path_it = path_it->next;
|
---|
712 | }
|
---|
713 |
|
---|
714 | report_it = report_it->next;
|
---|
715 | }
|
---|
716 | }
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * Releases whole linked list of report items
|
---|
720 | *
|
---|
721 | * @param head Head of list of report descriptor items (usb_hid_report_item_t)
|
---|
722 | * @return void
|
---|
723 | */
|
---|
724 | void usb_hid_free_report_list(link_t *head)
|
---|
725 | {
|
---|
726 | return;
|
---|
727 |
|
---|
728 | usb_hid_report_item_t *report_item;
|
---|
729 | link_t *next;
|
---|
730 |
|
---|
731 | if(head == NULL || list_empty(head)) {
|
---|
732 | return;
|
---|
733 | }
|
---|
734 |
|
---|
735 | next = head->next;
|
---|
736 | while(next != head) {
|
---|
737 |
|
---|
738 | report_item = list_get_instance(next, usb_hid_report_item_t, link);
|
---|
739 |
|
---|
740 | while(!list_empty(&report_item->usage_path->link)) {
|
---|
741 | usb_hid_report_remove_last_item(report_item->usage_path);
|
---|
742 | }
|
---|
743 |
|
---|
744 |
|
---|
745 | next = next->next;
|
---|
746 |
|
---|
747 | free(report_item);
|
---|
748 | }
|
---|
749 |
|
---|
750 | return;
|
---|
751 |
|
---|
752 | }
|
---|
753 |
|
---|
754 | /** Frees the HID report descriptor parser structure
|
---|
755 | *
|
---|
756 | * @param parser Opaque HID report parser structure
|
---|
757 | * @return void
|
---|
758 | */
|
---|
759 | void usb_hid_free_report(usb_hid_report_t *report)
|
---|
760 | {
|
---|
761 | if(report == NULL){
|
---|
762 | return;
|
---|
763 | }
|
---|
764 |
|
---|
765 | // free collection paths
|
---|
766 | usb_hid_report_path_t *path;
|
---|
767 | while(!list_empty(&report->collection_paths)) {
|
---|
768 | path = list_get_instance(report->collection_paths.next, usb_hid_report_path_t, link);
|
---|
769 | usb_hid_report_path_free(path);
|
---|
770 | }
|
---|
771 |
|
---|
772 | // free report items
|
---|
773 | usb_hid_report_description_t *report_des;
|
---|
774 | usb_hid_report_field_t *field;
|
---|
775 | while(!list_empty(&report->reports)) {
|
---|
776 | report_des = list_get_instance(report->reports.next, usb_hid_report_description_t, link);
|
---|
777 | list_remove(&report_des->link);
|
---|
778 |
|
---|
779 | while(!list_empty(&report_des->report_items)) {
|
---|
780 | field = list_get_instance(report_des->report_items.next, usb_hid_report_field_t, link);
|
---|
781 | list_remove(&field->link);
|
---|
782 |
|
---|
783 | free(field);
|
---|
784 | }
|
---|
785 |
|
---|
786 | free(report_des);
|
---|
787 | }
|
---|
788 |
|
---|
789 | return;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /** Parse and act upon a HID report.
|
---|
793 | *
|
---|
794 | * @see usb_hid_parse_report_descriptor
|
---|
795 | *
|
---|
796 | * @param parser Opaque HID report parser structure.
|
---|
797 | * @param data Data for the report.
|
---|
798 | * @return Error code.
|
---|
799 | */
|
---|
800 | int usb_hid_parse_report(const usb_hid_report_t *report,
|
---|
801 | const uint8_t *data, size_t size, uint8_t *report_id)
|
---|
802 | {
|
---|
803 | link_t *list_item;
|
---|
804 | usb_hid_report_field_t *item;
|
---|
805 |
|
---|
806 | usb_hid_report_description_t *report_des;
|
---|
807 | usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
|
---|
808 |
|
---|
809 | if(report == NULL) {
|
---|
810 | return EINVAL;
|
---|
811 | }
|
---|
812 |
|
---|
813 | if(report->use_report_ids != 0) {
|
---|
814 | *report_id = data[0];
|
---|
815 | }
|
---|
816 | else {
|
---|
817 | *report_id = 0;
|
---|
818 | }
|
---|
819 |
|
---|
820 |
|
---|
821 | report_des = usb_hid_report_find_description(report, *report_id, type);
|
---|
822 |
|
---|
823 | /* read data */
|
---|
824 | list_item = report_des->report_items.next;
|
---|
825 | while(list_item != &(report_des->report_items)) {
|
---|
826 |
|
---|
827 | item = list_get_instance(list_item, usb_hid_report_field_t, link);
|
---|
828 |
|
---|
829 | if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
|
---|
830 |
|
---|
831 | if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
|
---|
832 |
|
---|
833 | // array
|
---|
834 | item->value = usb_hid_translate_data(item, data);
|
---|
835 | item->usage = (item->value - item->physical_minimum) + item->usage_minimum;
|
---|
836 | }
|
---|
837 | else {
|
---|
838 | // variable item
|
---|
839 | item->value = usb_hid_translate_data(item, data);
|
---|
840 | }
|
---|
841 | }
|
---|
842 | list_item = list_item->next;
|
---|
843 | }
|
---|
844 |
|
---|
845 | return EOK;
|
---|
846 |
|
---|
847 | }
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * Translate data from the report as specified in report descriptor item
|
---|
851 | *
|
---|
852 | * @param item Report descriptor item with definition of translation
|
---|
853 | * @param data Data to translate
|
---|
854 | * @param j Index of processed field in report descriptor item
|
---|
855 | * @return Translated data
|
---|
856 | */
|
---|
857 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
|
---|
858 | {
|
---|
859 | int resolution;
|
---|
860 | int offset;
|
---|
861 | int part_size;
|
---|
862 |
|
---|
863 | int32_t value=0;
|
---|
864 | int32_t mask;
|
---|
865 | const uint8_t *foo;
|
---|
866 |
|
---|
867 | // now only shot tags are allowed
|
---|
868 | if(item->size > 32) {
|
---|
869 | return 0;
|
---|
870 | }
|
---|
871 |
|
---|
872 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
|
---|
873 | item->physical_minimum = item->logical_minimum;
|
---|
874 | item->physical_maximum = item->logical_maximum;
|
---|
875 | }
|
---|
876 |
|
---|
877 |
|
---|
878 | if(item->physical_maximum == item->physical_minimum){
|
---|
879 | resolution = 1;
|
---|
880 | }
|
---|
881 | else {
|
---|
882 | resolution = (item->logical_maximum - item->logical_minimum) /
|
---|
883 | ((item->physical_maximum - item->physical_minimum) *
|
---|
884 | (usb_pow(10,(item->unit_exponent))));
|
---|
885 | }
|
---|
886 |
|
---|
887 | offset = item->offset;
|
---|
888 | // FIXME
|
---|
889 | if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
|
---|
890 |
|
---|
891 | part_size = ((offset+item->size)%8);
|
---|
892 |
|
---|
893 | size_t i=0;
|
---|
894 | for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
|
---|
895 | if(i == (size_t)(offset/8)) {
|
---|
896 | // the higher one
|
---|
897 | foo = data + i;
|
---|
898 | mask = ((1 << (item->size-part_size))-1);
|
---|
899 | value = (*foo & mask) << part_size;
|
---|
900 | }
|
---|
901 | else if(i == ((offset+item->size-1)/8)){
|
---|
902 | // the lower one
|
---|
903 | foo = data + i;
|
---|
904 | mask = ((1 << part_size)-1) << (8-part_size);
|
---|
905 | value += ((*foo & mask) >> (8-part_size));
|
---|
906 | }
|
---|
907 | else {
|
---|
908 | value = value << 8;
|
---|
909 | value += *(data + 1);
|
---|
910 | }
|
---|
911 | }
|
---|
912 | }
|
---|
913 | else {
|
---|
914 | foo = data+(offset/8);
|
---|
915 | mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
|
---|
916 | value = (*foo & mask) >> (8-((offset%8)+item->size));
|
---|
917 | }
|
---|
918 |
|
---|
919 | if(!(item->logical_minimum >= 0 && item->logical_maximum >= 0)){
|
---|
920 | value = (int32_t)value;
|
---|
921 | }
|
---|
922 | else {
|
---|
923 | value = (uint32_t)value;
|
---|
924 | }
|
---|
925 |
|
---|
926 | return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
|
---|
927 |
|
---|
928 | }
|
---|
929 |
|
---|
930 | /**
|
---|
931 | * Returns number of items in input report which are accessible by given usage path
|
---|
932 | *
|
---|
933 | * @param parser Opaque report descriptor structure
|
---|
934 | * @param path Usage path specification
|
---|
935 | * @param flags Usage path comparison flags
|
---|
936 | * @return Number of items in input report
|
---|
937 | */
|
---|
938 | size_t usb_hid_report_input_length(const usb_hid_report_t *report,
|
---|
939 | usb_hid_report_path_t *path, int flags)
|
---|
940 | {
|
---|
941 |
|
---|
942 | size_t ret = 0;
|
---|
943 |
|
---|
944 | if(report == NULL) {
|
---|
945 | return 0;
|
---|
946 | }
|
---|
947 |
|
---|
948 | usb_hid_report_description_t *report_des;
|
---|
949 | report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_INPUT);
|
---|
950 | if(report_des == NULL) {
|
---|
951 | return 0;
|
---|
952 | }
|
---|
953 |
|
---|
954 | link_t *field_it = report_des->report_items.next;
|
---|
955 | usb_hid_report_field_t *field;
|
---|
956 | while(field_it != &report_des->report_items) {
|
---|
957 |
|
---|
958 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
---|
959 | if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
|
---|
960 |
|
---|
961 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
---|
962 | if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
|
---|
963 | ret++;
|
---|
964 | }
|
---|
965 | usb_hid_report_remove_last_item (field->collection_path);
|
---|
966 | }
|
---|
967 |
|
---|
968 | field_it = field_it->next;
|
---|
969 | }
|
---|
970 |
|
---|
971 | return ret;
|
---|
972 | }
|
---|
973 |
|
---|
974 |
|
---|
975 | /**
|
---|
976 | * Appends one item (couple of usage_path and usage) into the usage path
|
---|
977 | * structure
|
---|
978 | *
|
---|
979 | * @param usage_path Usage path structure
|
---|
980 | * @param usage_page Usage page constant
|
---|
981 | * @param usage Usage constant
|
---|
982 | * @return Error code
|
---|
983 | */
|
---|
984 | int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
|
---|
985 | int32_t usage_page, int32_t usage)
|
---|
986 | {
|
---|
987 | usb_hid_report_usage_path_t *item;
|
---|
988 |
|
---|
989 | if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
|
---|
990 | return ENOMEM;
|
---|
991 | }
|
---|
992 | list_initialize(&item->link);
|
---|
993 |
|
---|
994 | item->usage = usage;
|
---|
995 | item->usage_page = usage_page;
|
---|
996 | item->flags = 0;
|
---|
997 |
|
---|
998 | list_append (&item->link, &usage_path->head);
|
---|
999 | usage_path->depth++;
|
---|
1000 | return EOK;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | /**
|
---|
1004 | * Removes last item from the usage path structure
|
---|
1005 | * @param usage_path
|
---|
1006 | * @return void
|
---|
1007 | */
|
---|
1008 | void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
|
---|
1009 | {
|
---|
1010 | usb_hid_report_usage_path_t *item;
|
---|
1011 |
|
---|
1012 | if(!list_empty(&usage_path->head)){
|
---|
1013 | item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
|
---|
1014 | list_remove(usage_path->head.prev);
|
---|
1015 | usage_path->depth--;
|
---|
1016 | free(item);
|
---|
1017 | }
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | /**
|
---|
1021 | * Nulls last item of the usage path structure.
|
---|
1022 | *
|
---|
1023 | * @param usage_path
|
---|
1024 | * @return void
|
---|
1025 | */
|
---|
1026 | void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
|
---|
1027 | {
|
---|
1028 | usb_hid_report_usage_path_t *item;
|
---|
1029 |
|
---|
1030 | if(!list_empty(&usage_path->head)){
|
---|
1031 | item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
|
---|
1032 | memset(item, 0, sizeof(usb_hid_report_usage_path_t));
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | /**
|
---|
1037 | * Modifies last item of usage path structure by given usage page or usage
|
---|
1038 | *
|
---|
1039 | * @param usage_path Opaque usage path structure
|
---|
1040 | * @param tag Class of currently processed tag (Usage page tag falls into Global
|
---|
1041 | * class but Usage tag into the Local)
|
---|
1042 | * @param data Value of the processed tag
|
---|
1043 | * @return void
|
---|
1044 | */
|
---|
1045 | void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
|
---|
1046 | {
|
---|
1047 | usb_hid_report_usage_path_t *item;
|
---|
1048 |
|
---|
1049 | if(!list_empty(&usage_path->head)){
|
---|
1050 | item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
|
---|
1051 |
|
---|
1052 | switch(tag) {
|
---|
1053 | case USB_HID_TAG_CLASS_GLOBAL:
|
---|
1054 | item->usage_page = data;
|
---|
1055 | break;
|
---|
1056 | case USB_HID_TAG_CLASS_LOCAL:
|
---|
1057 | item->usage = data;
|
---|
1058 | break;
|
---|
1059 | }
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 |
|
---|
1065 | void usb_hid_print_usage_path(usb_hid_report_path_t *path)
|
---|
1066 | {
|
---|
1067 | usb_log_debug("USAGE_PATH FOR RId(%d):\n", path->report_id);
|
---|
1068 | usb_log_debug("\tLENGTH: %d\n", path->depth);
|
---|
1069 |
|
---|
1070 | link_t *item = path->head.next;
|
---|
1071 | usb_hid_report_usage_path_t *path_item;
|
---|
1072 | while(item != &path->head) {
|
---|
1073 |
|
---|
1074 | path_item = list_get_instance(item, usb_hid_report_usage_path_t, link);
|
---|
1075 | usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page);
|
---|
1076 | usb_log_debug("\tUSAGE: %X\n", path_item->usage);
|
---|
1077 | usb_log_debug("\tFLAGS: %d\n", path_item->flags);
|
---|
1078 |
|
---|
1079 | item = item->next;
|
---|
1080 | }
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | /**
|
---|
1084 | * Compares two usage paths structures
|
---|
1085 | *
|
---|
1086 | * If USB_HID_PATH_COMPARE_COLLECTION_ONLY flag is given, the last item in report_path structure is forgotten
|
---|
1087 | *
|
---|
1088 | * @param report_path usage path structure to compare
|
---|
1089 | * @param path usage patrh structure to compare
|
---|
1090 | * @param flags Flags determining the mode of comparison
|
---|
1091 | * @return EOK if both paths are identical, non zero number otherwise
|
---|
1092 | */
|
---|
1093 | int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
|
---|
1094 | usb_hid_report_path_t *path,
|
---|
1095 | int flags)
|
---|
1096 | {
|
---|
1097 | usb_hid_report_usage_path_t *report_item;
|
---|
1098 | usb_hid_report_usage_path_t *path_item;
|
---|
1099 |
|
---|
1100 | link_t *report_link;
|
---|
1101 | link_t *path_link;
|
---|
1102 |
|
---|
1103 | int only_page;
|
---|
1104 |
|
---|
1105 | if(report_path->report_id != path->report_id) {
|
---|
1106 | return 1;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | if(path->depth == 0){
|
---|
1110 | return EOK;
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 |
|
---|
1114 | if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
|
---|
1115 | flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | switch(flags){
|
---|
1119 | /* path must be completly identical */
|
---|
1120 | case USB_HID_PATH_COMPARE_STRICT:
|
---|
1121 | if(report_path->depth != path->depth){
|
---|
1122 | return 1;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | report_link = report_path->head.next;
|
---|
1126 | path_link = path->head.next;
|
---|
1127 |
|
---|
1128 | while((report_link != &report_path->head) && (path_link != &path->head)) {
|
---|
1129 | report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
|
---|
1130 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
---|
1131 |
|
---|
1132 | if((report_item->usage_page != path_item->usage_page) ||
|
---|
1133 | ((only_page == 0) && (report_item->usage != path_item->usage))) {
|
---|
1134 | return 1;
|
---|
1135 | } else {
|
---|
1136 | report_link = report_link->next;
|
---|
1137 | path_link = path_link->next;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | if(((report_link == &report_path->head) && (path_link == &path->head)) ||
|
---|
1143 | (((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) && (path_link = &path->head) && (report_link == report_path->head.prev))) {
|
---|
1144 | return EOK;
|
---|
1145 | }
|
---|
1146 | else {
|
---|
1147 | return 1;
|
---|
1148 | }
|
---|
1149 | break;
|
---|
1150 |
|
---|
1151 | /* compare with only the end of path*/
|
---|
1152 | case USB_HID_PATH_COMPARE_END:
|
---|
1153 |
|
---|
1154 | if((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) {
|
---|
1155 | report_link = report_path->head.prev->prev;
|
---|
1156 | }
|
---|
1157 | else {
|
---|
1158 | report_link = report_path->head.prev;
|
---|
1159 | }
|
---|
1160 | path_link = path->head.prev;
|
---|
1161 |
|
---|
1162 | if(list_empty(&path->head)){
|
---|
1163 | return EOK;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | while((report_link != &report_path->head) && (path_link != &path->head)) {
|
---|
1167 | report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
|
---|
1168 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
---|
1169 |
|
---|
1170 | if((report_item->usage_page != path_item->usage_page) ||
|
---|
1171 | ((only_page == 0) && (report_item->usage != path_item->usage))) {
|
---|
1172 | return 1;
|
---|
1173 | } else {
|
---|
1174 | report_link = report_link->prev;
|
---|
1175 | path_link = path_link->prev;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | if(path_link == &path->head) {
|
---|
1181 | return EOK;
|
---|
1182 | }
|
---|
1183 | else {
|
---|
1184 | return 1;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | break;
|
---|
1188 |
|
---|
1189 | default:
|
---|
1190 | return EINVAL;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 |
|
---|
1194 |
|
---|
1195 |
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | /**
|
---|
1199 | * Allocates and initializes new usage path structure.
|
---|
1200 | *
|
---|
1201 | * @return Initialized usage path structure
|
---|
1202 | */
|
---|
1203 | usb_hid_report_path_t *usb_hid_report_path(void)
|
---|
1204 | {
|
---|
1205 | usb_hid_report_path_t *path;
|
---|
1206 | path = malloc(sizeof(usb_hid_report_path_t));
|
---|
1207 | if(path == NULL){
|
---|
1208 | return NULL;
|
---|
1209 | }
|
---|
1210 | else {
|
---|
1211 | path->depth = 0;
|
---|
1212 | path->report_id = 0;
|
---|
1213 | list_initialize(&path->link);
|
---|
1214 | list_initialize(&path->head);
|
---|
1215 | return path;
|
---|
1216 | }
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /**
|
---|
1220 | * Releases given usage path structure.
|
---|
1221 | *
|
---|
1222 | * @param path usage path structure to release
|
---|
1223 | * @return void
|
---|
1224 | */
|
---|
1225 | void usb_hid_report_path_free(usb_hid_report_path_t *path)
|
---|
1226 | {
|
---|
1227 | while(!list_empty(&path->head)){
|
---|
1228 | usb_hid_report_remove_last_item(path);
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | list_remove(&path->link);
|
---|
1232 | free(path);
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 |
|
---|
1236 | /**
|
---|
1237 | * Clone content of given usage path to the new one
|
---|
1238 | *
|
---|
1239 | * @param usage_path Usage path structure to clone
|
---|
1240 | * @return New copy of given usage path structure
|
---|
1241 | */
|
---|
1242 | usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
|
---|
1243 | {
|
---|
1244 | link_t *path_link;
|
---|
1245 | usb_hid_report_usage_path_t *path_item;
|
---|
1246 | usb_hid_report_usage_path_t *new_path_item;
|
---|
1247 | usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
|
---|
1248 |
|
---|
1249 | if(new_usage_path == NULL){
|
---|
1250 | return NULL;
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | if(list_empty(&usage_path->head)){
|
---|
1254 | return new_usage_path;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | path_link = usage_path->head.next;
|
---|
1258 | while(path_link != &usage_path->head) {
|
---|
1259 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
---|
1260 | new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
|
---|
1261 | if(new_path_item == NULL) {
|
---|
1262 | return NULL;
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | list_initialize (&new_path_item->link);
|
---|
1266 | new_path_item->usage_page = path_item->usage_page;
|
---|
1267 | new_path_item->usage = path_item->usage;
|
---|
1268 | new_path_item->flags = path_item->flags;
|
---|
1269 |
|
---|
1270 | list_append(&new_path_item->link, &new_usage_path->head);
|
---|
1271 | new_usage_path->depth++;
|
---|
1272 |
|
---|
1273 | path_link = path_link->next;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | return new_usage_path;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 |
|
---|
1280 | /*** OUTPUT API **/
|
---|
1281 |
|
---|
1282 | /**
|
---|
1283 | * Allocates output report buffer for output report
|
---|
1284 | *
|
---|
1285 | * @param parser Report parsed structure
|
---|
1286 | * @param size Size of returned buffer
|
---|
1287 | * @param report_id Report id of created output report
|
---|
1288 | * @return Returns allocated output buffer for specified output
|
---|
1289 | */
|
---|
1290 | uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id)
|
---|
1291 | {
|
---|
1292 | if(report == NULL) {
|
---|
1293 | *size = 0;
|
---|
1294 | return NULL;
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | link_t *report_it = report->reports.next;
|
---|
1298 | usb_hid_report_description_t *report_des = NULL;
|
---|
1299 | while(report_it != &report->reports) {
|
---|
1300 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
1301 | if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
|
---|
1302 | break;
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | report_it = report_it->next;
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | if(report_des == NULL){
|
---|
1309 | *size = 0;
|
---|
1310 | return NULL;
|
---|
1311 | }
|
---|
1312 | else {
|
---|
1313 | *size = (report_des->bit_length + (8 - 1))/8;
|
---|
1314 | uint8_t *ret = malloc((*size) * sizeof(uint8_t));
|
---|
1315 | memset(ret, 0, (*size) * sizeof(uint8_t));
|
---|
1316 | return ret;
|
---|
1317 | }
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 |
|
---|
1321 | /** Frees output report buffer
|
---|
1322 | *
|
---|
1323 | * @param output Output report buffer
|
---|
1324 | * @return void
|
---|
1325 | */
|
---|
1326 | void usb_hid_report_output_free(uint8_t *output)
|
---|
1327 |
|
---|
1328 | {
|
---|
1329 | if(output != NULL) {
|
---|
1330 | free (output);
|
---|
1331 | }
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | /** Returns size of output for given usage path
|
---|
1335 | *
|
---|
1336 | * @param parser Opaque report parser structure
|
---|
1337 | * @param path Usage path specified which items will be thought for the output
|
---|
1338 | * @param flags Flags of usage path structure comparison
|
---|
1339 | * @return Number of items matching the given usage path
|
---|
1340 | */
|
---|
1341 | size_t usb_hid_report_output_size(usb_hid_report_t *report,
|
---|
1342 | usb_hid_report_path_t *path, int flags)
|
---|
1343 | {
|
---|
1344 | size_t ret = 0;
|
---|
1345 | usb_hid_report_description_t *report_des;
|
---|
1346 |
|
---|
1347 | if(report == NULL) {
|
---|
1348 | return 0;
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_OUTPUT);
|
---|
1352 | if(report_des == NULL){
|
---|
1353 | return 0;
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | link_t *field_it = report_des->report_items.next;
|
---|
1357 | usb_hid_report_field_t *field;
|
---|
1358 | while(field_it != &report_des->report_items) {
|
---|
1359 |
|
---|
1360 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
---|
1361 | if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0){
|
---|
1362 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
---|
1363 | if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
|
---|
1364 | ret++;
|
---|
1365 | }
|
---|
1366 | usb_hid_report_remove_last_item (field->collection_path);
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | field_it = field_it->next;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | return ret;
|
---|
1373 |
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | /** Makes the output report buffer for data given in the report structure
|
---|
1377 | *
|
---|
1378 | * @param parser Opaque report parser structure
|
---|
1379 | * @param path Usage path specifing which parts of output will be set
|
---|
1380 | * @param flags Usage path structure comparison flags
|
---|
1381 | * @param buffer Output buffer
|
---|
1382 | * @param size Size of output buffer
|
---|
1383 | * @return Error code
|
---|
1384 | */
|
---|
1385 | int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
|
---|
1386 | uint8_t *buffer, size_t size)
|
---|
1387 | {
|
---|
1388 | link_t *item;
|
---|
1389 | int32_t value=0;
|
---|
1390 | int offset;
|
---|
1391 | int length;
|
---|
1392 | int32_t tmp_value;
|
---|
1393 |
|
---|
1394 | if(report == NULL) {
|
---|
1395 | return EINVAL;
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | if(report->use_report_ids != 0) {
|
---|
1399 | buffer[0] = report_id;
|
---|
1400 | }
|
---|
1401 |
|
---|
1402 | usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
|
---|
1403 |
|
---|
1404 | usb_hid_report_description_t *report_des;
|
---|
1405 | report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT);
|
---|
1406 | if(report_des == NULL){
|
---|
1407 | return EINVAL;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | usb_hid_report_field_t *report_item;
|
---|
1411 | item = report_des->report_items.next;
|
---|
1412 | while(item != &report_des->report_items) {
|
---|
1413 | report_item = list_get_instance(item, usb_hid_report_field_t, link);
|
---|
1414 |
|
---|
1415 | if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
|
---|
1416 |
|
---|
1417 | // array
|
---|
1418 | value = usb_hid_translate_data_reverse(report_item, report_item->value);
|
---|
1419 | offset = report_item->offset;
|
---|
1420 | length = report_item->size;
|
---|
1421 | }
|
---|
1422 | else {
|
---|
1423 | // variable item
|
---|
1424 | value = usb_hid_translate_data_reverse(report_item, report_item->value);
|
---|
1425 | offset = report_item->offset;
|
---|
1426 | length = report_item->size;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | if((offset/8) == ((offset+length-1)/8)) {
|
---|
1430 | // je to v jednom bytu
|
---|
1431 | if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
|
---|
1432 | break; // TODO ErrorCode
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 | size_t shift = offset%8;
|
---|
1436 |
|
---|
1437 | value = value << shift;
|
---|
1438 | value = value & (((1 << length)-1) << shift);
|
---|
1439 |
|
---|
1440 | uint8_t mask = 0;
|
---|
1441 | mask = 0xff - (((1 << length) - 1) << shift);
|
---|
1442 | buffer[offset/8] = (buffer[offset/8] & mask) | value;
|
---|
1443 | }
|
---|
1444 | else {
|
---|
1445 | int i = 0;
|
---|
1446 | uint8_t mask = 0;
|
---|
1447 | for(i = (offset/8); i <= ((offset+length-1)/8); i++) {
|
---|
1448 | if(i == (offset/8)) {
|
---|
1449 | tmp_value = value;
|
---|
1450 | tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);
|
---|
1451 | tmp_value = tmp_value << (offset%8);
|
---|
1452 |
|
---|
1453 | mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
|
---|
1454 | buffer[i] = (buffer[i] & mask) | tmp_value;
|
---|
1455 | }
|
---|
1456 | else if (i == ((offset + length -1)/8)) {
|
---|
1457 |
|
---|
1458 | value = value >> (length - ((offset + length) % 8));
|
---|
1459 | value = value & ((1 << (length - ((offset + length) % 8))) - 1);
|
---|
1460 |
|
---|
1461 | mask = (1 << (length - ((offset + length) % 8))) - 1;
|
---|
1462 | buffer[i] = (buffer[i] & mask) | value;
|
---|
1463 | }
|
---|
1464 | else {
|
---|
1465 | buffer[i] = value & (0xFF << i);
|
---|
1466 | }
|
---|
1467 | }
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 |
|
---|
1471 | // reset value
|
---|
1472 | report_item->value = 0;
|
---|
1473 |
|
---|
1474 | item = item->next;
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 | usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
|
---|
1478 |
|
---|
1479 | return EOK;
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 | /**
|
---|
1483 | * Translate given data for putting them into the outoput report
|
---|
1484 | * @param item Report item structure
|
---|
1485 | * @param value Value to translate
|
---|
1486 | * @return ranslated value
|
---|
1487 | */
|
---|
1488 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value)
|
---|
1489 | {
|
---|
1490 | int ret=0;
|
---|
1491 | int resolution;
|
---|
1492 |
|
---|
1493 | if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
|
---|
1494 | ret = item->logical_minimum;
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
|
---|
1498 | item->physical_minimum = item->logical_minimum;
|
---|
1499 | item->physical_maximum = item->logical_maximum;
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 |
|
---|
1503 | if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
|
---|
1504 |
|
---|
1505 | // variable item
|
---|
1506 | if(item->physical_maximum == item->physical_minimum){
|
---|
1507 | resolution = 1;
|
---|
1508 | }
|
---|
1509 | else {
|
---|
1510 | resolution = (item->logical_maximum - item->logical_minimum) /
|
---|
1511 | ((item->physical_maximum - item->physical_minimum) *
|
---|
1512 | (usb_pow(10,(item->unit_exponent))));
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 | ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
|
---|
1516 | }
|
---|
1517 | else {
|
---|
1518 | // bitmapa
|
---|
1519 | if(value == 0) {
|
---|
1520 | ret = 0;
|
---|
1521 | }
|
---|
1522 | else {
|
---|
1523 | size_t bitmap_idx = (value - item->usage_minimum);
|
---|
1524 | ret = 1 << bitmap_idx;
|
---|
1525 | }
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 |
|
---|
1529 | return (uint32_t)ret;
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | /**
|
---|
1533 | * Sets report id in usage path structure
|
---|
1534 | *
|
---|
1535 | * @param path Usage path structure
|
---|
1536 | * @param report_id Report id to set
|
---|
1537 | * @return Error code
|
---|
1538 | */
|
---|
1539 | int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id)
|
---|
1540 | {
|
---|
1541 | if(path == NULL){
|
---|
1542 | return EINVAL;
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | path->report_id = report_id;
|
---|
1546 | return EOK;
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | /**
|
---|
1550 | *
|
---|
1551 | *
|
---|
1552 | *
|
---|
1553 | *
|
---|
1554 | *
|
---|
1555 | */
|
---|
1556 | int usb_hid_report_output_set_data(usb_hid_report_t *report,
|
---|
1557 | usb_hid_report_path_t *path, int flags,
|
---|
1558 | int *data, size_t data_size)
|
---|
1559 | {
|
---|
1560 | size_t data_idx = 0;
|
---|
1561 | if(report == NULL){
|
---|
1562 | return EINVAL;
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | usb_hid_report_description_t *report_des;
|
---|
1566 | report_des = usb_hid_report_find_description (report, path->report_id,
|
---|
1567 | USB_HID_REPORT_TYPE_OUTPUT);
|
---|
1568 | if(report_des == NULL){
|
---|
1569 | return EINVAL;
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 | usb_hid_report_field_t *field;
|
---|
1573 | link_t *field_it = report_des->report_items.next;
|
---|
1574 | while(field_it != &report_des->report_items){
|
---|
1575 |
|
---|
1576 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
---|
1577 | if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
|
---|
1578 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
---|
1579 | if(usb_hid_report_compare_usage_path (field->collection_path, path,
|
---|
1580 | flags) == EOK) {
|
---|
1581 | if(data_idx < data_size) {
|
---|
1582 | if((data[data_idx] >= field->physical_minimum) && (data[data_idx] >= field->physical_minimum)) {
|
---|
1583 | field->value = data[data_idx];
|
---|
1584 | }
|
---|
1585 | else {
|
---|
1586 | return ERANGE;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | data_idx++;
|
---|
1590 | }
|
---|
1591 | else {
|
---|
1592 | field->value = 0;
|
---|
1593 | }
|
---|
1594 | }
|
---|
1595 | usb_hid_report_remove_last_item (field->collection_path);
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | field_it = field_it->next;
|
---|
1599 | }
|
---|
1600 |
|
---|
1601 | return EOK;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 |
|
---|
1605 | usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item)
|
---|
1606 | {
|
---|
1607 | usb_hid_report_item_t *new_report_item;
|
---|
1608 |
|
---|
1609 | if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
|
---|
1610 | return NULL;
|
---|
1611 | }
|
---|
1612 | memcpy(new_report_item,item, sizeof(usb_hid_report_item_t));
|
---|
1613 | link_initialize(&(new_report_item->link));
|
---|
1614 |
|
---|
1615 | return new_report_item;
|
---|
1616 | }
|
---|
1617 |
|
---|
1618 |
|
---|
1619 | usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
|
---|
1620 | usb_hid_report_field_t *field,
|
---|
1621 | usb_hid_report_path_t *path, int flags,
|
---|
1622 | usb_hid_report_type_t type)
|
---|
1623 | {
|
---|
1624 | usb_hid_report_description_t *report_des = usb_hid_report_find_description (report, path->report_id, type);
|
---|
1625 | link_t *field_it;
|
---|
1626 |
|
---|
1627 | if(report_des == NULL){
|
---|
1628 | return NULL;
|
---|
1629 | }
|
---|
1630 |
|
---|
1631 | if(field == NULL){
|
---|
1632 | // vezmu prvni co mathuje podle path!!
|
---|
1633 | field_it = report_des->report_items.next;
|
---|
1634 | }
|
---|
1635 | else {
|
---|
1636 | field_it = field->link.next;
|
---|
1637 | }
|
---|
1638 |
|
---|
1639 | while(field_it != &report_des->report_items) {
|
---|
1640 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
---|
1641 |
|
---|
1642 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
---|
1643 | if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK){
|
---|
1644 | usb_hid_report_remove_last_item (field->collection_path);
|
---|
1645 | return field;
|
---|
1646 | }
|
---|
1647 | usb_hid_report_remove_last_item (field->collection_path);
|
---|
1648 |
|
---|
1649 | field_it = field_it->next;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | return NULL;
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
|
---|
1656 | {
|
---|
1657 | if(report == NULL){
|
---|
1658 | return 0;
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | usb_hid_report_description_t *report_des;
|
---|
1662 | link_t *report_it;
|
---|
1663 |
|
---|
1664 | if(report_id == 0) {
|
---|
1665 | report_it = usb_hid_report_find_description (report, report_id, type)->link.next;
|
---|
1666 | }
|
---|
1667 | else {
|
---|
1668 | report_it = report->reports.next;
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | while(report_it != &report->reports) {
|
---|
1672 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
1673 | if(report_des->type == type){
|
---|
1674 | return report_des->report_id;
|
---|
1675 | }
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | return 0;
|
---|
1679 | }
|
---|
1680 |
|
---|
1681 |
|
---|
1682 | /**
|
---|
1683 | * @}
|
---|
1684 | */
|
---|