f0011937292782434eb2d0421eaba4d74c4f8b8c
3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
37 #define INCLUDE_FROM_UIPMANAGEMENT_C
38 #include "uIPManagement.h"
40 /** Connection timer, to retain the time elapsed since the last time the uIP connections were managed. */
41 struct timer ConnectionTimer
;
43 /** ARP timer, to retain the time elapsed since the ARP cache was last updated. */
44 struct timer ARPTimer
;
46 /** MAC address of the RNDIS device, when enumerated */
47 struct uip_eth_addr MACAddress
;
50 /** Configures the uIP stack ready for network traffic. */
51 void uIPManagement_Init(void)
53 /* uIP Timing Initialization */
55 timer_set(&ConnectionTimer
, CLOCK_SECOND
/ 100);
56 timer_set(&ARPTimer
, CLOCK_SECOND
* 10);
58 /* uIP Stack Initialization */
61 /* DHCP/Server IP Settings Initialization */
62 #if defined(ENABLE_DHCP)
65 uip_ipaddr_t IPAddress
, Netmask
, GatewayIPAddress
;
66 uip_ipaddr(&IPAddress
, DEVICE_IP_ADDRESS
[0], DEVICE_IP_ADDRESS
[1], DEVICE_IP_ADDRESS
[2], DEVICE_IP_ADDRESS
[3]);
67 uip_ipaddr(&Netmask
, DEVICE_NETMASK
[0], DEVICE_NETMASK
[1], DEVICE_NETMASK
[2], DEVICE_NETMASK
[3]);
68 uip_ipaddr(&GatewayIPAddress
, DEVICE_GATEWAY
[0], DEVICE_GATEWAY
[1], DEVICE_GATEWAY
[2], DEVICE_GATEWAY
[3]);
69 uip_sethostaddr(&IPAddress
);
70 uip_setnetmask(&Netmask
);
71 uip_setdraddr(&GatewayIPAddress
);
74 uip_setethaddr(MACAddress
);
76 /* HTTP Webserver Initialization */
80 /** uIP Management function. This function manages the uIP stack when called while an RNDIS device has been
81 * attached to the system.
83 void uIPManagement_ManageNetwork(void)
85 if ((USB_CurrentMode
== USB_MODE_HOST
) && (USB_HostState
== HOST_STATE_Configured
))
87 uIPManagement_ProcessIncommingPacket();
88 uIPManagement_ManageConnections();
92 /** Processes incomming packets to the server from the connected RNDIS device, creating responses as needed. */
93 static void uIPManagement_ProcessIncommingPacket(void)
95 if (RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface
))
97 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
99 /* Read the incomming packet straight into the UIP packet buffer */
100 RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface
, &uip_buf
[0], &uip_len
);
104 bool PacketHandled
= true;
106 struct uip_eth_hdr
* EthernetHeader
= (struct uip_eth_hdr
*)&uip_buf
[0];
107 if (EthernetHeader
->type
== HTONS(UIP_ETHTYPE_IP
))
109 /* Filter packet by MAC destination */
112 /* Process incomming packet */
115 /* Add destination MAC to outgoing packet */
119 else if (EthernetHeader
->type
== HTONS(UIP_ETHTYPE_ARP
))
121 /* Process ARP packet */
126 PacketHandled
= false;
129 /* If a response was generated, send it */
130 if ((uip_len
> 0) && PacketHandled
)
131 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface
, &uip_buf
[0], uip_len
);
134 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
138 /** Manages the currently open network connections, including TCP and (if enabled) UDP. */
139 static void uIPManagement_ManageConnections(void)
141 /* Manage open connections */
142 if (timer_expired(&ConnectionTimer
))
144 timer_reset(&ConnectionTimer
);
146 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
148 for (uint8_t i
= 0; i
< UIP_CONNS
; i
++)
150 /* Run periodic connection management for each TCP connection */
153 /* If a response was generated, send it */
156 /* Add destination MAC to outgoing packet */
159 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface
, &uip_buf
[0], uip_len
);
163 #if defined(ENABLE_DHCP)
164 for (uint8_t i
= 0; i
< UIP_UDP_CONNS
; i
++)
166 /* Run periodic connection management for each UDP connection */
169 /* If a response was generated, send it */
172 /* Add destination MAC to outgoing packet */
175 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface
, &uip_buf
[0], uip_len
);
180 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
183 /* Manage ARP cache refreshing */
184 if (timer_expired(&ARPTimer
))
186 timer_reset(&ARPTimer
);