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