3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  31 #include "ConfigDescriptor.h" 
  33 uint8_t ProcessConfigurationDescriptor(void) 
  35         uint8_t  ConfigDescriptorData
[512]; 
  36         uint8_t* CurrConfigLocation 
= ConfigDescriptorData
; 
  37         uint16_t CurrConfigBytesRem
; 
  38         uint8_t  FoundEndpoints 
= 0; 
  40         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  41         switch (USB_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  43                 case HOST_GETCONFIG_Successful
: 
  45                 case HOST_GETCONFIG_InvalidData
: 
  46                         return InvalidConfigDataReturned
; 
  47                 case HOST_GETCONFIG_BuffOverflow
: 
  48                         return DescriptorTooLarge
; 
  50                         return ControlErrorDuringConfigRead
; 
  53         /* The bluetooth USB transport addendium mandates that the data (not streaming voice) endpoints 
  54            be in the first interface descriptor (interface 0) */ 
  55         USB_GetNextDescriptorOfType(&CurrConfigBytesRem
, &CurrConfigLocation
, DTYPE_Interface
); 
  57         /* Ensure that an interface was found, and the end of the descriptor was not reached */ 
  58         if (!(CurrConfigBytesRem
)) 
  59           return NoInterfaceFound
; 
  61         /* Get the data IN, data OUT and event notification endpoints for the bluetooth interface */ 
  62         while (FoundEndpoints 
!= ((1 << BLUETOOTH_DATA_IN_PIPE
) | (1 << BLUETOOTH_DATA_OUT_PIPE
) | 
  63                                   (1 << BLUETOOTH_EVENTS_PIPE
))) 
  65                 /* Fetch the next endpoint from the current bluetooth interface */ 
  66                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  67                                               NextInterfaceBluetoothDataEndpoint
)) 
  69                         /* Descriptor not found, error out */ 
  70                         return NoEndpointFound
; 
  73                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
); 
  75                 /* Check if the endpoint is a bulk or interrupt type endpoint */ 
  76                 if ((EndpointData
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
) 
  78                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
  80                                 /* Configure the events IN pipe */ 
  81                                 Pipe_ConfigurePipe(BLUETOOTH_EVENTS_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
  82                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
  85                                 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
); 
  87                                 /* Set the flag indicating that the events notification pipe has been found */ 
  88                                 FoundEndpoints 
|= (1 << BLUETOOTH_EVENTS_PIPE
);  
  93                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
  95                                 /* Configure the data IN pipe */ 
  96                                 Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
  97                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
 100                                 /* Set the flag indicating that the data IN pipe has been found */ 
 101                                 FoundEndpoints 
|= (1 << BLUETOOTH_DATA_IN_PIPE
); 
 105                                 /* Configure the data OUT pipe */ 
 106                                 Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 107                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
 110                                 /* Set the flag indicating that the data OUT pipe has been found */ 
 111                                 FoundEndpoints 
|= (1 << BLUETOOTH_DATA_OUT_PIPE
); 
 117         /* Valid data found, return success */ 
 118         return SuccessfulConfigRead
; 
 121 uint8_t NextInterfaceBluetoothDataEndpoint(void* CurrentDescriptor
) 
 123         /* PURPOSE: Find next interface endpoint descriptor before next interface descriptor */ 
 125         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 126           return DESCRIPTOR_SEARCH_Found
; 
 127         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 128           return DESCRIPTOR_SEARCH_Fail
; 
 130         return DESCRIPTOR_SEARCH_NotFound
;