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 project 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 /** Connection timer, to retain the time elapsed since the last time the uIP connections were managed. */
61 struct timer ConnectionTimer
;
63 /** ARP timer, to retain the time elapsed since the ARP cache was last updated. */
64 struct timer ARPTimer
;
66 /** MAC address of the RNDIS device, when enumerated */
67 struct uip_eth_addr MACAddress
;
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.
76 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
80 switch (USB_HostState
)
82 case HOST_STATE_Addressed
:
83 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
85 uint16_t ConfigDescriptorSize
;
86 uint8_t ConfigDescriptorData
[512];
88 if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
,
89 sizeof(ConfigDescriptorData
)) != HOST_GETCONFIG_Successful
)
91 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
92 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
96 if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface
,
97 ConfigDescriptorSize
, ConfigDescriptorData
) != RNDIS_ENUMERROR_NoError
)
99 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
100 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
104 if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful
)
106 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
107 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
111 if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface
) != HOST_SENDCONTROL_Successful
)
113 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
114 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
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
)
122 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
123 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
127 if (RNDIS_Host_QueryRNDISProperty(&Ethernet_RNDIS_Interface
, OID_802_3_CURRENT_ADDRESS
,
128 &MACAddress
, sizeof(MACAddress
)) != HOST_SENDCONTROL_Successful
)
130 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
131 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
135 uip_setethaddr(MACAddress
);
137 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
138 USB_HostState
= HOST_STATE_Configured
;
140 case HOST_STATE_Configured
:
141 ProcessIncommingPacket();
147 RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface
);
152 void ProcessIncommingPacket(void)
154 if (RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface
))
156 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
158 /* Read the incomming packet straight into the UIP packet buffer */
159 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface
, &uip_buf
[0], &uip_len
);
163 bool PacketHandled
= true;
165 struct uip_eth_hdr
* EthernetHeader
= (struct uip_eth_hdr
*)&uip_buf
[0];
166 if (EthernetHeader
->type
== HTONS(UIP_ETHTYPE_IP
))
168 /* Filter packet by MAC destination */
171 /* Process incomming packet */
174 /* Add destination MAC to outgoing packet */
178 else if (EthernetHeader
->type
== HTONS(UIP_ETHTYPE_ARP
))
180 /* Process ARP packet */
185 PacketHandled
= false;
188 /* If a response was generated, send it */
189 if ((uip_len
> 0) && PacketHandled
)
190 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface
, &uip_buf
[0], uip_len
);
193 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
197 void ManageConnections(void)
199 /* Manage open connections */
200 if (timer_expired(&ConnectionTimer
))
202 timer_reset(&ConnectionTimer
);
204 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
206 for (uint8_t i
= 0; i
< UIP_CONNS
; i
++)
208 /* Run periodic connection management for each TCP connection */
211 /* If a response was generated, send it */
214 /* Add destination MAC to outgoing packet */
217 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface
, &uip_buf
[0], uip_len
);
221 #if defined(ENABLE_DHCP)
222 for (uint8_t i
= 0; i
< UIP_UDP_CONNS
; i
++)
224 /* Run periodic connection management for each UDP connection */
227 /* If a response was generated, send it */
230 /* Add destination MAC to outgoing packet */
233 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface
, &uip_buf
[0], uip_len
);
238 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
241 /* Manage ARP cache refreshing */
242 if (timer_expired(&ARPTimer
))
244 timer_reset(&ARPTimer
);
249 /** Configures the board hardware and chip peripherals for the demo's functionality. */
250 void SetupHardware(void)
252 /* Disable watchdog if enabled by bootloader/fuses */
253 MCUSR
&= ~(1 << WDRF
);
256 /* Disable clock division */
257 clock_prescale_set(clock_div_1
);
259 /* Hardware Initialization */
263 /* uIP Timing Initialization */
265 timer_set(&ConnectionTimer
, CLOCK_SECOND
/ 2);
266 timer_set(&ARPTimer
, CLOCK_SECOND
* 10);
268 /* uIP Stack Initialization */
271 /* DHCP/Server IP Settings Initialization */
272 #if defined(ENABLE_DHCP)
275 uip_ipaddr_t IPAddress
, Netmask
, GatewayIPAddress
;
276 uip_ipaddr(&IPAddress
, DEVICE_IP_ADDRESS
[0], DEVICE_IP_ADDRESS
[1], DEVICE_IP_ADDRESS
[2], DEVICE_IP_ADDRESS
[3]);
277 uip_ipaddr(&Netmask
, DEVICE_NETMASK
[0], DEVICE_NETMASK
[1], DEVICE_NETMASK
[2], DEVICE_NETMASK
[3]);
278 uip_ipaddr(&GatewayIPAddress
, DEVICE_GATEWAY
[0], DEVICE_GATEWAY
[1], DEVICE_GATEWAY
[2], DEVICE_GATEWAY
[3]);
279 uip_sethostaddr(&IPAddress
);
280 uip_setnetmask(&Netmask
);
281 uip_setdraddr(&GatewayIPAddress
);
284 /* HTTP Webserver Initialization */
288 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
289 * starts the library USB task to begin the enumeration and USB management process.
291 void EVENT_USB_Host_DeviceAttached(void)
293 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
296 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
297 * stops the library USB task management process.
299 void EVENT_USB_Host_DeviceUnattached(void)
301 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
304 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
305 * enumerated by the host and is now ready to be used by the application.
307 void EVENT_USB_Host_DeviceEnumerationComplete(void)
309 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
312 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
313 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
)
317 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
321 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
322 * enumerating an attached USB device.
324 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
)
326 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);