| 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 |
|
|---|
| 29 | /** @addtogroup libusb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * USB HID report descriptor and report data parser
|
|---|
| 34 | */
|
|---|
| 35 | #ifndef LIBUSB_HIDPARSER_H_
|
|---|
| 36 | #define LIBUSB_HIDPARSER_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <stdint.h>
|
|---|
| 39 | #include <adt/list.h>
|
|---|
| 40 | #include <usb/classes/hid_report_items.h>
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Item prefix
|
|---|
| 44 | */
|
|---|
| 45 | #define USB_HID_ITEM_SIZE(data) ((uint8_t)(data & 0x3))
|
|---|
| 46 | #define USB_HID_ITEM_TAG(data) ((uint8_t)((data & 0xF0) >> 4))
|
|---|
| 47 | #define USB_HID_ITEM_TAG_CLASS(data) ((uint8_t)((data & 0xC) >> 2))
|
|---|
| 48 | #define USB_HID_ITEM_IS_LONG(data) (data == 0xFE)
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Input/Output/Feature Item flags
|
|---|
| 53 | */
|
|---|
| 54 | /** Constant (1) / Variable (0) */
|
|---|
| 55 | #define USB_HID_ITEM_FLAG_CONSTANT(flags) ((flags & 0x1) == 0x1)
|
|---|
| 56 | /** Variable (1) / Array (0) */
|
|---|
| 57 | #define USB_HID_ITEM_FLAG_VARIABLE(flags) ((flags & 0x2) == 0x2)
|
|---|
| 58 | /** Absolute / Relative*/
|
|---|
| 59 | #define USB_HID_ITEM_FLAG_RELATIVE(flags) ((flags & 0x4) == 0x4)
|
|---|
| 60 | /** Wrap / No Wrap */
|
|---|
| 61 | #define USB_HID_ITEM_FLAG_WRAP(flags) ((flags & 0x8) == 0x8)
|
|---|
| 62 | #define USB_HID_ITEM_FLAG_LINEAR(flags) ((flags & 0x10) == 0x10)
|
|---|
| 63 | #define USB_HID_ITEM_FLAG_PREFERRED(flags) ((flags & 0x20) == 0x20)
|
|---|
| 64 | #define USB_HID_ITEM_FLAG_POSITION(flags) ((flags & 0x40) == 0x40)
|
|---|
| 65 | #define USB_HID_ITEM_FLAG_VOLATILE(flags) ((flags & 0x80) == 0x80)
|
|---|
| 66 | #define USB_HID_ITEM_FLAG_BUFFERED(flags) ((flags & 0x100) == 0x100)
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Description of path of usage pages and usages in report descriptor
|
|---|
| 71 | */
|
|---|
| 72 | #define USB_HID_PATH_COMPARE_STRICT 0
|
|---|
| 73 | #define USB_HID_PATH_COMPARE_END 1
|
|---|
| 74 | #define USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY 4
|
|---|
| 75 |
|
|---|
| 76 | /** */
|
|---|
| 77 | typedef struct {
|
|---|
| 78 | /** */
|
|---|
| 79 | int32_t usage_page;
|
|---|
| 80 | /** */
|
|---|
| 81 | int32_t usage;
|
|---|
| 82 | /** */
|
|---|
| 83 | link_t link;
|
|---|
| 84 | } usb_hid_report_usage_path_t;
|
|---|
| 85 |
|
|---|
| 86 | /** */
|
|---|
| 87 | typedef struct {
|
|---|
| 88 | /** */
|
|---|
| 89 | int depth;
|
|---|
| 90 | uint8_t report_id;
|
|---|
| 91 |
|
|---|
| 92 | /** */
|
|---|
| 93 | link_t link;
|
|---|
| 94 |
|
|---|
| 95 | } usb_hid_report_path_t;
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Description of report items
|
|---|
| 99 | */
|
|---|
| 100 | typedef struct {
|
|---|
| 101 | /** */
|
|---|
| 102 | int32_t id;
|
|---|
| 103 | /** */
|
|---|
| 104 | int32_t usage_minimum;
|
|---|
| 105 | /** */
|
|---|
| 106 | int32_t usage_maximum;
|
|---|
| 107 | /** */
|
|---|
| 108 | int32_t logical_minimum;
|
|---|
| 109 | /** */
|
|---|
| 110 | int32_t logical_maximum;
|
|---|
| 111 | /** */
|
|---|
| 112 | int32_t size;
|
|---|
| 113 | /** */
|
|---|
| 114 | int32_t count;
|
|---|
| 115 | /** */
|
|---|
| 116 | size_t offset;
|
|---|
| 117 | /** */
|
|---|
| 118 | int32_t delimiter;
|
|---|
| 119 | /** */
|
|---|
| 120 | int32_t unit_exponent;
|
|---|
| 121 | /** */
|
|---|
| 122 | int32_t unit;
|
|---|
| 123 |
|
|---|
| 124 | /** */
|
|---|
| 125 | int32_t string_index;
|
|---|
| 126 | /** */
|
|---|
| 127 | int32_t string_minimum;
|
|---|
| 128 | /** */
|
|---|
| 129 | int32_t string_maximum;
|
|---|
| 130 | /** */
|
|---|
| 131 | int32_t designator_index;
|
|---|
| 132 | /** */
|
|---|
| 133 | int32_t designator_minimum;
|
|---|
| 134 | /** */
|
|---|
| 135 | int32_t designator_maximum;
|
|---|
| 136 | /** */
|
|---|
| 137 | int32_t physical_minimum;
|
|---|
| 138 | /** */
|
|---|
| 139 | int32_t physical_maximum;
|
|---|
| 140 |
|
|---|
| 141 | /** */
|
|---|
| 142 | uint8_t item_flags;
|
|---|
| 143 |
|
|---|
| 144 | /** */
|
|---|
| 145 | usb_hid_report_path_t *usage_path;
|
|---|
| 146 | /** */
|
|---|
| 147 | link_t link;
|
|---|
| 148 | } usb_hid_report_item_t;
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /** HID report parser structure. */
|
|---|
| 152 | typedef struct {
|
|---|
| 153 | /** */
|
|---|
| 154 | link_t input;
|
|---|
| 155 | /** */
|
|---|
| 156 | link_t output;
|
|---|
| 157 | /** */
|
|---|
| 158 | link_t feature;
|
|---|
| 159 |
|
|---|
| 160 | int use_report_id;
|
|---|
| 161 |
|
|---|
| 162 | /** */
|
|---|
| 163 | link_t stack;
|
|---|
| 164 | } usb_hid_report_parser_t;
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | /** HID parser callbacks for IN items. */
|
|---|
| 168 | typedef struct {
|
|---|
| 169 | /** Callback for keyboard.
|
|---|
| 170 | *
|
|---|
| 171 | * @param key_codes Array of pressed key (including modifiers).
|
|---|
| 172 | * @param count Length of @p key_codes.
|
|---|
| 173 | * @param arg Custom argument.
|
|---|
| 174 | */
|
|---|
| 175 | void (*keyboard)(const uint8_t *key_codes, size_t count, const uint8_t report_id, void *arg);
|
|---|
| 176 | } usb_hid_report_in_callbacks_t;
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 | typedef enum {
|
|---|
| 180 | USB_HID_MOD_LCTRL = 0x01,
|
|---|
| 181 | USB_HID_MOD_LSHIFT = 0x02,
|
|---|
| 182 | USB_HID_MOD_LALT = 0x04,
|
|---|
| 183 | USB_HID_MOD_LGUI = 0x08,
|
|---|
| 184 | USB_HID_MOD_RCTRL = 0x10,
|
|---|
| 185 | USB_HID_MOD_RSHIFT = 0x20,
|
|---|
| 186 | USB_HID_MOD_RALT = 0x40,
|
|---|
| 187 | USB_HID_MOD_RGUI = 0x80,
|
|---|
| 188 | USB_HID_MOD_COUNT = 8
|
|---|
| 189 | } usb_hid_modifiers_t;
|
|---|
| 190 |
|
|---|
| 191 | //typedef enum {
|
|---|
| 192 | // USB_HID_LED_NUM_LOCK = 0x1,
|
|---|
| 193 | // USB_HID_LED_CAPS_LOCK = 0x2,
|
|---|
| 194 | // USB_HID_LED_SCROLL_LOCK = 0x4,
|
|---|
| 195 | // USB_HID_LED_COMPOSE = 0x8,
|
|---|
| 196 | // USB_HID_LED_KANA = 0x10,
|
|---|
| 197 | // USB_HID_LED_COUNT = 5
|
|---|
| 198 | //} usb_hid_led_t;
|
|---|
| 199 |
|
|---|
| 200 | static const usb_hid_modifiers_t
|
|---|
| 201 | usb_hid_modifiers_consts[USB_HID_MOD_COUNT] = {
|
|---|
| 202 | USB_HID_MOD_LCTRL,
|
|---|
| 203 | USB_HID_MOD_LSHIFT,
|
|---|
| 204 | USB_HID_MOD_LALT,
|
|---|
| 205 | USB_HID_MOD_LGUI,
|
|---|
| 206 | USB_HID_MOD_RCTRL,
|
|---|
| 207 | USB_HID_MOD_RSHIFT,
|
|---|
| 208 | USB_HID_MOD_RALT,
|
|---|
| 209 | USB_HID_MOD_RGUI
|
|---|
| 210 | };
|
|---|
| 211 |
|
|---|
| 212 | //static const usb_hid_led_t usb_hid_led_consts[USB_HID_LED_COUNT] = {
|
|---|
| 213 | // USB_HID_LED_NUM_LOCK,
|
|---|
| 214 | // USB_HID_LED_CAPS_LOCK,
|
|---|
| 215 | // USB_HID_LED_SCROLL_LOCK,
|
|---|
| 216 | // USB_HID_LED_COMPOSE,
|
|---|
| 217 | // USB_HID_LED_KANA
|
|---|
| 218 | //};
|
|---|
| 219 |
|
|---|
| 220 | //#define USB_HID_BOOT_KEYBOARD_NUM_LOCK 0x01
|
|---|
| 221 | //#define USB_HID_BOOT_KEYBOARD_CAPS_LOCK 0x02
|
|---|
| 222 | //#define USB_HID_BOOT_KEYBOARD_SCROLL_LOCK 0x04
|
|---|
| 223 | //#define USB_HID_BOOT_KEYBOARD_COMPOSE 0x08
|
|---|
| 224 | //#define USB_HID_BOOT_KEYBOARD_KANA 0x10
|
|---|
| 225 |
|
|---|
| 226 | /*
|
|---|
| 227 | * Descriptor parser functions
|
|---|
| 228 | */
|
|---|
| 229 | /** */
|
|---|
| 230 | int usb_hid_parser_init(usb_hid_report_parser_t *parser);
|
|---|
| 231 |
|
|---|
| 232 | /** */
|
|---|
| 233 | int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
|
|---|
| 234 | const uint8_t *data, size_t size);
|
|---|
| 235 |
|
|---|
| 236 | /** */
|
|---|
| 237 | void usb_hid_free_report_parser(usb_hid_report_parser_t *parser);
|
|---|
| 238 |
|
|---|
| 239 | /** */
|
|---|
| 240 | void usb_hid_descriptor_print(usb_hid_report_parser_t *parser);
|
|---|
| 241 |
|
|---|
| 242 | /*
|
|---|
| 243 | * Boot protocol functions
|
|---|
| 244 | */
|
|---|
| 245 | /** */
|
|---|
| 246 | int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
|
|---|
| 247 | const usb_hid_report_in_callbacks_t *callbacks, void *arg);
|
|---|
| 248 |
|
|---|
| 249 | /** */
|
|---|
| 250 | int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size);
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 | /*
|
|---|
| 254 | * Input report parser functions
|
|---|
| 255 | */
|
|---|
| 256 | /** */
|
|---|
| 257 | int usb_hid_parse_report(const usb_hid_report_parser_t *parser,
|
|---|
| 258 | const uint8_t *data, size_t size,
|
|---|
| 259 | usb_hid_report_path_t *path, int flags,
|
|---|
| 260 | const usb_hid_report_in_callbacks_t *callbacks, void *arg);
|
|---|
| 261 |
|
|---|
| 262 | /** */
|
|---|
| 263 | size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
|
|---|
| 264 | usb_hid_report_path_t *path, int flags);
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 | /*
|
|---|
| 269 | * usage path functions
|
|---|
| 270 | */
|
|---|
| 271 | /** */
|
|---|
| 272 | usb_hid_report_path_t *usb_hid_report_path(void);
|
|---|
| 273 |
|
|---|
| 274 | /** */
|
|---|
| 275 | void usb_hid_report_path_free(usb_hid_report_path_t *path);
|
|---|
| 276 |
|
|---|
| 277 | /** */
|
|---|
| 278 | int usb_hid_report_path_set_report_id(usb_hid_report_path_t *usage_path, uint8_t report_id);
|
|---|
| 279 |
|
|---|
| 280 | /** */
|
|---|
| 281 | int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, int32_t usage_page, int32_t usage);
|
|---|
| 282 |
|
|---|
| 283 | /** */
|
|---|
| 284 | void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path);
|
|---|
| 285 |
|
|---|
| 286 | /** */
|
|---|
| 287 | void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path);
|
|---|
| 288 |
|
|---|
| 289 | /** */
|
|---|
| 290 | void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data);
|
|---|
| 291 |
|
|---|
| 292 | /** */
|
|---|
| 293 | int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, usb_hid_report_path_t *path, int flags);
|
|---|
| 294 |
|
|---|
| 295 | /** */
|
|---|
| 296 | usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path);
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 | /*
|
|---|
| 300 | * Output report parser functions
|
|---|
| 301 | */
|
|---|
| 302 | /** Allocates output report buffer*/
|
|---|
| 303 | uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size);
|
|---|
| 304 |
|
|---|
| 305 | /** Frees output report buffer*/
|
|---|
| 306 | void usb_hid_report_output_free(uint8_t *output);
|
|---|
| 307 |
|
|---|
| 308 | /** Returns size of output for given usage path */
|
|---|
| 309 | size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
|
|---|
| 310 | usb_hid_report_path_t *path, int flags);
|
|---|
| 311 |
|
|---|
| 312 | /** Updates the output report buffer by translated given data */
|
|---|
| 313 | int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
|
|---|
| 314 | usb_hid_report_path_t *path, int flags,
|
|---|
| 315 | uint8_t *buffer, size_t size,
|
|---|
| 316 | int32_t *data, size_t data_size);
|
|---|
| 317 | #endif
|
|---|
| 318 | /**
|
|---|
| 319 | * @}
|
|---|
| 320 | */
|
|---|