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_bool.h" 50 #include "btstack_hid.h" 51 52 #if defined __cplusplus 53 extern "C" { 54 #endif 55 56 typedef enum { 57 Main=0, 58 Global, 59 Local, 60 Reserved 61 } TagType; 62 63 typedef enum { 64 Input=8, 65 Output, 66 Coll, 67 Feature, 68 EndColl 69 } MainItemTag; 70 71 typedef enum { 72 UsagePage, 73 LogicalMinimum, 74 LogicalMaximum, 75 PhysicalMinimum, 76 PhysicalMaximum, 77 UnitExponent, 78 Unit, 79 ReportSize, 80 ReportID, 81 ReportCount, 82 Push, 83 Pop 84 } GlobalItemTag; 85 86 typedef enum { 87 Usage, 88 UsageMinimum, 89 UsageMaximum, 90 DesignatorIndex, 91 DesignatorMinimum, 92 DesignatorMaximum, 93 StringIndex, 94 StringMinimum, 95 StringMaximum, 96 Delimiter 97 } LocalItemTag; 98 99 typedef struct { 100 int32_t item_value; 101 uint16_t item_size; 102 uint8_t item_type; 103 uint8_t item_tag; 104 uint8_t data_size; 105 } hid_descriptor_item_t; 106 107 typedef enum { 108 BTSTACK_HID_PARSER_SCAN_FOR_REPORT_ITEM, 109 BTSTACK_HID_PARSER_USAGES_AVAILABLE, 110 BTSTACK_HID_PARSER_COMPLETE, 111 } btstack_hid_parser_state_t; 112 113 typedef struct { 114 // Descriptor 115 const uint8_t * descriptor; 116 uint16_t descriptor_pos; 117 uint16_t descriptor_len; 118 // parsed item 119 bool item_ready; 120 bool valid; 121 hid_descriptor_item_t descriptor_item; 122 } btstack_hid_descriptor_iterator_t; 123 124 typedef struct { 125 126 // Descriptor 127 const uint8_t * descriptor; 128 uint16_t descriptor_len; 129 130 // Report 131 hid_report_type_t report_type; 132 const uint8_t * report; 133 uint16_t report_len; 134 135 // State 136 btstack_hid_parser_state_t state; 137 138 hid_descriptor_item_t descriptor_item; 139 140 uint16_t descriptor_pos; 141 uint16_t report_pos_in_bit; 142 143 // usage pos and usage_page after last main item, used to find next usage 144 uint16_t usage_pos; 145 uint16_t usage_page; 146 147 // usage generator 148 uint32_t usage_minimum; 149 uint32_t usage_maximum; 150 uint16_t available_usages; 151 uint16_t required_usages; 152 bool usage_range; 153 uint8_t active_record; 154 155 // global 156 int32_t global_logical_minimum; 157 int32_t global_logical_maximum; 158 uint16_t global_usage_page; 159 uint8_t global_report_size; 160 uint8_t global_report_count; 161 uint8_t global_report_id; 162 } btstack_hid_parser_t; 163 164 165 /* API_START */ 166 167 /** 168 * @brief Init HID Descriptor iterator 169 * @param iterator 170 * @param hid_descriptor 171 * @param hid_descriptor_len 172 */ 173 void btstack_hid_descriptor_iterator_init(btstack_hid_descriptor_iterator_t * iterator, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len); 174 175 /** 176 * @brief Check if HID Descriptor Items are available 177 * @param iterator 178 * @return 179 */ 180 bool btstack_hid_descriptor_iterator_has_more(btstack_hid_descriptor_iterator_t * iterator); 181 182 /** 183 * @brief Get next HID Descriptor Item 184 * @param iterator 185 * @return 186 */ 187 const hid_descriptor_item_t * const btstack_hid_descriptor_iterator_get_item(btstack_hid_descriptor_iterator_t * iterator); 188 189 /** 190 * @brief Returns if HID Descriptor was well formed 191 * @param iterator 192 * @return 193 */ 194 bool btstack_hid_descriptor_iterator_valid(btstack_hid_descriptor_iterator_t * iterator); 195 196 /** 197 * @brief Initialize HID Parser. 198 * @param parser state 199 * @param hid_descriptor 200 * @param hid_descriptor_len 201 * @param hid_report_type 202 * @param hid_report 203 * @param hid_report_len 204 */ 205 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); 206 207 /** 208 * @brief Checks if more fields are available 209 * @param parser 210 */ 211 bool btstack_hid_parser_has_more(btstack_hid_parser_t * parser); 212 213 /** 214 * @brief Get next field 215 * @param parser 216 * @param usage_page 217 * @param usage 218 * @param value provided in HID report 219 */ 220 void btstack_hid_parser_get_field(btstack_hid_parser_t * parser, uint16_t * usage_page, uint16_t * usage, int32_t * value); 221 222 223 /** 224 * @brief Parses descriptor item 225 * @param item 226 * @param hid_descriptor 227 * @param hid_descriptor_len 228 * @return true if item has been parsed successfully 229 */ 230 bool btstack_hid_parse_descriptor_item(hid_descriptor_item_t * item, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len); 231 232 /** 233 * @brief Parses descriptor and returns report size for given report ID and report type 234 * @param report_id 235 * @param report_type 236 * @param hid_descriptor_len 237 * @param hid_descriptor 238 * @return report size in bytes or 0 on parsing error 239 */ 240 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); 241 242 /** 243 * @brief Parses descriptor and returns status for given report ID 244 * @param report_id 245 * @param hid_descriptor_len 246 * @param hid_descriptor 247 * @return status for report id 248 */ 249 hid_report_id_status_t btstack_hid_id_valid(int report_id, uint16_t hid_descriptor_len, const uint8_t * hid_descriptor); 250 251 /** 252 * @brief Parses descriptor and returns true if report ID found 253 * @param hid_descriptor_len 254 * @param hid_descriptor 255 * @return true if report ID declared in descriptor 256 */ 257 bool btstack_hid_report_id_declared(uint16_t hid_descriptor_len, const uint8_t * hid_descriptor); 258 259 /* API_END */ 260 261 #if defined __cplusplus 262 } 263 #endif 264 265 #endif // BTSTACK_HID_PARSER_H 266