]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/lwIP_Demo/lwIP_Apps/lwIP_Apps.c
861340256a4041ea60f997b57f8f44f854e56e16
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / lwIP_Demo / lwIP_Apps / lwIP_Apps.c
1 /*\r
2     FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Standard includes. */\r
55 #include <string.h>\r
56 \r
57 /* FreeRTOS includes. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 #include "semphr.h"\r
61 \r
62 /* lwIP core includes */\r
63 #include "lwip/opt.h"\r
64 #include "lwip/tcpip.h"\r
65 #include "lwip/inet.h"\r
66 #include "lwip/dhcp.h"\r
67 \r
68 /* applications includes */\r
69 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"\r
70 \r
71 /* include the port-dependent configuration */\r
72 #include "lwipcfg_msvc.h"\r
73 \r
74 /* Dimensions the cTxBuffer array - which is itself used to hold replies from \r
75 command line commands.  cTxBuffer is a shared buffer, so protected by the \r
76 xTxBufferMutex mutex. */\r
77 #define lwipappsTX_BUFFER_SIZE  1024\r
78 \r
79 /* The maximum time to block waiting to obtain the xTxBufferMutex to become\r
80 available. */\r
81 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS       ( 100 / portTICK_RATE_MS )\r
82 \r
83 /* Definitions of the various SSI callback functions within the pccSSITags \r
84 array.  If pccSSITags is updated, then these definitions must also be updated. */\r
85 #define ssiTASK_STATS_INDEX                     0\r
86 #define ssiRUN_TIME_STATS_INDEX         1\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /*\r
91  * The function that implements the lwIP based sockets command interpreter\r
92  * server.\r
93  */\r
94 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );\r
95 \r
96 /*\r
97  * The SSI handler callback function passed to lwIP.\r
98  */\r
99 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /* The SSI strings that are embedded in the served html files.  If this array\r
104 is changed, then the index position defined by the #defines such as \r
105 ssiTASK_STATS_INDEX above must also be updated. */\r
106 static const char *pccSSITags[] = \r
107 {\r
108         "rtos_stats",\r
109         "run_stats"\r
110 };\r
111 \r
112 /* Semaphore used to guard the Tx buffer. */\r
113 static xSemaphoreHandle xTxBufferMutex = NULL;\r
114 \r
115 /* The Tx buffer itself.  This is used to hold the text generated by the \r
116 execution of command line commands, and (hopefully) the execution of \r
117 server side include callbacks.  It is a shared buffer so protected by the\r
118 xTxBufferMutex mutex.  pcLwipAppsBlockingGetTxBuffer() and \r
119 vLwipAppsReleaseTxBuffer() are provided to obtain and release the \r
120 xTxBufferMutex respectively.  pcLwipAppsBlockingGetTxBuffer() must be used with\r
121 caution as it has the potential to block. */\r
122 static signed char cTxBuffer[ lwipappsTX_BUFFER_SIZE ];\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 void vStatusCallback( struct netif *pxNetIf )\r
127 {\r
128 char pcMessage[20];\r
129 struct in_addr* pxIPAddress;\r
130 \r
131         if( netif_is_up( pxNetIf ) != 0 )\r
132         {\r
133                 strcpy( pcMessage, "IP=" );\r
134                 pxIPAddress = ( struct in_addr* ) &( pxNetIf->ip_addr );\r
135                 strcat( pcMessage, inet_ntoa( ( *pxIPAddress ) ) );\r
136                 xil_printf( pcMessage );\r
137         }\r
138         else\r
139         {\r
140                 xil_printf( "Network is down" );\r
141         }\r
142 }\r
143 \r
144 /* Called from the TCP/IP thread. */\r
145 void lwIPAppsInit( void *pvArgument )\r
146 {\r
147 ip_addr_t xIPAddr, xNetMask, xGateway;\r
148 extern err_t xemacpsif_init( struct netif *netif );\r
149 extern void xemacif_input_thread( void *netif );\r
150 static struct netif xNetIf;\r
151 \r
152         ( void ) pvArgument;\r
153 \r
154         /* Set up the network interface. */\r
155         ip_addr_set_zero( &xGateway );\r
156         ip_addr_set_zero( &xIPAddr );\r
157         ip_addr_set_zero( &xNetMask );\r
158 \r
159         LWIP_PORT_INIT_GW(&xGateway);\r
160         LWIP_PORT_INIT_IPADDR( &xIPAddr );\r
161         LWIP_PORT_INIT_NETMASK(&xNetMask);\r
162 \r
163         /* Set mac address */\r
164         xNetIf.hwaddr_len = 6;\r
165         xNetIf.hwaddr[ 0 ] = configMAC_ADDR0;\r
166         xNetIf.hwaddr[ 1 ] = configMAC_ADDR1;\r
167         xNetIf.hwaddr[ 2 ] = configMAC_ADDR2;\r
168         xNetIf.hwaddr[ 3 ] = configMAC_ADDR3;\r
169         xNetIf.hwaddr[ 4 ] = configMAC_ADDR4;\r
170         xNetIf.hwaddr[ 5 ] = configMAC_ADDR5;\r
171 \r
172         netif_set_default( netif_add( &xNetIf, &xIPAddr, &xNetMask, &xGateway, ( void * ) XPAR_XEMACPS_0_BASEADDR, xemacpsif_init, tcpip_input ) );\r
173         netif_set_status_callback( &xNetIf, vStatusCallback );\r
174         #if LWIP_DHCP\r
175         {\r
176                 dhcp_start( &xNetIf );\r
177         }\r
178         #else\r
179         {\r
180                 netif_set_up( &xNetIf );\r
181         }\r
182         #endif\r
183 \r
184         /* Install the server side include handler. */\r
185         http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );\r
186 \r
187         /* Create the mutex used to ensure mutual exclusive access to the Tx \r
188         buffer. */\r
189         xTxBufferMutex = xSemaphoreCreateMutex();\r
190         configASSERT( xTxBufferMutex );\r
191 \r
192         /* Create the httpd server from the standard lwIP code.  This demonstrates\r
193         use of the lwIP raw API. */\r
194         httpd_init();\r
195 \r
196         sys_thread_new( "lwIP_In", xemacif_input_thread, &xNetIf, configMINIMAL_STACK_SIZE, configMAC_INPUT_TASK_PRIORITY );\r
197 \r
198         /* Create the FreeRTOS defined basic command server.  This demonstrates use\r
199         of the lwIP sockets API. */\r
200         xTaskCreate( vBasicSocketsCommandInterpreterTask, "CmdInt", configMINIMAL_STACK_SIZE * 5, NULL, configCLI_TASK_PRIORITY, NULL );\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )\r
205 {\r
206 static unsigned int uiUpdateCount = 0;\r
207 static char cUpdateString[ 200 ];\r
208 extern char *pcMainGetTaskStatusMessage( void );\r
209 \r
210         /* Unused parameter. */\r
211         ( void ) iBufferLength;\r
212 \r
213         /* The SSI handler function that generates text depending on the index of\r
214         the SSI tag encountered. */\r
215         \r
216         switch( iIndex )\r
217         {\r
218                 case ssiTASK_STATS_INDEX :\r
219                         vTaskList( pcBuffer );\r
220                         break;\r
221 \r
222                 case ssiRUN_TIME_STATS_INDEX :\r
223                         vTaskGetRunTimeStats( pcBuffer );\r
224                         break;\r
225         }\r
226 \r
227         /* Include a count of the number of times an SSI function has been executed\r
228         in the returned string. */\r
229         uiUpdateCount++;\r
230         sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );\r
231         strcat( pcBuffer, cUpdateString );\r
232 \r
233         return strlen( pcBuffer );\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 signed char *pcLwipAppsBlockingGetTxBuffer( void )\r
238 {\r
239 signed char *pcReturn;\r
240 \r
241         /* Attempt to obtain the semaphore that guards the Tx buffer. */\r
242         if( xSemaphoreTakeRecursive( xTxBufferMutex, lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ) == pdFAIL )\r
243         {\r
244                 /* The semaphore could not be obtained before timing out. */\r
245                 pcReturn = NULL;\r
246         }\r
247         else\r
248         {\r
249                 /* The semaphore was obtained successfully.  Return a pointer to the\r
250                 Tx buffer. */\r
251                 pcReturn = cTxBuffer;\r
252         }\r
253 \r
254         return pcReturn;\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 void vLwipAppsReleaseTxBuffer( void )\r
259 {\r
260         /* Finished with the Tx buffer.  Return the mutex. */\r
261         xSemaphoreGiveRecursive( xTxBufferMutex );\r
262 }\r
263 \r
264 \r
265 \r