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