Fix up the incomplete Webserver project so that it integrates with the uIP stack...
[pub/USBasp.git] / Projects / Incomplete / Webserver / Webserver.c
index e6ef9f0..74636b8 100644 (file)
@@ -31,7 +31,7 @@
 /** \file\r
  *\r
  *  Main source file for the Webserver project. This file contains the main tasks of\r
 /** \file\r
  *\r
  *  Main source file for the Webserver project. This file contains the main tasks of\r
- *  the demo and is responsible for the initial application hardware configuration.\r
+ *  the project and is responsible for the initial application hardware configuration.\r
  */\r
  \r
 #include "Webserver.h"\r
  */\r
  \r
 #include "Webserver.h"\r
@@ -57,20 +57,13 @@ USB_ClassInfo_RNDIS_Host_t Ethernet_RNDIS_Interface =
                        },\r
        };\r
 \r
                        },\r
        };\r
 \r
-volatile uint8_t uIPManagementTimeout;\r
+/** Connection timer, to retain the time elapsed since the last time the uIP connections were managed. */\r
+struct timer ConnectionTimer;\r
+\r
+/** ARP timer, to retain the time elapsed since the ARP cache was last updated. */\r
+struct timer ARPTimer;\r
 \r
 \r
-/** ISR for the management of the connection management timeout counter */\r
-ISR(TIMER0_COMPA_vect, ISR_BLOCK)\r
-{\r
-       if (uIPManagementTimeout)\r
-         uIPManagementTimeout--;\r
-}\r
 \r
 \r
-void TCPCallback(void)\r
-{\r
-       printf("Callback!\r\n");\r
-}\r
-       \r
 /** Main program entry point. This routine configures the hardware required by the application, then\r
  *  enters a loop to run the application tasks in sequence.\r
  */\r
 /** Main program entry point. This routine configures the hardware required by the application, then\r
  *  enters a loop to run the application tasks in sequence.\r
  */\r
@@ -129,7 +122,7 @@ int main(void)
                                \r
                                printf("Device Max Transfer Size: %lu bytes.\r\n", Ethernet_RNDIS_Interface.State.DeviceMaxPacketSize);\r
                                \r
                                \r
                                printf("Device Max Transfer Size: %lu bytes.\r\n", Ethernet_RNDIS_Interface.State.DeviceMaxPacketSize);\r
                                \r
-                               uint32_t PacketFilter = (REMOTE_NDIS_PACKET_DIRECTED | REMOTE_NDIS_PACKET_BROADCAST | REMOTE_NDIS_PACKET_ALL_MULTICAST);\r
+                               uint32_t PacketFilter = (REMOTE_NDIS_PACKET_DIRECTED | REMOTE_NDIS_PACKET_BROADCAST);\r
                                if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface, OID_GEN_CURRENT_PACKET_FILTER,\r
                                                                &PacketFilter, sizeof(PacketFilter)) != HOST_SENDCONTROL_Successful)\r
                                {\r
                                if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface, OID_GEN_CURRENT_PACKET_FILTER,\r
                                                                &PacketFilter, sizeof(PacketFilter)) != HOST_SENDCONTROL_Successful)\r
                                {\r
@@ -152,11 +145,13 @@ int main(void)
                                }\r
 \r
                                printf("MAC Address: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\r\n",\r
                                }\r
 \r
                                printf("MAC Address: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\r\n",\r
-                                       MACAddress.addr[0], MACAddress.addr[1], MACAddress.addr[2],\r
-                                       MACAddress.addr[3], MACAddress.addr[4], MACAddress.addr[5]);\r
+                                      MACAddress.addr[0], MACAddress.addr[1], MACAddress.addr[2],\r
+                                      MACAddress.addr[3], MACAddress.addr[4], MACAddress.addr[5]);\r
 \r
                                uip_setethaddr(MACAddress);\r
                                \r
 \r
                                uip_setethaddr(MACAddress);\r
                                \r
+                               LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
+\r
                                printf("RNDIS Device Enumerated.\r\n");\r
                                USB_HostState = HOST_STATE_Configured;\r
                                break;\r
                                printf("RNDIS Device Enumerated.\r\n");\r
                                USB_HostState = HOST_STATE_Configured;\r
                                break;\r
@@ -179,41 +174,39 @@ void ProcessIncommingPacket(void)
                LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
 \r
                /* Read the incomming packet straight into the UIP packet buffer */\r
                LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
 \r
                /* Read the incomming packet straight into the UIP packet buffer */\r
-               RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, &uip_buf, &uip_len);\r
-               \r
-               printf("RECEIVED PACKET (%d):\r\n", uip_len);\r
-               for (uint16_t i = 0; i < uip_len; i++)\r
-                 printf("0x%02X ", uip_buf[i]);\r
-               printf("\r\n\r\n");\r
-\r
-               #define BUF ((struct uip_eth_hdr *)&uip_buf[0])\r
+               printf("L=%d R=%d\r\n", uip_len, RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], &uip_len));\r
 \r
 \r
