Add explicit guards to all device mode tasks to ensure the device is connected and configured before running the task, to prevent any user tasks from locking up the main USB task if the device has not been properly configured.

pull/1469/head
Dean Camera 15 years ago
parent bf50959b80
commit 67f390fe74

@ -141,9 +141,10 @@ void EVENT_USB_UnhandledControlPacket(void)
void SideShow_Task(void)
{
/* Check if the USB System is connected to a Host */
if (USB_IsConnected)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Select the SideShow data out endpoint */
Endpoint_SelectEndpoint(SIDESHOW_OUT_EPNUM);
@ -154,4 +155,3 @@ void SideShow_Task(void)
Sideshow_ProcessCommandPacket();
}
}
}

@ -151,6 +151,10 @@ void EVENT_USB_UnhandledControlPacket(void)
/** Task to manage the Audio interface, reading in ADC samples from the microphone, and them to the host. */
void USB_Audio_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Check to see if the streaming interface is selected, if not the host is not receiving audio */
if (!(StreamingAudioInterfaceSelected))
return;

@ -180,6 +180,10 @@ void EVENT_USB_UnhandledControlPacket(void)
*/
void USB_Audio_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Check to see if the streaming interface is selected, if not the host is not receiving audio */
if (!(StreamingAudioInterfaceSelected))
return;

@ -254,6 +254,10 @@ void CDC_Task(void)
"Joystick Pressed\r\n",
};
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
#if 0
/* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232
* handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:

@ -229,6 +229,10 @@ void CDC1_Task(void)
uint8_t JoyStatus_LCL = Joystick_GetStatus();
static bool ActionSent = false;
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
char* JoystickStrings[] =
{
"Joystick Up\r\n",
@ -288,6 +292,10 @@ void CDC1_Task(void)
*/
void CDC2_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Select the Serial Rx Endpoint */
Endpoint_SelectEndpoint(CDC2_RX_EPNUM);

@ -202,9 +202,10 @@ void CreateGenericHIDReport(uint8_t* DataArray)
void HID_Task(void)
{
/* Check if the USB system is connected to a host */
if (USB_IsConnected)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
/* Check to see if a packet has been sent from the host */
@ -245,4 +246,3 @@ void HID_Task(void)
Endpoint_ClearIN();
}
}
}

@ -181,9 +181,10 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData)
/** Function to manage HID report generation and transmission to the host. */
void HID_Task(void)
{
/* Check if the USB System is connected to a Host */
if (USB_IsConnected)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Select the Joystick Report Endpoint */
Endpoint_SelectEndpoint(JOYSTICK_EPNUM);
@ -205,4 +206,3 @@ void HID_Task(void)
memset(&JoystickReportData, 0, sizeof(JoystickReportData));
}
}
}

@ -377,13 +377,13 @@ void ReceiveNextReport(void)
/** Function to manage HID report generation and transmission to the host, when in report mode. */
void HID_Task(void)
{
/* Check if the USB system is connected to a host */
if (USB_IsConnected)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Send the next keypress report to the host */
SendNextReport();
/* Process the LED report sent from the host */
ReceiveNextReport();
}
}

@ -212,6 +212,10 @@ void Keyboard_HID_Task(void)
{
uint8_t JoyStatus_LCL = Joystick_GetStatus();
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Check if board button is not pressed, if so mouse mode enabled */
if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
{
@ -229,9 +233,6 @@ void Keyboard_HID_Task(void)
KeyboardReportData.KeyCode[0] = 0x08; // E
}
/* Check if the USB system is connected to a host and report protocol mode is enabled */
if (USB_IsConnected)
{
/* Select the Keyboard Report Endpoint */
Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);
@ -274,7 +275,6 @@ void Keyboard_HID_Task(void)
Endpoint_ClearOUT();
}
}
}
/** Mouse task. This generates the next mouse HID report for the host, and transmits it via the
* mouse IN endpoint when the host is ready for more data.
@ -283,6 +283,10 @@ void Mouse_HID_Task(void)
{
uint8_t JoyStatus_LCL = Joystick_GetStatus();
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Check if board button is pressed, if so mouse mode enabled */
if (Buttons_GetStatus() & BUTTONS_BUTTON1)
{
@ -300,9 +304,6 @@ void Mouse_HID_Task(void)
MouseReportData.Button = (1 << 0);
}
/* Check if the USB system is connected to a host and report protocol mode is enabled */
if (USB_IsConnected)
{
/* Select the Mouse Report Endpoint */
Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
@ -319,4 +320,3 @@ void Mouse_HID_Task(void)
memset(&MouseReportData, 0, sizeof(MouseReportData));
}
}
}

