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