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