source: mainline/uspace/lib/usbhid/src/hidpath.c@ feeac0d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since feeac0d was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

  • Property mode set to 100644
File size: 10.7 KB
Line 
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 */
75int 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 = malloc(sizeof(usb_hid_report_usage_path_t));
80
81 if (item == NULL) {
82 return ENOMEM;
83 }
84 link_initialize(&item->rpath_items_link);
85
86 item->usage = usage;
87 item->usage_page = usage_page;
88 item->flags = 0;
89
90 list_append (&item->rpath_items_link, &usage_path->items);
91 usage_path->depth++;
92 return EOK;
93}
94
95
96/**
97 * Removes last item from the usage path structure
98 * @param usage_path
99 * @return void
100 */
101void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
102{
103 link_t *item_link;
104 usb_hid_report_usage_path_t *item;
105
106 if(!list_empty(&usage_path->items)){
107 item_link = list_last(&usage_path->items);
108 item = list_get_instance(item_link,
109 usb_hid_report_usage_path_t, rpath_items_link);
110 list_remove(item_link);
111 usage_path->depth--;
112 free(item);
113 }
114}
115
116
117/**
118 * Nulls last item of the usage path structure.
119 *
120 * @param usage_path
121 * @return void
122 */
123void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
124{
125 usb_hid_report_usage_path_t *item;
126
127 if(!list_empty(&usage_path->items)){
128 item = list_get_instance(list_last(&usage_path->items),
129 usb_hid_report_usage_path_t, rpath_items_link);
130
131 memset(item, 0, sizeof(usb_hid_report_usage_path_t));
132 }
133}
134
135
136/**
137 * Modifies last item of usage path structure by given usage page or usage
138 *
139 * @param usage_path Opaque usage path structure
140 * @param tag Class of currently processed tag (Usage page tag falls into Global
141 * class but Usage tag into the Local)
142 * @param data Value of the processed tag
143 * @return void
144 */
145void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path,
146 int32_t tag, int32_t data)
147{
148 usb_hid_report_usage_path_t *item;
149
150 if(!list_empty(&usage_path->items)){
151 item = list_get_instance(list_last(&usage_path->items),
152 usb_hid_report_usage_path_t, rpath_items_link);
153
154 switch(tag) {
155 case USB_HID_TAG_CLASS_GLOBAL:
156 item->usage_page = data;
157 break;
158 case USB_HID_TAG_CLASS_LOCAL:
159 item->usage = data;
160 break;
161 }
162 }
163
164}
165
166
167/**
168 *
169 *
170 *
171 *
172 */
173void usb_hid_print_usage_path(usb_hid_report_path_t *path)
174{
175 usb_log_debug("USAGE_PATH FOR RId(%d):\n", path->report_id);
176 usb_log_debug("\tLENGTH: %d\n", path->depth);
177
178 list_foreach(path->items, rpath_items_link,
179 usb_hid_report_usage_path_t, path_item) {
180
181 usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page);
182 usb_log_debug("\tUSAGE: %X\n", path_item->usage);
183 usb_log_debug("\tFLAGS: %d\n", path_item->flags);
184 }
185}
186
187
188/**
189 * Compares two usage paths structures
190 *
191 *
192 * @param report_path usage path structure to compare with @path
193 * @param path usage patrh structure to compare
194 * @param flags Flags determining the mode of comparison
195 * @return EOK if both paths are identical, non zero number otherwise
196 */
197int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
198 usb_hid_report_path_t *path,
199 int flags)
200{
201 usb_hid_report_usage_path_t *report_item;
202 usb_hid_report_usage_path_t *path_item;
203
204 link_t *report_link;
205 link_t *path_link;
206
207 int only_page;
208
209 if(report_path->report_id != path->report_id) {
210 if(path->report_id != 0) {
211 return 1;
212 }
213 }
214
215 // Empty path match all others
216 if(path->depth == 0){
217 return EOK;
218 }
219
220
221 if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
222 flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
223 }
224
225 switch(flags){
226 /* path is somewhere in report_path */
227 case USB_HID_PATH_COMPARE_ANYWHERE:
228 if(path->depth != 1){
229 return 1;
230 }
231
232 path_link = list_first(&path->items);
233 path_item = list_get_instance(path_link,
234 usb_hid_report_usage_path_t, rpath_items_link);
235
236 list_foreach(report_path->items, rpath_items_link,
237 usb_hid_report_usage_path_t, report_item) {
238
239 if(USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
240 path_item->usage_page)){
241
242 if(only_page == 0){
243 if(USB_HID_SAME_USAGE(
244 report_item->usage,
245 path_item->usage)) {
246
247 return EOK;
248 }
249 }
250 else {
251 return EOK;
252 }
253 }
254 }
255
256 return 1;
257 break;
258
259 /* the paths must be identical */
260 case USB_HID_PATH_COMPARE_STRICT:
261 if(report_path->depth != path->depth){
262 return 1;
263 }
264
265 /* path is prefix of the report_path */
266 case USB_HID_PATH_COMPARE_BEGIN:
267
268 report_link = report_path->items.head.next;
269 path_link = path->items.head.next;
270
271 while((report_link != &report_path->items.head) &&
272 (path_link != &path->items.head)) {
273
274 report_item = list_get_instance(report_link,
275 usb_hid_report_usage_path_t, rpath_items_link);
276
277 path_item = list_get_instance(path_link,
278 usb_hid_report_usage_path_t, rpath_items_link);
279
280 if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
281 path_item->usage_page) || ((only_page == 0) &&
282 !USB_HID_SAME_USAGE(report_item->usage,
283 path_item->usage))) {
284
285 return 1;
286 } else {
287 report_link = report_link->next;
288 path_link = path_link->next;
289 }
290 }
291
292 if((((flags & USB_HID_PATH_COMPARE_BEGIN) != 0) &&
293 (path_link == &path->items.head)) ||
294 ((report_link == &report_path->items.head) &&
295 (path_link == &path->items.head))) {
296
297 return EOK;
298 }
299 else {
300 return 1;
301 }
302 break;
303
304 /* path is suffix of report_path */
305 case USB_HID_PATH_COMPARE_END:
306
307 report_link = report_path->items.head.prev;
308 path_link = path->items.head.prev;
309
310 if(list_empty(&path->items)){
311 return EOK;
312 }
313
314 while((report_link != &report_path->items.head) &&
315 (path_link != &path->items.head)) {
316
317 report_item = list_get_instance(report_link,
318 usb_hid_report_usage_path_t, rpath_items_link);
319
320 path_item = list_get_instance(path_link,
321 usb_hid_report_usage_path_t, rpath_items_link);
322
323 if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
324 path_item->usage_page) || ((only_page == 0) &&
325 !USB_HID_SAME_USAGE(report_item->usage,
326 path_item->usage))) {
327
328 return 1;
329 } else {
330 report_link = report_link->prev;
331 path_link = path_link->prev;
332 }
333
334 }
335
336 if(path_link == &path->items.head) {
337 return EOK;
338 }
339 else {
340 return 1;
341 }
342
343 break;
344
345 default:
346 return EINVAL;
347 }
348}
349
350
351/**
352 * Allocates and initializes new usage path structure.
353 *
354 * @return Initialized usage path structure
355 */
356usb_hid_report_path_t *usb_hid_report_path(void)
357{
358 usb_hid_report_path_t *path;
359 path = malloc(sizeof(usb_hid_report_path_t));
360 if(path == NULL){
361 return NULL;
362 }
363 else {
364 path->depth = 0;
365 path->report_id = 0;
366 link_initialize(&path->cpath_link);
367 list_initialize(&path->items);
368 return path;
369 }
370}
371
372
373/**
374 * Releases given usage path structure.
375 *
376 * @param path usage path structure to release
377 * @return void
378 */
379void usb_hid_report_path_free(usb_hid_report_path_t *path)
380{
381 if (path == NULL)
382 return;
383 while(!list_empty(&path->items)){
384 usb_hid_report_remove_last_item(path);
385 }
386
387 assert_link_not_used(&path->cpath_link);
388 free(path);
389}
390
391
392/**
393 * Clone content of given usage path to the new one
394 *
395 * @param usage_path Usage path structure to clone
396 * @return New copy of given usage path structure
397 */
398usb_hid_report_path_t *usb_hid_report_path_clone(
399 usb_hid_report_path_t *usage_path)
400{
401 usb_hid_report_usage_path_t *new_path_item;
402 usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
403
404 if(new_usage_path == NULL){
405 return NULL;
406 }
407
408 new_usage_path->report_id = usage_path->report_id;
409
410 if(list_empty(&usage_path->items)){
411 return new_usage_path;
412 }
413
414 list_foreach(usage_path->items, rpath_items_link,
415 usb_hid_report_usage_path_t, path_item) {
416
417 new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
418 if(new_path_item == NULL) {
419 return NULL;
420 }
421
422 link_initialize(&new_path_item->rpath_items_link);
423 new_path_item->usage_page = path_item->usage_page;
424 new_path_item->usage = path_item->usage;
425 new_path_item->flags = path_item->flags;
426
427 list_append(&new_path_item->rpath_items_link,
428 &new_usage_path->items);
429 new_usage_path->depth++;
430 }
431
432 return new_usage_path;
433}
434
435
436/**
437 * Sets report id in usage path structure
438 *
439 * @param path Usage path structure
440 * @param report_id Report id to set
441 * @return Error code
442 */
443int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path,
444 uint8_t report_id)
445{
446 if(path == NULL){
447 return EINVAL;
448 }
449
450 path->report_id = report_id;
451 return EOK;
452}
453
454/**
455 * @}
456 */
Note: See TracBrowser for help on using the repository browser.