Skip to content

Commit 21a2889

Browse files
authored
Merge pull request #22 from forderud/tab-space-conv
TAB to space conversion & consistent indentation
2 parents 7c828b9 + d2cb5cc commit 21a2889

File tree

1 file changed

+148
-159
lines changed

1 file changed

+148
-159
lines changed

src/HID/HID.cpp

Lines changed: 148 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -23,80 +23,77 @@
2323

2424
HID_& HID()
2525
{
26-
static HID_ obj;
27-
return obj;
26+
static HID_ obj;
27+
return obj;
2828
}
2929

3030
int HID_::getInterface(uint8_t* interfaceCount)
3131
{
32-
*interfaceCount += 1; // uses 1
33-
HIDDescriptor hidInterface = {
34-
D_INTERFACE(pluggedInterface, 2, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
35-
D_HIDREPORT(descriptorSize),
36-
D_ENDPOINT(USB_ENDPOINT_IN(HID_TX), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x14),
37-
D_ENDPOINT(USB_ENDPOINT_OUT(HID_RX), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x0A)
38-
};
39-
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
32+
*interfaceCount += 1; // uses 1
33+
HIDDescriptor hidInterface = {
34+
D_INTERFACE(pluggedInterface, 2, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
35+
D_HIDREPORT(descriptorSize),
36+
D_ENDPOINT(USB_ENDPOINT_IN(HID_TX), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x14),
37+
D_ENDPOINT(USB_ENDPOINT_OUT(HID_RX), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x0A)
38+
};
39+
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
4040
}
4141

42-
// Since this function is not exposed in USBCore API, had to replicate here.
42+
// Since this function is not exposed in USBCore API, had to replicate here.
4343
static bool USB_SendStringDescriptor(const char* string_P, u8 string_len, uint8_t flags) {
44-
45-
u8 c[2] = {(u8)(2 + string_len * 2), 3};
46-
47-
USB_SendControl(0,&c,2);
48-
49-
bool pgm = flags & TRANSFER_PGM;
50-
for(u8 i = 0; i < string_len; i++) {
51-
c[0] = pgm ? pgm_read_byte(&string_P[i]) : string_P[i];
52-
c[1] = 0;
53-
int r = USB_SendControl(0,&c,2);
54-
if(!r) {
55-
return false;
56-
}
57-
}
58-
return true;
44+
u8 c[2] = {(u8)(2 + string_len * 2), 3};
45+
46+
USB_SendControl(0,&c,2);
47+
48+
bool pgm = flags & TRANSFER_PGM;
49+
for(u8 i = 0; i < string_len; i++) {
50+
c[0] = pgm ? pgm_read_byte(&string_P[i]) : string_P[i];
51+
c[1] = 0;
52+
int r = USB_SendControl(0,&c,2);
53+
if(!r) {
54+
return false;
55+
}
56+
}
57+
return true;
5958
}
6059

6160
int HID_::getDescriptor(USBSetup& setup)
6261
{
63-
64-
u8 t = setup.wValueH;
65-
66-
// HID-specific strings
67-
if(USB_STRING_DESCRIPTOR_TYPE == t) {
68-
69-
// we place all strings in the 0xFF00-0xFFFE range
70-
HIDReport* rep = GetFeature(0xFF00 | setup.wValueL );
71-
if(rep) {
72-
return USB_SendStringDescriptor((char*)rep->data, strlen_P((char*)rep->data), TRANSFER_PGM);
73-
}
74-
else {
75-
return 0;
76-
}
62+
u8 t = setup.wValueH;
63+
64+
// HID-specific strings
65+
if(USB_STRING_DESCRIPTOR_TYPE == t) {
66+
// we place all strings in the 0xFF00-0xFFFE range
67+
HIDReport* rep = GetFeature(0xFF00 | setup.wValueL );
68+
if(rep) {
69+
return USB_SendStringDescriptor((char*)rep->data, strlen_P((char*)rep->data), TRANSFER_PGM);
7770
}
78-
79-
// Check if this is a HID Class Descriptor request
80-
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
81-
if (HID_REPORT_DESCRIPTOR_TYPE != t) { return 0; }
82-
83-
// In a HID Class Descriptor wIndex cointains the interface number
84-
if (setup.wIndex != pluggedInterface) { return 0; }
85-
86-
int total = 0;
87-
HIDSubDescriptor* node;
88-
for (node = rootNode; node; node = node->next) {
89-
int res = USB_SendControl(TRANSFER_PGM, node->data, node->length);
90-
if (res == -1)
91-
return -1;
92-
total += res;
93-
}
94-
95-
// Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
96-
// due to the USB specs, but Windows and Linux just assumes its in report mode.
97-
protocol = HID_REPORT_PROTOCOL;
98-
99-
return total;
71+
else {
72+
return 0;
73+
}
74+
}
75+
76+
// Check if this is a HID Class Descriptor request
77+
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
78+
if (HID_REPORT_DESCRIPTOR_TYPE != t) { return 0; }
79+
80+
// In a HID Class Descriptor wIndex cointains the interface number
81+
if (setup.wIndex != pluggedInterface) { return 0; }
82+
83+
int total = 0;
84+
HIDSubDescriptor* node;
85+
for (node = rootNode; node; node = node->next) {
86+
int res = USB_SendControl(TRANSFER_PGM, node->data, node->length);
87+
if (res == -1)
88+
return -1;
89+
total += res;
90+
}
91+
92+
// Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
93+
// due to the USB specs, but Windows and Linux just assumes its in report mode.
94+
protocol = HID_REPORT_PROTOCOL;
95+
96+
return total;
10097
}
10198

10299
uint8_t HID_::getShortName(char *name)
@@ -108,30 +105,28 @@ uint8_t HID_::getShortName(char *name)
108105
return strlen_P(serial);
109106
}
110107
else {
111-
112108
// default serial number
113-
114-
name[0] = 'H';
115-
name[1] = 'I';
116-
name[2] = 'D';
117-
name[3] = 'A' + (descriptorSize & 0x0F);
118-
name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
119-
return 5;
109+
name[0] = 'H';
110+
name[1] = 'I';
111+
name[2] = 'D';
112+
name[3] = 'A' + (descriptorSize & 0x0F);
113+
name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
114+
return 5;
120115
}
121116
}
122117

123118
void HID_::AppendDescriptor(HIDSubDescriptor *node)
124119
{
125-
if (!rootNode) {
126-
rootNode = node;
127-
} else {
128-
HIDSubDescriptor *current = rootNode;
129-
while (current->next) {
130-
current = current->next;
131-
}
132-
current->next = node;
133-
}
134-
descriptorSize += node->length;
120+
if (!rootNode) {
121+
rootNode = node;
122+
} else {
123+
HIDSubDescriptor *current = rootNode;
124+
while (current->next) {
125+
current = current->next;
126+
}
127+
current->next = node;
128+
}
129+
descriptorSize += node->length;
135130
}
136131

137132
int HID_::SetFeature(uint16_t id, const void* data, int len)
@@ -151,9 +146,8 @@ int HID_::SetFeature(uint16_t id, const void* data, int len)
151146
break;
152147
}
153148
}
154-
155149
}
156-
150+
157151
reportCount++;
158152
return reportCount;
159153
}
@@ -174,11 +168,11 @@ bool HID_::LockFeature(uint16_t id, bool lock) {
174168

175169
int HID_::SendReport(uint16_t id, const void* data, int len)
176170
{
177-
auto ret = USB_Send(HID_TX, &id, 1);
178-
if (ret < 0) return ret;
179-
auto ret2 = USB_Send(HID_TX | TRANSFER_RELEASE, data, len);
180-
if (ret2 < 0) return ret2;
181-
return ret + ret2;
171+
auto ret = USB_Send(HID_TX, &id, 1);
172+
if (ret < 0) return ret;
173+
auto ret2 = USB_Send(HID_TX | TRANSFER_RELEASE, data, len);
174+
if (ret2 < 0) return ret2;
175+
return ret + ret2;
182176
}
183177

184178
HIDReport* HID_::GetFeature(uint16_t id)
@@ -194,89 +188,84 @@ HIDReport* HID_::GetFeature(uint16_t id)
194188
}
195189

