| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Matej Klonfar
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup libusbhid
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * USB HID report data parser implementation.
|
|---|
| 34 | */
|
|---|
| 35 | #include <usb/hid/hidparser.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <malloc.h>
|
|---|
| 39 | #include <mem.h>
|
|---|
| 40 | #include <usb/debug.h>
|
|---|
| 41 | #include <assert.h>
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | /*
|
|---|
| 45 | * Data translation private functions
|
|---|
| 46 | */
|
|---|
| 47 | uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size);
|
|---|
| 48 |
|
|---|
| 49 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
|
|---|
| 50 |
|
|---|
| 51 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
|
|---|
| 52 | int32_t value);
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 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 |
|
|---|
| 72 | /** Returns size of report of specified report id and type in items
|
|---|
| 73 | *
|
|---|
| 74 | * @param parser Opaque report parser structure
|
|---|
| 75 | * @param report_id
|
|---|
| 76 | * @param type
|
|---|
| 77 | * @return Number of items in specified report
|
|---|
| 78 | */
|
|---|
| 79 | size_t usb_hid_report_size(usb_hid_report_t *report, uint8_t report_id,
|
|---|
| 80 | usb_hid_report_type_t type)
|
|---|
| 81 | {
|
|---|
| 82 | usb_hid_report_description_t *report_des;
|
|---|
| 83 |
|
|---|
| 84 | if (report == NULL) {
|
|---|
| 85 | return 0;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | report_des = usb_hid_report_find_description(report, report_id, type);
|
|---|
| 89 | if (report_des == NULL) {
|
|---|
| 90 | return 0;
|
|---|
| 91 | } else {
|
|---|
| 92 | return report_des->item_length;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Returns size of report of specified report id and type in bytes
|
|---|
| 97 | *
|
|---|
| 98 | * @param parser Opaque report parser structure
|
|---|
| 99 | * @param report_id
|
|---|
| 100 | * @param type
|
|---|
| 101 | * @return Number of items in specified report
|
|---|
| 102 | */
|
|---|
| 103 | size_t usb_hid_report_byte_size(usb_hid_report_t *report, uint8_t report_id,
|
|---|
| 104 | usb_hid_report_type_t type)
|
|---|
| 105 | {
|
|---|
| 106 | usb_hid_report_description_t *report_des;
|
|---|
| 107 |
|
|---|
| 108 | if (report == NULL) {
|
|---|
| 109 | return 0;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | report_des = usb_hid_report_find_description(report, report_id, type);
|
|---|
| 113 | if (report_des == NULL) {
|
|---|
| 114 | return 0;
|
|---|
| 115 | } else {
|
|---|
| 116 | return ((report_des->bit_length + 7) / 8) ;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | /** Parse and act upon a HID report.
|
|---|
| 122 | *
|
|---|
| 123 | * @see usb_hid_parse_report_descriptor
|
|---|
| 124 | *
|
|---|
| 125 | * @param parser Opaque HID report parser structure.
|
|---|
| 126 | * @param data Data for the report.
|
|---|
| 127 | * @return Error code.
|
|---|
| 128 | */
|
|---|
| 129 | int usb_hid_parse_report(const usb_hid_report_t *report, const uint8_t *data,
|
|---|
| 130 | size_t size, uint8_t *report_id)
|
|---|
| 131 | {
|
|---|
| 132 | usb_hid_report_description_t *report_des;
|
|---|
| 133 | usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
|
|---|
| 134 |
|
|---|
| 135 | if (report == NULL) {
|
|---|
| 136 | return EINVAL;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | if (report->use_report_ids != 0) {
|
|---|
| 140 | *report_id = data[0];
|
|---|
| 141 | } else {
|
|---|
| 142 | *report_id = 0;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | report_des = usb_hid_report_find_description(report, *report_id,
|
|---|
| 146 | type);
|
|---|
| 147 |
|
|---|
| 148 | if (report_des == NULL) {
|
|---|
| 149 | return EINVAL;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* read data */
|
|---|
| 153 | list_foreach(report_des->report_items, ritems_link,
|
|---|
| 154 | usb_hid_report_field_t, item) {
|
|---|
| 155 |
|
|---|
| 156 | if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
|
|---|
| 157 |
|
|---|
| 158 | if (USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
|
|---|
| 159 | /* array */
|
|---|
| 160 | item->value =
|
|---|
| 161 | usb_hid_translate_data(item, data);
|
|---|
| 162 |
|
|---|
| 163 | item->usage = USB_HID_EXTENDED_USAGE(
|
|---|
| 164 | item->usages[item->value -
|
|---|
| 165 | item->physical_minimum]);
|
|---|
| 166 |
|
|---|
| 167 | item->usage_page =
|
|---|
| 168 | USB_HID_EXTENDED_USAGE_PAGE(
|
|---|
| 169 | item->usages[item->value -
|
|---|
| 170 | item->physical_minimum]);
|
|---|
| 171 |
|
|---|
| 172 | usb_hid_report_set_last_item(
|
|---|
| 173 | item->collection_path,
|
|---|
| 174 | USB_HID_TAG_CLASS_GLOBAL,
|
|---|
| 175 | item->usage_page);
|
|---|
| 176 |
|
|---|
| 177 | usb_hid_report_set_last_item(
|
|---|
| 178 | item->collection_path,
|
|---|
| 179 | USB_HID_TAG_CLASS_LOCAL, item->usage);
|
|---|
| 180 | } else {
|
|---|
| 181 | /* variable item */
|
|---|
| 182 | item->value = usb_hid_translate_data(item,
|
|---|
| 183 | data);
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | return EOK;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | /**
|
|---|
| 193 | * Translate data from the report as specified in report descriptor item
|
|---|
| 194 | *
|
|---|
| 195 | * @param item Report descriptor item with definition of translation
|
|---|
| 196 | * @param data Data to translate
|
|---|
| 197 | * @return Translated data
|
|---|
| 198 | */
|
|---|
| 199 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
|
|---|
| 200 | {
|
|---|
| 201 | int resolution;
|
|---|
| 202 | int offset;
|
|---|
| 203 | int part_size;
|
|---|
| 204 |
|
|---|
| 205 | int32_t value = 0;
|
|---|
| 206 | int32_t mask = 0;
|
|---|
| 207 | const uint8_t *foo = 0;
|
|---|
| 208 |
|
|---|
| 209 | /* now only short tags are allowed */
|
|---|
| 210 | if (item->size > 32) {
|
|---|
| 211 | return 0;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | if ((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
|
|---|
| 215 | item->physical_minimum = item->logical_minimum;
|
|---|
| 216 | item->physical_maximum = item->logical_maximum;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 | if (item->physical_maximum == item->physical_minimum) {
|
|---|
| 221 | resolution = 1;
|
|---|
| 222 | } else {
|
|---|
| 223 | resolution = (item->logical_maximum - item->logical_minimum) /
|
|---|
| 224 | ((item->physical_maximum - item->physical_minimum) *
|
|---|
| 225 | (usb_pow(10, (item->unit_exponent))));
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | offset = item->offset;
|
|---|
| 229 | // FIXME
|
|---|
| 230 | if ((size_t) (offset / 8) != (size_t) ((offset+item->size - 1) / 8)) {
|
|---|
| 231 |
|
|---|
| 232 | part_size = 0;
|
|---|
| 233 |
|
|---|
| 234 | size_t i = 0;
|
|---|
| 235 | for (i = (size_t) (offset / 8);
|
|---|
| 236 | i <= (size_t) (offset + item->size - 1) / 8; i++) {
|
|---|
| 237 | if (i == (size_t) (offset / 8)) {
|
|---|
| 238 | /* the higher one */
|
|---|
| 239 | part_size = 8 - (offset % 8);
|
|---|
| 240 | foo = data + i;
|
|---|
| 241 | mask = ((1 << (item->size - part_size)) - 1);
|
|---|
| 242 | value = (*foo & mask);
|
|---|
| 243 | } else if (i == ((offset + item->size - 1) / 8)) {
|
|---|
| 244 | /* the lower one */
|
|---|
| 245 | foo = data + i;
|
|---|
| 246 | mask = ((1 << (item->size - part_size)) - 1) <<
|
|---|
| 247 | (8 - (item->size - part_size));
|
|---|
| 248 |
|
|---|
| 249 | value = (((*foo & mask) >> (8 -
|
|---|
| 250 | (item->size - part_size))) << part_size) +
|
|---|
| 251 | value;
|
|---|
| 252 | } else {
|
|---|
| 253 | value = (*(data + 1) << (part_size + 8)) +
|
|---|
| 254 | value;
|
|---|
| 255 | part_size += 8;
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 | } else {
|
|---|
| 259 | foo = data + (offset / 8);
|
|---|
| 260 | mask = ((1 << item->size) - 1) <<
|
|---|
| 261 | (8 - ((offset % 8) + item->size));
|
|---|
| 262 | value = (*foo & mask) >> (8 - ((offset % 8) + item->size));
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | if ((item->logical_minimum < 0) || (item->logical_maximum < 0)) {
|
|---|
| 266 | value = USB_HID_UINT32_TO_INT32(value, item->size);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | return (int) (((value - item->logical_minimum) / resolution) +
|
|---|
| 270 | item->physical_minimum);
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 | /* OUTPUT API */
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Allocates output report buffer for output report
|
|---|
| 278 | *
|
|---|
| 279 | * @param parser Report parsed structure
|
|---|
| 280 | * @param size Size of returned buffer
|
|---|
| 281 | * @param report_id Report id of created output report
|
|---|
| 282 | * @return Returns allocated output buffer for specified output
|
|---|
| 283 | */
|
|---|
| 284 | uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size,
|
|---|
| 285 | uint8_t report_id)
|
|---|
| 286 | {
|
|---|
| 287 | if (report == NULL) {
|
|---|
| 288 | *size = 0;
|
|---|
| 289 | return NULL;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | usb_hid_report_description_t *report_des = NULL;
|
|---|
| 293 |
|
|---|
| 294 | list_foreach(report->reports, reports_link,
|
|---|
| 295 | usb_hid_report_description_t, report_des) {
|
|---|
| 296 | if ((report_des->report_id == report_id) &&
|
|---|
| 297 | (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)) {
|
|---|
| 298 | break;
|
|---|
| 299 | }
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | if (report_des == NULL) {
|
|---|
| 303 | *size = 0;
|
|---|
| 304 | return NULL;
|
|---|
| 305 | } else {
|
|---|
| 306 | *size = (report_des->bit_length + (8 - 1)) / 8;
|
|---|
| 307 | uint8_t *ret = malloc((*size) * sizeof(uint8_t));
|
|---|
| 308 | memset(ret, 0, (*size) * sizeof(uint8_t));
|
|---|
| 309 | return ret;
|
|---|
| 310 | }
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 | /** Frees output report buffer
|
|---|
| 315 | *
|
|---|
| 316 | * @param output Output report buffer
|
|---|
| 317 | * @return void
|
|---|
| 318 | */
|
|---|
| 319 | void usb_hid_report_output_free(uint8_t *output)
|
|---|
| 320 | {
|
|---|
| 321 | if (output != NULL) {
|
|---|
| 322 | free(output);
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | /** Makes the output report buffer for data given in the report structure
|
|---|
| 327 | *
|
|---|
| 328 | * @param parser Opaque report parser structure
|
|---|
| 329 | * @param path Usage path specifing which parts of output will be set
|
|---|
| 330 | * @param flags Usage path structure comparison flags
|
|---|
| 331 | * @param buffer Output buffer
|
|---|
| 332 | * @param size Size of output buffer
|
|---|
| 333 | * @return Error code
|
|---|
| 334 | */
|
|---|
| 335 | int usb_hid_report_output_translate(usb_hid_report_t *report,
|
|---|
| 336 | uint8_t report_id, uint8_t *buffer, size_t size)
|
|---|
| 337 | {
|
|---|
| 338 | int32_t value = 0;
|
|---|
| 339 | int offset;
|
|---|
| 340 | int length;
|
|---|
| 341 | int32_t tmp_value;
|
|---|
| 342 |
|
|---|
| 343 | if (report == NULL) {
|
|---|
| 344 | return EINVAL;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | if (report->use_report_ids != 0) {
|
|---|
| 348 | buffer[0] = report_id;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | usb_hid_report_description_t *report_des;
|
|---|
| 352 | report_des = usb_hid_report_find_description(report, report_id,
|
|---|
| 353 | USB_HID_REPORT_TYPE_OUTPUT);
|
|---|
| 354 |
|
|---|
| 355 | if (report_des == NULL) {
|
|---|
| 356 | return EINVAL;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | list_foreach(report_des->report_items, ritems_link,
|
|---|
| 360 | usb_hid_report_field_t, report_item) {
|
|---|
| 361 | value = usb_hid_translate_data_reverse(report_item,
|
|---|
| 362 | report_item->value);
|
|---|
| 363 |
|
|---|
| 364 | offset = report_des->bit_length - report_item->offset - 1;
|
|---|
| 365 | length = report_item->size;
|
|---|
| 366 |
|
|---|
| 367 | usb_log_debug("\ttranslated value: %x\n", value);
|
|---|
| 368 |
|
|---|
| 369 | if ((offset / 8) == ((offset + length - 1) / 8)) {
|
|---|
| 370 | if (((size_t) (offset / 8) >= size) ||
|
|---|
| 371 | ((size_t) (offset + length - 1) / 8) >= size) {
|
|---|
| 372 | break; // TODO ErrorCode
|
|---|
| 373 | }
|
|---|
| 374 | size_t shift = 8 - offset % 8 - length;
|
|---|
| 375 | value = value << shift;
|
|---|
| 376 | value = value & (((1 << length) - 1) << shift);
|
|---|
| 377 |
|
|---|
| 378 | uint8_t mask = 0;
|
|---|
| 379 | mask = 0xff - (((1 << length) - 1) << shift);
|
|---|
| 380 | buffer[offset / 8] = (buffer[offset / 8] & mask) |
|
|---|
| 381 | value;
|
|---|
| 382 | } else {
|
|---|
| 383 | int i = 0;
|
|---|
| 384 | uint8_t mask = 0;
|
|---|
| 385 | for (i = (offset / 8);
|
|---|
| 386 | i <= ((offset + length - 1) / 8); i++) {
|
|---|
| 387 | if (i == (offset / 8)) {
|
|---|
| 388 | tmp_value = value;
|
|---|
| 389 | tmp_value = tmp_value &
|
|---|
| 390 | ((1 << (8 - (offset % 8))) - 1);
|
|---|
| 391 |
|
|---|
| 392 | tmp_value = tmp_value << (offset % 8);
|
|---|
| 393 |
|
|---|
| 394 | mask = ~(((1 << (8 - (offset % 8))) - 1)
|
|---|
| 395 | << (offset % 8));
|
|---|
| 396 |
|
|---|
| 397 | buffer[i] = (buffer[i] & mask) |
|
|---|
| 398 | tmp_value;
|
|---|
| 399 | } else if (i == ((offset + length - 1) / 8)) {
|
|---|
| 400 |
|
|---|
| 401 | value = value >> (length -
|
|---|
| 402 | ((offset + length) % 8));
|
|---|
| 403 |
|
|---|
| 404 | value = value & ((1 << (length -
|
|---|
| 405 | ((offset + length) % 8))) - 1);
|
|---|
| 406 |
|
|---|
| 407 | mask = (1 << (length -
|
|---|
| 408 | ((offset + length) % 8))) - 1;
|
|---|
| 409 |
|
|---|
| 410 | buffer[i] = (buffer[i] & mask) | value;
|
|---|
| 411 | } else {
|
|---|
| 412 | buffer[i] = value & (0xff << i);
|
|---|
| 413 | }
|
|---|
| 414 | }
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | /* reset value */
|
|---|
| 418 | report_item->value = 0;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | return EOK;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 | /**
|
|---|
| 426 | * Translate given data for putting them into the outoput report
|
|---|
| 427 | * @param item Report item structure
|
|---|
| 428 | * @param value Value to translate
|
|---|
| 429 | * @return ranslated value
|
|---|
| 430 | */
|
|---|
| 431 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item,
|
|---|
| 432 | int value)
|
|---|
| 433 | {
|
|---|
| 434 | int ret = 0;
|
|---|
| 435 | int resolution;
|
|---|
| 436 |
|
|---|
| 437 | if (USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
|
|---|
| 438 | ret = item->logical_minimum;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | if ((item->physical_minimum == 0) && (item->physical_maximum == 0)) {
|
|---|
| 442 | item->physical_minimum = item->logical_minimum;
|
|---|
| 443 | item->physical_maximum = item->logical_maximum;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | /* variable item */
|
|---|
| 447 | if (item->physical_maximum == item->physical_minimum) {
|
|---|
| 448 | resolution = 1;
|
|---|
| 449 | } else {
|
|---|
| 450 | resolution = (item->logical_maximum - item->logical_minimum) /
|
|---|
| 451 | ((item->physical_maximum - item->physical_minimum) *
|
|---|
| 452 | (usb_pow(10, (item->unit_exponent))));
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | ret = ((value - item->physical_minimum) * resolution) +
|
|---|
| 456 | item->logical_minimum;
|
|---|
| 457 |
|
|---|
| 458 | usb_log_debug("\tvalue(%x), resolution(%x), phymin(%x) logmin(%x), "
|
|---|
| 459 | "ret(%x)\n", value, resolution, item->physical_minimum,
|
|---|
| 460 | item->logical_minimum, ret);
|
|---|
| 461 |
|
|---|
| 462 | if ((item->logical_minimum < 0) || (item->logical_maximum < 0)) {
|
|---|
| 463 | return USB_HID_INT32_TO_UINT32(ret, item->size);
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | return (int32_t) 0 + ret;
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 | /**
|
|---|
| 471 | * Clones given state table
|
|---|
| 472 | *
|
|---|
| 473 | * @param item State table to clone
|
|---|
| 474 | * @return Pointer to the cloned item
|
|---|
| 475 | */
|
|---|
| 476 | usb_hid_report_item_t *usb_hid_report_item_clone(
|
|---|
| 477 | const usb_hid_report_item_t *item)
|
|---|
| 478 | {
|
|---|
| 479 | usb_hid_report_item_t *new_report_item;
|
|---|
| 480 |
|
|---|
| 481 | if (!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
|
|---|
| 482 | return NULL;
|
|---|
| 483 | }
|
|---|
| 484 | memcpy(new_report_item,item, sizeof(usb_hid_report_item_t));
|
|---|
| 485 | link_initialize(&(new_report_item->link));
|
|---|
| 486 |
|
|---|
| 487 | return new_report_item;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 | /**
|
|---|
| 492 | * Function for sequence walking through the report. Returns next field in the
|
|---|
| 493 | * report or the first one when no field is given.
|
|---|
| 494 | *
|
|---|
| 495 | * @param report Searched report structure
|
|---|
| 496 | * @param field Current field. If NULL is given, the first one in the report
|
|---|
| 497 | * is returned. Otherwise the next one i nthe list is returned.
|
|---|
| 498 | * @param path Usage path specifying which fields wa are interested in.
|
|---|
| 499 | * @param flags Flags defining mode of usage paths comparison
|
|---|
| 500 | * @param type Type of report we search.
|
|---|
| 501 | * @retval NULL if no field is founded
|
|---|
| 502 | * @retval Pointer to the founded report structure when founded
|
|---|
| 503 | */
|
|---|
| 504 | usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
|
|---|
| 505 | usb_hid_report_field_t *field, usb_hid_report_path_t *path, int flags,
|
|---|
| 506 | usb_hid_report_type_t type)
|
|---|
| 507 | {
|
|---|
| 508 | usb_hid_report_description_t *report_des =
|
|---|
| 509 | usb_hid_report_find_description(report, path->report_id, type);
|
|---|
| 510 |
|
|---|
| 511 | link_t *field_it;
|
|---|
| 512 |
|
|---|
| 513 | if (report_des == NULL) {
|
|---|
| 514 | return NULL;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | if (field == NULL) {
|
|---|
| 518 | field_it = report_des->report_items.head.next;
|
|---|
| 519 | } else {
|
|---|
| 520 | field_it = field->ritems_link.next;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | while (field_it != &report_des->report_items.head) {
|
|---|
| 524 | field = list_get_instance(field_it, usb_hid_report_field_t,
|
|---|
| 525 | ritems_link);
|
|---|
| 526 |
|
|---|
| 527 | if (USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
|
|---|
| 528 | usb_hid_report_path_append_item(field->collection_path,
|
|---|
| 529 | field->usage_page, field->usage);
|
|---|
| 530 |
|
|---|
| 531 | if (usb_hid_report_compare_usage_path(
|
|---|
| 532 | field->collection_path, path, flags) == EOK) {
|
|---|
| 533 | usb_hid_report_remove_last_item(
|
|---|
| 534 | field->collection_path);
|
|---|
| 535 | return field;
|
|---|
| 536 | }
|
|---|
| 537 | usb_hid_report_remove_last_item(field->collection_path);
|
|---|
| 538 | }
|
|---|
| 539 | field_it = field_it->next;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | return NULL;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 | /**
|
|---|
| 547 | * Returns next report_id of report of specified type. If zero is given than
|
|---|
| 548 | * first report_id of specified type is returned (0 is not legal value for
|
|---|
| 549 | * repotr_id)
|
|---|
| 550 | *
|
|---|
| 551 | * @param report_id Current report_id, 0 if there is no current report_id
|
|---|
| 552 | * @param type Type of searched report
|
|---|
| 553 | * @param report Report structure inwhich we search
|
|---|
| 554 | * @retval 0 if report structure is null or there is no specified report
|
|---|
| 555 | * @retval report_id otherwise
|
|---|
| 556 | */
|
|---|
| 557 | uint8_t usb_hid_get_next_report_id(usb_hid_report_t *report, uint8_t report_id,
|
|---|
| 558 | usb_hid_report_type_t type)
|
|---|
| 559 | {
|
|---|
| 560 | if (report == NULL) {
|
|---|
| 561 | return 0;
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | usb_hid_report_description_t *report_des;
|
|---|
| 565 | link_t *report_it;
|
|---|
| 566 |
|
|---|
| 567 | if (report_id > 0) {
|
|---|
| 568 | report_des = usb_hid_report_find_description(report, report_id,
|
|---|
| 569 | type);
|
|---|
| 570 | if (report_des == NULL) {
|
|---|
| 571 | return 0;
|
|---|
| 572 | } else {
|
|---|
| 573 | report_it = report_des->reports_link.next;
|
|---|
| 574 | }
|
|---|
| 575 | } else {
|
|---|
| 576 | report_it = report->reports.head.next;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | while (report_it != &report->reports.head) {
|
|---|
| 580 | report_des = list_get_instance(report_it,
|
|---|
| 581 | usb_hid_report_description_t, reports_link);
|
|---|
| 582 |
|
|---|
| 583 | if (report_des->type == type) {
|
|---|
| 584 | return report_des->report_id;
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 | report_it = report_it->next;
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | return 0;
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 | /**
|
|---|
| 595 | * Reset all local items in given state table
|
|---|
| 596 | *
|
|---|
| 597 | * @param report_item State table containing current state of report
|
|---|
| 598 | * descriptor parsing
|
|---|
| 599 | *
|
|---|
| 600 | * @return void
|
|---|
| 601 | */
|
|---|
| 602 | void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
|
|---|
| 603 | {
|
|---|
| 604 | if (report_item == NULL) {
|
|---|
| 605 | return;
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | report_item->usages_count = 0;
|
|---|
| 609 | memset(report_item->usages, 0, USB_HID_MAX_USAGES);
|
|---|
| 610 |
|
|---|
| 611 | report_item->extended_usage_page = 0;
|
|---|
| 612 | report_item->usage_minimum = 0;
|
|---|
| 613 | report_item->usage_maximum = 0;
|
|---|
| 614 | report_item->designator_index = 0;
|
|---|
| 615 | report_item->designator_minimum = 0;
|
|---|
| 616 | report_item->designator_maximum = 0;
|
|---|
| 617 | report_item->string_index = 0;
|
|---|
| 618 | report_item->string_minimum = 0;
|
|---|
| 619 | report_item->string_maximum = 0;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | /**
|
|---|
| 623 | * @}
|
|---|
| 624 | */
|
|---|