OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [WizNET_DEMO_TERN_186/] [HTTPTask.c] - Blame information for rev 615

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 585 jeremybenn
/*
2
    FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
3
 
4
    ***************************************************************************
5
    *                                                                         *
6
    * If you are:                                                             *
7
    *                                                                         *
8
    *    + New to FreeRTOS,                                                   *
9
    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
10
    *    + Looking for basic training,                                        *
11
    *    + Wanting to improve your FreeRTOS skills and productivity           *
12
    *                                                                         *
13
    * then take a look at the FreeRTOS books - available as PDF or paperback  *
14
    *                                                                         *
15
    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
16
    *                  http://www.FreeRTOS.org/Documentation                  *
17
    *                                                                         *
18
    * A pdf reference manual is also available.  Both are usually delivered   *
19
    * to your inbox within 20 minutes to two hours when purchased between 8am *
20
    * and 8pm GMT (although please allow up to 24 hours in case of            *
21
    * exceptional circumstances).  Thank you for your support!                *
22
    *                                                                         *
23
    ***************************************************************************
24
 
25
    This file is part of the FreeRTOS distribution.
26
 
27
    FreeRTOS is free software; you can redistribute it and/or modify it under
28
    the terms of the GNU General Public License (version 2) as published by the
29
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30
    ***NOTE*** The exception to the GPL is included to allow you to distribute
31
    a combined work that includes FreeRTOS without being obliged to provide the
32
    source code for proprietary components outside of the FreeRTOS kernel.
33
    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
36
    more details. You should have received a copy of the GNU General Public
37
    License and the FreeRTOS license exception along with FreeRTOS; if not it
38
    can be viewed here: http://www.freertos.org/a00114.html and also obtained
39
    by writing to Richard Barry, contact details for whom are available on the
40
    FreeRTOS WEB site.
41
 
42
    1 tab == 4 spaces!
43
 
44
    http://www.FreeRTOS.org - Documentation, latest information, license and
45
    contact details.
46
 
47
    http://www.SafeRTOS.com - A version that is certified for use in safety
48
    critical systems.
49
 
50
    http://www.OpenRTOS.com - Commercial support, development, porting,
51
    licensing and training services.
52
*/
53
 
54
/*
55
 * Very simple task that responds with a single WEB page to http requests.
56
 *
57
 * The WEB page displays task and system status.  A semaphore is used to
58
 * wake the task when there is processing to perform as determined by the
59
 * interrupts generated by the Ethernet interface.
60
 */
61
 
62
/* Standard includes. */
63
#include <string.h>
64
#include <stdio.h>
65
 
66
/* Tern includes. */
67
#include "utils\system_common.h"
68
#include "i2chip_hw.h"
69
#include "socket.h"
70
 
71
/* FreeRTOS.org includes. */
72
#include <FreeRTOS.h>
73
#include <task.h>
74
#include <semphr.h>
75
 
76
/* The standard http port on which we are going to listen. */
77
#define httpPORT 80
78
 
79
#define httpTX_WAIT 2
80
 
81
/* Network address configuration. */
82
const unsigned char ucMacAddress[] =                    { 12, 128, 12, 34, 56, 78 };
83
const unsigned char ucGatewayAddress[] =                { 192, 168, 2, 1 };
84
const unsigned char ucIPAddress[] =                             { 172, 25, 218, 210 };
85
const unsigned char ucSubnetMask[] =                    { 255, 255, 255, 0 };
86
 
87
/* The number of sockets this task is going to handle. */
88
#define httpSOCKET_NUM                       3
89
unsigned char ucConnection[ httpSOCKET_NUM ];
90
 
91
/* The maximum data buffer size we can handle. */
92
#define httpSOCKET_BUFFER_SIZE  2048
93
 
94
/* Standard HTTP response. */
95
#define httpOUTPUT_OK   "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
96
 
