Update Studio Integration DLL, to include package logging.
[pub/USBasp.git] / Projects / Webserver / Lib / uIPManagement.c
index 1270e18..692da0d 100644 (file)
@@ -1,24 +1,24 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
-              
+     Copyright (C) Dean Camera, 2015.
+
   dean [at] fourwalledcubicle [dot] com
-      www.fourwalledcubicle.com
+           www.lufa-lib.org
 */
 
 /*
-  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2015  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
-  Permission to use, copy, modify, distribute, and sell this 
+  Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
-  without fee, provided that the above copyright notice appear in 
+  without fee, provided that the above copyright notice appear in
   all copies and that both that the copyright notice and this
-  permission notice and warranty disclaimer appear in supporting 
-  documentation, and that the name of the author not be used in 
-  advertising or publicity pertaining to distribution of the 
+  permission notice and warranty disclaimer appear in supporting
+  documentation, and that the name of the author not be used in
+  advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
-  The author disclaim all warranties with regard to this
+  The author disclaims all warranties with regard to this
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
 #include "uIPManagement.h"
 
 /** Connection timer, to retain the time elapsed since the last time the uIP connections were managed. */
-struct timer ConnectionTimer;
+static struct timer ConnectionTimer;
 
 /** ARP timer, to retain the time elapsed since the ARP cache was last updated. */
-struct timer ARPTimer;
+static struct timer ARPTimer;
 
-/** MAC address of the RNDIS device, when enumerated */
+/** MAC address of the RNDIS device, when enumerated. */
 struct uip_eth_addr MACAddress;
 
-bool HaveIPConfiguration;
 
-/** Configures the uIP stack ready for network traffic. */
+/** Configures the uIP stack ready for network traffic processing. */
 void uIPManagement_Init(void)
 {
        /* uIP Timing Initialization */
        clock_init();
        timer_set(&ConnectionTimer, CLOCK_SECOND / 2);
-       timer_set(&ARPTimer, CLOCK_SECOND * 10);        
+       timer_set(&ARPTimer, CLOCK_SECOND * 10);
 
        /* uIP Stack Initialization */
        uip_init();
        uip_arp_init();
-       uip_setethaddr(MACAddress);
 
        /* DHCP/Server IP Settings Initialization */
-       #if defined(ENABLE_DHCP_CLIENT)
-       HaveIPConfiguration = false;
-       DHCPClientApp_Init();
-       #else
-       HaveIPConfiguration = true;
-       uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
-       uip_ipaddr(&IPAddress,        DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
-       uip_ipaddr(&Netmask,          DEVICE_NETMASK[0],    DEVICE_NETMASK[1],    DEVICE_NETMASK[2],    DEVICE_NETMASK[3]);
-       uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0],    DEVICE_GATEWAY[1],    DEVICE_GATEWAY[2],    DEVICE_GATEWAY[3]);
-       uip_sethostaddr(&IPAddress);
-       uip_setnetmask(&Netmask);
-       uip_setdraddr(&GatewayIPAddress);
-       #endif
-       
+       if (USB_CurrentMode == USB_MODE_Device)
+       {
+               MACAddress.addr[0] = SERVER_MAC_ADDRESS[0];
+               MACAddress.addr[1] = SERVER_MAC_ADDRESS[1];
+               MACAddress.addr[2] = SERVER_MAC_ADDRESS[2];
+               MACAddress.addr[3] = SERVER_MAC_ADDRESS[3];
+               MACAddress.addr[4] = SERVER_MAC_ADDRESS[4];
+               MACAddress.addr[5] = SERVER_MAC_ADDRESS[5];
+
+               #if defined(ENABLE_DHCP_SERVER)
+               DHCPServerApp_Init();
+               #endif
+
+               uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
+               uip_ipaddr(&IPAddress,        DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
+               uip_ipaddr(&Netmask,          DEVICE_NETMASK[0],    DEVICE_NETMASK[1],    DEVICE_NETMASK[2],    DEVICE_NETMASK[3]);
+               uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0],    DEVICE_GATEWAY[1],    DEVICE_GATEWAY[2],    DEVICE_GATEWAY[3]);
+               uip_sethostaddr(&IPAddress);
+               uip_setnetmask(&Netmask);
+               uip_setdraddr(&GatewayIPAddress);
+       }
+       else
+       {
+               #if defined(ENABLE_DHCP_CLIENT)
+               DHCPClientApp_Init();
+               #else
+               uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;
+               uip_ipaddr(&IPAddress,        DEVICE_IP_ADDRESS[0], DEVICE_IP_ADDRESS[1], DEVICE_IP_ADDRESS[2], DEVICE_IP_ADDRESS[3]);
+               uip_ipaddr(&Netmask,          DEVICE_NETMASK[0],    DEVICE_NETMASK[1],    DEVICE_NETMASK[2],    DEVICE_NETMASK[3]);
+               uip_ipaddr(&GatewayIPAddress, DEVICE_GATEWAY[0],    DEVICE_GATEWAY[1],    DEVICE_GATEWAY[2],    DEVICE_GATEWAY[3]);
+               uip_sethostaddr(&IPAddress);
+               uip_setnetmask(&Netmask);
+               uip_setdraddr(&GatewayIPAddress);
+               #endif
+       }
+
+       /* Virtual Webserver Ethernet Address Configuration */
+       uip_setethaddr(MACAddress);
+
        /* HTTP Webserver Initialization */
        HTTPServerApp_Init();
