948c4fcc65df04fd826002bb0a9cc0e8b65cfcdd
[pub/USBasp.git] / Demos / Host / Incomplete / 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_GetDeviceConfigDescriptor(1, &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_GetDeviceConfigDescriptor(1, &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_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_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_SetInterruptPeriod(EndpointData->PollingIntervalMS);
90
91 /* Set the flag indicating that the events notification pipe has been found */
92 FoundEndpoints |= (1 << BLUETOOTH_EVENTS_PIPE);
93 }
94 }
95 else
96 {
97 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
98 {
99 /* Configure the data IN pipe */
100 Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
101 EndpointData->EndpointAddress, EndpointData->EndpointSize,
102 PIPE_BANK_SINGLE);
103
104 /* Set the flag indicating that the data IN pipe has been found */
105 FoundEndpoints |= (1 << BLUETOOTH_DATA_IN_PIPE);
106 }
107 else
108 {
109 /* Configure the data OUT pipe */
110 Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
111 EndpointData->EndpointAddress, EndpointData->EndpointSize,
112 PIPE_BANK_SINGLE);
113
114 /* Set the flag indicating that the data OUT pipe has been found */
115 FoundEndpoints |= (1 << BLUETOOTH_DATA_OUT_PIPE);
116 }
117 }
118
119 }
120
121 /* Valid data found, return success */
122 return SuccessfulConfigRead;
123 }
124
125 uint8_t NextInterfaceBluetoothDataEndpoint(void* CurrentDescriptor)
126 {
127 /* PURPOSE: Find next interface endpoint descriptor before next interface descriptor */
128
129 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
130 return DESCRIPTOR_SEARCH_Found;
131 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
132 return DESCRIPTOR_SEARCH_Fail;
133
134 return DESCRIPTOR_SEARCH_NotFound;
135 }
136