]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/lwIP_Demo/lwIP_Apps/lwIP_Apps.c
41482df23f604f686ce12b08697376b2ae2e5557
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / lwIP_Demo / lwIP_Apps / lwIP_Apps.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include <string.h>\r
30 \r
31 /* FreeRTOS includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 #include "semphr.h"\r
35 \r
36 /* lwIP core includes */\r
37 #include "lwip/opt.h"\r
38 #include "lwip/tcpip.h"\r
39 #include "lwip/inet.h"\r
40 #include "lwip/dhcp.h"\r
41 \r
42 /* applications includes */\r
43 #include "apps/httpserver_raw_from_lwIP_download/httpd.h"\r
44 \r
45 /* include the port-dependent configuration */\r
46 #include "lwipcfg_msvc.h"\r
47 \r
48 /* Dimensions the cTxBuffer array - which is itself used to hold replies from \r
49 command line commands.  cTxBuffer is a shared buffer, so protected by the \r
50 xTxBufferMutex mutex. */\r
51 #define lwipappsTX_BUFFER_SIZE  1024\r
52 \r
53 /* The maximum time to block waiting to obtain the xTxBufferMutex to become\r
54 available. */\r
55 #define lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS       ( 100 / portTICK_RATE_MS )\r
56 \r
57 /* Definitions of the various SSI callback functions within the pccSSITags \r
58 array.  If pccSSITags is updated, then these definitions must also be updated. */\r
59 #define ssiTASK_STATS_INDEX                     0\r
60 #define ssiRUN_TIME_STATS_INDEX         1\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /*\r
65  * The function that implements the lwIP based sockets command interpreter\r
66  * server.\r
67  */\r
68 extern void vBasicSocketsCommandInterpreterTask( void *pvParameters );\r
69 \r
70 /*\r
71  * The SSI handler callback function passed to lwIP.\r
72  */\r
73 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength );\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /* The SSI strings that are embedded in the served html files.  If this array\r
78 is changed, then the index position defined by the #defines such as \r
79 ssiTASK_STATS_INDEX above must also be updated. */\r
80 static const char *pccSSITags[] = \r
81 {\r
82         "rtos_stats",\r
83         "run_stats"\r
84 };\r
85 \r
86 /* Semaphore used to guard the Tx buffer. */\r
87 static xSemaphoreHandle xTxBufferMutex = NULL;\r
88 \r
89 /* The Tx buffer itself.  This is used to hold the text generated by the \r
90 execution of command line commands, and (hopefully) the execution of \r
91 server side include callbacks.  It is a shared buffer so protected by the\r
92 xTxBufferMutex mutex.  pcLwipAppsBlockingGetTxBuffer() and \r
93 vLwipAppsReleaseTxBuffer() are provided to obtain and release the \r
94 xTxBufferMutex respectively.  pcLwipAppsBlockingGetTxBuffer() must be used with\r
95 caution as it has the potential to block. */\r
96 static signed char cTxBuffer[ lwipappsTX_BUFFER_SIZE ];\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void vStatusCallback( struct netif *pxNetIf )\r
101 {\r
102 char pcMessage[20];\r
103 struct in_addr* pxIPAddress;\r
104 \r
105         if( netif_is_up( pxNetIf ) != 0 )\r
106         {\r
107                 strcpy( pcMessage, "IP=" );\r
108                 pxIPAddress = ( struct in_addr* ) &( pxNetIf->ip_addr );\r
109                 strcat( pcMessage, inet_ntoa( ( *pxIPAddress ) ) );\r
110                 xil_printf( pcMessage );\r
111         }\r
112         else\r
113         {\r
114                 xil_printf( "Network is down" );\r
115         }\r
116 }\r
117 \r
118 /* Called from the TCP/IP thread. */\r
119 void lwIPAppsInit( void *pvArgument )\r
120 {\r
121 ip_addr_t xIPAddr, xNetMask, xGateway;\r
122 extern err_t xemacpsif_init( struct netif *netif );\r
123 extern void xemacif_input_thread( void *netif );\r
124 static struct netif xNetIf;\r
125 \r
126         ( void ) pvArgument;\r
127 \r
128         /* Set up the network interface. */\r
129         ip_addr_set_zero( &xGateway );\r
130         ip_addr_set_zero( &xIPAddr );\r
131         ip_addr_set_zero( &xNetMask );\r
132 \r
133         LWIP_PORT_INIT_GW(&xGateway);\r
134         LWIP_PORT_INIT_IPADDR( &xIPAddr );\r
135         LWIP_PORT_INIT_NETMASK(&xNetMask);\r
136 \r
137         /* Set mac address */\r
138         xNetIf.hwaddr_len = 6;\r
139         xNetIf.hwaddr[ 0 ] = configMAC_ADDR0;\r
140         xNetIf.hwaddr[ 1 ] = configMAC_ADDR1;\r
141         xNetIf.hwaddr[ 2 ] = configMAC_ADDR2;\r
142         xNetIf.hwaddr[ 3 ] = configMAC_ADDR3;\r
143         xNetIf.hwaddr[ 4 ] = configMAC_ADDR4;\r
144         xNetIf.hwaddr[ 5 ] = configMAC_ADDR5;\r
145 \r
146         netif_set_default( netif_add( &xNetIf, &xIPAddr, &xNetMask, &xGateway, ( void * ) XPAR_XEMACPS_0_BASEADDR, xemacpsif_init, tcpip_input ) );\r
147         netif_set_status_callback( &xNetIf, vStatusCallback );\r
148         #if LWIP_DHCP\r
149         {\r
150                 dhcp_start( &xNetIf );\r
151         }\r
152         #else\r
153         {\r
154                 netif_set_up( &xNetIf );\r
155         }\r
156         #endif\r
157 \r
158         /* Install the server side include handler. */\r
159         http_set_ssi_handler( uslwIPAppsSSIHandler, pccSSITags, sizeof( pccSSITags ) / sizeof( char * ) );\r
160 \r
161         /* Create the mutex used to ensure mutual exclusive access to the Tx \r
162         buffer. */\r
163         xTxBufferMutex = xSemaphoreCreateMutex();\r
164         configASSERT( xTxBufferMutex );\r
165 \r
166         /* Create the httpd server from the standard lwIP code.  This demonstrates\r
167         use of the lwIP raw API. */\r
168         httpd_init();\r
169 \r
170         sys_thread_new( "lwIP_In", xemacif_input_thread, &xNetIf, configMINIMAL_STACK_SIZE, configMAC_INPUT_TASK_PRIORITY );\r
171 \r
172         /* Create the FreeRTOS defined basic command server.  This demonstrates use\r
173         of the lwIP sockets API. */\r
174         xTaskCreate( vBasicSocketsCommandInterpreterTask, "CmdInt", configMINIMAL_STACK_SIZE * 5, NULL, configCLI_TASK_PRIORITY, NULL );\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static unsigned short uslwIPAppsSSIHandler( int iIndex, char *pcBuffer, int iBufferLength )\r
179 {\r
180 static unsigned int uiUpdateCount = 0;\r
181 static char cUpdateString[ 200 ];\r
182 extern char *pcMainGetTaskStatusMessage( void );\r
183 \r
184         /* Unused parameter. */\r
185         ( void ) iBufferLength;\r
186 \r
187         /* The SSI handler function that generates text depending on the index of\r
188         the SSI tag encountered. */\r
189         \r
190         switch( iIndex )\r
191         {\r
192                 case ssiTASK_STATS_INDEX :\r
193                         vTaskList( pcBuffer );\r
194                         break;\r
195 \r
196                 case ssiRUN_TIME_STATS_INDEX :\r
197                         vTaskGetRunTimeStats( pcBuffer );\r
198                         break;\r
199         }\r
200 \r
201         /* Include a count of the number of times an SSI function has been executed\r
202         in the returned string. */\r
203         uiUpdateCount++;\r
204         sprintf( cUpdateString, "\r\n\r\n%u\r\nStatus - %s", uiUpdateCount, pcMainGetTaskStatusMessage() );\r
205         strcat( pcBuffer, cUpdateString );\r
206 \r
207         return strlen( pcBuffer );\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 signed char *pcLwipAppsBlockingGetTxBuffer( void )\r
212 {\r
213 signed char *pcReturn;\r
214 \r
215         /* Attempt to obtain the semaphore that guards the Tx buffer. */\r
216         if( xSemaphoreTakeRecursive( xTxBufferMutex, lwipappsMAX_TIME_TO_WAIT_FOR_TX_BUFFER_MS ) == pdFAIL )\r
217         {\r
218                 /* The semaphore could not be obtained before timing out. */\r
219                 pcReturn = NULL;\r
220         }\r
221         else\r
222         {\r
223                 /* The semaphore was obtained successfully.  Return a pointer to the\r
224                 Tx buffer. */\r
225                 pcReturn = cTxBuffer;\r
226         }\r
227 \r
228         return pcReturn;\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 void vLwipAppsReleaseTxBuffer( void )\r
233 {\r
234         /* Finished with the Tx buffer.  Return the mutex. */\r
235         xSemaphoreGiveRecursive( xTxBufferMutex );\r
236 }\r
237 \r
238 \r
239 \r