From f3c061d5c8ef95a0bd2ec829cc1d7461af94a845 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Mon, 20 Jul 2009 10:19:13 +0000
Subject: [PATCH] Fixed PrinterHost demo Printer_GetDeviceID() routine not
 removing the Device ID string length from the start of the returned array
 (thanks to John Andrews).

Fixed error in new pipe stream function template system not setting the right device token for R/W operations (also thanks to John Andrews).
---
 .../Incomplete/PrinterHost/Lib/PrinterCommands.c   | 11 +++++++----
 .../Incomplete/PrinterHost/Lib/PrinterCommands.h   |  2 +-
 Demos/Host/Incomplete/PrinterHost/PrinterHost.c    |  6 +++---
 LUFA/Drivers/USB/LowLevel/HostChapter9.c           | 14 +++++++-------
 LUFA/Drivers/USB/LowLevel/Pipe.c                   | 10 ++++++++++
 .../USB/LowLevel/Template/Template_Pipe_RW.c       |  5 +++--
 6 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c b/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c
index 87651e906..4e2a0d9db 100644
--- a/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c
+++ b/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.c
@@ -63,7 +63,7 @@ uint8_t Printer_SendData(Printer_Data_t* PrinterCommands)
  *
  *  \return A value from the USB_Host_SendControlErrorCodes_t enum
  */
-uint8_t Printer_GetDeviceID(char* DeviceIDString, uint8_t BufferSize)
+uint8_t Printer_GetDeviceID(char* DeviceIDString, uint16_t BufferSize)
 {
 	uint8_t  ErrorCode = HOST_SENDCONTROL_Successful;
 	uint16_t DeviceIDStringLength;
@@ -85,12 +85,15 @@ uint8_t Printer_GetDeviceID(char* DeviceIDString, uint8_t BufferSize)
 	if (DeviceIDStringLength > BufferSize)
 	  DeviceIDStringLength = BufferSize;
 
-	USB_ControlRequest.wLength = (DeviceIDStringLength - 1);
+	USB_ControlRequest.wLength = DeviceIDStringLength;
 	
 	if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
 	  return ErrorCode;
-	
-	DeviceIDString[DeviceIDStringLength] = 0x00;
+	  
+	/* Move string back two characters to remove the string length value from the start of the array */
+	memmove(&DeviceIDString[0], &DeviceIDString[2], DeviceIDStringLength - 2);
+
+	DeviceIDString[DeviceIDStringLength - 2] = 0x00;
 	
 	return HOST_SENDCONTROL_Successful;
 }
diff --git a/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.h b/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.h
index 9ba6d4d5d..4656d190e 100644
--- a/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.h
+++ b/Demos/Host/Incomplete/PrinterHost/Lib/PrinterCommands.h
@@ -62,7 +62,7 @@
 		
 	/* Function Prototypes: */
 		uint8_t Printer_SendData(Printer_Data_t* PrinterCommands);
-		uint8_t Printer_GetDeviceID(char* DeviceIDString, uint8_t BufferSize);
+		uint8_t Printer_GetDeviceID(char* DeviceIDString, uint16_t BufferSize);
 		uint8_t Printer_GetPortStatus(uint8_t* PortStatus);
 		uint8_t Printer_SoftReset(void);
 	
diff --git a/Demos/Host/Incomplete/PrinterHost/PrinterHost.c b/Demos/Host/Incomplete/PrinterHost/PrinterHost.c
index d9f409e97..79e6f9ee8 100644
--- a/Demos/Host/Incomplete/PrinterHost/PrinterHost.c
+++ b/Demos/Host/Incomplete/PrinterHost/PrinterHost.c
@@ -180,7 +180,7 @@ void USB_Printer_Host(void)
 		case HOST_STATE_Configured:
 			puts_P(PSTR("Retrieving Device ID...\r\n"));
 		
-			char DeviceIDString[128];
+			char DeviceIDString[256];
 			if ((ErrorCode = Printer_GetDeviceID(DeviceIDString, sizeof(DeviceIDString))) != HOST_SENDCONTROL_Successful)
 			{
 				puts_P(PSTR(ESC_FG_RED "Control Error (Get DeviceID).\r\n"));
@@ -206,8 +206,8 @@ void USB_Printer_Host(void)
 		
 			Printer_Data_t TestPageData =
 				{
-					"\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X",
-//					"\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n",
+//					"\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X",
+					"\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n",
 					(sizeof(TestPageData.Data) - 1)
 				};
 		
diff --git a/LUFA/Drivers/USB/LowLevel/HostChapter9.c b/LUFA/Drivers/USB/LowLevel/HostChapter9.c
index a271c06cb..42e3262ff 100644
--- a/LUFA/Drivers/USB/LowLevel/HostChapter9.c
+++ b/LUFA/Drivers/USB/LowLevel/HostChapter9.c
@@ -58,7 +58,7 @@ uint8_t USB_Host_SendControlRequest(void* BufferPtr)
 
 	Pipe_ClearSETUP();
 	
-	if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)))
+	if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful)
 	  goto End_Of_Control_Send;
 
 	Pipe_Freeze();
