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