Fix up the incomplete Webserver project so that it integrates with the uIP stack...
[pub/USBasp.git] / Projects / Incomplete / Webserver / Webserver.c
index c706fd0..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
- *  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
@@ -57,20 +57,13 @@ USB_ClassInfo_RNDIS_Host_t Ethernet_RNDIS_Interface =
                        },\r
        };\r
 \r
-struct timer ConnectionTimer, ARPTimer;\r
-uint16_t MillisecondTickCount;\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
-/** ISR for the management of the connection management timeout counter */\r
-ISR(TIMER0_COMPA_vect, ISR_BLOCK)\r
-{\r
-       MillisecondTickCount++;\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
@@ -129,7 +122,7 @@ int main(void)
                                \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
@@ -157,6 +150,8 @@ int main(void)
 \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
@@ -179,40 +174,39 @@ void ProcessIncommingPacket(void)
                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
+               printf("L=%d R=%d\r\n", uip_len, RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, &uip_buf[0], &uip_len));\r
 \r
-               struct uip_eth_hdr* EthernetHeader = (struct uip_eth_hdr*)&uip_buf[0];\r
-               if (EthernetHeader->type == HTONS(UIP_ETHTYPE_IP))\r
+               if (uip_len > 0)\r
                {\r
-                       /* Filter packet by MAC destination */\r
-                       uip_arp_ipin();\r
+                       bool PacketHandled = true;\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
-                       /* 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
+                               /* Process incomming packet */\r
+                               uip_input();\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
-               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
@@ -234,7 +228,7 @@ void ManageConnections(void)
 \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
                LEDs_SetAllLEDs(LEDMASK_USB_READY);\r