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