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