-               if (BUF->type == HTONS(UIP_ETHTYPE_IP))\r
+               if (uip_len > 0)\r
                {\r
                {\r
-                       /* Filter packet by MAC destination */\r
-                       uip_arp_ipin();\r
+                       bool PacketHandled = true;\r
 \r
 \r
-                       /* Process incomming packet */\r
-                       uip_input();\r
+                       struct uip_eth_hdr* EthernetHeader = (struct uip_eth_hdr*)&uip_buf[0];\r
+                       if (EthernetHeader->type == HTONS(UIP_ETHTYPE_IP))\r
+                       {\r
+                               /* Filter packet by MAC destination */\r
+                               uip_arp_ipin();\r
 \r
 \r
-                       /* Add destination MAC to outgoing packet */\r
-                       if (uip_len > 0)\r
-                         uip_arp_out();\r
-               }\r
-               else if (BUF->type == HTONS(UIP_ETHTYPE_ARP))\r
-               {\r
-                       /* Process ARP packet */\r
-                       uip_arp_arpin();\r
-               }\r
+                               /* Process incomming packet */\r
+                               uip_input();\r
 \r
 \r
-               /* If a response was generated, send it */\r
-               if (uip_len > 0)\r
-                 RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf, uip_len);\r
+                               /* Add destination MAC to outgoing packet */\r
+                               if (uip_len > 0)\r
+                                 uip_arp_out();\r
+                       }\r
+                       else if (EthernetHeader->type == HTONS(UIP_ETHTYPE_ARP))\r
+                       {\r
+                               /* Process ARP packet */\r
+                               uip_arp_arpin();\r
+                       }\r
+                       else\r
+                       {\r
+                               PacketHandled = false;\r
+                       }\r
 \r
 \r
-               printf("SENT PACKET (%d):\r\n", uip_len);\r
-               for (uint16_t i = 0; i < uip_len; i++)\r
-                 printf("0x%02X ", uip_buf[i]);\r
-               printf("\r\n\r\n");\r
+                       /* If a response was generated, send it */\r
+                       if ((uip_len > 0) && PacketHandled)\r
+                         RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);\r
+               }\r
 \r
                LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
        }\r
 \r
                LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
        }\r
@@ -221,8 +214,11 @@ void ProcessIncommingPacket(void)
 \r
 void ManageConnections(void)\r
 {\r
 \r
 void ManageConnections(void)\r
 {\r
-       if (!(uIPManagementTimeout))\r
+       /* Manage open connections */\r
+       if (timer_expired(&ConnectionTimer))\r
        {\r
        {\r
+               timer_reset(&ConnectionTimer);\r
+\r
                LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                \r
                for (uint8_t i = 0; i < UIP_CONNS; i++)\r
                LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                \r
                for (uint8_t i = 0; i < UIP_CONNS; i++)\r
@@ -232,15 +228,18 @@ void ManageConnections(void)
 \r
                        /* If a response was generated, send it */\r
                        if (uip_len > 0)\r
 \r
                        /* If a response was generated, send it */\r
                        if (uip_len > 0)\r
-                         RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf, uip_len);\r
+                         RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], uip_len);\r
                }\r
                \r
                }\r
                \r
-               uip_arp_timer();\r
-               \r
-               uIPManagementTimeout = 250;\r
-\r
                LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
        }\r
                LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
        }\r
+\r
+       /* Manage ARP cache refreshing */\r
+       if (timer_expired(&ARPTimer))\r
+       {\r
+               timer_reset(&ARPTimer);\r
+               uip_arp_timer();\r
+       }\r
 }\r
 \r
 /** Configures the board hardware and chip peripherals for the demo's functionality. */\r
 }\r
 \r
 /** Configures the board hardware and chip peripherals for the demo's functionality. */\r
@@ -258,22 +257,21 @@ void SetupHardware(void)
        LEDs_Init();\r
        USB_Init();\r
 \r
        LEDs_Init();\r
        USB_Init();\r
 \r
-       /* Millisecond timer initialization for managing the command timeout counter */\r
-       OCR0A  = ((F_CPU / 64) / 1000);\r
-       TCCR0A = (1 << WGM01);\r
-       TCCR0B = ((1 << CS01) | (1 << CS00));\r
-       \r
+       /* uIP Timing Initialization */\r
+       clock_init();\r
+       timer_set(&ConnectionTimer, CLOCK_SECOND / 2);\r
+       timer_set(&ARPTimer, CLOCK_SECOND * 10);        \r
+\r
        /* uIP Stack Initialization */\r
        uip_init();\r
        /* uIP Stack Initialization */\r
        uip_init();\r
-\r
        uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;\r
        uip_ipaddr_t IPAddress, Netmask, GatewayIPAddress;\r
-       uip_ipaddr(&IPAddress, 192, 168, 1, 10);\r
-       uip_ipaddr(&Netmask, 0xFF, 0xFF, 0xFF, 0x00);\r
+       uip_ipaddr(&IPAddress,        192, 168, 1, 10);\r
+       uip_ipaddr(&Netmask,          255, 255, 255, 0);\r
        uip_ipaddr(&GatewayIPAddress, 192, 168, 1, 1);\r
        uip_sethostaddr(&IPAddress);\r
        uip_setnetmask(&Netmask);\r
        uip_setdraddr(&GatewayIPAddress);\r
        uip_ipaddr(&GatewayIPAddress, 192, 168, 1, 1);\r
        uip_sethostaddr(&IPAddress);\r
        uip_setnetmask(&Netmask);\r
        uip_setdraddr(&GatewayIPAddress);\r
-       \r
+               \r
        /* HTTP Webserver Initialization */\r
        uip_listen(HTONS(80));\r
 }\r
        /* HTTP Webserver Initialization */\r
        uip_listen(HTONS(80));\r
 }\r