Changed the parameters and behaviour of the USB_GetDeviceConfigDescriptor() function so that it now performs size checks and data validations internally, to simplify user code.

This commit is contained in:
Dean Camera 2009-08-31 07:48:32 +00:00
parent 813e6f0318
commit 7fbb759287
19 changed files with 232 additions and 254 deletions

View file

@ -36,32 +36,26 @@ uint8_t PrinterAltSetting;
uint8_t ProcessConfigurationDescriptor(void)
{
uint8_t* ConfigDescriptorData;
uint16_t ConfigDescriptorSize;
uint8_t ErrorCode;
uint8_t ConfigDescriptorData[512];
uint8_t* CurrConfigLocation = ConfigDescriptorData;
uint16_t CurrConfigBytesRem;
uint8_t FoundEndpoints = 0;
/* Get Configuration Descriptor size from the device */
if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
return ControlError;
/* Ensure that the Configuration Descriptor isn't too large */
if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
return DescriptorTooLarge;
/* Allocate enough memory for the entire config descriptor */
ConfigDescriptorData = alloca(ConfigDescriptorSize);
/* Retrieve the entire configuration descriptor into the allocated buffer */
USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
/* Validate returned data - ensure first entry is a configuration header descriptor */
if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
return InvalidConfigDataReturned;
switch (USB_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
{
case HOST_GETCONFIG_Successful:
break;
case HOST_GETCONFIG_InvalidData:
return InvalidConfigDataReturned;
case HOST_GETCONFIG_BuffOverflow:
return DescriptorTooLarge;
default:
return ControlError;
}
/* Get the printer interface from the configuration descriptor */
if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
DComp_NextBidirectionalPrinterInterface)))
if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, DComp_NextBidirectionalPrinterInterface))
{
/* Descriptor not found, error out */
return NoInterfaceFound;
@ -74,8 +68,7 @@ uint8_t ProcessConfigurationDescriptor(void)
while (FoundEndpoints != ((1 << PRINTER_DATA_OUT_PIPE) | (1 << PRINTER_DATA_IN_PIPE)))
{
/* Fetch the next bulk endpoint from the current printer interface */
if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
DComp_NextInterfaceBulkDataEndpoint)))
if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation, DComp_NextInterfaceBulkDataEndpoint))
{
/* Descriptor not found, error out */
return NoEndpointFound;