1
- /* Copyright (c) 2015, Arduino LLC
2
- **
3
- ** Original code (pre-library): Copyright (c) 2011, Peter Barrett
4
- **
5
- ** Permission to use, copy, modify, and/or distribute this software for
6
- ** any purpose with or without fee is hereby granted, provided that the
7
- ** above copyright notice and this permission notice appear in all copies.
8
- **
9
- ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10
- ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11
- ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12
- ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13
- ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14
- ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15
- ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16
- ** SOFTWARE.
17
- */
1
+ /*
2
+ Copyright (c) 2015, Arduino LLC
3
+ Original code (pre-library): Copyright (c) 2011, Peter Barrett
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for
6
+ any purpose with or without fee is hereby granted, provided that the
7
+ above copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10
+ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11
+ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12
+ BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13
+ OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14
+ WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15
+ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16
+ SOFTWARE.
17
+ */
18
18
19
19
#include " PluggableUSB.h"
20
20
#include " HID.h"
@@ -25,11 +25,10 @@ HID_ HID;
25
25
26
26
int HID_::getInterface (uint8_t * interfaceNum)
27
27
{
28
- interfaceNum[0 ] += 1 ; // uses 1
29
- hidInterface =
30
- {
28
+ *interfaceNum += 1 ; // uses 1
29
+ hidInterface = {
31
30
D_INTERFACE (interface (), 1 , 3 , 0 , 0 ),
32
- D_HIDREPORT (sizeof_hidReportDescriptor ),
31
+ D_HIDREPORT (descriptorSize ),
33
32
D_ENDPOINT (USB_ENDPOINT_IN (endpoint ()), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01 )
34
33
};
35
34
return USB_SendControl (0 , &hidInterface, sizeof (hidInterface));
@@ -38,14 +37,13 @@ int HID_::getInterface(uint8_t* interfaceNum)
38
37
int HID_::getDescriptor (int8_t type)
39
38
{
40
39
if (HID_REPORT_DESCRIPTOR_TYPE == type) {
41
- HIDDescriptorListNode* current = rootNode;
42
40
int total = 0 ;
43
- while (current != NULL ) {
44
- int res = USB_SendControl (TRANSFER_PGM, current->data , current->length );
41
+ HIDDescriptorListNode* node;
42
+ for (node = rootNode; node; node = node->next ) {
43
+ int res = USB_SendControl (TRANSFER_PGM, node->data , node->length );
45
44
if (res == -1 )
46
45
return -1 ;
47
46
total += res;
48
- current = current->next ;
49
47
}
50
48
return total;
51
49
}
@@ -60,61 +58,58 @@ void HID_::AppendDescriptor(HIDDescriptorListNode *node)
60
58
rootNode = node;
61
59
} else {
62
60
HIDDescriptorListNode *current = rootNode;
63
- while (current->next != NULL ) {
61
+ while (current->next ) {
64
62
current = current->next ;
65
63
}
66
64
current->next = node;
67
65
}
68
- sizeof_hidReportDescriptor += ( uint16_t ) node->length ;
66
+ descriptorSize += node->length ;
69
67
}
70
68
71
69
void HID_::SendReport (uint8_t id, const void * data, int len)
72
70
{
73
71
USB_Send (endpoint (), &id, 1 );
74
- USB_Send (endpoint () | TRANSFER_RELEASE,data,len);
72
+ USB_Send (endpoint () | TRANSFER_RELEASE, data, len);
75
73
}
76
74
77
75
bool HID_::setup (USBSetup& setup, uint8_t interfaceNum)
78
76
{
79
77
if (interface () != interfaceNum) {
80
78
return false ;
81
- } else {
82
- uint8_t r = setup.bRequest ;
83
- uint8_t requestType = setup.bmRequestType ;
84
- if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
85
- {
86
- if (HID_GET_REPORT == r)
87
- {
88
- // HID_GetReport();
89
- return true ;
90
- }
91
- if (HID_GET_PROTOCOL == r)
92
- {
93
- // Send8(protocol); // TODO
94
- return true ;
95
- }
79
+ }
80
+
81
+ uint8_t request = setup.bRequest ;
82
+ uint8_t requestType = setup.bmRequestType ;
83
+
84
+ if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
85
+ {
86
+ if (request == HID_GET_REPORT) {
87
+ // TODO: HID_GetReport();
88
+ return true ;
96
89
}
97
-
98
- if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
99
- {
100
- if (HID_SET_PROTOCOL == r)
101
- {
102
- protocol = setup.wValueL ;
103
- return true ;
104
- }
105
-
106
- if (HID_SET_IDLE == r)
107
- {
108
- idle = setup.wValueL ;
109
- return true ;
110
- }
90
+ if (request == HID_GET_PROTOCOL) {
91
+ // TODO: Send8(protocol);
92
+ return true ;
93
+ }
94
+ }
95
+
96
+ if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
97
+ {
98
+ if (request == HID_SET_PROTOCOL) {
99
+ protocol = setup.wValueL ;
100
+ return true ;
101
+ }
102
+ if (request == HID_SET_IDLE) {
103
+ idle = setup.wValueL ;
104
+ return true ;
111
105
}
112
- return false ;
113
106
}
107
+
108
+ return false ;
114
109
}
115
110
116
111
HID_::HID_ (void ) : PUSBListNode(1 , 1 , epType),
117
- rootNode(NULL ), sizeof_hidReportDescriptor (0 ),
112
+ rootNode(NULL ), descriptorSize (0 ),
118
113
protocol(1 ), idle(1 )
119
114
{
120
115
epType[0 ] = EP_TYPE_INTERRUPT_IN;
0 commit comments