Changed stream wait timeout counter to be 16-bit, so that very long timeout periods...
[pub/USBasp.git] / Demos / BluetoothHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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;
36 uint16_t ConfigDescriptorSize;
37 uint8_t FoundEndpoints = 0;
38
39 /* Get Configuration Descriptor size from the device */
40 if (USB_Host_GetDeviceConfigDescriptor(&ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
41 return ControlErrorDuringConfigRead;
42
43 /* Ensure that the Configuration Descriptor isn't too large */
44 if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
45 return DescriptorTooLarge;
46
47 /* Allocate enough memory for the entire config descriptor */
48 ConfigDescriptorData = alloca(ConfigDescriptorSize);
49
50 /* Retrieve the entire configuration descriptor into the allocated buffer */
51 USB_Host_GetDeviceConfigDescriptor(&ConfigDescriptorSize, ConfigDescriptorData);
52
53 /* Validate returned data - ensure first entry is a configuration header descriptor */
54 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
55 return InvalidConfigDataReturned;
56
57 /* The bluetooth USB transport addendium mandates that the data (not streaming voice) endpoints
58 be in the first interface descriptor (interface 0) */
59 USB_Host_GetNextDescriptorOfType(&ConfigDescriptorSize, &ConfigDescriptorData, DTYPE_Interface);
60
61 /* Ensure that an interface was found, and the end of the descriptor was not reached */
62 if (!(ConfigDescriptorSize))
63 return NoInterfaceFound;
64
65 /* Get the data IN, data OUT and event notification endpoints for the bluetooth interface */
66 while (FoundEndpoints != ((1 << BLUETOOTH_DATA_IN_PIPE) | (1 << BLUETOOTH_DATA_OUT_PIPE) |
67 (1 << BLUETOOTH_EVENTS_PIPE)))
68 {
69 /* Fetch the next endpoint from the current bluetooth interface */
70 if (USB_Host_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
71 NextInterfaceBluetoothDataEndpoint))
72 {
73 /* Descriptor not found, error out */
74 return NoEndpointFound;
75 }
76
77 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
78
79 /* Check if the endpoint is a bulk or interrupt type endpoint */
80 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
81 {
82 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
83 {
84 /* Configure the events IN pipe */
85 Pipe_ConfigurePipe(BLUETOOTH_EVENTS_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
86 EndpointData->EndpointAddress, EndpointData->EndpointSize,
87 PIPE_BANK_SINGLE);
88
89 Pipe_SetInfiniteINRequests();
90 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
91
92 /* Set the flag indicating that the events notification pipe has been found */
93 FoundEndpoints |= (1 << BLUETOOTH_EVENTS_PIPE);
94 }
95 }
96 else
97 {
98 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
99 {
100 /* Configure the data IN pipe */
101 Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
102 EndpointData->EndpointAddress, EndpointData->EndpointSize,
103 PIPE_BANK_SINGLE);
104
105 Pipe_SetInfiniteINRequests();
106
107 /* Set the flag indicating that the data IN pipe has been found */
108 FoundEndpoints |= (1 << BLUETOOTH_DATA_IN_PIPE);
109 }
110 else
111 {
112 /* Configure the data OUT pipe */
113 Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
114 EndpointData->EndpointAddress, EndpointData->EndpointSize,
115 PIPE_BANK_SINGLE);
116
117 /* Set the flag indicating that the data OUT pipe has been found */
118 FoundEndpoints |= (1 << BLUETOOTH_DATA_OUT_PIPE);
119 }
120 }
121
122 }
123
124 /* Valid data found, return success */
125 return SuccessfulConfigRead;
126 }
127
128 DESCRIPTOR_COMPARATOR(NextInterfaceBluetoothDataEndpoint)
129 {
130 /* PURPOSE: Find next interface endpoint descriptor before next interface descriptor */
131
132 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
133 return Descriptor_Search_Found;
134 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
135 return Descriptor_Search_Fail;
136
137 return Descriptor_Search_NotFound;
138 }
139