1 | /*
|
---|
2 | * Copyright (c) 2011 Matej Klonfar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libusbhid
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * HID report descriptor and report data parser implementation.
|
---|
34 | */
|
---|
35 | #include <usb/hid/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 | /*---------------------------------------------------------------------------*/
|
---|
44 | /**
|
---|
45 | * Compares two usages if they are same or not or one of the usages is not
|
---|
46 | * set.
|
---|
47 | *
|
---|
48 | * @param usage1
|
---|
49 | * @param usage2
|
---|
50 | * @return boolean
|
---|
51 | */
|
---|
52 | #define USB_HID_SAME_USAGE(usage1, usage2) \
|
---|
53 | ((usage1 == usage2) || (usage1 == 0) || (usage2 == 0))
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Compares two usage pages if they are same or not or one of them is not set.
|
---|
57 | *
|
---|
58 | * @param page1
|
---|
59 | * @param page2
|
---|
60 | * @return boolean
|
---|
61 | */
|
---|
62 | #define USB_HID_SAME_USAGE_PAGE(page1, page2) \
|
---|
63 | ((page1 == page2) || (page1 == 0) || (page2 == 0))
|
---|
64 |
|
---|
65 | /*---------------------------------------------------------------------------*/
|
---|
66 | /**
|
---|
67 | * Appends one item (couple of usage_path and usage) into the usage path
|
---|
68 | * structure
|
---|
69 | *
|
---|
70 | * @param usage_path Usage path structure
|
---|
71 | * @param usage_page Usage page constant
|
---|
72 | * @param usage Usage constant
|
---|
73 | * @return Error code
|
---|
74 | */
|
---|
75 | int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
|
---|
76 | int32_t usage_page, int32_t usage)
|
---|
77 | {
|
---|
78 | usb_hid_report_usage_path_t *item;
|
---|
79 |
|
---|
80 | if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
|
---|
81 | return ENOMEM;
|
---|
82 | }
|
---|
83 | list_initialize(&item->link);
|
---|
84 |
|
---|
85 | item->usage = usage;
|
---|
86 | item->usage_page = usage_page;
|
---|
87 | item->flags = 0;
|
---|
88 |
|
---|
89 | list_append (&item->link, &usage_path->head);
|
---|
90 | usage_path->depth++;
|
---|
91 | return EOK;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /*---------------------------------------------------------------------------*/
|
---|
95 | /**
|
---|
96 | * Removes last item from the usage path structure
|
---|
97 | * @param usage_path
|
---|
98 | * @return void
|
---|
99 | */
|
---|
100 | void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
|
---|
101 | {
|
---|
102 | usb_hid_report_usage_path_t *item;
|
---|
103 |
|
---|
104 | if(!list_empty(&usage_path->head)){
|
---|
105 | item = list_get_instance(usage_path->head.prev,
|
---|
106 | usb_hid_report_usage_path_t, link);
|
---|
107 | list_remove(usage_path->head.prev);
|
---|
108 | usage_path->depth--;
|
---|
109 | free(item);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | /*---------------------------------------------------------------------------*/
|
---|
114 | /**
|
---|
115 | * Nulls last item of the usage path structure.
|
---|
116 | *
|
---|
117 | * @param usage_path
|
---|
118 | * @return void
|
---|
119 | */
|
---|
120 | void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
|
---|
121 | {
|
---|
122 | usb_hid_report_usage_path_t *item;
|
---|
123 |
|
---|
124 | if(!list_empty(&usage_path->head)){
|
---|
125 | item = list_get_instance(usage_path->head.prev,
|
---|
126 | usb_hid_report_usage_path_t, link);
|
---|
127 |
|
---|
128 | memset(item, 0, sizeof(usb_hid_report_usage_path_t));
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*---------------------------------------------------------------------------*/
|
---|
133 | /**
|
---|
134 | * Modifies last item of usage path structure by given usage page or usage
|
---|
135 | *
|
---|
136 | * @param usage_path Opaque usage path structure
|
---|
137 | * @param tag Class of currently processed tag (Usage page tag falls into Global
|
---|
138 | * class but Usage tag into the Local)
|
---|
139 | * @param data Value of the processed tag
|
---|
140 | * @return void
|
---|
141 | */
|
---|
142 | void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path,
|
---|
143 | int32_t tag, int32_t data)
|
---|
144 | {
|
---|
145 | usb_hid_report_usage_path_t *item;
|
---|
146 |
|
---|
147 | if(!list_empty(&usage_path->head)){
|
---|
148 | item = list_get_instance(usage_path->head.prev,
|
---|
149 | usb_hid_report_usage_path_t, link);
|
---|
150 |
|
---|
151 | switch(tag) {
|
---|
152 | case USB_HID_TAG_CLASS_GLOBAL:
|
---|
153 | item->usage_page = data;
|
---|
154 | break;
|
---|
155 | case USB_HID_TAG_CLASS_LOCAL:
|
---|
156 | item->usage = data;
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*---------------------------------------------------------------------------*/
|
---|
164 | /**
|
---|
165 | *
|
---|
166 | *
|
---|
167 | *
|
---|
168 | *
|
---|
169 | */
|
---|
170 | void usb_hid_print_usage_path(usb_hid_report_path_t *path)
|
---|
171 | {
|
---|
172 | usb_log_debug("USAGE_PATH FOR RId(%d):\n", path->report_id);
|
---|
173 | usb_log_debug("\tLENGTH: %d\n", path->depth);
|
---|
174 |
|
---|
175 | link_t *item = path->head.next;
|
---|
176 | usb_hid_report_usage_path_t *path_item;
|
---|
177 | while(item != &path->head) {
|
---|
178 |
|
---|
179 | path_item = list_get_instance(item, usb_hid_report_usage_path_t,
|
---|
180 | link);
|
---|
181 |
|
---|
182 | usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page);
|
---|
183 | usb_log_debug("\tUSAGE: %X\n", path_item->usage);
|
---|
184 | usb_log_debug("\tFLAGS: %d\n", path_item->flags);
|
---|
185 |
|
---|
186 | item = item->next;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*---------------------------------------------------------------------------*/
|
---|
191 | /**
|
---|
192 | * Compares two usage paths structures
|
---|
193 | *
|
---|
194 | *
|
---|
195 | * @param report_path usage path structure to compare with @path
|
---|
196 | * @param path usage patrh structure to compare
|
---|
197 | * @param flags Flags determining the mode of comparison
|
---|
198 | * @return EOK if both paths are identical, non zero number otherwise
|
---|
199 | */
|
---|
200 | int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
|
---|
201 | usb_hid_report_path_t *path,
|
---|
202 | int flags)
|
---|
203 | {
|
---|
204 | usb_hid_report_usage_path_t *report_item;
|
---|
205 | usb_hid_report_usage_path_t *path_item;
|
---|
206 |
|
---|
207 | link_t *report_link;
|
---|
208 | link_t *path_link;
|
---|
209 |
|
---|
210 | int only_page;
|
---|
211 |
|
---|
212 | if(report_path->report_id != path->report_id) {
|
---|
213 | return 1;
|
---|
214 | }
|
---|
215 |
|
---|
216 | // Empty path match all others
|
---|
217 | if(path->depth == 0){
|
---|
218 | return EOK;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
|
---|
223 | flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
|
---|
224 | }
|
---|
225 |
|
---|
226 | switch(flags){
|
---|
227 | /* path is somewhere in report_path */
|
---|
228 | case USB_HID_PATH_COMPARE_ANYWHERE:
|
---|
229 | if(path->depth != 1){
|
---|
230 | return 1;
|
---|
231 | }
|
---|
232 |
|
---|
233 | report_link = report_path->head.next;
|
---|
234 | path_link = path->head.next;
|
---|
235 | path_item = list_get_instance(path_link,
|
---|
236 | usb_hid_report_usage_path_t, link);
|
---|
237 |
|
---|
238 | while(report_link != &report_path->head) {
|
---|
239 | report_item = list_get_instance(report_link,
|
---|
240 | usb_hid_report_usage_path_t, link);
|
---|
241 |
|
---|
242 | if(USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
|
---|
243 | path_item->usage_page)){
|
---|
244 |
|
---|
245 | if(only_page == 0){
|
---|
246 | if(USB_HID_SAME_USAGE(report_item->usage,
|
---|
247 | path_item->usage)) {
|
---|
248 |
|
---|
249 | return EOK;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | else {
|
---|
253 | return EOK;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | report_link = report_link->next;
|
---|
258 | }
|
---|
259 |
|
---|
260 | return 1;
|
---|
261 | break;
|
---|
262 | /* the paths must be identical */
|
---|
263 | case USB_HID_PATH_COMPARE_STRICT:
|
---|
264 | if(report_path->depth != path->depth){
|
---|
265 | return 1;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /* path is prefix of the report_path */
|
---|
269 | case USB_HID_PATH_COMPARE_BEGIN:
|
---|
270 |
|
---|
271 | report_link = report_path->head.next;
|
---|
272 | path_link = path->head.next;
|
---|
273 |
|
---|
274 | while((report_link != &report_path->head) &&
|
---|
275 | (path_link != &path->head)) {
|
---|
276 |
|
---|
277 | report_item = list_get_instance(report_link,
|
---|
278 | usb_hid_report_usage_path_t, link);
|
---|
279 |
|
---|
280 | path_item = list_get_instance(path_link,
|
---|
281 | usb_hid_report_usage_path_t, link);
|
---|
282 |
|
---|
283 | if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
|
---|
284 | path_item->usage_page) || ((only_page == 0) &&
|
---|
285 | !USB_HID_SAME_USAGE(report_item->usage,
|
---|
286 | path_item->usage))) {
|
---|
287 |
|
---|
288 | return 1;
|
---|
289 | } else {
|
---|
290 | report_link = report_link->next;
|
---|
291 | path_link = path_link->next;
|
---|
292 | }
|
---|
293 |
|
---|
294 | }
|
---|
295 |
|
---|
296 | if((((flags & USB_HID_PATH_COMPARE_BEGIN) != 0) &&
|
---|
297 | (path_link == &path->head)) ||
|
---|
298 | ((report_link == &report_path->head) &&
|
---|
299 | (path_link == &path->head))) {
|
---|
300 |
|
---|
301 | return EOK;
|
---|
302 | }
|
---|
303 | else {
|
---|
304 | return 1;
|
---|
305 | }
|
---|
306 | break;
|
---|
307 |
|
---|
308 | /* path is suffix of report_path */
|
---|
309 | case USB_HID_PATH_COMPARE_END:
|
---|
310 |
|
---|
311 | report_link = report_path->head.prev;
|
---|
312 | path_link = path->head.prev;
|
---|
313 |
|
---|
314 | if(list_empty(&path->head)){
|
---|
315 | return EOK;
|
---|
316 | }
|
---|
317 |
|
---|
318 | while((report_link != &report_path->head) &&
|
---|
319 | (path_link != &path->head)) {
|
---|
320 |
|
---|
321 | report_item = list_get_instance(report_link,
|
---|
322 | usb_hid_report_usage_path_t, link);
|
---|
323 |
|
---|
324 | path_item = list_get_instance(path_link,
|
---|
325 | usb_hid_report_usage_path_t, link);
|
---|
326 |
|
---|
327 | if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
|
---|
328 | path_item->usage_page) || ((only_page == 0) &&
|
---|
329 | !USB_HID_SAME_USAGE(report_item->usage,
|
---|
330 | path_item->usage))) {
|
---|
331 |
|
---|
332 | return 1;
|
---|
333 | } else {
|
---|
334 | report_link = report_link->prev;
|
---|
335 | path_link = path_link->prev;
|
---|
336 | }
|
---|
337 |
|
---|
338 | }
|
---|
339 |
|
---|
340 | if(path_link == &path->head) {
|
---|
341 | return EOK;
|
---|
342 | }
|
---|
343 | else {
|
---|
344 | return 1;
|
---|
345 | }
|
---|
346 |
|
---|
347 | break;
|
---|
348 |
|
---|
349 | default:
|
---|
350 | return EINVAL;
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | /*---------------------------------------------------------------------------*/
|
---|
355 | /**
|
---|
356 | * Allocates and initializes new usage path structure.
|
---|
357 | *
|
---|
358 | * @return Initialized usage path structure
|
---|
359 | */
|
---|
360 | usb_hid_report_path_t *usb_hid_report_path(void)
|
---|
361 | {
|
---|
362 | usb_hid_report_path_t *path;
|
---|
363 | path = malloc(sizeof(usb_hid_report_path_t));
|
---|
364 | if(path == NULL){
|
---|
365 | return NULL;
|
---|
366 | }
|
---|
367 | else {
|
---|
368 | path->depth = 0;
|
---|
369 | path->report_id = 0;
|
---|
370 | list_initialize(&path->link);
|
---|
371 | list_initialize(&path->head);
|
---|
372 | return path;
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | /*---------------------------------------------------------------------------*/
|
---|
377 | /**
|
---|
378 | * Releases given usage path structure.
|
---|
379 | *
|
---|
380 | * @param path usage path structure to release
|
---|
381 | * @return void
|
---|
382 | */
|
---|
383 | void usb_hid_report_path_free(usb_hid_report_path_t *path)
|
---|
384 | {
|
---|
385 | while(!list_empty(&path->head)){
|
---|
386 | usb_hid_report_remove_last_item(path);
|
---|
387 | }
|
---|
388 |
|
---|
389 | list_remove(&path->link);
|
---|
390 | free(path);
|
---|
391 | }
|
---|
392 |
|
---|
393 | /*---------------------------------------------------------------------------*/
|
---|
394 | /**
|
---|
395 | * Clone content of given usage path to the new one
|
---|
396 | *
|
---|
397 | * @param usage_path Usage path structure to clone
|
---|
398 | * @return New copy of given usage path structure
|
---|
399 | */
|
---|
400 | usb_hid_report_path_t *usb_hid_report_path_clone(
|
---|
401 | usb_hid_report_path_t *usage_path)
|
---|
402 | {
|
---|
403 | link_t *path_link;
|
---|
404 | usb_hid_report_usage_path_t *path_item;
|
---|
405 | usb_hid_report_usage_path_t *new_path_item;
|
---|
406 | usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
|
---|
407 |
|
---|
408 | if(new_usage_path == NULL){
|
---|
409 | return NULL;
|
---|
410 | }
|
---|
411 |
|
---|
412 | new_usage_path->report_id = usage_path->report_id;
|
---|
413 |
|
---|
414 | if(list_empty(&usage_path->head)){
|
---|
415 | return new_usage_path;
|
---|
416 | }
|
---|
417 |
|
---|
418 | path_link = usage_path->head.next;
|
---|
419 | while(path_link != &usage_path->head) {
|
---|
420 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t,
|
---|
421 | link);
|
---|
422 | new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
|
---|
423 | if(new_path_item == NULL) {
|
---|
424 | return NULL;
|
---|
425 | }
|
---|
426 |
|
---|
427 | list_initialize (&new_path_item->link);
|
---|
428 | new_path_item->usage_page = path_item->usage_page;
|
---|
429 | new_path_item->usage = path_item->usage;
|
---|
430 | new_path_item->flags = path_item->flags;
|
---|
431 |
|
---|
432 | list_append(&new_path_item->link, &new_usage_path->head);
|
---|
433 | new_usage_path->depth++;
|
---|
434 |
|
---|
435 | path_link = path_link->next;
|
---|
436 | }
|
---|
437 |
|
---|
438 | return new_usage_path;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /*---------------------------------------------------------------------------*/
|
---|
442 | /**
|
---|
443 | * Sets report id in usage path structure
|
---|
444 | *
|
---|
445 | * @param path Usage path structure
|
---|
446 | * @param report_id Report id to set
|
---|
447 | * @return Error code
|
---|
448 | */
|
---|
449 | int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path,
|
---|
450 | uint8_t report_id)
|
---|
451 | {
|
---|
452 | if(path == NULL){
|
---|
453 | return EINVAL;
|
---|
454 | }
|
---|
455 |
|
---|
456 | path->report_id = report_id;
|
---|
457 | return EOK;
|
---|
458 | }
|
---|
459 |
|
---|
460 | /**
|
---|
461 | * @}
|
---|
462 | */
|
---|