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