Device mode class driver callbacks are now fired before the control request status...
[pub/USBasp.git] / Projects / Webserver / Lib / uIPManagement.c
index 44da609..45f8a6a 100644 (file)
@@ -30,7 +30,7 @@
 \r
 /** \file\r
  *\r
- *  uIP Managament functions. This file contains the functions and globals needed to maintain the uIP\r
+ *  uIP Management functions. This file contains the functions and globals needed to maintain the uIP\r
  *  stack once an RNDIS device has been attached to the system.\r
  */\r
 \r
@@ -46,22 +46,27 @@ struct timer ARPTimer;
 /** MAC address of the RNDIS device, when enumerated */\r
 struct uip_eth_addr MACAddress;\r
 \r
+bool HaveIPConfiguration;\r
 \r
 /** Configures the uIP stack ready for network traffic. */\r
 void uIPManagement_Init(void)\r
 {\r
        /* uIP Timing Initialization */\r
        clock_init();\r
-       timer_set(&ConnectionTimer, CLOCK_SECOND / 10);\r
+       timer_set(&ConnectionTimer, CLOCK_SECOND / 2);\r
        timer_set(&ARPTimer, CLOCK_SECOND * 10);        \r
 \r
        /* uIP Stack Initialization */\r
        uip_init();\r
+       uip_arp_init();\r
+       uip_setethaddr(MACAddress);\r
 \r
        /* DHCP/Server IP Settings Initialization */\r
-       #if defined(ENABLE_DHCP)\r
-       DHCPApp_Init();\r
+       #if defined(ENABLE_DHCP_CLIENT)\r
+       HaveIPConfiguration = false;\r
+       DHCPClientApp_Init();\r
        #else\r
+       HaveIPConfiguration = true;\r
        uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;\r
        uip_ipaddr(&IPAddress,        DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);\r
        uip_ipaddr(&Netmask,          DEVICE_NETMASK[0],    DEVICE_NETMASK[1],    DEVICE_NETMASK[2],    DEVICE_NETMASK[3]);\r
@@ -70,11 +75,14 @@ void uIPManagement_Init(void)
        uip_setnetmask(&Netmask);\r
        uip_setdraddr(&GatewayIPAddress);\r
        #endif\r
-\r
-       uip_setethaddr(MACAddress);\r
        \r
        /* HTTP Webserver Initialization */\r
-       WebserverApp_Init();\r
+       HTTPServerApp_Init();\r
+       \r
+       /* TELNET Server Initialization */\r
+       #if defined(ENABLE_TELNET_SERVER)\r
+       TELNETServerApp_Init();\r
+       #endif\r
 }\r
 \r
 /** uIP Management function. This function manages the uIP stack when called while an RNDIS device has been\r
@@ -84,55 +92,91 @@ void uIPManagement_ManageNetwork(void)
 {\r
        if ((USB_CurrentMode == USB_MODE_HOST) && (USB_HostState == HOST_STATE_Configured))\r
        {\r
-               uIPManagement_ProcessIncommingPacket();\r
+               uIPManagement_ProcessIncomingPacket();\r
                uIPManagement_ManageConnections();\r
        }\r
 }\r
 \r
-/** Processes incomming packets to the server from the connected RNDIS device, creating responses as needed. */\r
-static void uIPManagement_ProcessIncommingPacket(void)\r
+/** uIP TCP/IP network stack callback function for the processing of a given TCP connection. This routine dispatches\r
+ *  to the appropriate TCP protocol application based on the connection's listen port number.\r
+ */\r
+void uIPManagement_TCPCallback(void)\r
 {\r
-       if (RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface))\r
+       /* Call the correct TCP application based on the port number the connection is listening on */\r
+       switch (uip_conn->lport)\r
        {\r
-               LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
+               case HTONS(HTTP_SERVER_PORT):\r
+                       HTTPServerApp_Callback();\r
+                       break;\r
+               #if defined(ENABLE_TELNET_SERVER)\r
+               case HTONS(TELNET_SERVER_PORT):\r
+                       TELNETServerApp_Callback();\r
+                       break;\r
+               #endif\r
+       }\r
+}\r
 \r
