| 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 | * @brief USB HID 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 | typedef struct {
|
|---|
| 77 | int32_t usage_page;
|
|---|
| 78 | int32_t usage;
|
|---|
| 79 |
|
|---|
| 80 | link_t link;
|
|---|
| 81 | } usb_hid_report_usage_path_t;
|
|---|
| 82 |
|
|---|
| 83 | typedef struct {
|
|---|
| 84 | int depth;
|
|---|
| 85 | link_t link;
|
|---|
| 86 | } usb_hid_report_path_t;
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Description of report items
|
|---|
| 90 | */
|
|---|
| 91 | typedef struct {
|
|---|
| 92 | int32_t id;
|
|---|
| 93 | int32_t usage_minimum;
|
|---|
| 94 | int32_t usage_maximum;
|
|---|
| 95 | int32_t logical_minimum;
|
|---|
| 96 | int32_t logical_maximum;
|
|---|
| 97 | int32_t size;
|
|---|
| 98 | int32_t count;
|
|---|
| 99 | size_t offset;
|
|---|
| 100 | int32_t delimiter;
|
|---|
| 101 |
|
|---|
| 102 | int32_t unit_exponent;
|
|---|
| 103 | int32_t unit;
|
|---|
| 104 |
|
|---|
| 105 | /*
|
|---|
| 106 | * some not yet used fields
|
|---|
| 107 | */
|
|---|
| 108 | int32_t string_index;
|
|---|
| 109 | int32_t string_minimum;
|
|---|
| 110 | int32_t string_maximum;
|
|---|
| 111 | int32_t designator_index;
|
|---|
| 112 | int32_t designator_minimum;
|
|---|
| 113 | int32_t designator_maximum;
|
|---|
| 114 | int32_t physical_minimum;
|
|---|
| 115 | int32_t physical_maximum;
|
|---|
| 116 |
|
|---|
| 117 | uint8_t item_flags;
|
|---|
| 118 |
|
|---|
| 119 | usb_hid_report_path_t *usage_path;
|
|---|
| 120 | link_t link;
|
|---|
| 121 | } usb_hid_report_item_t;
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | /** HID report parser structure. */
|
|---|
| 125 | typedef struct {
|
|---|
| 126 | link_t input;
|
|---|
| 127 | link_t output;
|
|---|
| 128 | link_t feature;
|
|---|
| 129 | } usb_hid_report_parser_t;
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 | /** HID parser callbacks for IN items. */
|
|---|
| 133 | typedef struct {
|
|---|
| 134 | /** Callback for keyboard.
|
|---|
| 135 | *
|
|---|
| 136 | * @param key_codes Array of pressed key (including modifiers).
|
|---|
| 137 | * @param count Length of @p key_codes.
|
|---|
| 138 | * @param arg Custom argument.
|
|---|
| 139 | */
|
|---|
| 140 | void (*keyboard)(const uint8_t *key_codes, size_t count, const uint8_t modifiers, void *arg);
|
|---|
| 141 | } usb_hid_report_in_callbacks_t;
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | typedef enum {
|
|---|
| 145 | USB_HID_MOD_LCTRL = 0x01,
|
|---|
| 146 | USB_HID_MOD_LSHIFT = 0x02,
|
|---|
| 147 | USB_HID_MOD_LALT = 0x04,
|
|---|
| 148 | USB_HID_MOD_LGUI = 0x08,
|
|---|
| 149 | USB_HID_MOD_RCTRL = 0x10,
|
|---|
| 150 | USB_HID_MOD_RSHIFT = 0x20,
|
|---|
| 151 | USB_HID_MOD_RALT = 0x40,
|
|---|
| 152 | USB_HID_MOD_RGUI = 0x80,
|
|---|
| 153 | USB_HID_MOD_COUNT = 8
|
|---|
| 154 | } usb_hid_modifiers_t;
|
|---|
| 155 |
|
|---|
| 156 | typedef enum {
|
|---|
| 157 | USB_HID_LED_NUM_LOCK = 0x1,
|
|---|
| 158 | USB_HID_LED_CAPS_LOCK = 0x2,
|
|---|
| 159 | USB_HID_LED_SCROLL_LOCK = 0x4,
|
|---|
| 160 | USB_HID_LED_COMPOSE = 0x8,
|
|---|
| 161 | USB_HID_LED_KANA = 0x10,
|
|---|
| 162 | USB_HID_LED_COUNT = 5
|
|---|
| 163 | } usb_hid_led_t;
|
|---|
| 164 |
|
|---|
| 165 | static const usb_hid_modifiers_t
|
|---|
| 166 | usb_hid_modifiers_consts[USB_HID_MOD_COUNT] = {
|
|---|
| 167 | USB_HID_MOD_LCTRL,
|
|---|
| 168 | USB_HID_MOD_LSHIFT,
|
|---|
| 169 | USB_HID_MOD_LALT,
|
|---|
| 170 | USB_HID_MOD_LGUI,
|
|---|
| 171 | USB_HID_MOD_RCTRL,
|
|---|
| 172 | USB_HID_MOD_RSHIFT,
|
|---|
| 173 | USB_HID_MOD_RALT,
|
|---|
| 174 | USB_HID_MOD_RGUI
|
|---|
| 175 | };
|
|---|
| 176 |
|
|---|
| 177 | //static const usb_hid_led_t usb_hid_led_consts[USB_HID_LED_COUNT] = {
|
|---|
| 178 | // USB_HID_LED_NUM_LOCK,
|
|---|
| 179 | // USB_HID_LED_CAPS_LOCK,
|
|---|
| 180 | // USB_HID_LED_SCROLL_LOCK,
|
|---|
| 181 | // USB_HID_LED_COMPOSE,
|
|---|
| 182 | // USB_HID_LED_KANA
|
|---|
| 183 | //};
|
|---|
| 184 |
|
|---|
| 185 | //#define USB_HID_BOOT_KEYBOARD_NUM_LOCK 0x01
|
|---|
| 186 | //#define USB_HID_BOOT_KEYBOARD_CAPS_LOCK 0x02
|
|---|
| 187 | //#define USB_HID_BOOT_KEYBOARD_SCROLL_LOCK 0x04
|
|---|
| 188 | //#define USB_HID_BOOT_KEYBOARD_COMPOSE 0x08
|
|---|
| 189 | //#define USB_HID_BOOT_KEYBOARD_KANA 0x10
|
|---|
| 190 |
|
|---|
| 191 | /*
|
|---|
| 192 | * modifiers definitions
|
|---|
| 193 | */
|
|---|
| 194 |
|
|---|
| 195 | int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
|
|---|
| 196 | const usb_hid_report_in_callbacks_t *callbacks, void *arg);
|
|---|
| 197 |
|
|---|
| 198 | int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size);
|
|---|
| 199 |
|
|---|
| 200 | int usb_hid_parser_init(usb_hid_report_parser_t *parser);
|
|---|
| 201 | int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
|
|---|
| 202 | const uint8_t *data, size_t size);
|
|---|
| 203 |
|
|---|
| 204 | int usb_hid_parse_report(const usb_hid_report_parser_t *parser,
|
|---|
| 205 | const uint8_t *data, size_t size,
|
|---|
| 206 | usb_hid_report_path_t *path, int flags,
|
|---|
| 207 | const usb_hid_report_in_callbacks_t *callbacks, void *arg);
|
|---|
| 208 |
|
|---|
| 209 | size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser,
|
|---|
| 210 | usb_hid_report_path_t *path, int flags);
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 | void usb_hid_free_report_parser(usb_hid_report_parser_t *parser);
|
|---|
| 214 |
|
|---|
| 215 | void usb_hid_descriptor_print(usb_hid_report_parser_t *parser);
|
|---|
| 216 |
|
|---|
| 217 | /* usage path functions */
|
|---|
| 218 | usb_hid_report_path_t *usb_hid_report_path(void);
|
|---|
| 219 | void usb_hid_report_path_free(usb_hid_report_path_t *path);
|
|---|
| 220 | int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, int32_t usage_page, int32_t usage);
|
|---|
| 221 | void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path);
|
|---|
| 222 | void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path);
|
|---|
| 223 | void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data);
|
|---|
| 224 | int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, usb_hid_report_path_t *path, int flags);
|
|---|
| 225 | usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path);
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 | // output API
|
|---|
| 229 | /** Allocates output report buffer*/
|
|---|
| 230 | uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser);
|
|---|
| 231 | /** Frees output report buffer*/
|
|---|
| 232 | void usb_hid_report_output_free(uint8_t *output);
|
|---|
| 233 |
|
|---|
| 234 | /** Returns size of output for given usage path */
|
|---|
| 235 | size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,
|
|---|
| 236 | usb_hid_report_path_t *path, int flags);
|
|---|
| 237 | /** Updates the output report buffer by translated given data */
|
|---|
| 238 | int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,
|
|---|
| 239 | usb_hid_report_path_t *path, int flags,
|
|---|
| 240 | uint8_t *buffer, size_t size,
|
|---|
| 241 | int32_t *data, size_t data_size);
|
|---|
| 242 | #endif
|
|---|
| 243 | /**
|
|---|
| 244 | * @}
|
|---|
| 245 | */
|
|---|