@@ -76,7 +76,7 @@ uint8_t USB_Host_SendControlRequest(void* BufferPtr)
 			{
 				Pipe_Unfreeze();
 
-				if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)))
+				if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
 				  goto End_Of_Control_Send;
 							
 				if (!(Pipe_BytesInPipe()))
@@ -96,12 +96,12 @@ uint8_t USB_Host_SendControlRequest(void* BufferPtr)
 		Pipe_SetToken(PIPE_TOKEN_OUT);
 		Pipe_Unfreeze();
 		
-		if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)))
+		if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
 		  goto End_Of_Control_Send;
 
 		Pipe_ClearOUT();
 
-		if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)))
+		if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
 		  goto End_Of_Control_Send;
 	}
 	else
@@ -113,7 +113,7 @@ uint8_t USB_Host_SendControlRequest(void* BufferPtr)
 
 			while (DataLen)
 			{
-				if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)))
+				if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
 				  goto End_Of_Control_Send;
 
 				while (DataLen && (Pipe_BytesInPipe() < USB_ControlPipeSize))
@@ -125,7 +125,7 @@ uint8_t USB_Host_SendControlRequest(void* BufferPtr)
 				Pipe_ClearOUT();
 			}
 
-			if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)))
+			if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
 			  goto End_Of_Control_Send;
 
 			Pipe_Freeze();
@@ -134,7 +134,7 @@ uint8_t USB_Host_SendControlRequest(void* BufferPtr)
 		Pipe_SetToken(PIPE_TOKEN_IN);
 		Pipe_Unfreeze();
 
-		if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)))
+		if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
 		  goto End_Of_Control_Send;
 
 		Pipe_ClearIN();
diff --git a/LUFA/Drivers/USB/LowLevel/Pipe.c b/LUFA/Drivers/USB/LowLevel/Pipe.c
index 18fbc19b5..3fb30a2f7 100644
--- a/LUFA/Drivers/USB/LowLevel/Pipe.c
+++ b/LUFA/Drivers/USB/LowLevel/Pipe.c
@@ -184,60 +184,70 @@ uint8_t Pipe_Discard_Stream(uint16_t Length
 }
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_Stream_LE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(*(BufferPtr++))
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_PStream_LE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(pgm_read_byte(BufferPtr++))
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_EStream_LE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(eeprom_read_byte(BufferPtr++))
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_Stream_BE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            Length - 1
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(*(BufferPtr--))
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_PStream_BE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            Length - 1
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(pgm_read_byte(BufferPtr--))
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_EStream_BE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            Length - 1
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(eeprom_read_byte(BufferPtr--))
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_Stream_LE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *(BufferPtr++) = Pipe_Read_Byte()
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_EStream_LE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_write_byte(BufferPtr++, Pipe_Read_Byte())
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_Stream_BE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            Length - 1
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *(BufferPtr--) = Pipe_Read_Byte()
 #include "Template/Template_Pipe_RW.c"
 
 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_EStream_BE
+#define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN
 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN()
 #define  TEMPLATE_BUFFER_OFFSET(Length)            Length - 1
 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_write_byte(BufferPtr--, Pipe_Read_Byte())
diff --git a/LUFA/Drivers/USB/LowLevel/Template/Template_Pipe_RW.c b/LUFA/Drivers/USB/LowLevel/Template/Template_Pipe_RW.c
index f36f82da5..4250d613b 100644
--- a/LUFA/Drivers/USB/LowLevel/Template/Template_Pipe_RW.c
+++ b/LUFA/Drivers/USB/LowLevel/Template/Template_Pipe_RW.c
@@ -3,7 +3,7 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length _CALLBACK_PARAM)
 	uint8_t* DataStream = (uint8_t*)(Buffer + TEMPLATE_BUFFER_OFFSET(Length));
 	uint8_t  ErrorCode;
 	
-	Pipe_SetToken(PIPE_TOKEN_IN);
+	Pipe_SetToken(TEMPLATE_TOKEN);
 
 	if ((ErrorCode = Pipe_WaitUntilReady()))
 	  return ErrorCode;
@@ -73,6 +73,7 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length _CALLBACK_PARAM)
 }
 
 #undef TEMPLATE_FUNC_NAME
+#undef TEMPLATE_TOKEN
 #undef TEMPLATE_TRANSFER_BYTE
 #undef TEMPLATE_CLEAR_PIPE
-#undef TEMPLATE_BUFFER_OFFSET
\ No newline at end of file
+#undef TEMPLATE_BUFFER_OFFSET
-- 
GitLab