- Main Page
- Data Structures
- Files

# BasicWEB.c

Go to the documentation of this file.

    00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
    00015 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00016  *
    00017  * Redistribution and use in source and binary forms, with or without
    00018  * modification, are permitted provided that the following conditions are met:
    00019  *
    00020  * 1. Redistributions of source code must retain the above copyright notice, this
    00021  * list of conditions and the following disclaimer.
    00022  *
    00023  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00024  * this list of conditions and the following disclaimer in the documentation
    00025  * and/or other materials provided with the distribution.
    00026  *
    00027  * 3. The name of Atmel may not be used to endorse or promote products derived
    00028  * from this software without specific prior written permission.
    00029  *
    00030  * 4. This software may only be redistributed and used in connection with an Atmel
    00031  * AVR product.
    00032  *
    00033  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00034  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00035  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
    00036  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
    00037  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00038  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00039  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00040  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00041  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00042  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
    00043  *
    00044  */
    00045
    00046 /*
    00047   Implements a simplistic WEB server.  Every time a connection is made and
    00048   data is received a dynamic page that shows the current FreeRTOS.org kernel
    00049   statistics is generated and returned.  The connection is then closed.
    00050
    00051   This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
    00052   party.
    00053 */
    00054
    00055 #if (HTTP_USED == 1)
    00056
    00057
    00058 /* Standard includes. */
    00059 #include <stdio.h>
    00060 #include <string.h>
    00061
    00062 #include "conf_eth.h"
    00063
    00064 /* Scheduler includes. */
    00065 #include "FreeRTOS.h"
    00066 #include "task.h"
    00067 #include "semphr.h"
    00068 #include "partest.h"
    00069 #include "serial.h"
    00070
    00071 /* Demo includes. */
    00072 /* Demo app includes. */
    00073 #include "portmacro.h"
    00074
    00075 /* lwIP includes. */
    00076 #include "lwip/api.h"
    00077 #include "lwip/tcpip.h"
    00078 #include "lwip/memp.h"
    00079 #include "lwip/stats.h"
    00080 #include "netif/loopif.h"
    00081
    00082 /* ethernet includes */
    00083 #include "ethernet.h"
    00084
    00086 #define webMAX_PAGE_SIZE    512
    00087
    00089 #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
    00090
    00092 #define webHTTP_PORT        ( 80 )
    00093
    00095 #define webSHORT_DELAY      ( 10 )
    00096
    00098 #define webHTML_START \
    00099 "<html>\
    00100 <head>\
    00101 </head>\
    00102 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\" bgcolor=\"#FFFFFF\" text=\"#2477E6\">\
    00103 \r\nPage Hits = "
    00104
    00105 #define webHTML_END \
    00106 "\r\n</pre>\
    00107 \r\n</font></BODY>\
    00108 </html>"
    00109
    00110 portCHAR cDynamicPage[ webMAX_PAGE_SIZE ];
    00111 portCHAR cPageHits[ 11 ];
    00112
    00113
    00115 static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon );
    00116
    00117
    00124 portTASK_FUNCTION( vBasicWEBServer, pvParameters )
    00125 {
    00126 struct netconn *pxHTTPListener, *pxNewConnection;
    00127
    00128     /* Create a new tcp connection handle */
    00129     pxHTTPListener = netconn_new( NETCONN_TCP );
    00130     netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
    00131     netconn_listen( pxHTTPListener );
    00132
    00133     /* Loop forever */
    00134     for( ;; )
    00135     {
    00136         /* Wait for a first connection. */
    00137         pxNewConnection = netconn_accept(pxHTTPListener);
    00138         vParTestSetLED(webCONN_LED, pdTRUE);
    00139
    00140         if(pxNewConnection != NULL)
    00141         {
    00142             prvweb_ParseHTMLRequest(pxNewConnection);
    00143         }/* end if new connection */
    00144
    00145         vParTestSetLED(webCONN_LED, pdFALSE);
    00146
    00147     } /* end infinite loop */
    00148 }
    00149
    00150
    00157 static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon )
    00158 {
    00159 struct netbuf *pxRxBuffer;
    00160 portCHAR *pcRxString;
    00161 unsigned portSHORT usLength;
    00162 static unsigned portLONG ulPageHits = 0;
    00163
    00164     /* We expect to immediately get data. */
    00165     pxRxBuffer = netconn_recv( pxNetCon );
    00166
    00167     if( pxRxBuffer != NULL )
    00168     {
    00169         /* Where is the data? */
    00170         netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
    00171
    00172         /* Is this a GET?  We don't handle anything else. */
    00173         if(( NULL != pcRxString               )
    00174         && ( !strncmp( pcRxString, "GET", 3 ) ))
    00175         {
    00176             /* Update the hit count. */
    00177             ulPageHits++;
    00178             sprintf( cPageHits, "%d", (int)ulPageHits );
    00179
    00180             /* Write out the HTTP OK header. */
    00181             netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );
    00182
    00183             /* Generate the dynamic page... First the page header. */
    00184             strcpy( cDynamicPage, webHTML_START );
    00185
    00186             /* ... Then the hit count... */
    00187             strcat( cDynamicPage, cPageHits );
    00188             strcat( cDynamicPage, "<p><pre>Task          State  Priority  Stack #<br>************************************************<br>" );
    00189
    00190             /* ... Then the list of tasks and their status... */
    00191             vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) );
    00192
    00193             /* ... Finally the page footer. */
    00194             strcat( cDynamicPage, webHTML_END );
    00195
    00196             /* Write out the dynamically generated page. */
    00197             netconn_write( pxNetCon, cDynamicPage, (u16_t) strlen( cDynamicPage ), NETCONN_COPY );
    00198         }
    00199         netbuf_delete( pxRxBuffer );
    00200     }
    00201
    00202     netconn_close( pxNetCon );
    00203     netconn_delete( pxNetCon );
    00204 }
    00205
    00206 #endif
    00207