-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhid_parser.c
303 lines (260 loc) · 9.78 KB
/
hid_parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "bsp/board.h"
#include "tusb.h"
#include "hid_parser.h"
//--------------------------------------------------------------------+
// Report Descriptor Parser
//--------------------------------------------------------------------+
uint8_t hid_parse_report_descriptor(hid_report_info_t* report_info_arr, uint8_t arr_count, uint8_t const* desc_report, uint16_t desc_len)
{
// Report Item 6.2.2.2 USB HID 1.11
union TU_ATTR_PACKED
{
uint8_t byte;
struct TU_ATTR_PACKED
{
uint8_t size : 2;
uint8_t type : 2;
uint8_t tag : 4;
};
} header;
tu_memclr(report_info_arr, arr_count*sizeof(tuh_hid_report_info_t));
uint8_t report_num = 0;
hid_report_info_t* info = report_info_arr;
// current parsed report count & size from descriptor
uint16_t ri_global_usage_page = 0;
int32_t ri_global_logical_min = 0;
int32_t ri_global_logical_max = 0;
int32_t ri_global_physical_min = 0;
int32_t ri_global_physical_max = 0;
uint8_t ri_report_count = 0;
uint8_t ri_report_size = 0;
uint8_t ri_report_usage_count = 0;
uint8_t ri_collection_depth = 0;
while(desc_len && report_num < arr_count)
{
header.byte = *desc_report++;
desc_len--;
uint8_t const tag = header.tag;
uint8_t const type = header.type;
uint8_t const size = header.size;
uint32_t data;
uint32_t sdata;
switch (size) {
case 1: data = desc_report[0]; sdata = ((data & 0x80) ? 0xFFFFFF00 : 0 ) | data; break;
case 2: data = (desc_report[1] << 8) | desc_report[0]; sdata = ((data & 0x8000) ? 0xFFFF0000 : 0 ) | data; break;
case 3: data = (desc_report[3] << 24) | (desc_report[2] << 16) | (desc_report[1] << 8) | desc_report[0]; sdata = data; break;
default: data = 0; sdata = 0;
}
TU_LOG2("tag = %d, type = %d, size = %d, data = ", tag, type, size);
for(uint32_t i = 0; i < size; i++) TU_LOG(3, "%02X ", desc_report[i]);
TU_LOG2("\r\n");
switch(type)
{
case RI_TYPE_MAIN:
switch (tag)
{
case RI_MAIN_INPUT:
case RI_MAIN_OUTPUT:
case RI_MAIN_FEATURE:
TU_LOG2("INPUT %d\n", data);
uint16_t offset = (info->num_items == 0) ? 0 : (info->item[info->num_items - 1].bit_offset + info->item[info->num_items - 1].bit_size);
for (int i = 0; i < ri_report_count; i++) {
if (info->num_items + i < MAX_REPORT_ITEMS) {
info->item[info->num_items + i].bit_offset = offset;
info->item[info->num_items + i].bit_size = ri_report_size;
info->item[info->num_items + i].item_type = tag;
info->item[info->num_items + i].attributes.logical.min = ri_global_logical_min;
info->item[info->num_items + i].attributes.logical.max = ri_global_logical_max;
info->item[info->num_items + i].attributes.physical.min = ri_global_physical_min;
info->item[info->num_items + i].attributes.physical.max = ri_global_physical_max;
info->item[info->num_items + i].attributes.usage.page = ri_global_usage_page;
if (ri_report_usage_count != ri_report_count && ri_report_usage_count > 0) {
if (i >= ri_report_usage_count) {
info->item[info->num_items + i].attributes.usage = info->item[info->num_items + i - 1].attributes.usage;
}
}
} else {
printf("%s: too much items!\n", __func__);
}
offset += ri_report_size;
}
info->num_items += ri_report_count;
ri_report_usage_count = 0;
break;
case RI_MAIN_COLLECTION:
ri_collection_depth++;
break;
case RI_MAIN_COLLECTION_END:
ri_collection_depth--;
if (ri_collection_depth == 0)
{
info++;
report_num++;
}
break;
default: break;
}
break;
case RI_TYPE_GLOBAL:
switch(tag)
{
case RI_GLOBAL_USAGE_PAGE:
// only take in account the "usage page" before REPORT ID
if ( ri_collection_depth == 0 ) {
info->usage_page = data;
}
ri_global_usage_page = data;
break;
case RI_GLOBAL_LOGICAL_MIN :
ri_global_logical_min = sdata;
break;
case RI_GLOBAL_LOGICAL_MAX :
ri_global_logical_max = sdata;
break;
case RI_GLOBAL_PHYSICAL_MIN :
ri_global_physical_min = sdata;
break;
case RI_GLOBAL_PHYSICAL_MAX :
ri_global_physical_max = sdata;
break;
case RI_GLOBAL_REPORT_ID:
info->report_id = data;
break;
case RI_GLOBAL_REPORT_SIZE:
ri_report_size = data;
break;
case RI_GLOBAL_REPORT_COUNT:
ri_report_count = data;
break;
case RI_GLOBAL_UNIT_EXPONENT : break;
case RI_GLOBAL_UNIT : break;
case RI_GLOBAL_PUSH : break;
case RI_GLOBAL_POP : break;
default: break;
}
break;
case RI_TYPE_LOCAL:
switch(tag)
{
case RI_LOCAL_USAGE:
// only take in account the "usage" before starting REPORT ID
if ( ri_collection_depth == 0 ) {
info->usage = data;
} else {
TU_LOG2("USAGE %02X\n", data);
if (ri_report_usage_count < MAX_REPORT_ITEMS) {
info->item[info->num_items + ri_report_usage_count].attributes.usage.usage = data;
ri_report_usage_count++;
} else {
printf("%s: too much report items!\n", __func__);
}
}
break;
case RI_LOCAL_USAGE_MIN : break;
case RI_LOCAL_USAGE_MAX : break;
case RI_LOCAL_DESIGNATOR_INDEX : break;
case RI_LOCAL_DESIGNATOR_MIN : break;
case RI_LOCAL_DESIGNATOR_MAX : break;
case RI_LOCAL_STRING_INDEX : break;
case RI_LOCAL_STRING_MIN : break;
case RI_LOCAL_STRING_MAX : break;
case RI_LOCAL_DELIMITER : break;
default: break;
}
break;
// error
default:
TU_LOG2("%s: Unknown type %02X\n", __func__, type);
break;
}
desc_report += size;
desc_len -= size;
}
for ( uint8_t i = 0; i < report_num; i++ )
{
info = report_info_arr+i;
TU_LOG2("%u: id = %u, usage_page = %u, usage = %u\r\n", i, info->report_id, info->usage_page, info->usage);
}
//////
for (int i = 0; i < info->num_items; i++) {
TU_LOG2("type %02X\n", info->item[i].item_type);
TU_LOG2(" offset %d\n", info->item[i].bit_offset);
TU_LOG2(" size %d\n", info->item[i].bit_size);
TU_LOG2(" page %04X\n", info->item[i].attributes.usage.page);
TU_LOG2(" usage %04X\n", info->item[i].attributes.usage.usage);
TU_LOG2(" logical min %d\n", info->item[i].attributes.logical.min);
TU_LOG2(" logical max %d\n", info->item[i].attributes.logical.max);
TU_LOG2(" physical min %d\n", info->item[i].attributes.physical.min);
TU_LOG2(" physical max %d\n", info->item[i].attributes.physical.max);
}
//////
return report_num;
}
bool hid_parse_find_item_by_page(hid_report_info_t* report_info_arr, uint8_t type, uint16_t page, const hid_report_item_t **item)
{
for (int i = 0; i < report_info_arr->num_items; i++) {
if (report_info_arr->item[i].item_type == type &&
report_info_arr->item[i].attributes.usage.page == page) {
if (item) {
*item = &report_info_arr->item[i];
}
return true;
}
}
return false;
}
bool hid_parse_find_item_by_usage(hid_report_info_t* report_info_arr, uint8_t type, uint16_t usage, const hid_report_item_t **item)
{
for (int i = 0; i < report_info_arr->num_items; i++) {
if (report_info_arr->item[i].item_type == type &&
report_info_arr->item[i].attributes.usage.usage == usage) {
if (item) {
*item = &report_info_arr->item[i];
}
return true;
}
}
return false;
}
bool hid_parse_find_bit_item_by_page(hid_report_info_t* report_info_arr, uint8_t type, uint16_t page, uint8_t bit, const hid_report_item_t **item)
{
for (int i = 0; i < report_info_arr->num_items; i++) {
if (report_info_arr->item[i].item_type == type &&
report_info_arr->item[i].attributes.usage.page == page) {
if (item) {
if (i + bit < report_info_arr->num_items &&
report_info_arr->item[i + bit].item_type == type &&
report_info_arr->item[i + bit].attributes.usage.page == page) {
*item = &report_info_arr->item[i + bit];
} else {
return false;
}
}
return true;
}
}
return false;
}
bool hid_parse_get_item_value(const hid_report_item_t *item, const uint8_t *report, uint8_t len, int32_t *value)
{
if (item == NULL || report == NULL) {
return false;
}
uint8_t boffs = item->bit_offset & 0x07;
uint8_t pos = 8 - boffs;
uint8_t offs = item->bit_offset >> 3;
uint32_t mask = ~(0xFFFFFFFF << item->bit_size);
int32_t val = report[offs++] >> boffs;
while (item->bit_size > pos) {
val |= (report[offs++] << pos);
pos += 8;
}
val &= mask;
if (item->attributes.logical.min < 0) {
if (val & (1 << (item->bit_size - 1))) {
val |= (0xFFFFFFFF << item->bit_size);
}
}
*value = val;
return true;
}