USBDescriptors.c
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------- */
2 /* Atmel Microcontroller Software Support */
3 /* SAM Software Package License */
4 /* ---------------------------------------------------------------------------- */
5 /* Copyright (c) 2015, Atmel Corporation */
6 /* */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without */
10 /* modification, are permitted provided that the following condition is met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, */
13 /* this list of conditions and the disclaimer below. */
14 /* */
15 /* Atmel's name may not be used to endorse or promote products derived from */
16 /* this software without specific prior written permission. */
17 /* */
18 /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */
19 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
20 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */
21 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */
22 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
23 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
24 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
25 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
26 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
27 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
28 /* ---------------------------------------------------------------------------- */
29 
39 /*------------------------------------------------------------------------------
40  * Headers
41  *------------------------------------------------------------------------------*/
42 
43 #include "USBDescriptors.h"
44 
45 /*------------------------------------------------------------------------------
46  * Exported functions
47  *------------------------------------------------------------------------------*/
48 
55  const USBGenericDescriptor *descriptor)
56 {
57  return descriptor->bLength;
58 }
59 
66  const USBGenericDescriptor *descriptor)
67 {
68  return descriptor->bDescriptorType;
69 }
70 
78  const USBGenericDescriptor *descriptor)
79 {
80  return (USBGenericDescriptor *)
81  (((char *) descriptor) + USBGenericDescriptor_GetLength(descriptor));
82 }
83 
92 USBGenericDescriptor *USBGenericDescriptor_Parse(
93  const USBGenericDescriptor *descriptor,
94  uint32_t totalLength,
95  USBDescriptorParseFunction parseFunction,
96  void *parseArg)
97 {
98  int32_t size = totalLength;
99 
100  if (size == 0)
101  return 0;
102 
103  /* Start parsing descriptors */
104  while (1) {
105  uint32_t parseRC = 0;
106 
107  /* Parse current descriptor */
108  if (parseFunction)
109  parseRC = parseFunction((void *)descriptor, parseArg);
110 
111  /* Get next descriptor */
112  size -= USBGenericDescriptor_GetLength(descriptor);
113  descriptor = USBGenericDescriptor_GetNextDescriptor(descriptor);
114 
115  if (size) {
116  if (parseRC != 0)
117 
118  return (USBGenericDescriptor *)descriptor;
119  } else
120  break;
121  }
122 
123  /* No descriptors remaining */
124  return 0;
125 }
126 
127 
134  const USBEndpointDescriptor *endpoint)
135 {
136  return endpoint->bEndpointAddress & 0xF;
137 }
138 
145  const USBEndpointDescriptor *endpoint)
146 {
147  if ((endpoint->bEndpointAddress & 0x80) != 0)
148 
150  else
151 
153 }
154 
161  const USBEndpointDescriptor *endpoint)
162 {
163  return endpoint->bmAttributes & 0x3;
164 }
165 
173  const USBEndpointDescriptor *endpoint)
174 {
175  return endpoint->wMaxPacketSize;
176 }
177 
184  const USBEndpointDescriptor *endpoint)
185 {
186  return endpoint->bInterval;
187 }
188 
189 
190 
197  const USBConfigurationDescriptor *configuration)
198 {
199  return configuration->wTotalLength;
200 }
201 
207  const USBConfigurationDescriptor *configuration)
208 {
209  return configuration->bNumInterfaces;
210 }
211 
218  const USBConfigurationDescriptor *configuration)
219 {
220  if ((configuration->bmAttributes & (1 << 6)) != 0)
221 
222  return 1;
223  else
224 
225  return 0;
226 }
227 
243  const USBConfigurationDescriptor *configuration,
244  USBInterfaceDescriptor **interfaces,
245  USBEndpointDescriptor **endpoints,
246  USBGenericDescriptor **others)
247 {
248  /* Get size of configuration to parse */
249  int size = USBConfigurationDescriptor_GetTotalLength(configuration);
250  size -= sizeof(USBConfigurationDescriptor);
251 
252  /* Start parsing descriptors */
253  USBGenericDescriptor *descriptor = (USBGenericDescriptor *) configuration;
254 
255  while (size > 0) {
256  /* Get next descriptor */
257  descriptor = USBGenericDescriptor_GetNextDescriptor(descriptor);
258  size -= USBGenericDescriptor_GetLength(descriptor);
259 
260  /* Store descriptor in corresponding array */
261  if (USBGenericDescriptor_GetType(descriptor)
263 
264  if (interfaces) {
265  *interfaces = (USBInterfaceDescriptor *) descriptor;
266  interfaces++;
267  }
268  } else if (USBGenericDescriptor_GetType(descriptor)
270 
271  if (endpoints) {
272  *endpoints = (USBEndpointDescriptor *) descriptor;
273  endpoints++;
274  }
275  } else if (others) {
276  *others = descriptor;
277  others++;
278  }
279  }
280 
281  /* Null-terminate arrays */
282  if (interfaces)
283  *interfaces = 0;
284 
285  if (endpoints)
286  *endpoints = 0;
287 
288  if (others)
289  *others = 0;
290 }
291 
#define USBGenericDescriptor_ENDPOINT
uint8_t USBGenericDescriptor_GetType(const USBGenericDescriptor *descriptor)
#define USBEndpointDescriptor_OUT
void USBConfigurationDescriptor_Parse(const USBConfigurationDescriptor *configuration, USBInterfaceDescriptor **interfaces, USBEndpointDescriptor **endpoints, USBGenericDescriptor **others)
static Endpoint endpoints[CHIP_USB_NUMENDPOINTS]
Definition: USBD_HAL.c:207
unsigned char USBConfigurationDescriptor_GetNumInterfaces(const USBConfigurationDescriptor *configuration)
uint32_t USBGenericDescriptor_GetLength(const USBGenericDescriptor *descriptor)
uint8_t USBEndpointDescriptor_GetNumber(const USBEndpointDescriptor *endpoint)
uint16_t USBEndpointDescriptor_GetMaxPacketSize(const USBEndpointDescriptor *endpoint)
uint8_t USBEndpointDescriptor_GetType(const USBEndpointDescriptor *endpoint)
uint32_t(* USBDescriptorParseFunction)(void *descriptor, void *parseArg)
USBConfigurationDescriptor configuration
unsigned char USBConfigurationDescriptor_IsSelfPowered(const USBConfigurationDescriptor *configuration)
USBGenericDescriptor * USBGenericDescriptor_GetNextDescriptor(const USBGenericDescriptor *descriptor)
#define USBGenericDescriptor_INTERFACE
uint8_t USBEndpointDescriptor_GetDirection(const USBEndpointDescriptor *endpoint)
#define USBEndpointDescriptor_IN
uint32_t USBConfigurationDescriptor_GetTotalLength(const USBConfigurationDescriptor *configuration)
USBGenericDescriptor * USBGenericDescriptor_Parse(const USBGenericDescriptor *descriptor, uint32_t totalLength, USBDescriptorParseFunction parseFunction, void *parseArg)
uint8_t USBEndpointDescriptor_GetInterval(const USBEndpointDescriptor *endpoint)


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58