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