-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdescriptor_parser.cpp
182 lines (152 loc) · 5.55 KB
/
descriptor_parser.cpp
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
#include "descriptor_parser.h"
#include <string.h>
#include <stddef.h>
#include "debug_print.h"
#include <Arduino.h>
enum ConfigParserState {
ConfigParserStateFindingInterfaceClass,
ConfigParserStateFindingInterfaceSubClass,
ConfigParserStateFindingInterfaceProtocol,
ConfigParserStateFindingEndpoint,
ConfigParserStateDone,
};
struct ConfigParser {
uint8_t state;
uint8_t descSize;
uint8_t descType;
uint8_t descOffset;
struct HidInfo* info;
};
void configParserInit(struct ConfigParser* parser, struct HidInfo* info) {
parser->state = ConfigParserStateFindingInterfaceClass;
parser->descSize = 0;
parser->descType = 0;
// size of header
parser->descOffset = 2;
parser->info = info;
info->bootMouseEndpoint = 0;
}
void configParserStep(struct ConfigParser* parser, uint8_t next) {
if (parser->descSize == 0) {
parser->descSize = next;
return;
}
if (parser->descType == 0) {
parser->descType = next;
return;
}
if (parser->state != ConfigParserStateDone) {
if (parser->descType == DESC_TYPE_CONFIGURATION &&
parser->descOffset == offsetof(struct ConfigurationDescriptor, bConfigurationValue)) {
parser->info->bootMouseConfiguration = next;
}
if (parser->descType == DESC_TYPE_INTERFACE &&
parser->descOffset == offsetof(struct InterfaceDescriptor, bInterfaceNumber)) {
parser->info->bootMouseInterface = next;
}
}
switch (parser->state) {
case ConfigParserStateFindingInterfaceClass:
if (parser->descType == DESC_TYPE_INTERFACE &&
parser->descOffset == offsetof(struct InterfaceDescriptor, bInterfaceClass) &&
next == DEVICE_CLASS_HID) {
parser->state = ConfigParserStateFindingInterfaceSubClass;
}
break;
case ConfigParserStateFindingInterfaceSubClass:
if (parser->descType == DESC_TYPE_INTERFACE &&
parser->descOffset == offsetof(struct InterfaceDescriptor, bInterfaceSubClass)) {
if (next == HID_SUBCLASS_BOOT) {
parser->state = ConfigParserStateFindingInterfaceProtocol;
} else {
parser->state = ConfigParserStateFindingInterfaceClass;
}
}
break;
case ConfigParserStateFindingInterfaceProtocol:
if (parser->descType == DESC_TYPE_INTERFACE &&
parser->descOffset == offsetof(struct InterfaceDescriptor, bInterfaceProtocol)) {
if (next == HID_PROTOCOL_MOUSE) {
parser->state = ConfigParserStateFindingEndpoint;
} else {
parser->state = ConfigParserStateFindingInterfaceClass;
}
}
break;
case ConfigParserStateFindingEndpoint:
if (parser->descType == DESC_TYPE_ENDPOINT &&
parser->descOffset == offsetof(struct EndpointDescriptor, bEndpointAddress)) {
parser->info->bootMouseEndpoint = next;
parser->state = ConfigParserStateDone;
}
}
parser->descOffset++;
if (parser->descOffset == parser->descSize) {
parser->descSize = 0;
parser->descType = 0;
parser->descOffset = 2;
}
}
void configParserPacketHandler(void* data, char* packetData, uint8_t packetSize, uint8_t offset) {
struct ConfigParser* parser = (struct ConfigParser*)data;
for (size_t i = 0; i < packetSize; ++i) {
configParserStep(parser, (uint8_t)packetData[i]);
}
}
void packetDirectCopy(void* data, char* packetData, uint8_t packetSize, uint8_t offset) {
memcpy((char*)data + offset, packetData, packetSize);
}
bool getHIDInfo(struct HidInfo* result) {
char readBuffer[18];
if (!readControlTransfer(
0,
REQUEST_TYPE_STANDARD | REQUEST_RECIPIENT_DEVICE,
GET_DESCRIPTOR,
PACK_WORD_BYTES(DESC_TYPE_DEVICE, 0x00),
0x00,
sizeof(struct DeviceDescriptor),
readBuffer,
packetDirectCopy
)) {
return false;
}
uint8_t bDeviceClass = ((struct DeviceDescriptor*)readBuffer)->bDeviceClass;
if (bDeviceClass != DEVICE_CLASS_DEVICE && bDeviceClass != DEVICE_CLASS_HID) {
// unsupported device
return false;
}
uint8_t bNumConfigurations = 1;//((struct DeviceDescriptor*)readBuffer)->bNumConfigurations;
for (uint8_t configurationIndex = 0; configurationIndex < bNumConfigurations; ++configurationIndex) {
if (!readControlTransfer(
0,
REQUEST_TYPE_STANDARD | REQUEST_RECIPIENT_DEVICE,
GET_DESCRIPTOR,
PACK_WORD_BYTES(DESC_TYPE_CONFIGURATION, configurationIndex),
0x00,
sizeof(struct ConfigurationDescriptor),
readBuffer,
packetDirectCopy
)) {
return false;
}
uint16_t wTotalLength = ((struct ConfigurationDescriptor*)readBuffer)->wTotalLength;
struct ConfigParser parser;
configParserInit(&parser, result);
if (!readControlTransfer(
0,
REQUEST_TYPE_STANDARD | REQUEST_RECIPIENT_DEVICE,
GET_DESCRIPTOR,
PACK_WORD_BYTES(DESC_TYPE_CONFIGURATION, configurationIndex),
0x00,
wTotalLength,
&parser,
configParserPacketHandler
)) {
return false;
}
if (result->bootMouseEndpoint != 0) {
return true;
}
}
return false;
}