Minor fixups to the documentation and preprocessor tokens.
[pub/lufa.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 Managament 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 / 8);
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 defined(ENABLE_DHCP)
64 DHCPApp_Init();
65 #else
66 uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
67 uip_ipaddr(&IPAddress, DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
68 uip_ipaddr(&Netmask, DEVICE_NETMASK[0], DEVICE_NETMASK[1], DEVICE_NETMASK[2], DEVICE_NETMASK[3]);
69 uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0], DEVICE_GATEWAY[1], DEVICE_GATEWAY[2], DEVICE_GATEWAY[3]);
70 uip_sethostaddr(&IPAddress);
71 uip_setnetmask(&Netmask);
72 uip_setdraddr(&GatewayIPAddress);
73 #endif
74
75 uip_setethaddr(MACAddress);
76
77 /* HTTP Webserver Initialization */
78 WebserverApp_Init();
79 }
80
81 /** uIP Management function. This function manages the uIP stack when called while an RNDIS device has been
82 * attached to the system.
83 */
84 void uIPManagement_ManageNetwork(void)
85 {
86 if ((USB_CurrentMode == USB_MODE_HOST) && (USB_HostState == HOST_STATE_Configured))
87 {
88 uIPManagement_ProcessIncommingPacket();
89 uIPManagement_ManageConnections();
90 }
91 }
92
93 /** Processes incomming packets to the server from the connected RNDIS device, creating responses as needed. */
94 static void uIPManagement_ProcessIncommingPacket(void)
95 {
96 /* If no packet received, exit processing routine */
97 if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface)))
98 return;
99
100 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
101
102 /* Read the incomming packet straight into the UIP packet buffer */
103 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, uip_buf, &uip_len);
104
105 /* If the packet contains an Ethernet frame, process it */
106 if (uip_len > 0)
107 {
108 switch (((struct uip_eth_hdr*)uip_buf)->type)
109 {
110 case HTONS(UIP_ETHTYPE_IP):
111 /* Filter packet by MAC destination */
112 uip_arp_ipin();
113
114 /* Process incomming packet */
115 uip_input();
116
117 /* If a response was generated, send it */
118 if (uip_len > 0)
119 {
120 /* Add destination MAC to outgoing packet */
121 uip_arp_out();
122
123 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
124 }
125
126 break;
127 case HTONS(UIP_ETHTYPE_ARP):
128 /* Process ARP packet */
129 uip_arp_arpin();
130
131 /* If a response was generated, send it */
132 if (uip_len > 0)
133 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
134
135 break;
136 }
137 }
138
139 LEDs_SetAllLEDs(LEDMASK_USB_READY);
140 }
141
142 /** Manages the currently open network connections, including TCP and (if enabled) UDP. */
143 static void uIPManagement_ManageConnections(void)
144 {
145 /* Poll TCP connections for more data to send back to the host */
146 for (uint8_t i = 0; i < UIP_CONNS; i++)
147 {
148 uip_poll_conn(&uip_conns[i]);
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
160 /* Manage open connections for timeouts */
161 if (timer_expired(&ConnectionTimer))
162 {
163 timer_reset(&ConnectionTimer);
164
165 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
166
167 for (uint8_t i = 0; i < UIP_CONNS; i++)
168 {
169 /* Run periodic connection management for each TCP connection */
170 uip_periodic(i);
171
172 /* If a response was generated, send it */
173 if (uip_len > 0)
174 {
175 /* Add destination MAC to outgoing packet */
176 uip_arp_out();
177
178 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
179 }
180 }
181
182 #if defined(ENABLE_DHCP)
183 for (uint8_t i = 0; i < UIP_UDP_CONNS; i++)
184 {
185 /* Run periodic connection management for each UDP connection */
186 uip_udp_periodic(i);
187
188 /* If a response was generated, send it */
189 if (uip_len > 0)
190 {
191 /* Add destination MAC to outgoing packet */
192 uip_arp_out();
193
194 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);
195 }
196 }
197 #endif
198
199 LEDs_SetAllLEDs(LEDMASK_USB_READY);
200 }
201
202 /* Manage ARP cache refreshing */
203 if (timer_expired(&ARPTimer))
204 {
205 timer_reset(&ARPTimer);
206 uip_arp_timer();
207 }
208 }