|
| 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 | +*/ |
| 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, 0x40, 0x01) |
| 36 | + }; |
| 37 | + return USBD_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 cointains 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 = USBD_SendControl(0, node->data, node->length); |
| 53 | + if (res == -1) |
| 54 | + return -1; |
| 55 | + total += res; |
| 56 | + } |
| 57 | + return total; |
| 58 | +} |
| 59 | + |
| 60 | +uint8_t HID_::getShortName(char *name) |
| 61 | +{ |
| 62 | + name[0] = 'H'; |
| 63 | + name[1] = 'I'; |
| 64 | + name[2] = 'D'; |
| 65 | + name[3] = 'A' + (descriptorSize & 0x0F); |
| 66 | + name[4] = 'A' + ((descriptorSize >> 4) & 0x0F); |
| 67 | + return 5; |
| 68 | +} |
| 69 | + |
| 70 | +void HID_::AppendDescriptor(HIDSubDescriptor *node) |
| 71 | +{ |
| 72 | + if (!rootNode) { |
| 73 | + rootNode = node; |
| 74 | + } else { |
| 75 | + HIDSubDescriptor *current = rootNode; |
| 76 | + while (current->next) { |
| 77 | + current = current->next; |
| 78 | + } |
| 79 | + current->next = node; |
| 80 | + } |
| 81 | + descriptorSize += node->length; |
| 82 | +} |
| 83 | + |
| 84 | +int HID_::SendReport(uint8_t id, const void* data, int len) |
| 85 | +{ |
| 86 | + uint8_t p[64]; |
| 87 | + p[0] = id; |
| 88 | + memcpy(&p[1], data, len); |
| 89 | + return USBD_Send(pluggedEndpoint, p, len+1); |
| 90 | +} |
| 91 | + |
| 92 | +bool HID_::setup(USBSetup& setup) |
| 93 | +{ |
| 94 | + if (pluggedInterface != setup.wIndex) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + uint8_t request = setup.bRequest; |
| 99 | + uint8_t requestType = setup.bmRequestType; |
| 100 | + |
| 101 | + if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) |
| 102 | + { |
| 103 | + if (request == HID_GET_REPORT) { |
| 104 | + // TODO: HID_GetReport(); |
| 105 | + return true; |
| 106 | + } |
| 107 | + if (request == HID_GET_PROTOCOL) { |
| 108 | + // TODO: Send8(protocol); |
| 109 | + return true; |
| 110 | + } |
| 111 | + if (request == HID_GET_IDLE) { |
| 112 | + // TODO: Send8(idle); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE) |
| 117 | + { |
| 118 | + if (request == HID_SET_PROTOCOL) { |
| 119 | + // The USB Host tells us if we are in boot or report mode. |
| 120 | + // This only works with a real boot compatible device. |
| 121 | + protocol = setup.wValueL; |
| 122 | + return true; |
| 123 | + } |
| 124 | + if (request == HID_SET_IDLE) { |
| 125 | + idle = setup.wValueL; |
| 126 | + return true; |
| 127 | + } |
| 128 | + if (request == HID_SET_REPORT) |
| 129 | + { |
| 130 | + //uint8_t reportID = setup.wValueL; |
| 131 | + //uint16_t length = setup.wLength; |
| 132 | + //uint8_t data[length]; |
| 133 | + // Make sure to not read more data than USB_EP_SIZE. |
| 134 | + // You can read multiple times through a loop. |
| 135 | + // The first byte (may!) contain the reportID on a multreport. |
| 136 | + //USB_RecvControl(data, length); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return false; |
| 141 | +} |
| 142 | + |
| 143 | +HID_::HID_(void) : PluggableUSBModule(1, 1, epType), |
| 144 | + rootNode(NULL), descriptorSize(0), |
| 145 | + protocol(1), idle(1) |
| 146 | +{ |
| 147 | + epType[0] = EP_TYPE_INTERRUPT_IN; |
| 148 | + PluggableUSB().plug(this); |
| 149 | +} |
| 150 | + |
| 151 | +int HID_::begin(void) |
| 152 | +{ |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +#endif /* if defined(USBCON) */ |
0 commit comments