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