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