]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WizNET_DEMO_TERN_186/HTTPTask.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / WizNET_DEMO_TERN_186 / HTTPTask.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  * Very simple task that responds with a single WEB page to http requests.\r
31  *\r
32  * The WEB page displays task and system status.  A semaphore is used to\r
33  * wake the task when there is processing to perform as determined by the\r
34  * interrupts generated by the Ethernet interface.\r
35  */\r
36 \r
37 /* Standard includes. */\r
38 #include <string.h>\r
39 #include <stdio.h>\r
40 \r
41 /* Tern includes. */\r
42 #include "utils\system_common.h"\r
43 #include "i2chip_hw.h"\r
44 #include "socket.h"\r
45 \r
46 /* FreeRTOS.org includes. */\r
47 #include <FreeRTOS.h>\r
48 #include <task.h>\r
49 #include <semphr.h>\r
50 \r
51 /* The standard http port on which we are going to listen. */\r
52 #define httpPORT 80\r
53 \r
54 #define httpTX_WAIT 2\r
55 \r
56 /* Network address configuration. */\r
57 const unsigned char ucMacAddress[] =                    { 12, 128, 12, 34, 56, 78 };\r
58 const unsigned char ucGatewayAddress[] =                { 192, 168, 2, 1 };\r
59 const unsigned char ucIPAddress[] =                             { 172, 25, 218, 210 };\r
60 const unsigned char ucSubnetMask[] =                    { 255, 255, 255, 0 };\r
61 \r
62 /* The number of sockets this task is going to handle. */\r
63 #define httpSOCKET_NUM                       3\r
64 unsigned char ucConnection[ httpSOCKET_NUM ];\r
65 \r
66 /* The maximum data buffer size we can handle. */\r
67 #define httpSOCKET_BUFFER_SIZE  2048\r
68 \r
69 /* Standard HTTP response. */\r
70 #define httpOUTPUT_OK   "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"\r
71 \r
72 /* Hard coded HTML components.  Other data is generated dynamically. */\r
73 #define HTML_OUTPUT_BEGIN "\\r
74 <HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\\r
75 <BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\\r
76 <a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\\r
77 <HR>Task status table:\r\n\\r
78 <p><font face=\"courier\"><pre>Task          State  Priority  Stack     #<br>\\r
79 ************************************************<br>"\r
80 \r
81 #define HTML_OUTPUT_END   "\\r
82 </font></BODY></HTML>"\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /*\r
87  * Initialise the data structures used to hold the socket status.\r
88  */\r
89 static void prvHTTPInit( void );\r
90 \r
91 /*\r
92  * Setup the Ethernet interface with the network addressing information.\r
93  */\r
94 static void prvNetifInit( void );\r
95 \r
96 /*\r
97  * Generate the dynamic components of the served WEB page and transmit the\r
98  * entire page through the socket.\r
99  */\r
100 static void prvTransmitHTTP( unsigned char socket );\r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /* This variable is simply incremented by the idle task hook so the number of\r
104 iterations the idle task has performed can be displayed as part of the served\r
105 page. */\r
106 unsigned long ulIdleLoops = 0UL;\r
107 \r
108 /* Data buffer shared by sockets. */\r
109 unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];\r
110 \r
111 /* The semaphore used by the Ethernet ISR to signal that the task should wake\r
112 and process whatever caused the interrupt. */\r
113 SemaphoreHandle_t xTCPSemaphore = NULL;\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 void vHTTPTask( void * pvParameters )\r
117 {\r
118 short i, sLen;\r
119 unsigned char ucState;\r
120 \r
121         ( void ) pvParameters;\r
122 \r
123     /* Create the semaphore used to communicate between this task and the\r
124     WIZnet ISR. */\r
125     vSemaphoreCreateBinary( xTCPSemaphore );\r
126 \r
127         /* Make sure everything is setup before we start. */\r
128         prvNetifInit();\r
129         prvHTTPInit();\r
130 \r
131         for( ;; )\r
132         {\r
133                 /* Wait until the ISR tells us there is something to do. */\r
134         xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );\r
135 \r
136                 /* Check each socket. */\r
137                 for( i = 0; i < httpSOCKET_NUM; i++ )\r
138                 {\r
139                         ucState = select( i, SEL_CONTROL );\r
140 \r
141                         switch (ucState)\r
142                         {\r
143                                 case SOCK_ESTABLISHED :  /* new connection established. */\r
144 \r
145                                         if( ( sLen = select( i, SEL_RECV ) ) > 0 )\r
146                                         {\r
147                                                 if( sLen > httpSOCKET_BUFFER_SIZE )\r
148                                                 {\r
149                                                         sLen = httpSOCKET_BUFFER_SIZE;\r
150                                                 }\r
151 \r
152                                                 disable();\r
153 \r
154                                                 sLen = recv( i, ucSocketBuffer, sLen );\r
155 \r
156                                                 if( ucConnection[ i ] == 1 )\r
157                                                 {\r
158                                                         /* This is our first time processing a HTTP\r
159                                                          request on this connection. */\r
160                                                         prvTransmitHTTP( i );\r
161                                                         ucConnection[i] = 0;\r
162                                                 }\r
163                                                 enable();\r
164                                         }\r
165                                         break;\r
166 \r
167                                 case SOCK_CLOSE_WAIT :\r
168 \r
169                                         close(i);\r
170                                         break;\r
171 \r
172                                 case SOCK_CLOSED :\r
173 \r
174                                         ucConnection[i] = 1;\r
175                                         socket( i, SOCK_STREAM, 80, 0x00 );\r
176                                         NBlisten( i ); /* reinitialize socket. */\r
177                                         break;\r
178                         }\r
179                 }\r
180         }\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 static void prvHTTPInit( void )\r
185 {\r
186 unsigned char ucIndex;\r
187 \r
188         /* There are 4 total sockets available; we will claim 3 for HTTP. */\r
189         for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)\r
190         {\r
191                 socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );\r
192                 NBlisten( ucIndex );\r
193                 ucConnection[ ucIndex ] = 1;\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 static void prvNetifInit( void )\r
199 {\r
200         i2chip_init();\r
201         initW3100A();\r
202 \r
203         setMACAddr( ( unsigned char * ) ucMacAddress );\r
204         setgateway( ( unsigned char * ) ucGatewayAddress );\r
205         setsubmask( ( unsigned char * ) ucSubnetMask );\r
206         setIP( ( unsigned char * ) ucIPAddress );\r
207 \r
208         /* See definition of 'sysinit' in socket.c\r
209          - 8 KB transmit buffer, and 8 KB receive buffer available.  These buffers\r
210            are shared by all 4 channels.\r
211          - (0x55, 0x55) configures the send and receive buffers at\r
212                 httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */\r
213         sysinit( 0x55, 0x55 );\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void prvTransmitHTTP(unsigned char socket)\r
218 {\r
219 extern short usCheckStatus;\r
220 \r
221         /* Send the http and html headers. */\r
222         send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );\r
223         send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );\r
224 \r
225         /* Generate then send the table showing the status of each task. */\r
226         vTaskList( ( char * ) ucSocketBuffer );\r
227         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
228 \r
229         /* Send the number of times the idle task has looped. */\r
230     sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );\r
231         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
232 \r
233         /* Send the tick count. */\r
234     sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );\r
235         send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
236 \r
237         /* Show a message indicating whether or not the check task has discovered\r
238         an error in any of the standard demo tasks. */\r
239     if( usCheckStatus == 0 )\r
240     {\r
241             sprintf( ucSocketBuffer, "No errors detected." );\r
242                 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
243     }\r
244     else\r
245     {\r
246             sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );\r
247                 send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );\r
248     }\r
249 \r
250         /* Finish the page off. */\r
251         send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));\r
252 \r
253         /* Must make sure the data is gone before closing the socket. */\r
254         while( !tx_empty( socket ) )\r
255     {\r
256         vTaskDelay( httpTX_WAIT );\r
257     }\r
258         close(socket);\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 void vApplicationIdleHook( void )\r
263 {\r
264         ulIdleLoops++;\r
265 }\r
266 \r
267 \r
268 \r
269 \r
270 \r
271 \r
272 \r
273 \r
274 \r
275 \r
276 \r
277 \r
278 \r
279 \r
280 \r
281 \r
282 \r
283 \r
284 \r
285 \r