3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
37 #include "Webserver.h"
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.
43 USB_ClassInfo_RNDIS_Host_t Ethernet_RNDIS_Interface
=
47 .DataINPipeNumber
= 1,
48 .DataINPipeDoubleBank
= false,
50 .DataOUTPipeNumber
= 2,
51 .DataOUTPipeDoubleBank
= false,
53 .NotificationPipeNumber
= 3,
54 .NotificationPipeDoubleBank
= false,
56 .HostMaxPacketSize
= UIP_CONF_BUFFER_SIZE
,
60 struct timer ConnectionTimer
, ARPTimer
;
61 uint16_t MillisecondTickCount
;
63 /** ISR for the management of the connection management timeout counter */
64 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
66 MillisecondTickCount
++;
69 void TCPCallback(void)
71 printf("Callback!\r\n");
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.
81 puts_P(PSTR(ESC_FG_CYAN
"RNDIS Host Demo running.\r\n" ESC_FG_WHITE
));
83 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
87 switch (USB_HostState
)
89 case HOST_STATE_Addressed
:
90 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
92 uint16_t ConfigDescriptorSize
;
93 uint8_t ConfigDescriptorData
[512];
95 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
,
96 sizeof(ConfigDescriptorData
)) != HOST_GETCONFIG_Successful
)
98 printf("Error Retrieving Configuration Descriptor.\r\n");
99 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
100 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
104 if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface
,
105 ConfigDescriptorSize
, ConfigDescriptorData
) != RNDIS_ENUMERROR_NoError
)
107 printf("Attached Device Not a Valid RNDIS Class Device.\r\n");
108 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
109 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
113 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful
)
115 printf("Error Setting Device Configuration.\r\n");
116 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
117 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
121 if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface
) != HOST_SENDCONTROL_Successful
)
123 printf("Error Initializing Device.\r\n");
125 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
126 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
130 printf("Device Max Transfer Size: %lu bytes.\r\n", Ethernet_RNDIS_Interface
.State
.DeviceMaxPacketSize
);
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
)
136 printf("Error Setting Device Packet Filter.\r\n");
138 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
139 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
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
)
147 printf("Error Getting MAC Address.\r\n");
149 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
150 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
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]);
158 uip_setethaddr(MACAddress
);
160 printf("RNDIS Device Enumerated.\r\n");
161 USB_HostState
= HOST_STATE_Configured
;
163 case HOST_STATE_Configured
:
164 ProcessIncommingPacket();
170 RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface
);
175 void ProcessIncommingPacket(void)
177 if (RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface
))
179 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
181 /* Read the incomming packet straight into the UIP packet buffer */
182 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface
, uip_buf
, &uip_len
);
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
]);
189 struct uip_eth_hdr
* EthernetHeader
= (struct uip_eth_hdr
*)&uip_buf
[0];
190 if (EthernetHeader
->type
== HTONS(UIP_ETHTYPE_IP
))
192 /* Filter packet by MAC destination */
195 /* Process incomming packet */
198 /* Add destination MAC to outgoing packet */
202 else if (EthernetHeader
->type
== HTONS(UIP_ETHTYPE_ARP
))
204 /* Process ARP packet */
208 /* If a response was generated, send it */
210 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface
, uip_buf
, uip_len
);
212 printf("SENT PACKET (%d):\r\n", uip_len
);
213 for (uint16_t i
= 0; i
< uip_len
; i
++)
214 printf("0x%02X ", uip_buf
[i
]);
217 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
221 void ManageConnections(void)
223 /* Manage open connections */
224 if (timer_expired(&ConnectionTimer
))
226 timer_reset(&ConnectionTimer
);
228 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
230 for (uint8_t i
= 0; i
< UIP_CONNS
; i
++)
232 /* Run periodic connection management for each connection */
235 /* If a response was generated, send it */
237 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface
, uip_buf
, uip_len
);
240 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
243 /* Manage ARP cache refreshing */
244 if (timer_expired(&ARPTimer
))
246 timer_reset(&ARPTimer
);
251 /** Configures the board hardware and chip peripherals for the demo's functionality. */
252 void SetupHardware(void)
254 /* Disable watchdog if enabled by bootloader/fuses */
255 MCUSR
&= ~(1 << WDRF
);
258 /* Disable clock division */
259 clock_prescale_set(clock_div_1
);
261 /* Hardware Initialization */
262 SerialStream_Init(9600, false);
266 /* uIP Timing Initialization */
268 timer_set(&ConnectionTimer
, CLOCK_SECOND
/ 2);
269 timer_set(&ARPTimer
, CLOCK_SECOND
* 10);
271 /* uIP Stack Initialization */
273 uip_ipaddr_t IPAddress
, Netmask
, GatewayIPAddress
;
274 uip_ipaddr(&IPAddress
, 192, 168, 1, 10);
275 uip_ipaddr(&Netmask
, 255, 255, 255, 0);
276 uip_ipaddr(&GatewayIPAddress
, 192, 168, 1, 1);
277 uip_sethostaddr(&IPAddress
);
278 uip_setnetmask(&Netmask
);
279 uip_setdraddr(&GatewayIPAddress
);
281 /* HTTP Webserver Initialization */
282 uip_listen(HTONS(80));
285 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
286 * starts the library USB task to begin the enumeration and USB management process.
288 void EVENT_USB_Host_DeviceAttached(void)
290 puts_P(PSTR("Device Attached.\r\n"));
291 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
294 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
295 * stops the library USB task management process.
297 void EVENT_USB_Host_DeviceUnattached(void)
299 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
300 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
303 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
304 * enumerated by the host and is now ready to be used by the application.
306 void EVENT_USB_Host_DeviceEnumerationComplete(void)
308 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
311 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
312 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
)
316 printf_P(PSTR(ESC_FG_RED
"Host Mode Error\r\n"
317 " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
);
319 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
323 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
324 * enumerating an attached USB device.
326 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
)
328 printf_P(PSTR(ESC_FG_RED
"Dev Enum Error\r\n"
329 " -- Error Code %d\r\n"
330 " -- Sub Error Code %d\r\n"
331 " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
);
333 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);