[bf2063e9] | 1 | /*
|
---|
[2571089] | 2 | * Copyright (c) 2011 Matej Klonfar
|
---|
[bf2063e9] | 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>
|
---|
[681f24b3] | 41 | #include <assert.h>
|
---|
[976f546] | 42 |
|
---|
[175ad13e] | 43 |
|
---|
[57d9c05e] | 44 | /*
|
---|
| 45 | * Data translation private functions
|
---|
| 46 | */
|
---|
[1553cbf] | 47 | uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
|
---|
[b7d9606] | 48 | inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
|
---|
[cfbbe1d3] | 49 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
|
---|
[175ad13e] | 50 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value);
|
---|
[fad14d7] | 51 | int usb_pow(int a, int b);
|
---|
| 52 |
|
---|
[2571089] | 53 |
|
---|
[57d9c05e] | 54 | // TODO: tohle ma bejt asi jinde
|
---|
[fad14d7] | 55 | int usb_pow(int a, int b)
|
---|
| 56 | {
|
---|
| 57 | switch(b) {
|
---|
| 58 | case 0:
|
---|
| 59 | return 1;
|
---|
| 60 | break;
|
---|
| 61 | case 1:
|
---|
| 62 | return a;
|
---|
| 63 | break;
|
---|
| 64 | default:
|
---|
| 65 | return a * usb_pow(a, b-1);
|
---|
| 66 | break;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[976f546] | 70 |
|
---|
[bd2394b] | 71 | /** Returns size of report of specified report id and type in items
|
---|
| 72 | *
|
---|
| 73 | * @param parser Opaque report parser structure
|
---|
| 74 | * @param report_id
|
---|
| 75 | * @param type
|
---|
| 76 | * @return Number of items in specified report
|
---|
| 77 | */
|
---|
| 78 | size_t usb_hid_report_size(usb_hid_report_t *report, uint8_t report_id,
|
---|
| 79 | usb_hid_report_type_t type)
|
---|
| 80 | {
|
---|
| 81 | usb_hid_report_description_t *report_des;
|
---|
| 82 |
|
---|
| 83 | if(report == NULL) {
|
---|
| 84 | return 0;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | report_des = usb_hid_report_find_description (report, report_id, type);
|
---|
| 88 | if(report_des == NULL){
|
---|
| 89 | return 0;
|
---|
| 90 | }
|
---|
| 91 | else {
|
---|
| 92 | return report_des->item_length;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
[976f546] | 95 |
|
---|
[c7a2e7e] | 96 |
|
---|
[fad14d7] | 97 | /** Parse and act upon a HID report.
|
---|
| 98 | *
|
---|
| 99 | * @see usb_hid_parse_report_descriptor
|
---|
| 100 | *
|
---|
| 101 | * @param parser Opaque HID report parser structure.
|
---|
| 102 | * @param data Data for the report.
|
---|
| 103 | * @return Error code.
|
---|
| 104 | */
|
---|
[175ad13e] | 105 | int usb_hid_parse_report(const usb_hid_report_t *report,
|
---|
[cfbbe1d3] | 106 | const uint8_t *data, size_t size, uint8_t *report_id)
|
---|
[fad14d7] | 107 | {
|
---|
| 108 | link_t *list_item;
|
---|
[175ad13e] | 109 | usb_hid_report_field_t *item;
|
---|
| 110 |
|
---|
| 111 | usb_hid_report_description_t *report_des;
|
---|
| 112 | usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
|
---|
[fad14d7] | 113 |
|
---|
[175ad13e] | 114 | if(report == NULL) {
|
---|
[55e388a1] | 115 | return EINVAL;
|
---|
| 116 | }
|
---|
[c156c2d] | 117 |
|
---|
[175ad13e] | 118 | if(report->use_report_ids != 0) {
|
---|
[cfbbe1d3] | 119 | *report_id = data[0];
|
---|
| 120 | }
|
---|
| 121 | else {
|
---|
| 122 | *report_id = 0;
|
---|
[c156c2d] | 123 | }
|
---|
| 124 |
|
---|
[cfbbe1d3] | 125 |
|
---|
| 126 | report_des = usb_hid_report_find_description(report, *report_id, type);
|
---|
[c156c2d] | 127 |
|
---|
[175ad13e] | 128 | /* read data */
|
---|
| 129 | list_item = report_des->report_items.next;
|
---|
| 130 | while(list_item != &(report_des->report_items)) {
|
---|
[fad14d7] | 131 |
|
---|
[175ad13e] | 132 | item = list_get_instance(list_item, usb_hid_report_field_t, link);
|
---|
[fad14d7] | 133 |
|
---|
[cfbbe1d3] | 134 | if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
|
---|
[175ad13e] | 135 |
|
---|
[cfbbe1d3] | 136 | if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
|
---|
[175ad13e] | 137 |
|
---|
[cfbbe1d3] | 138 | // array
|
---|
| 139 | item->value = usb_hid_translate_data(item, data);
|
---|
[cc5908e] | 140 |
|
---|
| 141 | item->usage = USB_HID_EXTENDED_USAGE(item->usages[item->value - item->physical_minimum]);
|
---|
| 142 | item->usage_page = USB_HID_EXTENDED_USAGE_PAGE(item->usages[item->value - item->physical_minimum]);
|
---|
| 143 |
|
---|
[fad14d7] | 144 | }
|
---|
[175ad13e] | 145 | else {
|
---|
[cfbbe1d3] | 146 | // variable item
|
---|
| 147 | item->value = usb_hid_translate_data(item, data);
|
---|
| 148 | }
|
---|
[fad14d7] | 149 | }
|
---|
| 150 | list_item = list_item->next;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | return EOK;
|
---|
| 154 |
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[57d9c05e] | 157 | /**
|
---|
[c156c2d] | 158 | * Translate data from the report as specified in report descriptor item
|
---|
[57d9c05e] | 159 | *
|
---|
| 160 | * @param item Report descriptor item with definition of translation
|
---|
| 161 | * @param data Data to translate
|
---|
| 162 | * @param j Index of processed field in report descriptor item
|
---|
| 163 | * @return Translated data
|
---|
| 164 | */
|
---|
[cfbbe1d3] | 165 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
|
---|
[c7a2e7e] | 166 | {
|
---|
[fad14d7] | 167 | int resolution;
|
---|
| 168 | int offset;
|
---|
| 169 | int part_size;
|
---|
| 170 |
|
---|
[175ad13e] | 171 | int32_t value=0;
|
---|
[fad14d7] | 172 | int32_t mask;
|
---|
| 173 | const uint8_t *foo;
|
---|
[bda06a3] | 174 |
|
---|
[c156c2d] | 175 | // now only shot tags are allowed
|
---|
[fad14d7] | 176 | if(item->size > 32) {
|
---|
| 177 | return 0;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[cfbbe1d3] | 180 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
|
---|
[fad14d7] | 181 | item->physical_minimum = item->logical_minimum;
|
---|
[cfbbe1d3] | 182 | item->physical_maximum = item->logical_maximum;
|
---|
[fad14d7] | 183 | }
|
---|
[cfbbe1d3] | 184 |
|
---|
[fad14d7] | 185 |
|
---|
[60a228f] | 186 | if(item->physical_maximum == item->physical_minimum){
|
---|
| 187 | resolution = 1;
|
---|
| 188 | }
|
---|
| 189 | else {
|
---|
| 190 | resolution = (item->logical_maximum - item->logical_minimum) /
|
---|
| 191 | ((item->physical_maximum - item->physical_minimum) *
|
---|
| 192 | (usb_pow(10,(item->unit_exponent))));
|
---|
| 193 | }
|
---|
[bda06a3] | 194 |
|
---|
[cfbbe1d3] | 195 | offset = item->offset;
|
---|
[fad14d7] | 196 | // FIXME
|
---|
[175ad13e] | 197 | if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
|
---|
[fad14d7] | 198 |
|
---|
| 199 | part_size = ((offset+item->size)%8);
|
---|
| 200 |
|
---|
[175ad13e] | 201 | size_t i=0;
|
---|
| 202 | for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
|
---|
| 203 | if(i == (size_t)(offset/8)) {
|
---|
| 204 | // the higher one
|
---|
| 205 | foo = data + i;
|
---|
| 206 | mask = ((1 << (item->size-part_size))-1);
|
---|
| 207 | value = (*foo & mask) << part_size;
|
---|
| 208 | }
|
---|
| 209 | else if(i == ((offset+item->size-1)/8)){
|
---|
| 210 | // the lower one
|
---|
| 211 | foo = data + i;
|
---|
| 212 | mask = ((1 << part_size)-1) << (8-part_size);
|
---|
| 213 | value += ((*foo & mask) >> (8-part_size));
|
---|
| 214 | }
|
---|
| 215 | else {
|
---|
| 216 | value = value << 8;
|
---|
| 217 | value += *(data + 1);
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
[fad14d7] | 220 | }
|
---|
| 221 | else {
|
---|
| 222 | foo = data+(offset/8);
|
---|
| 223 | mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
|
---|
| 224 | value = (*foo & mask) >> (8-((offset%8)+item->size));
|
---|
[175ad13e] | 225 | }
|
---|
[fad14d7] | 226 |
|
---|
[1553cbf] | 227 | if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
|
---|
| 228 | value = USB_HID_UINT32_TO_INT32(value, item->size);
|
---|
[fad14d7] | 229 | }
|
---|
| 230 |
|
---|
[767da0a] | 231 | return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
|
---|
[fad14d7] | 232 |
|
---|
[c7a2e7e] | 233 | }
|
---|
[0bd4810c] | 234 |
|
---|
[57d9c05e] | 235 | /*** OUTPUT API **/
|
---|
| 236 |
|
---|
[a694a58] | 237 | /**
|
---|
| 238 | * Allocates output report buffer for output report
|
---|
[57d9c05e] | 239 | *
|
---|
[a694a58] | 240 | * @param parser Report parsed structure
|
---|
| 241 | * @param size Size of returned buffer
|
---|
| 242 | * @param report_id Report id of created output report
|
---|
| 243 | * @return Returns allocated output buffer for specified output
|
---|
[57d9c05e] | 244 | */
|
---|
[175ad13e] | 245 | uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id)
|
---|
[57d9c05e] | 246 | {
|
---|
[175ad13e] | 247 | if(report == NULL) {
|
---|
[57d9c05e] | 248 | *size = 0;
|
---|
| 249 | return NULL;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[175ad13e] | 252 | link_t *report_it = report->reports.next;
|
---|
| 253 | usb_hid_report_description_t *report_des = NULL;
|
---|
| 254 | while(report_it != &report->reports) {
|
---|
| 255 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
[dc9f122] | 256 | if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
|
---|
[a694a58] | 257 | break;
|
---|
[c156c2d] | 258 | }
|
---|
| 259 |
|
---|
[175ad13e] | 260 | report_it = report_it->next;
|
---|
| 261 | }
|
---|
[841e6e5] | 262 |
|
---|
[175ad13e] | 263 | if(report_des == NULL){
|
---|
| 264 | *size = 0;
|
---|
| 265 | return NULL;
|
---|
[57d9c05e] | 266 | }
|
---|
| 267 | else {
|
---|
[175ad13e] | 268 | *size = (report_des->bit_length + (8 - 1))/8;
|
---|
| 269 | uint8_t *ret = malloc((*size) * sizeof(uint8_t));
|
---|
| 270 | memset(ret, 0, (*size) * sizeof(uint8_t));
|
---|
| 271 | return ret;
|
---|
[57d9c05e] | 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | /** Frees output report buffer
|
---|
| 277 | *
|
---|
| 278 | * @param output Output report buffer
|
---|
[a694a58] | 279 | * @return void
|
---|
[57d9c05e] | 280 | */
|
---|
| 281 | void usb_hid_report_output_free(uint8_t *output)
|
---|
[841e6e5] | 282 |
|
---|
[57d9c05e] | 283 | {
|
---|
| 284 | if(output != NULL) {
|
---|
| 285 | free (output);
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[175ad13e] | 289 | /** Makes the output report buffer for data given in the report structure
|
---|
[57d9c05e] | 290 | *
|
---|
[a694a58] | 291 | * @param parser Opaque report parser structure
|
---|
| 292 | * @param path Usage path specifing which parts of output will be set
|
---|
| 293 | * @param flags Usage path structure comparison flags
|
---|
| 294 | * @param buffer Output buffer
|
---|
| 295 | * @param size Size of output buffer
|
---|
| 296 | * @return Error code
|
---|
[57d9c05e] | 297 | */
|
---|
[175ad13e] | 298 | int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
|
---|
| 299 | uint8_t *buffer, size_t size)
|
---|
[57d9c05e] | 300 | {
|
---|
| 301 | link_t *item;
|
---|
| 302 | int32_t value=0;
|
---|
[841e6e5] | 303 | int offset;
|
---|
| 304 | int length;
|
---|
| 305 | int32_t tmp_value;
|
---|
[57d9c05e] | 306 |
|
---|
[175ad13e] | 307 | if(report == NULL) {
|
---|
[57d9c05e] | 308 | return EINVAL;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[175ad13e] | 311 | if(report->use_report_ids != 0) {
|
---|
| 312 | buffer[0] = report_id;
|
---|
[bda06a3] | 313 | }
|
---|
| 314 |
|
---|
[70a71e5] | 315 | usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
|
---|
[cfbbe1d3] | 316 |
|
---|
[175ad13e] | 317 | usb_hid_report_description_t *report_des;
|
---|
| 318 | report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT);
|
---|
| 319 | if(report_des == NULL){
|
---|
| 320 | return EINVAL;
|
---|
| 321 | }
|
---|
[57d9c05e] | 322 |
|
---|
[175ad13e] | 323 | usb_hid_report_field_t *report_item;
|
---|
| 324 | item = report_des->report_items.next;
|
---|
| 325 | while(item != &report_des->report_items) {
|
---|
| 326 | report_item = list_get_instance(item, usb_hid_report_field_t, link);
|
---|
[841e6e5] | 327 |
|
---|
[cfbbe1d3] | 328 | if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
|
---|
[d012590] | 329 |
|
---|
[cfbbe1d3] | 330 | // array
|
---|
[175ad13e] | 331 | value = usb_hid_translate_data_reverse(report_item, report_item->value);
|
---|
| 332 | offset = report_item->offset;
|
---|
[841e6e5] | 333 | length = report_item->size;
|
---|
| 334 | }
|
---|
| 335 | else {
|
---|
[cfbbe1d3] | 336 | // variable item
|
---|
| 337 | value = usb_hid_translate_data_reverse(report_item, report_item->value);
|
---|
[175ad13e] | 338 | offset = report_item->offset;
|
---|
| 339 | length = report_item->size;
|
---|
[841e6e5] | 340 | }
|
---|
| 341 |
|
---|
[d012590] | 342 | if((offset/8) == ((offset+length-1)/8)) {
|
---|
[841e6e5] | 343 | // je to v jednom bytu
|
---|
[d012590] | 344 | if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
|
---|
[841e6e5] | 345 | break; // TODO ErrorCode
|
---|
| 346 | }
|
---|
| 347 |
|
---|
[3b5d5b9d] | 348 | size_t shift = 8 - offset%8 - length;
|
---|
[841e6e5] | 349 |
|
---|
[d012590] | 350 | value = value << shift;
|
---|
| 351 | value = value & (((1 << length)-1) << shift);
|
---|
[70a71e5] | 352 |
|
---|
| 353 | uint8_t mask = 0;
|
---|
| 354 | mask = 0xff - (((1 << length) - 1) << shift);
|
---|
| 355 | buffer[offset/8] = (buffer[offset/8] & mask) | value;
|
---|
[841e6e5] | 356 | }
|
---|
| 357 | else {
|
---|
[175ad13e] | 358 | int i = 0;
|
---|
[70a71e5] | 359 | uint8_t mask = 0;
|
---|
[175ad13e] | 360 | for(i = (offset/8); i <= ((offset+length-1)/8); i++) {
|
---|
| 361 | if(i == (offset/8)) {
|
---|
| 362 | tmp_value = value;
|
---|
| 363 | tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);
|
---|
| 364 | tmp_value = tmp_value << (offset%8);
|
---|
| 365 |
|
---|
| 366 | mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
|
---|
| 367 | buffer[i] = (buffer[i] & mask) | tmp_value;
|
---|
| 368 | }
|
---|
| 369 | else if (i == ((offset + length -1)/8)) {
|
---|
| 370 |
|
---|
| 371 | value = value >> (length - ((offset + length) % 8));
|
---|
| 372 | value = value & ((1 << (length - ((offset + length) % 8))) - 1);
|
---|
[d012590] | 373 |
|
---|
[175ad13e] | 374 | mask = (1 << (length - ((offset + length) % 8))) - 1;
|
---|
| 375 | buffer[i] = (buffer[i] & mask) | value;
|
---|
| 376 | }
|
---|
| 377 | else {
|
---|
| 378 | buffer[i] = value & (0xFF << i);
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
[841e6e5] | 381 | }
|
---|
[57d9c05e] | 382 |
|
---|
[841e6e5] | 383 |
|
---|
[cfbbe1d3] | 384 | // reset value
|
---|
| 385 | report_item->value = 0;
|
---|
| 386 |
|
---|
[841e6e5] | 387 | item = item->next;
|
---|
[57d9c05e] | 388 | }
|
---|
[cfbbe1d3] | 389 |
|
---|
[70a71e5] | 390 | usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
|
---|
[841e6e5] | 391 |
|
---|
[57d9c05e] | 392 | return EOK;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[841e6e5] | 395 | /**
|
---|
[a694a58] | 396 | * Translate given data for putting them into the outoput report
|
---|
| 397 | * @param item Report item structure
|
---|
| 398 | * @param value Value to translate
|
---|
| 399 | * @return ranslated value
|
---|
[841e6e5] | 400 | */
|
---|
[175ad13e] | 401 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value)
|
---|
[841e6e5] | 402 | {
|
---|
| 403 | int ret=0;
|
---|
| 404 | int resolution;
|
---|
| 405 |
|
---|
[70a71e5] | 406 | if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
|
---|
| 407 | ret = item->logical_minimum;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[cfbbe1d3] | 410 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
|
---|
| 411 | item->physical_minimum = item->logical_minimum;
|
---|
| 412 | item->physical_maximum = item->logical_maximum;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 |
|
---|
[d012590] | 416 | if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
|
---|
[841e6e5] | 417 |
|
---|
| 418 | // variable item
|
---|
| 419 | if(item->physical_maximum == item->physical_minimum){
|
---|
| 420 | resolution = 1;
|
---|
| 421 | }
|
---|
| 422 | else {
|
---|
| 423 | resolution = (item->logical_maximum - item->logical_minimum) /
|
---|
| 424 | ((item->physical_maximum - item->physical_minimum) *
|
---|
| 425 | (usb_pow(10,(item->unit_exponent))));
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
|
---|
| 429 | }
|
---|
| 430 | else {
|
---|
| 431 | // bitmapa
|
---|
| 432 | if(value == 0) {
|
---|
| 433 | ret = 0;
|
---|
| 434 | }
|
---|
| 435 | else {
|
---|
| 436 | size_t bitmap_idx = (value - item->usage_minimum);
|
---|
| 437 | ret = 1 << bitmap_idx;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 |
|
---|
[1553cbf] | 441 | if((item->logical_minimum < 0) || (item->logical_maximum < 0)){
|
---|
| 442 | return USB_HID_INT32_TO_UINT32(ret, item->size);
|
---|
| 443 | }
|
---|
| 444 | return (int32_t)ret;
|
---|
[841e6e5] | 445 | }
|
---|
| 446 |
|
---|
[64dbc83] | 447 | usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item)
|
---|
| 448 | {
|
---|
| 449 | usb_hid_report_item_t *new_report_item;
|
---|
| 450 |
|
---|
| 451 | if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
|
---|
| 452 | return NULL;
|
---|
| 453 | }
|
---|
| 454 | memcpy(new_report_item,item, sizeof(usb_hid_report_item_t));
|
---|
| 455 | link_initialize(&(new_report_item->link));
|
---|
| 456 |
|
---|
| 457 | return new_report_item;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[e50cd7f] | 460 |
|
---|
| 461 | usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
|
---|
| 462 | usb_hid_report_field_t *field,
|
---|
| 463 | usb_hid_report_path_t *path, int flags,
|
---|
| 464 | usb_hid_report_type_t type)
|
---|
| 465 | {
|
---|
| 466 | usb_hid_report_description_t *report_des = usb_hid_report_find_description (report, path->report_id, type);
|
---|
| 467 | link_t *field_it;
|
---|
| 468 |
|
---|
| 469 | if(report_des == NULL){
|
---|
| 470 | return NULL;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | if(field == NULL){
|
---|
| 474 | // vezmu prvni co mathuje podle path!!
|
---|
| 475 | field_it = report_des->report_items.next;
|
---|
| 476 | }
|
---|
| 477 | else {
|
---|
| 478 | field_it = field->link.next;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | while(field_it != &report_des->report_items) {
|
---|
| 482 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
---|
[c7c0984a] | 483 |
|
---|
| 484 | if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
|
---|
| 485 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
---|
| 486 | if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK){
|
---|
| 487 | usb_hid_report_remove_last_item (field->collection_path);
|
---|
| 488 | return field;
|
---|
| 489 | }
|
---|
[e50cd7f] | 490 | usb_hid_report_remove_last_item (field->collection_path);
|
---|
| 491 | }
|
---|
| 492 | field_it = field_it->next;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | return NULL;
|
---|
| 496 | }
|
---|
[cfbbe1d3] | 497 |
|
---|
| 498 | uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
|
---|
| 499 | {
|
---|
| 500 | if(report == NULL){
|
---|
| 501 | return 0;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | usb_hid_report_description_t *report_des;
|
---|
| 505 | link_t *report_it;
|
---|
| 506 |
|
---|
| 507 | if(report_id == 0) {
|
---|
| 508 | report_it = usb_hid_report_find_description (report, report_id, type)->link.next;
|
---|
| 509 | }
|
---|
| 510 | else {
|
---|
| 511 | report_it = report->reports.next;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | while(report_it != &report->reports) {
|
---|
| 515 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
---|
| 516 | if(report_des->type == type){
|
---|
| 517 | return report_des->report_id;
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | return 0;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[2020927] | 524 | void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
|
---|
| 525 | {
|
---|
| 526 | if(report_item == NULL) {
|
---|
| 527 | return;
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | report_item->usages_count = 0;
|
---|
| 531 | memset(report_item->usages, 0, USB_HID_MAX_USAGES);
|
---|
| 532 |
|
---|
| 533 | report_item->extended_usage_page = 0;
|
---|
| 534 | report_item->usage_minimum = 0;
|
---|
| 535 | report_item->usage_maximum = 0;
|
---|
| 536 | report_item->designator_index = 0;
|
---|
| 537 | report_item->designator_minimum = 0;
|
---|
| 538 | report_item->designator_maximum = 0;
|
---|
| 539 | report_item->string_index = 0;
|
---|
| 540 | report_item->string_minimum = 0;
|
---|
| 541 | report_item->string_maximum = 0;
|
---|
[cfbbe1d3] | 542 |
|
---|
[2020927] | 543 | return;
|
---|
| 544 | }
|
---|
[976f546] | 545 | /**
|
---|
| 546 | * @}
|
---|
[c7a2e7e] | 547 | */
|
---|