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

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

More comment fixing (ccheck).

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