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

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

Separate list_t typedef from link_t (user-space part).

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