97
/* Hard coded HTML components.  Other data is generated dynamically. */
98
#define HTML_OUTPUT_BEGIN "\
99
<HTML><head><meta http-equiv=\"Refresh\" content=\"1\;url=index.htm\"></head>\
100
<BODY bgcolor=\"#CCCCFF\"><font face=\"arial\"><H2>FreeRTOS.org<sup>tm</sup> + Tern E-Engine<sup>tm</sup></H2>\
101
<a href=\"http:\/\/www.FreeRTOS.org\">FreeRTOS.org Homepage</a><P>\
102
<HR>Task status table:\r\n\
103
<p><font face=\"courier\"><pre>Task          State  Priority  Stack     #<br>\
104
************************************************<br>"
105
 
106
#define HTML_OUTPUT_END   "\
107
</font></BODY></HTML>"
108
 
109
/*-----------------------------------------------------------*/
110
 
111
/*
112
 * Initialise the data structures used to hold the socket status.
113
 */
114
static void prvHTTPInit( void );
115
 
116
/*
117
 * Setup the Ethernet interface with the network addressing information.
118
 */
119
static void prvNetifInit( void );
120
 
121
/*
122
 * Generate the dynamic components of the served WEB page and transmit the
123
 * entire page through the socket.
124
 */
125
static void prvTransmitHTTP( unsigned char socket );
126
/*-----------------------------------------------------------*/
127
 
128
/* This variable is simply incremented by the idle task hook so the number of
129
iterations the idle task has performed can be displayed as part of the served
130
page. */
131
unsigned long ulIdleLoops = 0UL;
132
 
133
/* Data buffer shared by sockets. */
134
unsigned char ucSocketBuffer[ httpSOCKET_BUFFER_SIZE ];
135
 
136
/* The semaphore used by the Ethernet ISR to signal that the task should wake
137
and process whatever caused the interrupt. */
138
xSemaphoreHandle xTCPSemaphore = NULL;
139
 
140
/*-----------------------------------------------------------*/
141
void vHTTPTask( void * pvParameters )
142
{
143
short i, sLen;
144
unsigned char ucState;
145
 
146
        ( void ) pvParameters;
147
 
148
    /* Create the semaphore used to communicate between this task and the
149
    WIZnet ISR. */
150
    vSemaphoreCreateBinary( xTCPSemaphore );
151
 
152
        /* Make sure everything is setup before we start. */
153
        prvNetifInit();
154
        prvHTTPInit();
155
 
156
        for( ;; )
157
        {
158
                /* Wait until the ISR tells us there is something to do. */
159
        xSemaphoreTake( xTCPSemaphore, portMAX_DELAY );
160
 
161
                /* Check each socket. */
162
                for( i = 0; i < httpSOCKET_NUM; i++ )
163
                {
164
                        ucState = select( i, SEL_CONTROL );
165
 
166
                        switch (ucState)
167
                        {
168
                                case SOCK_ESTABLISHED :  /* new connection established. */
169
 
170
                                        if( ( sLen = select( i, SEL_RECV ) ) > 0 )
171
                                        {
172
                                                if( sLen > httpSOCKET_BUFFER_SIZE )
173
                                                {
174
                                                        sLen = httpSOCKET_BUFFER_SIZE;
175
                                                }
176
 
177
                                                disable();
178
 
179
                                                sLen = recv( i, ucSocketBuffer, sLen );
180
 
181
                                                if( ucConnection[ i ] == 1 )
182
                                                {
183
                                                        /* This is our first time processing a HTTP
184
                                                         request on this connection. */
185
                                                        prvTransmitHTTP( i );
186
                                                        ucConnection[i] = 0;
187
                                                }
188
                                                enable();
189
                                        }
190
                                        break;
191
 
192
                                case SOCK_CLOSE_WAIT :
193
 
194
                                        close(i);
195
                                        break;
196
 
197
                                case SOCK_CLOSED :
198
 
199
                                        ucConnection[i] = 1;
200
                                        socket( i, SOCK_STREAM, 80, 0x00 );
201
                                        NBlisten( i ); /* reinitialize socket. */
202
                                        break;
203
                        }
204
                }
205
        }
206
}
207
/*-----------------------------------------------------------*/
208
 
