]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/lwIP_MCF5235_GCC/web.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / lwIP_MCF5235_GCC / web.c
diff --git a/FreeRTOS/Demo/lwIP_MCF5235_GCC/web.c b/FreeRTOS/Demo/lwIP_MCF5235_GCC/web.c
new file mode 100644 (file)
index 0000000..87e88d0
--- /dev/null
@@ -0,0 +1,238 @@
+/*\r
+    FreeRTOS V4.6.1 - copyright (C) 2003-2006 Richard Barry.\r
+\r
+    This file is part of the FreeRTOS distribution.\r
+\r
+    FreeRTOS is free software; you can redistribute it and/or modify\r
+    it under the terms of the GNU General Public License** as published by\r
+    the Free Software Foundation; either version 2 of the License, or\r
+    (at your option) any later version.\r
+\r
+    FreeRTOS is distributed in the hope that it will be useful,\r
+    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+    GNU General Public License for more details.\r
+\r
+    You should have received a copy of the GNU General Public License\r
+    along with FreeRTOS; if not, write to the Free Software\r
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+\r
+    A special exception to the GPL can be applied should you wish to distribute\r
+    a combined work that includes FreeRTOS, without being obliged to provide\r
+    the source code for any proprietary components.  See the licensing section\r
+    of http://www.FreeRTOS.org for full details of how and when the exception\r
+    can be applied.\r
+\r
+    ***************************************************************************\r
+    ***************************************************************************\r
+    *                                                                         *\r
+    * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
+       *                                                                         *\r
+       * This is a concise, step by step, 'hands on' guide that describes both   *\r
+       * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
+       * explains numerous examples that are written using the FreeRTOS API.     *\r
+       * Full source code for all the examples is provided in an accompanying    *\r
+       * .zip file.                                                              *\r
+    *                                                                         *\r
+    ***************************************************************************\r
+    ***************************************************************************\r
+\r
+       Please ensure to read the configuration and relevant port sections of the\r
+       online documentation.\r
+\r
+       http://www.FreeRTOS.org - Documentation, latest information, license and \r
+       contact details.\r
+\r
+       http://www.SafeRTOS.com - A version that is certified for use in safety \r
+       critical systems.\r
+\r
+       http://www.OpenRTOS.com - Commercial support, development, porting, \r
+       licensing and training services.\r
+*/\r
+\r
+/*\r
+    Implements a simplistic WEB server.  Every time a connection is made and\r
+    data is received a dynamic page that shows the current TCP/IP statistics\r
+    is generated and returned.  The connection is then closed.\r
+\r
+    This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
+    party.\r
+*/\r
+\r
+/* ------------------------ System includes ------------------------------- */\r
+#include <stdio.h>\r
+#include <string.h>\r
+\r
+/* ------------------------ FreeRTOS includes ----------------------------- */\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+#include "semphr.h"\r
+\r
+/* ------------------------ lwIP includes --------------------------------- */\r
+#include "lwip/api.h"\r
+#include "lwip/tcpip.h"\r
+#include "lwip/memp.h"\r
+#include "lwip/stats.h"\r
+#include "netif/loopif.h"\r
+\r
+/* ------------------------ Project includes ------------------------------ */\r
+#include "mcf5xxx.h"\r
+#include "mcf523x.h"\r
+#include "netif/fec.h"\r
+\r
+#include "web.h"\r
+\r
+/* ------------------------ Defines --------------------------------------- */\r
+/* The size of the buffer in which the dynamic WEB page is created. */\r
+#define webMAX_PAGE_SIZE        ( 2048 )\r
+\r
+/* Standard GET response. */\r
+#define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
+\r
+/* The port on which we listen. */\r
+#define webHTTP_PORT            ( 80 )\r
+\r
+/* Delay on close error. */\r
+#define webSHORT_DELAY          ( 10 )\r
+\r
+/* Format of the dynamic page that is returned on each connection. */\r
+#define webHTML_START \\r
+"<html>\\r
+<head>\\r
+</head>\\r
+<BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
+\r\nPage Hits = "\r
+\r
+#define webHTML_END \\r
+"\r\n" \\r
+"FreeRTOS MCF5235 port (c) 2006 by Christian Walter &lt;wolti@sil.at&gt;\r\n" \\r
+"</pre>\r\n" \\r
+"</BODY>\r\n" \\r
+"</html>"\r
+\r
+/* ------------------------ Prototypes ------------------------------------ */\r
+static void     vProcessConnection( struct netconn *pxNetCon );\r
+\r
+/*------------------------------------------------------------*/\r
+\r
+/*\r
+ * Process an incoming connection on port 80.\r
+ *\r
+ * This simply checks to see if the incoming data contains a GET request, and\r
+ * if so sends back a single dynamically created page.  The connection is then\r
+ * closed.  A more complete implementation could create a task for each\r
+ * connection.\r
+ */\r
+static void\r
+vProcessConnection( struct netconn *pxNetCon )\r
+{\r
+    static char cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];\r
+    struct netbuf  *pxRxBuffer;\r
+    char       *pcRxString;\r
+    unsigned short usLength;\r
+    static unsigned long ulPageHits = 0;\r
+\r
+    /* We expect to immediately get data. */\r
+    pxRxBuffer = netconn_recv( pxNetCon );\r
+\r
+    if( pxRxBuffer != NULL )\r
+    {\r
+        /* Where is the data? */\r
+        netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );\r
+\r
+        /* Is this a GET?  We don't handle anything else. */\r
+        if( !strncmp( pcRxString, "GET", 3 ) )\r
+        {\r
+            pcRxString = cDynamicPage;\r
+\r
+            /* Update the hit count. */\r
+            ulPageHits++;\r
+            sprintf( cPageHits, "%lu", ulPageHits );\r
+\r
+            /* Write out the HTTP OK header. */\r
+            netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );\r
+\r
+            /* Generate the dynamic page...\r
+\r
+               ... First the page header. */\r
+            strcpy( cDynamicPage, webHTML_START );\r
+            /* ... Then the hit count... */\r
+            strcat( cDynamicPage, cPageHits );\r
+            strcat( cDynamicPage,\r
+                    "<p><pre>Task          State  Priority  Stack #<br>************************************************<br>" );\r
+            /* ... Then the list of tasks and their status... */\r
+            vTaskList( ( signed char * )cDynamicPage + strlen( cDynamicPage ) );\r
+            /* ... Finally the page footer. */\r
+            strcat( cDynamicPage, webHTML_END );\r
+\r
+            /* Write out the dynamically generated page. */\r
+            netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );\r
+        }\r
+\r
+        netbuf_delete( pxRxBuffer );\r
+    }\r
+\r
+    netconn_close( pxNetCon );\r
+}\r
+\r
+/*------------------------------------------------------------*/\r
+\r
+void\r
+vlwIPInit( void )\r
+{\r
+    /* Initialize lwIP and its interface layer. */\r
+    sys_init(  );\r
+    mem_init(  );\r
+    memp_init(  );\r
+    pbuf_init(  );\r
+    netif_init(  );\r
+    ip_init(  );\r
+    tcpip_init( NULL, NULL );\r
+}\r
+\r
+/*------------------------------------------------------------*/\r
+\r
+void\r
+vBasicWEBServer( void *pvParameters )\r
+{\r
+    struct netconn *pxHTTPListener, *pxNewConnection;\r
+    struct ip_addr  xIpAddr, xNetMast, xGateway;\r
+    static struct netif fec523x_if;\r
+\r
+    /* Parameters are not used - suppress compiler error. */\r
+    ( void )pvParameters;\r
+\r
+    /* Create and configure the EMAC interface. */\r
+    IP4_ADDR( &xIpAddr, 10, 0, 10, 2 );\r
+    IP4_ADDR( &xNetMast, 255, 255, 255, 0 );\r
+    IP4_ADDR( &xGateway, 10, 0, 10, 1 );\r
+    netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, mcf523xfec_init, tcpip_input );\r
+\r
+    /* make it the default interface */\r
+    netif_set_default( &fec523x_if );\r
+\r
+    /* bring it up */\r
+    netif_set_up( &fec523x_if );\r
+\r
+    /* Create a new tcp connection handle */\r
+    pxHTTPListener = netconn_new( NETCONN_TCP );\r
+    netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );\r
+    netconn_listen( pxHTTPListener );\r
+\r
+    /* Loop forever */\r
+    for( ;; )\r
+    {\r
+        /* Wait for connection. */\r
+        pxNewConnection = netconn_accept( pxHTTPListener );\r
+\r
+        if( pxNewConnection != NULL )\r
+        {\r
+            /* Service connection. */\r
+            vProcessConnection( pxNewConnection );\r
+            while( netconn_delete( pxNewConnection ) != ERR_OK )\r
+            {\r
+                vTaskDelay( webSHORT_DELAY );\r
+            }\r
+        }\r
+    }\r
+}\r