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