forked from zhuhuijia0001/ch559-usb-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseHidData.c
280 lines (229 loc) · 6.04 KB
/
ParseHidData.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
#include "Type.h"
#include "Protocol.h"
#include "UsbHost.h"
#include "ParseHidData.h"
#include "Trace.h"
static UINT8C lowest_bit_bitmap[] =
{
/* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
static BOOL NewUsbKeyboardParse(const UINT8 *pUsb, UINT8 len, KEYBOARD_PARSE_STRUCT *const pKeyboardParseStruct,
UINT8 *pOut, UINT8 maxOut)
{
UINT8 i;
UINT8 offset = 0;
if (maxOut < HID_KEYBOARD_VAL_LEN)
{
return FALSE;
}
if (len > MAX_HID_KEYBOARD_BIT_VAL_LEN)
{
len = MAX_HID_KEYBOARD_BIT_VAL_LEN;
}
for (i = 0; i < len; i++)
{
UINT8 d = pUsb[i] ^ pKeyboardParseStruct->KeyboardBitVal[i];
while (d)
{
UINT8 lowestBit = lowest_bit_bitmap[d];
UINT8 mask = 1u << lowestBit;
UINT8 key = offset + lowestBit;
UINT8 j;
if (pUsb[i] & mask)
{
// make key
for (j = 0; j < HID_KEYBOARD_VAL_LEN; j++)
{
if (pKeyboardParseStruct->KeyboardVal[j] == 0x00)
{
pKeyboardParseStruct->KeyboardVal[j] = key;
break;
}
}
if (j >= HID_KEYBOARD_VAL_LEN)
{
return FALSE;
}
pKeyboardParseStruct->KeyboardBitVal[i] |= mask;
}
else
{
//break key
for (j = 0; j < HID_KEYBOARD_VAL_LEN; j++)
{
if (pKeyboardParseStruct->KeyboardVal[j] == key)
{
UINT8 k;
for (k = j + 1; k < HID_KEYBOARD_VAL_LEN && pKeyboardParseStruct->KeyboardVal[k] != 0x00; k++)
{
pKeyboardParseStruct->KeyboardVal[k - 1] = pKeyboardParseStruct->KeyboardVal[k];
}
pKeyboardParseStruct->KeyboardVal[k - 1] = 0x00;
break;
}
}
if (j >= HID_KEYBOARD_VAL_LEN)
{
return FALSE;
}
pKeyboardParseStruct->KeyboardBitVal[i] &= ~mask;
}
d &= ~mask;
}
offset += 8;
}
for (i = 0; i < HID_KEYBOARD_VAL_LEN; i++)
{
pOut[i] = pKeyboardParseStruct->KeyboardVal[i];
}
return TRUE;
}
BOOL UsbKeyboardParse(const UINT8 *pUsb, UINT8 *pOut, const HID_SEG_STRUCT *pKeyboardSegStruct, KEYBOARD_PARSE_STRUCT *const pKeyboardParseStruct)
{
UINT8 i;
BOOL ret;
if (pKeyboardSegStruct->HIDSeg[HID_SEG_KEYBOARD_MODIFIER_INDEX].start == 0xff)
{
return FALSE;
}
if (pKeyboardSegStruct->KeyboardReportId != 0 && pKeyboardSegStruct->KeyboardReportId != pUsb[0])
{
return FALSE;
}
ret = TRUE;
if (pOut != NULL)
{
UINT8 index = pKeyboardSegStruct->HIDSeg[HID_SEG_KEYBOARD_MODIFIER_INDEX].start / 8;
pOut[0] = pUsb[index];
pOut[1] = 0x00;
index = pKeyboardSegStruct->HIDSeg[HID_SEG_KEYBOARD_VAL_INDEX].start / 8;
if (pKeyboardSegStruct->HIDSeg[HID_SEG_KEYBOARD_VAL_INDEX].size > 1)
{
for (i = 0; i < 6; i++)
{
pOut[2 + i] = pUsb[index + i];
}
}
else
{
//bit
if (!NewUsbKeyboardParse(&pUsb[index], pKeyboardSegStruct->HIDSeg[HID_SEG_KEYBOARD_VAL_INDEX].count / 8,
pKeyboardParseStruct, &pOut[2], 6))
{
ret = FALSE;
}
}
}
return ret;
}
static UINT8 GetMouseFieldData(const UINT8 *pData, UINT8 start, UINT8 size)
{
UINT8 index = start / 8;
UINT8 mouseData = pData[index];
UINT8 i;
start -= index * 8;
mouseData >>= start;
if (size > 8 - start)
{
size = 8 - start;
}
for (i = size; i < 8; i++)
{
mouseData &= ~(0x01 << i);
}
return mouseData;
}
//--------------------------------------------------------------------------------------------------
// 函数名称: UsbMouseParse()
// 函数功能: 鼠标数据解析
//--------------------------------------------------------------------------------------------------
BOOL UsbMouseParse(const UINT8 *pUsb, UINT8 *pOut, const HID_SEG_STRUCT *pMouseSegStruct)
{
UINT8 index;
UINT16 datx, daty;
UINT8 size;
if (pMouseSegStruct->MouseReportId != 0 && pMouseSegStruct->MouseReportId != pUsb[0])
{
return FALSE;
}
//解析button位置
index = pMouseSegStruct->HIDSeg[HID_SEG_BUTTON_INDEX].start;
if (index == 0xff)
{
pOut[0] = 0;
}
else
{
index >>= 3;
pOut[0] = pUsb[index] & 0x07;
}
//解析x,y位置
index = pMouseSegStruct->HIDSeg[HID_SEG_X_INDEX].start;
if (index == 0xff)
{
pOut[1] = 0;
pOut[2] = 0;
}
else
{
index >>= 3;
size = pMouseSegStruct->HIDSeg[HID_SEG_X_INDEX].size;
if (size == 16)
{
datx = pUsb[index] | (pUsb[index + 1] << 8);
daty = pUsb[index + 2] | (pUsb[index + 3] << 8);
pOut[1] = (UINT8)datx;
pOut[2] = (UINT8)daty;
}
else if (size == 12)
{
datx = pUsb[index] | ((pUsb[index + 1] & 0x0f) << 8);
daty = ((pUsb[index + 1] >> 4) & 0x0f) | (pUsb[index + 2] << 4);
pOut[1] = (UINT8)datx;
pOut[2] = (UINT8)daty;
}
else
{
//size == 8
pOut[1] = pUsb[index];
pOut[2] = pUsb[index + 1];
}
}
//解析wheel位置
index = pMouseSegStruct->HIDSeg[HID_SEG_WHEEL_INDEX].start;
if (index == 0xff)
{
pOut[3] = 0x00;
}
else
{
if (pMouseSegStruct->HIDSeg[HID_SEG_WHEEL_INDEX].size > 0)
{
pOut[3] = GetMouseFieldData(pUsb, pMouseSegStruct->HIDSeg[HID_SEG_WHEEL_INDEX].start, pMouseSegStruct->HIDSeg[HID_SEG_WHEEL_INDEX].size);
if (pOut[3] & (0x01 << (pMouseSegStruct->HIDSeg[HID_SEG_WHEEL_INDEX].size - 1)))
{
pOut[3] = 0xff;
}
}
else
{
pOut[3] = 0x00;
}
}
return TRUE;
}