From b2ddb2267d6dde5e9c13083cb581435cc0a4b0fe Mon Sep 17 00:00:00 2001
From: Cristian Maglie <c.maglie@arduino.cc>
Date: Tue, 28 Jun 2016 15:54:38 +0200
Subject: [PATCH] USB: Added interface to possibly setup dynamic USB EP Handler

---
 cores/arduino/USB/USBCore.cpp | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/cores/arduino/USB/USBCore.cpp b/cores/arduino/USB/USBCore.cpp
index c66f75a4..2a7fa11f 100644
--- a/cores/arduino/USB/USBCore.cpp
+++ b/cores/arduino/USB/USBCore.cpp
@@ -46,6 +46,12 @@ extern "C" void UDD_Handler(void) {
 	USBDevice.ISRHandler();
 }
 
+class EPHandler {
+public:
+	virtual void handleEndpoint() = 0;
+	virtual uint32_t recv(void *_data, uint32_t len) = 0;
+	virtual uint32_t available() const = 0;
+};
 
 
 const uint16_t STRING_LANGUAGE[2] = {
@@ -88,6 +94,8 @@ uint8_t udd_ep_out_cache_buffer[7][64];
 static __attribute__((__aligned__(4))) //__attribute__((__section__(".bss_hram0")))
 uint8_t udd_ep_in_cache_buffer[7][64];
 
+static EPHandler *epHandlers[7];
+
 //==================================================================
 
 // Send a USB descriptor string. The string is stored as a
@@ -532,7 +540,11 @@ uint32_t USBDeviceClass::recvControl(void *_data, uint32_t len)
 // Number of bytes, assumes a rx endpoint
 uint32_t USBDeviceClass::available(uint32_t ep)
 {
-	return usbd.epBank0ByteCount(ep);
+	if (epHandlers[ep]) {
+		return epHandlers[ep]->available();
+	} else {
+		return usbd.epBank0ByteCount(ep);
+	}
 }
 
 // Non Blocking receive
@@ -542,6 +554,10 @@ uint32_t USBDeviceClass::recv(uint32_t ep, void *_data, uint32_t len)
 	if (!_usbConfiguration)
 		return -1;
 
+	if (epHandlers[ep]) {
+		return epHandlers[ep]->recv(_data, len);
+	}
+
 	if (available(ep) < len)
 		len = available(ep);
 
@@ -891,7 +907,11 @@ void USBDeviceClass::ISRHandler()
 			if (usbd.epBank0IsTransferComplete(i) ||
 			    usbd.epBank1IsTransferComplete(i))
 			{
-				handleEndpoint(i);
+				if (epHandlers[i]) {
+					epHandlers[i]->handleEndpoint();
+				} else {
+					handleEndpoint(i);
+				}
 			}
 			ept_int &= ~(1 << i);
 		}
-- 
GitLab