]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/lwIP_Demo/lwIP_Apps/lwIP_Apps.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / lwIP_Demo / lwIP_Apps / lwIP_Apps.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Standard includes. */\r
71 #include <string.h>\r
72 \r
73 /* FreeRTOS includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "semphr.h"\r
77 \r
78 /* lwIP core includes */\r
79 #include "lwip/opt.h"\r
80 #include "lwip/tcpip.h"\r
81 #include "lwip/inet.h"\r
82 #include "lwip/dhcp.h"\r
83 \r
84 /* applications includes */\r
85 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"\r
86 \r
87 /* include the port-dependent configuration */\r
88 #include "lwipcfg_msvc.h"\r
89 \r
90 /* Dimensions the cTxBuffer array - which is itself used to hold replies from \r
91 command line commands.  cTxBuffer is a shared buffer, so protected by the \r
92 xTxBufferMutex mutex. */\r
93 #define lwipappsTX_BUFFER_SIZE  1024\r
94 \r
95 /* The maximum time to block waiting to obtain the xTxBufferMutex to become\r
96 available. */\r
97 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS       ( 100 / portTICK_RATE_MS )\r
98 \r
99 /* Definitions of the various SSI callback functions within the pccSSITags \r
100 array.  If pccSSITags is updated, then these definitions must also be updated. */\r
101 #define ssiTASK_STATS_INDEX                     0\r
102 #define ssiRUN_TIME_STATS_INDEX         1\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /*\r
107  * The function that implements the lwIP based sockets command interpreter\r
108  * server.\r
109  */\r
110 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );\r
111 \r
112 /*\r
113  * The SSI handler callback function passed to lwIP.\r
114  */\r
115 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* The SSI strings that are embedded in the served html files.  If this array\r
120 is changed, then the index position defined by the #defines such as \r
121 ssiTASK_STATS_INDEX above must also be updated. */\r
122 static const char *pccSSITags[] = \r
123 {\r
124         "rtos_stats",\r
125         "run_stats"\r
126 };\r
127 \r
128 /* Semaphore used to guard the Tx buffer. */\r
129 static xSemaphoreHandle xTxBufferMutex = NULL;\r
130 \r
131 /* The Tx buffer itself.  This is used to hold the text generated by the \r
132 execution of command line commands, and (hopefully) the execution of \r
133 server side include callbacks.  It is a shared buffer so protected by the\r
134 xTxBufferMutex mutex.  pcLwipAppsBlockingGetTxBuffer() and \r
135 vLwipAppsReleaseTxBuffer() are provided to obtain and release the \r
136 xTxBufferMutex respectively.  pcLwipAppsBlockingGetTxBuffer() must be used with\r
137 caution as it has the potential to block. */\r
138 static signed char cTxBuffer[ lwipappsTX_BUFFER_SIZE ];\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vStatusCallback( struct netif *pxNetIf )\r
143 {\r
144 char pcMessage[20];\r
145 struct in_addr* pxIPAddress;\r
146 \r
147         if( netif_is_up( pxNetIf ) != 0 )\r
148         {\r
149                 strcpy( pcMessage, "IP=" );\r
150                 pxIPAddress = ( struct in_addr* ) &( pxNetIf->ip_addr );\r
151                 strcat( pcMessage, inet_ntoa( ( *pxIPAddress ) ) );\r
152                 xil_printf( pcMessage );\r
153         }\r
154         else\r
155         {\r
156                 xil_printf( "Network is down" );\r
157         }\r
158 }\r
159 \r
160 /* Called from the TCP/IP thread. */\r
161 void lwIPAppsInit( void *pvArgument )\r
162 {\r
163 ip_addr_t xIPAddr, xNetMask, xGateway;\r
164 extern err_t xemacpsif_init( struct netif *netif );\r
165 extern void xemacif_input_thread( void *netif );\r
166 static struct netif xNetIf;\r
167 \r
168         ( void ) pvArgument;\r
169 \r
170         /* Set up the network interface. */\r
171         ip_addr_set_zero( &xGateway );\r
172         ip_addr_set_zero( &xIPAddr );\r
173         ip_addr_set_zero( &xNetMask );\r
174 \r
175         LWIP_PORT_INIT_GW(&xGateway);\r
176         LWIP_PORT_INIT_IPADDR( &xIPAddr );\r
177         LWIP_PORT_INIT_NETMASK(&xNetMask);\r
178 \r
179         /* Set mac address */\r
180         xNetIf.hwaddr_len = 6;\r
181         xNetIf.hwaddr[ 0 ] = configMAC_ADDR0;\r
182         xNetIf.hwaddr[ 1 ] = configMAC_ADDR1;\r
183         xNetIf.hwaddr[ 2 ] = configMAC_ADDR2;\r
184         xNetIf.hwaddr[ 3 ] = configMAC_ADDR3;\r
185         xNetIf.hwaddr[ 4 ] = configMAC_ADDR4;\r
186         xNetIf.hwaddr[ 5 ] = configMAC_ADDR5;\r
187 \r
188         netif_set_default( netif_add( &xNetIf, &xIPAddr, &xNetMask, &xGateway, ( void * ) XPAR_XEMACPS_0_BASEADDR, xemacpsif_init, tcpip_input ) );\r
189         netif_set_status_callback( &xNetIf, vStatusCallback );\r
190         #if LWIP_DHCP\r
191         {\r
192                 dhcp_start( &xNetIf );\r
193         }\r
194         #else\r
195         {\r
196                 netif_set_up( &xNetIf );\r
197         }\r
198         #endif\r
199 \r
200         /* Install the server side include handler. */\r
201         http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );\r
202 \r
203         /* Create the mutex used to ensure mutual exclusive access to the Tx \r
204         buffer. */\r
205         xTxBufferMutex = xSemaphoreCreateMutex();\r
206         configASSERT( xTxBufferMutex );\r
207 \r
208         /* Create the httpd server from the standard lwIP code.  This demonstrates\r
209         use of the lwIP raw API. */\r
210         httpd_init();\r
211 \r
212         sys_thread_new( "lwIP_In", xemacif_input_thread, &xNetIf, configMINIMAL_STACK_SIZE, configMAC_INPUT_TASK_PRIORITY );\r
213 \r
214         /* Create the FreeRTOS defined basic command server.  This demonstrates use\r
215         of the lwIP sockets API. */\r
216         xTaskCreate( vBasicSocketsCommandInterpreterTask, "CmdInt", configMINIMAL_STACK_SIZE * 5, NULL, configCLI_TASK_PRIORITY, NULL );\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )\r
221 {\r
222 static unsigned int uiUpdateCount = 0;\r
223 static char cUpdateString[ 200 ];\r
224 extern char *pcMainGetTaskStatusMessage( void );\r
225 \r
226         /* Unused parameter. */\r
227         ( void ) iBufferLength;\r
228 \r
229         /* The SSI handler function that generates text depending on the index of\r
230         the SSI tag encountered. */\r
231         \r
232         switch( iIndex )\r
233         {\r
234                 case ssiTASK_STATS_INDEX :\r
235                         vTaskList( pcBuffer );\r
236                         break;\r
237 \r
238                 case ssiRUN_TIME_STATS_INDEX :\r
239                         vTaskGetRunTimeStats( pcBuffer );\r
240                         break;\r
241         }\r
242 \r
243         /* Include a count of the number of times an SSI function has been executed\r
244         in the returned string. */\r
245         uiUpdateCount++;\r
246         sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );\r
247         strcat( pcBuffer, cUpdateString );\r
248 \r
249         return strlen( pcBuffer );\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 signed char *pcLwipAppsBlockingGetTxBuffer( void )\r
254 {\r
255 signed char *pcReturn;\r
256 \r
257         /* Attempt to obtain the semaphore that guards the Tx buffer. */\r
258         if( xSemaphoreTakeRecursive( xTxBufferMutex, lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ) == pdFAIL )\r
259         {\r
260                 /* The semaphore could not be obtained before timing out. */\r
261                 pcReturn = NULL;\r
262         }\r
263         else\r
264         {\r
265                 /* The semaphore was obtained successfully.  Return a pointer to the\r
266                 Tx buffer. */\r
267                 pcReturn = cTxBuffer;\r
268         }\r
269 \r
270         return pcReturn;\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 void vLwipAppsReleaseTxBuffer( void )\r
275 {\r
276         /* Finished with the Tx buffer.  Return the mutex. */\r
277         xSemaphoreGiveRecursive( xTxBufferMutex );\r
278 }\r
279 \r
280 \r
281 \r