-               /* Read the incomming packet straight into the UIP packet buffer */\r
-               RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, uip_buf, &uip_len);\r
+/** uIP TCP/IP network stack callback function for the processing of a given UDP connection. This routine dispatches\r
+ *  to the appropriate UDP protocol application based on the connection's listen port number.\r
+ */\r
+void uIPManagement_UDPCallback(void)\r
+{\r
+       /* Call the correct UDP application based on the port number the connection is listening on */\r
+       switch (uip_udp_conn->lport)\r
+       {\r
+               case HTONS(DHCPC_CLIENT_PORT):\r
+                       DHCPClientApp_Callback();\r
+                       break;\r
+       }\r
+}\r
 \r
-               if (uip_len > 0)\r
-               {\r
-                       bool PacketHandled = true;\r
+/** Processes Incoming packets to the server from the connected RNDIS device, creating responses as needed. */\r
+static void uIPManagement_ProcessIncomingPacket(void)\r
+{\r
+       /* If no packet received, exit processing routine */\r
+       if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface)))\r
+         return;\r
+         \r
+       LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
 \r
-                       struct uip_eth_hdr* EthernetHeader = (struct uip_eth_hdr*)uip_buf;\r
-                       if (EthernetHeader->type == HTONS(UIP_ETHTYPE_IP))\r
-                       {\r
+       /* Read the Incoming packet straight into the UIP packet buffer */\r
+       RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, uip_buf, &uip_len);\r
+\r
+       /* If the packet contains an Ethernet frame, process it */\r
+       if (uip_len > 0)\r
+       {\r
+               switch (((struct uip_eth_hdr*)uip_buf)->type)\r
+               {\r
+                       case HTONS(UIP_ETHTYPE_IP):\r
                                /* Filter packet by MAC destination */\r
                                uip_arp_ipin();\r
 \r
-                               /* Process incomming packet */\r
+                               /* Process Incoming packet */\r
                                uip_input();\r
 \r
-                               /* Add destination MAC to outgoing packet */\r
+                               /* If a response was generated, send it */\r
                                if (uip_len > 0)\r
-                                 uip_arp_out();\r
-                       }\r
-                       else if (EthernetHeader->type == HTONS(UIP_ETHTYPE_ARP))\r
-                       {\r
+                               {\r
+                                       /* Add destination MAC to outgoing packet */\r
+                                       uip_arp_out();\r
+\r
+                                       uip_split_output();\r
+                               }\r
+                               \r
+                               break;\r
+                       case HTONS(UIP_ETHTYPE_ARP):\r
                                /* Process ARP packet */\r
                                uip_arp_arpin();\r
-                       }\r
-                       else\r
-                       {\r
-                               PacketHandled = false;\r
-                       }\r
-\r
-                       /* If a response was generated, send it */\r
-                       if ((uip_len > 0) && PacketHandled)\r
-                         RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);\r
+                               \r
+                               /* If a response was generated, send it */\r
+                               if (uip_len > 0)\r
+                                 uip_split_output();\r
+                               \r
+                               break;\r
                }\r
-\r
-               LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
        }\r
+\r
+       LEDs_SetAllLEDs(LEDMASK_USB_READY | ((HaveIPConfiguration) ? LEDMASK_UIP_READY_CONFIG : LEDMASK_UIP_READY_NOCONFIG));\r
 }\r
 \r
 /** Manages the currently open network connections, including TCP and (if enabled) UDP. */\r
@@ -149,7 +193,8 @@ static void uIPManagement_ManageConnections(void)
                        /* Add destination MAC to outgoing packet */\r
                        uip_arp_out();\r
 \r
-                       RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);\r
+                       /* Split and send the outgoing packet */\r
+                       uip_split_output();\r
                }\r
        }\r
 \r
@@ -171,11 +216,12 @@ static void uIPManagement_ManageConnections(void)
                                /* Add destination MAC to outgoing packet */\r
                                uip_arp_out();\r
 \r
-                               RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);\r
+                               /* Split and send the outgoing packet */\r
+                               uip_split_output();\r
                        }\r
                }\r
                \r
-               #if defined(ENABLE_DHCP)\r
+               #if defined(ENABLE_DHCP_CLIENT)\r
                for (uint8_t i = 0; i < UIP_UDP_CONNS; i++)\r
                {\r
                        /* Run periodic connection management for each UDP connection */\r
@@ -187,7 +233,8 @@ static void uIPManagement_ManageConnections(void)
                                /* Add destination MAC to outgoing packet */\r
                                uip_arp_out();\r
 \r
-                               RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, uip_buf, uip_len);\r
+                               /* Split and send the outgoing packet */\r
+                               uip_split_output();\r
                        }\r
                }\r
                #endif\r