209
static void prvHTTPInit( void )
210
{
211
unsigned char ucIndex;
212
 
213
        /* There are 4 total sockets available; we will claim 3 for HTTP. */
214
        for(ucIndex = 0; ucIndex < httpSOCKET_NUM; ucIndex++)
215
        {
216
                socket( ucIndex, SOCK_STREAM, httpPORT, 0x00 );
217
                NBlisten( ucIndex );
218
                ucConnection[ ucIndex ] = 1;
219
        }
220
}
221
/*-----------------------------------------------------------*/
222
 
223
static void prvNetifInit( void )
224
{
225
        i2chip_init();
226
        initW3100A();
227
 
228
        setMACAddr( ( unsigned char * ) ucMacAddress );
229
        setgateway( ( unsigned char * ) ucGatewayAddress );
230
        setsubmask( ( unsigned char * ) ucSubnetMask );
231
        setIP( ( unsigned char * ) ucIPAddress );
232
 
233
        /* See definition of 'sysinit' in socket.c
234
         - 8 KB transmit buffer, and 8 KB receive buffer available.  These buffers
235
           are shared by all 4 channels.
236
         - (0x55, 0x55) configures the send and receive buffers at
237
                httpSOCKET_BUFFER_SIZE bytes for each of the 4 channels. */
238
        sysinit( 0x55, 0x55 );
239
}
240
/*-----------------------------------------------------------*/
241
 
242
static void prvTransmitHTTP(unsigned char socket)
243
{
244
extern short usCheckStatus;
245
 
246
        /* Send the http and html headers. */
247
        send( socket, ( unsigned char * ) httpOUTPUT_OK, strlen( httpOUTPUT_OK ) );
248
        send( socket, ( unsigned char * ) HTML_OUTPUT_BEGIN, strlen( HTML_OUTPUT_BEGIN ) );
249
 
250
        /* Generate then send the table showing the status of each task. */
251
        vTaskList( ucSocketBuffer );
252
        send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
253
 
254
        /* Send the number of times the idle task has looped. */
255
    sprintf( ucSocketBuffer, "</pre></font><p><br>The idle task has looped 0x%08lx times<br>", ulIdleLoops );
256
        send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
257
 
258
        /* Send the tick count. */
259
    sprintf( ucSocketBuffer, "The tick count is 0x%08lx<br>", xTaskGetTickCount() );
260
        send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
261
 
262
        /* Show a message indicating whether or not the check task has discovered
263
        an error in any of the standard demo tasks. */
264
    if( usCheckStatus == 0 )
265
    {
266
            sprintf( ucSocketBuffer, "No errors detected." );
267
                send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
268
    }
269
    else
270
    {
271
            sprintf( ucSocketBuffer, "<font color=\"red\">An error has been detected in at least one task %x.</font><p>", usCheckStatus );
272
                send( socket, ( unsigned char * ) ucSocketBuffer, strlen( ucSocketBuffer ) );
273
    }
274
 
275
        /* Finish the page off. */
276
        send( socket, (unsigned char*)HTML_OUTPUT_END, strlen(HTML_OUTPUT_END));
277
 
278
        /* Must make sure the data is gone before closing the socket. */
279
        while( !tx_empty( socket ) )
280
    {
281
        vTaskDelay( httpTX_WAIT );
282
    }
283
        close(socket);
284
}
285
/*-----------------------------------------------------------*/
286
 
287
void vApplicationIdleHook( void )
288
{
289
        ulIdleLoops++;
290
}
291
 
292
 
293
 
294
 
295
 
296
 
297
 
298
 
299
 
300
 
301
 
302
 
303
 
304
 
305
 
306
 
307
 
308
 
309
 
310
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.