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