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 uint16_t report_id; // 8-bit report ID or 0xffff if not set 126 uint16_t bit_pos; // position in bit 127 uint16_t usage_page; 128 uint16_t usage; 129 uint8_t size; // in bit 130 // cached values of relevant descriptor item (input,output,feature) 131 hid_descriptor_item_t descriptor_item; 132 int32_t global_logical_minimum; 133 } btstack_hid_usage_item_t; 134 135 typedef struct { 136 137 // Descriptor 138 const uint8_t * descriptor; 139 uint16_t descriptor_len; 140 btstack_hid_descriptor_iterator_t descriptor_iterator; 141 142 // Report 143 hid_report_type_t report_type; 144 const uint8_t * report; 145 uint16_t report_len; 146 147 // State 148 btstack_hid_parser_state_t state; 149 150 hid_descriptor_item_t descriptor_item; 151 152 uint16_t report_pos_in_bit; 153 154 // usage pos and usage_page after last main item, used to find next usage 155 uint16_t usage_pos; 156 uint16_t usage_page; 157 158 // usage generator 159 uint32_t usage_minimum; 160 uint32_t usage_maximum; 161 uint16_t available_usages; 162 uint16_t required_usages; 163 bool usage_range; 164 uint8_t active_record; 165 166 // global 167 int32_t global_logical_minimum; 168 int32_t global_logical_maximum; 169 uint16_t global_usage_page; 170 uint8_t global_report_size; 171 uint8_t global_report_count; 172 uint16_t global_report_id; 173 174 // for HID Parser 175 bool have_report_usage_ready; 176 btstack_hid_usage_item_t descriptor__usage_item; 177 } btstack_hid_parser_t; 178 179 180 /* API_START */ 181 182 // HID Descriptor Iterator 183 184 /** 185 * @brief Init HID Descriptor iterator 186 * @param iterator 187 * @param hid_descriptor 188 * @param hid_descriptor_len 189 */ 190 void btstack_hid_descriptor_iterator_init(btstack_hid_descriptor_iterator_t * iterator, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len); 191 192 /** 193 * @brief Check if HID Descriptor Items are available 194 * @param iterator 195 * @return 196 */ 197 bool btstack_hid_descriptor_iterator_has_more(btstack_hid_descriptor_iterator_t * iterator); 198 199 /** 200 * @brief Get next HID Descriptor Item 201 * @param iterator 202 * @return 203 */ 204 const hid_descriptor_item_t * const btstack_hid_descriptor_iterator_get_item(btstack_hid_descriptor_iterator_t * iterator); 205 206 /** 207 * @brief Returns if HID Descriptor was well formed 208 * @param iterator 209 * @return 210 */ 211 bool btstack_hid_descriptor_iterator_valid(btstack_hid_descriptor_iterator_t * iterator); 212 213 214 // HID Descriptor Usage Iterator 215 216 /** 217 * @brief Initialize usages iterator for HID Descriptor and report type 218 * @param parser 219 * @param hid_descriptor 220 * @param hid_descriptor_len 221 * @param hid_report_type 222 */ 223 void btstack_hid_usage_iterator_init(btstack_hid_parser_t * parser, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len, hid_report_type_t hid_report_type); 224 225 /** 226 * @brief Checks if more usages are available 227 * @param parser 228 */ 229 bool btstack_hid_usage_iterator_has_more(btstack_hid_parser_t * parser); 230 231 /** 232 * @brief Get current usage item 233 * @param parser 234 * @param item 235 */ 236 void btstack_hid_usage_iterator_get_item(btstack_hid_parser_t * parser, btstack_hid_usage_item_t * item); 237 238 239 // HID Report Iterator 240 241 /** 242 * @brief Initialize HID Parser. 243 * @param parser state 244 * @param hid_descriptor 245 * @param hid_descriptor_len 246 * @param hid_report_type 247 * @param hid_report 248 * @param hid_report_len 249 */ 250 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); 251 252 /** 253 * @brief Checks if more fields are available 254 * @param parser 255 */ 256 bool btstack_hid_parser_has_more(btstack_hid_parser_t * parser); 257 258 /** 259 * @brief Get next field 260 * @param parser 261 * @param usage_page 262 * @param usage 263 * @param value provided in HID report 264 */ 265 void btstack_hid_parser_get_field(btstack_hid_parser_t * parser, uint16_t * usage_page, uint16_t * usage, int32_t * value); 266 267 268 /** 269 * @brief Parses descriptor item 270 * @param item 271 * @param hid_descriptor 272 * @param hid_descriptor_len 273 * @return true if item has been parsed successfully 274 */ 275 bool btstack_hid_parse_descriptor_item(hid_descriptor_item_t * item, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len); 276 277 /** 278 * @brief Parses descriptor and returns report size for given report ID and report type 279 * @param report_id or HID_REPORT_ID_UNDEFINED 280 * @param report_type 281 * @param hid_descriptor 282 * @param hid_descriptor_len 283 * @return report size in bytes or 0 on parsing error 284 */ 285 int btstack_hid_get_report_size_for_id(uint16_t report_id, hid_report_type_t report_type, const uint8_t *hid_descriptor, 286 uint16_t hid_descriptor_len); 287 288 /** 289 * @brief Parses descriptor and returns status for given report ID 290 * @param report_id 291 * @param hid_descriptor 292 * @param hid_descriptor_len 293 * @return status for report id 294 */ 295 hid_report_id_status_t btstack_hid_report_id_valid(uint16_t report_id, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len); 296 297 /** 298 * @brief Parses descriptor and returns true if report ID found 299 * @param hid_descriptor 300 * @param hid_descriptor_len 301 * @return true if report ID declared in descriptor 302 */ 303 bool btstack_hid_report_id_declared(const uint8_t *hid_descriptor, uint16_t hid_descriptor_len); 304 305 306 /* API_END */ 307 308 #if defined __cplusplus 309 } 310 #endif 311 312 #endif // BTSTACK_HID_PARSER_H 313