Moved all source to the trunk directory.
[pub/lufa.git] / Demos / BluetoothHost / ConfigDescriptor.c
diff --git a/Demos/BluetoothHost/ConfigDescriptor.c b/Demos/BluetoothHost/ConfigDescriptor.c
new file mode 100644 (file)
index 0000000..16ba1b0
--- /dev/null
@@ -0,0 +1,139 @@
+/*\r
+             LUFA Library\r
+     Copyright (C) Dean Camera, 2009.\r
+              \r
+  dean [at] fourwalledcubicle [dot] com\r
+      www.fourwalledcubicle.com\r
+*/\r
+\r
+/*\r
+  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, and distribute this software\r
+  and its documentation for any purpose and without fee is hereby\r
+  granted, provided that the above copyright notice appear in all\r
+  copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting\r
+  documentation, and that the name of the author not be used in\r
+  advertising or publicity pertaining to distribution of the\r
+  software without specific, written prior permission.\r
+\r
+  The author disclaim all warranties with regard to this\r
+  software, including all implied warranties of merchantability\r
+  and fitness.  In no event shall the author be liable for any\r
+  special, indirect or consequential damages or any damages\r
+  whatsoever resulting from loss of use, data or profits, whether\r
+  in an action of contract, negligence or other tortious action,\r
+  arising out of or in connection with the use or performance of\r
+  this software.\r
+*/\r
+\r
+#include "ConfigDescriptor.h"\r
+\r
+uint8_t ProcessConfigurationDescriptor(void)\r
+{\r
+       uint8_t* ConfigDescriptorData;\r
+       uint16_t ConfigDescriptorSize;\r
+       uint8_t  FoundEndpoints = 0;\r
+       \r
+       /* Get Configuration Descriptor size from the device */\r
+       if (USB_Host_GetDeviceConfigDescriptor(&ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)\r
+         return ControlErrorDuringConfigRead;\r
+       \r
+       /* Ensure that the Configuration Descriptor isn't too large */\r
+       if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)\r
+         return DescriptorTooLarge;\r
+         \r
+       /* Allocate enough memory for the entire config descriptor */\r
+       ConfigDescriptorData = alloca(ConfigDescriptorSize);\r
+\r
+       /* Retrieve the entire configuration descriptor into the allocated buffer */\r
+       USB_Host_GetDeviceConfigDescriptor(&ConfigDescriptorSize, ConfigDescriptorData);\r
+       \r
+       /* Validate returned data - ensure first entry is a configuration header descriptor */\r
+       if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)\r
+         return InvalidConfigDataReturned;\r
+\r
+       /* The bluetooth USB transport addendium mandates that the data (not streaming voice) endpoints\r
+          be in the first interface descriptor (interface 0) */\r
+       USB_Host_GetNextDescriptorOfType(&ConfigDescriptorSize, &ConfigDescriptorData, DTYPE_Interface);\r
+       \r
+       /* Ensure that an interface was found, and the end of the descriptor was not reached */\r
+       if (!(ConfigDescriptorSize))\r
+         return NoInterfaceFound;\r
+\r
+       /* Get the data IN, data OUT and event notification endpoints for the bluetooth interface */\r
+       while (FoundEndpoints != ((1 << BLUETOOTH_DATA_IN_PIPE) | (1 << BLUETOOTH_DATA_OUT_PIPE) |\r
+                                 (1 << BLUETOOTH_EVENTS_PIPE)))\r
+       {\r
+               /* Fetch the next endpoint from the current bluetooth interface */\r
+               if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,\r
+                                                  NextInterfaceBluetoothDataEndpoint))\r
+               {\r
+                       /* Descriptor not found, error out */\r
+                       return NoEndpointFound;\r
+               }\r
+               \r
+               USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);\r
+\r
+               /* Check if the endpoint is a bulk or interrupt type endpoint */\r
+               if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)\r
+               {\r
+                       if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)\r
+                       {\r
+                               /* Configure the events IN pipe */\r
+                               Pipe_ConfigurePipe(BLUETOOTH_EVENTS_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,\r
+                                                                  EndpointData->EndpointAddress, EndpointData->EndpointSize,\r
+                                                                  PIPE_BANK_SINGLE);\r
+\r
+                               Pipe_SetInfiniteINRequests();\r
+                               Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);\r
+                               \r
+                               /* Set the flag indicating that the events notification pipe has been found */\r
+                               FoundEndpoints |= (1 << BLUETOOTH_EVENTS_PIPE); \r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)\r
+                       {\r
+                               /* Configure the data IN pipe */\r
+                               Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,\r
+                                                                  EndpointData->EndpointAddress, EndpointData->EndpointSize,\r
+                                                                  PIPE_BANK_SINGLE);\r
+\r
+                               Pipe_SetInfiniteINRequests();\r
+\r
+                               /* Set the flag indicating that the data IN pipe has been found */\r
+                               FoundEndpoints |= (1 << BLUETOOTH_DATA_IN_PIPE);\r
+                       }\r
+                       else\r
+                       {\r
+                               /* Configure the data OUT pipe */\r
+                               Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,\r
+                                                                  EndpointData->EndpointAddress, EndpointData->EndpointSize,\r
+                                                                  PIPE_BANK_SINGLE);\r
+\r
+                               /* Set the flag indicating that the data OUT pipe has been found */\r
+                               FoundEndpoints |= (1 << BLUETOOTH_DATA_OUT_PIPE);\r
+                       }               \r
+               }\r
+\r
+       }\r
+\r
+       /* Valid data found, return success */\r
+       return SuccessfulConfigRead;\r
+}\r
+\r
+DESCRIPTOR_COMPARATOR(NextInterfaceBluetoothDataEndpoint)\r
+{\r
+       /* PURPOSE: Find next interface endpoint descriptor before next interface descriptor */\r
+\r
+       if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)\r
+         return Descriptor_Search_Found;\r
+       else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)\r
+         return Descriptor_Search_Fail;\r
+\r
+       return Descriptor_Search_NotFound;\r
+}\r
+\r