9ad26dd2f671ffa07ad684d4cad828b57f16d429
[pub/USBasp.git] / Demos / Device / LowLevel / RNDISEthernet / RNDISEthernet.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 /** \file
32 *
33 * Main source file for the RNDISEthernet demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "RNDISEthernet.h"
38
39 /** Main program entry point. This routine configures the hardware required by the application, then
40 * enters a loop to run the application tasks in sequence.
41 */
42 int main(void)
43 {
44 SetupHardware();
45
46 /* Webserver Initialization */
47 TCP_Init();
48 Webserver_Init();
49
50 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
51 sei();
52
53 for (;;)
54 {
55 Ethernet_Task();
56 TCP_Task();
57 RNDIS_Task();
58 USB_USBTask();
59 }
60 }
61
62 /** Configures the board hardware and chip peripherals for the demo's functionality. */
63 void SetupHardware(void)
64 {
65 /* Disable watchdog if enabled by bootloader/fuses */
66 MCUSR &= ~(1 << WDRF);
67 wdt_disable();
68
69 /* Disable clock division */
70 clock_prescale_set(clock_div_1);
71
72 /* Hardware Initialization */
73 LEDs_Init();
74 Serial_Init(9600, false);
75 USB_Init();
76
77 /* Create a stdio stream for the serial port for stdin and stdout */
78 Serial_CreateStream(NULL);
79 }
80
81 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
82 * starts the library USB task to begin the enumeration and USB management process.
83 */
84 void EVENT_USB_Device_Connect(void)
85 {
86 /* Indicate USB enumerating */
87 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
88 }
89
90 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
91 * the status LEDs and stops all the relevant tasks.
92 */
93 void EVENT_USB_Device_Disconnect(void)
94 {
95 /* Indicate USB not ready */
96 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
97 }
98
99 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
100 * of the USB device after enumeration, and configures the RNDIS device endpoints and starts the relevant tasks.
101 */
102 void EVENT_USB_Device_ConfigurationChanged(void)
103 {
104 bool ConfigSuccess = true;
105
106 /* Setup RNDIS Data Endpoints */
107 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_IN,
108 CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE);
109 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_OUT,
110 CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE);
111 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
112 CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
113
114 /* Indicate endpoint configuration success or failure */
115 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
116 }
117
118 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
119 * the device from the USB host before passing along unhandled control requests to the library for processing
120 * internally.
121 */
122 void EVENT_USB_Device_ControlRequest(void)
123 {
124 /* Process RNDIS class commands */
125 switch (USB_ControlRequest.bRequest)
126 {
127 case RNDIS_REQ_SendEncapsulatedCommand:
128 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
129 {
130 Endpoint_ClearSETUP();
131
132 /* Read in the RNDIS message into the message buffer */
133 Endpoint_Read_Control_Stream_LE(RNDISMessageBuffer, USB_ControlRequest.wLength);
134 Endpoint_ClearIN();
135
136 /* Process the RNDIS message */
137 ProcessRNDISControlMessage();
138 }
139
140 break;
141 case RNDIS_REQ_GetEncapsulatedResponse:
142 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
143 {
144 /* Check if a response to the last message is ready */
145 if (!(MessageHeader->MessageLength))
146 {
147 /* Set the response to a single 0x00 byte to indicate that no response is ready */
148 RNDISMessageBuffer[0] = 0;
149 MessageHeader->MessageLength = 1;
150 }
151
152 Endpoint_ClearSETUP();
153
154 /* Write the message response data to the endpoint */
155 Endpoint_Write_Control_Stream_LE(RNDISMessageBuffer, MessageHeader->MessageLength);
156 Endpoint_ClearOUT();
157
158 /* Reset the message header once again after transmission */
159 MessageHeader->MessageLength = 0;
160 }
161
162 break;
163 }
164 }
165
166 /** Task to manage the sending and receiving of encapsulated RNDIS data and notifications. This removes the RNDIS
167 * wrapper from received Ethernet frames and places them in the FrameIN global buffer, or adds the RNDIS wrapper
168 * to a frame in the FrameOUT global before sending the buffer contents to the host.
169 */
170 void RNDIS_Task(void)
171 {
172 /* Select the notification endpoint */
173 Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM);
174
175 /* Check if a message response is ready for the host */
176 if (Endpoint_IsINReady() && ResponseReady)
177 {
178 USB_Request_Header_t Notification = (USB_Request_Header_t)
179 {
180 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
181 .bRequest = RNDIS_NOTIF_ResponseAvailable,
182 .wValue = 0,
183 .wIndex = 0,
184 .wLength = 0,
185 };
186
187 /* Indicate that a message response is ready for the host */
188 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification), NULL);
189
190 /* Finalize the stream transfer to send the last packet */
191 Endpoint_ClearIN();
192
193 /* Indicate a response is no longer ready */
194 ResponseReady = false;
195 }
196
197 /* Don't process the data endpoints until the system is in the data initialized state, and the buffer is free */
198 if ((CurrRNDISState == RNDIS_Data_Initialized) && !(MessageHeader->MessageLength))
199 {
200 /* Create a new packet header for reading/writing */
201 RNDIS_Packet_Message_t RNDISPacketHeader;
202
203 /* Select the data OUT endpoint */
204 Endpoint_SelectEndpoint(CDC_RX_EPNUM);
205
206 /* Check if the data OUT endpoint contains data, and that the IN buffer is empty */
207 if (Endpoint_IsOUTReceived() && !(FrameIN.FrameLength))
208 {
209 /* Read in the packet message header */
210 Endpoint_Read_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
211
212 /* Stall the request if the data is too large */
213 if (RNDISPacketHeader.DataLength > ETHERNET_FRAME_SIZE_MAX)
214 {
215 Endpoint_StallTransaction();
216 return;
217 }
218
219 /* Read in the Ethernet frame into the buffer */
220 Endpoint_Read_Stream_LE(FrameIN.FrameData, RNDISPacketHeader.DataLength, NULL);
221
222 /* Finalize the stream transfer to send the last packet */
223 Endpoint_ClearOUT();
224
225 /* Store the size of the Ethernet frame */
226 FrameIN.FrameLength = RNDISPacketHeader.DataLength;
227 }
228
229 /* Select the data IN endpoint */
230 Endpoint_SelectEndpoint(CDC_TX_EPNUM);
231
232 /* Check if the data IN endpoint is ready for more data, and that the IN buffer is full */
233 if (Endpoint_IsINReady() && FrameOUT.FrameLength)
234 {
235 /* Clear the packet header with all 0s so that the relevant fields can be filled */
236 memset(&RNDISPacketHeader, 0, sizeof(RNDIS_Packet_Message_t));
237
238 /* Construct the required packet header fields in the buffer */
239 RNDISPacketHeader.MessageType = REMOTE_NDIS_PACKET_MSG;
240 RNDISPacketHeader.MessageLength = (sizeof(RNDIS_Packet_Message_t) + FrameOUT.FrameLength);
241 RNDISPacketHeader.DataOffset = (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t));
242 RNDISPacketHeader.DataLength = FrameOUT.FrameLength;
243
244 /* Send the packet header to the host */
245 Endpoint_Write_Stream_LE(&RNDISPacketHeader, sizeof(RNDIS_Packet_Message_t), NULL);
246
247 /* Send the Ethernet frame data to the host */
248 Endpoint_Write_Stream_LE(FrameOUT.FrameData, RNDISPacketHeader.DataLength, NULL);
249
250 /* Finalize the stream transfer to send the last packet */
251 Endpoint_ClearIN();
252
253 /* Indicate Ethernet OUT buffer no longer full */
254 FrameOUT.FrameLength = 0;
255 }
256 }
257 }
258
259 /** Ethernet frame processing task. This task checks to see if a frame has been received, and if so hands off the processing
260 * of the frame to the Ethernet processing routines.
261 */
262 void Ethernet_Task(void)
263 {
264 /* Task for Ethernet processing. Incoming ethernet frames are loaded into the FrameIN structure, and
265 outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single
266 Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
267
268 /* Device must be connected and configured for the task to run */
269 if (USB_DeviceState != DEVICE_STATE_Configured)
270 return;
271
272 /* Check if a frame has been written to the IN frame buffer */
273 if (FrameIN.FrameLength)
274 {
275 /* Indicate packet processing started */
276 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
277
278 /* Process the ethernet frame - replace this with your own Ethernet handler code as desired */
279 Ethernet_ProcessPacket();
280
281 /* Indicate packet processing complete */
282 LEDs_SetAllLEDs(LEDMASK_USB_READY);
283 }
284 }
285