forked from arpruss/USBComposite_stm32f1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
USBHID.cpp
321 lines (269 loc) · 8.95 KB
/
USBHID.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* Copyright (c) 2011, Peter Barrett
* Copyright (c) 2017-2018, Alexander Pruss
**
** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
** SOFTWARE.
*/
#include "USBComposite.h"
#include <string.h>
#include <stdint.h>
#include <libmaple/nvic.h>
#include "usb_hid.h"
#include "usb_composite_serial.h"
#include "usb_generic.h"
#include <libmaple/usb.h>
#include <string.h>
#include <libmaple/iwdg.h>
/*
* USB HID interface
*/
bool USBHID::init(USBHID* me) {
usb_hid_setTXEPSize(me->txPacketSize);
HIDReporter* r = me->profiles;
if (me->baseChunk.data != NULL) {
/* user set an explicit report for USBHID */
while (r != NULL) {
r->reportID = r->userSuppliedReportID;
if (r->reportID != 0)
r->reportBuffer[0] = r->reportID;
r = r->next;
}
usb_hid_set_report_descriptor(&(me->baseChunk));
}
else {
uint8 reportID = HID_AUTO_REPORT_ID_START;
while (r != NULL) {
if (r->forceUserSuppliedReportID)
r->reportID = r->userSuppliedReportID;
else
r->reportID = reportID++;
if (r->reportID != 0)
r->reportBuffer[0] = r->reportID;
r = r->next;
}
usb_hid_set_report_descriptor(me->chunkList);
}
return true;
}
bool USBHID::registerComponent() {
return USBComposite.add(&usbHIDPart, this, (USBPartInitializer)&USBHID::init);
}
void USBHID::setReportDescriptor(const uint8_t* report_descriptor, uint16_t report_descriptor_length) {
baseChunk.dataLength = report_descriptor_length;
baseChunk.data = report_descriptor;
baseChunk.next = NULL;
}
void USBHID::clear() {
clearBuffers();
baseChunk.data = NULL;
baseChunk.dataLength = 0;
baseChunk.next = NULL;
chunkList = NULL;
profiles = NULL;
}
void USBHID::addReport(HIDReporter* r, bool always) {
if (! always && ! autoRegister)
return;
r->next = NULL;
if (profiles == NULL) {
profiles = r;
}
else {
HIDReporter* tail = profiles;
while (tail != r && tail->next != NULL) {
tail = tail->next;
}
if (tail == r)
return;
tail->next = r;
}
struct usb_chunk* chunkTail = chunkList;
if (chunkTail == NULL) {
chunkList = &(r->reportChunks[0]);
chunkTail = chunkList;
}
else {
while (chunkTail->next != NULL)
chunkTail = chunkTail->next;
chunkTail->next = &(r->reportChunks[0]);
chunkTail = chunkTail->next;
}
chunkTail->next = &(r->reportChunks[1]);
chunkTail = chunkTail->next;
chunkTail->next = &(r->reportChunks[2]);
chunkTail = chunkTail->next;
chunkTail->next = NULL;
}
void USBHID::setReportDescriptor(const HIDReportDescriptor* report) {
if (report == NULL)
setReportDescriptor(NULL, 0);
else
setReportDescriptor(report->descriptor, report->length);
}
void USBHID::begin(const uint8_t* report_descriptor, uint16_t report_descriptor_length) {
if (enabledHID)
return;
setReportDescriptor(report_descriptor, report_descriptor_length);
USBComposite.clear();
registerComponent();
USBComposite.begin();
enabledHID = true;
}
void USBHID::begin(const HIDReportDescriptor* report) {
if (report == NULL)
begin(NULL, 0);
else
begin(report->descriptor, report->length);
}
void USBHID::setBuffers(uint8_t type, volatile HIDBuffer_t* fb, int count) {
usb_hid_set_buffers(type, fb, count);
}
bool USBHID::addBuffer(uint8_t type, volatile HIDBuffer_t* buffer) {
return 0 != usb_hid_add_buffer(type, buffer);
}
void USBHID::clearBuffers(uint8_t type) {
usb_hid_clear_buffers(type);
}
void USBHID::clearBuffers() {
clearBuffers(HID_REPORT_TYPE_OUTPUT);
clearBuffers(HID_REPORT_TYPE_FEATURE);
}
void USBHID::end(void){
if(enabledHID) {
USBComposite.end();
enabledHID = false;
}
}
void USBHID::begin(USBCompositeSerial serial, const uint8_t* report_descriptor, uint16_t report_descriptor_length) {
if (enabledHID)
return;
USBComposite.clear();
setReportDescriptor(report_descriptor, report_descriptor_length);
registerComponent();
serial.registerComponent();
USBComposite.begin();
}
void USBHID::begin(USBCompositeSerial serial, const HIDReportDescriptor* report) {
if (report == NULL)
begin(serial, NULL, 0);
else
begin(serial, report->descriptor, report->length);
}
void HIDReporter::sendReport() {
unsigned toSend = bufferSize;
uint8* b = reportBuffer;
while (toSend) {
unsigned delta = usb_hid_tx(b, toSend);
toSend -= delta;
b += delta;
}
/* flush out to avoid having the pc wait for more data */
/* TODO: remove? this does nothing right now! */
usb_hid_tx(NULL, 0);
}
void HIDReporter::registerProfile(bool always) {
for (uint32 i=0; i<3; i++) {
reportChunks[i].data = NULL;
reportChunks[i].dataLength = 0;
}
if (reportDescriptor.descriptor != NULL) {
int32_t reportIDOffset = -1;
if (! forceUserSuppliedReportID) {
uint32_t i = 0;
/*
* parse bits of beginning of report looking for a single-byte report ID (0x85 ID)
*/
while (i < reportDescriptor.length && reportIDOffset < 0) {
if (reportDescriptor.descriptor[i]==0x85) {
if (i+1 < reportDescriptor.length)
reportIDOffset = i+1;
}
// the lowest 2 bit determine the data length of the tag
if ((reportDescriptor.descriptor[i] & 3) == 3)
i += 5;
else
i += 1 + (reportDescriptor.descriptor[i] & 3);
}
if (i >= reportDescriptor.length) {
forceUserSuppliedReportID = true;
}
}
if (forceUserSuppliedReportID) {
reportChunks[0].data = reportDescriptor.descriptor;
reportChunks[0].dataLength = reportDescriptor.length;
}
else {
reportChunks[0].data = reportDescriptor.descriptor;
reportChunks[0].dataLength = reportIDOffset;
reportChunks[1].data = &(reportID);
reportChunks[1].dataLength = 1;
reportChunks[2].data = reportDescriptor.descriptor+reportIDOffset+1;
reportChunks[2].dataLength = reportDescriptor.length - (reportIDOffset+1);
}
}
HID.addReport(this, always);
}
HIDReporter::HIDReporter(USBHID& _HID, const HIDReportDescriptor* r, uint8_t* _buffer, unsigned _size, uint8_t _reportID, bool forceReportID) : HID(_HID) {
if (r != NULL) {
memcpy(&reportDescriptor, r, sizeof(reportDescriptor));
}
else {
reportDescriptor.descriptor = NULL;
reportDescriptor.length = 0;
}
if (_reportID == 0) {
reportBuffer = _buffer+1;
bufferSize = _size-1;
}
else {
reportBuffer = _buffer;
bufferSize = _size;
}
memset(reportBuffer, 0, bufferSize);
userSuppliedReportID = _reportID;
if (_size > 0 && _reportID != 0 && ! forceReportID) {
reportBuffer[0] = _reportID;
forceUserSuppliedReportID = false;
}
else {
forceUserSuppliedReportID = true;
}
registerProfile(false);
}
HIDReporter::HIDReporter(USBHID& _HID, const HIDReportDescriptor* r, uint8_t* _buffer, unsigned _size) : HID(_HID) {
if (r != NULL) {
memcpy(&reportDescriptor, r, sizeof(reportDescriptor));
}
else {
reportDescriptor.descriptor = NULL;
reportDescriptor.length = 0;
}
reportBuffer = _buffer;
bufferSize = _size;
memset(_buffer, 0, _size);
userSuppliedReportID = 0;
forceUserSuppliedReportID = true;
registerProfile(false);
}
void HIDReporter::setFeature(uint8_t* in) {
return usb_hid_set_feature(reportID, in);
}
uint16_t HIDReporter::getData(uint8_t type, uint8_t* out, uint8_t poll) {
return usb_hid_get_data(type, reportID, out, poll);
}
uint16_t HIDReporter::getFeature(uint8_t* out, uint8_t poll) {
return usb_hid_get_data(HID_REPORT_TYPE_FEATURE, reportID, out, poll);
}
uint16_t HIDReporter::getOutput(uint8_t* out, uint8_t poll) {
return usb_hid_get_data(HID_REPORT_TYPE_OUTPUT, reportID, out, poll);
}