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