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