@ -116,16 +116,17 @@ void MIDI_Task(void)
{
static uint8_t PrevJoystickStatus;
/* Select the MIDI IN stream */
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);
/* Check if endpoint is ready to be written to */
if (Endpoint_IsINReady())
{
uint8_t MIDICommand = 0;
uint8_t MIDIPitch;
/* Get current joystick mask, XOR with previous to detect joystick changes */
uint8_t JoystickStatus = Joystick_GetStatus();
uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus);

@ -313,10 +313,10 @@ void SendNextReport(void)
/** Task to manage HID report generation and transmission to the host, when in report mode. */
void Mouse_Task(void)
{
/* Check if the USB system is connected to a host */
if (USB_IsConnected)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Send the next mouse report to the host */
SendNextReport();
}
}

@ -284,6 +284,10 @@ void Ethernet_Task(void)
outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single
Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
/* Check if a frame has been written to the IN frame buffer */
if (FrameIN.FrameInBuffer)
{

@ -204,8 +204,10 @@ void EVENT_USB_UnhandledControlPacket(void)
/** Task to manage CDC data transmission and reception to and from the host, from and to the physical USART. */
void CDC_Task(void)
{
if (USB_IsConnected)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
#if 0
/* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232
handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code:
@ -289,7 +291,6 @@ void CDC_Task(void)
}
}
}
}
/** ISR to handle the USART receive complete interrupt, fired each time the USART has received a character. This stores the received
* character into the Tx_Buffer circular buffer for later transmission to the host.

@ -62,7 +62,7 @@ int main(void)
{
SetupHardware();
puts_P(PSTR(ESC_FG_CYAN "CDC Host Demo running.\r\n"));
puts_P(PSTR(ESC_FG_CYAN "CDC Host Demo running.\r\n" ESC_FG_WHITE));
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
@ -72,7 +72,10 @@ int main(void)
{
case HOST_STATE_Addressed:
if (!(CDC_Host_ConfigurePipes(&VirtualSerial_CDC_Interface)))
{
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
USB_HostState = HOST_STATE_WaitForDeviceRemoval;
}
USB_HostState = HOST_STATE_Configured;
break;
@ -135,8 +138,8 @@ void EVENT_USB_HostError(const uint8_t ErrorCode)
{
USB_ShutDown();
puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n" ESC_FG_WHITE));
printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"));
printf_P(PSTR(" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
for(;;);

@ -226,7 +226,7 @@ void USB_Printer_Host(void)
Printer_Data_t TestPageData =
{
"\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X",
"\033%-12345X\033E" "LUFA PCL Test Page" "\033E\033%-12345X",
(sizeof(TestPageData.Data) - 1)
};

@ -114,7 +114,7 @@ bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
{
if (!(USB_IsConnected))
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);

@ -152,7 +152,7 @@ bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfac
void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
{
if (!(USB_IsConnected))
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);

@ -96,7 +96,7 @@ bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceIn
void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
{
if (!(USB_IsConnected))
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);

@ -138,7 +138,7 @@ bool RNDIS_Device_ConfigureEndpoints(USB_ClassInfo_RNDIS_Device_t* const RNDISIn
void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
{
if (!(USB_IsConnected))
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
return;
RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->State.RNDISMessageBuffer;

Loading…
Cancel
Save