Exclude host application directories from the Doxygen input source file directories.
[pub/USBasp.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
67 /** Main program entry point. This routine configures the hardware required by the application, then
68 * enters a loop to run the application tasks in sequence.
69 */
70 int main(void)
71 {
72 SetupHardware();
73
74 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
75
76 for (;;)
77 {
78 switch (USB_HostState)
79 {
80 case HOST_STATE_Addressed:
81 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
82
83 uint16_t ConfigDescriptorSize;
84 uint8_t ConfigDescriptorData[512];
85
86 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
87 sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
88 {
89 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
90 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
91 break;
92 }
93
94 if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface,
95 ConfigDescriptorSize, ConfigDescriptorData) != RNDIS_ENUMERROR_NoError)
96 {
97 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
98 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
99 break;
100 }
101
102 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
103 {
104 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
105 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
106 break;
107 }
108
109 if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface) != HOST_SENDCONTROL_Successful)
110 {
111 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
112 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
113 break;
114 }
115
116 uint32_t PacketFilter = (REMOTE_NDIS_PACKET_DIRECTED | REMOTE_NDIS_PACKET_BROADCAST);
117 if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface, OID_GEN_CURRENT_PACKET_FILTER,
118 &PacketFilter, sizeof(PacketFilter)) != HOST_SENDCONTROL_Successful)
119 {
120 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
121 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
122 break;
123 }
124
125 struct uip_eth_addr MACAddress;
126 if (RNDIS_Host_QueryRNDISProperty(&Ethernet_RNDIS_Interface, OID_802_3_CURRENT_ADDRESS,
127 &MACAddress, sizeof(MACAddress)) != HOST_SENDCONTROL_Successful)
128 {
129 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
130 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
131 break;
132 }
133
134 uip_setethaddr(MACAddress);
135
136 LEDs_SetAllLEDs(LEDMASK_USB_READY);
137 USB_HostState = HOST_STATE_Configured;
138 break;
139 case HOST_STATE_Configured:
140 ProcessIncommingPacket();
141 ManageConnections();
142
143 break;
144 }
145
146 RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface);
147 USB_USBTask();
148 }
149 }
150
151 void ProcessIncommingPacket(void)
152 {
153 if (RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface))
154 {
155 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
156
157 /* Read the incomming packet straight into the UIP packet buffer */
158 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], &uip_len);
159
160 if (uip_len > 0)
161 {
162 bool PacketHandled = true;
163
164 struct uip_eth_hdr* EthernetHeader = (struct uip_eth_hdr*)&uip_buf[0];
165 if (EthernetHeader->type == HTONS(UIP_ETHTYPE_IP))
166 {
167 /* Filter packet by MAC destination */
168 uip_arp_ipin();
169
170 /* Process incomming packet */
171 uip_input();
172
173 /* Add destination MAC to outgoing packet */
174 if (uip_len > 0)
175 uip_arp_out();
176 }
177 else if (EthernetHeader->type == HTONS(UIP_ETHTYPE_ARP))
178 {
179 /* Process ARP packet */
180 uip_arp_arpin();
181 }
182 else
183 {
184 PacketHandled = false;
185 }
186
187 /* If a response was generated, send it */
188 if ((uip_len > 0) && PacketHandled)
189 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);
190 }
191
192 LEDs_SetAllLEDs(LEDMASK_USB_READY);
193 }
194 }
195
196 void ManageConnections(void)
197 {
198 /* Manage open connections */
199 if (timer_expired(&ConnectionTimer))
200 {
201 timer_reset(&ConnectionTimer);
202
203 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
204
205 for (uint8_t i = 0; i < UIP_CONNS; i++)
206 {
207 /* Run periodic connection management for each connection */
208 uip_periodic(i);
209
210 /* If a response was generated, send it */
211 if (uip_len > 0)
212 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);
213 }
214
215 LEDs_SetAllLEDs(LEDMASK_USB_READY);
216 }
217
218 /* Manage ARP cache refreshing */
219 if (timer_expired(&ARPTimer))
220 {
221 timer_reset(&ARPTimer);
222 uip_arp_timer();
223 }
224 }
225
226 /** Configures the board hardware and chip peripherals for the demo's functionality. */
227 void SetupHardware(void)
228 {
229 /* Disable watchdog if enabled by bootloader/fuses */
230 MCUSR &= ~(1 << WDRF);
231 wdt_disable();
232
233 /* Disable clock division */
234 clock_prescale_set(clock_div_1);
235
236 /* Hardware Initialization */
237 LEDs_Init();
238 USB_Init();
239
240 /* uIP Timing Initialization */
241 clock_init();
242 timer_set(&ConnectionTimer, CLOCK_SECOND / 2);
243 timer_set(&ARPTimer, CLOCK_SECOND * 10);
244
245 /* uIP Stack Initialization */
246 uip_init();
247 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
248 uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
249 uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
250 uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
251 uip_sethostaddr(&IPAddress);
252 uip_setnetmask(&Netmask);
253 uip_setdraddr(&GatewayIPAddress);
254
255 /* HTTP Webserver Initialization */
256 WebserverApp_Init();
257 }
258
259 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
260 * starts the library USB task to begin the enumeration and USB management process.
261 */
262 void EVENT_USB_Host_DeviceAttached(void)
263 {
264 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
265 }
266
267 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
268 * stops the library USB task management process.
269 */
270 void EVENT_USB_Host_DeviceUnattached(void)
271 {
272 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
273 }
274
275 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
276 * enumerated by the host and is now ready to be used by the application.
277 */
278 void EVENT_USB_Host_DeviceEnumerationComplete(void)
279 {
280 LEDs_SetAllLEDs(LEDMASK_USB_READY);
281 }
282
283 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
284 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
285 {
286 USB_ShutDown();
287
288 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
289 for(;;);
290 }
291
292 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
293 * enumerating an attached USB device.
294 */
295 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
296 {
297 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
298 }