c0ceeed7821575e875a820310294453340b60c83
[pub/USBasp.git] / Projects / Incomplete / 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 puts_P(PSTR(ESC_FG_CYAN "RNDIS Host Demo running.\r\n" ESC_FG_WHITE));
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 printf("Error Retrieving Configuration Descriptor.\r\n");
92 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
93 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
94 break;
95 }
96
97 if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface,
98 ConfigDescriptorSize, ConfigDescriptorData) != RNDIS_ENUMERROR_NoError)
99 {
100 printf("Attached Device Not a Valid RNDIS Class Device.\r\n");
101 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
102 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
103 break;
104 }
105
106 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
107 {
108 printf("Error Setting Device Configuration.\r\n");
109 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
110 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
111 break;
112 }
113
114 if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface) != HOST_SENDCONTROL_Successful)
115 {
116 printf("Error Initializing Device.\r\n");
117
118 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
119 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
120 break;
121 }
122
123 printf("Device Max Transfer Size: %lu bytes.\r\n", Ethernet_RNDIS_Interface.State.DeviceMaxPacketSize);
124
125 uint32_t PacketFilter = (REMOTE_NDIS_PACKET_DIRECTED | REMOTE_NDIS_PACKET_BROADCAST);
126 if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface, OID_GEN_CURRENT_PACKET_FILTER,
127 &PacketFilter, sizeof(PacketFilter)) != HOST_SENDCONTROL_Successful)
128 {
129 printf("Error Setting Device Packet Filter.\r\n");
130
131 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
132 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
133 break;
134 }
135
136 struct uip_eth_addr MACAddress;
137 if (RNDIS_Host_QueryRNDISProperty(&Ethernet_RNDIS_Interface, OID_802_3_CURRENT_ADDRESS,
138 &MACAddress, sizeof(MACAddress)) != HOST_SENDCONTROL_Successful)
139 {
140 printf("Error Getting MAC Address.\r\n");
141
142 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
143 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
144 break;
145 }
146
147 printf("MAC Address: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\r\n",
148 MACAddress.addr[0], MACAddress.addr[1], MACAddress.addr[2],
149 MACAddress.addr[3], MACAddress.addr[4], MACAddress.addr[5]);
150
151 uip_setethaddr(MACAddress);
152
153 LEDs_SetAllLEDs(LEDMASK_USB_READY);
154
155 printf("RNDIS Device Enumerated.\r\n");
156 USB_HostState = HOST_STATE_Configured;
157 break;
158 case HOST_STATE_Configured:
159 ProcessIncommingPacket();
160 ManageConnections();
161
162 break;
163 }
164
165 RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface);
166 USB_USBTask();
167 }
168 }
169
170 void ProcessIncommingPacket(void)
171 {
172 if (RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface))
173 {
174 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
175
176 /* Read the incomming packet straight into the UIP packet buffer */
177 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], &uip_len);
178
179 if (uip_len > 0)
180 {
181 bool PacketHandled = true;
182
183 struct uip_eth_hdr* EthernetHeader = (struct uip_eth_hdr*)&uip_buf[0];
184 if (EthernetHeader->type == HTONS(UIP_ETHTYPE_IP))
185 {
186 /* Filter packet by MAC destination */
187 uip_arp_ipin();
188
189 /* Process incomming packet */
190 uip_input();
191
192 /* Add destination MAC to outgoing packet */
193 if (uip_len > 0)
194 uip_arp_out();
195 }
196 else if (EthernetHeader->type == HTONS(UIP_ETHTYPE_ARP))
197 {
198 /* Process ARP packet */
199 uip_arp_arpin();
200 }
201 else
202 {
203 PacketHandled = false;
204 }
205
206 /* If a response was generated, send it */
207 if ((uip_len > 0) && PacketHandled)
208 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);
209 }
210
211 LEDs_SetAllLEDs(LEDMASK_USB_READY);
212 }
213 }
214
215 void ManageConnections(void)
216 {
217 /* Manage open connections */
218 if (timer_expired(&ConnectionTimer))
219 {
220 timer_reset(&ConnectionTimer);
221
222 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
223
224 for (uint8_t i = 0; i < UIP_CONNS; i++)
225 {
226 /* Run periodic connection management for each connection */
227 uip_periodic(i);
228
229 /* If a response was generated, send it */
230 if (uip_len > 0)
231 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);
232 }
233
234 LEDs_SetAllLEDs(LEDMASK_USB_READY);
235 }
236
237 /* Manage ARP cache refreshing */
238 if (timer_expired(&ARPTimer))
239 {
240 timer_reset(&ARPTimer);
241 uip_arp_timer();
242 }
243 }
244
245 /** Configures the board hardware and chip peripherals for the demo's functionality. */
246 void SetupHardware(void)
247 {
248 /* Disable watchdog if enabled by bootloader/fuses */
249 MCUSR &= ~(1 << WDRF);
250 wdt_disable();
251
252 /* Disable clock division */
253 clock_prescale_set(clock_div_1);
254
255 /* Hardware Initialization */
256 SerialStream_Init(9600, false);
257 LEDs_Init();
258 USB_Init();
259
260 /* uIP Timing Initialization */
261 clock_init();
262 timer_set(&ConnectionTimer, CLOCK_SECOND / 2);
263 timer_set(&ARPTimer, CLOCK_SECOND * 10);
264
265 /* uIP Stack Initialization */
266 uip_init();
267 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
268 uip_ipaddr(&IPAddress, 192, 168, 1, 10);
269 uip_ipaddr(&Netmask, 255, 255, 255, 0);
270 uip_ipaddr(&GatewayIPAddress, 192, 168, 1, 1);
271 uip_sethostaddr(&IPAddress);
272 uip_setnetmask(&Netmask);
273 uip_setdraddr(&GatewayIPAddress);
274
275 /* HTTP Webserver Initialization */
276 uip_listen(HTONS(80));
277 }
278
279 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
280 * starts the library USB task to begin the enumeration and USB management process.
281 */
282 void EVENT_USB_Host_DeviceAttached(void)
283 {
284 puts_P(PSTR("Device Attached.\r\n"));
285 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
286 }
287
288 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
289 * stops the library USB task management process.
290 */
291 void EVENT_USB_Host_DeviceUnattached(void)
292 {
293 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
294 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
295 }
296
297 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
298 * enumerated by the host and is now ready to be used by the application.
299 */
300 void EVENT_USB_Host_DeviceEnumerationComplete(void)
301 {
302 LEDs_SetAllLEDs(LEDMASK_USB_READY);
303 }
304
305 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
306 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
307 {
308 USB_ShutDown();
309
310 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
311 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
312
313 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
314 for(;;);
315 }
316
317 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
318 * enumerating an attached USB device.
319 */
320 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
321 {
322 printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
323 " -- Error Code %d\r\n"
324 " -- Sub Error Code %d\r\n"
325 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
326
327 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
328 }