| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 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 libusb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief HID parser implementation.
|
|---|
| 34 | */
|
|---|
| 35 | #include <usb/classes/hidparser.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <malloc.h>
|
|---|
| 39 | #include <mem.h>
|
|---|
| 40 | #include <usb/debug.h>
|
|---|
| 41 |
|
|---|
| 42 | #define USB_HID_NEW_REPORT_ITEM 1
|
|---|
| 43 | #define USB_HID_NO_ACTION 2
|
|---|
| 44 | #define USB_HID_UNKNOWN_TAG -99
|
|---|
| 45 |
|
|---|
| 46 | #define BAD_HACK_USAGE_PAGE 0x07
|
|---|
| 47 |
|
|---|
| 48 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
|---|
| 49 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
|---|
| 50 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 51 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
|---|
| 52 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 53 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
|---|
| 54 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 55 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
|---|
| 56 |
|
|---|
| 57 | void usb_hid_descriptor_print_list(link_t *head);
|
|---|
| 58 | int usb_hid_report_reset_local_items();
|
|---|
| 59 | void usb_hid_free_report_list(link_t *head);
|
|---|
| 60 | int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
|
|---|
| 61 | inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
|
|---|
| 62 | int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j);
|
|---|
| 63 | int usb_pow(int a, int b);
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | int usb_pow(int a, int b)
|
|---|
| 67 | {
|
|---|
| 68 | switch(b) {
|
|---|
| 69 | case 0:
|
|---|
| 70 | return 1;
|
|---|
| 71 | break;
|
|---|
| 72 | case 1:
|
|---|
| 73 | return a;
|
|---|
| 74 | break;
|
|---|
| 75 | default:
|
|---|
| 76 | return a * usb_pow(a, b-1);
|
|---|
| 77 | break;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | *
|
|---|
| 83 | */
|
|---|
| 84 | int usb_hid_parser_init(usb_hid_report_parser_t *parser)
|
|---|
| 85 | {
|
|---|
| 86 | if(parser == NULL) {
|
|---|
| 87 | return EINVAL;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | list_initialize(&(parser->input));
|
|---|
| 91 | list_initialize(&(parser->output));
|
|---|
| 92 | list_initialize(&(parser->feature));
|
|---|
| 93 |
|
|---|
| 94 | return EOK;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | /** Parse HID report descriptor.
|
|---|
| 99 | *
|
|---|
| 100 | * @param parser Opaque HID report parser structure.
|
|---|
| 101 | * @param data Data describing the report.
|
|---|
| 102 | * @return Error code.
|
|---|
| 103 | */
|
|---|
| 104 | int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
|
|---|
| 105 | const uint8_t *data, size_t size)
|
|---|
| 106 | {
|
|---|
| 107 | size_t i=0;
|
|---|
| 108 | uint8_t tag=0;
|
|---|
| 109 | uint8_t item_size=0;
|
|---|
| 110 | int class=0;
|
|---|
| 111 | int ret;
|
|---|
| 112 | usb_hid_report_item_t *report_item=0;
|
|---|
| 113 | usb_hid_report_item_t *new_report_item;
|
|---|
| 114 | usb_hid_report_path_t *usage_path;
|
|---|
| 115 | usb_hid_report_path_t *tmp_usage_path;
|
|---|
| 116 |
|
|---|
| 117 | size_t offset_input=0;
|
|---|
| 118 | size_t offset_output=0;
|
|---|
| 119 | size_t offset_feature=0;
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | /* parser structure initialization*/
|
|---|
| 123 | if(usb_hid_parser_init(parser) != EOK) {
|
|---|
| 124 | return EINVAL;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | /*report item initialization*/
|
|---|
| 129 | if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
|
|---|
| 130 | return ENOMEM;
|
|---|
| 131 | }
|
|---|
| 132 | memset(report_item, 0, sizeof(usb_hid_report_item_t));
|
|---|
| 133 | list_initialize(&(report_item->link));
|
|---|
| 134 |
|
|---|
| 135 | /* usage path context initialization */
|
|---|
| 136 | if(!(usage_path=usb_hid_report_path())){
|
|---|
| 137 | return ENOMEM;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | while(i<size){
|
|---|
| 141 | if(!USB_HID_ITEM_IS_LONG(data[i])){
|
|---|
| 142 |
|
|---|
| 143 | if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
|
|---|
| 144 | return EINVAL; // TODO ERROR CODE
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | tag = USB_HID_ITEM_TAG(data[i]);
|
|---|
| 148 | item_size = USB_HID_ITEM_SIZE(data[i]);
|
|---|
| 149 | class = USB_HID_ITEM_TAG_CLASS(data[i]);
|
|---|
| 150 |
|
|---|
| 151 | usb_log_debug2(
|
|---|
| 152 | "i(%u) data(%X) value(%X): TAG %u, class %u, size %u - ", i,
|
|---|
| 153 | data[i], usb_hid_report_tag_data_int32(data+i+1,item_size),
|
|---|
| 154 | tag, class, item_size);
|
|---|
| 155 |
|
|---|
| 156 | ret = usb_hid_report_parse_tag(tag,class,data+i+1,
|
|---|
| 157 | item_size,report_item, usage_path);
|
|---|
| 158 | usb_log_debug2("ret: %u\n", ret);
|
|---|
| 159 | switch(ret){
|
|---|
| 160 | case USB_HID_NEW_REPORT_ITEM:
|
|---|
| 161 | // store report item to report and create the new one
|
|---|
| 162 | usb_log_debug("\nNEW REPORT ITEM: %X",ret);
|
|---|
| 163 |
|
|---|
| 164 | // store current usage path
|
|---|
| 165 | report_item->usage_path = usage_path;
|
|---|
| 166 |
|
|---|
| 167 | // new current usage path
|
|---|
| 168 | tmp_usage_path = usb_hid_report_path();
|
|---|
| 169 |
|
|---|
| 170 | // copy old path to the new one
|
|---|
| 171 | usb_hid_report_path_clone(tmp_usage_path, usage_path);
|
|---|
| 172 |
|
|---|
| 173 | // swap
|
|---|
| 174 | usage_path = tmp_usage_path;
|
|---|
| 175 | tmp_usage_path = NULL;
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 | switch(tag) {
|
|---|
| 179 | case USB_HID_REPORT_TAG_INPUT:
|
|---|
| 180 | report_item->offset = offset_input;
|
|---|
| 181 | offset_input += report_item->count * report_item->size;
|
|---|
| 182 | usb_log_debug(" - INPUT\n");
|
|---|
| 183 | list_append(&(report_item->link), &(parser->input));
|
|---|
| 184 | break;
|
|---|
| 185 | case USB_HID_REPORT_TAG_OUTPUT:
|
|---|
| 186 | report_item->offset = offset_output;
|
|---|
| 187 | offset_output += report_item->count * report_item->size;
|
|---|
| 188 | usb_log_debug(" - OUTPUT\n");
|
|---|
| 189 | list_append(&(report_item->link), &(parser->output));
|
|---|
| 190 |
|
|---|
| 191 | break;
|
|---|
| 192 | case USB_HID_REPORT_TAG_FEATURE:
|
|---|
| 193 | report_item->offset = offset_feature;
|
|---|
| 194 | offset_feature += report_item->count * report_item->size;
|
|---|
| 195 | usb_log_debug(" - FEATURE\n");
|
|---|
| 196 | list_append(&(report_item->link), &(parser->feature));
|
|---|
| 197 | break;
|
|---|
| 198 | default:
|
|---|
| 199 | usb_log_debug("\tjump over - tag %X\n", tag);
|
|---|
| 200 | break;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /* clone current state table to the new item */
|
|---|
| 204 | if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
|
|---|
| 205 | return ENOMEM;
|
|---|
| 206 | }
|
|---|
| 207 | memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));
|
|---|
| 208 | /* reset local items */
|
|---|
| 209 | new_report_item->usage_minimum = 0;
|
|---|
| 210 | new_report_item->usage_maximum = 0;
|
|---|
| 211 |
|
|---|
| 212 | link_initialize(&(new_report_item->link));
|
|---|
| 213 | report_item = new_report_item;
|
|---|
| 214 |
|
|---|
| 215 | break;
|
|---|
| 216 | case USB_HID_REPORT_TAG_PUSH:
|
|---|
| 217 | // push current state to stack
|
|---|
| 218 | // not yet implemented
|
|---|
| 219 | break;
|
|---|
| 220 | case USB_HID_REPORT_TAG_POP:
|
|---|
| 221 | // restore current state from stack
|
|---|
| 222 | // not yet implemented
|
|---|
| 223 | break;
|
|---|
| 224 |
|
|---|
| 225 | default:
|
|---|
| 226 | // nothing special to do
|
|---|
| 227 | break;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /* jump over the processed block */
|
|---|
| 231 | i += 1 + USB_HID_ITEM_SIZE(data[i]);
|
|---|
| 232 | }
|
|---|
| 233 | else{
|
|---|
| 234 | // TBD
|
|---|
| 235 | i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | return EOK;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 | /**
|
|---|
| 246 | * Parse input report.
|
|---|
| 247 | *
|
|---|
| 248 | * @param data Data for report
|
|---|
| 249 | * @param size Size of report
|
|---|
| 250 | * @param callbacks Callbacks for report actions
|
|---|
| 251 | * @param arg Custom arguments
|
|---|
| 252 | *
|
|---|
| 253 | * @return Error code
|
|---|
| 254 | */
|
|---|
| 255 | int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
|
|---|
| 256 | const usb_hid_report_in_callbacks_t *callbacks, void *arg)
|
|---|
| 257 | {
|
|---|
| 258 | int i;
|
|---|
| 259 | usb_hid_report_item_t item;
|
|---|
| 260 |
|
|---|
| 261 | /* fill item due to the boot protocol report descriptor */
|
|---|
| 262 | // modifier keys are in the first byte
|
|---|
| 263 | uint8_t modifiers = data[0];
|
|---|
| 264 |
|
|---|
| 265 | item.offset = 2; /* second byte is reserved */
|
|---|
| 266 | item.size = 8;
|
|---|
| 267 | item.count = 6;
|
|---|
| 268 | item.usage_minimum = 0;
|
|---|
| 269 | item.usage_maximum = 255;
|
|---|
| 270 | item.logical_minimum = 0;
|
|---|
| 271 | item.logical_maximum = 255;
|
|---|
| 272 |
|
|---|
| 273 | if (size != 8) {
|
|---|
| 274 | return -1; //ERANGE;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | uint8_t keys[6];
|
|---|
| 278 | for (i = 0; i < item.count; i++) {
|
|---|
| 279 | keys[i] = data[i + item.offset];
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | callbacks->keyboard(keys, 6, modifiers, arg);
|
|---|
| 283 | return EOK;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /**
|
|---|
| 287 | * Makes output report for keyboard boot protocol
|
|---|
| 288 | *
|
|---|
| 289 | * @param leds
|
|---|
| 290 | * @param output Output report data buffer
|
|---|
| 291 | * @param size Size of the output buffer
|
|---|
| 292 | * @return Error code
|
|---|
| 293 | */
|
|---|
| 294 | int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
|
|---|
| 295 | {
|
|---|
| 296 | if(size != 1){
|
|---|
| 297 | return -1;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /* used only first five bits, others are only padding*/
|
|---|
| 301 | *data = leds;
|
|---|
| 302 | return EOK;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | /**
|
|---|
| 306 | *
|
|---|
| 307 | * @param Tag to parse
|
|---|
| 308 | * @param Report descriptor buffer
|
|---|
| 309 | * @param Size of data belongs to this tag
|
|---|
| 310 | * @param Current report item structe
|
|---|
| 311 | * @return Code of action to be done next
|
|---|
| 312 | */
|
|---|
| 313 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
|---|
| 314 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
|---|
| 315 | {
|
|---|
| 316 | int ret;
|
|---|
| 317 |
|
|---|
| 318 | switch(class){
|
|---|
| 319 | case USB_HID_TAG_CLASS_MAIN:
|
|---|
| 320 |
|
|---|
| 321 | if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
|
|---|
| 322 | return USB_HID_NEW_REPORT_ITEM;
|
|---|
| 323 | }
|
|---|
| 324 | else {
|
|---|
| 325 | /*TODO process the error */
|
|---|
| 326 | return ret;
|
|---|
| 327 | }
|
|---|
| 328 | break;
|
|---|
| 329 |
|
|---|
| 330 | case USB_HID_TAG_CLASS_GLOBAL:
|
|---|
| 331 | return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
|
|---|
| 332 | break;
|
|---|
| 333 |
|
|---|
| 334 | case USB_HID_TAG_CLASS_LOCAL:
|
|---|
| 335 | return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
|
|---|
| 336 | break;
|
|---|
| 337 | default:
|
|---|
| 338 | return USB_HID_NO_ACTION;
|
|---|
| 339 | }
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | /**
|
|---|
| 343 | * Parse main tags of report descriptor
|
|---|
| 344 | *
|
|---|
| 345 | * @param Tag identifier
|
|---|
| 346 | * @param Data buffer
|
|---|
| 347 | * @param Length of data buffer
|
|---|
| 348 | * @param Current state table
|
|---|
| 349 | * @return Error code
|
|---|
| 350 | */
|
|---|
| 351 |
|
|---|
| 352 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 353 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
|---|
| 354 | {
|
|---|
| 355 | switch(tag)
|
|---|
| 356 | {
|
|---|
| 357 | case USB_HID_REPORT_TAG_INPUT:
|
|---|
| 358 | case USB_HID_REPORT_TAG_OUTPUT:
|
|---|
| 359 | case USB_HID_REPORT_TAG_FEATURE:
|
|---|
| 360 | report_item->item_flags = *data;
|
|---|
| 361 | return EOK;
|
|---|
| 362 | break;
|
|---|
| 363 |
|
|---|
| 364 | case USB_HID_REPORT_TAG_COLLECTION:
|
|---|
| 365 | usb_hid_report_path_append_item(usage_path, 0, 0);
|
|---|
| 366 |
|
|---|
| 367 | return USB_HID_NO_ACTION;
|
|---|
| 368 | break;
|
|---|
| 369 |
|
|---|
| 370 | case USB_HID_REPORT_TAG_END_COLLECTION:
|
|---|
| 371 | // TODO
|
|---|
| 372 | // znici posledni uroven ve vsech usage paths
|
|---|
| 373 | // otazka jestli nema nicit dve, respektive novou posledni vynulovat?
|
|---|
| 374 | usb_hid_report_remove_last_item(usage_path);
|
|---|
| 375 | return USB_HID_NO_ACTION;
|
|---|
| 376 | break;
|
|---|
| 377 | default:
|
|---|
| 378 | return USB_HID_NO_ACTION;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | return EOK;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /**
|
|---|
| 385 | * Parse global tags of report descriptor
|
|---|
| 386 | *
|
|---|
| 387 | * @param Tag identifier
|
|---|
| 388 | * @param Data buffer
|
|---|
| 389 | * @param Length of data buffer
|
|---|
| 390 | * @param Current state table
|
|---|
| 391 | * @return Error code
|
|---|
| 392 | */
|
|---|
| 393 |
|
|---|
| 394 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 395 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
|---|
| 396 | {
|
|---|
| 397 | // TODO take care about the bit length of data
|
|---|
| 398 | switch(tag)
|
|---|
| 399 | {
|
|---|
| 400 | case USB_HID_REPORT_TAG_USAGE_PAGE:
|
|---|
| 401 | // zmeni to jenom v poslednim poli aktualni usage path
|
|---|
| 402 | usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_GLOBAL,
|
|---|
| 403 | usb_hid_report_tag_data_int32(data,item_size));
|
|---|
| 404 | break;
|
|---|
| 405 | case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
|
|---|
| 406 | report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 407 | break;
|
|---|
| 408 | case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
|
|---|
| 409 | report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 410 | break;
|
|---|
| 411 | case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
|
|---|
| 412 | report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 413 | break;
|
|---|
| 414 | case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
|
|---|
| 415 | report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 416 | break;
|
|---|
| 417 | case USB_HID_REPORT_TAG_UNIT_EXPONENT:
|
|---|
| 418 | report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 419 | break;
|
|---|
| 420 | case USB_HID_REPORT_TAG_UNIT:
|
|---|
| 421 | report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 422 | break;
|
|---|
| 423 | case USB_HID_REPORT_TAG_REPORT_SIZE:
|
|---|
| 424 | report_item->size = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 425 | break;
|
|---|
| 426 | case USB_HID_REPORT_TAG_REPORT_COUNT:
|
|---|
| 427 | report_item->count = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 428 | break;
|
|---|
| 429 | case USB_HID_REPORT_TAG_REPORT_ID:
|
|---|
| 430 | report_item->id = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 431 | break;
|
|---|
| 432 | case USB_HID_REPORT_TAG_PUSH:
|
|---|
| 433 | case USB_HID_REPORT_TAG_POP:
|
|---|
| 434 | return tag;
|
|---|
| 435 | break;
|
|---|
| 436 |
|
|---|
| 437 | default:
|
|---|
| 438 | return USB_HID_NO_ACTION;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | return EOK;
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /**
|
|---|
| 445 | * Parse local tags of report descriptor
|
|---|
| 446 | *
|
|---|
| 447 | * @param Tag identifier
|
|---|
| 448 | * @param Data buffer
|
|---|
| 449 | * @param Length of data buffer
|
|---|
| 450 | * @param Current state table
|
|---|
| 451 | * @return Error code
|
|---|
| 452 | */
|
|---|
| 453 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 454 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
|---|
| 455 | {
|
|---|
| 456 | switch(tag)
|
|---|
| 457 | {
|
|---|
| 458 | case USB_HID_REPORT_TAG_USAGE:
|
|---|
| 459 | usb_hid_report_set_last_item(usage_path, USB_HID_TAG_CLASS_LOCAL,
|
|---|
| 460 | usb_hid_report_tag_data_int32(data,item_size));
|
|---|
| 461 | break;
|
|---|
| 462 | case USB_HID_REPORT_TAG_USAGE_MINIMUM:
|
|---|
| 463 | report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 464 | break;
|
|---|
| 465 | case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
|
|---|
| 466 | report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 467 | break;
|
|---|
| 468 | case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
|
|---|
| 469 | report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 470 | break;
|
|---|
| 471 | case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
|
|---|
| 472 | report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 473 | break;
|
|---|
| 474 | case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
|
|---|
| 475 | report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 476 | break;
|
|---|
| 477 | case USB_HID_REPORT_TAG_STRING_INDEX:
|
|---|
| 478 | report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 479 | break;
|
|---|
| 480 | case USB_HID_REPORT_TAG_STRING_MINIMUM:
|
|---|
| 481 | report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 482 | break;
|
|---|
| 483 | case USB_HID_REPORT_TAG_STRING_MAXIMUM:
|
|---|
| 484 | report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 485 | break;
|
|---|
| 486 | case USB_HID_REPORT_TAG_DELIMITER:
|
|---|
| 487 | report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 488 | break;
|
|---|
| 489 |
|
|---|
| 490 | default:
|
|---|
| 491 | return USB_HID_NO_ACTION;
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | return EOK;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | /**
|
|---|
| 498 | * Converts raw data to int32 (thats the maximum length of short item data)
|
|---|
| 499 | *
|
|---|
| 500 | * @param Data buffer
|
|---|
| 501 | * @param Size of buffer
|
|---|
| 502 | * @return Converted int32 number
|
|---|
| 503 | */
|
|---|
| 504 | int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
|
|---|
| 505 | {
|
|---|
| 506 | unsigned int i;
|
|---|
| 507 | int32_t result;
|
|---|
| 508 |
|
|---|
| 509 | result = 0;
|
|---|
| 510 | for(i=0; i<size; i++) {
|
|---|
| 511 | result = (result | (data[i]) << (i*8));
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | return result;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 | /**
|
|---|
| 520 | * Prints content of given list of report items.
|
|---|
| 521 | *
|
|---|
| 522 | * @param List of report items
|
|---|
| 523 | * @return void
|
|---|
| 524 | */
|
|---|
| 525 | void usb_hid_descriptor_print_list(link_t *head)
|
|---|
| 526 | {
|
|---|
| 527 | usb_hid_report_item_t *report_item;
|
|---|
| 528 | usb_hid_report_usage_path_t *path_item;
|
|---|
| 529 | link_t *path;
|
|---|
| 530 | link_t *item;
|
|---|
| 531 |
|
|---|
| 532 | if(head == NULL || list_empty(head)) {
|
|---|
| 533 | usb_log_debug("\tempty\n");
|
|---|
| 534 | return;
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | for(item = head->next; item != head; item = item->next) {
|
|---|
| 538 |
|
|---|
| 539 | report_item = list_get_instance(item, usb_hid_report_item_t, link);
|
|---|
| 540 |
|
|---|
| 541 | usb_log_debug("\tOFFSET: %X\n", report_item->offset);
|
|---|
| 542 | usb_log_debug("\tCOUNT: %X\n", report_item->count);
|
|---|
| 543 | usb_log_debug("\tSIZE: %X\n", report_item->size);
|
|---|
| 544 | usb_log_debug("\tCONSTANT/VAR: %X\n", USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags));
|
|---|
| 545 | usb_log_debug("\tVARIABLE/ARRAY: %X\n", USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags));
|
|---|
| 546 | usb_log_debug("\tUSAGE PATH:\n");
|
|---|
| 547 |
|
|---|
| 548 | path = report_item->usage_path->link.next;
|
|---|
| 549 | while(path != &report_item->usage_path->link) {
|
|---|
| 550 | path_item = list_get_instance(path, usb_hid_report_usage_path_t, link);
|
|---|
| 551 | usb_log_debug("\t\tUSAGE PAGE: %X, USAGE: %X\n", path_item->usage_page, path_item->usage);
|
|---|
| 552 | path = path->next;
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 | // usb_log_debug("\tUSAGE: %X\n", report_item->usage);
|
|---|
| 557 | // usb_log_debug("\tUSAGE PAGE: %X\n", report_item->usage_page);
|
|---|
| 558 | usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum);
|
|---|
| 559 | usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum);
|
|---|
| 560 | usb_log_debug("\tPHYMIN: %X\n", report_item->physical_minimum);
|
|---|
| 561 | usb_log_debug("\tPHYMAX: %X\n", report_item->physical_maximum);
|
|---|
| 562 | usb_log_debug("\tUSAGEMIN: %X\n", report_item->usage_minimum);
|
|---|
| 563 | usb_log_debug("\tUSAGEMAX: %X\n", report_item->usage_maximum);
|
|---|
| 564 |
|
|---|
| 565 | usb_log_debug("\n");
|
|---|
| 566 |
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 | }
|
|---|
| 571 | /**
|
|---|
| 572 | * Prints content of given descriptor in human readable format.
|
|---|
| 573 | *
|
|---|
| 574 | * @param Parsed descriptor to print
|
|---|
| 575 | * @return void
|
|---|
| 576 | */
|
|---|
| 577 | void usb_hid_descriptor_print(usb_hid_report_parser_t *parser)
|
|---|
| 578 | {
|
|---|
| 579 | if(parser == NULL) {
|
|---|
| 580 | return;
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | usb_log_debug("INPUT:\n");
|
|---|
| 584 | usb_hid_descriptor_print_list(&parser->input);
|
|---|
| 585 |
|
|---|
| 586 | usb_log_debug("OUTPUT: \n");
|
|---|
| 587 | usb_hid_descriptor_print_list(&parser->output);
|
|---|
| 588 |
|
|---|
| 589 | usb_log_debug("FEATURE:\n");
|
|---|
| 590 | usb_hid_descriptor_print_list(&parser->feature);
|
|---|
| 591 |
|
|---|
| 592 | }
|
|---|
| 593 |
|
|---|
| 594 | /**
|
|---|
| 595 | * Releases whole linked list of report items
|
|---|
| 596 | *
|
|---|
| 597 | *
|
|---|
| 598 | */
|
|---|
| 599 | void usb_hid_free_report_list(link_t *head)
|
|---|
| 600 | {
|
|---|
| 601 | return;
|
|---|
| 602 |
|
|---|
| 603 | usb_hid_report_item_t *report_item;
|
|---|
| 604 | link_t *next;
|
|---|
| 605 |
|
|---|
| 606 | if(head == NULL || list_empty(head)) {
|
|---|
| 607 | return;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | next = head->next;
|
|---|
| 611 | while(next != head) {
|
|---|
| 612 |
|
|---|
| 613 | report_item = list_get_instance(next, usb_hid_report_item_t, link);
|
|---|
| 614 |
|
|---|
| 615 | while(!list_empty(&report_item->usage_path->link)) {
|
|---|
| 616 | usb_hid_report_remove_last_item(report_item->usage_path);
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 | next = next->next;
|
|---|
| 621 |
|
|---|
| 622 | free(report_item);
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | return;
|
|---|
| 626 |
|
|---|
| 627 | }
|
|---|
| 628 |
|
|---|
| 629 | /** Free the HID report parser structure
|
|---|
| 630 | *
|
|---|
| 631 | * @param parser Opaque HID report parser structure
|
|---|
| 632 | * @return Error code
|
|---|
| 633 | */
|
|---|
| 634 | void usb_hid_free_report_parser(usb_hid_report_parser_t *parser)
|
|---|
| 635 | {
|
|---|
| 636 | if(parser == NULL){
|
|---|
| 637 | return;
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | usb_hid_free_report_list(&parser->input);
|
|---|
| 641 | usb_hid_free_report_list(&parser->output);
|
|---|
| 642 | usb_hid_free_report_list(&parser->feature);
|
|---|
| 643 |
|
|---|
| 644 | return;
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | /** Parse and act upon a HID report.
|
|---|
| 648 | *
|
|---|
| 649 | * @see usb_hid_parse_report_descriptor
|
|---|
| 650 | *
|
|---|
| 651 | * @param parser Opaque HID report parser structure.
|
|---|
| 652 | * @param data Data for the report.
|
|---|
| 653 | * @param callbacks Callbacks for report actions.
|
|---|
| 654 | * @param arg Custom argument (passed through to the callbacks).
|
|---|
| 655 | * @return Error code.
|
|---|
| 656 | */
|
|---|
| 657 | int usb_hid_parse_report(const usb_hid_report_parser_t *parser,
|
|---|
| 658 | const uint8_t *data, size_t size,
|
|---|
| 659 | usb_hid_report_path_t *path, int flags,
|
|---|
| 660 | const usb_hid_report_in_callbacks_t *callbacks, void *arg)
|
|---|
| 661 | {
|
|---|
| 662 | /*
|
|---|
| 663 | *
|
|---|
| 664 | * only key codes (usage page 0x07) will be processed
|
|---|
| 665 | * other usages will be ignored
|
|---|
| 666 | */
|
|---|
| 667 | link_t *list_item;
|
|---|
| 668 | usb_hid_report_item_t *item;
|
|---|
| 669 | uint8_t *keys;
|
|---|
| 670 | uint8_t item_value;
|
|---|
| 671 | size_t key_count=0;
|
|---|
| 672 | size_t i=0;
|
|---|
| 673 | size_t j=0;
|
|---|
| 674 |
|
|---|
| 675 | if(parser == NULL) {
|
|---|
| 676 | return EINVAL;
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 | // get the size of result keycodes array
|
|---|
| 681 | key_count = usb_hid_report_input_length(parser, path, flags);
|
|---|
| 682 |
|
|---|
| 683 | if(!(keys = malloc(sizeof(uint8_t) * key_count))){
|
|---|
| 684 | return ENOMEM;
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | // read data
|
|---|
| 688 | list_item = parser->input.next;
|
|---|
| 689 | while(list_item != &(parser->input)) {
|
|---|
| 690 |
|
|---|
| 691 | item = list_get_instance(list_item, usb_hid_report_item_t, link);
|
|---|
| 692 | if(!USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) &&
|
|---|
| 693 | (usb_hid_report_compare_usage_path(item->usage_path, path, flags) == EOK)) {
|
|---|
| 694 | for(j=0; j<(size_t)(item->count); j++) {
|
|---|
| 695 | if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) ||
|
|---|
| 696 | ((item->usage_minimum == 0) && (item->usage_maximum == 0))) {
|
|---|
| 697 | // variable item
|
|---|
| 698 | keys[i++] = usb_hid_translate_data(item, data,j);
|
|---|
| 699 | }
|
|---|
| 700 | else {
|
|---|
| 701 | // bitmapa
|
|---|
| 702 | if((item_value = usb_hid_translate_data(item, data, j)) != 0) {
|
|---|
| 703 | keys[i++] = (item->count - 1 - j) + item->usage_minimum;
|
|---|
| 704 | }
|
|---|
| 705 | else {
|
|---|
| 706 | keys[i++] = 0;
|
|---|
| 707 | }
|
|---|
| 708 | }
|
|---|
| 709 | }
|
|---|
| 710 | }
|
|---|
| 711 | list_item = list_item->next;
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | callbacks->keyboard(keys, key_count, 0, arg);
|
|---|
| 715 |
|
|---|
| 716 | free(keys);
|
|---|
| 717 | return EOK;
|
|---|
| 718 |
|
|---|
| 719 | }
|
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 | int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j)
|
|---|
| 723 | {
|
|---|
| 724 | int resolution;
|
|---|
| 725 | int offset;
|
|---|
| 726 | int part_size;
|
|---|
| 727 |
|
|---|
| 728 | int32_t value;
|
|---|
| 729 | int32_t mask;
|
|---|
| 730 | const uint8_t *foo;
|
|---|
| 731 |
|
|---|
| 732 | // now only common numbers llowed
|
|---|
| 733 | if(item->size > 32) {
|
|---|
| 734 | return 0;
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
|
|---|
| 738 | item->physical_minimum = item->logical_minimum;
|
|---|
| 739 | item->physical_maximum = item->logical_maximum;
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | if(item->physical_maximum == item->physical_minimum){
|
|---|
| 743 | resolution = 1;
|
|---|
| 744 | }
|
|---|
| 745 | else {
|
|---|
| 746 | resolution = (item->logical_maximum - item->logical_minimum) /
|
|---|
| 747 | ((item->physical_maximum - item->physical_minimum) *
|
|---|
| 748 | (usb_pow(10,(item->unit_exponent))));
|
|---|
| 749 | }
|
|---|
| 750 | offset = item->offset + (j * item->size);
|
|---|
| 751 |
|
|---|
| 752 | // FIXME
|
|---|
| 753 | if((offset/8) != ((offset+item->size)/8)) {
|
|---|
| 754 | usb_log_debug2("offset %d\n", offset);
|
|---|
| 755 |
|
|---|
| 756 | part_size = ((offset+item->size)%8);
|
|---|
| 757 | usb_log_debug2("part size %d\n",part_size);
|
|---|
| 758 |
|
|---|
| 759 | // the higher one
|
|---|
| 760 | foo = data+(offset/8);
|
|---|
| 761 | mask = ((1 << (item->size-part_size))-1);
|
|---|
| 762 | value = (*foo & mask) << part_size;
|
|---|
| 763 |
|
|---|
| 764 | usb_log_debug2("hfoo %x\n", *foo);
|
|---|
| 765 | usb_log_debug2("hmaska %x\n", mask);
|
|---|
| 766 | usb_log_debug2("hval %d\n", value);
|
|---|
| 767 |
|
|---|
| 768 | // the lower one
|
|---|
| 769 | foo = data+((offset+item->size)/8);
|
|---|
| 770 | mask = ((1 << part_size)-1) << (8-part_size);
|
|---|
| 771 | value += ((*foo & mask) >> (8-part_size));
|
|---|
| 772 |
|
|---|
| 773 | usb_log_debug2("lfoo %x\n", *foo);
|
|---|
| 774 | usb_log_debug2("lmaska %x\n", mask);
|
|---|
| 775 | usb_log_debug2("lval %d\n", ((*foo & mask) >> (8-(item->size-part_size))));
|
|---|
| 776 | usb_log_debug2("val %d\n", value);
|
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 | }
|
|---|
| 780 | else {
|
|---|
| 781 | foo = data+(offset/8);
|
|---|
| 782 | mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
|
|---|
| 783 | value = (*foo & mask) >> (8-((offset%8)+item->size));
|
|---|
| 784 |
|
|---|
| 785 | usb_log_debug2("offset %d\n", offset);
|
|---|
| 786 |
|
|---|
| 787 | usb_log_debug2("foo %x\n", *foo);
|
|---|
| 788 | usb_log_debug2("maska %x\n", mask);
|
|---|
| 789 | usb_log_debug2("val %d\n", value);
|
|---|
| 790 | }
|
|---|
| 791 |
|
|---|
| 792 | usb_log_debug2("---\n\n");
|
|---|
| 793 |
|
|---|
| 794 | return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
|
|---|
| 795 |
|
|---|
| 796 | }
|
|---|
| 797 |
|
|---|
| 798 | int usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
|
|---|
| 799 | usb_hid_report_path_t *path, int flags)
|
|---|
| 800 | {
|
|---|
| 801 | int ret = 0;
|
|---|
| 802 | link_t *item;
|
|---|
| 803 | usb_hid_report_item_t *report_item;
|
|---|
| 804 |
|
|---|
| 805 | if(parser == NULL) {
|
|---|
| 806 | return EINVAL;
|
|---|
| 807 | }
|
|---|
| 808 |
|
|---|
| 809 | item = (&parser->input)->next;
|
|---|
| 810 | while(&parser->input != item) {
|
|---|
| 811 | report_item = list_get_instance(item, usb_hid_report_item_t, link);
|
|---|
| 812 | if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
|
|---|
| 813 | (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
|
|---|
| 814 | ret += report_item->count;
|
|---|
| 815 | }
|
|---|
| 816 |
|
|---|
| 817 | item = item->next;
|
|---|
| 818 | }
|
|---|
| 819 |
|
|---|
| 820 | return ret;
|
|---|
| 821 | }
|
|---|
| 822 |
|
|---|
| 823 |
|
|---|
| 824 | /**
|
|---|
| 825 | *
|
|---|
| 826 | */
|
|---|
| 827 | int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
|
|---|
| 828 | int32_t usage_page, int32_t usage)
|
|---|
| 829 | {
|
|---|
| 830 | usb_hid_report_usage_path_t *item;
|
|---|
| 831 |
|
|---|
| 832 | if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
|
|---|
| 833 | return ENOMEM;
|
|---|
| 834 | }
|
|---|
| 835 | list_initialize(&item->link);
|
|---|
| 836 |
|
|---|
| 837 | item->usage = usage;
|
|---|
| 838 | item->usage_page = usage_page;
|
|---|
| 839 |
|
|---|
| 840 | list_append (&usage_path->link, &item->link);
|
|---|
| 841 | usage_path->depth++;
|
|---|
| 842 | return EOK;
|
|---|
| 843 | }
|
|---|
| 844 |
|
|---|
| 845 | /**
|
|---|
| 846 | *
|
|---|
| 847 | */
|
|---|
| 848 | void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
|
|---|
| 849 | {
|
|---|
| 850 | usb_hid_report_usage_path_t *item;
|
|---|
| 851 |
|
|---|
| 852 | if(!list_empty(&usage_path->link)){
|
|---|
| 853 | item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
|
|---|
| 854 | list_remove(usage_path->link.prev);
|
|---|
| 855 | usage_path->depth--;
|
|---|
| 856 | free(item);
|
|---|
| 857 | }
|
|---|
| 858 | }
|
|---|
| 859 |
|
|---|
| 860 | /**
|
|---|
| 861 | *
|
|---|
| 862 | */
|
|---|
| 863 | void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
|
|---|
| 864 | {
|
|---|
| 865 | usb_hid_report_usage_path_t *item;
|
|---|
| 866 |
|
|---|
| 867 | if(!list_empty(&usage_path->link)){
|
|---|
| 868 | item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
|
|---|
| 869 | memset(item, 0, sizeof(usb_hid_report_usage_path_t));
|
|---|
| 870 | }
|
|---|
| 871 | }
|
|---|
| 872 |
|
|---|
| 873 | /**
|
|---|
| 874 | *
|
|---|
| 875 | */
|
|---|
| 876 | void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
|
|---|
| 877 | {
|
|---|
| 878 | usb_hid_report_usage_path_t *item;
|
|---|
| 879 |
|
|---|
| 880 | if(!list_empty(&usage_path->link)){
|
|---|
| 881 | item = list_get_instance(usage_path->link.prev, usb_hid_report_usage_path_t, link);
|
|---|
| 882 |
|
|---|
| 883 | switch(tag) {
|
|---|
| 884 | case USB_HID_TAG_CLASS_GLOBAL:
|
|---|
| 885 | item->usage_page = data;
|
|---|
| 886 | break;
|
|---|
| 887 | case USB_HID_TAG_CLASS_LOCAL:
|
|---|
| 888 | item->usage = data;
|
|---|
| 889 | break;
|
|---|
| 890 | }
|
|---|
| 891 | }
|
|---|
| 892 |
|
|---|
| 893 | }
|
|---|
| 894 |
|
|---|
| 895 | /**
|
|---|
| 896 | *
|
|---|
| 897 | */
|
|---|
| 898 | int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
|
|---|
| 899 | usb_hid_report_path_t *path,
|
|---|
| 900 | int flags)
|
|---|
| 901 | {
|
|---|
| 902 | usb_hid_report_usage_path_t *report_item;
|
|---|
| 903 | usb_hid_report_usage_path_t *path_item;
|
|---|
| 904 |
|
|---|
| 905 | link_t *report_link;
|
|---|
| 906 | link_t *path_link;
|
|---|
| 907 |
|
|---|
| 908 | int only_page;
|
|---|
| 909 |
|
|---|
| 910 | if(path->depth == 0){
|
|---|
| 911 | return EOK;
|
|---|
| 912 | }
|
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 | if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
|
|---|
| 916 | flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
|
|---|
| 917 | }
|
|---|
| 918 |
|
|---|
| 919 | switch(flags){
|
|---|
| 920 | /* path must be completly identical */
|
|---|
| 921 | case USB_HID_PATH_COMPARE_STRICT:
|
|---|
| 922 | if(report_path->depth != path->depth){
|
|---|
| 923 | return 1;
|
|---|
| 924 | }
|
|---|
| 925 |
|
|---|
| 926 | report_link = report_path->link.next;
|
|---|
| 927 | path_link = path->link.next;
|
|---|
| 928 |
|
|---|
| 929 | while((report_link != &report_path->link) && (path_link != &path->link)) {
|
|---|
| 930 | report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
|
|---|
| 931 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
|---|
| 932 |
|
|---|
| 933 | if((report_item->usage_page != path_item->usage_page) ||
|
|---|
| 934 | ((only_page == 0) && (report_item->usage != path_item->usage))) {
|
|---|
| 935 | return 1;
|
|---|
| 936 | } else {
|
|---|
| 937 | report_link = report_link->next;
|
|---|
| 938 | path_link = path_link->next;
|
|---|
| 939 | }
|
|---|
| 940 |
|
|---|
| 941 | }
|
|---|
| 942 |
|
|---|
| 943 | if((report_link == &report_path->link) && (path_link == &path->link)) {
|
|---|
| 944 | return EOK;
|
|---|
| 945 | }
|
|---|
| 946 | else {
|
|---|
| 947 | return 1;
|
|---|
| 948 | }
|
|---|
| 949 | break;
|
|---|
| 950 |
|
|---|
| 951 | /* given path must be the end of the report one*/
|
|---|
| 952 | case USB_HID_PATH_COMPARE_END:
|
|---|
| 953 | report_link = report_path->link.prev;
|
|---|
| 954 | path_link = path->link.prev;
|
|---|
| 955 |
|
|---|
| 956 | if(list_empty(&path->link)){
|
|---|
| 957 | return EOK;
|
|---|
| 958 | }
|
|---|
| 959 |
|
|---|
| 960 | while((report_link != &report_path->link) && (path_link != &path->link)) {
|
|---|
| 961 | report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
|
|---|
| 962 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
|---|
| 963 |
|
|---|
| 964 | if((report_item->usage_page != path_item->usage_page) ||
|
|---|
| 965 | ((only_page == 0) && (report_item->usage != path_item->usage))) {
|
|---|
| 966 | return 1;
|
|---|
| 967 | } else {
|
|---|
| 968 | report_link = report_link->prev;
|
|---|
| 969 | path_link = path_link->prev;
|
|---|
| 970 | }
|
|---|
| 971 |
|
|---|
| 972 | }
|
|---|
| 973 |
|
|---|
| 974 | if(path_link == &path->link) {
|
|---|
| 975 | return EOK;
|
|---|
| 976 | }
|
|---|
| 977 | else {
|
|---|
| 978 | return 1;
|
|---|
| 979 | }
|
|---|
| 980 |
|
|---|
| 981 | break;
|
|---|
| 982 |
|
|---|
| 983 | default:
|
|---|
| 984 | return EINVAL;
|
|---|
| 985 | }
|
|---|
| 986 |
|
|---|
| 987 |
|
|---|
| 988 |
|
|---|
| 989 |
|
|---|
| 990 | }
|
|---|
| 991 |
|
|---|
| 992 | /**
|
|---|
| 993 | *
|
|---|
| 994 | */
|
|---|
| 995 | usb_hid_report_path_t *usb_hid_report_path(void)
|
|---|
| 996 | {
|
|---|
| 997 | usb_hid_report_path_t *path;
|
|---|
| 998 | path = malloc(sizeof(usb_hid_report_path_t));
|
|---|
| 999 | if(!path){
|
|---|
| 1000 | return NULL;
|
|---|
| 1001 | }
|
|---|
| 1002 | else {
|
|---|
| 1003 | path->depth = 0;
|
|---|
| 1004 | list_initialize(&path->link);
|
|---|
| 1005 | return path;
|
|---|
| 1006 | }
|
|---|
| 1007 | }
|
|---|
| 1008 |
|
|---|
| 1009 | /**
|
|---|
| 1010 | *
|
|---|
| 1011 | */
|
|---|
| 1012 | void usb_hid_report_path_free(usb_hid_report_path_t *path)
|
|---|
| 1013 | {
|
|---|
| 1014 | while(!list_empty(&path->link)){
|
|---|
| 1015 | usb_hid_report_remove_last_item(path);
|
|---|
| 1016 | }
|
|---|
| 1017 | }
|
|---|
| 1018 |
|
|---|
| 1019 |
|
|---|
| 1020 | /**
|
|---|
| 1021 | *
|
|---|
| 1022 | */
|
|---|
| 1023 | int usb_hid_report_path_clone(usb_hid_report_path_t *new_usage_path, usb_hid_report_path_t *usage_path)
|
|---|
| 1024 | {
|
|---|
| 1025 | usb_hid_report_usage_path_t *path_item;
|
|---|
| 1026 | link_t *path_link;
|
|---|
| 1027 |
|
|---|
| 1028 |
|
|---|
| 1029 | if(list_empty(&usage_path->link)){
|
|---|
| 1030 | return EOK;
|
|---|
| 1031 | }
|
|---|
| 1032 |
|
|---|
| 1033 | path_link = usage_path->link.next;
|
|---|
| 1034 | while(path_link != &usage_path->link) {
|
|---|
| 1035 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
|---|
| 1036 | usb_hid_report_path_append_item (new_usage_path, path_item->usage_page, path_item->usage);
|
|---|
| 1037 |
|
|---|
| 1038 | path_link = path_link->next;
|
|---|
| 1039 | }
|
|---|
| 1040 |
|
|---|
| 1041 | return EOK;
|
|---|
| 1042 | }
|
|---|
| 1043 |
|
|---|
| 1044 |
|
|---|
| 1045 | /**
|
|---|
| 1046 | * @}
|
|---|
| 1047 | */
|
|---|