[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);
|
---|
[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 |
|
---|
| 108 | return EOK;
|
---|
[976f546] | 109 | }
|
---|
| 110 |
|
---|
[bf2063e9] | 111 |
|
---|
| 112 | /** Parse HID report descriptor.
|
---|
| 113 | *
|
---|
| 114 | * @param parser Opaque HID report parser structure.
|
---|
| 115 | * @param data Data describing the report.
|
---|
| 116 | * @return Error code.
|
---|
| 117 | */
|
---|
| 118 | int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
|
---|
[b7d9606] | 119 | const uint8_t *data, size_t size)
|
---|
[bf2063e9] | 120 | {
|
---|
[976f546] | 121 | size_t i=0;
|
---|
| 122 | uint8_t tag=0;
|
---|
| 123 | uint8_t item_size=0;
|
---|
| 124 | int class=0;
|
---|
| 125 | int ret;
|
---|
| 126 | usb_hid_report_item_t *report_item=0;
|
---|
[b53d3b7] | 127 | usb_hid_report_item_t *new_report_item;
|
---|
| 128 | usb_hid_report_path_t *usage_path;
|
---|
| 129 | usb_hid_report_path_t *tmp_usage_path;
|
---|
[c7a2e7e] | 130 |
|
---|
[60a228f] | 131 | size_t offset_input=0;
|
---|
| 132 | size_t offset_output=0;
|
---|
| 133 | size_t offset_feature=0;
|
---|
[b53d3b7] | 134 |
|
---|
[55e388a1] | 135 |
|
---|
[b53d3b7] | 136 | /* parser structure initialization*/
|
---|
[55e388a1] | 137 | if(usb_hid_parser_init(parser) != EOK) {
|
---|
| 138 | return EINVAL;
|
---|
| 139 | }
|
---|
[976f546] | 140 |
|
---|
| 141 |
|
---|
[b53d3b7] | 142 | /*report item initialization*/
|
---|
[976f546] | 143 | if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
|
---|
| 144 | return ENOMEM;
|
---|
| 145 | }
|
---|
[60a228f] | 146 | memset(report_item, 0, sizeof(usb_hid_report_item_t));
|
---|
[b53d3b7] | 147 | list_initialize(&(report_item->link));
|
---|
[8bec4d1] | 148 |
|
---|
[b53d3b7] | 149 | /* usage path context initialization */
|
---|
| 150 | if(!(usage_path=usb_hid_report_path())){
|
---|
| 151 | return ENOMEM;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[b7d9606] | 154 | while(i<size){
|
---|
[976f546] | 155 | if(!USB_HID_ITEM_IS_LONG(data[i])){
|
---|
[b7d9606] | 156 |
|
---|
[8bec4d1] | 157 | if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
|
---|
[55e388a1] | 158 | return EINVAL; // TODO ERROR CODE
|
---|
[b7d9606] | 159 | }
|
---|
| 160 |
|
---|
[976f546] | 161 | tag = USB_HID_ITEM_TAG(data[i]);
|
---|
| 162 | item_size = USB_HID_ITEM_SIZE(data[i]);
|
---|
| 163 | class = USB_HID_ITEM_TAG_CLASS(data[i]);
|
---|
[b7d9606] | 164 |
|
---|
| 165 | usb_log_debug2(
|
---|
| 166 | "i(%u) data(%X) value(%X): TAG %u, class %u, size %u - ", i,
|
---|
| 167 | data[i], usb_hid_report_tag_data_int32(data+i+1,item_size),
|
---|
| 168 | tag, class, item_size);
|
---|
| 169 |
|
---|
| 170 | ret = usb_hid_report_parse_tag(tag,class,data+i+1,
|
---|
[b53d3b7] | 171 | item_size,report_item, usage_path);
|
---|
[fad14d7] | 172 | usb_log_debug2("ret: %u\n", ret);
|
---|
[976f546] | 173 | switch(ret){
|
---|
| 174 | case USB_HID_NEW_REPORT_ITEM:
|
---|
| 175 | // store report item to report and create the new one
|
---|
[b53d3b7] | 176 | usb_log_debug("\nNEW REPORT ITEM: %X",ret);
|
---|
| 177 |
|
---|
| 178 | // store current usage path
|
---|
| 179 | report_item->usage_path = usage_path;
|
---|
| 180 |
|
---|
[96bfe76] | 181 | // clone path to the new one
|
---|
| 182 | tmp_usage_path = usb_hid_report_path_clone(usage_path);
|
---|
[b53d3b7] | 183 |
|
---|
| 184 | // swap
|
---|
| 185 | usage_path = tmp_usage_path;
|
---|
| 186 | tmp_usage_path = NULL;
|
---|
| 187 |
|
---|
[fad14d7] | 188 |
|
---|
[976f546] | 189 | switch(tag) {
|
---|
| 190 | case USB_HID_REPORT_TAG_INPUT:
|
---|
[60a228f] | 191 | report_item->offset = offset_input;
|
---|
| 192 | offset_input += report_item->count * report_item->size;
|
---|
[b7d9606] | 193 | usb_log_debug(" - INPUT\n");
|
---|
[976f546] | 194 | list_append(&(report_item->link), &(parser->input));
|
---|
| 195 | break;
|
---|
| 196 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
[60a228f] | 197 | report_item->offset = offset_output;
|
---|
| 198 | offset_output += report_item->count * report_item->size;
|
---|
[b7d9606] | 199 | usb_log_debug(" - OUTPUT\n");
|
---|
[976f546] | 200 | list_append(&(report_item->link), &(parser->output));
|
---|
| 201 |
|
---|
| 202 | break;
|
---|
| 203 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
[60a228f] | 204 | report_item->offset = offset_feature;
|
---|
| 205 | offset_feature += report_item->count * report_item->size;
|
---|
[b7d9606] | 206 | usb_log_debug(" - FEATURE\n");
|
---|
[976f546] | 207 | list_append(&(report_item->link), &(parser->feature));
|
---|
| 208 | break;
|
---|
| 209 | default:
|
---|
[b7d9606] | 210 | usb_log_debug("\tjump over - tag %X\n", tag);
|
---|
[976f546] | 211 | break;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /* clone current state table to the new item */
|
---|
| 215 | if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
|
---|
| 216 | return ENOMEM;
|
---|
| 217 | }
|
---|
| 218 | memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));
|
---|
[c32688d] | 219 | /* reset local items */
|
---|
| 220 | new_report_item->usage_minimum = 0;
|
---|
| 221 | new_report_item->usage_maximum = 0;
|
---|
| 222 |
|
---|
[976f546] | 223 | link_initialize(&(new_report_item->link));
|
---|
| 224 | report_item = new_report_item;
|
---|
[b53d3b7] | 225 |
|
---|
[976f546] | 226 | break;
|
---|
| 227 | case USB_HID_REPORT_TAG_PUSH:
|
---|
| 228 | // push current state to stack
|
---|
| 229 | // not yet implemented
|
---|
| 230 | break;
|
---|
| 231 | case USB_HID_REPORT_TAG_POP:
|
---|
| 232 | // restore current state from stack
|
---|
| 233 | // not yet implemented
|
---|
| 234 | break;
|
---|
| 235 |
|
---|
| 236 | default:
|
---|
[b7d9606] | 237 | // nothing special to do
|
---|
[976f546] | 238 | break;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /* jump over the processed block */
|
---|
| 242 | i += 1 + USB_HID_ITEM_SIZE(data[i]);
|
---|
| 243 | }
|
---|
| 244 | else{
|
---|
| 245 | // TBD
|
---|
| 246 | i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 |
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | return EOK;
|
---|
[bf2063e9] | 253 | }
|
---|
| 254 |
|
---|
[2f60e57d] | 255 |
|
---|
| 256 | /**
|
---|
| 257 | * Parse input report.
|
---|
| 258 | *
|
---|
| 259 | * @param data Data for report
|
---|
| 260 | * @param size Size of report
|
---|
| 261 | * @param callbacks Callbacks for report actions
|
---|
| 262 | * @param arg Custom arguments
|
---|
| 263 | *
|
---|
| 264 | * @return Error code
|
---|
| 265 | */
|
---|
[b7d9606] | 266 | int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
|
---|
[2f60e57d] | 267 | const usb_hid_report_in_callbacks_t *callbacks, void *arg)
|
---|
| 268 | {
|
---|
| 269 | int i;
|
---|
| 270 | usb_hid_report_item_t item;
|
---|
| 271 |
|
---|
| 272 | /* fill item due to the boot protocol report descriptor */
|
---|
| 273 | // modifier keys are in the first byte
|
---|
| 274 | uint8_t modifiers = data[0];
|
---|
| 275 |
|
---|
| 276 | item.offset = 2; /* second byte is reserved */
|
---|
| 277 | item.size = 8;
|
---|
| 278 | item.count = 6;
|
---|
[976f546] | 279 | item.usage_minimum = 0;
|
---|
| 280 | item.usage_maximum = 255;
|
---|
| 281 | item.logical_minimum = 0;
|
---|
| 282 | item.logical_maximum = 255;
|
---|
[2f60e57d] | 283 |
|
---|
[976f546] | 284 | if (size != 8) {
|
---|
[e24e7b1] | 285 | return -1; //ERANGE;
|
---|
[2f60e57d] | 286 | }
|
---|
| 287 |
|
---|
| 288 | uint8_t keys[6];
|
---|
[976f546] | 289 | for (i = 0; i < item.count; i++) {
|
---|
| 290 | keys[i] = data[i + item.offset];
|
---|
[2f60e57d] | 291 | }
|
---|
| 292 |
|
---|
| 293 | callbacks->keyboard(keys, 6, modifiers, arg);
|
---|
[e7726a4] | 294 | return EOK;
|
---|
[bf2063e9] | 295 | }
|
---|
| 296 |
|
---|
[2f60e57d] | 297 | /**
|
---|
| 298 | * Makes output report for keyboard boot protocol
|
---|
| 299 | *
|
---|
| 300 | * @param leds
|
---|
| 301 | * @param output Output report data buffer
|
---|
| 302 | * @param size Size of the output buffer
|
---|
| 303 | * @return Error code
|
---|
| 304 | */
|
---|
| 305 | int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
|
---|
| 306 | {
|
---|
| 307 | if(size != 1){
|
---|
| 308 | return -1;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | /* used only first five bits, others are only padding*/
|
---|
| 312 | *data = leds;
|
---|
| 313 | return EOK;
|
---|
| 314 | }
|
---|
[bf2063e9] | 315 |
|
---|
[976f546] | 316 | /**
|
---|
[57d9c05e] | 317 | * Parse one tag of the report descriptor
|
---|
[976f546] | 318 | *
|
---|
| 319 | * @param Tag to parse
|
---|
| 320 | * @param Report descriptor buffer
|
---|
| 321 | * @param Size of data belongs to this tag
|
---|
| 322 | * @param Current report item structe
|
---|
| 323 | * @return Code of action to be done next
|
---|
| 324 | */
|
---|
[b7d9606] | 325 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
---|
[b53d3b7] | 326 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
[976f546] | 327 | {
|
---|
[b7d9606] | 328 | int ret;
|
---|
| 329 |
|
---|
[976f546] | 330 | switch(class){
|
---|
| 331 | case USB_HID_TAG_CLASS_MAIN:
|
---|
| 332 |
|
---|
[b53d3b7] | 333 | if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
|
---|
[976f546] | 334 | return USB_HID_NEW_REPORT_ITEM;
|
---|
| 335 | }
|
---|
| 336 | else {
|
---|
| 337 | /*TODO process the error */
|
---|
[b7d9606] | 338 | return ret;
|
---|
[976f546] | 339 | }
|
---|
| 340 | break;
|
---|
| 341 |
|
---|
| 342 | case USB_HID_TAG_CLASS_GLOBAL:
|
---|
[b53d3b7] | 343 | return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
|
---|
[976f546] | 344 | break;
|
---|
| 345 |
|
---|
| 346 | case USB_HID_TAG_CLASS_LOCAL:
|
---|
[b53d3b7] | 347 | return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
|
---|
[976f546] | 348 | break;
|
---|
| 349 | default:
|
---|
[b7d9606] | 350 | return USB_HID_NO_ACTION;
|
---|
[976f546] | 351 | }
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /**
|
---|
| 355 | * Parse main tags of report descriptor
|
---|
| 356 | *
|
---|
| 357 | * @param Tag identifier
|
---|
| 358 | * @param Data buffer
|
---|
| 359 | * @param Length of data buffer
|
---|
| 360 | * @param Current state table
|
---|
| 361 | * @return Error code
|
---|
| 362 | */
|
---|
| 363 |
|
---|
[c7a2e7e] | 364 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
---|
[b53d3b7] | 365 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
---|
| 366 | {
|
---|
[976f546] | 367 | switch(tag)
|
---|
| 368 | {
|
---|
| 369 | case USB_HID_REPORT_TAG_INPUT:
|
---|
| 370 | case USB_HID_REPORT_TAG_OUTPUT:
|
---|
| 371 | case USB_HID_REPORT_TAG_FEATURE:
|
---|
[b7d9606] | 372 | report_item->item_flags = *data;
|
---|
| 373 | return EOK;
|
---|
[976f546] | 374 | break;
|
---|
| 375 |
|
---|
| 376 | case USB_HID_REPORT_TAG_COLLECTION:
|
---|
[b53d3b7] | 377 | usb_hid_report_path_append_item(usage_path, 0, 0);
|
---|
| 378 |
|
---|
[8bec4d1] | 379 | return USB_HID_NO_ACTION;
|
---|
[976f546] | 380 | break;
|
---|
| 381 |
|
---|
| 382 | case USB_HID_REPORT_TAG_END_COLLECTION:
|
---|
[b53d3b7] | 383 | // TODO
|
---|
| 384 | // znici posledni uroven ve vsech usage paths
|
---|
| 385 | // otazka jestli nema nicit dve, respektive novou posledni vynulovat?
|
---|
| 386 | usb_hid_report_remove_last_item(usage_path);
|
---|
[8bec4d1] | 387 | return USB_HID_NO_ACTION;
|
---|
[976f546] | 388 | break;
|
---|
| 389 | default:
|
---|
[b7d9606] | 390 | return USB_HID_NO_ACTION;
|
---|
[976f546] | 391 | }
|
---|
| 392 |
|
---|
[8bec4d1] | 393 | return EOK;
|
---|
[976f546] | 394 | }
|
---|
| 395 |
|
---|
| 396 | /**
|
---|
| 397 | * Parse global tags of report descriptor
|
---|
| 398 | *
|
---|
| 399 | * @param Tag identifier
|
---|
| 400 | * @param Data buffer
|
---|
| 401 | * @param Length of data buffer
|
---|
| 402 | * @param Current state table
|
---|
| 403 | * @return Error code
|
---|
| 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 |
|
---|
[841e6e5] | 1105 | // read the last output report item
|
---|
[57d9c05e] | 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));
|
---|
[841e6e5] | 1115 | memset(buffer, 0, sizeof(uint8_t) * (*size));
|
---|
| 1116 | usb_log_debug("output buffer: %s\n", usb_debug_str_buffer(buffer, *size, 0));
|
---|
| 1117 |
|
---|
[57d9c05e] | 1118 | return buffer;
|
---|
| 1119 | }
|
---|
| 1120 | else {
|
---|
| 1121 | *size = 0;
|
---|
| 1122 | return NULL;
|
---|
| 1123 | }
|
---|
| 1124 | }
|
---|
| 1125 |
|
---|
| 1126 |
|
---|
| 1127 | /** Frees output report buffer
|
---|
| 1128 | *
|
---|
| 1129 | * @param output Output report buffer
|
---|
| 1130 | * @return
|
---|
| 1131 | */
|
---|
| 1132 | void usb_hid_report_output_free(uint8_t *output)
|
---|
[841e6e5] | 1133 |
|
---|
[57d9c05e] | 1134 | {
|
---|
| 1135 | if(output != NULL) {
|
---|
| 1136 | free (output);
|
---|
| 1137 | }
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
| 1140 | /** Returns size of output for given usage path
|
---|
| 1141 | *
|
---|
| 1142 | * @param parser
|
---|
| 1143 | * @param path
|
---|
| 1144 | * @param flags
|
---|
| 1145 | * @return
|
---|
| 1146 | */
|
---|
| 1147 | size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
|
---|
| 1148 | usb_hid_report_path_t *path, int flags)
|
---|
| 1149 | {
|
---|
| 1150 | size_t ret = 0;
|
---|
| 1151 | link_t *item;
|
---|
| 1152 | usb_hid_report_item_t *report_item;
|
---|
| 1153 |
|
---|
| 1154 | if(parser == NULL) {
|
---|
| 1155 | return 0;
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
| 1158 | item = parser->output.next;
|
---|
[841e6e5] | 1159 | while(&parser->output != item) {
|
---|
[57d9c05e] | 1160 | report_item = list_get_instance(item, usb_hid_report_item_t, link);
|
---|
| 1161 | if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&
|
---|
| 1162 | (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {
|
---|
| 1163 | ret += report_item->count;
|
---|
| 1164 | }
|
---|
| 1165 |
|
---|
| 1166 | item = item->next;
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | return ret;
|
---|
| 1170 |
|
---|
| 1171 | }
|
---|
| 1172 |
|
---|
| 1173 | /** Updates the output report buffer by translated given data
|
---|
| 1174 | *
|
---|
| 1175 | * @param parser
|
---|
| 1176 | * @param path
|
---|
| 1177 | * @param flags
|
---|
| 1178 | * @param buffer
|
---|
| 1179 | * @param size
|
---|
| 1180 | * @param data
|
---|
| 1181 | * @param data_size
|
---|
| 1182 | * @return
|
---|
| 1183 | */
|
---|
| 1184 | int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
|
---|
| 1185 | usb_hid_report_path_t *path, int flags,
|
---|
| 1186 | uint8_t *buffer, size_t size,
|
---|
| 1187 | int32_t *data, size_t data_size)
|
---|
| 1188 | {
|
---|
| 1189 | usb_hid_report_item_t *report_item;
|
---|
| 1190 | link_t *item;
|
---|
| 1191 | size_t idx=0;
|
---|
| 1192 | int i=0;
|
---|
| 1193 | int32_t value=0;
|
---|
[841e6e5] | 1194 | int offset;
|
---|
| 1195 | int length;
|
---|
| 1196 | int32_t tmp_value;
|
---|
[57d9c05e] | 1197 |
|
---|
| 1198 | if(parser == NULL) {
|
---|
| 1199 | return EINVAL;
|
---|
| 1200 | }
|
---|
| 1201 |
|
---|
[70a71e5] | 1202 | usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
|
---|
| 1203 | usb_log_debug("OUTPUT DATA[0]: %d, DATA[1]: %d, DATA[2]: %d\n", data[0], data[1], data[2]);
|
---|
| 1204 |
|
---|
[57d9c05e] | 1205 | item = parser->output.next;
|
---|
| 1206 | while(item != &parser->output) {
|
---|
[841e6e5] | 1207 | report_item = list_get_instance(item, usb_hid_report_item_t, link);
|
---|
[57d9c05e] | 1208 |
|
---|
[d012590] | 1209 | for(i=0; i<report_item->count; i++) {
|
---|
[841e6e5] | 1210 |
|
---|
| 1211 | if(idx >= data_size) {
|
---|
| 1212 | break;
|
---|
| 1213 | }
|
---|
| 1214 |
|
---|
| 1215 | if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) ||
|
---|
| 1216 | ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
|
---|
[d012590] | 1217 |
|
---|
[841e6e5] | 1218 | // // variable item
|
---|
[70a71e5] | 1219 | value = usb_hid_translate_data_reverse(report_item, data[idx++]);
|
---|
[841e6e5] | 1220 | offset = report_item->offset + (i * report_item->size);
|
---|
| 1221 | length = report_item->size;
|
---|
| 1222 | }
|
---|
| 1223 | else {
|
---|
| 1224 | //bitmap
|
---|
[70a71e5] | 1225 | value += usb_hid_translate_data_reverse(report_item, data[idx++]);
|
---|
[841e6e5] | 1226 | offset = report_item->offset;
|
---|
| 1227 | length = report_item->size * report_item->count;
|
---|
| 1228 | }
|
---|
| 1229 |
|
---|
[d012590] | 1230 | if((offset/8) == ((offset+length-1)/8)) {
|
---|
[841e6e5] | 1231 | // je to v jednom bytu
|
---|
[d012590] | 1232 | if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
|
---|
[841e6e5] | 1233 | break; // TODO ErrorCode
|
---|
| 1234 | }
|
---|
| 1235 |
|
---|
[2f4b3a4] | 1236 | size_t shift = offset%8;
|
---|
[841e6e5] | 1237 |
|
---|
[d012590] | 1238 | value = value << shift;
|
---|
| 1239 | value = value & (((1 << length)-1) << shift);
|
---|
[70a71e5] | 1240 |
|
---|
| 1241 | uint8_t mask = 0;
|
---|
| 1242 | mask = 0xff - (((1 << length) - 1) << shift);
|
---|
| 1243 | buffer[offset/8] = (buffer[offset/8] & mask) | value;
|
---|
[841e6e5] | 1244 | }
|
---|
| 1245 | else {
|
---|
| 1246 | // je to ve dvou!! FIXME: melo by to umet delsi jak 2
|
---|
| 1247 |
|
---|
[2f4b3a4] | 1248 | // konec prvniho -- dolni x bitu
|
---|
[841e6e5] | 1249 | tmp_value = value;
|
---|
[d012590] | 1250 | tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);
|
---|
[2f4b3a4] | 1251 | tmp_value = tmp_value << (offset%8);
|
---|
[d012590] | 1252 |
|
---|
[70a71e5] | 1253 | uint8_t mask = 0;
|
---|
| 1254 | mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
|
---|
| 1255 | buffer[offset/8] = (buffer[offset/8] & mask) | tmp_value;
|
---|
[841e6e5] | 1256 |
|
---|
[2f4b3a4] | 1257 | // a ted druhej -- hornich length-x bitu
|
---|
| 1258 | value = value >> (8 - (offset % 8));
|
---|
| 1259 | value = value & ((1 << (length - (8 - (offset % 8)))) - 1);
|
---|
[d012590] | 1260 |
|
---|
[70a71e5] | 1261 | mask = ((1 << (length - (8 - (offset % 8)))) - 1);
|
---|
| 1262 | buffer[(offset+length-1)/8] = (buffer[(offset+length-1)/8] & mask) | value;
|
---|
[841e6e5] | 1263 | }
|
---|
[57d9c05e] | 1264 |
|
---|
| 1265 | }
|
---|
[841e6e5] | 1266 |
|
---|
| 1267 | item = item->next;
|
---|
[57d9c05e] | 1268 | }
|
---|
| 1269 |
|
---|
[70a71e5] | 1270 | usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
|
---|
[841e6e5] | 1271 |
|
---|
[57d9c05e] | 1272 | return EOK;
|
---|
| 1273 | }
|
---|
| 1274 |
|
---|
[841e6e5] | 1275 | /**
|
---|
| 1276 | *
|
---|
| 1277 | * @param item
|
---|
| 1278 | * @param value
|
---|
| 1279 | * @return
|
---|
| 1280 | */
|
---|
| 1281 | int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int value)
|
---|
| 1282 | {
|
---|
| 1283 | int ret=0;
|
---|
| 1284 | int resolution;
|
---|
| 1285 |
|
---|
[70a71e5] | 1286 | if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
|
---|
| 1287 | ret = item->logical_minimum;
|
---|
| 1288 | }
|
---|
| 1289 |
|
---|
[d012590] | 1290 | if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
|
---|
[841e6e5] | 1291 |
|
---|
| 1292 | // variable item
|
---|
| 1293 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
|
---|
| 1294 | item->physical_minimum = item->logical_minimum;
|
---|
| 1295 | item->physical_maximum = item->logical_maximum;
|
---|
| 1296 | }
|
---|
| 1297 |
|
---|
| 1298 | if(item->physical_maximum == item->physical_minimum){
|
---|
| 1299 | resolution = 1;
|
---|
| 1300 | }
|
---|
| 1301 | else {
|
---|
| 1302 | resolution = (item->logical_maximum - item->logical_minimum) /
|
---|
| 1303 | ((item->physical_maximum - item->physical_minimum) *
|
---|
| 1304 | (usb_pow(10,(item->unit_exponent))));
|
---|
| 1305 | }
|
---|
| 1306 |
|
---|
| 1307 | ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
|
---|
| 1308 | }
|
---|
| 1309 | else {
|
---|
| 1310 | // bitmapa
|
---|
| 1311 | if(value == 0) {
|
---|
| 1312 | ret = 0;
|
---|
| 1313 | }
|
---|
| 1314 | else {
|
---|
| 1315 | size_t bitmap_idx = (value - item->usage_minimum);
|
---|
| 1316 | ret = 1 << bitmap_idx;
|
---|
| 1317 | }
|
---|
| 1318 | }
|
---|
| 1319 |
|
---|
| 1320 |
|
---|
| 1321 | return ret;
|
---|
| 1322 | }
|
---|
| 1323 |
|
---|
[57d9c05e] | 1324 |
|
---|
[976f546] | 1325 | /**
|
---|
| 1326 | * @}
|
---|
[c7a2e7e] | 1327 | */
|
---|