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