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