110 lines
3.2 KiB
C++
110 lines
3.2 KiB
C++
/*
|
|
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 "BootKeyboard.h"
|
|
#include "HID-Driver.h"
|
|
|
|
BootKeyboard_::BootKeyboard_(void) : PUSBListNode(1, 1, epType), protocol(1), idle(1), leds(0)
|
|
{
|
|
epType[0] = EP_TYPE_INTERRUPT_IN;
|
|
PluggableUSB().plug(this);
|
|
}
|
|
|
|
int BootKeyboard_::getInterface(uint8_t* interfaceCount)
|
|
{
|
|
*interfaceCount += 1; // uses 1
|
|
HIDDescriptor hidInterface = {
|
|
D_INTERFACE(pluggedInterface, 1, 3, 1, 1), // Boot compatible keyboard
|
|
D_HIDREPORT(sizeof(_hidReportDescriptorKeyboard)),
|
|
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
|
|
};
|
|
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
|
|
}
|
|
|
|
int BootKeyboard_::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; }
|
|
|
|
return USB_SendControl(TRANSFER_PGM, _hidReportDescriptorKeyboard, sizeof(_hidReportDescriptorKeyboard));
|
|
}
|
|
|
|
bool BootKeyboard_::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)
|
|
{
|
|
// Check if data has the correct length
|
|
auto length = setup.wLength;
|
|
if(length == sizeof(leds)){
|
|
USB_RecvControl(&leds, length);
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
uint8_t BootKeyboard_::getLeds(void){
|
|
return leds;
|
|
}
|
|
|
|
void BootKeyboard_::sendReport(void* data, int length){
|
|
USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, length);
|
|
}
|
|
|
|
BootKeyboard_ BootKeyboard;
|
|
|
|
|