From cbbd3d746acef02a4afd562652a2ba71b24dfd6f Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Sun, 26 Jul 2009 10:22:16 +0000
Subject: [PATCH] Added new Pipe_BoundEndpointNumber() and
 Pipe_IsEndpointBound() functions.

---
 LUFA/Drivers/USB/LowLevel/Host.c |  1 -
 LUFA/Drivers/USB/LowLevel/Pipe.c | 16 ++++++++++++++++
 LUFA/Drivers/USB/LowLevel/Pipe.h | 24 +++++++++++++++++++++---
 LUFA/ManPages/ChangeLog.txt      |  1 +
 4 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/LUFA/Drivers/USB/LowLevel/Host.c b/LUFA/Drivers/USB/LowLevel/Host.c
index ae7525bee..3d99b2594 100644
--- a/LUFA/Drivers/USB/LowLevel/Host.c
+++ b/LUFA/Drivers/USB/LowLevel/Host.c
@@ -187,7 +187,6 @@ void USB_Host_ProcessNextHostState(void)
 
 			EVENT_USB_DeviceEnumerationComplete();
 			USB_HostState = HOST_STATE_Addressed;
-
 			break;
 	}
 
diff --git a/LUFA/Drivers/USB/LowLevel/Pipe.c b/LUFA/Drivers/USB/LowLevel/Pipe.c
index 7d3e87602..0a32f183b 100644
--- a/LUFA/Drivers/USB/LowLevel/Pipe.c
+++ b/LUFA/Drivers/USB/LowLevel/Pipe.c
@@ -70,6 +70,22 @@ void Pipe_ClearPipes(void)
 	}
 }
 
+bool Pipe_IsEndpointBound(uint8_t EndpointAddress)
+{
+	uint8_t PrevPipeNumber = Pipe_GetPipeNumber();
+
+	for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
+	{
+		Pipe_SelectPipe(PNum);
+		
+		if (Pipe_IsConfigured() && (Pipe_BoundEndpointAddress() == EndpointAddress))
+		  return true;
+	}
+	
+	Pipe_SelectPipe(PrevPipeNumber);
+	return false;
+}
+
 uint8_t Pipe_WaitUntilReady(void)
 {
 	#if (USB_STREAM_TIMEOUT_MS < 0xFF)
diff --git a/LUFA/Drivers/USB/LowLevel/Pipe.h b/LUFA/Drivers/USB/LowLevel/Pipe.h
index fae8105ff..b6fad5146 100644
--- a/LUFA/Drivers/USB/LowLevel/Pipe.h
+++ b/LUFA/Drivers/USB/LowLevel/Pipe.h
@@ -257,6 +257,13 @@
 				 */
 				static inline bool Pipe_IsConfigured(void);
 				
+				/** Retrieves the endpoint number of the endpoint within the attached device that the currently selected
+				 *  pipe is bound to.
+				 *
+				 *  \return Endpoint number the currently selected pipe is bound to
+				 */
+				static inline uint8_t Pipe_BoundEndpointNumber(void);
+
 				/** Sets the period between interrupts for an INTERRUPT type pipe to a specified number of milliseconds.
 				 *
 				 *  \param[in] Milliseconds  Number of milliseconds between each pipe poll
@@ -427,6 +434,8 @@
 
 				#define Pipe_IsConfigured()            ((UPSTAX  & (1 << CFGOK)) ? true : false)
 
+				#define Pipe_BoundEndpointNumber()     ((UPCFG0X >> PEPNUM0) & PIPE_EPNUM_MASK)
+				
 				#define Pipe_SetInterruptPeriod(ms)    MACROS{ UPCFG2X = ms; }MACROE
 
 				#define Pipe_GetPipeInterrupts()       UPINT
@@ -764,14 +773,23 @@
 			bool Pipe_ConfigurePipe(const uint8_t  Number, const uint8_t Type, const uint8_t Token, const uint8_t EndpointNumber,
 			                        const uint16_t Size, const uint8_t Banks);
 
-			/** Spinloops until the currently selected non-control pipe is ready for the next packed of data
-			 *  to be read or written to it.
+			/** Spinloops until the currently selected non-control pipe is ready for the next packed of data to be read 
+			 *  or written to it, aborting in the case of an error condition (such as a timeout or device disconnect).
 			 *
 			 *  \ingroup Group_PipeRW
 			 *
 			 *  \return A value from the Pipe_WaitUntilReady_ErrorCodes_t enum.
 			 */
-			uint8_t Pipe_WaitUntilReady(void);		
+			uint8_t Pipe_WaitUntilReady(void);
+			
+			/** Determines if a pipe has been bound to the given device endpoint address. If a pipe which is bound to the given
+			 *  endpoint is found, it is automatically selected.
+			 *
+			 *  \param EndpointAddress Address of the endpoint within the attached device to check
+			 *
+			 *  \return Boolean true if a pipe bound to the given endpoint address is found, false otherwise
+			 */
+			bool Pipe_IsEndpointBound(uint8_t EndpointAddress);
 		
 			/** Reads and discards the given number of bytes from the pipe, discarding fully read packets from the host
 			 *  as needed. The last packet is not automatically discarded once the remaining bytes has been read; the
diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index eb6c89ebf..f96168f37 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -33,6 +33,7 @@
   *  - Added new Endpoint_ClearStatusStage() convenience function to assist with the status stages of control transfers
   *  - Added new Benito Arduino Programmer project
   *  - Added new LEDs_ToggleLEDs() function to the LEDs driver
+  *  - Added new Pipe_BoundEndpointNumber() and Pipe_IsEndpointBound() functions
   *  
   *  <b>Changed:</b>
   *  - Deprecated psuedo-scheduler and removed dynamic memory allocator from the library (first no longer needed and second unused)
-- 
GitLab