2 * FreeRTOS Kernel V10.0.1
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * This project is a cut down version of the project described on the following
\r
30 * link. Only the simple UDP client and server and the TCP echo clients are
\r
31 * included in the build:
\r
32 * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
35 /* Standard includes. */
\r
39 /* FreeRTOS includes. */
\r
40 #include <FreeRTOS.h>
\r
43 /* Demo application includes. */
\r
44 #include "FreeRTOS_IP.h"
\r
45 #include "FreeRTOS_Sockets.h"
\r
46 #include "SimpleUDPClientAndServer.h"
\r
47 #include "SimpleTCPEchoServer.h"
\r
48 #include "TCPEchoClient_SingleTasks.h"
\r
49 #include "demo_logging.h"
\r
51 /* Simple UDP client and server task parameters. */
\r
52 #define mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
53 #define mainSIMPLE_UDP_CLIENT_SERVER_PORT ( 5005UL )
\r
55 /* Echo client task parameters - used for both TCP and UDP echo clients. */
\r
56 #define mainECHO_CLIENT_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE * 2 ) /* Not used in the Windows port. */
\r
57 #define mainECHO_CLIENT_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
59 /* Echo server task parameters. */
\r
60 #define mainECHO_SERVER_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE * 2 ) /* Not used in the Windows port. */
\r
61 #define mainECHO_SERVER_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
63 /* Define a name that will be used for LLMNR and NBNS searches. */
\r
64 #define mainHOST_NAME "RTOSDemo"
\r
65 #define mainDEVICE_NICK_NAME "windows_demo"
\r
67 /* Set the following constants to 1 or 0 to define which tasks to include and
\r
70 mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS: When set to 1 two UDP client tasks
\r
71 and two UDP server tasks are created. The clients talk to the servers. One set
\r
72 of tasks use the standard sockets interface, and the other the zero copy sockets
\r
73 interface. These tasks are self checking and will trigger a configASSERT() if
\r
74 they detect a difference in the data that is received from that which was sent.
\r
75 As these tasks use UDP, and can therefore loose packets, they will cause
\r
76 configASSERT() to be called when they are run in a less than perfect networking
\r
79 mainCREATE_TCP_ECHO_TASKS_SINGLE: When set to 1 a set of tasks are created that
\r
80 send TCP echo requests to the standard echo port (port 7), then wait for and
\r
81 verify the echo reply, from within the same task (Tx and Rx are performed in the
\r
82 same RTOS task). The IP address of the echo server must be configured using the
\r
83 configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 constants in
\r
86 mainCREATE_TCP_ECHO_SERVER_TASK: When set to 1 a task is created that accepts
\r
87 connections on the standard echo port (port 7), then echos back any data
\r
88 received on that connection.
\r
90 #define mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS 1
\r
91 #define mainCREATE_TCP_ECHO_TASKS_SINGLE 1
\r
92 #define mainCREATE_TCP_ECHO_SERVER_TASK 0
\r
93 /*-----------------------------------------------------------*/
\r
96 * Just seeds the simple pseudo random number generator.
\r
98 static void prvSRand( UBaseType_t ulSeed );
\r
101 * Miscellaneous initialisation including preparing the logging and seeding the
\r
102 * random number generator.
\r
104 static void prvMiscInitialisation( void );
\r
106 /* The default IP and MAC address used by the demo. The address configuration
\r
107 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is
\r
108 1 but a DHCP server could not be contacted. See the online documentation for
\r
109 more information. */
\r
110 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };
\r
111 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };
\r
112 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };
\r
113 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };
\r
115 /* Set the following constant to pdTRUE to log using the method indicated by the
\r
116 name of the constant, or pdFALSE to not log using the method indicated by the
\r
117 name of the constant. Options include to standard out (xLogToStdout), to a disk
\r
118 file (xLogToFile), and to a UDP port (xLogToUDP). If xLogToUDP is set to pdTRUE
\r
119 then UDP messages are sent to the IP address configured as the echo server
\r
120 address (see the configECHO_SERVER_ADDR0 definitions in FreeRTOSConfig.h) and
\r
121 the port number set by configPRINT_PORT in FreeRTOSConfig.h. */
\r
122 const BaseType_t xLogToStdout = pdTRUE, xLogToFile = pdFALSE, xLogToUDP = pdFALSE;
\r
124 /* Default MAC address configuration. The demo creates a virtual network
\r
125 connection that uses this MAC address by accessing the raw Ethernet data
\r
126 to and from a real network connection on the host PC. See the
\r
127 configNETWORK_INTERFACE_TO_USE definition for information on how to configure
\r
128 the real network connection to use. */
\r
129 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };
\r
131 /* Use by the pseudo random number generator. */
\r
132 static UBaseType_t ulNextRand;
\r
134 /*-----------------------------------------------------------*/
\r
138 const uint32_t ulLongTime_ms = pdMS_TO_TICKS( 1000UL );
\r
141 * Instructions for using this project are provided on:
\r
142 * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
145 /* Miscellaneous initialisation including preparing the logging and seeding
\r
146 the random number generator. */
\r
147 prvMiscInitialisation();
\r
149 /* Initialise the network interface.
\r
151 ***NOTE*** Tasks that use the network are created in the network event hook
\r
152 when the network is connected and ready for use (see the definition of
\r
153 vApplicationIPNetworkEventHook() below). The address values passed in here
\r
154 are used if ipconfigUSE_DHCP is set to 0, or if ipconfigUSE_DHCP is set to 1
\r
155 but a DHCP server cannot be contacted. */
\r
156 FreeRTOS_debug_printf( ( "FreeRTOS_IPInit\n" ) );
\r
157 FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );
\r
159 /* Start the RTOS scheduler. */
\r
160 FreeRTOS_debug_printf( ("vTaskStartScheduler\n") );
\r
161 vTaskStartScheduler();
\r
163 /* If all is well, the scheduler will now be running, and the following
\r
164 line will never be reached. If the following line does execute, then
\r
165 there was insufficient FreeRTOS heap memory available for the idle and/or
\r
166 timer tasks to be created. See the memory management section on the
\r
167 FreeRTOS web site for more details (this is standard text that is not not
\r
168 really applicable to the Win32 simulator port). */
\r
171 Sleep( ulLongTime_ms );
\r
174 /*-----------------------------------------------------------*/
\r
176 void vApplicationIdleHook( void )
\r
178 const uint32_t ulMSToSleep = 1;
\r
180 /* This is just a trivial example of an idle hook. It is called on each
\r
181 cycle of the idle task if configUSE_IDLE_HOOK is set to 1 in
\r
182 FreeRTOSConfig.h. It must *NOT* attempt to block. In this case the
\r
183 idle task just sleeps to lower the CPU usage. */
\r
184 Sleep( ulMSToSleep );
\r
186 /*-----------------------------------------------------------*/
\r
188 void vAssertCalled( const char *pcFile, uint32_t ulLine )
\r
190 const uint32_t ulLongSleep = 1000UL;
\r
191 volatile uint32_t ulBlockVariable = 0UL;
\r
192 volatile char *pcFileName = ( volatile char * ) pcFile;
\r
193 volatile uint32_t ulLineNumber = ulLine;
\r
195 ( void ) pcFileName;
\r
196 ( void ) ulLineNumber;
\r
198 FreeRTOS_debug_printf( ( "vAssertCalled( %s, %ld\n", pcFile, ulLine ) );
\r
200 /* Setting ulBlockVariable to a non-zero value in the debugger will allow
\r
201 this function to be exited. */
\r
202 taskDISABLE_INTERRUPTS();
\r
204 while( ulBlockVariable == 0UL )
\r
206 Sleep( ulLongSleep );
\r
209 taskENABLE_INTERRUPTS();
\r
211 /*-----------------------------------------------------------*/
\r
213 /* Called by FreeRTOS+TCP when the network connects or disconnects. Disconnect
\r
214 events are only received if implemented in the MAC driver. */
\r
215 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
\r
217 uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress;
\r
218 char cBuffer[ 16 ];
\r
219 static BaseType_t xTasksAlreadyCreated = pdFALSE;
\r
221 /* If the network has just come up...*/
\r
222 if( eNetworkEvent == eNetworkUp )
\r
224 /* Create the tasks that use the IP stack if they have not already been
\r
226 if( xTasksAlreadyCreated == pdFALSE )
\r
228 /* See the comments above the definitions of these pre-processor
\r
229 macros at the top of this file for a description of the individual
\r
231 #if( mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS == 1 )
\r
233 vStartSimpleUDPClientServerTasks( configMINIMAL_STACK_SIZE, mainSIMPLE_UDP_CLIENT_SERVER_PORT, mainSIMPLE_UDP_CLIENT_SERVER_TASK_PRIORITY );
\r
235 #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */
\r
237 #if( mainCREATE_TCP_ECHO_TASKS_SINGLE == 1 )
\r
239 vStartTCPEchoClientTasks_SingleTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );
\r
241 #endif /* mainCREATE_TCP_ECHO_TASKS_SINGLE */
\r
243 #if( mainCREATE_TCP_ECHO_SERVER_TASK == 1 )
\r
245 vStartSimpleTCPServerTasks( mainECHO_SERVER_TASK_STACK_SIZE, mainECHO_SERVER_TASK_PRIORITY );
\r
249 xTasksAlreadyCreated = pdTRUE;
\r
252 /* Print out the network configuration, which may have come from a DHCP
\r
254 FreeRTOS_GetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress );
\r
255 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );
\r
256 FreeRTOS_printf( ( "\r\n\r\nIP Address: %s\r\n", cBuffer ) );
\r
258 FreeRTOS_inet_ntoa( ulNetMask, cBuffer );
\r
259 FreeRTOS_printf( ( "Subnet Mask: %s\r\n", cBuffer ) );
\r
261 FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );
\r
262 FreeRTOS_printf( ( "Gateway Address: %s\r\n", cBuffer ) );
\r
264 FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );
\r
265 FreeRTOS_printf( ( "DNS Server Address: %s\r\n\r\n\r\n", cBuffer ) );
\r
268 /*-----------------------------------------------------------*/
\r
270 void vApplicationMallocFailedHook( void )
\r
272 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
273 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
274 internally by FreeRTOS API functions that create tasks, queues, software
\r
275 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
276 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
277 vAssertCalled( __FILE__, __LINE__ );
\r
279 /*-----------------------------------------------------------*/
\r
281 UBaseType_t uxRand( void )
\r
283 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
\r
285 /* Utility function to generate a pseudo random number. */
\r
287 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
\r
288 return( ( int ) ( ulNextRand >> 16UL ) & 0x7fffUL );
\r
290 /*-----------------------------------------------------------*/
\r
292 static void prvSRand( UBaseType_t ulSeed )
\r
294 /* Utility function to seed the pseudo random number generator. */
\r
295 ulNextRand = ulSeed;
\r
297 /*-----------------------------------------------------------*/
\r
299 static void prvMiscInitialisation( void )
\r
302 uint32_t ulLoggingIPAddress;
\r
304 ulLoggingIPAddress = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0, configECHO_SERVER_ADDR1, configECHO_SERVER_ADDR2, configECHO_SERVER_ADDR3 );
\r
305 vLoggingInit( xLogToStdout, xLogToFile, xLogToUDP, ulLoggingIPAddress, configPRINT_PORT );
\r
307 /* Seed the random number generator. */
\r
309 FreeRTOS_debug_printf( ( "Seed for randomiser: %lu\n", xTimeNow ) );
\r
310 prvSRand( ( uint32_t ) xTimeNow );
\r
311 FreeRTOS_debug_printf( ( "Random numbers: %08X %08X %08X %08X\n", ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32(), ipconfigRAND32() ) );
\r
313 /*-----------------------------------------------------------*/
\r
315 #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) || ( ipconfigDHCP_REGISTER_HOSTNAME == 1 )
\r
317 const char *pcApplicationHostnameHook( void )
\r
319 /* Assign the name "FreeRTOS" to this network node. This function will
\r
320 be called during the DHCP: the machine will be registered with an IP
\r
321 address plus this name. */
\r
322 return mainHOST_NAME;
\r
326 /*-----------------------------------------------------------*/
\r
328 #if( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )
\r
330 BaseType_t xApplicationDNSQueryHook( const char *pcName )
\r
332 BaseType_t xReturn;
\r
334 /* Determine if a name lookup is for this node. Two names are given
\r
335 to this node: that returned by pcApplicationHostnameHook() and that set
\r
336 by mainDEVICE_NICK_NAME. */
\r
337 if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 )
\r
341 else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )
\r