| 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 | #define USB_HID_ITEM_FLAG_CONSTANT(flags) (flags & 0x1)
|
|---|
| 55 | #define USB_HID_ITEM_FLAG_VARIABLE(flags) (flags & 0x2)
|
|---|
| 56 | #define USB_HID_ITEM_FLAG_RELATIVE(flags) (flags & 0x4)
|
|---|
| 57 | #define USB_HID_ITEM_FLAG_WRAP(flags) (flags & 0x8)
|
|---|
| 58 | #define USB_HID_ITEM_FLAG_LINEAR(flags) (flags & 0x10)
|
|---|
| 59 | #define USB_HID_ITEM_FLAG_PREFERRED(flags) (flags & 0x20)
|
|---|
| 60 | #define USB_HID_ITEM_FLAG_POSITION(flags) (flags & 0x40)
|
|---|
| 61 | #define USB_HID_ITEM_FLAG_VOLATILE(flags) (flags & 0x80)
|
|---|
| 62 | #define USB_HID_ITEM_FLAG_BUFFERED(flags) (flags & 0x100)
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Description of report items
|
|---|
| 67 | */
|
|---|
| 68 | typedef struct {
|
|---|
| 69 | int32_t id;
|
|---|
| 70 | int32_t usage_page;
|
|---|
| 71 | int32_t usage;
|
|---|
| 72 | int32_t usage_minimum;
|
|---|
| 73 | int32_t usage_maximum;
|
|---|
| 74 | int32_t logical_minimum;
|
|---|
| 75 | int32_t logical_maximum;
|
|---|
| 76 | int32_t size;
|
|---|
| 77 | int32_t count;
|
|---|
| 78 | size_t offset;
|
|---|
| 79 | int32_t delimiter;
|
|---|
| 80 |
|
|---|
| 81 | int32_t unit_exponent;
|
|---|
| 82 | int32_t unit;
|
|---|
| 83 |
|
|---|
| 84 | /*
|
|---|
| 85 | * some not yet used fields
|
|---|
| 86 | */
|
|---|
| 87 | int32_t string_index;
|
|---|
| 88 | int32_t string_minimum;
|
|---|
| 89 | int32_t string_maximum;
|
|---|
| 90 | int32_t designator_index;
|
|---|
| 91 | int32_t designator_minimum;
|
|---|
| 92 | int32_t designator_maximum;
|
|---|
| 93 | int32_t physical_minimum;
|
|---|
| 94 | int32_t physical_maximum;
|
|---|
| 95 |
|
|---|
| 96 | uint8_t item_flags;
|
|---|
| 97 |
|
|---|
| 98 | link_t link;
|
|---|
| 99 | } usb_hid_report_item_t;
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | /** HID report parser structure. */
|
|---|
| 103 | typedef struct {
|
|---|
| 104 | link_t input;
|
|---|
| 105 | link_t output;
|
|---|
| 106 | link_t feature;
|
|---|
| 107 | } usb_hid_report_parser_t;
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | /** HID parser callbacks for IN items. */
|
|---|
| 112 | typedef struct {
|
|---|
| 113 | /** Callback for keyboard.
|
|---|
| 114 | *
|
|---|
| 115 | * @param key_codes Array of pressed key (including modifiers).
|
|---|
| 116 | * @param count Length of @p key_codes.
|
|---|
| 117 | * @param arg Custom argument.
|
|---|
| 118 | */
|
|---|
| 119 | void (*keyboard)(const uint8_t *key_codes, size_t count, const uint8_t modifiers, void *arg);
|
|---|
| 120 | } usb_hid_report_in_callbacks_t;
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | typedef enum {
|
|---|
| 124 | USB_HID_MOD_LCTRL = 0x01,
|
|---|
| 125 | USB_HID_MOD_LSHIFT = 0x02,
|
|---|
| 126 | USB_HID_MOD_LALT = 0x04,
|
|---|
| 127 | USB_HID_MOD_LGUI = 0x08,
|
|---|
| 128 | USB_HID_MOD_RCTRL = 0x10,
|
|---|
| 129 | USB_HID_MOD_RSHIFT = 0x20,
|
|---|
| 130 | USB_HID_MOD_RALT = 0x40,
|
|---|
| 131 | USB_HID_MOD_RGUI = 0x80,
|
|---|
| 132 | USB_HID_MOD_COUNT = 8
|
|---|
| 133 | } usb_hid_modifiers_t;
|
|---|
| 134 |
|
|---|
| 135 | typedef enum {
|
|---|
| 136 | USB_HID_LED_NUM_LOCK = 0x1,
|
|---|
| 137 | USB_HID_LED_CAPS_LOCK = 0x2,
|
|---|
| 138 | USB_HID_LED_SCROLL_LOCK = 0x4,
|
|---|
| 139 | USB_HID_LED_COMPOSE = 0x8,
|
|---|
| 140 | USB_HID_LED_KANA = 0x10,
|
|---|
| 141 | USB_HID_LED_COUNT = 5
|
|---|
| 142 | } usb_hid_led_t;
|
|---|
| 143 |
|
|---|
| 144 | static const usb_hid_modifiers_t
|
|---|
| 145 | usb_hid_modifiers_consts[USB_HID_MOD_COUNT] = {
|
|---|
| 146 | USB_HID_MOD_LCTRL,
|
|---|
| 147 | USB_HID_MOD_LSHIFT,
|
|---|
| 148 | USB_HID_MOD_LALT,
|
|---|
| 149 | USB_HID_MOD_LGUI,
|
|---|
| 150 | USB_HID_MOD_RCTRL,
|
|---|
| 151 | USB_HID_MOD_RSHIFT,
|
|---|
| 152 | USB_HID_MOD_RALT,
|
|---|
| 153 | USB_HID_MOD_RGUI
|
|---|
| 154 | };
|
|---|
| 155 |
|
|---|
| 156 | //static const usb_hid_led_t usb_hid_led_consts[USB_HID_LED_COUNT] = {
|
|---|
| 157 | // USB_HID_LED_NUM_LOCK,
|
|---|
| 158 | // USB_HID_LED_CAPS_LOCK,
|
|---|
| 159 | // USB_HID_LED_SCROLL_LOCK,
|
|---|
| 160 | // USB_HID_LED_COMPOSE,
|
|---|
| 161 | // USB_HID_LED_KANA
|
|---|
| 162 | //};
|
|---|
| 163 |
|
|---|
| 164 | //#define USB_HID_BOOT_KEYBOARD_NUM_LOCK 0x01
|
|---|
| 165 | //#define USB_HID_BOOT_KEYBOARD_CAPS_LOCK 0x02
|
|---|
| 166 | //#define USB_HID_BOOT_KEYBOARD_SCROLL_LOCK 0x04
|
|---|
| 167 | //#define USB_HID_BOOT_KEYBOARD_COMPOSE 0x08
|
|---|
| 168 | //#define USB_HID_BOOT_KEYBOARD_KANA 0x10
|
|---|
| 169 |
|
|---|
| 170 | /*
|
|---|
| 171 | * modifiers definitions
|
|---|
| 172 | */
|
|---|
| 173 |
|
|---|
| 174 | int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
|
|---|
| 175 | const usb_hid_report_in_callbacks_t *callbacks, void *arg);
|
|---|
| 176 |
|
|---|
| 177 | int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size);
|
|---|
| 178 |
|
|---|
| 179 | int usb_hid_parser_init(usb_hid_report_parser_t *parser);
|
|---|
| 180 | int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
|
|---|
| 181 | const uint8_t *data, size_t size);
|
|---|
| 182 |
|
|---|
| 183 | int usb_hid_parse_report(const usb_hid_report_parser_t *parser,
|
|---|
| 184 | const uint8_t *data, size_t size,
|
|---|
| 185 | const usb_hid_report_in_callbacks_t *callbacks, void *arg);
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 | void usb_hid_free_report_parser(usb_hid_report_parser_t *parser);
|
|---|
| 189 |
|
|---|
| 190 | void usb_hid_descriptor_print(usb_hid_report_parser_t *parser);
|
|---|
| 191 |
|
|---|
| 192 | #endif
|
|---|
| 193 | /**
|
|---|
| 194 | * @}
|
|---|
| 195 | */
|
|---|