1 /* 2 * Copyright (C) 2017 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /* 39 * btstack_hid_parser.h 40 * 41 * Single-pass HID Report Parser: HID Report is directly parsed without preprocessing HID Descriptor to minimize memory 42 */ 43 44 #ifndef __BTSTACK_HID_PARSER_H 45 #define __BTSTACK_HID_PARSER_H 46 47 #include <stdint.h> 48 49 #if defined __cplusplus 50 extern "C" { 51 #endif 52 53 typedef struct { 54 int32_t item_value; 55 uint16_t item_size; 56 uint8_t item_type; 57 uint8_t item_tag; 58 uint8_t data_size; 59 } hid_descriptor_item_t; 60 61 typedef enum { 62 BTSTACK_HID_REPORT_TYPE_OTHER = 0x0, 63 BTSTACK_HID_REPORT_TYPE_INPUT, 64 BTSTACK_HID_REPORT_TYPE_OUTPUT, 65 BTSTACK_HID_REPORT_TYPE_FEATURE, 66 } btstack_hid_report_type_t; 67 68 typedef enum { 69 BTSTACK_HID_PARSER_SCAN_FOR_REPORT_ITEM, 70 BTSTACK_HID_PARSER_USAGES_AVAILABLE, 71 BTSTACK_HID_PARSER_COMPLETE, 72 } btstack_hid_parser_state_t; 73 74 typedef struct { 75 76 // Descriptor 77 const uint8_t * descriptor; 78 uint16_t descriptor_len; 79 80 // Report 81 btstack_hid_report_type_t report_type; 82 const uint8_t * report; 83 uint16_t report_len; 84 85 // State 86 btstack_hid_parser_state_t state; 87 88 hid_descriptor_item_t descriptor_item; 89 90 uint16_t descriptor_pos; 91 uint16_t report_pos_in_bit; 92 93 // usage pos and usage_page after last main item, used to find next usage 94 uint16_t usage_pos; 95 uint16_t usage_page; 96 97 // usage generator 98 uint32_t usage_minimum; 99 uint32_t usage_maximum; 100 uint16_t available_usages; 101 uint8_t required_usages; 102 uint8_t active_record; 103 uint8_t have_usage_min; 104 uint8_t have_usage_max; 105 106 // global 107 int32_t global_logical_minimum; 108 int32_t global_logical_maximum; 109 uint16_t global_usage_page; 110 uint8_t global_report_size; 111 uint8_t global_report_count; 112 uint8_t global_report_id; 113 } btstack_hid_parser_t; 114 115 /* API_START */ 116 117 /** 118 * @brief Initialize HID Parser. 119 * @param parser state 120 * @param hid_descriptor 121 * @param hid_descriptor_len 122 * @param hid_report_type 123 * @param hid_report 124 * @param hid_report_len 125 */ 126 void btstack_hid_parser_init(btstack_hid_parser_t * parser, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len, btstack_hid_report_type_t hid_report_type, const uint8_t * hid_report, uint16_t hid_report_len); 127 128 /** 129 * @brief Checks if more fields are available 130 * @param parser 131 */ 132 int btstack_hid_parser_has_more(btstack_hid_parser_t * parser); 133 134 /** 135 * @brief Get next field 136 * @param parser 137 * @param usage_page 138 * @param usage 139 * @param value provided in HID report 140 */ 141 void btstack_hid_parser_get_field(btstack_hid_parser_t * parser, uint16_t * usage_page, uint16_t * usage, int32_t * value); 142 143 /* API_END */ 144 145 #if defined __cplusplus 146 } 147 #endif 148 149 #endif // __BTSTACK_HID_PARSER_H 150