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