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