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 BLUEKITCHEN 24 * GMBH 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 * @title HID Parser 40 * 41 * Single-pass HID Report Parser: HID Report is directly parsed without preprocessing HID Descriptor to minimize memory. 42 * 43 */ 44 45 #ifndef BTSTACK_HID_PARSER_H 46 #define BTSTACK_HID_PARSER_H 47 48 #include <stdint.h> 49 #include "btstack_hid.h" 50 51 #if defined __cplusplus 52 extern "C" { 53 #endif 54 55 typedef enum { 56 Main=0, 57 Global, 58 Local, 59 Reserved 60 } TagType; 61 62 typedef enum { 63 Input=8, 64 Output, 65 Coll, 66 Feature, 67 EndColl 68 } MainItemTag; 69 70 typedef enum { 71 UsagePage, 72 LogicalMinimum, 73 LogicalMaximum, 74 PhysicalMinimum, 75 PhysicalMaximum, 76 UnitExponent, 77 Unit, 78 ReportSize, 79 ReportID, 80 ReportCount, 81 Push, 82 Pop 83 } GlobalItemTag; 84 85 typedef enum { 86 Usage, 87 UsageMinimum, 88 UsageMaximum, 89 DesignatorIndex, 90 DesignatorMinimum, 91 DesignatorMaximum, 92 StringIndex, 93 StringMinimum, 94 StringMaximum, 95 Delimiter 96 } LocalItemTag; 97 98 typedef struct { 99 int32_t item_value; 100 uint16_t item_size; 101 uint8_t item_type; 102 uint8_t item_tag; 103 uint8_t data_size; 104 } hid_descriptor_item_t; 105 106 typedef enum { 107 BTSTACK_HID_PARSER_SCAN_FOR_REPORT_ITEM, 108 BTSTACK_HID_PARSER_USAGES_AVAILABLE, 109 BTSTACK_HID_PARSER_COMPLETE, 110 } btstack_hid_parser_state_t; 111 112 113 typedef struct { 114 115 // Descriptor 116 const uint8_t * descriptor; 117 uint16_t descriptor_len; 118 119 // Report 120 hid_report_type_t report_type; 121 const uint8_t * report; 122 uint16_t report_len; 123 124 // State 125 btstack_hid_parser_state_t state; 126 127 hid_descriptor_item_t descriptor_item; 128 129 uint16_t descriptor_pos; 130 uint16_t report_pos_in_bit; 131 132 // usage pos and usage_page after last main item, used to find next usage 133 uint16_t usage_pos; 134 uint16_t usage_page; 135 136 // usage generator 137 uint32_t usage_minimum; 138 uint32_t usage_maximum; 139 uint16_t available_usages; 140 uint8_t required_usages; 141 uint8_t active_record; 142 uint8_t have_usage_min; 143 uint8_t have_usage_max; 144 145 // global 146 int32_t global_logical_minimum; 147 int32_t global_logical_maximum; 148 uint16_t global_usage_page; 149 uint8_t global_report_size; 150 uint8_t global_report_count; 151 uint8_t global_report_id; 152 } btstack_hid_parser_t; 153 154 /* API_START */ 155 156 /** 157 * @brief Initialize HID Parser. 158 * @param parser state 159 * @param hid_descriptor 160 * @param hid_descriptor_len 161 * @param hid_report_type 162 * @param hid_report 163 * @param hid_report_len 164 */ 165 void btstack_hid_parser_init(btstack_hid_parser_t * parser, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len, hid_report_type_t hid_report_type, const uint8_t * hid_report, uint16_t hid_report_len); 166 167 /** 168 * @brief Checks if more fields are available 169 * @param parser 170 */ 171 int btstack_hid_parser_has_more(btstack_hid_parser_t * parser); 172 173 /** 174 * @brief Get next field 175 * @param parser 176 * @param usage_page 177 * @param usage 178 * @param value provided in HID report 179 */ 180 void btstack_hid_parser_get_field(btstack_hid_parser_t * parser, uint16_t * usage_page, uint16_t * usage, int32_t * value); 181 182 /** 183 * @brief Parses descriptor item 184 * @param item 185 * @param hid_descriptor 186 * @param hid_descriptor_len 187 */ 188 void btstack_hid_parse_descriptor_item(hid_descriptor_item_t * item, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len); 189 190 /** 191 * @brief Parses descriptor and returns report size for given report ID and report type 192 * @param report_id 193 * @param report_type 194 * @param hid_descriptor_len 195 * @param hid_descriptor 196 */ 197 int btstack_hid_get_report_size_for_id(int report_id, hid_report_type_t report_type, uint16_t hid_descriptor_len, const uint8_t * hid_descriptor); 198 199 /** 200 * @brief Parses descriptor and returns report size for given report ID and report type 201 * @param report_id 202 * @param hid_descriptor_len 203 * @param hid_descriptor 204 */ 205 hid_report_id_status_t btstack_hid_id_valid(int report_id, uint16_t hid_descriptor_len, const uint8_t * hid_descriptor); 206 207 /** 208 * @brief Parses descriptor and returns 1 if report ID found 209 * @param hid_descriptor_len 210 * @param hid_descriptor 211 */ 212 int btstack_hid_report_id_declared(uint16_t hid_descriptor_len, const uint8_t * hid_descriptor); 213 /* API_END */ 214 215 #if defined __cplusplus 216 } 217 #endif 218 219 #endif // BTSTACK_HID_PARSER_H 220