xref: /btstack/src/btstack_hid_parser.h (revision 308e35cf33f6937b53ef6c055adea9d10e215e1a)
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 
114 typedef struct {
115 
116     // Descriptor
117     const uint8_t * descriptor;
118     uint16_t        descriptor_len;
119 
120     // Report
121     hid_report_type_t report_type;
122     const uint8_t * report;
123     uint16_t        report_len;
124 
125     // State
126     btstack_hid_parser_state_t state;
127 
128     hid_descriptor_item_t descriptor_item;
129 
130     uint16_t        descriptor_pos;
131     uint16_t        report_pos_in_bit;
132 
133     // usage pos and usage_page after last main item, used to find next usage
134     uint16_t        usage_pos;
135     uint16_t        usage_page;
136 
137     // usage generator
138     uint32_t        usage_minimum;
139     uint32_t        usage_maximum;
140     uint16_t        available_usages;
141     uint16_t        required_usages;
142     bool            usage_range;
143     uint8_t         active_record;
144 
145     // global
146     int32_t         global_logical_minimum;
147     int32_t         global_logical_maximum;
148     uint16_t        global_usage_page;
149     uint8_t         global_report_size;
150     uint8_t         global_report_count;
151     uint8_t         global_report_id;
152 } btstack_hid_parser_t;
153 
154 /* API_START */
155 
156 /**
157  * @brief Initialize HID Parser.
158  * @param parser state
159  * @param hid_descriptor
160  * @param hid_descriptor_len
161  * @param hid_report_type
162  * @param hid_report
163  * @param hid_report_len
164  */
165 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);
166 
167 /**
168  * @brief Checks if more fields are available
169  * @param parser
170  */
171 bool btstack_hid_parser_has_more(btstack_hid_parser_t * parser);
172 
173 /**
174  * @brief Get next field
175  * @param parser
176  * @param usage_page
177  * @param usage
178  * @param value provided in HID report
179  */
180 void btstack_hid_parser_get_field(btstack_hid_parser_t * parser, uint16_t * usage_page, uint16_t * usage, int32_t * value);
181 
182 /**
183  * @brief Parses descriptor item
184  * @param item
185  * @param hid_descriptor
186  * @param hid_descriptor_len
187  * @return true if item has been parsed successfully
188  */
189 bool btstack_hid_parse_descriptor_item(hid_descriptor_item_t * item, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len);
190 
191 /**
192  * @brief Parses descriptor and returns report size for given report ID and report type
193  * @param report_id
194  * @param report_type
195  * @param hid_descriptor_len
196  * @param hid_descriptor
197  * @return report size in bytes or 0 on parsing error
198  */
199 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);
200 
201 /**
202  * @brief Parses descriptor and returns status for given report ID
203  * @param report_id
204  * @param hid_descriptor_len
205  * @param hid_descriptor
206  * @return status for report id
207  */
208 hid_report_id_status_t btstack_hid_id_valid(int report_id, uint16_t hid_descriptor_len, const uint8_t * hid_descriptor);
209 
210 /**
211  * @brief Parses descriptor and returns true if report ID found
212  * @param hid_descriptor_len
213  * @param hid_descriptor
214  * @return true if report ID declared in descriptor
215  */
216 bool btstack_hid_report_id_declared(uint16_t hid_descriptor_len, const uint8_t * hid_descriptor);
217 /* API_END */
218 
219 #if defined __cplusplus
220 }
221 #endif
222 
223 #endif // BTSTACK_HID_PARSER_H
224