| M480 BSP V3.05.001 The Board Support Package for M480 Series

cdc_parser.c
Go to the documentation of this file.
1 /**************************************************************************/
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "NuMicro.h"
15
16 #include "usb.h"
17 #include "usbh_lib.h"
18 #include "usbh_cdc.h"
19
21
22
23 static int cdc_parse_cs_interface(CDC_DEV_T *cdev, uint8_t *buffer, int size)
24 {
25 DESC_HDR_T *header;
26 CDC_IF_HDR_T *cifd;
27 int parsed = 0;
28
29 while (size > 0)
30 {
31 while(size >= sizeof(DESC_HDR_T))
32 {
33 header = (DESC_HDR_T *)buffer;
34
35 if (header->bLength < 2)
36 {
37 CDC_DBGMSG("Invalid descriptor length of %d\n", header->bLength);
38 return -1;
39 }
40
41 if (header->bDescriptorType != CDC_CS_INTERFACE)
42 return parsed;
43
44 cifd = (CDC_IF_HDR_T *)header;
45
46 CDC_DBGMSG("CS_INTERFACE: 0x%x, ", cifd->bDescriptorSubtype);
47
48 switch (cifd->bDescriptorSubtype)
49 {
50 case CDC_DT_HDR_FUNC:
51 CDC_DBGMSG("Header Functional\n");
52 break;
53 case CDC_DT_CALL_MANAGE:
54 CDC_DBGMSG("Call Management\n");
55 break;
56 case CDC_DT_ABS_CTRL:
57 CDC_DBGMSG("Abstract Control Management\n");
58 break;
59 case CDC_DT_LINE_MANAGE:
60 CDC_DBGMSG("Direct Line Management\n");
61 break;
62 case CDC_DT_TEL_RINGER:
63 CDC_DBGMSG("Telephone Ringer\n");
64 break;
65 case CDC_DT_TEL_OPER_MODES:
66 CDC_DBGMSG("Telephone Operational Modes\n");
67 break;
68 case CDC_DT_CALL_LINE_CAP:
69 CDC_DBGMSG("Telephone Call and Line State Reporting Capabilities\n");
70 break;
71 case CDC_DT_UNION:
72 CDC_DBGMSG("Union Functional\n");
73 if (cifd->bLength >= 5)
74 cdev->ifnum_data = cifd->payload[1];
75 if (cifd->bLength >= 6)
76 {
77 CDC_DBGMSG("Union Functional length %d, not supported!\n", cifd->bLength);
78 }
79 break;
80 case CDC_DT_COUNTRY_SEL:
81 CDC_DBGMSG("Country Selection\n");
82 break;
83 case CDC_DT_USB_TERMINAL:
84 CDC_DBGMSG("USB Terminal\n");
85 break;
86 case CDC_DT_NET_CHANNEL:
87 CDC_DBGMSG("Network Channel Terminal\n");
88 break;
89 case CDC_DT_PROTO_UNIT:
90 CDC_DBGMSG("Protocol Unit\n");
91 break;
92 case CDC_DT_EXTENT_UNIT:
93 CDC_DBGMSG("Extension Unit\n");
94 break;
95 case CDC_DT_MULTI_CHANNEL:
96 CDC_DBGMSG("Multi-Channel Management\n");
97 break;
98 case CDC_DT_CAPI_CTRL:
99 CDC_DBGMSG("CAPI Control Management\n");
100 break;
101 case CDC_DT_ETHERNET_FUNC:
102 CDC_DBGMSG("Ethernet Networking Functional\n");
103 break;
104 case CDC_DT_ATM_FUNC:
105 CDC_DBGMSG("ATM Networking Functional\n");
106 break;
107 }
108
109 buffer += header->bLength;
110 parsed += header->bLength;
111 size -= header->bLength;
112 }
113
114 } /* end of while */
115 return parsed;
116 }
117
118
119 int cdc_config_parser(CDC_DEV_T *cdev)
120 {
121 UDEV_T *udev = cdev->udev;
122 DESC_CONF_T *config;
123 DESC_HDR_T *header;
124 DESC_IF_T *ifd;
125 uint8_t *bptr;
126 int size, result;
127
128 config = (DESC_CONF_T *)udev->cfd_buff;
129 bptr = (uint8_t *)config;
130 bptr += config->bLength;
131 size = config->wTotalLength - config->bLength;
132
133 while (size >= sizeof(DESC_HDR_T))
134 {
135 header = (DESC_HDR_T *)bptr;
136
137 if ((header->bLength > size) || (header->bLength < 2))
138 {
139 CDC_DBGMSG("Error - invalid descriptor length of %d\n", header->bLength);
140 return USBH_ERR_NOT_SUPPORTED;
141 }
142
143 /*
144 * Is the interface descriptor of this CDC device?
145 */
146 if (header->bDescriptorType == USB_DT_INTERFACE)
147 {
148 ifd = (DESC_IF_T *)header;
149 if (ifd->bInterfaceNumber == cdev->iface_cdc->if_num)
150 {
151 bptr += header->bLength;
152 size -= header->bLength;
153 break;
154 }
155 }
156 bptr += header->bLength;
157 size -= header->bLength;
158 } /* end of while */
159
160 /*------------------------------------------------------------------*/
161 /* Parsing all follwoing CDC class interface descriptors */
162 /*------------------------------------------------------------------*/
163
164 while (size >= sizeof(DESC_HDR_T))
165 {
166 header = (DESC_HDR_T *)bptr;
167
168 if ((header->bLength > size) || (header->bLength < 2))
169 {
170 CDC_DBGMSG("Error - invalid descriptor length of %d\n", header->bLength);
171 return USBH_ERR_NOT_SUPPORTED;
172 }
173
174 /*
175 * Is a class interface descriptor?
176 */
177 if (header->bDescriptorType != CDC_CS_INTERFACE)
178 break;
179
180 result = cdc_parse_cs_interface(cdev, bptr, size);
181 if (result < 0)
182 return result;
183 bptr += result;
184 size -= result;
185 } /* end of while */
186
187 CDC_DBGMSG("CDC ifnum_cdc = %d\n", cdev->iface_cdc->if_num);
188 if (cdev->iface_data)
189 {
190 CDC_DBGMSG("CDC ifnum_data = %d\n", cdev->iface_data->if_num);
191 }
192 return 0;
193 }
194
195
197
198 /*** (C) COPYRIGHT 2018~2019 Nuvoton Technology Corp. ***/
199
USBH_ERR_NOT_SUPPORTED
#define USBH_ERR_NOT_SUPPORTED
Definition: usbh_lib.h:35
NuMicro.h
NuMicro peripheral access layer header file.
cdc_dev_t::ifnum_data
int ifnum_data
Definition: usbh_cdc.h:180
usbh_lib.h
USB Host library exported header file.
cdc_dev_t::udev
UDEV_T * udev
Definition: usbh_cdc.h:177
cdc_dev_t::iface_data
IFACE_T * iface_data
Definition: usbh_cdc.h:179
uac_dev_t::udev
UDEV_T * udev
Definition: usbh_uac.h:111
usb.h
USB Host library header file.
usbh_cdc.h
USB Host CDC(Communication Device Class) driver header file.
cdc_dev_t::iface_cdc
IFACE_T * iface_cdc
Definition: usbh_cdc.h:178
cdc_dev_t
Definition: usbh_cdc.h:175