]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Projects/Webserver/Lib/uIPManagement.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Projects / Webserver / Lib / uIPManagement.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 disclaims 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
50 /** Configures the uIP stack ready for network traffic processing. */
51 void uIPManagement_Init(void)
52 {
53 /* uIP Timing Initialization */
54 clock_init();
55 timer_set(&ConnectionTimer, CLOCK_SECOND / 2);
56 timer_set(&ARPTimer, CLOCK_SECOND * 10);
57
58 /* uIP Stack Initialization */
59 uip_init();
60 uip_arp_init();
61
62 /* DHCP/Server IP Settings Initialization */
63 if (USB_CurrentMode == USB_MODE_Device)
64 {
65 MACAddress.addr[0] = SERVER_MAC_ADDRESS[0];
66 MACAddress.addr[1] = SERVER_MAC_ADDRESS[1];
67 MACAddress.addr[2] = SERVER_MAC_ADDRESS[2];
68 MACAddress.addr[3] = SERVER_MAC_ADDRESS[3];
69 MACAddress.addr[4] = SERVER_MAC_ADDRESS[4];
70 MACAddress.addr[5] = SERVER_MAC_ADDRESS[5];
71
72 #if defined(ENABLE_DHCP_SERVER)
73 DHCPServerApp_Init();
74 #endif
75
76 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
77 uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
78 uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
79 uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
80 uip_sethostaddr(&IPAddress);
81 uip_setnetmask(&Netmask);
82 uip_setdraddr(&GatewayIPAddress);
83 }
84 else
85 {
86 #if defined(ENABLE_DHCP_CLIENT)
87 DHCPClientApp_Init();
88 #else
89 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
90 uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
91 uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
92 uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
93 uip_sethostaddr(&IPAddress);
94 uip_setnetmask(&Netmask);
95 uip_setdraddr(&GatewayIPAddress);
96 #endif
97 }
98
99 /* Virtual Webserver Ethernet Address Configuration */
100 uip_setethaddr(MACAddress);
101
102 /* HTTP Webserver Initialization */
103 HTTPServerApp_Init();
104
105 /* TELNET Server Initialization */
106 #if defined(ENABLE_TELNET_SERVER)
107 TELNETServerApp_Init();
108 #endif
109 }
110
111 /** uIP Management function. This function manages the uIP stack when called while an RNDIS device has been
112 * attached to the system.
113 */
114 void uIPManagement_ManageNetwork(void)
115 {
116 if (((USB_CurrentMode == USB_MODE_Host) && (USB_HostState == HOST_STATE_Configured)) ||
117 ((USB_CurrentMode == USB_MODE_Device) && (USB_DeviceState == DEVICE_STATE_Configured)))
118 {
119 uIPManagement_ProcessIncomingPacket();
120 uIPManagement_ManageConnections();
121 }
122 }
123
124 /** uIP TCP/IP network stack callback function for the processing of a given TCP connection. This routine dispatches
125 * to the appropriate TCP protocol application based on the connection's listen port number.
126 */
127 void uIPManagement_TCPCallback(void)
128 {
129 /* Call the correct TCP application based on the port number the connection is listening on */
130 switch (uip_conn->lport)
131 {
132 case HTONS(HTTP_SERVER_PORT):
133 HTTPServerApp_Callback();
134 break;
135 #if defined(ENABLE_TELNET_SERVER)
136 case HTONS(TELNET_SERVER_PORT):
137 TELNETServerApp_Callback();
138 break;
139 #endif
140 }
141 }
142
143 /** uIP TCP/IP network stack callback function for the processing of a given UDP connection. This routine dispatches
144 * to the appropriate UDP protocol application based on the connection's listen port number.
145 */
146 void uIPManagement_UDPCallback(void)
147 {
148 /* Call the correct UDP application based on the port number the connection is listening on */
149 switch (uip_udp_conn->lport)
150 {
151 #if defined(ENABLE_DHCP_CLIENT)
152 case HTONS(DHCP_CLIENT_PORT):
153 DHCPClientApp_Callback();
154 break;
155 #endif
156 #if defined(ENABLE_DHCP_SERVER)
157 case HTONS(DHCP_SERVER_PORT):
158 DHCPServerApp_Callback();
159 break;
160 #endif
161 }
162 }
163
164 /** Processes Incoming packets to the server from the connected RNDIS device, creating responses as needed. */
165 static void uIPManagement_ProcessIncomingPacket(void)
166 {
167 /* Determine which USB mode the system is currently initialized in */
168 if (USB_CurrentMode == USB_MODE_Device)
169 {
170 /* If no packet received, exit processing routine */
171 if (!(RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface_Device)))
172 return;
173
174 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
175
176 /* Read the Incoming packet straight into the UIP packet buffer */
177 RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, &uip_len);
178 }
179 else
180 {
181 /* If no packet received, exit processing routine */
182 if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface_Host)))
183 return;
184
185 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
186
187 /* Read the Incoming packet straight into the UIP packet buffer */
188 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, &uip_len);
189 }
190
191 /* If the packet contains an Ethernet frame, process it */
192 if (uip_len > 0)
193 {
194 switch (((struct uip_eth_hdr*)uip_buf)->type)
195 {
196 case HTONS(UIP_ETHTYPE_IP):
197 /* Filter packet by MAC destination */
198 uip_arp_ipin();
199
200 /* Process Incoming packet */
201 uip_input();
202
203 /* If a response was generated, send it */
204 if (uip_len > 0)
205 {
206 /* Add destination MAC to outgoing packet */
207 uip_arp_out();
208
209 uip_split_output();
210 }
211
212 break;
213 case HTONS(UIP_ETHTYPE_ARP):
214 /* Process ARP packet */
215 uip_arp_arpin();
216
217 /* If a response was generated, send it */
218 if (uip_len > 0)
219 uip_split_output();
220
221 break;
222 }
223 }
224
225 LEDs_SetAllLEDs(LEDMASK_USB_READY);
226 }
227
228 /** Manages the currently open network connections, including TCP and (if enabled) UDP. */
229 static void uIPManagement_ManageConnections(void)
230 {
231 /* Poll TCP connections for more data to send back to the host */
232 for (uint8_t i = 0; i < UIP_CONNS; i++)
233 {
234 uip_poll_conn(&uip_conns[i]);
235
236 /* If a response was generated, send it */
237 if (uip_len > 0)
238 {
239 /* Add destination MAC to outgoing packet */
240 uip_arp_out();
241
242 /* Split and send the outgoing packet */
243 uip_split_output();
244 }
245 }
246
247 /* Manage open connections for timeouts */
248 if (timer_expired(&ConnectionTimer))
249 {
250 timer_reset(&ConnectionTimer);
251
252 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
253
254 for (uint8_t i = 0; i < UIP_CONNS; i++)
255 {
256 /* Run periodic connection management for each TCP connection */
257 uip_periodic(i);
258
259 /* If a response was generated, send it */
260 if (uip_len > 0)
261 {
262 /* Add destination MAC to outgoing packet */
263 uip_arp_out();
264
265 /* Split and send the outgoing packet */
266 uip_split_output();
267 }
268 }
269
270 #if defined(ENABLE_DHCP_CLIENT)
271 for (uint8_t i = 0; i < UIP_UDP_CONNS; i++)
272 {
273 /* Run periodic connection management for each UDP connection */
274 uip_udp_periodic(i);
275
276 /* If a response was generated, send it */
277 if (uip_len > 0)
278 {
279 /* Add destination MAC to outgoing packet */
280 uip_arp_out();
281
282 /* Split and send the outgoing packet */
283 uip_split_output();
284 }
285 }
286 #endif
287
288 LEDs_SetAllLEDs(LEDMASK_USB_READY);
289 }
290
291 /* Manage ARP cache refreshing */
292 if (timer_expired(&ARPTimer))
293 {
294 timer_reset(&ARPTimer);
295 uip_arp_timer();
296 }
297 }
298
Imprint / Impressum