HID/src/SingleReport/RawHID.cpp

171 lines
5.6 KiB
C++
Raw Normal View History

2015-10-11 12:51:18 +00:00
/*
Copyright (c) 2014-2015 NicoHood
See the readme for credit to other people.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "RawHID.h"
static const uint8_t _hidReportDescriptorRawHID[] PROGMEM = {
/* RAW HID */
0x06, lowByte(RAWHID_USAGE_PAGE), highByte(RAWHID_USAGE_PAGE), /* 30 */
0x0A, lowByte(RAWHID_USAGE), highByte(RAWHID_USAGE),
0xA1, 0x01, /* Collection 0x01 */
// RawHID is not multireport compatible.
// On Linux it might work with some modifications,
// however you are not happy to use it like that.
//0x85, HID_REPORTID_RAWHID, /* REPORT_ID */
0x75, 0x08, /* report size = 8 bits */
0x15, 0x00, /* logical minimum = 0 */
0x26, 0xFF, 0x00, /* logical maximum = 255 */
0x95, RAWHID_TX_SIZE, /* report count TX */
0x09, 0x01, /* usage */
0x81, 0x02, /* Input (array) */
0x95, RAWHID_RX_SIZE, /* report count RX */
0x09, 0x02, /* usage */
0x91, 0x02, /* Output (array) */
0xC0 /* end collection */
};
// Weak implementation of the Event function
2015-11-07 09:54:50 +00:00
void RawHIDEvent(uint8_t num) __attribute__ ((weak));
void RawHIDEvent(uint8_t num){
// Empty
}
2015-10-12 16:04:11 +00:00
RawHID_::RawHID_(void) : PluggableUSBModule(1, 1, epType), protocol(HID_REPORT_PROTOCOL), idle(1), dataLength(0)
2015-10-11 12:51:18 +00:00
{
epType[0] = EP_TYPE_INTERRUPT_IN;
PluggableUSB().plug(this);
}
int RawHID_::getInterface(uint8_t* interfaceCount)
{
2015-10-13 16:41:15 +00:00
// Maybe as optional device FastRawHID with different USAGE PAGE
2015-10-11 12:51:18 +00:00
*interfaceCount += 1; // uses 1
HIDDescriptor hidInterface = {
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
D_HIDREPORT(sizeof(_hidReportDescriptorRawHID)),
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
};
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
}
int RawHID_::getDescriptor(USBSetup& setup)
{
// Check if this is a HID Class Descriptor request
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
// In a HID Class Descriptor wIndex cointains the interface number
if (setup.wIndex != pluggedInterface) { return 0; }
2015-10-12 16:04:11 +00:00
// Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
// due to the USB specs, but Windows and Linux just assumes its in report mode.
protocol = HID_REPORT_PROTOCOL;
2015-10-11 12:51:18 +00:00
return USB_SendControl(TRANSFER_PGM, _hidReportDescriptorRawHID, sizeof(_hidReportDescriptorRawHID));
}
bool RawHID_::setup(USBSetup& setup)
{
if (pluggedInterface != setup.wIndex) {
return false;
}
uint8_t request = setup.bRequest;
uint8_t requestType = setup.bmRequestType;
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
{
if (request == HID_GET_REPORT) {
// TODO: HID_GetReport();
return true;
}
if (request == HID_GET_PROTOCOL) {
// TODO: Send8(protocol);
return true;
}
}
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
{
if (request == HID_SET_PROTOCOL) {
protocol = setup.wValueL;
return true;
}
if (request == HID_SET_IDLE) {
idle = setup.wValueL;
return true;
}
if (request == HID_SET_REPORT)
{
2015-10-13 16:41:15 +00:00
// Get the data length information and the corresponding bytes
int length = setup.wLength;
2015-11-07 09:54:50 +00:00
uint8_t num = 0;
while(length)
2015-10-13 16:41:15 +00:00
{
// Dont receive more than the USB EP has to offer
// TODO use fixed 64 because control EP always have 64 bytes even on 16 u2. Test!
auto recvLength = length;
if(recvLength > USB_EP_SIZE){
recvLength = USB_EP_SIZE;
}
if(recvLength > int(sizeof(data))){
recvLength = int(sizeof(data));
}
length -= recvLength;
// Only receive data if the last one was read fully.
// Discard data if it wasnt read yet.
// To not miss any data you can use the (weak) event function.
if(!dataLength)
{
// Write data to fit to the end (not the beginning) of the array
USB_RecvControl(data + (sizeof(data) - recvLength), recvLength);
dataLength = recvLength;
2015-11-07 09:54:50 +00:00
RawHIDEvent(num++);
}
// Do not read the data, flag an error to the USB Host
// TODO always return no error, even if data was discarded? -> use break then
else{
return false;
2015-10-13 16:41:15 +00:00
}
}
// Flag no error
// TODO this is required to get hyperion working
// however this blocks the CDC serial!?
return true;
2015-10-11 12:51:18 +00:00
}
}
return false;
}
void RawHID_::SendReport(void* data, int length){
USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, length);
}
RawHID_ RawHID;