xref: /btstack/src/btstack_hid_parser.h (revision 2c4f9bbb6d93b3f1a90ed62ac67e4cd019f0736a)
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 MATTHIAS
24  * RINGWALD 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  *  btstack_hid_parser.h
40  *
41  *  Single-pass HID Report Parser: HID Report is directly parsed without preprocessing HID Descriptor to minimize memory
42  */
43 
44 #ifndef BTSTACK_HID_PARSER_H
45 #define BTSTACK_HID_PARSER_H
46 
47 #include <stdint.h>
48 #include "btstack_hid.h"
49 
50 #if defined __cplusplus
51 extern "C" {
52 #endif
53 
54 typedef enum {
55     Main=0,
56     Global,
57     Local,
58     Reserved
59 } TagType;
60 
61 typedef enum {
62     Input=8,
63     Output,
64     Coll,
65     Feature,
66     EndColl
67 } MainItemTag;
68 
69 typedef enum {
70     UsagePage,
71     LogicalMinimum,
72     LogicalMaximum,
73     PhysicalMinimum,
74     PhysicalMaximum,
75     UnitExponent,
76     Unit,
77     ReportSize,
78     ReportID,
79     ReportCount,
80     Push,
81     Pop
82 } GlobalItemTag;
83 
84 typedef enum {
85     Usage,
86     UsageMinimum,
87     UsageMaximum,
88     DesignatorIndex,
89     DesignatorMinimum,
90     DesignatorMaximum,
91     StringIndex,
92     StringMinimum,
93     StringMaximum,
94     Delimiter
95 } LocalItemTag;
96 
97 typedef struct  {
98     int32_t  item_value;
99     uint16_t item_size;
100     uint8_t  item_type;
101     uint8_t  item_tag;
102     uint8_t  data_size;
103 } hid_descriptor_item_t;
104 
105 typedef enum {
106     BTSTACK_HID_PARSER_SCAN_FOR_REPORT_ITEM,
107     BTSTACK_HID_PARSER_USAGES_AVAILABLE,
108     BTSTACK_HID_PARSER_COMPLETE,
109 } btstack_hid_parser_state_t;
110 
111 
112 typedef struct {
113 
114     // Descriptor
115     const uint8_t * descriptor;
116     uint16_t        descriptor_len;
117 
118     // Report
119     hid_report_type_t report_type;
120     const uint8_t * report;
121     uint16_t        report_len;
122 
123     // State
124     btstack_hid_parser_state_t state;
125 
126     hid_descriptor_item_t descriptor_item;
127 
128     uint16_t        descriptor_pos;
129     uint16_t        report_pos_in_bit;
130 
131     // usage pos and usage_page after last main item, used to find next usage
132     uint16_t        usage_pos;
133     uint16_t        usage_page;
134 
135     // usage generator
136     uint32_t        usage_minimum;
137     uint32_t        usage_maximum;
138     uint16_t        available_usages;
139     uint8_t         required_usages;
140     uint8_t         active_record;
141     uint8_t         have_usage_min;
142     uint8_t         have_usage_max;
143 
144     // global
145     int32_t         global_logical_minimum;
146     int32_t         global_logical_maximum;
147     uint16_t        global_usage_page;
148     uint8_t         global_report_size;
149     uint8_t         global_report_count;
150     uint8_t         global_report_id;
151 } btstack_hid_parser_t;
152 
153 /* API_START */
154 
155 /**
156  * @brief Initialize HID Parser.
157  * @param parser state
158  * @param hid_descriptor
159  * @param hid_descriptor_len
160  * @param hid_report_type
161  * @param hid_report
162  * @param hid_report_len
163  */
164 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);
165 
166 /**
167  * @brief Checks if more fields are available
168  * @param parser
169  */
170 int  btstack_hid_parser_has_more(btstack_hid_parser_t * parser);
171 
172 /**
173  * @brief Get next field
174  * @param parser
175  * @param usage_page
176  * @param usage
177  * @param value provided in HID report
178  */
179 void btstack_hid_parser_get_field(btstack_hid_parser_t * parser, uint16_t * usage_page, uint16_t * usage, int32_t * value);
180 
181 /**
182  * @brief Parses descriptor item
183  * @param item
184  * @param hid_descriptor
185  * @param hid_descriptor_len
186  */
187 void btstack_hid_parse_descriptor_item(hid_descriptor_item_t * item, const uint8_t * hid_descriptor, uint16_t hid_descriptor_len);
188 
189 /**
190  * @brief Parses descriptor and returns report size for given report ID and report type
191  * @param report_id
192  * @param report_type
193  * @param hid_descriptor_len
194  * @param hid_descriptor
195  */
196 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);
197 
198 /**
199  * @brief Parses descriptor and returns report size for given report ID and report type
200  * @param report_id
201  * @param hid_descriptor_len
202  * @param hid_descriptor
203  */
204 hid_report_id_status_t btstack_hid_id_valid(int report_id, uint16_t hid_descriptor_len, const uint8_t * hid_descriptor);
205 
206 /**
207  * @brief Parses descriptor and returns 1 if report ID found
208  * @param hid_descriptor_len
209  * @param hid_descriptor
210  */
211 int btstack_hid_report_id_declared(uint16_t hid_descriptor_len, const uint8_t * hid_descriptor);
212 /* API_END */
213 
214 #if defined __cplusplus
215 }
216 #endif
217 
218 #endif // BTSTACK_HID_PARSER_H
219