Debian Codegraph @

libraries/HID/src/HID.cpp

raw
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
19 #include "HID.h"
20
21 #if defined(USBCON)
22
23 HID_& HID()
24 {
25 static HID_ obj;
26 return obj;
27 }
28
29 int HID_::getInterface(uint8_t* interfaceCount)
30 {
31 *interfaceCount += 1; // uses 1
32 HIDDescriptor hidInterface = {
33 D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
34 D_HIDREPORT(descriptorSize),
35 D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
36 };
37 return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
38 }
39
40 int HID_::getDescriptor(USBSetup& setup)
41 {
42 // Check if this is a HID Class Descriptor request
43 if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
44 if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
45
46 // In a HID Class Descriptor wIndex contains the interface number
47 if (setup.wIndex != pluggedInterface) { return 0; }
48
49 int total = 0;
50 HIDSubDescriptor* node;
51 for (node = rootNode; node; node = node->next) {
52 int res = USB_SendControl(TRANSFER_PGM, node->data, node->length);
53 if (res == -1)
54 return -1;
55 total += res;
56 }
57
58 // Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
59 // due to the USB specs, but Windows and Linux just assumes its in report mode.
60 protocol = HID_REPORT_PROTOCOL;
61
62 return total;
63 }
64
65 uint8_t HID_::getShortName(char *name)
66 {
67 name[0] = 'H';
68 name[1] = 'I';
69 name[2] = 'D';
70 name[3] = 'A' + (descriptorSize & 0x0F);
71 name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
72 return 5;
73 }
74
75 void HID_::AppendDescriptor(HIDSubDescriptor *node)
76 {
77 if (!rootNode) {
78 rootNode = node;
79 } else {
80 HIDSubDescriptor *current = rootNode;
81 while (current->next) {
82 current = current->next;
83 }
84 current->next = node;
85 }
86 descriptorSize += node->length;
87 }
88
89 int HID_::SendReport(uint8_t id, const void* data, int len)
90 {
91 auto ret = USB_Send(pluggedEndpoint, &id, 1);
92 if (ret < 0) return ret;
93 auto ret2 = USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, len);
94 if (ret2 < 0) return ret2;
95 return ret + ret2;
96 }
97
98 bool HID_::setup(USBSetup& setup)
99 {
100 if (pluggedInterface != setup.wIndex) {
101 return false;
102 }
103
104 uint8_t request = setup.bRequest;
105 uint8_t requestType = setup.bmRequestType;
106
107 if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
108 {
109 if (request == HID_GET_REPORT) {
110 // TODO: HID_GetReport();
111 return true;
112 }
113 if (request == HID_GET_PROTOCOL) {
114 // TODO: Send8(protocol);
115 return true;
116 }
117 if (request == HID_GET_IDLE) {
118 // TODO: Send8(idle);
119 }
120 }
121
122 if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
123 {
124 if (request == HID_SET_PROTOCOL) {
125 // The USB Host tells us if we are in boot or report mode.
126 // This only works with a real boot compatible device.
127 protocol = setup.wValueL;
128 return true;
129 }
130 if (request == HID_SET_IDLE) {
131 idle = setup.wValueL;
132 return true;
133 }
134 if (request == HID_SET_REPORT)
135 {
136 //uint8_t reportID = setup.wValueL;
137 //uint16_t length = setup.wLength;
138 //uint8_t data[length];
139 // Make sure to not read more data than USB_EP_SIZE.
140 // You can read multiple times through a loop.
141 // The first byte (may!) contain the reportID on a multreport.
142 //USB_RecvControl(data, length);
143 }
144 }
145
146 return false;
147 }
148
149 HID_::HID_(void) : PluggableUSBModule(1, 1, epType),
150 rootNode(NULL), descriptorSize(0),
151 protocol(HID_REPORT_PROTOCOL), idle(1)
152 {
153 epType[0] = EP_TYPE_INTERRUPT_IN;
154 PluggableUSB().plug(this);
155 }
156
157 int HID_::begin(void)
158 {
159 return 0;
160 }
161
162 #endif /* if defined(USBCON) */