196190
bool HID_::setup(USBSetup& setup)
197-
{
198-
if (pluggedInterface != setup.wIndex) {
199-
return false;
200-
}
201-
202-
uint8_t request = setup.bRequest;
203-
uint8_t requestType = setup.bmRequestType;
204-
205-
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
206-
{
207-
if (request == HID_GET_REPORT) {
208-
209-
if(setup.wValueH == HID_REPORT_TYPE_FEATURE)
210-
{
211-
212-
HIDReport* current = GetFeature(setup.wValueL);
213-
if(current){
214-
if(USB_SendControl(0, &(current->id), 1)>0 &&
215-
USB_SendControl(0, current->data, current->length)>0)
216-
return true;
217-
}
218-
219-
return false;
220-
221-
}
222-
return true;
223-
}
224-
if (request == HID_GET_PROTOCOL) {
225-
// TODO: Send8(protocol);
226-
return true;
227-
}
228-
if (request == HID_GET_IDLE) {
229-
// TODO: Send8(idle);
230-
}
231-
}
232-
233-
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
234-
{
235-
if (request == HID_SET_PROTOCOL) {
236-
// The USB Host tells us if we are in boot or report mode.
237-
// This only works with a real boot compatible device.
238-
protocol = setup.wValueL;
239-
return true;
240-
}
241-
if (request == HID_SET_IDLE) {
242-
idle = setup.wValueL;
243-
return true;
244-
}
245-
if (request == HID_SET_REPORT)
246-
{
247-
if(setup.wValueH == HID_REPORT_TYPE_FEATURE)
248-
{
249-
250-
HIDReport* current = GetFeature(setup.wValueL);
251-
if(!current) return false;
252-
if(setup.wLength != current->length + 1) return false;
253-
uint8_t* data = new uint8_t[setup.wLength];
254-
USB_RecvControl(data, setup.wLength);
255-
if(*data != current->id) return false;
256-
memcpy((uint8_t*)current->data, data+1, current->length);
257-
delete[] data;
258-
return true;
259-
260-
}
261-
262-
}
263-
}
264-
265-
return false;
191+
{
192+
if (pluggedInterface != setup.wIndex) {
193+
return false;
194+
}
195+
196+
uint8_t request = setup.bRequest;
197+
uint8_t requestType = setup.bmRequestType;
198+
199+
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
200+
{
201+
if (request == HID_GET_REPORT) {
202+
if(setup.wValueH == HID_REPORT_TYPE_FEATURE)
203+
{
204+
HIDReport* current = GetFeature(setup.wValueL);
205+
if(current){
206+
if(USB_SendControl(0, &(current->id), 1)>0 &&
207+
USB_SendControl(0, current->data, current->length)>0)
208+
return true;
209+
}
210+
211+
return false;
212+
}
213+
return true;
214+
}
215+
if (request == HID_GET_PROTOCOL) {
216+
// TODO: Send8(protocol);
217+
return true;
218+
}
219+
if (request == HID_GET_IDLE) {
220+
// TODO: Send8(idle);
221+
}
222+
}
223+
224+
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
225+
{
226+
if (request == HID_SET_PROTOCOL) {
227+
// The USB Host tells us if we are in boot or report mode.
228+
// This only works with a real boot compatible device.
229+
protocol = setup.wValueL;
230+
return true;
231+
}
232+
if (request == HID_SET_IDLE) {
233+
idle = setup.wValueL;
234+
return true;
235+
}
236+
if (request == HID_SET_REPORT)
237+
{
238+
if(setup.wValueH == HID_REPORT_TYPE_FEATURE)
239+
{
240+
241+
HIDReport* current = GetFeature(setup.wValueL);
242+
if(!current) return false;
243+
if(setup.wLength != current->length + 1) return false;
244+
uint8_t* data = new uint8_t[setup.wLength];
245+
USB_RecvControl(data, setup.wLength);
246+
if(*data != current->id) return false;
247+
memcpy((uint8_t*)current->data, data+1, current->length);
248+
delete[] data;
249+
return true;
250+
}
251+
}
252+
}
253+
254+
return false;
266255
}
267256

268257
HID_::HID_(void) : PluggableUSBModule(2, 1, epType),
269258
rootNode(NULL), descriptorSize(0),
270259
protocol(HID_REPORT_PROTOCOL), idle(1)
271260
{
272-
epType[0] = EP_TYPE_INTERRUPT_IN;
261+
epType[0] = EP_TYPE_INTERRUPT_IN;
273262
epType[1] = EP_TYPE_INTERRUPT_OUT;
274-
PluggableUSB().plug(this);
263+
PluggableUSB().plug(this);
275264
}
276265

277266
int HID_::begin(void)
278267
{
279-
return 0;
268+
return 0;
280269
}
281270

282271
#endif /* if defined(USBCON) */

0 commit comments

Comments
 (0)