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