-       
+
        /* TELNET Server Initialization */
        #if defined(ENABLE_TELNET_SERVER)
        TELNETServerApp_Init();
@@ -90,7 +113,8 @@ void uIPManagement_Init(void)
  */
 void uIPManagement_ManageNetwork(void)
 {
-       if ((USB_CurrentMode == USB_MODE_Host) && (USB_HostState == HOST_STATE_Configured))
+       if (((USB_CurrentMode == USB_MODE_Host)   && (USB_HostState   == HOST_STATE_Configured)) ||
+           ((USB_CurrentMode == USB_MODE_Device) && (USB_DeviceState == DEVICE_STATE_Configured)))
        {
                uIPManagement_ProcessIncomingPacket();
                uIPManagement_ManageConnections();
@@ -124,23 +148,45 @@ void uIPManagement_UDPCallback(void)
        /* Call the correct UDP application based on the port number the connection is listening on */
        switch (uip_udp_conn->lport)
        {
-               case HTONS(DHCPC_CLIENT_PORT):
+               #if defined(ENABLE_DHCP_CLIENT)
+               case HTONS(DHCP_CLIENT_PORT):
                        DHCPClientApp_Callback();
                        break;
+               #endif
+               #if defined(ENABLE_DHCP_SERVER)
+               case HTONS(DHCP_SERVER_PORT):
+                       DHCPServerApp_Callback();
+                       break;
+               #endif
        }
 }
 
 /** Processes Incoming packets to the server from the connected RNDIS device, creating responses as needed. */
 static void uIPManagement_ProcessIncomingPacket(void)
 {
-       /* If no packet received, exit processing routine */
-       if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface)))
-         return;
-         
-       LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
+       /* Determine which USB mode the system is currently initialized in */
+       if (USB_CurrentMode == USB_MODE_Device)
+       {
+               /* If no packet received, exit processing routine */
+               if (!(RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface_Device)))
+                 return;
+
+               LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
 
-       /* Read the Incoming packet straight into the UIP packet buffer */
-       RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, uip_buf, &uip_len);
+               /* Read the Incoming packet straight into the UIP packet buffer */
+               RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, &uip_len);
+       }
+       else
+       {
+               /* If no packet received, exit processing routine */
+               if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface_Host)))
+                 return;
+
+               LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
+
+               /* Read the Incoming packet straight into the UIP packet buffer */
+               RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, &uip_len);
+       }
 
        /* If the packet contains an Ethernet frame, process it */
        if (uip_len > 0)
@@ -162,21 +208,21 @@ static void uIPManagement_ProcessIncomingPacket(void)
 
                                        uip_split_output();
                                }
-                               
+
                                break;
                        case HTONS(UIP_ETHTYPE_ARP):
                                /* Process ARP packet */
                                uip_arp_arpin();
-                               
+
                                /* If a response was generated, send it */
                                if (uip_len > 0)
                                  uip_split_output();
-                               
+
                                break;
                }
        }
 
-       LEDs_SetAllLEDs(LEDMASK_USB_READY | ((HaveIPConfiguration) ? LEDMASK_UIP_READY_CONFIG : LEDMASK_UIP_READY_NOCONFIG));
+       LEDs_SetAllLEDs(LEDMASK_USB_READY);
 }
 
 /** Manages the currently open network connections, including TCP and (if enabled) UDP. */
@@ -204,7 +250,7 @@ static void uIPManagement_ManageConnections(void)
                timer_reset(&ConnectionTimer);
 
                LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
-               
+
                for (uint8_t i = 0; i < UIP_CONNS; i++)
                {
                        /* Run periodic connection management for each TCP connection */
@@ -220,7 +266,7 @@ static void uIPManagement_ManageConnections(void)
                                uip_split_output();
                        }
                }
-               
+
                #if defined(ENABLE_DHCP_CLIENT)
                for (uint8_t i = 0; i < UIP_UDP_CONNS; i++)
                {
@@ -249,3 +295,4 @@ static void uIPManagement_ManageConnections(void)
                uip_arp_timer();
        }
 }
+