| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Matej Klonfar
|
|---|
| 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libusbhid
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * USB HID report data parser implementation.
|
|---|
| 35 | */
|
|---|
| 36 | #include <usb/hid/hidparser.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <mem.h>
|
|---|
| 41 | #include <usb/debug.h>
|
|---|
| 42 | #include <assert.h>
|
|---|
| 43 | #include <bitops.h>
|
|---|
| 44 | #include <macros.h>
|
|---|
| 45 |
|
|---|
| 46 | /*
|
|---|
| 47 | * Data translation private functions
|
|---|
| 48 | */
|
|---|
| 49 | uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
|
|---|
| 50 |
|
|---|
| 51 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
|
|---|
| 52 |
|
|---|
| 53 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
|
|---|
| 54 | int32_t value);
|
|---|
| 55 |
|
|---|
| 56 | static int usb_pow(int a, int b)
|
|---|
| 57 | {
|
|---|
| 58 | switch (b) {
|
|---|
| 59 | case 0:
|
|---|
| 60 | return 1;
|
|---|
| 61 | break;
|
|---|
| 62 | case 1:
|
|---|
| 63 | return a;
|
|---|
| 64 | break;
|
|---|
| 65 | default:
|
|---|
| 66 | return a * usb_pow(a, b - 1);
|
|---|
| 67 | break;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 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 | } else {
|
|---|
| 91 | return report_des->item_length;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /** Returns size of report of specified report id and type in bytes
|
|---|
| 96 | *
|
|---|
| 97 | * @param parser Opaque report parser structure
|
|---|
| 98 | * @param report_id
|
|---|
| 99 | * @param type
|
|---|
| 100 | * @return Number of items in specified report
|
|---|
| 101 | */
|
|---|
| 102 | size_t usb_hid_report_byte_size(usb_hid_report_t *report, uint8_t report_id,
|
|---|
| 103 | usb_hid_report_type_t type)
|
|---|
| 104 | {
|
|---|
| 105 | usb_hid_report_description_t *report_des;
|
|---|
| 106 |
|
|---|
| 107 | if (report == NULL) {
|
|---|
| 108 | return 0;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | report_des = usb_hid_report_find_description(report, report_id, type);
|
|---|
| 112 | if (report_des == NULL) {
|
|---|
| 113 | return 0;
|
|---|
| 114 | } else {
|
|---|
| 115 | return ((report_des->bit_length + 7) / 8);
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /** Parse and act upon a HID report.
|
|---|
| 120 | *
|
|---|
| 121 | * @see usb_hid_parse_report_descriptor
|
|---|
| 122 | *
|
|---|
| 123 | * @param parser Opaque HID report parser structure.
|
|---|
| 124 | * @param data Data for the report.
|
|---|
| 125 | * @return Error code.
|
|---|
| 126 | */
|
|---|
| 127 | errno_t usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data,
|
|---|
| 128 | size_t size, uint8_t *report_id)
|
|---|
| 129 | {
|
|---|
| 130 | usb_hid_report_description_t *report_des;
|
|---|
| 131 | usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
|
|---|
| 132 |
|
|---|
| 133 | if (report == NULL) {
|
|---|
| 134 | return EINVAL;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | if (report->use_report_ids != 0) {
|
|---|
| 138 | *report_id = data[0];
|
|---|
| 139 | } else {
|
|---|
| 140 | *report_id = 0;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | report_des = usb_hid_report_find_description(report, *report_id,
|
|---|
| 144 | type);
|
|---|
| 145 |
|
|---|
| 146 | if (report_des == NULL) {
|
|---|
| 147 | return EINVAL;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /* read data */
|
|---|
| 151 | list_foreach(report_des->report_items, ritems_link,
|
|---|
| 152 | usb_hid_report_field_t, item) {
|
|---|
| 153 |
|
|---|
| 154 | if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
|
|---|
| 155 |
|
|---|
| 156 | if (USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
|
|---|
| 157 | /* array */
|
|---|
| 158 | item->value =
|
|---|
| 159 | usb_hid_translate_data(item, data);
|
|---|
| 160 |
|
|---|
| 161 | item->usage = USB_HID_EXTENDED_USAGE(
|
|---|
| 162 | item->usages[item->value -
|
|---|
| 163 | item->physical_minimum]);
|
|---|
| 164 |
|
|---|
| 165 | item->usage_page =
|
|---|
| 166 | USB_HID_EXTENDED_USAGE_PAGE(
|
|---|
| 167 | item->usages[item->value -
|
|---|
| 168 | item->physical_minimum]);
|
|---|
| 169 |
|
|---|
| 170 | usb_hid_report_set_last_item(
|
|---|
| 171 | item->collection_path,
|
|---|
| 172 | USB_HID_TAG_CLASS_GLOBAL,
|
|---|
| 173 | item->usage_page);
|
|---|
| 174 |
|
|---|
| 175 | usb_hid_report_set_last_item(
|
|---|
| 176 | item->collection_path,
|
|---|
| 177 | USB_HID_TAG_CLASS_LOCAL, item->usage);
|
|---|
| 178 | } else {
|
|---|
| 179 | /* variable item */
|
|---|
| 180 | item->value = usb_hid_translate_data(item,
|
|---|
| 181 | data);
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | return EOK;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /**
|
|---|
| 190 | * Translate data from the report as specified in report descriptor item
|
|---|
| 191 | *
|
|---|
| 192 | * @param item Report descriptor item with definition of translation
|
|---|
| 193 | * @param data Data to translate
|
|---|
| 194 | * @return Translated data
|
|---|
| 195 | */
|
|---|
| 196 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
|
|---|
| 197 | {
|
|---|
| 198 | /* now only short tags are allowed */
|
|---|
| 199 | if (item->size > 32) {
|
|---|
| 200 | return 0;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | if ((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
|
|---|
| 204 | item->physical_minimum = item->logical_minimum;
|
|---|
| 205 | item->physical_maximum = item->logical_maximum;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | int resolution;
|
|---|
| 209 | if (item->physical_maximum == item->physical_minimum) {
|
|---|
| 210 | resolution = 1;
|
|---|
| 211 | } else {
|
|---|
| 212 | resolution = (item->logical_maximum - item->logical_minimum) /
|
|---|
| 213 | ((item->physical_maximum - item->physical_minimum) *
|
|---|
| 214 | (usb_pow(10, (item->unit_exponent))));
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | int32_t value = 0;
|
|---|
| 218 |
|
|---|
| 219 | /* First, skip all bytes we don't care */
|
|---|
| 220 | data += item->offset / 8;
|
|---|
| 221 |
|
|---|
| 222 | int bits = item->size;
|
|---|
| 223 | int taken = 0;
|
|---|
| 224 |
|
|---|
| 225 | /* Than we take the higher bits from the LSB */
|
|---|
| 226 | const unsigned bit_offset = item->offset % 8;
|
|---|
| 227 | const int lsb_bits = min(bits, 8);
|
|---|
| 228 |
|
|---|
| 229 | value |= (*data >> bit_offset) & BIT_RRANGE(uint8_t, lsb_bits);
|
|---|
| 230 | bits -= lsb_bits;
|
|---|
| 231 | taken += lsb_bits;
|
|---|
| 232 | data++;
|
|---|
| 233 |
|
|---|
| 234 | /* Then there may be bytes, which we take as a whole. */
|
|---|
| 235 | while (bits > 8) {
|
|---|
| 236 | value |= *data << taken;
|
|---|
| 237 | taken += 8;
|
|---|
| 238 | bits -= 8;
|
|---|
| 239 | data++;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /* And, finally, lower bits from HSB. */
|
|---|
| 243 | if (bits > 0) {
|
|---|
| 244 | value |= (*data & BIT_RRANGE(uint8_t, bits)) << taken;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | if ((item->logical_minimum < 0) || (item->logical_maximum < 0)) {
|
|---|
| 248 | value = USB_HID_UINT32_TO_INT32(value, item->size);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | return (int) (((value - item->logical_minimum) / resolution) +
|
|---|
| 252 | item->physical_minimum);
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /* OUTPUT API */
|
|---|
| 256 |
|
|---|
| 257 | /**
|
|---|
| 258 | * Allocates output report buffer for output report
|
|---|
| 259 | *
|
|---|
| 260 | * @param parser Report parsed structure
|
|---|
| 261 | * @param size Size of returned buffer
|
|---|
| 262 | * @param report_id Report id of created output report
|
|---|
| 263 | * @return Returns allocated output buffer for specified output
|
|---|
| 264 | */
|
|---|
| 265 | uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size,
|
|---|
| 266 | uint8_t report_id)
|
|---|
| 267 | {
|
|---|
| 268 | if (report == NULL) {
|
|---|
| 269 | *size = 0;
|
|---|
| 270 | return NULL;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | usb_hid_report_description_t *report_des = NULL;
|
|---|
| 274 |
|
|---|
| 275 | list_foreach(report->reports, reports_link,
|
|---|
| 276 | usb_hid_report_description_t, rdes) {
|
|---|
| 277 | if ((rdes->report_id == report_id) &&
|
|---|
| 278 | (rdes->type == USB_HID_REPORT_TYPE_OUTPUT)) {
|
|---|
| 279 | report_des = rdes;
|
|---|
| 280 | break;
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | if (report_des == NULL) {
|
|---|
| 285 | *size = 0;
|
|---|
| 286 | return NULL;
|
|---|
| 287 | } else {
|
|---|
| 288 | *size = (report_des->bit_length + (8 - 1)) / 8;
|
|---|
| 289 | uint8_t *ret = malloc((*size) * sizeof(uint8_t));
|
|---|
| 290 | memset(ret, 0, (*size) * sizeof(uint8_t));
|
|---|
| 291 | return ret;
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | /** Frees output report buffer
|
|---|
| 296 | *
|
|---|
| 297 | * @param output Output report buffer
|
|---|
| 298 | * @return void
|
|---|
| 299 | */
|
|---|
| 300 | void usb_hid_report_output_free(uint8_t *output)
|
|---|
| 301 | {
|
|---|
| 302 | if (output != NULL) {
|
|---|
| 303 | free(output);
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /** Makes the output report buffer for data given in the report structure
|
|---|
| 308 | *
|
|---|
| 309 | * @param parser Opaque report parser structure
|
|---|
| 310 | * @param path Usage path specifing which parts of output will be set
|
|---|
| 311 | * @param flags Usage path structure comparison flags
|
|---|
| 312 | * @param buffer Output buffer
|
|---|
| 313 | * @param size Size of output buffer
|
|---|
| 314 | * @return Error code
|
|---|
| 315 | */
|
|---|
| 316 | errno_t usb_hid_report_output_translate(usb_hid_report_t *report,
|
|---|
| 317 | uint8_t report_id, uint8_t *buffer, size_t size)
|
|---|
| 318 | {
|
|---|
| 319 | int32_t value = 0;
|
|---|
| 320 | int offset;
|
|---|
| 321 | int length;
|
|---|
| 322 | int32_t tmp_value;
|
|---|
| 323 |
|
|---|
| 324 | if (report == NULL) {
|
|---|
| 325 | return EINVAL;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | if (report->use_report_ids != 0) {
|
|---|
| 329 | buffer[0] = report_id;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | usb_hid_report_description_t *report_des;
|
|---|
| 333 | report_des = usb_hid_report_find_description(report, report_id,
|
|---|
| 334 | USB_HID_REPORT_TYPE_OUTPUT);
|
|---|
| 335 |
|
|---|
| 336 | if (report_des == NULL) {
|
|---|
| 337 | return EINVAL;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | list_foreach(report_des->report_items, ritems_link,
|
|---|
| 341 | usb_hid_report_field_t, report_item) {
|
|---|
| 342 | value = usb_hid_translate_data_reverse(report_item,
|
|---|
| 343 | report_item->value);
|
|---|
| 344 |
|
|---|
| 345 | offset = report_des->bit_length - report_item->offset - 1;
|
|---|
| 346 | length = report_item->size;
|
|---|
| 347 |
|
|---|
| 348 | usb_log_debug("\ttranslated value: %x", value);
|
|---|
| 349 |
|
|---|
| 350 | if ((offset / 8) == ((offset + length - 1) / 8)) {
|
|---|
| 351 | if (((size_t) (offset / 8) >= size) ||
|
|---|
| 352 | ((size_t) (offset + length - 1) / 8) >= size) {
|
|---|
| 353 | break; // TODO ErrorCode
|
|---|
| 354 | }
|
|---|
| 355 | size_t shift = 8 - offset % 8 - length;
|
|---|
| 356 | value = value << shift;
|
|---|
| 357 | value = value & (((1 << length) - 1) << shift);
|
|---|
| 358 |
|
|---|
| 359 | uint8_t mask = 0;
|
|---|
| 360 | mask = 0xff - (((1 << length) - 1) << shift);
|
|---|
| 361 | buffer[offset / 8] = (buffer[offset / 8] & mask) |
|
|---|
| 362 | value;
|
|---|
| 363 | } else {
|
|---|
| 364 | int i = 0;
|
|---|
| 365 | uint8_t mask = 0;
|
|---|
| 366 | for (i = (offset / 8);
|
|---|
| 367 | i <= ((offset + length - 1) / 8); i++) {
|
|---|
| 368 | if (i == (offset / 8)) {
|
|---|
| 369 | tmp_value = value;
|
|---|
| 370 | tmp_value = tmp_value &
|
|---|
| 371 | ((1 << (8 - (offset % 8))) - 1);
|
|---|
| 372 |
|
|---|
| 373 | tmp_value = tmp_value << (offset % 8);
|
|---|
| 374 |
|
|---|
| 375 | mask = ~(((1 << (8 - (offset % 8))) - 1) << (offset % 8));
|
|---|
| 376 |
|
|---|
| 377 | buffer[i] = (buffer[i] & mask) |
|
|---|
| 378 | tmp_value;
|
|---|
| 379 | } else if (i == ((offset + length - 1) / 8)) {
|
|---|
| 380 |
|
|---|
| 381 | value = value >> (length -
|
|---|
| 382 | ((offset + length) % 8));
|
|---|
| 383 |
|
|---|
| 384 | value = value & ((1 << (length -
|
|---|
| 385 | ((offset + length) % 8))) - 1);
|
|---|
| 386 |
|
|---|
| 387 | mask = (1 << (length -
|
|---|
| 388 | ((offset + length) % 8))) - 1;
|
|---|
| 389 |
|
|---|
| 390 | buffer[i] = (buffer[i] & mask) | value;
|
|---|
| 391 | } else {
|
|---|
| 392 | buffer[i] = value & (0xff << i);
|
|---|
| 393 | }
|
|---|
| 394 | }
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | /* reset value */
|
|---|
| 398 | report_item->value = 0;
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | return EOK;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | /**
|
|---|
| 405 | * Translate given data for putting them into the outoput report
|
|---|
| 406 | * @param item Report item structure
|
|---|
| 407 | * @param value Value to translate
|
|---|
| 408 | * @return ranslated value
|
|---|
| 409 | */
|
|---|
| 410 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
|
|---|
| 411 | int value)
|
|---|
| 412 | {
|
|---|
| 413 | int ret = 0;
|
|---|
| 414 | int resolution;
|
|---|
| 415 |
|
|---|
| 416 | if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
|
|---|
| 417 | return item->logical_minimum;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | if ((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
|
|---|
| 421 | item->physical_minimum = item->logical_minimum;
|
|---|
| 422 | item->physical_maximum = item->logical_maximum;
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | /* variable item */
|
|---|
| 426 | if (item->physical_maximum == item->physical_minimum) {
|
|---|
| 427 | resolution = 1;
|
|---|
| 428 | } else {
|
|---|
| 429 | resolution = (item->logical_maximum - item->logical_minimum) /
|
|---|
| 430 | ((item->physical_maximum - item->physical_minimum) *
|
|---|
| 431 | (usb_pow(10, (item->unit_exponent))));
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | ret = ((value - item->physical_minimum) * resolution) +
|
|---|
| 435 | item->logical_minimum;
|
|---|
| 436 |
|
|---|
| 437 | usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), "
|
|---|
| 438 | "ret(%x)\n", value, resolution, item->physical_minimum,
|
|---|
| 439 | item->logical_minimum, ret);
|
|---|
| 440 |
|
|---|
| 441 | if ((item->logical_minimum < 0) || (item->logical_maximum < 0)) {
|
|---|
| 442 | return USB_HID_INT32_TO_UINT32(ret, item->size);
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | return (int32_t) 0 + ret;
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | /**
|
|---|
| 449 | * Clones given state table
|
|---|
| 450 | *
|
|---|
| 451 | * @param item State table to clone
|
|---|
| 452 | * @return Pointer to the cloned item
|
|---|
| 453 | */
|
|---|
| 454 | usb_hid_report_item_t *usb_hid_report_item_clone(
|
|---|
| 455 | const usb_hid_report_item_t *item)
|
|---|
| 456 | {
|
|---|
| 457 | usb_hid_report_item_t *new_report_item;
|
|---|
| 458 |
|
|---|
| 459 | if (!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
|
|---|
| 460 | return NULL;
|
|---|
| 461 | }
|
|---|
| 462 | memcpy(new_report_item, item, sizeof(usb_hid_report_item_t));
|
|---|
| 463 | link_initialize(&(new_report_item->link));
|
|---|
| 464 |
|
|---|
| 465 | return new_report_item;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | /**
|
|---|
| 469 | * Function for sequence walking through the report. Returns next field in the
|
|---|
| 470 | * report or the first one when no field is given.
|
|---|
| 471 | *
|
|---|
| 472 | * @param report Searched report structure
|
|---|
| 473 | * @param field Current field. If NULL is given, the first one in the report
|
|---|
| 474 | * is returned. Otherwise the next one i nthe list is returned.
|
|---|
| 475 | * @param path Usage path specifying which fields wa are interested in.
|
|---|
| 476 | * @param flags Flags defining mode of usage paths comparison
|
|---|
| 477 | * @param type Type of report we search.
|
|---|
| 478 | * @retval NULL if no field is founded
|
|---|
| 479 | * @retval Pointer to the founded report structure when founded
|
|---|
| 480 | */
|
|---|
| 481 | usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
|
|---|
| 482 | usb_hid_report_field_t *field, usb_hid_report_path_t *path, int flags,
|
|---|
| 483 | usb_hid_report_type_t type)
|
|---|
| 484 | {
|
|---|
| 485 | usb_hid_report_description_t *report_des =
|
|---|
| 486 | usb_hid_report_find_description(report, path->report_id, type);
|
|---|
| 487 |
|
|---|
| 488 | link_t *field_it;
|
|---|
| 489 |
|
|---|
| 490 | if (report_des == NULL) {
|
|---|
| 491 | return NULL;
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | if (field == NULL) {
|
|---|
| 495 | field_it = report_des->report_items.head.next;
|
|---|
| 496 | } else {
|
|---|
| 497 | field_it = field->ritems_link.next;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | while (field_it != &report_des->report_items.head) {
|
|---|
| 501 | field = list_get_instance(field_it, usb_hid_report_field_t,
|
|---|
| 502 | ritems_link);
|
|---|
| 503 |
|
|---|
| 504 | if (USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
|
|---|
| 505 | usb_hid_report_path_append_item(field->collection_path,
|
|---|
| 506 | field->usage_page, field->usage);
|
|---|
| 507 |
|
|---|
| 508 | if (usb_hid_report_compare_usage_path(
|
|---|
| 509 | field->collection_path, path, flags) == 0) {
|
|---|
| 510 | usb_hid_report_remove_last_item(
|
|---|
| 511 | field->collection_path);
|
|---|
| 512 | return field;
|
|---|
| 513 | }
|
|---|
| 514 | usb_hid_report_remove_last_item(field->collection_path);
|
|---|
| 515 | }
|
|---|
| 516 | field_it = field_it->next;
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | return NULL;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | /**
|
|---|
| 523 | * Returns next report_id of report of specified type. If zero is given than
|
|---|
| 524 | * first report_id of specified type is returned (0 is not legal value for
|
|---|
| 525 | * repotr_id)
|
|---|
| 526 | *
|
|---|
| 527 | * @param report_id Current report_id, 0 if there is no current report_id
|
|---|
| 528 | * @param type Type of searched report
|
|---|
| 529 | * @param report Report structure inwhich we search
|
|---|
| 530 | * @retval 0 if report structure is null or there is no specified report
|
|---|
| 531 | * @retval report_id otherwise
|
|---|
| 532 | */
|
|---|
| 533 | uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report, uint8_t report_id,
|
|---|
| 534 | usb_hid_report_type_t type)
|
|---|
| 535 | {
|
|---|
| 536 | if (report == NULL) {
|
|---|
| 537 | return 0;
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | usb_hid_report_description_t *report_des;
|
|---|
| 541 | link_t *report_it;
|
|---|
| 542 |
|
|---|
| 543 | if (report_id > 0) {
|
|---|
| 544 | report_des = usb_hid_report_find_description(report, report_id,
|
|---|
| 545 | type);
|
|---|
| 546 | if (report_des == NULL) {
|
|---|
| 547 | return 0;
|
|---|
| 548 | } else {
|
|---|
| 549 | report_it = report_des->reports_link.next;
|
|---|
| 550 | }
|
|---|
| 551 | } else {
|
|---|
| 552 | report_it = report->reports.head.next;
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | while (report_it != &report->reports.head) {
|
|---|
| 556 | report_des = list_get_instance(report_it,
|
|---|
| 557 | usb_hid_report_description_t, reports_link);
|
|---|
| 558 |
|
|---|
| 559 | if (report_des->type == type) {
|
|---|
| 560 | return report_des->report_id;
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | report_it = report_it->next;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | return 0;
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | /**
|
|---|
| 570 | * Reset all local items in given state table
|
|---|
| 571 | *
|
|---|
| 572 | * @param report_item State table containing current state of report
|
|---|
| 573 | * descriptor parsing
|
|---|
| 574 | *
|
|---|
| 575 | * @return void
|
|---|
| 576 | */
|
|---|
| 577 | void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
|
|---|
| 578 | {
|
|---|
| 579 | if (report_item == NULL) {
|
|---|
| 580 | return;
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | report_item->usages_count = 0;
|
|---|
| 584 | memset(report_item->usages, 0, sizeof(report_item->usages));
|
|---|
| 585 |
|
|---|
| 586 | report_item->extended_usage_page = 0;
|
|---|
| 587 | report_item->usage_minimum = 0;
|
|---|
| 588 | report_item->usage_maximum = 0;
|
|---|
| 589 | report_item->designator_index = 0;
|
|---|
| 590 | report_item->designator_minimum = 0;
|
|---|
| 591 | report_item->designator_maximum = 0;
|
|---|
| 592 | report_item->string_index = 0;
|
|---|
| 593 | report_item->string_minimum = 0;
|
|---|
| 594 | report_item->string_maximum = 0;
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | /**
|
|---|
| 598 | * @}
|
|---|
| 599 | */
|
|---|