| [bf2063e9] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 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 |
|
|---|
| [976f546] | 29 | /** @addtogroup libusb
|
|---|
| [bf2063e9] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| [57d9c05e] | 33 | * HID report descriptor and report data parser implementation.
|
|---|
| [bf2063e9] | 34 | */
|
|---|
| 35 | #include <usb/classes/hidparser.h>
|
|---|
| 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 |
|
|---|
| [a694a58] | 43 | /** The new report item flag. Used to determine when the item is completly
|
|---|
| 44 | * configured and should be added to the report structure
|
|---|
| 45 | */
|
|---|
| [b7d9606] | 46 | #define USB_HID_NEW_REPORT_ITEM 1
|
|---|
| [57d9c05e] | 47 |
|
|---|
| [a694a58] | 48 | /** No special action after the report descriptor tag is processed should be
|
|---|
| 49 | * done
|
|---|
| 50 | */
|
|---|
| 51 | #define USB_HID_NO_ACTION 2
|
|---|
| [976f546] | 52 |
|
|---|
| [175ad13e] | 53 | #define USB_HID_RESET_OFFSET 3
|
|---|
| 54 |
|
|---|
| [a694a58] | 55 | /** Unknown tag was founded in report descriptor data*/
|
|---|
| [57d9c05e] | 56 | #define USB_HID_UNKNOWN_TAG -99
|
|---|
| [976f546] | 57 |
|
|---|
| [57d9c05e] | 58 | /*
|
|---|
| 59 | * Private descriptor parser functions
|
|---|
| 60 | */
|
|---|
| [175ad13e] | 61 | int usb_hid_report_init(usb_hid_report_t *report);
|
|---|
| 62 | int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item);
|
|---|
| 63 | usb_hid_report_description_t * usb_hid_report_find_description(const usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type);
|
|---|
| [b7d9606] | 64 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
|---|
| [b53d3b7] | 65 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
|---|
| [c7a2e7e] | 66 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| [b53d3b7] | 67 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
|---|
| [c7a2e7e] | 68 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| [b53d3b7] | 69 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
|---|
| [c7a2e7e] | 70 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| [b53d3b7] | 71 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path);
|
|---|
| [976f546] | 72 |
|
|---|
| [175ad13e] | 73 | void usb_hid_print_usage_path(usb_hid_report_path_t *path);
|
|---|
| [e24e7b1] | 74 | void usb_hid_descriptor_print_list(link_t *head);
|
|---|
| [2020927] | 75 | void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item);
|
|---|
| [e24e7b1] | 76 | void usb_hid_free_report_list(link_t *head);
|
|---|
| [64dbc83] | 77 | usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item);
|
|---|
| [57d9c05e] | 78 | /*
|
|---|
| 79 | * Data translation private functions
|
|---|
| 80 | */
|
|---|
| [c7a2e7e] | 81 | int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
|
|---|
| [b7d9606] | 82 | inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
|
|---|
| [cfbbe1d3] | 83 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data);
|
|---|
| [175ad13e] | 84 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int32_t value);
|
|---|
| [fad14d7] | 85 | int usb_pow(int a, int b);
|
|---|
| 86 |
|
|---|
| [57d9c05e] | 87 | // TODO: tohle ma bejt asi jinde
|
|---|
| [fad14d7] | 88 | int usb_pow(int a, int b)
|
|---|
| 89 | {
|
|---|
| 90 | switch(b) {
|
|---|
| 91 | case 0:
|
|---|
| 92 | return 1;
|
|---|
| 93 | break;
|
|---|
| 94 | case 1:
|
|---|
| 95 | return a;
|
|---|
| 96 | break;
|
|---|
| 97 | default:
|
|---|
| 98 | return a * usb_pow(a, b-1);
|
|---|
| 99 | break;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| [976f546] | 103 | /**
|
|---|
| [57d9c05e] | 104 | * Initialize the report descriptor parser structure
|
|---|
| [976f546] | 105 | *
|
|---|
| [57d9c05e] | 106 | * @param parser Report descriptor parser structure
|
|---|
| 107 | * @return Error code
|
|---|
| [976f546] | 108 | */
|
|---|
| [175ad13e] | 109 | int usb_hid_report_init(usb_hid_report_t *report)
|
|---|
| [976f546] | 110 | {
|
|---|
| [175ad13e] | 111 | if(report == NULL) {
|
|---|
| [57d9c05e] | 112 | return EINVAL;
|
|---|
| 113 | }
|
|---|
| [976f546] | 114 |
|
|---|
| [175ad13e] | 115 | memset(report, 0, sizeof(usb_hid_report_t));
|
|---|
| 116 | list_initialize(&report->reports);
|
|---|
| 117 | list_initialize(&report->collection_paths);
|
|---|
| [64dbc83] | 118 |
|
|---|
| [175ad13e] | 119 | report->use_report_ids = 0;
|
|---|
| [da3965e] | 120 | return EOK;
|
|---|
| [976f546] | 121 | }
|
|---|
| 122 |
|
|---|
| [175ad13e] | 123 | int usb_hid_report_append_fields(usb_hid_report_t *report, usb_hid_report_item_t *report_item)
|
|---|
| 124 | {
|
|---|
| 125 | usb_hid_report_field_t *field;
|
|---|
| 126 | int i;
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | /* find or append current collection path to the list */
|
|---|
| 130 | link_t *path_it = report->collection_paths.next;
|
|---|
| 131 | usb_hid_report_path_t *path = NULL;
|
|---|
| 132 | while(path_it != &report->collection_paths) {
|
|---|
| 133 | path = list_get_instance(path_it, usb_hid_report_path_t, link);
|
|---|
| 134 |
|
|---|
| 135 | if(usb_hid_report_compare_usage_path(path, report_item->usage_path, USB_HID_PATH_COMPARE_STRICT) == EOK){
|
|---|
| 136 | break;
|
|---|
| 137 | }
|
|---|
| 138 | path_it = path_it->next;
|
|---|
| 139 | }
|
|---|
| 140 | if(path_it == &report->collection_paths) {
|
|---|
| 141 | path = usb_hid_report_path_clone(report_item->usage_path);
|
|---|
| [681f24b3] | 142 | list_append(&path->link, &report->collection_paths);
|
|---|
| [175ad13e] | 143 | report->collection_paths_count++;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| [2020927] | 146 | for(i=0; i<report_item->usages_count; i++){
|
|---|
| 147 | usb_log_debug("usages (%d) - %x\n", i, report_item->usages[i]);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| [175ad13e] | 150 |
|
|---|
| 151 | for(i=0; i<report_item->count; i++){
|
|---|
| 152 |
|
|---|
| 153 | field = malloc(sizeof(usb_hid_report_field_t));
|
|---|
| 154 | memset(field, 0, sizeof(usb_hid_report_field_t));
|
|---|
| 155 | list_initialize(&field->link);
|
|---|
| 156 |
|
|---|
| [cfbbe1d3] | 157 | /* fill the attributes */
|
|---|
| [175ad13e] | 158 | field->collection_path = path;
|
|---|
| 159 | field->logical_minimum = report_item->logical_minimum;
|
|---|
| 160 | field->logical_maximum = report_item->logical_maximum;
|
|---|
| 161 | field->physical_minimum = report_item->physical_minimum;
|
|---|
| 162 | field->physical_maximum = report_item->physical_maximum;
|
|---|
| [cfbbe1d3] | 163 |
|
|---|
| [175ad13e] | 164 | field->usage_minimum = report_item->usage_minimum;
|
|---|
| 165 | field->usage_maximum = report_item->usage_maximum;
|
|---|
| 166 | if(report_item->extended_usage_page != 0){
|
|---|
| 167 | field->usage_page = report_item->extended_usage_page;
|
|---|
| 168 | }
|
|---|
| 169 | else {
|
|---|
| 170 | field->usage_page = report_item->usage_page;
|
|---|
| 171 | }
|
|---|
| [cfbbe1d3] | 172 |
|
|---|
| 173 | if(report_item->usages_count > 0 && ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {
|
|---|
| [2020927] | 174 | uint32_t usage;
|
|---|
| 175 | if(report_item->type == USB_HID_REPORT_TYPE_INPUT) {
|
|---|
| 176 | if(i < report_item->usages_count){
|
|---|
| 177 | usage = report_item->usages[i];
|
|---|
| [175ad13e] | 178 | }
|
|---|
| 179 | else {
|
|---|
| [2020927] | 180 | usage = report_item->usages[report_item->usages_count - 1];
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | else {
|
|---|
| 184 | if((report_item->count - i - 1) < report_item->usages_count){
|
|---|
| 185 | usage = report_item->usages[(report_item->count - i - 1)];
|
|---|
| [175ad13e] | 186 | }
|
|---|
| [2020927] | 187 | else {
|
|---|
| 188 | usage = report_item->usages[report_item->usages_count - 1];
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 | if((usage & 0xFF00) != 0){
|
|---|
| 194 | field->usage_page = (usage >> 16);
|
|---|
| 195 | field->usage = (usage & 0xFF);
|
|---|
| [175ad13e] | 196 | }
|
|---|
| 197 | else {
|
|---|
| [2020927] | 198 | field->usage = usage;
|
|---|
| [175ad13e] | 199 | }
|
|---|
| [2020927] | 200 |
|
|---|
| 201 |
|
|---|
| [cfbbe1d3] | 202 | }
|
|---|
| 203 |
|
|---|
| 204 | if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) != 0) && (!((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0)))) {
|
|---|
| 205 | if(report_item->type == USB_HID_REPORT_TYPE_INPUT) {
|
|---|
| 206 | field->usage = report_item->usage_maximum - i;
|
|---|
| 207 | }
|
|---|
| 208 | else {
|
|---|
| 209 | field->usage = report_item->usage_minimum + i;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | }
|
|---|
| [175ad13e] | 213 |
|
|---|
| 214 | field->size = report_item->size;
|
|---|
| 215 | field->offset = report_item->offset + (i * report_item->size);
|
|---|
| 216 | if(report_item->id != 0) {
|
|---|
| 217 | field->offset += 8;
|
|---|
| 218 | report->use_report_ids = 1;
|
|---|
| 219 | }
|
|---|
| 220 | field->item_flags = report_item->item_flags;
|
|---|
| 221 |
|
|---|
| 222 | /* find the right report list*/
|
|---|
| 223 | usb_hid_report_description_t *report_des;
|
|---|
| 224 | report_des = usb_hid_report_find_description(report, report_item->id, report_item->type);
|
|---|
| 225 | if(report_des == NULL){
|
|---|
| 226 | report_des = malloc(sizeof(usb_hid_report_description_t));
|
|---|
| 227 | memset(report_des, 0, sizeof(usb_hid_report_description_t));
|
|---|
| 228 |
|
|---|
| 229 | report_des->type = report_item->type;
|
|---|
| 230 | report_des->report_id = report_item->id;
|
|---|
| 231 | list_initialize (&report_des->link);
|
|---|
| 232 | list_initialize (&report_des->report_items);
|
|---|
| 233 |
|
|---|
| 234 | list_append(&report_des->link, &report->reports);
|
|---|
| 235 | report->report_count++;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /* append this field to the end of founded report list */
|
|---|
| 239 | list_append (&field->link, &report_des->report_items);
|
|---|
| 240 |
|
|---|
| 241 | /* update the sizes */
|
|---|
| 242 | report_des->bit_length += field->size;
|
|---|
| 243 | report_des->item_length++;
|
|---|
| 244 |
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 | return EOK;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | usb_hid_report_description_t * usb_hid_report_find_description(const usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
|
|---|
| 252 | {
|
|---|
| 253 | link_t *report_it = report->reports.next;
|
|---|
| 254 | usb_hid_report_description_t *report_des = NULL;
|
|---|
| 255 |
|
|---|
| 256 | while(report_it != &report->reports) {
|
|---|
| 257 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
|---|
| [dc9f122] | 258 |
|
|---|
| [175ad13e] | 259 | if((report_des->report_id == report_id) && (report_des->type == type)){
|
|---|
| [dc9f122] | 260 | return report_des;
|
|---|
| [175ad13e] | 261 | }
|
|---|
| [dc9f122] | 262 |
|
|---|
| [175ad13e] | 263 | report_it = report_it->next;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| [dc9f122] | 266 | return NULL;
|
|---|
| [175ad13e] | 267 | }
|
|---|
| [bf2063e9] | 268 |
|
|---|
| 269 | /** Parse HID report descriptor.
|
|---|
| 270 | *
|
|---|
| 271 | * @param parser Opaque HID report parser structure.
|
|---|
| 272 | * @param data Data describing the report.
|
|---|
| 273 | * @return Error code.
|
|---|
| 274 | */
|
|---|
| [175ad13e] | 275 | int usb_hid_parse_report_descriptor(usb_hid_report_t *report,
|
|---|
| [b7d9606] | 276 | const uint8_t *data, size_t size)
|
|---|
| [bf2063e9] | 277 | {
|
|---|
| [976f546] | 278 | size_t i=0;
|
|---|
| 279 | uint8_t tag=0;
|
|---|
| 280 | uint8_t item_size=0;
|
|---|
| 281 | int class=0;
|
|---|
| 282 | int ret;
|
|---|
| 283 | usb_hid_report_item_t *report_item=0;
|
|---|
| [b53d3b7] | 284 | usb_hid_report_item_t *new_report_item;
|
|---|
| 285 | usb_hid_report_path_t *usage_path;
|
|---|
| [c7a2e7e] | 286 |
|
|---|
| [60a228f] | 287 | size_t offset_input=0;
|
|---|
| 288 | size_t offset_output=0;
|
|---|
| 289 | size_t offset_feature=0;
|
|---|
| [175ad13e] | 290 |
|
|---|
| 291 | link_t stack;
|
|---|
| 292 | list_initialize(&stack);
|
|---|
| [55e388a1] | 293 |
|
|---|
| [b53d3b7] | 294 | /* parser structure initialization*/
|
|---|
| [175ad13e] | 295 | if(usb_hid_report_init(report) != EOK) {
|
|---|
| [55e388a1] | 296 | return EINVAL;
|
|---|
| 297 | }
|
|---|
| [976f546] | 298 |
|
|---|
| [b53d3b7] | 299 | /*report item initialization*/
|
|---|
| [976f546] | 300 | if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
|
|---|
| 301 | return ENOMEM;
|
|---|
| 302 | }
|
|---|
| [60a228f] | 303 | memset(report_item, 0, sizeof(usb_hid_report_item_t));
|
|---|
| [b53d3b7] | 304 | list_initialize(&(report_item->link));
|
|---|
| [8bec4d1] | 305 |
|
|---|
| [b53d3b7] | 306 | /* usage path context initialization */
|
|---|
| 307 | if(!(usage_path=usb_hid_report_path())){
|
|---|
| 308 | return ENOMEM;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| [b7d9606] | 311 | while(i<size){
|
|---|
| [976f546] | 312 | if(!USB_HID_ITEM_IS_LONG(data[i])){
|
|---|
| [b7d9606] | 313 |
|
|---|
| [8bec4d1] | 314 | if((i+USB_HID_ITEM_SIZE(data[i]))>= size){
|
|---|
| [175ad13e] | 315 | return EINVAL;
|
|---|
| [b7d9606] | 316 | }
|
|---|
| 317 |
|
|---|
| [976f546] | 318 | tag = USB_HID_ITEM_TAG(data[i]);
|
|---|
| 319 | item_size = USB_HID_ITEM_SIZE(data[i]);
|
|---|
| 320 | class = USB_HID_ITEM_TAG_CLASS(data[i]);
|
|---|
| [b7d9606] | 321 |
|
|---|
| 322 | ret = usb_hid_report_parse_tag(tag,class,data+i+1,
|
|---|
| [b53d3b7] | 323 | item_size,report_item, usage_path);
|
|---|
| [976f546] | 324 | switch(ret){
|
|---|
| 325 | case USB_HID_NEW_REPORT_ITEM:
|
|---|
| 326 | // store report item to report and create the new one
|
|---|
| [175ad13e] | 327 | // store current collection path
|
|---|
| [b53d3b7] | 328 | report_item->usage_path = usage_path;
|
|---|
| 329 |
|
|---|
| [bda06a3] | 330 | usb_hid_report_path_set_report_id(report_item->usage_path, report_item->id);
|
|---|
| 331 | if(report_item->id != 0){
|
|---|
| [175ad13e] | 332 | report->use_report_ids = 1;
|
|---|
| [bda06a3] | 333 | }
|
|---|
| [fad14d7] | 334 |
|
|---|
| [976f546] | 335 | switch(tag) {
|
|---|
| 336 | case USB_HID_REPORT_TAG_INPUT:
|
|---|
| [175ad13e] | 337 | report_item->type = USB_HID_REPORT_TYPE_INPUT;
|
|---|
| [60a228f] | 338 | report_item->offset = offset_input;
|
|---|
| 339 | offset_input += report_item->count * report_item->size;
|
|---|
| [976f546] | 340 | break;
|
|---|
| 341 | case USB_HID_REPORT_TAG_OUTPUT:
|
|---|
| [175ad13e] | 342 | report_item->type = USB_HID_REPORT_TYPE_OUTPUT;
|
|---|
| [60a228f] | 343 | report_item->offset = offset_output;
|
|---|
| 344 | offset_output += report_item->count * report_item->size;
|
|---|
| [976f546] | 345 |
|
|---|
| 346 | break;
|
|---|
| 347 | case USB_HID_REPORT_TAG_FEATURE:
|
|---|
| [175ad13e] | 348 | report_item->type = USB_HID_REPORT_TYPE_FEATURE;
|
|---|
| [60a228f] | 349 | report_item->offset = offset_feature;
|
|---|
| 350 | offset_feature += report_item->count * report_item->size;
|
|---|
| [976f546] | 351 | break;
|
|---|
| 352 | default:
|
|---|
| [b7d9606] | 353 | usb_log_debug("\tjump over - tag %X\n", tag);
|
|---|
| [976f546] | 354 | break;
|
|---|
| 355 | }
|
|---|
| [a9974ef] | 356 |
|
|---|
| [175ad13e] | 357 | /*
|
|---|
| 358 | * append new fields to the report
|
|---|
| 359 | * structure
|
|---|
| 360 | */
|
|---|
| 361 | usb_hid_report_append_fields(report, report_item);
|
|---|
| 362 |
|
|---|
| [c32688d] | 363 | /* reset local items */
|
|---|
| [2020927] | 364 | usb_hid_report_reset_local_items (report_item);
|
|---|
| [175ad13e] | 365 |
|
|---|
| [976f546] | 366 | break;
|
|---|
| [175ad13e] | 367 |
|
|---|
| 368 | case USB_HID_RESET_OFFSET:
|
|---|
| 369 | offset_input = 0;
|
|---|
| 370 | offset_output = 0;
|
|---|
| 371 | offset_feature = 0;
|
|---|
| [c7c0984a] | 372 | usb_hid_report_path_set_report_id (usage_path, report_item->id);
|
|---|
| [175ad13e] | 373 | break;
|
|---|
| 374 |
|
|---|
| [976f546] | 375 | case USB_HID_REPORT_TAG_PUSH:
|
|---|
| 376 | // push current state to stack
|
|---|
| [64dbc83] | 377 | new_report_item = usb_hid_report_item_clone(report_item);
|
|---|
| [c156c2d] | 378 | usb_hid_report_path_t *tmp_path = usb_hid_report_path_clone(usage_path);
|
|---|
| 379 | new_report_item->usage_path = tmp_path;
|
|---|
| 380 |
|
|---|
| [175ad13e] | 381 | list_prepend (&new_report_item->link, &stack);
|
|---|
| [976f546] | 382 | break;
|
|---|
| 383 | case USB_HID_REPORT_TAG_POP:
|
|---|
| 384 | // restore current state from stack
|
|---|
| [175ad13e] | 385 | if(list_empty (&stack)) {
|
|---|
| [64dbc83] | 386 | return EINVAL;
|
|---|
| 387 | }
|
|---|
| [c156c2d] | 388 | free(report_item);
|
|---|
| 389 |
|
|---|
| [175ad13e] | 390 | report_item = list_get_instance(stack.next, usb_hid_report_item_t, link);
|
|---|
| [64dbc83] | 391 |
|
|---|
| [c156c2d] | 392 | usb_hid_report_usage_path_t *tmp_usage_path;
|
|---|
| 393 | tmp_usage_path = list_get_instance(report_item->usage_path->link.prev, usb_hid_report_usage_path_t, link);
|
|---|
| 394 |
|
|---|
| 395 | usb_hid_report_set_last_item(usage_path, tmp_usage_path->usage_page, tmp_usage_path->usage);
|
|---|
| 396 |
|
|---|
| 397 | usb_hid_report_path_free(report_item->usage_path);
|
|---|
| 398 | list_initialize(&report_item->usage_path->link);
|
|---|
| [175ad13e] | 399 | list_remove (stack.next);
|
|---|
| [64dbc83] | 400 |
|
|---|
| [976f546] | 401 | break;
|
|---|
| 402 |
|
|---|
| 403 | default:
|
|---|
| [b7d9606] | 404 | // nothing special to do
|
|---|
| [976f546] | 405 | break;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | /* jump over the processed block */
|
|---|
| 409 | i += 1 + USB_HID_ITEM_SIZE(data[i]);
|
|---|
| 410 | }
|
|---|
| 411 | else{
|
|---|
| 412 | // TBD
|
|---|
| 413 | i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | return EOK;
|
|---|
| [bf2063e9] | 420 | }
|
|---|
| 421 |
|
|---|
| [2f60e57d] | 422 |
|
|---|
| [976f546] | 423 | /**
|
|---|
| [57d9c05e] | 424 | * Parse one tag of the report descriptor
|
|---|
| [976f546] | 425 | *
|
|---|
| 426 | * @param Tag to parse
|
|---|
| 427 | * @param Report descriptor buffer
|
|---|
| 428 | * @param Size of data belongs to this tag
|
|---|
| 429 | * @param Current report item structe
|
|---|
| 430 | * @return Code of action to be done next
|
|---|
| 431 | */
|
|---|
| [b7d9606] | 432 | int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
|
|---|
| [b53d3b7] | 433 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
|---|
| [976f546] | 434 | {
|
|---|
| [b7d9606] | 435 | int ret;
|
|---|
| 436 |
|
|---|
| [976f546] | 437 | switch(class){
|
|---|
| 438 | case USB_HID_TAG_CLASS_MAIN:
|
|---|
| 439 |
|
|---|
| [b53d3b7] | 440 | if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item, usage_path)) == EOK) {
|
|---|
| [976f546] | 441 | return USB_HID_NEW_REPORT_ITEM;
|
|---|
| 442 | }
|
|---|
| 443 | else {
|
|---|
| 444 | /*TODO process the error */
|
|---|
| [b7d9606] | 445 | return ret;
|
|---|
| [976f546] | 446 | }
|
|---|
| 447 | break;
|
|---|
| 448 |
|
|---|
| 449 | case USB_HID_TAG_CLASS_GLOBAL:
|
|---|
| [b53d3b7] | 450 | return usb_hid_report_parse_global_tag(tag,data,item_size,report_item, usage_path);
|
|---|
| [976f546] | 451 | break;
|
|---|
| 452 |
|
|---|
| 453 | case USB_HID_TAG_CLASS_LOCAL:
|
|---|
| [b53d3b7] | 454 | return usb_hid_report_parse_local_tag(tag,data,item_size,report_item, usage_path);
|
|---|
| [976f546] | 455 | break;
|
|---|
| 456 | default:
|
|---|
| [b7d9606] | 457 | return USB_HID_NO_ACTION;
|
|---|
| [976f546] | 458 | }
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | /**
|
|---|
| 462 | * Parse main tags of report descriptor
|
|---|
| 463 | *
|
|---|
| 464 | * @param Tag identifier
|
|---|
| 465 | * @param Data buffer
|
|---|
| 466 | * @param Length of data buffer
|
|---|
| 467 | * @param Current state table
|
|---|
| 468 | * @return Error code
|
|---|
| 469 | */
|
|---|
| 470 |
|
|---|
| [c7a2e7e] | 471 | int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| [b53d3b7] | 472 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
|---|
| 473 | {
|
|---|
| [976f546] | 474 | switch(tag)
|
|---|
| 475 | {
|
|---|
| 476 | case USB_HID_REPORT_TAG_INPUT:
|
|---|
| 477 | case USB_HID_REPORT_TAG_OUTPUT:
|
|---|
| 478 | case USB_HID_REPORT_TAG_FEATURE:
|
|---|
| [b7d9606] | 479 | report_item->item_flags = *data;
|
|---|
| 480 | return EOK;
|
|---|
| [976f546] | 481 | break;
|
|---|
| 482 |
|
|---|
| 483 | case USB_HID_REPORT_TAG_COLLECTION:
|
|---|
| [175ad13e] | 484 | // TODO usage_path->flags = *data;
|
|---|
| 485 | usb_hid_report_path_append_item(usage_path, report_item->usage_page, report_item->usages[report_item->usages_count-1]);
|
|---|
| [2020927] | 486 | usb_hid_report_reset_local_items (report_item);
|
|---|
| [8bec4d1] | 487 | return USB_HID_NO_ACTION;
|
|---|
| [976f546] | 488 | break;
|
|---|
| 489 |
|
|---|
| 490 | case USB_HID_REPORT_TAG_END_COLLECTION:
|
|---|
| [b53d3b7] | 491 | usb_hid_report_remove_last_item(usage_path);
|
|---|
| [8bec4d1] | 492 | return USB_HID_NO_ACTION;
|
|---|
| [976f546] | 493 | break;
|
|---|
| 494 | default:
|
|---|
| [b7d9606] | 495 | return USB_HID_NO_ACTION;
|
|---|
| [976f546] | 496 | }
|
|---|
| 497 |
|
|---|
| [8bec4d1] | 498 | return EOK;
|
|---|
| [976f546] | 499 | }
|
|---|
| 500 |
|
|---|
| 501 | /**
|
|---|
| 502 | * Parse global tags of report descriptor
|
|---|
| 503 | *
|
|---|
| 504 | * @param Tag identifier
|
|---|
| 505 | * @param Data buffer
|
|---|
| 506 | * @param Length of data buffer
|
|---|
| 507 | * @param Current state table
|
|---|
| 508 | * @return Error code
|
|---|
| 509 | */
|
|---|
| [c7a2e7e] | 510 | int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| [b53d3b7] | 511 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
|---|
| [976f546] | 512 | {
|
|---|
| 513 | // TODO take care about the bit length of data
|
|---|
| 514 | switch(tag)
|
|---|
| 515 | {
|
|---|
| 516 | case USB_HID_REPORT_TAG_USAGE_PAGE:
|
|---|
| [175ad13e] | 517 | report_item->usage_page = usb_hid_report_tag_data_int32(data, item_size);
|
|---|
| [976f546] | 518 | break;
|
|---|
| 519 | case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
|
|---|
| 520 | report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 521 | break;
|
|---|
| 522 | case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
|
|---|
| 523 | report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 524 | break;
|
|---|
| 525 | case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
|
|---|
| 526 | report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 527 | break;
|
|---|
| 528 | case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
|
|---|
| 529 | report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 530 | break;
|
|---|
| 531 | case USB_HID_REPORT_TAG_UNIT_EXPONENT:
|
|---|
| 532 | report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 533 | break;
|
|---|
| 534 | case USB_HID_REPORT_TAG_UNIT:
|
|---|
| 535 | report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 536 | break;
|
|---|
| 537 | case USB_HID_REPORT_TAG_REPORT_SIZE:
|
|---|
| 538 | report_item->size = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 539 | break;
|
|---|
| 540 | case USB_HID_REPORT_TAG_REPORT_COUNT:
|
|---|
| 541 | report_item->count = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 542 | break;
|
|---|
| 543 | case USB_HID_REPORT_TAG_REPORT_ID:
|
|---|
| 544 | report_item->id = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| [175ad13e] | 545 | return USB_HID_RESET_OFFSET;
|
|---|
| [976f546] | 546 | break;
|
|---|
| 547 | case USB_HID_REPORT_TAG_PUSH:
|
|---|
| 548 | case USB_HID_REPORT_TAG_POP:
|
|---|
| [175ad13e] | 549 | /*
|
|---|
| 550 | * stack operations are done in top level parsing
|
|---|
| 551 | * function
|
|---|
| 552 | */
|
|---|
| [976f546] | 553 | return tag;
|
|---|
| 554 | break;
|
|---|
| 555 |
|
|---|
| 556 | default:
|
|---|
| [b7d9606] | 557 | return USB_HID_NO_ACTION;
|
|---|
| [976f546] | 558 | }
|
|---|
| [da3965e] | 559 |
|
|---|
| 560 | return EOK;
|
|---|
| [976f546] | 561 | }
|
|---|
| 562 |
|
|---|
| 563 | /**
|
|---|
| 564 | * Parse local tags of report descriptor
|
|---|
| 565 | *
|
|---|
| 566 | * @param Tag identifier
|
|---|
| 567 | * @param Data buffer
|
|---|
| 568 | * @param Length of data buffer
|
|---|
| 569 | * @param Current state table
|
|---|
| 570 | * @return Error code
|
|---|
| 571 | */
|
|---|
| [c7a2e7e] | 572 | int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
|
|---|
| [b53d3b7] | 573 | usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path)
|
|---|
| [976f546] | 574 | {
|
|---|
| 575 | switch(tag)
|
|---|
| 576 | {
|
|---|
| 577 | case USB_HID_REPORT_TAG_USAGE:
|
|---|
| [2020927] | 578 | report_item->usages[report_item->usages_count] = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 579 | report_item->usages_count++;
|
|---|
| [976f546] | 580 | break;
|
|---|
| 581 | case USB_HID_REPORT_TAG_USAGE_MINIMUM:
|
|---|
| [175ad13e] | 582 | if (item_size == 3) {
|
|---|
| 583 | // usage extended usages
|
|---|
| 584 | report_item->extended_usage_page = (usb_hid_report_tag_data_int32(data,item_size) & 0xFF00) >> 16;
|
|---|
| 585 | report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size) & 0xFF;
|
|---|
| 586 | }
|
|---|
| 587 | else {
|
|---|
| 588 | report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 589 | }
|
|---|
| [976f546] | 590 | break;
|
|---|
| 591 | case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
|
|---|
| [175ad13e] | 592 | if (item_size == 3) {
|
|---|
| 593 | // usage extended usages
|
|---|
| 594 | report_item->extended_usage_page = (usb_hid_report_tag_data_int32(data,item_size) & 0xFF00) >> 16;
|
|---|
| 595 | report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size) & 0xFF;
|
|---|
| 596 | }
|
|---|
| 597 | else {
|
|---|
| 598 | report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 599 | }
|
|---|
| [976f546] | 600 | break;
|
|---|
| 601 | case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
|
|---|
| 602 | report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 603 | break;
|
|---|
| 604 | case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
|
|---|
| 605 | report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 606 | break;
|
|---|
| 607 | case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
|
|---|
| 608 | report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 609 | break;
|
|---|
| 610 | case USB_HID_REPORT_TAG_STRING_INDEX:
|
|---|
| 611 | report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 612 | break;
|
|---|
| 613 | case USB_HID_REPORT_TAG_STRING_MINIMUM:
|
|---|
| 614 | report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 615 | break;
|
|---|
| 616 | case USB_HID_REPORT_TAG_STRING_MAXIMUM:
|
|---|
| 617 | report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| [3de529c] | 618 | break;
|
|---|
| [976f546] | 619 | case USB_HID_REPORT_TAG_DELIMITER:
|
|---|
| [175ad13e] | 620 | //report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
|
|---|
| 621 | //TODO:
|
|---|
| 622 | // DELIMITER STUFF
|
|---|
| [976f546] | 623 | break;
|
|---|
| [3de529c] | 624 |
|
|---|
| [976f546] | 625 | default:
|
|---|
| [b7d9606] | 626 | return USB_HID_NO_ACTION;
|
|---|
| [976f546] | 627 | }
|
|---|
| [da3965e] | 628 |
|
|---|
| 629 | return EOK;
|
|---|
| [976f546] | 630 | }
|
|---|
| 631 |
|
|---|
| 632 | /**
|
|---|
| 633 | * Converts raw data to int32 (thats the maximum length of short item data)
|
|---|
| 634 | *
|
|---|
| 635 | * @param Data buffer
|
|---|
| 636 | * @param Size of buffer
|
|---|
| 637 | * @return Converted int32 number
|
|---|
| 638 | */
|
|---|
| [c7a2e7e] | 639 | int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
|
|---|
| [976f546] | 640 | {
|
|---|
| [da3965e] | 641 | unsigned int i;
|
|---|
| [976f546] | 642 | int32_t result;
|
|---|
| 643 |
|
|---|
| 644 | result = 0;
|
|---|
| 645 | for(i=0; i<size; i++) {
|
|---|
| 646 | result = (result | (data[i]) << (i*8));
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | return result;
|
|---|
| 650 | }
|
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 | /**
|
|---|
| 655 | * Prints content of given list of report items.
|
|---|
| 656 | *
|
|---|
| [57d9c05e] | 657 | * @param List of report items (usb_hid_report_item_t)
|
|---|
| [976f546] | 658 | * @return void
|
|---|
| 659 | */
|
|---|
| 660 | void usb_hid_descriptor_print_list(link_t *head)
|
|---|
| 661 | {
|
|---|
| [175ad13e] | 662 | usb_hid_report_field_t *report_item;
|
|---|
| [976f546] | 663 | link_t *item;
|
|---|
| [175ad13e] | 664 |
|
|---|
| 665 |
|
|---|
| [976f546] | 666 | if(head == NULL || list_empty(head)) {
|
|---|
| [60a228f] | 667 | usb_log_debug("\tempty\n");
|
|---|
| [976f546] | 668 | return;
|
|---|
| 669 | }
|
|---|
| [b7d9606] | 670 |
|
|---|
| [976f546] | 671 | for(item = head->next; item != head; item = item->next) {
|
|---|
| 672 |
|
|---|
| [175ad13e] | 673 | report_item = list_get_instance(item, usb_hid_report_field_t, link);
|
|---|
| 674 |
|
|---|
| 675 | usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
|
|---|
| 676 | usb_log_debug("\t\tSIZE: %X\n", report_item->size);
|
|---|
| 677 | usb_log_debug("\t\tLOGMIN: %X\n", report_item->logical_minimum);
|
|---|
| 678 | usb_log_debug("\t\tLOGMAX: %X\n", report_item->logical_maximum);
|
|---|
| 679 | usb_log_debug("\t\tPHYMIN: %X\n", report_item->physical_minimum);
|
|---|
| 680 | usb_log_debug("\t\tPHYMAX: %X\n", report_item->physical_maximum);
|
|---|
| 681 | usb_log_debug("\t\ttUSAGEMIN: %X\n", report_item->usage_minimum);
|
|---|
| 682 | usb_log_debug("\t\tUSAGEMAX: %X\n", report_item->usage_maximum);
|
|---|
| 683 |
|
|---|
| [ef354b6] | 684 | usb_log_debug("\t\tVALUE: %X\n", report_item->value);
|
|---|
| [175ad13e] | 685 | usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage);
|
|---|
| 686 | usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page);
|
|---|
| 687 |
|
|---|
| [ef354b6] | 688 | // usb_log_debug("\n");
|
|---|
| [976f546] | 689 |
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 | }
|
|---|
| 694 | /**
|
|---|
| [57d9c05e] | 695 | * Prints content of given report descriptor in human readable format.
|
|---|
| [976f546] | 696 | *
|
|---|
| [57d9c05e] | 697 | * @param parser Parsed descriptor to print
|
|---|
| [976f546] | 698 | * @return void
|
|---|
| 699 | */
|
|---|
| [175ad13e] | 700 | void usb_hid_descriptor_print(usb_hid_report_t *report)
|
|---|
| [976f546] | 701 | {
|
|---|
| [175ad13e] | 702 | if(report == NULL) {
|
|---|
| [55e388a1] | 703 | return;
|
|---|
| 704 | }
|
|---|
| [976f546] | 705 |
|
|---|
| [175ad13e] | 706 | link_t *report_it = report->reports.next;
|
|---|
| 707 | usb_hid_report_description_t *report_des;
|
|---|
| 708 |
|
|---|
| 709 | while(report_it != &report->reports) {
|
|---|
| 710 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
|---|
| 711 | usb_log_debug("Report ID: %d\n", report_des->report_id);
|
|---|
| 712 | usb_log_debug("\tType: %d\n", report_des->type);
|
|---|
| 713 | usb_log_debug("\tLength: %d\n", report_des->bit_length);
|
|---|
| 714 | usb_log_debug("\tItems: %d\n", report_des->item_length);
|
|---|
| 715 |
|
|---|
| 716 | usb_hid_descriptor_print_list(&report_des->report_items);
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 | link_t *path_it = report->collection_paths.next;
|
|---|
| 720 | while(path_it != &report->collection_paths) {
|
|---|
| 721 | usb_hid_print_usage_path (list_get_instance(path_it, usb_hid_report_path_t, link));
|
|---|
| 722 | path_it = path_it->next;
|
|---|
| 723 | }
|
|---|
| 724 |
|
|---|
| 725 | report_it = report_it->next;
|
|---|
| 726 | }
|
|---|
| [976f546] | 727 | }
|
|---|
| 728 |
|
|---|
| 729 | /**
|
|---|
| 730 | * Releases whole linked list of report items
|
|---|
| 731 | *
|
|---|
| [57d9c05e] | 732 | * @param head Head of list of report descriptor items (usb_hid_report_item_t)
|
|---|
| 733 | * @return void
|
|---|
| [976f546] | 734 | */
|
|---|
| [e24e7b1] | 735 | void usb_hid_free_report_list(link_t *head)
|
|---|
| [976f546] | 736 | {
|
|---|
| [e24e7b1] | 737 | return;
|
|---|
| [e259d95] | 738 |
|
|---|
| [976f546] | 739 | usb_hid_report_item_t *report_item;
|
|---|
| [e259d95] | 740 | link_t *next;
|
|---|
| [976f546] | 741 |
|
|---|
| 742 | if(head == NULL || list_empty(head)) {
|
|---|
| [e24e7b1] | 743 | return;
|
|---|
| [976f546] | 744 | }
|
|---|
| [e259d95] | 745 |
|
|---|
| 746 | next = head->next;
|
|---|
| 747 | while(next != head) {
|
|---|
| 748 |
|
|---|
| 749 | report_item = list_get_instance(next, usb_hid_report_item_t, link);
|
|---|
| [b53d3b7] | 750 |
|
|---|
| 751 | while(!list_empty(&report_item->usage_path->link)) {
|
|---|
| 752 | usb_hid_report_remove_last_item(report_item->usage_path);
|
|---|
| 753 | }
|
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| [e259d95] | 756 | next = next->next;
|
|---|
| [976f546] | 757 |
|
|---|
| [e259d95] | 758 | free(report_item);
|
|---|
| [976f546] | 759 | }
|
|---|
| [e259d95] | 760 |
|
|---|
| [e24e7b1] | 761 | return;
|
|---|
| [e259d95] | 762 |
|
|---|
| [976f546] | 763 | }
|
|---|
| 764 |
|
|---|
| [57d9c05e] | 765 | /** Frees the HID report descriptor parser structure
|
|---|
| [19a1800] | 766 | *
|
|---|
| 767 | * @param parser Opaque HID report parser structure
|
|---|
| [57d9c05e] | 768 | * @return void
|
|---|
| [976f546] | 769 | */
|
|---|
| [175ad13e] | 770 | void usb_hid_free_report(usb_hid_report_t *report)
|
|---|
| [976f546] | 771 | {
|
|---|
| [175ad13e] | 772 | if(report == NULL){
|
|---|
| [976f546] | 773 | return;
|
|---|
| 774 | }
|
|---|
| 775 |
|
|---|
| [175ad13e] | 776 | // free collection paths
|
|---|
| 777 | usb_hid_report_path_t *path;
|
|---|
| 778 | while(!list_empty(&report->collection_paths)) {
|
|---|
| 779 | path = list_get_instance(report->collection_paths.next, usb_hid_report_path_t, link);
|
|---|
| 780 | usb_hid_report_path_free(path);
|
|---|
| 781 | }
|
|---|
| 782 |
|
|---|
| 783 | // free report items
|
|---|
| 784 | usb_hid_report_description_t *report_des;
|
|---|
| 785 | usb_hid_report_field_t *field;
|
|---|
| 786 | while(!list_empty(&report->reports)) {
|
|---|
| 787 | report_des = list_get_instance(report->reports.next, usb_hid_report_description_t, link);
|
|---|
| 788 | list_remove(&report_des->link);
|
|---|
| 789 |
|
|---|
| 790 | while(!list_empty(&report_des->report_items)) {
|
|---|
| 791 | field = list_get_instance(report_des->report_items.next, usb_hid_report_field_t, link);
|
|---|
| 792 | list_remove(&field->link);
|
|---|
| [bda06a3] | 793 |
|
|---|
| [175ad13e] | 794 | free(field);
|
|---|
| 795 | }
|
|---|
| 796 |
|
|---|
| 797 | free(report_des);
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| [976f546] | 800 | return;
|
|---|
| 801 | }
|
|---|
| [c7a2e7e] | 802 |
|
|---|
| [fad14d7] | 803 | /** Parse and act upon a HID report.
|
|---|
| 804 | *
|
|---|
| 805 | * @see usb_hid_parse_report_descriptor
|
|---|
| 806 | *
|
|---|
| 807 | * @param parser Opaque HID report parser structure.
|
|---|
| 808 | * @param data Data for the report.
|
|---|
| 809 | * @return Error code.
|
|---|
| 810 | */
|
|---|
| [175ad13e] | 811 | int usb_hid_parse_report(const usb_hid_report_t *report,
|
|---|
| [cfbbe1d3] | 812 | const uint8_t *data, size_t size, uint8_t *report_id)
|
|---|
| [fad14d7] | 813 | {
|
|---|
| 814 | link_t *list_item;
|
|---|
| [175ad13e] | 815 | usb_hid_report_field_t *item;
|
|---|
| 816 |
|
|---|
| 817 | usb_hid_report_description_t *report_des;
|
|---|
| 818 | usb_hid_report_type_t type = USB_HID_REPORT_TYPE_INPUT;
|
|---|
| [fad14d7] | 819 |
|
|---|
| [175ad13e] | 820 | if(report == NULL) {
|
|---|
| [55e388a1] | 821 | return EINVAL;
|
|---|
| 822 | }
|
|---|
| [c156c2d] | 823 |
|
|---|
| [175ad13e] | 824 | if(report->use_report_ids != 0) {
|
|---|
| [cfbbe1d3] | 825 | *report_id = data[0];
|
|---|
| 826 | }
|
|---|
| 827 | else {
|
|---|
| 828 | *report_id = 0;
|
|---|
| [c156c2d] | 829 | }
|
|---|
| 830 |
|
|---|
| [cfbbe1d3] | 831 |
|
|---|
| 832 | report_des = usb_hid_report_find_description(report, *report_id, type);
|
|---|
| [c156c2d] | 833 |
|
|---|
| [175ad13e] | 834 | /* read data */
|
|---|
| 835 | list_item = report_des->report_items.next;
|
|---|
| 836 | while(list_item != &(report_des->report_items)) {
|
|---|
| [fad14d7] | 837 |
|
|---|
| [175ad13e] | 838 | item = list_get_instance(list_item, usb_hid_report_field_t, link);
|
|---|
| [fad14d7] | 839 |
|
|---|
| [cfbbe1d3] | 840 | if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags) == 0) {
|
|---|
| [175ad13e] | 841 |
|
|---|
| [cfbbe1d3] | 842 | if(USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0) {
|
|---|
| [175ad13e] | 843 |
|
|---|
| [cfbbe1d3] | 844 | // array
|
|---|
| 845 | item->value = usb_hid_translate_data(item, data);
|
|---|
| 846 | item->usage = (item->value - item->physical_minimum) + item->usage_minimum;
|
|---|
| [fad14d7] | 847 | }
|
|---|
| [175ad13e] | 848 | else {
|
|---|
| [cfbbe1d3] | 849 | // variable item
|
|---|
| 850 | item->value = usb_hid_translate_data(item, data);
|
|---|
| 851 | }
|
|---|
| [fad14d7] | 852 | }
|
|---|
| 853 | list_item = list_item->next;
|
|---|
| 854 | }
|
|---|
| 855 |
|
|---|
| 856 | return EOK;
|
|---|
| 857 |
|
|---|
| 858 | }
|
|---|
| 859 |
|
|---|
| [57d9c05e] | 860 | /**
|
|---|
| [c156c2d] | 861 | * Translate data from the report as specified in report descriptor item
|
|---|
| [57d9c05e] | 862 | *
|
|---|
| 863 | * @param item Report descriptor item with definition of translation
|
|---|
| 864 | * @param data Data to translate
|
|---|
| 865 | * @param j Index of processed field in report descriptor item
|
|---|
| 866 | * @return Translated data
|
|---|
| 867 | */
|
|---|
| [cfbbe1d3] | 868 | int usb_hid_translate_data(usb_hid_report_field_t *item, const uint8_t *data)
|
|---|
| [c7a2e7e] | 869 | {
|
|---|
| [fad14d7] | 870 | int resolution;
|
|---|
| 871 | int offset;
|
|---|
| 872 | int part_size;
|
|---|
| 873 |
|
|---|
| [175ad13e] | 874 | int32_t value=0;
|
|---|
| [fad14d7] | 875 | int32_t mask;
|
|---|
| 876 | const uint8_t *foo;
|
|---|
| [bda06a3] | 877 |
|
|---|
| [c156c2d] | 878 | // now only shot tags are allowed
|
|---|
| [fad14d7] | 879 | if(item->size > 32) {
|
|---|
| 880 | return 0;
|
|---|
| 881 | }
|
|---|
| 882 |
|
|---|
| [cfbbe1d3] | 883 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
|
|---|
| [fad14d7] | 884 | item->physical_minimum = item->logical_minimum;
|
|---|
| [cfbbe1d3] | 885 | item->physical_maximum = item->logical_maximum;
|
|---|
| [fad14d7] | 886 | }
|
|---|
| [cfbbe1d3] | 887 |
|
|---|
| [fad14d7] | 888 |
|
|---|
| [60a228f] | 889 | if(item->physical_maximum == item->physical_minimum){
|
|---|
| 890 | resolution = 1;
|
|---|
| 891 | }
|
|---|
| 892 | else {
|
|---|
| 893 | resolution = (item->logical_maximum - item->logical_minimum) /
|
|---|
| 894 | ((item->physical_maximum - item->physical_minimum) *
|
|---|
| 895 | (usb_pow(10,(item->unit_exponent))));
|
|---|
| 896 | }
|
|---|
| [bda06a3] | 897 |
|
|---|
| [cfbbe1d3] | 898 | offset = item->offset;
|
|---|
| [fad14d7] | 899 | // FIXME
|
|---|
| [175ad13e] | 900 | if((size_t)(offset/8) != (size_t)((offset+item->size-1)/8)) {
|
|---|
| [fad14d7] | 901 |
|
|---|
| 902 | part_size = ((offset+item->size)%8);
|
|---|
| 903 |
|
|---|
| [175ad13e] | 904 | size_t i=0;
|
|---|
| 905 | for(i=(size_t)(offset/8); i<=(size_t)(offset+item->size-1)/8; i++){
|
|---|
| 906 | if(i == (size_t)(offset/8)) {
|
|---|
| 907 | // the higher one
|
|---|
| 908 | foo = data + i;
|
|---|
| 909 | mask = ((1 << (item->size-part_size))-1);
|
|---|
| 910 | value = (*foo & mask) << part_size;
|
|---|
| 911 | }
|
|---|
| 912 | else if(i == ((offset+item->size-1)/8)){
|
|---|
| 913 | // the lower one
|
|---|
| 914 | foo = data + i;
|
|---|
| 915 | mask = ((1 << part_size)-1) << (8-part_size);
|
|---|
| 916 | value += ((*foo & mask) >> (8-part_size));
|
|---|
| 917 | }
|
|---|
| 918 | else {
|
|---|
| 919 | value = value << 8;
|
|---|
| 920 | value += *(data + 1);
|
|---|
| 921 | }
|
|---|
| 922 | }
|
|---|
| [fad14d7] | 923 | }
|
|---|
| 924 | else {
|
|---|
| 925 | foo = data+(offset/8);
|
|---|
| 926 | mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
|
|---|
| 927 | value = (*foo & mask) >> (8-((offset%8)+item->size));
|
|---|
| [175ad13e] | 928 | }
|
|---|
| [fad14d7] | 929 |
|
|---|
| [175ad13e] | 930 | if(!(item->logical_minimum >= 0 && item->logical_maximum >= 0)){
|
|---|
| 931 | value = (int32_t)value;
|
|---|
| 932 | }
|
|---|
| 933 | else {
|
|---|
| 934 | value = (uint32_t)value;
|
|---|
| [fad14d7] | 935 | }
|
|---|
| 936 |
|
|---|
| [767da0a] | 937 | return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
|
|---|
| [fad14d7] | 938 |
|
|---|
| [c7a2e7e] | 939 | }
|
|---|
| [0bd4810c] | 940 |
|
|---|
| [57d9c05e] | 941 | /**
|
|---|
| [a694a58] | 942 | * Returns number of items in input report which are accessible by given usage path
|
|---|
| [57d9c05e] | 943 | *
|
|---|
| [a694a58] | 944 | * @param parser Opaque report descriptor structure
|
|---|
| 945 | * @param path Usage path specification
|
|---|
| 946 | * @param flags Usage path comparison flags
|
|---|
| 947 | * @return Number of items in input report
|
|---|
| [57d9c05e] | 948 | */
|
|---|
| [175ad13e] | 949 | size_t usb_hid_report_input_length(const usb_hid_report_t *report,
|
|---|
| [b53d3b7] | 950 | usb_hid_report_path_t *path, int flags)
|
|---|
| [55e388a1] | 951 | {
|
|---|
| [175ad13e] | 952 |
|
|---|
| [57d9c05e] | 953 | size_t ret = 0;
|
|---|
| [0bd4810c] | 954 |
|
|---|
| [175ad13e] | 955 | if(report == NULL) {
|
|---|
| [57d9c05e] | 956 | return 0;
|
|---|
| [55e388a1] | 957 | }
|
|---|
| [0bd4810c] | 958 |
|
|---|
| [175ad13e] | 959 | usb_hid_report_description_t *report_des;
|
|---|
| 960 | report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_INPUT);
|
|---|
| 961 | if(report_des == NULL) {
|
|---|
| 962 | return 0;
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | link_t *field_it = report_des->report_items.next;
|
|---|
| 966 | usb_hid_report_field_t *field;
|
|---|
| 967 | while(field_it != &report_des->report_items) {
|
|---|
| 968 |
|
|---|
| 969 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
|---|
| 970 | if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
|
|---|
| [dc9f122] | 971 |
|
|---|
| 972 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
|---|
| 973 | if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
|
|---|
| [175ad13e] | 974 | ret++;
|
|---|
| 975 | }
|
|---|
| [dc9f122] | 976 | usb_hid_report_remove_last_item (field->collection_path);
|
|---|
| [175ad13e] | 977 | }
|
|---|
| 978 |
|
|---|
| 979 | field_it = field_it->next;
|
|---|
| 980 | }
|
|---|
| [0bd4810c] | 981 |
|
|---|
| 982 | return ret;
|
|---|
| [175ad13e] | 983 | }
|
|---|
| [0bd4810c] | 984 |
|
|---|
| 985 |
|
|---|
| [b53d3b7] | 986 | /**
|
|---|
| [a694a58] | 987 | * Appends one item (couple of usage_path and usage) into the usage path
|
|---|
| 988 | * structure
|
|---|
| 989 | *
|
|---|
| 990 | * @param usage_path Usage path structure
|
|---|
| 991 | * @param usage_page Usage page constant
|
|---|
| 992 | * @param usage Usage constant
|
|---|
| 993 | * @return Error code
|
|---|
| [b53d3b7] | 994 | */
|
|---|
| 995 | int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path,
|
|---|
| 996 | int32_t usage_page, int32_t usage)
|
|---|
| 997 | {
|
|---|
| 998 | usb_hid_report_usage_path_t *item;
|
|---|
| 999 |
|
|---|
| 1000 | if(!(item=malloc(sizeof(usb_hid_report_usage_path_t)))) {
|
|---|
| 1001 | return ENOMEM;
|
|---|
| 1002 | }
|
|---|
| 1003 | list_initialize(&item->link);
|
|---|
| 1004 |
|
|---|
| 1005 | item->usage = usage;
|
|---|
| 1006 | item->usage_page = usage_page;
|
|---|
| [175ad13e] | 1007 | item->flags = 0;
|
|---|
| [b53d3b7] | 1008 |
|
|---|
| [dc9f122] | 1009 | list_append (&item->link, &usage_path->head);
|
|---|
| [b53d3b7] | 1010 | usage_path->depth++;
|
|---|
| 1011 | return EOK;
|
|---|
| 1012 | }
|
|---|
| 1013 |
|
|---|
| 1014 | /**
|
|---|
| [a694a58] | 1015 | * Removes last item from the usage path structure
|
|---|
| 1016 | * @param usage_path
|
|---|
| 1017 | * @return void
|
|---|
| [b53d3b7] | 1018 | */
|
|---|
| 1019 | void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path)
|
|---|
| 1020 | {
|
|---|
| 1021 | usb_hid_report_usage_path_t *item;
|
|---|
| 1022 |
|
|---|
| [dc9f122] | 1023 | if(!list_empty(&usage_path->head)){
|
|---|
| 1024 | item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
|
|---|
| 1025 | list_remove(usage_path->head.prev);
|
|---|
| [b53d3b7] | 1026 | usage_path->depth--;
|
|---|
| 1027 | free(item);
|
|---|
| 1028 | }
|
|---|
| 1029 | }
|
|---|
| 1030 |
|
|---|
| 1031 | /**
|
|---|
| [a694a58] | 1032 | * Nulls last item of the usage path structure.
|
|---|
| [b53d3b7] | 1033 | *
|
|---|
| [57d9c05e] | 1034 | * @param usage_path
|
|---|
| [a694a58] | 1035 | * @return void
|
|---|
| [b53d3b7] | 1036 | */
|
|---|
| 1037 | void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path)
|
|---|
| 1038 | {
|
|---|
| 1039 | usb_hid_report_usage_path_t *item;
|
|---|
| 1040 |
|
|---|
| [dc9f122] | 1041 | if(!list_empty(&usage_path->head)){
|
|---|
| 1042 | item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
|
|---|
| [b53d3b7] | 1043 | memset(item, 0, sizeof(usb_hid_report_usage_path_t));
|
|---|
| 1044 | }
|
|---|
| 1045 | }
|
|---|
| 1046 |
|
|---|
| 1047 | /**
|
|---|
| [a694a58] | 1048 | * Modifies last item of usage path structure by given usage page or usage
|
|---|
| [b53d3b7] | 1049 | *
|
|---|
| [a694a58] | 1050 | * @param usage_path Opaque usage path structure
|
|---|
| 1051 | * @param tag Class of currently processed tag (Usage page tag falls into Global
|
|---|
| 1052 | * class but Usage tag into the Local)
|
|---|
| 1053 | * @param data Value of the processed tag
|
|---|
| 1054 | * @return void
|
|---|
| [b53d3b7] | 1055 | */
|
|---|
| 1056 | void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data)
|
|---|
| 1057 | {
|
|---|
| 1058 | usb_hid_report_usage_path_t *item;
|
|---|
| 1059 |
|
|---|
| [dc9f122] | 1060 | if(!list_empty(&usage_path->head)){
|
|---|
| 1061 | item = list_get_instance(usage_path->head.prev, usb_hid_report_usage_path_t, link);
|
|---|
| [b53d3b7] | 1062 |
|
|---|
| 1063 | switch(tag) {
|
|---|
| 1064 | case USB_HID_TAG_CLASS_GLOBAL:
|
|---|
| 1065 | item->usage_page = data;
|
|---|
| 1066 | break;
|
|---|
| 1067 | case USB_HID_TAG_CLASS_LOCAL:
|
|---|
| 1068 | item->usage = data;
|
|---|
| 1069 | break;
|
|---|
| 1070 | }
|
|---|
| 1071 | }
|
|---|
| 1072 |
|
|---|
| 1073 | }
|
|---|
| 1074 |
|
|---|
| [175ad13e] | 1075 |
|
|---|
| 1076 | void usb_hid_print_usage_path(usb_hid_report_path_t *path)
|
|---|
| 1077 | {
|
|---|
| 1078 | usb_log_debug("USAGE_PATH FOR RId(%d):\n", path->report_id);
|
|---|
| 1079 | usb_log_debug("\tLENGTH: %d\n", path->depth);
|
|---|
| 1080 |
|
|---|
| [dc9f122] | 1081 | link_t *item = path->head.next;
|
|---|
| [175ad13e] | 1082 | usb_hid_report_usage_path_t *path_item;
|
|---|
| [dc9f122] | 1083 | while(item != &path->head) {
|
|---|
| [175ad13e] | 1084 |
|
|---|
| 1085 | path_item = list_get_instance(item, usb_hid_report_usage_path_t, link);
|
|---|
| 1086 | usb_log_debug("\tUSAGE_PAGE: %X\n", path_item->usage_page);
|
|---|
| 1087 | usb_log_debug("\tUSAGE: %X\n", path_item->usage);
|
|---|
| 1088 | usb_log_debug("\tFLAGS: %d\n", path_item->flags);
|
|---|
| 1089 |
|
|---|
| 1090 | item = item->next;
|
|---|
| 1091 | }
|
|---|
| 1092 | }
|
|---|
| 1093 |
|
|---|
| [b53d3b7] | 1094 | /**
|
|---|
| [a694a58] | 1095 | * Compares two usage paths structures
|
|---|
| [b53d3b7] | 1096 | *
|
|---|
| [dc9f122] | 1097 | * If USB_HID_PATH_COMPARE_COLLECTION_ONLY flag is given, the last item in report_path structure is forgotten
|
|---|
| 1098 | *
|
|---|
| [a694a58] | 1099 | * @param report_path usage path structure to compare
|
|---|
| 1100 | * @param path usage patrh structure to compare
|
|---|
| 1101 | * @param flags Flags determining the mode of comparison
|
|---|
| 1102 | * @return EOK if both paths are identical, non zero number otherwise
|
|---|
| [b53d3b7] | 1103 | */
|
|---|
| 1104 | int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path,
|
|---|
| 1105 | usb_hid_report_path_t *path,
|
|---|
| 1106 | int flags)
|
|---|
| 1107 | {
|
|---|
| 1108 | usb_hid_report_usage_path_t *report_item;
|
|---|
| 1109 | usb_hid_report_usage_path_t *path_item;
|
|---|
| 1110 |
|
|---|
| 1111 | link_t *report_link;
|
|---|
| 1112 | link_t *path_link;
|
|---|
| 1113 |
|
|---|
| 1114 | int only_page;
|
|---|
| 1115 |
|
|---|
| [bda06a3] | 1116 | if(report_path->report_id != path->report_id) {
|
|---|
| 1117 | return 1;
|
|---|
| 1118 | }
|
|---|
| 1119 |
|
|---|
| [b53d3b7] | 1120 | if(path->depth == 0){
|
|---|
| 1121 | return EOK;
|
|---|
| 1122 | }
|
|---|
| 1123 |
|
|---|
| 1124 |
|
|---|
| 1125 | if((only_page = flags & USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY) != 0){
|
|---|
| 1126 | flags -= USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY;
|
|---|
| 1127 | }
|
|---|
| 1128 |
|
|---|
| 1129 | switch(flags){
|
|---|
| 1130 | /* path must be completly identical */
|
|---|
| 1131 | case USB_HID_PATH_COMPARE_STRICT:
|
|---|
| 1132 | if(report_path->depth != path->depth){
|
|---|
| 1133 | return 1;
|
|---|
| 1134 | }
|
|---|
| 1135 |
|
|---|
| [681f24b3] | 1136 | report_link = report_path->head.next;
|
|---|
| 1137 | path_link = path->head.next;
|
|---|
| [b53d3b7] | 1138 |
|
|---|
| [681f24b3] | 1139 | while((report_link != &report_path->head) && (path_link != &path->head)) {
|
|---|
| [b53d3b7] | 1140 | report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
|
|---|
| 1141 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
|---|
| 1142 |
|
|---|
| 1143 | if((report_item->usage_page != path_item->usage_page) ||
|
|---|
| 1144 | ((only_page == 0) && (report_item->usage != path_item->usage))) {
|
|---|
| 1145 | return 1;
|
|---|
| 1146 | } else {
|
|---|
| 1147 | report_link = report_link->next;
|
|---|
| 1148 | path_link = path_link->next;
|
|---|
| 1149 | }
|
|---|
| 1150 |
|
|---|
| 1151 | }
|
|---|
| 1152 |
|
|---|
| [dc9f122] | 1153 | if(((report_link == &report_path->head) && (path_link == &path->head)) ||
|
|---|
| 1154 | (((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) && (path_link = &path->head) && (report_link == report_path->head.prev))) {
|
|---|
| [b53d3b7] | 1155 | return EOK;
|
|---|
| 1156 | }
|
|---|
| 1157 | else {
|
|---|
| 1158 | return 1;
|
|---|
| 1159 | }
|
|---|
| 1160 | break;
|
|---|
| 1161 |
|
|---|
| [96bfe76] | 1162 | /* compare with only the end of path*/
|
|---|
| [b53d3b7] | 1163 | case USB_HID_PATH_COMPARE_END:
|
|---|
| [dc9f122] | 1164 |
|
|---|
| 1165 | if((flags & USB_HID_PATH_COMPARE_COLLECTION_ONLY) != 0) {
|
|---|
| 1166 | report_link = report_path->head.prev->prev;
|
|---|
| 1167 | }
|
|---|
| 1168 | else {
|
|---|
| 1169 | report_link = report_path->head.prev;
|
|---|
| 1170 | }
|
|---|
| [681f24b3] | 1171 | path_link = path->head.prev;
|
|---|
| [b53d3b7] | 1172 |
|
|---|
| [681f24b3] | 1173 | if(list_empty(&path->head)){
|
|---|
| [b53d3b7] | 1174 | return EOK;
|
|---|
| 1175 | }
|
|---|
| 1176 |
|
|---|
| [681f24b3] | 1177 | while((report_link != &report_path->head) && (path_link != &path->head)) {
|
|---|
| [b53d3b7] | 1178 | report_item = list_get_instance(report_link, usb_hid_report_usage_path_t, link);
|
|---|
| 1179 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
|---|
| 1180 |
|
|---|
| 1181 | if((report_item->usage_page != path_item->usage_page) ||
|
|---|
| 1182 | ((only_page == 0) && (report_item->usage != path_item->usage))) {
|
|---|
| 1183 | return 1;
|
|---|
| 1184 | } else {
|
|---|
| 1185 | report_link = report_link->prev;
|
|---|
| 1186 | path_link = path_link->prev;
|
|---|
| 1187 | }
|
|---|
| 1188 |
|
|---|
| 1189 | }
|
|---|
| 1190 |
|
|---|
| [681f24b3] | 1191 | if(path_link == &path->head) {
|
|---|
| [b53d3b7] | 1192 | return EOK;
|
|---|
| 1193 | }
|
|---|
| 1194 | else {
|
|---|
| 1195 | return 1;
|
|---|
| 1196 | }
|
|---|
| 1197 |
|
|---|
| 1198 | break;
|
|---|
| 1199 |
|
|---|
| 1200 | default:
|
|---|
| 1201 | return EINVAL;
|
|---|
| 1202 | }
|
|---|
| 1203 |
|
|---|
| 1204 |
|
|---|
| 1205 |
|
|---|
| 1206 |
|
|---|
| 1207 | }
|
|---|
| 1208 |
|
|---|
| 1209 | /**
|
|---|
| [a694a58] | 1210 | * Allocates and initializes new usage path structure.
|
|---|
| [b53d3b7] | 1211 | *
|
|---|
| [a694a58] | 1212 | * @return Initialized usage path structure
|
|---|
| [b53d3b7] | 1213 | */
|
|---|
| 1214 | usb_hid_report_path_t *usb_hid_report_path(void)
|
|---|
| 1215 | {
|
|---|
| 1216 | usb_hid_report_path_t *path;
|
|---|
| 1217 | path = malloc(sizeof(usb_hid_report_path_t));
|
|---|
| [175ad13e] | 1218 | if(path == NULL){
|
|---|
| [b53d3b7] | 1219 | return NULL;
|
|---|
| 1220 | }
|
|---|
| 1221 | else {
|
|---|
| 1222 | path->depth = 0;
|
|---|
| [bda06a3] | 1223 | path->report_id = 0;
|
|---|
| [b53d3b7] | 1224 | list_initialize(&path->link);
|
|---|
| [dc9f122] | 1225 | list_initialize(&path->head);
|
|---|
| [b53d3b7] | 1226 | return path;
|
|---|
| 1227 | }
|
|---|
| 1228 | }
|
|---|
| 1229 |
|
|---|
| 1230 | /**
|
|---|
| [a694a58] | 1231 | * Releases given usage path structure.
|
|---|
| [b53d3b7] | 1232 | *
|
|---|
| [a694a58] | 1233 | * @param path usage path structure to release
|
|---|
| [57d9c05e] | 1234 | * @return void
|
|---|
| [b53d3b7] | 1235 | */
|
|---|
| 1236 | void usb_hid_report_path_free(usb_hid_report_path_t *path)
|
|---|
| 1237 | {
|
|---|
| [dc9f122] | 1238 | while(!list_empty(&path->head)){
|
|---|
| [b53d3b7] | 1239 | usb_hid_report_remove_last_item(path);
|
|---|
| 1240 | }
|
|---|
| [175ad13e] | 1241 |
|
|---|
| 1242 | list_remove(&path->link);
|
|---|
| 1243 | free(path);
|
|---|
| [b53d3b7] | 1244 | }
|
|---|
| 1245 |
|
|---|
| 1246 |
|
|---|
| 1247 | /**
|
|---|
| [96bfe76] | 1248 | * Clone content of given usage path to the new one
|
|---|
| [b53d3b7] | 1249 | *
|
|---|
| [a694a58] | 1250 | * @param usage_path Usage path structure to clone
|
|---|
| 1251 | * @return New copy of given usage path structure
|
|---|
| [b53d3b7] | 1252 | */
|
|---|
| [96bfe76] | 1253 | usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path)
|
|---|
| [b53d3b7] | 1254 | {
|
|---|
| 1255 | link_t *path_link;
|
|---|
| [175ad13e] | 1256 | usb_hid_report_usage_path_t *path_item;
|
|---|
| 1257 | usb_hid_report_usage_path_t *new_path_item;
|
|---|
| [96bfe76] | 1258 | usb_hid_report_path_t *new_usage_path = usb_hid_report_path ();
|
|---|
| [b53d3b7] | 1259 |
|
|---|
| [96bfe76] | 1260 | if(new_usage_path == NULL){
|
|---|
| 1261 | return NULL;
|
|---|
| 1262 | }
|
|---|
| [c7c0984a] | 1263 |
|
|---|
| 1264 | new_usage_path->report_id = usage_path->report_id;
|
|---|
| [b53d3b7] | 1265 |
|
|---|
| [dc9f122] | 1266 | if(list_empty(&usage_path->head)){
|
|---|
| [96bfe76] | 1267 | return new_usage_path;
|
|---|
| [b53d3b7] | 1268 | }
|
|---|
| 1269 |
|
|---|
| [dc9f122] | 1270 | path_link = usage_path->head.next;
|
|---|
| 1271 | while(path_link != &usage_path->head) {
|
|---|
| [b53d3b7] | 1272 | path_item = list_get_instance(path_link, usb_hid_report_usage_path_t, link);
|
|---|
| [175ad13e] | 1273 | new_path_item = malloc(sizeof(usb_hid_report_usage_path_t));
|
|---|
| 1274 | if(new_path_item == NULL) {
|
|---|
| 1275 | return NULL;
|
|---|
| 1276 | }
|
|---|
| [dc9f122] | 1277 |
|
|---|
| 1278 | list_initialize (&new_path_item->link);
|
|---|
| [175ad13e] | 1279 | new_path_item->usage_page = path_item->usage_page;
|
|---|
| 1280 | new_path_item->usage = path_item->usage;
|
|---|
| 1281 | new_path_item->flags = path_item->flags;
|
|---|
| 1282 |
|
|---|
| [dc9f122] | 1283 | list_append(&new_path_item->link, &new_usage_path->head);
|
|---|
| [175ad13e] | 1284 | new_usage_path->depth++;
|
|---|
| [b53d3b7] | 1285 |
|
|---|
| 1286 | path_link = path_link->next;
|
|---|
| 1287 | }
|
|---|
| 1288 |
|
|---|
| [96bfe76] | 1289 | return new_usage_path;
|
|---|
| [b53d3b7] | 1290 | }
|
|---|
| 1291 |
|
|---|
| [0bd4810c] | 1292 |
|
|---|
| [57d9c05e] | 1293 | /*** OUTPUT API **/
|
|---|
| 1294 |
|
|---|
| [a694a58] | 1295 | /**
|
|---|
| 1296 | * Allocates output report buffer for output report
|
|---|
| [57d9c05e] | 1297 | *
|
|---|
| [a694a58] | 1298 | * @param parser Report parsed structure
|
|---|
| 1299 | * @param size Size of returned buffer
|
|---|
| 1300 | * @param report_id Report id of created output report
|
|---|
| 1301 | * @return Returns allocated output buffer for specified output
|
|---|
| [57d9c05e] | 1302 | */
|
|---|
| [175ad13e] | 1303 | uint8_t *usb_hid_report_output(usb_hid_report_t *report, size_t *size, uint8_t report_id)
|
|---|
| [57d9c05e] | 1304 | {
|
|---|
| [175ad13e] | 1305 | if(report == NULL) {
|
|---|
| [57d9c05e] | 1306 | *size = 0;
|
|---|
| 1307 | return NULL;
|
|---|
| 1308 | }
|
|---|
| 1309 |
|
|---|
| [175ad13e] | 1310 | link_t *report_it = report->reports.next;
|
|---|
| 1311 | usb_hid_report_description_t *report_des = NULL;
|
|---|
| 1312 | while(report_it != &report->reports) {
|
|---|
| 1313 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
|---|
| [dc9f122] | 1314 | if((report_des->report_id == report_id) && (report_des->type == USB_HID_REPORT_TYPE_OUTPUT)){
|
|---|
| [a694a58] | 1315 | break;
|
|---|
| [c156c2d] | 1316 | }
|
|---|
| 1317 |
|
|---|
| [175ad13e] | 1318 | report_it = report_it->next;
|
|---|
| 1319 | }
|
|---|
| [841e6e5] | 1320 |
|
|---|
| [175ad13e] | 1321 | if(report_des == NULL){
|
|---|
| 1322 | *size = 0;
|
|---|
| 1323 | return NULL;
|
|---|
| [57d9c05e] | 1324 | }
|
|---|
| 1325 | else {
|
|---|
| [175ad13e] | 1326 | *size = (report_des->bit_length + (8 - 1))/8;
|
|---|
| 1327 | uint8_t *ret = malloc((*size) * sizeof(uint8_t));
|
|---|
| 1328 | memset(ret, 0, (*size) * sizeof(uint8_t));
|
|---|
| 1329 | return ret;
|
|---|
| [57d9c05e] | 1330 | }
|
|---|
| 1331 | }
|
|---|
| 1332 |
|
|---|
| 1333 |
|
|---|
| 1334 | /** Frees output report buffer
|
|---|
| 1335 | *
|
|---|
| 1336 | * @param output Output report buffer
|
|---|
| [a694a58] | 1337 | * @return void
|
|---|
| [57d9c05e] | 1338 | */
|
|---|
| 1339 | void usb_hid_report_output_free(uint8_t *output)
|
|---|
| [841e6e5] | 1340 |
|
|---|
| [57d9c05e] | 1341 | {
|
|---|
| 1342 | if(output != NULL) {
|
|---|
| 1343 | free (output);
|
|---|
| 1344 | }
|
|---|
| 1345 | }
|
|---|
| 1346 |
|
|---|
| 1347 | /** Returns size of output for given usage path
|
|---|
| 1348 | *
|
|---|
| [a694a58] | 1349 | * @param parser Opaque report parser structure
|
|---|
| 1350 | * @param path Usage path specified which items will be thought for the output
|
|---|
| 1351 | * @param flags Flags of usage path structure comparison
|
|---|
| 1352 | * @return Number of items matching the given usage path
|
|---|
| [57d9c05e] | 1353 | */
|
|---|
| [175ad13e] | 1354 | size_t usb_hid_report_output_size(usb_hid_report_t *report,
|
|---|
| [57d9c05e] | 1355 | usb_hid_report_path_t *path, int flags)
|
|---|
| 1356 | {
|
|---|
| [175ad13e] | 1357 | size_t ret = 0;
|
|---|
| 1358 | usb_hid_report_description_t *report_des;
|
|---|
| [57d9c05e] | 1359 |
|
|---|
| [175ad13e] | 1360 | if(report == NULL) {
|
|---|
| [57d9c05e] | 1361 | return 0;
|
|---|
| 1362 | }
|
|---|
| [bda06a3] | 1363 |
|
|---|
| [175ad13e] | 1364 | report_des = usb_hid_report_find_description (report, path->report_id, USB_HID_REPORT_TYPE_OUTPUT);
|
|---|
| 1365 | if(report_des == NULL){
|
|---|
| 1366 | return 0;
|
|---|
| 1367 | }
|
|---|
| [dc9f122] | 1368 |
|
|---|
| [175ad13e] | 1369 | link_t *field_it = report_des->report_items.next;
|
|---|
| 1370 | usb_hid_report_field_t *field;
|
|---|
| 1371 | while(field_it != &report_des->report_items) {
|
|---|
| 1372 |
|
|---|
| 1373 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
|---|
| [e50cd7f] | 1374 | if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0){
|
|---|
| 1375 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
|---|
| 1376 | if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK) {
|
|---|
| 1377 | ret++;
|
|---|
| 1378 | }
|
|---|
| 1379 | usb_hid_report_remove_last_item (field->collection_path);
|
|---|
| [175ad13e] | 1380 | }
|
|---|
| 1381 |
|
|---|
| 1382 | field_it = field_it->next;
|
|---|
| [c156c2d] | 1383 | }
|
|---|
| [57d9c05e] | 1384 |
|
|---|
| 1385 | return ret;
|
|---|
| 1386 |
|
|---|
| 1387 | }
|
|---|
| 1388 |
|
|---|
| [175ad13e] | 1389 | /** Makes the output report buffer for data given in the report structure
|
|---|
| [57d9c05e] | 1390 | *
|
|---|
| [a694a58] | 1391 | * @param parser Opaque report parser structure
|
|---|
| 1392 | * @param path Usage path specifing which parts of output will be set
|
|---|
| 1393 | * @param flags Usage path structure comparison flags
|
|---|
| 1394 | * @param buffer Output buffer
|
|---|
| 1395 | * @param size Size of output buffer
|
|---|
| 1396 | * @return Error code
|
|---|
| [57d9c05e] | 1397 | */
|
|---|
| [175ad13e] | 1398 | int usb_hid_report_output_translate(usb_hid_report_t *report, uint8_t report_id,
|
|---|
| 1399 | uint8_t *buffer, size_t size)
|
|---|
| [57d9c05e] | 1400 | {
|
|---|
| 1401 | link_t *item;
|
|---|
| 1402 | int32_t value=0;
|
|---|
| [841e6e5] | 1403 | int offset;
|
|---|
| 1404 | int length;
|
|---|
| 1405 | int32_t tmp_value;
|
|---|
| [57d9c05e] | 1406 |
|
|---|
| [175ad13e] | 1407 | if(report == NULL) {
|
|---|
| [57d9c05e] | 1408 | return EINVAL;
|
|---|
| 1409 | }
|
|---|
| 1410 |
|
|---|
| [175ad13e] | 1411 | if(report->use_report_ids != 0) {
|
|---|
| 1412 | buffer[0] = report_id;
|
|---|
| [bda06a3] | 1413 | }
|
|---|
| 1414 |
|
|---|
| [70a71e5] | 1415 | usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
|
|---|
| [cfbbe1d3] | 1416 |
|
|---|
| [175ad13e] | 1417 | usb_hid_report_description_t *report_des;
|
|---|
| 1418 | report_des = usb_hid_report_find_description (report, report_id, USB_HID_REPORT_TYPE_OUTPUT);
|
|---|
| 1419 | if(report_des == NULL){
|
|---|
| 1420 | return EINVAL;
|
|---|
| 1421 | }
|
|---|
| [57d9c05e] | 1422 |
|
|---|
| [175ad13e] | 1423 | usb_hid_report_field_t *report_item;
|
|---|
| 1424 | item = report_des->report_items.next;
|
|---|
| 1425 | while(item != &report_des->report_items) {
|
|---|
| 1426 | report_item = list_get_instance(item, usb_hid_report_field_t, link);
|
|---|
| [841e6e5] | 1427 |
|
|---|
| [cfbbe1d3] | 1428 | if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
|
|---|
| [d012590] | 1429 |
|
|---|
| [cfbbe1d3] | 1430 | // array
|
|---|
| [175ad13e] | 1431 | value = usb_hid_translate_data_reverse(report_item, report_item->value);
|
|---|
| 1432 | offset = report_item->offset;
|
|---|
| [841e6e5] | 1433 | length = report_item->size;
|
|---|
| 1434 | }
|
|---|
| 1435 | else {
|
|---|
| [cfbbe1d3] | 1436 | // variable item
|
|---|
| 1437 | value = usb_hid_translate_data_reverse(report_item, report_item->value);
|
|---|
| [175ad13e] | 1438 | offset = report_item->offset;
|
|---|
| 1439 | length = report_item->size;
|
|---|
| [841e6e5] | 1440 | }
|
|---|
| 1441 |
|
|---|
| [d012590] | 1442 | if((offset/8) == ((offset+length-1)/8)) {
|
|---|
| [841e6e5] | 1443 | // je to v jednom bytu
|
|---|
| [d012590] | 1444 | if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {
|
|---|
| [841e6e5] | 1445 | break; // TODO ErrorCode
|
|---|
| 1446 | }
|
|---|
| 1447 |
|
|---|
| [2f4b3a4] | 1448 | size_t shift = offset%8;
|
|---|
| [841e6e5] | 1449 |
|
|---|
| [d012590] | 1450 | value = value << shift;
|
|---|
| 1451 | value = value & (((1 << length)-1) << shift);
|
|---|
| [70a71e5] | 1452 |
|
|---|
| 1453 | uint8_t mask = 0;
|
|---|
| 1454 | mask = 0xff - (((1 << length) - 1) << shift);
|
|---|
| 1455 | buffer[offset/8] = (buffer[offset/8] & mask) | value;
|
|---|
| [841e6e5] | 1456 | }
|
|---|
| 1457 | else {
|
|---|
| [175ad13e] | 1458 | int i = 0;
|
|---|
| [70a71e5] | 1459 | uint8_t mask = 0;
|
|---|
| [175ad13e] | 1460 | for(i = (offset/8); i <= ((offset+length-1)/8); i++) {
|
|---|
| 1461 | if(i == (offset/8)) {
|
|---|
| 1462 | tmp_value = value;
|
|---|
| 1463 | tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);
|
|---|
| 1464 | tmp_value = tmp_value << (offset%8);
|
|---|
| 1465 |
|
|---|
| 1466 | mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));
|
|---|
| 1467 | buffer[i] = (buffer[i] & mask) | tmp_value;
|
|---|
| 1468 | }
|
|---|
| 1469 | else if (i == ((offset + length -1)/8)) {
|
|---|
| 1470 |
|
|---|
| 1471 | value = value >> (length - ((offset + length) % 8));
|
|---|
| 1472 | value = value & ((1 << (length - ((offset + length) % 8))) - 1);
|
|---|
| [d012590] | 1473 |
|
|---|
| [175ad13e] | 1474 | mask = (1 << (length - ((offset + length) % 8))) - 1;
|
|---|
| 1475 | buffer[i] = (buffer[i] & mask) | value;
|
|---|
| 1476 | }
|
|---|
| 1477 | else {
|
|---|
| 1478 | buffer[i] = value & (0xFF << i);
|
|---|
| 1479 | }
|
|---|
| 1480 | }
|
|---|
| [841e6e5] | 1481 | }
|
|---|
| [57d9c05e] | 1482 |
|
|---|
| [841e6e5] | 1483 |
|
|---|
| [cfbbe1d3] | 1484 | // reset value
|
|---|
| 1485 | report_item->value = 0;
|
|---|
| 1486 |
|
|---|
| [841e6e5] | 1487 | item = item->next;
|
|---|
| [57d9c05e] | 1488 | }
|
|---|
| [cfbbe1d3] | 1489 |
|
|---|
| [70a71e5] | 1490 | usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));
|
|---|
| [841e6e5] | 1491 |
|
|---|
| [57d9c05e] | 1492 | return EOK;
|
|---|
| 1493 | }
|
|---|
| 1494 |
|
|---|
| [841e6e5] | 1495 | /**
|
|---|
| [a694a58] | 1496 | * Translate given data for putting them into the outoput report
|
|---|
| 1497 | * @param item Report item structure
|
|---|
| 1498 | * @param value Value to translate
|
|---|
| 1499 | * @return ranslated value
|
|---|
| [841e6e5] | 1500 | */
|
|---|
| [175ad13e] | 1501 | uint32_t usb_hid_translate_data_reverse(usb_hid_report_field_t *item, int value)
|
|---|
| [841e6e5] | 1502 | {
|
|---|
| 1503 | int ret=0;
|
|---|
| 1504 | int resolution;
|
|---|
| 1505 |
|
|---|
| [70a71e5] | 1506 | if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {
|
|---|
| 1507 | ret = item->logical_minimum;
|
|---|
| 1508 | }
|
|---|
| 1509 |
|
|---|
| [cfbbe1d3] | 1510 | if((item->physical_minimum == 0) && (item->physical_maximum == 0)){
|
|---|
| 1511 | item->physical_minimum = item->logical_minimum;
|
|---|
| 1512 | item->physical_maximum = item->logical_maximum;
|
|---|
| 1513 | }
|
|---|
| 1514 |
|
|---|
| 1515 |
|
|---|
| [d012590] | 1516 | if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {
|
|---|
| [841e6e5] | 1517 |
|
|---|
| 1518 | // variable item
|
|---|
| 1519 | if(item->physical_maximum == item->physical_minimum){
|
|---|
| 1520 | resolution = 1;
|
|---|
| 1521 | }
|
|---|
| 1522 | else {
|
|---|
| 1523 | resolution = (item->logical_maximum - item->logical_minimum) /
|
|---|
| 1524 | ((item->physical_maximum - item->physical_minimum) *
|
|---|
| 1525 | (usb_pow(10,(item->unit_exponent))));
|
|---|
| 1526 | }
|
|---|
| 1527 |
|
|---|
| 1528 | ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;
|
|---|
| 1529 | }
|
|---|
| 1530 | else {
|
|---|
| 1531 | // bitmapa
|
|---|
| 1532 | if(value == 0) {
|
|---|
| 1533 | ret = 0;
|
|---|
| 1534 | }
|
|---|
| 1535 | else {
|
|---|
| 1536 | size_t bitmap_idx = (value - item->usage_minimum);
|
|---|
| 1537 | ret = 1 << bitmap_idx;
|
|---|
| 1538 | }
|
|---|
| 1539 | }
|
|---|
| 1540 |
|
|---|
| 1541 |
|
|---|
| [175ad13e] | 1542 | return (uint32_t)ret;
|
|---|
| [841e6e5] | 1543 | }
|
|---|
| 1544 |
|
|---|
| [a694a58] | 1545 | /**
|
|---|
| 1546 | * Sets report id in usage path structure
|
|---|
| 1547 | *
|
|---|
| 1548 | * @param path Usage path structure
|
|---|
| 1549 | * @param report_id Report id to set
|
|---|
| 1550 | * @return Error code
|
|---|
| 1551 | */
|
|---|
| [bda06a3] | 1552 | int usb_hid_report_path_set_report_id(usb_hid_report_path_t *path, uint8_t report_id)
|
|---|
| 1553 | {
|
|---|
| 1554 | if(path == NULL){
|
|---|
| 1555 | return EINVAL;
|
|---|
| 1556 | }
|
|---|
| 1557 |
|
|---|
| 1558 | path->report_id = report_id;
|
|---|
| 1559 | return EOK;
|
|---|
| 1560 | }
|
|---|
| 1561 |
|
|---|
| [175ad13e] | 1562 | /**
|
|---|
| 1563 | *
|
|---|
| 1564 | *
|
|---|
| 1565 | *
|
|---|
| 1566 | *
|
|---|
| 1567 | *
|
|---|
| 1568 | */
|
|---|
| 1569 | int usb_hid_report_output_set_data(usb_hid_report_t *report,
|
|---|
| 1570 | usb_hid_report_path_t *path, int flags,
|
|---|
| 1571 | int *data, size_t data_size)
|
|---|
| 1572 | {
|
|---|
| 1573 | size_t data_idx = 0;
|
|---|
| 1574 | if(report == NULL){
|
|---|
| 1575 | return EINVAL;
|
|---|
| 1576 | }
|
|---|
| 1577 |
|
|---|
| 1578 | usb_hid_report_description_t *report_des;
|
|---|
| 1579 | report_des = usb_hid_report_find_description (report, path->report_id,
|
|---|
| 1580 | USB_HID_REPORT_TYPE_OUTPUT);
|
|---|
| 1581 | if(report_des == NULL){
|
|---|
| 1582 | return EINVAL;
|
|---|
| 1583 | }
|
|---|
| 1584 |
|
|---|
| 1585 | usb_hid_report_field_t *field;
|
|---|
| 1586 | link_t *field_it = report_des->report_items.next;
|
|---|
| 1587 | while(field_it != &report_des->report_items){
|
|---|
| 1588 |
|
|---|
| 1589 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
|---|
| 1590 | if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
|
|---|
| [dc9f122] | 1591 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
|---|
| 1592 | if(usb_hid_report_compare_usage_path (field->collection_path, path,
|
|---|
| [175ad13e] | 1593 | flags) == EOK) {
|
|---|
| 1594 | if(data_idx < data_size) {
|
|---|
| [cfbbe1d3] | 1595 | if((data[data_idx] >= field->physical_minimum) && (data[data_idx] >= field->physical_minimum)) {
|
|---|
| 1596 | field->value = data[data_idx];
|
|---|
| 1597 | }
|
|---|
| 1598 | else {
|
|---|
| 1599 | return ERANGE;
|
|---|
| 1600 | }
|
|---|
| 1601 |
|
|---|
| 1602 | data_idx++;
|
|---|
| [175ad13e] | 1603 | }
|
|---|
| 1604 | else {
|
|---|
| 1605 | field->value = 0;
|
|---|
| 1606 | }
|
|---|
| 1607 | }
|
|---|
| [dc9f122] | 1608 | usb_hid_report_remove_last_item (field->collection_path);
|
|---|
| [175ad13e] | 1609 | }
|
|---|
| 1610 |
|
|---|
| 1611 | field_it = field_it->next;
|
|---|
| 1612 | }
|
|---|
| 1613 |
|
|---|
| 1614 | return EOK;
|
|---|
| 1615 | }
|
|---|
| 1616 |
|
|---|
| [64dbc83] | 1617 |
|
|---|
| 1618 | usb_hid_report_item_t *usb_hid_report_item_clone(const usb_hid_report_item_t *item)
|
|---|
| 1619 | {
|
|---|
| 1620 | usb_hid_report_item_t *new_report_item;
|
|---|
| 1621 |
|
|---|
| 1622 | if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
|
|---|
| 1623 | return NULL;
|
|---|
| 1624 | }
|
|---|
| 1625 | memcpy(new_report_item,item, sizeof(usb_hid_report_item_t));
|
|---|
| 1626 | link_initialize(&(new_report_item->link));
|
|---|
| 1627 |
|
|---|
| 1628 | return new_report_item;
|
|---|
| 1629 | }
|
|---|
| 1630 |
|
|---|
| [e50cd7f] | 1631 |
|
|---|
| 1632 | usb_hid_report_field_t *usb_hid_report_get_sibling(usb_hid_report_t *report,
|
|---|
| 1633 | usb_hid_report_field_t *field,
|
|---|
| 1634 | usb_hid_report_path_t *path, int flags,
|
|---|
| 1635 | usb_hid_report_type_t type)
|
|---|
| 1636 | {
|
|---|
| 1637 | usb_hid_report_description_t *report_des = usb_hid_report_find_description (report, path->report_id, type);
|
|---|
| 1638 | link_t *field_it;
|
|---|
| 1639 |
|
|---|
| 1640 | if(report_des == NULL){
|
|---|
| 1641 | return NULL;
|
|---|
| 1642 | }
|
|---|
| 1643 |
|
|---|
| 1644 | if(field == NULL){
|
|---|
| 1645 | // vezmu prvni co mathuje podle path!!
|
|---|
| 1646 | field_it = report_des->report_items.next;
|
|---|
| 1647 | }
|
|---|
| 1648 | else {
|
|---|
| 1649 | field_it = field->link.next;
|
|---|
| 1650 | }
|
|---|
| 1651 |
|
|---|
| 1652 | while(field_it != &report_des->report_items) {
|
|---|
| 1653 | field = list_get_instance(field_it, usb_hid_report_field_t, link);
|
|---|
| [c7c0984a] | 1654 |
|
|---|
| 1655 | if(USB_HID_ITEM_FLAG_CONSTANT(field->item_flags) == 0) {
|
|---|
| 1656 | usb_hid_report_path_append_item (field->collection_path, field->usage_page, field->usage);
|
|---|
| 1657 | if(usb_hid_report_compare_usage_path (field->collection_path, path, flags) == EOK){
|
|---|
| 1658 | usb_hid_report_remove_last_item (field->collection_path);
|
|---|
| 1659 | return field;
|
|---|
| 1660 | }
|
|---|
| [e50cd7f] | 1661 | usb_hid_report_remove_last_item (field->collection_path);
|
|---|
| 1662 | }
|
|---|
| 1663 | field_it = field_it->next;
|
|---|
| 1664 | }
|
|---|
| 1665 |
|
|---|
| 1666 | return NULL;
|
|---|
| 1667 | }
|
|---|
| [cfbbe1d3] | 1668 |
|
|---|
| 1669 | uint8_t usb_hid_report_get_report_id(usb_hid_report_t *report, uint8_t report_id, usb_hid_report_type_t type)
|
|---|
| 1670 | {
|
|---|
| 1671 | if(report == NULL){
|
|---|
| 1672 | return 0;
|
|---|
| 1673 | }
|
|---|
| 1674 |
|
|---|
| 1675 | usb_hid_report_description_t *report_des;
|
|---|
| 1676 | link_t *report_it;
|
|---|
| 1677 |
|
|---|
| 1678 | if(report_id == 0) {
|
|---|
| 1679 | report_it = usb_hid_report_find_description (report, report_id, type)->link.next;
|
|---|
| 1680 | }
|
|---|
| 1681 | else {
|
|---|
| 1682 | report_it = report->reports.next;
|
|---|
| 1683 | }
|
|---|
| 1684 |
|
|---|
| 1685 | while(report_it != &report->reports) {
|
|---|
| 1686 | report_des = list_get_instance(report_it, usb_hid_report_description_t, link);
|
|---|
| 1687 | if(report_des->type == type){
|
|---|
| 1688 | return report_des->report_id;
|
|---|
| 1689 | }
|
|---|
| 1690 | }
|
|---|
| 1691 |
|
|---|
| 1692 | return 0;
|
|---|
| 1693 | }
|
|---|
| 1694 |
|
|---|
| [2020927] | 1695 | void usb_hid_report_reset_local_items(usb_hid_report_item_t *report_item)
|
|---|
| 1696 | {
|
|---|
| 1697 | if(report_item == NULL) {
|
|---|
| 1698 | return;
|
|---|
| 1699 | }
|
|---|
| 1700 |
|
|---|
| 1701 | report_item->usages_count = 0;
|
|---|
| 1702 | memset(report_item->usages, 0, USB_HID_MAX_USAGES);
|
|---|
| 1703 |
|
|---|
| 1704 | report_item->extended_usage_page = 0;
|
|---|
| 1705 | report_item->usage_minimum = 0;
|
|---|
| 1706 | report_item->usage_maximum = 0;
|
|---|
| 1707 | report_item->designator_index = 0;
|
|---|
| 1708 | report_item->designator_minimum = 0;
|
|---|
| 1709 | report_item->designator_maximum = 0;
|
|---|
| 1710 | report_item->string_index = 0;
|
|---|
| 1711 | report_item->string_minimum = 0;
|
|---|
| 1712 | report_item->string_maximum = 0;
|
|---|
| [cfbbe1d3] | 1713 |
|
|---|
| [2020927] | 1714 | return;
|
|---|
| 1715 | }
|
|---|
| [976f546] | 1716 | /**
|
|---|
| 1717 | * @}
|
|---|
| [c7a2e7e] | 1718 | */
|
|---|