]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_AVR32_UC3/NETWORK/BasicWEB/BasicWEB.c
d1e85ef494ba46e59c4a1717f449835878cddf44
[freertos] / FreeRTOS / Demo / lwIP_AVR32_UC3 / NETWORK / BasicWEB / BasicWEB.c
1 /*This file has been prepared for Doxygen automatic documentation generation.*/\r
2 /*! \file *********************************************************************\r
3  *\r
4  * \brief Basic WEB Server for AVR32 UC3.\r
5  *\r
6  * - Compiler:           GNU GCC for AVR32\r
7  * - Supported devices:  All AVR32 devices can be used.\r
8  * - AppNote:\r
9  *\r
10  * \author               Atmel Corporation: http://www.atmel.com \n\r
11  *                       Support and FAQ: http://support.atmel.no/\r
12  *\r
13  *****************************************************************************/\r
14 \r
15 /* Copyright (c) 2007, Atmel Corporation All rights reserved.\r
16  *\r
17  * Redistribution and use in source and binary forms, with or without\r
18  * modification, are permitted provided that the following conditions are met:\r
19  *\r
20  * 1. Redistributions of source code must retain the above copyright notice,\r
21  * this list of conditions and the following disclaimer.\r
22  *\r
23  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
24  * this list of conditions and the following disclaimer in the documentation\r
25  * and/or other materials provided with the distribution.\r
26  *\r
27  * 3. The name of ATMEL may not be used to endorse or promote products derived\r
28  * from this software without specific prior written permission.\r
29  *\r
30  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
31  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
32  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND\r
33  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,\r
34  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
35  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
37  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
39  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
40  */\r
41 \r
42 \r
43 /*\r
44   Implements a simplistic WEB server.  Every time a connection is made and\r
45   data is received a dynamic page that shows the current FreeRTOS.org kernel\r
46   statistics is generated and returned.  The connection is then closed.\r
47 \r
48   This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
49   party.\r
50 */\r
51 \r
52 #if (HTTP_USED == 1)\r
53 \r
54 \r
55 /* Standard includes. */\r
56 #include <stdio.h>\r
57 #include <string.h>\r
58 \r
59 #include "conf_eth.h"\r
60 \r
61 /* Scheduler includes. */\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 #include "semphr.h"\r
65 #include "partest.h"\r
66 #include "serial.h"\r
67 \r
68 /* Demo includes. */\r
69 /* Demo app includes. */\r
70 #include "portmacro.h"\r
71 \r
72 /* lwIP includes. */\r
73 #include "lwip/api.h"\r
74 #include "lwip/tcpip.h"\r
75 #include "lwip/memp.h"\r
76 #include "lwip/stats.h"\r
77 #include "netif/loopif.h"\r
78 \r
79 /* ethernet includes */\r
80 #include "ethernet.h"\r
81 \r
82 /*! The size of the buffer in which the dynamic WEB page is created. */\r
83 #define webMAX_PAGE_SIZE        512\r
84 \r
85 /*! Standard GET response. */\r
86 #define webHTTP_OK      "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
87 \r
88 /*! The port on which we listen. */\r
89 #define webHTTP_PORT            ( 80 )\r
90 \r
91 /*! Delay on close error. */\r
92 #define webSHORT_DELAY          ( 10 )\r
93 \r
94 /*! Format of the dynamic page that is returned on each connection. */\r
95 #define webHTML_START \\r
96 "<html>\\r
97 <head>\\r
98 </head>\\r
99 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\" bgcolor=\"#FFFFFF\" text=\"#2477E6\">\\r
100 \r\nPage Hits = "\r
101 \r
102 #define webHTML_END \\r
103 "\r\n</pre>\\r
104 \r\n</font></BODY>\\r
105 </html>"\r
106 \r
107 portCHAR cDynamicPage[ webMAX_PAGE_SIZE ];\r
108 portCHAR cPageHits[ 11 ];\r
109 \r
110 \r
111 /*! Function to process the current connection */\r
112 static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon );\r
113 \r
114 \r
115 /*! \brief WEB server main task\r
116  *         check for incoming connection and process it\r
117  *\r
118  *  \param pvParameters   Input. Not Used.\r
119  *\r
120  */\r
121 portTASK_FUNCTION( vBasicWEBServer, pvParameters )\r
122 {\r
123 struct netconn *pxHTTPListener, *pxNewConnection;\r
124 \r
125         /* Create a new tcp connection handle */\r
126         pxHTTPListener = netconn_new( NETCONN_TCP );\r
127         netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );\r
128         netconn_listen( pxHTTPListener );\r
129 \r
130         /* Loop forever */\r
131         for( ;; )\r
132         {\r
133                 /* Wait for a first connection. */\r
134                 pxNewConnection = netconn_accept(pxHTTPListener);\r
135                 vParTestSetLED(webCONN_LED, pdTRUE);\r
136 \r
137                 if(pxNewConnection != NULL)\r
138                 {\r
139                         prvweb_ParseHTMLRequest(pxNewConnection);\r
140                 }/* end if new connection */\r
141 \r
142                 vParTestSetLED(webCONN_LED, pdFALSE);\r
143 \r
144         } /* end infinite loop */\r
145 }\r
146 \r
147 \r
148 /*! \brief parse the incoming request\r
149  *         parse the HTML request and send file\r
150  *\r
151  *  \param pxNetCon   Input. The netconn to use to send and receive data.\r
152  *\r
153  */\r
154 static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon )\r
155 {\r
156 struct netbuf *pxRxBuffer;\r
157 portCHAR *pcRxString;\r
158 unsigned portSHORT usLength;\r
159 static unsigned portLONG ulPageHits = 0;\r
160 \r
161         /* We expect to immediately get data. */\r
162         pxRxBuffer = netconn_recv( pxNetCon );\r
163 \r
164         if( pxRxBuffer != NULL )\r
165         {\r
166                 /* Where is the data? */\r
167                 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );\r
168 \r
169                 /* Is this a GET?  We don't handle anything else. */\r
170                 if( !strncmp( pcRxString, "GET", 3 ) )\r
171                 {\r
172                         pcRxString = cDynamicPage;\r
173 \r
174                         /* Update the hit count. */\r
175                         ulPageHits++;\r
176                         sprintf( cPageHits, "%d", (int)ulPageHits );\r
177 \r
178                         /* Write out the HTTP OK header. */\r
179                         netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY );\r
180 \r
181                         /* Generate the dynamic page... First the page header. */\r
182                         strcpy( cDynamicPage, webHTML_START );\r
183 \r
184                         /* ... Then the hit count... */\r
185                         strcat( cDynamicPage, cPageHits );\r
186                         strcat( cDynamicPage, "<p><pre>Task          State  Priority  Stack     #<br>************************************************<br>" );\r
187 \r
188                         /* ... Then the list of tasks and their status... */\r
189                         vTaskList( cDynamicPage + strlen( cDynamicPage ) );\r
190 \r
191                         /* ... Finally the page footer. */\r
192                         strcat( cDynamicPage, webHTML_END );\r
193 \r
194                         /* Write out the dynamically generated page. */\r
195                         netconn_write( pxNetCon, cDynamicPage, (u16_t) strlen( cDynamicPage ), NETCONN_COPY );\r
196                 }\r
197                 netbuf_delete( pxRxBuffer );\r
198         }\r
199 \r
200         netconn_close( pxNetCon );\r
201         netconn_delete( pxNetCon );\r
202 }\r
203 \r
204 #endif\r
205 \r