ec1dabffb2715c1c47afcedb09809d7a58b452ca
[pub/lufa.git] / Projects / Webserver / Webserver.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 Webserver project. This file contains the main tasks of
34 * the project and is responsible for the initial application hardware configuration.
35 */
36
37 #include "Webserver.h"
38
39 /** LUFA RNDIS Class driver interface configuration and state information. This structure is
40 * passed to all RNDIS Class driver functions, so that multiple instances of the same class
41 * within a device can be differentiated from one another.
42 */
43 USB_ClassInfo_RNDIS_Host_t Ethernet_RNDIS_Interface =
44 {
45 .Config =
46 {
47 .DataINPipeNumber = 1,
48 .DataINPipeDoubleBank = false,
49
50 .DataOUTPipeNumber = 2,
51 .DataOUTPipeDoubleBank = false,
52
53 .NotificationPipeNumber = 3,
54 .NotificationPipeDoubleBank = false,
55
56 .HostMaxPacketSize = UIP_CONF_BUFFER_SIZE,
57 },
58 };
59
60 /** Connection timer, to retain the time elapsed since the last time the uIP connections were managed. */
61 struct timer ConnectionTimer;
62
63 /** ARP timer, to retain the time elapsed since the ARP cache was last updated. */
64 struct timer ARPTimer;
65
66 /** MAC address of the RNDIS device, when enumerated */
67 struct uip_eth_addr MACAddress;
68
69 /** Main program entry point. This routine configures the hardware required by the application, then
70 * enters a loop to run the application tasks in sequence.
71 */
72 int main(void)
73 {
74 SetupHardware();
75
76 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
77
78 for (;;)
79 {
80 switch (USB_HostState)
81 {
82 case HOST_STATE_Addressed:
83 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
84
85 uint16_t ConfigDescriptorSize;
86 uint8_t ConfigDescriptorData[512];
87
88 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
89 sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
90 {
91 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
92 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
93 break;
94 }
95
96 if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface,
97 ConfigDescriptorSize, ConfigDescriptorData) != RNDIS_ENUMERROR_NoError)
98 {
99 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
100 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
101 break;
102 }
103
104 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
105 {
106 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
107 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
108 break;
109 }
110
111 if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface) != HOST_SENDCONTROL_Successful)
112 {
113 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
114 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
115 break;
116 }
117
118 uint32_t PacketFilter = (REMOTE_NDIS_PACKET_DIRECTED | REMOTE_NDIS_PACKET_BROADCAST);
119 if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface, OID_GEN_CURRENT_PACKET_FILTER,
120 &PacketFilter, sizeof(PacketFilter)) != HOST_SENDCONTROL_Successful)
121 {
122 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
123 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
124 break;
125 }
126
127 if (RNDIS_Host_QueryRNDISProperty(&Ethernet_RNDIS_Interface, OID_802_3_CURRENT_ADDRESS,
128 &MACAddress, sizeof(MACAddress)) != HOST_SENDCONTROL_Successful)
129 {
130 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
131 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
132 break;
133 }
134
135 uip_setethaddr(MACAddress);
136
137 LEDs_SetAllLEDs(LEDMASK_USB_READY);
138 USB_HostState = HOST_STATE_Configured;
139 break;
140 case HOST_STATE_Configured:
141 ProcessIncommingPacket();
142 ManageConnections();
143
144 break;
145 }
146
147 RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface);
148 USB_USBTask();
149 }
150 }
151
152 /** Processes incomming packets to the server from the connected RNDIS device, creating responses as needed. */
153 void ProcessIncommingPacket(void)
154 {
155 if (RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface))
156 {
157 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
158
159 /* Read the incomming packet straight into the UIP packet buffer */
160 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], &uip_len);
161
162 if (uip_len > 0)
163 {
164 bool PacketHandled = true;
165
166 struct uip_eth_hdr* EthernetHeader = (struct uip_eth_hdr*)&uip_buf[0];
167 if (EthernetHeader->type == HTONS(UIP_ETHTYPE_IP))
168 {
169 /* Filter packet by MAC destination */
170 uip_arp_ipin();
171
172 /* Process incomming packet */
173 uip_input();
174
175 /* Add destination MAC to outgoing packet */
176 if (uip_len > 0)
177 uip_arp_out();
178 }
179 else if (EthernetHeader->type == HTONS(UIP_ETHTYPE_ARP))
180 {
181 /* Process ARP packet */
182 uip_arp_arpin();
183 }
184 else
185 {
186 PacketHandled = false;
187 }
188
189 /* If a response was generated, send it */
190 if ((uip_len > 0) && PacketHandled)
191 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);
192 }
193
194 LEDs_SetAllLEDs(LEDMASK_USB_READY);
195 }
196 }
197
198 /** Manages the currently open network connections, including TCP and (if enabled) UDP. */
199 void ManageConnections(void)
200 {
201 /* Manage open connections */
202 if (timer_expired(&ConnectionTimer))
203 {
204 timer_reset(&ConnectionTimer);
205
206 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
207
208 for (uint8_t i = 0; i < UIP_CONNS; i++)
209 {
210 /* Run periodic connection management for each TCP connection */
211 uip_periodic(i);
212
213 /* If a response was generated, send it */
214 if (uip_len > 0)
215 {
216 /* Add destination MAC to outgoing packet */
217 uip_arp_out();
218
219 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);
220 }
221 }
222
223 #if defined(ENABLE_DHCP)
224 for (uint8_t i = 0; i < UIP_UDP_CONNS; i++)
225 {
226 /* Run periodic connection management for each UDP connection */
227 uip_udp_periodic(i);
228
229 /* If a response was generated, send it */
230 if (uip_len > 0)
231 {
232 /* Add destination MAC to outgoing packet */
233 uip_arp_out();
234
235 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);
236 }
237 }
238 #endif
239
240 LEDs_SetAllLEDs(LEDMASK_USB_READY);
241 }
242
243 /* Manage ARP cache refreshing */
244 if (timer_expired(&ARPTimer))
245 {
246 timer_reset(&ARPTimer);
247 uip_arp_timer();
248 }
249 }
250
251 /** Configures the board hardware and chip peripherals for the demo's functionality. */
252 void SetupHardware(void)
253 {
254 /* Disable watchdog if enabled by bootloader/fuses */
255 MCUSR &= ~(1 << WDRF);
256 wdt_disable();
257
258 /* Disable clock division */
259 clock_prescale_set(clock_div_1);
260
261 /* Hardware Initialization */
262 LEDs_Init();
263 USB_Init();
264
265 /* uIP Timing Initialization */
266 clock_init();
267 timer_set(&ConnectionTimer, CLOCK_SECOND / 2);
268 timer_set(&ARPTimer, CLOCK_SECOND * 10);
269
270 /* uIP Stack Initialization */
271 uip_init();
272
273 /* DHCP/Server IP Settings Initialization */
274 #if defined(ENABLE_DHCP)
275 DHCPApp_Init();
276 #else
277 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
278 uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
279 uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
280 uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
281 uip_sethostaddr(&IPAddress);
282 uip_setnetmask(&Netmask);
283 uip_setdraddr(&GatewayIPAddress);
284 #endif
285
286 /* HTTP Webserver Initialization */
287 WebserverApp_Init();
288 }
289
290 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
291 * starts the library USB task to begin the enumeration and USB management process.
292 */
293 void EVENT_USB_Host_DeviceAttached(void)
294 {
295 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
296 }
297
298 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
299 * stops the library USB task management process.
300 */
301 void EVENT_USB_Host_DeviceUnattached(void)
302 {
303 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
304 }
305
306 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
307 * enumerated by the host and is now ready to be used by the application.
308 */
309 void EVENT_USB_Host_DeviceEnumerationComplete(void)
310 {
311 LEDs_SetAllLEDs(LEDMASK_USB_READY);
312 }
313
314 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
315 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
316 {
317 USB_ShutDown();
318
319 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
320 for(;;);
321 }
322
323 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
324 * enumerating an attached USB device.
325 */
326 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
327 {
328 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
329 }