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