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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f3b39b4 was f3b39b4, checked in by Matej Klonfar <maklf@…>, 15 years ago

doxygen and coding style update

  • Property mode set to 100644
File size: 11.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
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 */
100void 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 */
120void 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 */
142void 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 */
170void 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 */
200int 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(
247 report_item->usage,
248 path_item->usage)) {
249
250 return EOK;
251 }
252 }
253 else {
254 return EOK;
255 }
256 }
257
258 report_link = report_link->next;
259 }
260
261 return 1;
262 break;
263
264 /* the paths must be identical */
265 case USB_HID_PATH_COMPARE_STRICT:
266 if(report_path->depth != path->depth){
267 return 1;
268 }
269
270 /* path is prefix of the report_path */
271 case USB_HID_PATH_COMPARE_BEGIN:
272
273 report_link = report_path->head.next;
274 path_link = path->head.next;
275
276 while((report_link != &report_path->head) &&
277 (path_link != &path->head)) {
278
279 report_item = list_get_instance(report_link,
280 usb_hid_report_usage_path_t, link);
281
282 path_item = list_get_instance(path_link,
283 usb_hid_report_usage_path_t, link);
284
285 if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
286 path_item->usage_page) || ((only_page == 0) &&
287 !USB_HID_SAME_USAGE(report_item->usage,
288 path_item->usage))) {
289
290 return 1;
291 }
292 else {
293 report_link = report_link->next;
294 path_link = path_link->next;
295 }
296
297 }
298
299 if((((flags & USB_HID_PATH_COMPARE_BEGIN) != 0) &&
300 (path_link == &path->head)) ||
301 ((report_link == &report_path->head) &&
302 (path_link == &path->head))) {
303
304 return EOK;
305 }
306 else {
307 return 1;
308 }
309 break;
310
311 /* path is suffix of report_path */
312 case USB_HID_PATH_COMPARE_END:
313
314 report_link = report_path->head.prev;
315 path_link = path->head.prev;
316
317 if(list_empty(&path->head)){
318 return EOK;
319 }
320
321 while((report_link != &report_path->head) &&
322 (path_link != &path->head)) {
323
324 report_item = list_get_instance(report_link,
325 usb_hid_report_usage_path_t, link);
326
327 path_item = list_get_instance(path_link,
328 usb_hid_report_usage_path_t, link);
329
330 if(!USB_HID_SAME_USAGE_PAGE(report_item->usage_page,
331 path_item->usage_page) || ((only_page == 0) &&
332 !USB_HID_SAME_USAGE(report_item->usage,
333 path_item->usage))) {
334
335 return 1;
336 } else {
337 report_link = report_link->prev;
338 path_link = path_link->prev;
339 }
340
341 }
342
343 if(path_link == &path->head) {
344 return EOK;
345 }
346 else {
347 return 1;
348 }
349
350 break;
351
352 default:
353 return EINVAL;
354 }
355}
356
357/*---------------------------------------------------------------------------*/
358/**
359 * Allocates and initializes new usage path structure.
360 *
361 * @return Initialized usage path structure
362 */
363usb_hid_report_path_t *usb_hid_report_path(void)
364{
365 usb_hid_report_path_t *path;
366 path = malloc(sizeof(usb_hid_report_path_t));
367 if(path == NULL){
368 return NULL;
369 }
370 else {
371 path->depth = 0;
372 path->report_id = 0;
373 list_initialize(&path->link);
374 list_initialize(&path->head);
375 return path;
376 }
377}
378
379/*---------------------------------------------------------------------------*/
380/**
381 * Releases given usage path structure.
382 *
383 * @param path usage path structure to release
384 * @return void
385 */
386void usb_hid_report_path_free(usb_hid_report_path_t *path)
387{
388 while(!list_empty(&path->head)){
389 usb_hid_report_remove_last_item(path);
390 }
391
392 list_remove(&path->link);
393 free(path);
394}
395
396/*---------------------------------------------------------------------------*/
397/**
398 * Clone content of given usage path to the new one
399 *
400 * @param usage_path Usage path structure to clone
401 * @return New copy of given usage path structure
402 */
403usb_hid_report_path_t *usb_hid_report_path_clone(
404 usb_hid_report_path_t *usage_path)
405{
406 link_t *path_link;
407 usb_hid_report_usage_path_t *path_item;
408 usb_hid_report_usage_path_t *new_path_item;
409 usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
410
411 if(new_usage_path == NULL){
412 return NULL;
413 }
414
415 new_usage_path->report_id = usage_path->report_id;
416
417 if(list_empty(&usage_path->head)){
418 return new_usage_path;
419 }
420
421 path_link = usage_path->head.next;
422 while(path_link != &usage_path->head) {
423 path_item = list_get_instance(path_link,
424 usb_hid_report_usage_path_t, link);
425
426 new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
427 if(new_path_item == NULL) {
428 return NULL;
429 }
430
431 list_initialize (&new_path_item->link);
432 new_path_item->usage_page = path_item->usage_page;
433 new_path_item->usage = path_item->usage;
434 new_path_item->flags = path_item->flags;
435
436 list_append(&new_path_item->link, &new_usage_path->head);
437 new_usage_path->depth++;
438
439 path_link = path_link->next;
440 }
441
442 return new_usage_path;
443}
444
445/*---------------------------------------------------------------------------*/
446/**
447 * Sets report id in usage path structure
448 *
449 * @param path Usage path structure
450 * @param report_id Report id to set
451 * @return Error code
452 */
453int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path,
454 uint8_t report_id)
455{
456 if(path == NULL){
457 return EINVAL;
458 }
459
460 path->report_id = report_id;
461 return EOK;
462}
463
464/**
465 * @}
466 */
Note: See TracBrowser for help on using the repository browser.