]> begriffs open source - cmsis-freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/BasicWEB.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / lwIP_Demo_Rowley_ARM7 / BasicWEB.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29         Implements a simplistic WEB server.  Every time a connection is made and
30         data is received a dynamic page that shows the current TCP/IP statistics
31         is generated and returned.  The connection is then closed.
32
33         This file was adapted from a FreeRTOS lwIP slip demo supplied by a third
34         party.
35 */
36
37 /*
38         Changes from V3.2.2
39
40         + Changed the page returned by the lwIP WEB server demo to display the
41           task status table rather than the TCP/IP statistics.
42 */
43
44
45 /* Standard includes. */
46 #include <stdio.h>
47 #include <string.h>
48
49 /* Scheduler includes. */
50 #include "FreeRTOS.h"
51 #include "task.h"
52 #include "semphr.h"
53
54 /* Demo includes. */
55 #include "BasicWEB.h"
56 #include "SAM7_EMAC.h"
57
58 /* lwIP includes. */
59 #include "lwip/api.h"
60 #include "lwip/tcpip.h"
61 #include "lwip/memp.h"
62 #include "lwip/stats.h"
63 #include "netif/loopif.h"
64
65 /* The size of the buffer in which the dynamic WEB page is created. */
66 #define webMAX_PAGE_SIZE        2048
67
68 /* Standard GET response. */
69 #define webHTTP_OK      "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n"
70
71 /* The port on which we listen. */
72 #define webHTTP_PORT            ( 80 )
73
74 /* Delay on close error. */
75 #define webSHORT_DELAY          ( 10 )
76
77 /* Format of the dynamic page that is returned on each connection. */
78 #define webHTML_START \
79 "<html>\
80 <head>\
81 </head>\
82 <BODY onLoad=\"window.setTimeout(&quot;location.href='index.html'&quot;,1000)\"bgcolor=\"#CCCCff\">\
83 \r\nPage Hits = "
84
85 #define webHTML_END \
86 "\r\n</pre>\
87 \r\n</BODY>\
88 </html>"
89
90 /*------------------------------------------------------------*/
91
92 /*
93  * Process an incoming connection on port 80.
94  *
95  * This simply checks to see if the incoming data contains a GET request, and
96  * if so sends back a single dynamically created page.  The connection is then
97  * closed.  A more complete implementation could create a task for each
98  * connection.
99  */
100 static void vProcessConnection( struct netconn *pxNetCon );
101
102 /*------------------------------------------------------------*/
103
104 static void vProcessConnection( struct netconn *pxNetCon )
105 {
106 static char cDynamicPage[ webMAX_PAGE_SIZE ], cPageHits[ 11 ];
107 struct netbuf *pxRxBuffer;
108 char *pcRxString;
109 unsigned short usLength;
110 static unsigned long ulPageHits = 0;
111
112         /* We expect to immediately get data. */
113         pxRxBuffer = netconn_recv( pxNetCon );
114
115         if( pxRxBuffer != NULL )
116         {
117                 /* Where is the data? */
118                 netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
119
120                 /* Is this a GET?  We don't handle anything else. */
121                 if( !strncmp( pcRxString, "GET", 3 ) )
122                 {
123                         pcRxString = cDynamicPage;
124
125                         /* Update the hit count. */
126                         ulPageHits++;
127                         sprintf( cPageHits, "%lu", ulPageHits );
128
129                         /* Write out the HTTP OK header. */
130             netconn_write(pxNetCon, webHTTP_OK, (u16_t)strlen( webHTTP_OK ), NETCONN_COPY );
131
132                         /* Generate the dynamic page...
133
134                         ... First the page header. */
135                         strcpy( cDynamicPage, webHTML_START );
136                         /* ... Then the hit count... */
137                         strcat( cDynamicPage, cPageHits );
138                         strcat( cDynamicPage, "<p><pre>Task          State  Priority  Stack     #<br>************************************************<br>" );
139                         /* ... Then the list of tasks and their status... */
140                         vTaskList( cDynamicPage + strlen( cDynamicPage ) );
141                         /* ... Finally the page footer. */
142                         strcat( cDynamicPage, webHTML_END );
143
144                         /* Write out the dynamically generated page. */
145                         netconn_write(pxNetCon, cDynamicPage, (u16_t)strlen( cDynamicPage ), NETCONN_COPY );
146                 }
147
148                 netbuf_delete( pxRxBuffer );
149         }
150
151         netconn_close( pxNetCon );
152 }
153 /*------------------------------------------------------------*/
154
155 void vlwIPInit( void )
156 {
157     /* Initialize lwIP and its interface layer. */
158         sys_init();
159         mem_init();
160         memp_init();
161         pbuf_init();
162         netif_init();
163         ip_init();
164         tcpip_init( NULL, NULL );
165 }
166 /*------------------------------------------------------------*/
167
168 void vBasicWEBServer( void *pvParameters )
169 {
170 struct netconn *pxHTTPListener, *pxNewConnection;
171 struct ip_addr xIpAddr, xNetMast, xGateway;
172 extern err_t ethernetif_init( struct netif *netif );
173 static struct netif EMAC_if;
174
175         /* Parameters are not used - suppress compiler error. */
176         ( void ) pvParameters;
177
178
179         /* Create and configure the EMAC interface. */
180         IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3);
181         IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3);
182         IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3);
183         netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input);
184
185         /* make it the default interface */
186     netif_set_default(&EMAC_if);
187
188         /* bring it up */
189     netif_set_up(&EMAC_if);
190
191         /* Create a new tcp connection handle */
192
193         pxHTTPListener = netconn_new( NETCONN_TCP );
194         netconn_bind(pxHTTPListener, NULL, webHTTP_PORT );
195         netconn_listen( pxHTTPListener );
196
197         /* Loop forever */
198         for( ;; )
199         {
200                 /* Wait for connection. */
201                 pxNewConnection = netconn_accept(pxHTTPListener);
202
203                 if(pxNewConnection != NULL)
204                 {
205                         /* Service connection. */
206                         vProcessConnection( pxNewConnection );
207                         while( netconn_delete( pxNewConnection ) != ERR_OK )
208                         {
209                                 vTaskDelay( webSHORT_DELAY );
210                         }
211                 }
212         }
213 }
214
215