Oops - missing brackets in the declaration of a string in TELNETServerApp.c.
[pub/USBasp.git] / Projects / Webserver / Lib / uIPManagement.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 * uIP Management functions. This file contains the functions and globals needed to maintain the uIP
34 * stack once an RNDIS device has been attached to the system.
35 */
36
37 #define INCLUDE_FROM_UIPMANAGEMENT_C
38 #include "uIPManagement.h"
39
40 /** Connection timer, to retain the time elapsed since the last time the uIP connections were managed. */
41 struct timer ConnectionTimer;
42
43 /** ARP timer, to retain the time elapsed since the ARP cache was last updated. */
44 struct timer ARPTimer;
45
46 /** MAC address of the RNDIS device, when enumerated */
47 struct uip_eth_addr MACAddress;
48
49
50 /** Configures the uIP stack ready for network traffic. */
51 void uIPManagement_Init(void)
52 {
53 /* uIP Timing Initialization */
54 clock_init();
55 timer_set(&ConnectionTimer, CLOCK_SECOND / 10);
56 timer_set(&ARPTimer, CLOCK_SECOND * 10);
57
58 /* uIP Stack Initialization */
59 uip_init();
60 uip_arp_init();
61 uip_setethaddr(MACAddress);
62
63 /* DHCP/Server IP Settings Initialization */
64 #if defined(ENABLE_DHCP_CLIENT)
65 DHCPClientApp_Init();
66 #else
67 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
68 uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
69 uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
70 uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
71 uip_sethostaddr(&IPAddress);
72 uip_setnetmask(&Netmask);
73 uip_setdraddr(&GatewayIPAddress);
74 #endif
75
76 /* HTTP Webserver Initialization */
77 HTTPServerApp_Init();
78
79 /* TELNET Server Initialization */
80 TELNETServerApp_Init();
81 }
82
83 /** uIP Management function. This function manages the uIP stack when called while an RNDIS device has been
84 * attached to the system.
85 */
86 void uIPManagement_ManageNetwork(void)
87 {
88 if ((USB_CurrentMode == USB_MODE_HOST) && (USB_HostState == HOST_STATE_Configured))
89 {
90 uIPManagement_ProcessIncomingPacket();
91 uIPManagement_ManageConnections();
92 }
93 }
94
95 /** uIP TCP/IP network stack callback function for the processing of a given TCP connection. This routine dispatches
96 * to the appropriate TCP protocol application based on the connection's listen port number.
97 */
98 void uIPManagement_TCPCallback(void)
99 {
100 /* Call the correct TCP application based on the port number the connection is listening on */
101 switch (uip_conn->lport)
102 {
103 case HTONS(HTTP_SERVER_PORT):
104 HTTPServerApp_Callback();
105 break;
106 case HTONS(TELNET_SERVER_PORT):
107 TELNETServerApp_Callback();
108 break;
109 }
110 }
111
112 /** uIP TCP/IP network stack callback function for the processing of a given UDP connection. This routine dispatches
113 * to the appropriate UDP protocol application based on the connection's listen port number.
114 */
115 void uIPManagement_UDPCallback(void)
116 {
117 /* Call the correct UDP application based on the port number the connection is listening on */
118 switch (uip_udp_conn->lport)
119 {
120 case HTONS(DHCPC_CLIENT_PORT):
121 DHCPClientApp_Callback();
122 break;
123 }
124 }
125
126 /** Processes Incoming packets to the server from the connected RNDIS device, creating responses as needed. */
127 static void uIPManagement_ProcessIncomingPacket(void)
128 {
129 /* If no packet received, exit processing routine */
130 if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface)))
131 return;
132
133 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
134
135 /* Read the Incoming packet straight into the UIP packet buffer */
136 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, uip_buf, &uip_len);
137
138 /* If the packet contains an Ethernet frame, process it */
139 if (uip_len > 0)
140 {
141 switch (((struct uip_eth_hdr*)uip_buf)->type)
142 {
143 case HTONS(UIP_ETHTYPE_IP):
144 /* Filter packet by MAC destination */
145 uip_arp_ipin();
146
147 /* Process Incoming packet */
148 uip_input();
149
150 /* If a response was generated, send it */
151 if (uip_len > 0)
152 {
153 /* Add destination MAC to outgoing packet */
154 uip_arp_out();
155
156 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
157 }
158
159 break;
160 case HTONS(UIP_ETHTYPE_ARP):
161 /* Process ARP packet */
162 uip_arp_arpin();
163
164 /* If a response was generated, send it */
165 if (uip_len > 0)
166 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
167
168 break;
169 }
170 }
171
172 LEDs_SetAllLEDs(LEDMASK_USB_READY);
173 }
174
175 /** Manages the currently open network connections, including TCP and (if enabled) UDP. */
176 static void uIPManagement_ManageConnections(void)
177 {
178 /* Poll TCP connections for more data to send back to the host */
179 for (uint8_t i = 0; i < UIP_CONNS; i++)
180 {
181 uip_poll_conn(&uip_conns[i]);
182
183 /* If a response was generated, send it */
184 if (uip_len > 0)
185 {
186 /* Add destination MAC to outgoing packet */
187 uip_arp_out();
188
189 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
190 }
191 }
192
193 /* Manage open connections for timeouts */
194 if (timer_expired(&ConnectionTimer))
195 {
196 timer_reset(&ConnectionTimer);
197
198 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
199
200 for (uint8_t i = 0; i < UIP_CONNS; i++)
201 {
202 /* Run periodic connection management for each TCP connection */
203 uip_periodic(i);
204
205 /* If a response was generated, send it */
206 if (uip_len > 0)
207 {
208 /* Add destination MAC to outgoing packet */
209 uip_arp_out();
210
211 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
212 }
213 }
214
215 #if defined(ENABLE_DHCP_CLIENT)
216 for (uint8_t i = 0; i < UIP_UDP_CONNS; i++)
217 {
218 /* Run periodic connection management for each UDP connection */
219 uip_udp_periodic(i);
220
221 /* If a response was generated, send it */
222 if (uip_len > 0)
223 {
224 /* Add destination MAC to outgoing packet */
225 uip_arp_out();
226
227 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
228 }
229 }
230 #endif
231
232 LEDs_SetAllLEDs(LEDMASK_USB_READY);
233 }
234
235 /* Manage ARP cache refreshing */
236 if (timer_expired(&ARPTimer))
237 {
238 timer_reset(&ARPTimer);
239 uip_arp_timer();
240 }
241 }