From a8c292a7bd08b30a6d26183ee28ca73b53c0363a Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sat, 7 Nov 2015 23:32:02 +0100 Subject: [PATCH] Added Feature Report function to BootKeyboard This may be added to other HID Devices as well, but Keyboard makes the most sense. --- src/SingleReport/BootKeyboard.cpp | 38 +++++++++++++++++++++++++++---- src/SingleReport/BootKeyboard.h | 25 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/SingleReport/BootKeyboard.cpp b/src/SingleReport/BootKeyboard.cpp index debadb3..8d63c3c 100644 --- a/src/SingleReport/BootKeyboard.cpp +++ b/src/SingleReport/BootKeyboard.cpp @@ -120,10 +120,12 @@ bool BootKeyboard_::setup(USBSetup& setup) return true; } if (request == HID_GET_PROTOCOL) { + // TODO improve UEDATX = protocol; return true; } if (request == HID_GET_IDLE) { + // TODO improve UEDATX = idle; return true; } @@ -141,10 +143,38 @@ bool BootKeyboard_::setup(USBSetup& setup) } if (request == HID_SET_REPORT) { - // Check if data has the correct length - auto length = setup.wLength; - if(length == sizeof(leds)){ - USB_RecvControl(&leds, length); + // Check if data has the correct length afterwards + int length = setup.wLength; + + // Feature (set feature report) + if(setup.wValueH == HID_REPORT_TYPE_FEATURE){ + // No need to check for negative featureLength values, + // except the host tries to send more then 32k bytes. + // We dont have that much ram anyways. + if (length == featureLength) { + USB_RecvControl(featureReport, featureLength); + + // Block until data is read (make length negative) + disableFeatureReport(); + return true; + } + } + + // Output (set led states) + else if(setup.wValueH == HID_REPORT_TYPE_OUTPUT){ + if(length == sizeof(leds)){ + USB_RecvControl(&leds, length); + return true; + } + } + + // Input (set HID report) + else if(setup.wValueH == HID_REPORT_TYPE_INPUT) + { + if(length == sizeof(_keyReport)){ + USB_RecvControl(&_keyReport, length); + return true; + } } } } diff --git a/src/SingleReport/BootKeyboard.h b/src/SingleReport/BootKeyboard.h index 4d83e72..221272d 100644 --- a/src/SingleReport/BootKeyboard.h +++ b/src/SingleReport/BootKeyboard.h @@ -38,6 +38,28 @@ public: uint8_t getLeds(void); uint8_t getProtocol(void); void wakeupHost(void); + + void setFeatureReport(void* report, int length){ + if(length > 0){ + featureReport = (uint8_t*)report; + featureLength = length; + } + } + + int availableFeatureReport(void){ + if(featureLength < 0){ + return featureLength & ~0x8000; + } + return 0; + } + + void enableFeatureReport(void){ + featureLength &= ~0x8000; + } + + void disableFeatureReport(void){ + featureLength |= 0x8000; + } protected: // Implementation of the PUSBListNode @@ -51,6 +73,9 @@ protected: uint8_t leds; + uint8_t* featureReport; + int featureLength; + virtual int send(void) override; }; extern BootKeyboard_ BootKeyboard;