| 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);
|
|---|
| 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);
|
|---|
| 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);
|
|---|
| 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);
|
|---|
| 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 | int usb_pow(int a, int b)
|
|---|
| 66 | {
|
|---|
| 67 | switch(b) {
|
|---|
| 68 | case 0:
|
|---|
| 69 | return 1;
|
|---|
| 70 | break;
|
|---|
| 71 | case 1:
|
|---|
| 72 | return a;
|
|---|
| 73 | break;
|
|---|
| 74 | default:
|
|---|
| 75 | return a * usb_pow(a, b-1);
|
|---|
| 76 | break;
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | *
|
|---|
| 82 | */
|
|---|
| 83 | int usb_hid_parser_init(usb_hid_report_parser_t *parser)
|
|---|
| 84 | {
|
|---|
| 85 | if(parser == NULL) {
|
|---|
| 86 | return -1;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | list_initialize(&(parser->input));
|
|---|
| 90 | list_initialize(&(parser->output));
|
|---|
| 91 | list_initialize(&(parser->feature));
|
|---|
| 92 |
|
|---|
| 93 | return EOK;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | /** Parse HID report descriptor.
|
|---|
| 98 | *
|
|---|
| 99 | * @param parser Opaque HID report parser structure.
|
|---|
| 100 | * @param data Data describing the report.
|
|---|
| 101 | * @return Error code.
|
|---|
| 102 | */
|
|---|
| 103 | int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
|
|---|
| 104 | const uint8_t *data, size_t size)
|
|---|
| 105 | {
|
|---|
| 106 | size_t i=0;
|
|---|
| 107 | uint8_t tag=0;
|
|---|
| 108 | uint8_t item_size=0;
|
|---|
| 109 | int class=0;
|
|---|
| 110 | int ret;
|
|---|
| 111 | usb_hid_report_item_t *report_item=0;
|
|---|
| 112 | usb_hid_report_item_t *new_report_item;
|
|---|
| 113 |
|
|---|
| 114 | size_t offset_input=0;
|
|---|
| 115 | size_t offset_output=0;
|
|---|
| 116 | size_t offset_feature=0;
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
|
|---|
| 120 | return ENOMEM;
|
|---|
| 121 | }
|
|---|
| 122 | memset(report_item, 0, sizeof(usb_hid_report_item_t));
|
|---|
| 123 |
|
|---|
| 124 | link_initialize(&(report_item->link));
|
|---|
| 125 |
|
|---|
| 126 | while(i<size){
|
|---|
| 127 | if(!USB_HID_ITEM_IS_LONG(data[i])){
|
|---|
| 128 |
|
|---|
| 129 | if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
|
|---|
| 130 | return -1; // TODO ERROR CODE
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | tag = USB_HID_ITEM_TAG(data[i]);
|
|---|
| 134 | item_size = USB_HID_ITEM_SIZE(data[i]);
|
|---|
| 135 | class = USB_HID_ITEM_TAG_CLASS(data[i]);
|
|---|
| 136 |
|
|---|
| 137 | usb_log_debug2(
|
|---|
| 138 | "i(%u) data(%X) value(%X): TAG %u, class %u, size %u - ", i,
|
|---|
| 139 | data[i], usb_hid_report_tag_data_int32(data+i+1,item_size),
|
|---|
| 140 | tag, class, item_size);
|
|---|
| 141 |
|
|---|
| 142 | ret = usb_hid_report_parse_tag(tag,class,data+i+1,
|
|---|
| 143 | item_size,report_item);
|
|---|
| 144 | usb_log_debug2("ret: %u\n", ret);
|
|---|
| 145 | switch(ret){
|
|---|
| 146 | case USB_HID_NEW_REPORT_ITEM:
|
|---|
| 147 | // store report item to report and create the new one
|
|---|
| 148 | usb_log_debug("\nNEW REPORT ITEM: %X",tag);
|
|---|
| 149 |
|
|---|
| 150 | switch(tag) {
|
|---|
| 151 | case USB_HID_REPORT_TAG_INPUT:
|
|---|
| 152 | report_item->offset = offset_input;
|
|---|
| 153 | offset_input += report_item->count * report_item->size;
|
|---|
| 154 | usb_log_debug(" - INPUT\n");
|
|---|
| 155 | list_append(&(report_item->link), &(parser->input));
|
|---|
| 156 | break;
|
|---|
| 157 | case USB_HID_REPORT_TAG_OUTPUT:
|
|---|
| 158 | report_item->offset = offset_output;
|
|---|
| 159 | offset_output += report_item->count * report_item->size;
|
|---|
| 160 | usb_log_debug(" - OUTPUT\n");
|
|---|
| 161 | list_append(&(report_item->link), &(parser->output));
|
|---|
| 162 |
|
|---|
| 163 | break;
|
|---|
| 164 | case USB_HID_REPORT_TAG_FEATURE:
|
|---|
| 165 | report_item->offset = offset_feature;
|
|---|
| 166 | offset_feature += report_item->count * report_item->size;
|
|---|
| 167 | usb_log_debug(" - FEATURE\n");
|
|---|
| 168 | list_append(&(report_item->link), &(parser->feature));
|
|---|
| 169 | break;
|
|---|
| 170 | default:
|
|---|
| 171 | usb_log_debug("\tjump over - tag %X\n", tag);
|
|---|
| 172 | break;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /* clone current state table to the new item */
|
|---|
| 176 | if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
|
|---|
| 177 | return ENOMEM;
|
|---|
| 178 | }
|
|---|
| 179 | memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));
|
|---|
| 180 | link_initialize(&(new_report_item->link));
|
|---|
| 181 | report_item = new_report_item;
|
|---|
| 182 |
|
|---|
| 183 | break;
|
|---|
| 184 | case USB_HID_REPORT_TAG_PUSH:
|
|---|
| 185 | // push current state to stack
|
|---|
| 186 | // not yet implemented
|
|---|
| 187 | break;
|
|---|
| 188 | case USB_HID_REPORT_TAG_POP:
|
|---|
| 189 | // restore current state from stack
|
|---|
| 190 | // not yet implemented
|
|---|
| 191 | break;
|
|---|
| 192 |
|
|---|
| 193 | default:
|
|---|
| 194 | // nothing special to do
|
|---|
| 195 | break;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /* jump over the processed block */
|
|---|
| 199 | i += 1 + USB_HID_ITEM_SIZE(data[i]);
|
|---|
| 200 | }
|
|---|
| 201 | else{
|
|---|
| 202 | // TBD
|
|---|
| 203 | i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | return EOK;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * Parse input report.
|
|---|
| 215 | *
|
|---|
| 216 | * @param data Data for report
|
|---|
| 217 | * @param size Size of report
|
|---|
| 218 | * @param callbacks Callbacks for report actions
|
|---|
| 219 | * @param arg Custom arguments
|
|---|
| 220 | *
|
|---|
| 221 | * @return Error code
|
|---|
| 222 | */
|
|---|
| 223 | int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
|
|---|
| 224 | const usb_hid_report_in_callbacks_t *callbacks, void *arg)
|
|---|
| 225 | {
|
|---|
| 226 | int i;
|
|---|
| 227 | usb_hid_report_item_t item;
|
|---|
| 228 |
|
|---|
| 229 | /* fill item due to the boot protocol report descriptor */
|
|---|
| 230 | // modifier keys are in the first byte
|
|---|
| 231 | uint8_t modifiers = data[0];
|
|---|
| 232 |
|
|---|
| 233 | item.offset = 2; /* second byte is reserved */
|
|---|
| 234 | item.size = 8;
|
|---|
| 235 | item.count = 6;
|
|---|
| 236 | item.usage_minimum = 0;
|
|---|
| 237 | item.usage_maximum = 255;
|
|---|
| 238 | item.logical_minimum = 0;
|
|---|
| 239 | item.logical_maximum = 255;
|
|---|
| 240 |
|
|---|
| 241 | if (size != 8) {
|
|---|
| 242 | return -1; //ERANGE;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | uint8_t keys[6];
|
|---|
| 246 | for (i = 0; i < item.count; i++) {
|
|---|
| 247 | keys[i] = data[i + item.offset];
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | callbacks->keyboard(keys, 6, modifiers, arg);
|
|---|
| 251 | return EOK;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /**
|
|---|
| 255 | * Makes output report for keyboard boot protocol
|
|---|
| 256 | *
|
|---|
| 257 | * @param leds
|
|---|
| 258 | * @param output Output report data buffer
|
|---|
| 259 | * @param size Size of the output buffer
|
|---|
| 260 | * @return Error code
|
|---|
| 261 | */
|
|---|
| 262 | int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
|
|---|
| 263 | {
|
|---|
| 264 | if(size != 1){
|
|---|
| 265 | return -1;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /* used only first five bits, others are only padding*/
|
|---|
| 269 | *data = leds;
|
|---|
| 270 | return EOK;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /**
|
|---|
| 274 | *
|
|---|
| 275 | * @param Tag to parse
|
|---|
| 276 | * @param Report descriptor buffer
|
|---|
| 277 | * @param Size of data belongs to this tag
|
|---|
| 278 | * @param Current report item structe
|
|---|
| 279 | * @return Code of action to be done next
|
|---|
| 280 | */
|
|---|
| 281 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
|---|
| 282 | usb_hid_report_item_t *report_item)
|
|---|
| 283 | {
|
|---|
| 284 | int ret;
|
|---|
| 285 |
|
|---|
| 286 | switch(class){
|
|---|
| 287 | case USB_HID_TAG_CLASS_MAIN:
|
|---|
| 288 |
|
|---|
| 289 | if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item)) == EOK) {
|
|---|
| 290 | return USB_HID_NEW_REPORT_ITEM;
|
|---|
| 291 | }
|
|---|
| 292 | else {
|
|---|
| 293 | /*TODO process the error */
|
|---|
| 294 | return ret;
|
|---|
| 295 | }
|
|---|
| 296 | break;
|
|---|
| 297 |
|
|---|
| 298 | case USB_HID_TAG_CLASS_GLOBAL:
|
|---|
| 299 | return usb_hid_report_parse_global_tag(tag,data,item_size,report_item);
|
|---|
| 300 | break;
|
|---|
| 301 |
|
|---|
| 302 | case USB_HID_TAG_CLASS_LOCAL:
|
|---|
| 303 | return usb_hid_report_parse_local_tag(tag,data,item_size,report_item);
|
|---|
| 304 | break;
|
|---|
| 305 | default:
|
|---|
| 306 | return USB_HID_NO_ACTION;
|
|---|
| 307 | }
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /**
|
|---|
| 311 | * Parse main tags of report descriptor
|
|---|
| 312 | *
|
|---|
| 313 | * @param Tag identifier
|
|---|
| 314 | * @param Data buffer
|
|---|
| 315 | * @param Length of data buffer
|
|---|
| 316 | * @param Current state table
|
|---|
| 317 | * @return Error code
|
|---|
| 318 | */
|
|---|
| 319 |
|
|---|
| 320 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 321 | usb_hid_report_item_t *report_item)
|
|---|
| 322 | {
|
|---|
| 323 | switch(tag)
|
|---|
| 324 | {
|
|---|
| 325 | case USB_HID_REPORT_TAG_INPUT:
|
|---|
| 326 | case USB_HID_REPORT_TAG_OUTPUT:
|
|---|
| 327 | case USB_HID_REPORT_TAG_FEATURE:
|
|---|
| 328 | report_item->item_flags = *data;
|
|---|
| 329 | return EOK;
|
|---|
| 330 | break;
|
|---|
| 331 |
|
|---|
| 332 | case USB_HID_REPORT_TAG_COLLECTION:
|
|---|
| 333 | // TODO
|
|---|
| 334 | return USB_HID_NO_ACTION;
|
|---|
| 335 | break;
|
|---|
| 336 |
|
|---|
| 337 | case USB_HID_REPORT_TAG_END_COLLECTION:
|
|---|
| 338 | /* should be ignored */
|
|---|
| 339 | return USB_HID_NO_ACTION;
|
|---|
| 340 | break;
|
|---|
| 341 | default:
|
|---|
| 342 | return USB_HID_NO_ACTION;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | return EOK;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | /**
|
|---|
| 349 | * Parse global tags of report descriptor
|
|---|
| 350 | *
|
|---|
| 351 | * @param Tag identifier
|
|---|
| 352 | * @param Data buffer
|
|---|
| 353 | * @param Length of data buffer
|
|---|
| 354 | * @param Current state table
|
|---|
| 355 | * @return Error code
|
|---|
| 356 | */
|
|---|
| 357 |
|
|---|
| 358 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 359 | usb_hid_report_item_t *report_item)
|
|---|
| 360 | {
|
|---|
| 361 | // TODO take care about the bit length of data
|
|---|
| 362 | switch(tag)
|
|---|
| 363 | {
|
|---|
| 364 | case USB_HID_REPORT_TAG_USAGE_PAGE:
|
|---|
| 365 | report_item->usage_page = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 366 | break;
|
|---|
| 367 | case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
|
|---|
| 368 | report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 369 | break;
|
|---|
| 370 | case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
|
|---|
| 371 | report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 372 | break;
|
|---|
| 373 | case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
|
|---|
| 374 | report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 375 | break;
|
|---|
| 376 | case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
|
|---|
| 377 | report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 378 | break;
|
|---|
| 379 | case USB_HID_REPORT_TAG_UNIT_EXPONENT:
|
|---|
| 380 | report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 381 | break;
|
|---|
| 382 | case USB_HID_REPORT_TAG_UNIT:
|
|---|
| 383 | report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 384 | break;
|
|---|
| 385 | case USB_HID_REPORT_TAG_REPORT_SIZE:
|
|---|
| 386 | report_item->size = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 387 | break;
|
|---|
| 388 | case USB_HID_REPORT_TAG_REPORT_COUNT:
|
|---|
| 389 | report_item->count = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 390 | break;
|
|---|
| 391 | case USB_HID_REPORT_TAG_REPORT_ID:
|
|---|
| 392 | report_item->id = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 393 | break;
|
|---|
| 394 | case USB_HID_REPORT_TAG_PUSH:
|
|---|
| 395 | case USB_HID_REPORT_TAG_POP:
|
|---|
| 396 | return tag;
|
|---|
| 397 | break;
|
|---|
| 398 |
|
|---|
| 399 | default:
|
|---|
| 400 | return USB_HID_NO_ACTION;
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | return EOK;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | /**
|
|---|
| 407 | * Parse local tags of report descriptor
|
|---|
| 408 | *
|
|---|
| 409 | * @param Tag identifier
|
|---|
| 410 | * @param Data buffer
|
|---|
| 411 | * @param Length of data buffer
|
|---|
| 412 | * @param Current state table
|
|---|
| 413 | * @return Error code
|
|---|
| 414 | */
|
|---|
| 415 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| 416 | usb_hid_report_item_t *report_item)
|
|---|
| 417 | {
|
|---|
| 418 | switch(tag)
|
|---|
| 419 | {
|
|---|
| 420 | case USB_HID_REPORT_TAG_USAGE:
|
|---|
| 421 | report_item->usage = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 422 | break;
|
|---|
| 423 | case USB_HID_REPORT_TAG_USAGE_MINIMUM:
|
|---|
| 424 | report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 425 | break;
|
|---|
| 426 | case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
|
|---|
| 427 | report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 428 | break;
|
|---|
| 429 | case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
|
|---|
| 430 | report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 431 | break;
|
|---|
| 432 | case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
|
|---|
| 433 | report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 434 | break;
|
|---|
| 435 | case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
|
|---|
| 436 | report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 437 | break;
|
|---|
| 438 | case USB_HID_REPORT_TAG_STRING_INDEX:
|
|---|
| 439 | report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 440 | break;
|
|---|
| 441 | case USB_HID_REPORT_TAG_STRING_MINIMUM:
|
|---|
| 442 | report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 443 | break;
|
|---|
| 444 | case USB_HID_REPORT_TAG_STRING_MAXIMUM:
|
|---|
| 445 | report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 446 | break;
|
|---|
| 447 | case USB_HID_REPORT_TAG_DELIMITER:
|
|---|
| 448 | report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 449 | break;
|
|---|
| 450 |
|
|---|
| 451 | default:
|
|---|
| 452 | return USB_HID_NO_ACTION;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | return EOK;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | /**
|
|---|
| 459 | * Converts raw data to int32 (thats the maximum length of short item data)
|
|---|
| 460 | *
|
|---|
| 461 | * @param Data buffer
|
|---|
| 462 | * @param Size of buffer
|
|---|
| 463 | * @return Converted int32 number
|
|---|
| 464 | */
|
|---|
| 465 | int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
|
|---|
| 466 | {
|
|---|
| 467 | unsigned int i;
|
|---|
| 468 | int32_t result;
|
|---|
| 469 |
|
|---|
| 470 | result = 0;
|
|---|
| 471 | for(i=0; i<size; i++) {
|
|---|
| 472 | result = (result | (data[i]) << (i*8));
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | return result;
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 | /**
|
|---|
| 481 | * Prints content of given list of report items.
|
|---|
| 482 | *
|
|---|
| 483 | * @param List of report items
|
|---|
| 484 | * @return void
|
|---|
| 485 | */
|
|---|
| 486 | void usb_hid_descriptor_print_list(link_t *head)
|
|---|
| 487 | {
|
|---|
| 488 | usb_hid_report_item_t *report_item;
|
|---|
| 489 | link_t *item;
|
|---|
| 490 |
|
|---|
| 491 | if(head == NULL || list_empty(head)) {
|
|---|
| 492 | usb_log_debug("\tempty\n");
|
|---|
| 493 | return;
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | for(item = head->next; item != head; item = item->next) {
|
|---|
| 497 |
|
|---|
| 498 | report_item = list_get_instance(item, usb_hid_report_item_t, link);
|
|---|
| 499 |
|
|---|
| 500 | usb_log_debug("\tOFFSET: %X\n", report_item->offset);
|
|---|
| 501 | usb_log_debug("\tCOUNT: %X\n", report_item->count);
|
|---|
| 502 | usb_log_debug("\tSIZE: %X\n", report_item->size);
|
|---|
| 503 | usb_log_debug("\tCONSTANT: %X\n", USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags));
|
|---|
| 504 | usb_log_debug("\tUSAGE: %X\n", report_item->usage);
|
|---|
| 505 | usb_log_debug("\tUSAGE PAGE: %X\n", report_item->usage_page);
|
|---|
| 506 | usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum);
|
|---|
| 507 | usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum);
|
|---|
| 508 | usb_log_debug("\tPHYMIN: %X\n", report_item->physical_minimum);
|
|---|
| 509 | usb_log_debug("\tPHYMAX: %X\n", report_item->physical_maximum);
|
|---|
| 510 | usb_log_debug("\n");
|
|---|
| 511 |
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 | }
|
|---|
| 516 | /**
|
|---|
| 517 | * Prints content of given descriptor in human readable format.
|
|---|
| 518 | *
|
|---|
| 519 | * @param Parsed descriptor to print
|
|---|
| 520 | * @return void
|
|---|
| 521 | */
|
|---|
| 522 | void usb_hid_descriptor_print(usb_hid_report_parser_t *parser)
|
|---|
| 523 | {
|
|---|
| 524 | usb_log_debug("INPUT:\n");
|
|---|
| 525 | usb_hid_descriptor_print_list(&parser->input);
|
|---|
| 526 |
|
|---|
| 527 | usb_log_debug("OUTPUT: \n");
|
|---|
| 528 | usb_hid_descriptor_print_list(&parser->output);
|
|---|
| 529 |
|
|---|
| 530 | usb_log_debug("FEATURE:\n");
|
|---|
| 531 | usb_hid_descriptor_print_list(&parser->feature);
|
|---|
| 532 |
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | /**
|
|---|
| 536 | * Releases whole linked list of report items
|
|---|
| 537 | *
|
|---|
| 538 | *
|
|---|
| 539 | */
|
|---|
| 540 | void usb_hid_free_report_list(link_t *head)
|
|---|
| 541 | {
|
|---|
| 542 | return;
|
|---|
| 543 |
|
|---|
| 544 | usb_hid_report_item_t *report_item;
|
|---|
| 545 | link_t *next;
|
|---|
| 546 |
|
|---|
| 547 | if(head == NULL || list_empty(head)) {
|
|---|
| 548 | return;
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | next = head->next;
|
|---|
| 552 | while(next != head) {
|
|---|
| 553 |
|
|---|
| 554 | report_item = list_get_instance(next, usb_hid_report_item_t, link);
|
|---|
| 555 | next = next->next;
|
|---|
| 556 |
|
|---|
| 557 | free(report_item);
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | return;
|
|---|
| 561 |
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | /** Free the HID report parser structure
|
|---|
| 565 | *
|
|---|
| 566 | * @param parser Opaque HID report parser structure
|
|---|
| 567 | * @return Error code
|
|---|
| 568 | */
|
|---|
| 569 | void usb_hid_free_report_parser(usb_hid_report_parser_t *parser)
|
|---|
| 570 | {
|
|---|
| 571 | if(parser == NULL){
|
|---|
| 572 | return;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | usb_hid_free_report_list(&parser->input);
|
|---|
| 576 | usb_hid_free_report_list(&parser->output);
|
|---|
| 577 | usb_hid_free_report_list(&parser->feature);
|
|---|
| 578 |
|
|---|
| 579 | return;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | /** Parse and act upon a HID report.
|
|---|
| 583 | *
|
|---|
| 584 | * @see usb_hid_parse_report_descriptor
|
|---|
| 585 | *
|
|---|
| 586 | * @param parser Opaque HID report parser structure.
|
|---|
| 587 | * @param data Data for the report.
|
|---|
| 588 | * @param callbacks Callbacks for report actions.
|
|---|
| 589 | * @param arg Custom argument (passed through to the callbacks).
|
|---|
| 590 | * @return Error code.
|
|---|
| 591 | */
|
|---|
| 592 | int usb_hid_parse_report(const usb_hid_report_parser_t *parser,
|
|---|
| 593 | const uint8_t *data, size_t size,
|
|---|
| 594 | const usb_hid_report_in_callbacks_t *callbacks, void *arg)
|
|---|
| 595 | {
|
|---|
| 596 | /*
|
|---|
| 597 | *
|
|---|
| 598 | * only key codes (usage page 0x07) will be processed
|
|---|
| 599 | * other usages will be ignored
|
|---|
| 600 | */
|
|---|
| 601 | link_t *list_item;
|
|---|
| 602 | usb_hid_report_item_t *item;
|
|---|
| 603 | uint8_t *keys;
|
|---|
| 604 | size_t key_count=0;
|
|---|
| 605 | size_t i=0;
|
|---|
| 606 | size_t j=0;
|
|---|
| 607 |
|
|---|
| 608 | // get the size of result keycodes array
|
|---|
| 609 | list_item = parser->input.next;
|
|---|
| 610 | while(list_item != &(parser->input)) {
|
|---|
| 611 |
|
|---|
| 612 | item = list_get_instance(list_item, usb_hid_report_item_t, link);
|
|---|
| 613 | if(item->usage_page == BAD_HACK_USAGE_PAGE) {
|
|---|
| 614 | key_count += item->count;
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | list_item = list_item->next;
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 | if(!(keys = malloc(sizeof(uint8_t) * key_count))){
|
|---|
| 622 | return ENOMEM;
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | // read data
|
|---|
| 626 | list_item = parser->input.next;
|
|---|
| 627 | while(list_item != &(parser->input)) {
|
|---|
| 628 |
|
|---|
| 629 | item = list_get_instance(list_item, usb_hid_report_item_t, link);
|
|---|
| 630 | if(item->usage_page == BAD_HACK_USAGE_PAGE) {
|
|---|
| 631 | for(j=0; j<(size_t)(item->count); j++) {
|
|---|
| 632 | keys[i++] = usb_hid_translate_data(item, data,j);
|
|---|
| 633 | }
|
|---|
| 634 | }
|
|---|
| 635 | list_item = list_item->next;
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | callbacks->keyboard(keys, key_count, 0, arg);
|
|---|
| 639 |
|
|---|
| 640 | free(keys);
|
|---|
| 641 | return EOK;
|
|---|
| 642 |
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 | int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j)
|
|---|
| 647 | {
|
|---|
| 648 | int resolution;
|
|---|
| 649 | int offset;
|
|---|
| 650 | int part_size;
|
|---|
| 651 |
|
|---|
| 652 | int32_t value;
|
|---|
| 653 | int32_t mask;
|
|---|
| 654 | const uint8_t *foo;
|
|---|
| 655 |
|
|---|
| 656 | // now only common numbers llowed
|
|---|
| 657 | if(item->size > 32) {
|
|---|
| 658 | return 0;
|
|---|
| 659 | }
|
|---|
| 660 |
|
|---|
| 661 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
|
|---|
| 662 | item->physical_minimum = item->logical_minimum;
|
|---|
| 663 | item->physical_maximum = item->logical_maximum;
|
|---|
| 664 | }
|
|---|
| 665 |
|
|---|
| 666 | if(item->physical_maximum == item->physical_minimum){
|
|---|
| 667 | resolution = 1;
|
|---|
| 668 | }
|
|---|
| 669 | else {
|
|---|
| 670 | resolution = (item->logical_maximum - item->logical_minimum) /
|
|---|
| 671 | ((item->physical_maximum - item->physical_minimum) *
|
|---|
| 672 | (usb_pow(10,(item->unit_exponent))));
|
|---|
| 673 | }
|
|---|
| 674 | offset = item->offset + (j * item->size);
|
|---|
| 675 |
|
|---|
| 676 | // FIXME
|
|---|
| 677 | if((offset/8) != ((offset+item->size)/8)) {
|
|---|
| 678 | usb_log_debug2("offset %d\n", offset);
|
|---|
| 679 |
|
|---|
| 680 | part_size = ((offset+item->size)%8);
|
|---|
| 681 | usb_log_debug2("part size %d\n",part_size);
|
|---|
| 682 |
|
|---|
| 683 | // the higher one
|
|---|
| 684 | foo = data+(offset/8);
|
|---|
| 685 | mask = ((1 << (item->size-part_size))-1);
|
|---|
| 686 | value = (*foo & mask) << part_size;
|
|---|
| 687 |
|
|---|
| 688 | usb_log_debug2("hfoo %x\n", *foo);
|
|---|
| 689 | usb_log_debug2("hmaska %x\n", mask);
|
|---|
| 690 | usb_log_debug2("hval %d\n", value);
|
|---|
| 691 |
|
|---|
| 692 | // the lower one
|
|---|
| 693 | foo = data+((offset+item->size)/8);
|
|---|
| 694 | mask = ((1 << part_size)-1) << (8-part_size);
|
|---|
| 695 | value += ((*foo & mask) >> (8-part_size));
|
|---|
| 696 |
|
|---|
| 697 | usb_log_debug2("lfoo %x\n", *foo);
|
|---|
| 698 | usb_log_debug2("lmaska %x\n", mask);
|
|---|
| 699 | usb_log_debug2("lval %d\n", ((*foo & mask) >> (8-(item->size-part_size))));
|
|---|
| 700 | usb_log_debug2("val %d\n", value);
|
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 | }
|
|---|
| 704 | else {
|
|---|
| 705 | foo = data+(offset/8);
|
|---|
| 706 | mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
|
|---|
| 707 | value = (*foo & mask) >> (8-((offset%8)+item->size));
|
|---|
| 708 |
|
|---|
| 709 | usb_log_debug2("offset %d\n", offset);
|
|---|
| 710 |
|
|---|
| 711 | usb_log_debug2("foo %x\n", *foo);
|
|---|
| 712 | usb_log_debug2("maska %x\n", mask);
|
|---|
| 713 | usb_log_debug2("val %d\n", value);
|
|---|
| 714 | }
|
|---|
| 715 |
|
|---|
| 716 | usb_log_debug2("---\n\n");
|
|---|
| 717 |
|
|---|
| 718 | return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
|
|---|
| 719 |
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | int usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
|
|---|
| 723 | const usb_hid_report_path_t *path)
|
|---|
| 724 | {
|
|---|
| 725 | int ret = 0;
|
|---|
| 726 | link_t *item;
|
|---|
| 727 | usb_hid_report_item_t *report_item;
|
|---|
| 728 |
|
|---|
| 729 | item = (&parser->input)->next;
|
|---|
| 730 | while(&parser->input != item) {
|
|---|
| 731 | report_item = list_get_instance(item, usb_hid_report_item_t, link);
|
|---|
| 732 | if(report_item->usage_page == path->usage_page) {
|
|---|
| 733 | ret += report_item->count;
|
|---|
| 734 | }
|
|---|
| 735 |
|
|---|
| 736 | item = item->next;
|
|---|
| 737 | }
|
|---|
| 738 |
|
|---|
| 739 | return ret;
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 | /**
|
|---|
| 745 | * @}
|
|---|
| 746 | */
|
|---|