]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF52259_CodeWarrior/HTTPDemo.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / ColdFire_MCF52259_CodeWarrior / HTTPDemo.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97     Implements a simplistic WEB server.  Every time a connection is made and\r
98     data is received a dynamic page that shows the current TCP/IP statistics\r
99     is generated and returned.  The connection is then closed.\r
100 \r
101     This file was adapted from a FreeRTOS lwIP slip demo supplied by a third\r
102     party.\r
103 */\r
104 \r
105 /* ------------------------ System includes ------------------------------- */\r
106 \r
107 \r
108 /* ------------------------ FreeRTOS includes ----------------------------- */\r
109 #include "FreeRTOS.h"\r
110 #include "task.h"\r
111 #include "semphr.h"\r
112 \r
113 /* ------------------------ lwIP includes --------------------------------- */\r
114 #include "lwip/api.h"\r
115 #include "lwip/tcpip.h"\r
116 #include "lwip/ip.h"\r
117 #include "lwip/memp.h"\r
118 #include "lwip/stats.h"\r
119 #include "netif/loopif.h"\r
120 \r
121 /* ------------------------ Project includes ------------------------------ */\r
122 #include "common.h"\r
123 \r
124 #include "HTTPDemo.h"\r
125 \r
126 /* ------------------------ Defines --------------------------------------- */\r
127 /* The size of the buffer in which the dynamic WEB page is created. */\r
128 #define webMAX_PAGE_SIZE        ( 1024 ) /*FSL: buffer containing array*/\r
129 \r
130 /* Standard GET response. */\r
131 #define webHTTP_OK  "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"\r
132 \r
133 /* The port on which we listen. */\r
134 #define webHTTP_PORT            ( 80 )\r
135 \r
136 /* Delay on close error. */\r
137 #define webSHORT_DELAY          ( 10 )\r
138 \r
139 /* Format of the dynamic page that is returned on each connection. */\r
140 #define webHTML_START \\r
141 "<html>\\r
142 <head>\\r
143 </head>\\r
144 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\\r
145 \r\n\r\nPage Hits = "\r
146 \r
147 #define webHTML_END \\r
148 "\r\n" \\r
149 "</pre>\r\n" \\r
150 "</BODY>\r\n" \\r
151 "</html>"\r
152 \r
153 #if INCLUDE_uxTaskGetStackHighWaterMark\r
154         static volatile unsigned portBASE_TYPE uxHighWaterMark_web = 0;\r
155 #endif\r
156 \r
157 /* ------------------------ Prototypes ------------------------------------ */\r
158 static void     vProcessConnection( struct netconn *pxNetCon );\r
159 \r
160 /*------------------------------------------------------------*/\r
161 \r
162 /*\r
163  * Process an incoming connection on port 80.\r
164  *\r
165  * This simply checks to see if the incoming data contains a GET request, and\r
166  * if so sends back a single dynamically created page.  The connection is then\r
167  * closed.  A more complete implementation could create a task for each\r
168  * connection.\r
169  */\r
170 static void vProcessConnection( struct netconn *pxNetCon )\r
171 {\r
172     static char cDynamicPage[webMAX_PAGE_SIZE], cPageHits[11];\r
173     struct netbuf  *pxRxBuffer;\r
174     char       *pcRxString;\r
175     unsigned short usLength;\r
176     static unsigned long ulPageHits = 0;\r
177 \r
178     /* We expect to immediately get data. */\r
179     pxRxBuffer = netconn_recv( pxNetCon );\r
180 \r
181     if( pxRxBuffer != NULL )\r
182     {\r
183         /* Where is the data? */\r
184         netbuf_data( pxRxBuffer, ( void * )&pcRxString, &usLength );\r
185 \r
186         /* Is this a GET?  We don't handle anything else. */\r
187         if( !strncmp( pcRxString, "GET", 3 ) )\r
188         {\r
189             pcRxString = cDynamicPage;\r
190 \r
191             /* Update the hit count. */\r
192             ulPageHits++;\r
193             sprintf( cPageHits, "%d", (int)ulPageHits );\r
194 \r
195             /* Write out the HTTP OK header. */\r
196             netconn_write( pxNetCon, webHTTP_OK, ( u16_t ) strlen( webHTTP_OK ), NETCONN_COPY );\r
197 \r
198             /* Generate the dynamic page...\r
199 \r
200                ... First the page header. */\r
201             strcpy( cDynamicPage, webHTML_START );\r
202             /* ... Then the hit count... */\r
203             strcat( cDynamicPage, cPageHits );\r
204 \r
205             strcat( cDynamicPage,\r
206                     "<p><pre>Task          State  Priority  Stack      #<br>************************************************<br>" );\r
207             /* ... Then the list of tasks and their status... */\r
208             vTaskList( cDynamicPage + strlen( cDynamicPage ) );\r
209 \r
210             /* ... Finally the page footer. */\r
211             strcat( cDynamicPage, webHTML_END );\r
212 \r
213             /* Write out the dynamically generated page. */\r
214             netconn_write( pxNetCon, cDynamicPage, ( u16_t ) strlen( cDynamicPage ), NETCONN_COPY );\r
215         }\r
216         netbuf_delete( pxRxBuffer );\r
217     }\r
218     netconn_close( pxNetCon );\r
219 }\r
220 \r
221 /*------------------------------------------------------------*/\r
222 \r
223 void vlwIPInit( void )\r
224 {\r
225     /* Initialize lwIP and its interface layer. */\r
226     tcpip_init( NULL, NULL );\r
227 }\r
228 \r
229 /*------------------------------------------------------------*/\r
230 \r
231 void vBasicWEBServer( void *pvParameters )\r
232 {\r
233     struct netconn *pxHTTPListener, *pxNewConnection;\r
234     struct ip_addr  xIpAddr, xNetMast, xGateway;\r
235     static struct netif fec523x_if;\r
236         extern err_t ethernetif_init(struct netif *netif);\r
237 \r
238     /* Parameters are not used - suppress compiler error. */\r
239     ( void )pvParameters;\r
240 \r
241         vlwIPInit();\r
242 \r
243     /* Create and configure the FEC interface. */\r
244     IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
245     IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
246     IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 );\r
247     netif_add( &fec523x_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input );\r
248 \r
249     /* make it the default interface */\r
250     netif_set_default( &fec523x_if );\r
251 \r
252     /* bring it up */\r
253     netif_set_up( &fec523x_if );\r
254 \r
255     /* Create a new tcp connection handle */\r
256     pxHTTPListener = netconn_new( NETCONN_TCP );\r
257     netconn_bind( pxHTTPListener, NULL, webHTTP_PORT );\r
258     netconn_listen( pxHTTPListener );\r
259 \r
260     /* Loop forever */\r
261     for( ;; )\r
262     {\r
263         /* Wait for connection. */\r
264         pxNewConnection = netconn_accept( pxHTTPListener );\r
265 \r
266         if( pxNewConnection != NULL )\r
267         {\r
268             /* Service connection. */\r
269             vProcessConnection( pxNewConnection );\r
270             while( netconn_delete( pxNewConnection ) != ERR_OK )\r
271             {\r
272                 vTaskDelay( webSHORT_DELAY );\r
273             }\r
274         }\r
275     }\r
276 }\r