Update copyright year to 2010.
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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.
20
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
28 this software.
29 */
30
31 #include "ConfigDescriptor.h"
32
33 uint8_t ProcessConfigurationDescriptor(void)
34 {
35 uint8_t ConfigDescriptorData[512];
36 void* CurrConfigLocation = ConfigDescriptorData;
37 uint16_t CurrConfigBytesRem;
38 uint8_t FoundEndpoints = 0;
39
40 /* Retrieve the entire configuration descriptor into the allocated buffer */
41 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
42 {
43 case HOST_GETCONFIG_Successful:
44 break;
45 case HOST_GETCONFIG_InvalidData:
46 return InvalidConfigDataReturned;
47 case HOST_GETCONFIG_BuffOverflow:
48 return DescriptorTooLarge;
49 default:
50 return ControlErrorDuringConfigRead;
51 }
52
53 /* The bluetooth USB transport addendum mandates that the data (not streaming voice) endpoints
54 be in the first interface descriptor (interface 0) */
55 USB_GetNextDescriptorOfType(&CurrConfigBytesRem, &CurrConfigLocation, DTYPE_Interface);
56
57 /* Ensure that an interface was found, and the end of the descriptor was not reached */
58 if (!(CurrConfigBytesRem))
59 return NoInterfaceFound;
60
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)))
64 {
65 /* Fetch the next endpoint from the current bluetooth interface */
66 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
67 NextInterfaceBluetoothDataEndpoint))
68 {
69 /* Descriptor not found, error out */
70 return NoEndpointFound;
71 }
72
73 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
74
75 /* Check if the endpoint is a bulk or interrupt type endpoint */
76 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
77 {
78 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
79 {
80 /* Configure the events IN pipe */
81 Pipe_ConfigurePipe(BLUETOOTH_EVENTS_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
82 EndpointData->EndpointAddress, EndpointData->EndpointSize,
83 PIPE_BANK_SINGLE);
84
85 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
86
87 /* Set the flag indicating that the events notification pipe has been found */
88 FoundEndpoints |= (1 << BLUETOOTH_EVENTS_PIPE);
89 }
90 }
91 else
92 {
93 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
94 {
95 /* Configure the data IN pipe */
96 Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
97 EndpointData->EndpointAddress, EndpointData->EndpointSize,
98 PIPE_BANK_SINGLE);
99
100 /* Set the flag indicating that the data IN pipe has been found */
101 FoundEndpoints |= (1 << BLUETOOTH_DATA_IN_PIPE);
102 }
103 else
104 {
105 /* Configure the data OUT pipe */
106 Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
107 EndpointData->EndpointAddress, EndpointData->EndpointSize,
108 PIPE_BANK_SINGLE);
109
110 /* Set the flag indicating that the data OUT pipe has been found */
111 FoundEndpoints |= (1 << BLUETOOTH_DATA_OUT_PIPE);
112 }
113 }
114
115 }
116
117 /* Valid data found, return success */
118 return SuccessfulConfigRead;
119 }
120
121 uint8_t NextInterfaceBluetoothDataEndpoint(void* CurrentDescriptor)
122 {
123 /* PURPOSE: Find next interface endpoint descriptor before next interface descriptor */
124
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;
129
130 return DESCRIPTOR_SEARCH_NotFound;
131 }
132