]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Minimal_Windows_Simulator/DemoTasks/SimpleUDPClientAndServer.c
Added +TCP code to main repo.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_TCP_Minimal_Windows_Simulator / DemoTasks / SimpleUDPClientAndServer.c
diff --git a/FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Minimal_Windows_Simulator/DemoTasks/SimpleUDPClientAndServer.c b/FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Minimal_Windows_Simulator/DemoTasks/SimpleUDPClientAndServer.c
new file mode 100644 (file)
index 0000000..9e67c9a
--- /dev/null
@@ -0,0 +1,397 @@
+/*\r
+    FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.\r
+    All rights reserved\r
+\r
+    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
+\r
+    This file is part of the FreeRTOS distribution.\r
+\r
+    FreeRTOS is free software; you can redistribute it and/or modify it under\r
+    the terms of the GNU General Public License (version 2) as published by the\r
+    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
+\r
+    ***************************************************************************\r
+    >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
+    >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
+    >>!   obliged to provide the source code for proprietary components     !<<\r
+    >>!   outside of the FreeRTOS kernel.                                   !<<\r
+    ***************************************************************************\r
+\r
+    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
+    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
+    FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
+    link: http://www.freertos.org/a00114.html\r
+\r
+    ***************************************************************************\r
+     *                                                                       *\r
+     *    FreeRTOS provides completely free yet professionally developed,    *\r
+     *    robust, strictly quality controlled, supported, and cross          *\r
+     *    platform software that is more than just the market leader, it     *\r
+     *    is the industry's de facto standard.                               *\r
+     *                                                                       *\r
+     *    Help yourself get started quickly while simultaneously helping     *\r
+     *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
+     *    tutorial book, reference manual, or both:                          *\r
+     *    http://www.FreeRTOS.org/Documentation                              *\r
+     *                                                                       *\r
+    ***************************************************************************\r
+\r
+    http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
+    the FAQ page "My application does not run, what could be wrong?".  Have you\r
+    defined configASSERT()?\r
+\r
+    http://www.FreeRTOS.org/support - In return for receiving this top quality\r
+    embedded software for free we request you assist our global community by\r
+    participating in the support forum.\r
+\r
+    http://www.FreeRTOS.org/training - Investing in training allows your team to\r
+    be as productive as possible as early as possible.  Now you can receive\r
+    FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
+    Ltd, and the world's leading authority on the world's leading RTOS.\r
+\r
+    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
+    including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
+    compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
+\r
+    http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
+    Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
+\r
+    http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
+    Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
+    licenses offer ticketed support, indemnification and commercial middleware.\r
+\r
+    http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
+    engineered and independently SIL3 certified version for use in safety and\r
+    mission critical applications that require provable dependability.\r
+\r
+    1 tab == 4 spaces!\r
+*/\r
+\r
+/*\r
+ * Creates two transmitting tasks and two receiving tasks.  The transmitting\r
+ * tasks send values that are received by the receiving tasks.  One set of tasks\r
+ * uses the standard API.  The other set of tasks uses the zero copy API.\r
+ *\r
+ * See the following web page for essential demo usage and configuration\r
+ * details:\r
+ * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html\r
+ */\r
+\r
+/* Standard includes. */\r
+#include <stdint.h>\r
+#include <stdio.h>\r
+\r
+/* FreeRTOS includes. */\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+\r
+/* FreeRTOS+TCP includes. */\r
+#include "FreeRTOS_IP.h"\r
+#include "FreeRTOS_Sockets.h"\r
+\r
+#define simpTINY_DELAY ( ( TickType_t ) 2 )\r
+\r
+/*\r
+ * Uses a socket to send data without using the zero copy option.\r
+ * prvSimpleServerTask() will receive the data.\r
+ */\r
+static void prvSimpleClientTask( void *pvParameters );\r
+\r
+/*\r
+ * Uses a socket to receive the data sent by the prvSimpleClientTask() task.\r
+ * Does not use the zero copy option.\r
+ */\r
+static void prvSimpleServerTask( void *pvParameters );\r
+\r
+/*\r
+ * Uses a socket to send data using the zero copy option.\r
+ * prvSimpleZeroCopyServerTask() will receive the data.\r
+ */\r
+static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );\r
+\r
+/*\r
+ * Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()\r
+ * task.  Uses the zero copy option.\r
+ */\r
+static void prvSimpleZeroCopyServerTask( void *pvParameters );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )\r
+{\r
+       /* Create the client and server tasks that do not use the zero copy\r
+       interface. */\r
+       xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );\r
+       xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );\r
+\r
+       /* Create the client and server tasks that do use the zero copy interface. */\r
+       xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );\r
+       xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSimpleClientTask( void *pvParameters )\r
+{\r
+Socket_t xClientSocket;\r
+struct freertos_sockaddr xDestinationAddress;\r
+uint8_t cString[ 50 ];\r
+BaseType_t lReturned;\r
+uint32_t ulCount = 0UL, ulIPAddress;\r
+const uint32_t ulLoopsPerSocket = 10UL;\r
+const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;\r
+\r
+       /* Remove compiler warning about unused parameters. */\r
+       ( void ) pvParameters;\r
+\r
+       /* It is assumed that this task is not created until the network is up,\r
+       so the IP address can be obtained immediately.  store the IP address being\r
+       used in ulIPAddress.  This is done so the socket can send to a different\r
+       port on the same IP address. */\r
+       FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
+\r
+       /* This test sends to itself, so data sent from here is received by a server\r
+       socket on the same IP address.  Setup the freertos_sockaddr structure with\r
+       this nodes IP address, and the port number being sent to.  The strange\r
+       casting is to try and remove compiler warnings on 32 bit machines. */\r
+       xDestinationAddress.sin_addr = ulIPAddress;\r
+       xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
+       xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
+\r
+       for( ;; )\r
+       {\r
+               /* Create the socket. */\r
+               xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
+               configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );\r
+\r
+               /* The count is used to differentiate between different messages sent to\r
+               the server, and to break out of the do while loop below. */\r
+               ulCount = 0UL;\r
+\r
+               do\r
+               {\r
+                       /* Create the string that is sent to the server. */\r
+                       sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );\r
+\r
+                       /* Send the string to the socket.  ulFlags is set to 0, so the zero\r
+                       copy option is not selected.  That means the data from cString[] is\r
+                       copied into a network buffer inside FreeRTOS_sendto(), and cString[]\r
+                       can be reused as soon as FreeRTOS_sendto() has returned. */\r
+                       lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );\r
+\r
+                       ulCount++;\r
+\r
+               } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );\r
+\r
+               FreeRTOS_closesocket( xClientSocket );\r
+\r
+               /* A short delay to prevent the messages printed by the server task\r
+               scrolling off the screen too quickly, and to prevent reduce the network\r
+               loading. */\r
+               vTaskDelay( x150ms );\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSimpleServerTask( void *pvParameters )\r
+{\r
+int32_t lBytes;\r
+uint8_t cReceivedString[ 60 ];\r
+struct freertos_sockaddr xClient, xBindAddress;\r
+uint32_t xClientLength = sizeof( xClient );\r
+Socket_t xListeningSocket;\r
+\r
+       /* Just to prevent compiler warnings. */\r
+       ( void ) pvParameters;\r
+\r
+       /* Attempt to open the socket. */\r
+       xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
+       configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );\r
+\r
+       /* This test receives data sent from a different port on the same IP\r
+       address.  Configure the freertos_sockaddr structure with the address being\r
+       bound to.  The strange casting is to try and remove     compiler warnings on 32\r
+       bit machines.  Note that this task is only created after the network is up,\r
+       so the IP address is valid here. */\r
+       xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
+       xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );\r
+\r
+       /* Bind the socket to the port that the client task will send to. */\r
+       FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );\r
+\r
+       for( ;; )\r
+       {\r
+               /* Zero out the receive array so there is NULL at the end of the string\r
+               when it is printed out. */\r
+               memset( cReceivedString, 0x00, sizeof( cReceivedString ) );\r
+\r
+               /* Receive data on the socket.  ulFlags is zero, so the zero copy option\r
+               is not set and the received data is copied into the buffer pointed to by\r
+               cReceivedString.  By default the block time is portMAX_DELAY.\r
+               xClientLength is not actually used by FreeRTOS_recvfrom(), but is set\r
+               appropriately in case future versions do use it. */\r
+               lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );\r
+\r
+               /* Error check. */\r
+               configASSERT( lBytes == ( BaseType_t ) strlen( ( const char * ) cReceivedString ) );\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )\r
+{\r
+Socket_t xClientSocket;\r
+uint8_t *pucUDPPayloadBuffer;\r
+struct freertos_sockaddr xDestinationAddress;\r
+BaseType_t lReturned;\r
+uint32_t ulCount = 0UL, ulIPAddress;\r
+const uint32_t ulLoopsPerSocket = 10UL;\r
+const char *pcStringToSend = "Server received (using zero copy): Message number ";\r
+const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;\r
+/* 15 is added to ensure the number, \r\n and terminating zero fit. */\r
+const size_t xStringLength = strlen( pcStringToSend ) + 15;\r
+\r
+       /* Remove compiler warning about unused parameters. */\r
+       ( void ) pvParameters;\r
+\r
+       /* It is assumed that this task is not created until the network is up,\r
+       so the IP address can be obtained immediately.  store the IP address being\r
+       used in ulIPAddress.  This is done so the socket can send to a different\r
+       port on the same IP address. */\r
+       FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
+\r
+       /* This test sends to itself, so data sent from here is received by a server\r
+       socket on the same IP address.  Setup the freertos_sockaddr structure with\r
+       this nodes IP address, and the port number being sent to.  The strange\r
+       casting is to try and remove compiler warnings on 32 bit machines. */\r
+       xDestinationAddress.sin_addr = ulIPAddress;\r
+       xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
+       xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );\r
+\r
+       for( ;; )\r
+       {\r
+               /* Create the socket. */\r
+               xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
+               configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );\r
+\r
+               /* The count is used to differentiate between different messages sent to\r
+               the server, and to break out of the do while loop below. */\r
+               ulCount = 0UL;\r
+\r
+               do\r
+               {\r
+                       /* This task is going to send using the zero copy interface.  The\r
+                       data being sent is therefore written directly into a buffer that is\r
+                       passed into, rather than copied into, the FreeRTOS_sendto()\r
+                       function.\r
+\r
+                       First obtain a buffer of adequate length from the IP stack into which\r
+                       the string will be written.  Although a max delay is used, the actual\r
+                       delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence\r
+                       the do while loop is used to ensure a buffer is obtained. */\r
+                       do\r
+                       {\r
+                       } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );\r
+\r
+                       /* A buffer was successfully obtained.  Create the string that is\r
+                       sent to the server.  First the string is filled with zeros as this will\r
+                       effectively be the null terminator when the string is received at the other\r
+                       end.  Note that the string is being written directly into the buffer\r
+                       obtained from the IP stack above. */\r
+                       memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );\r
+                       sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", pcStringToSend, ulCount );\r
+\r
+                       /* Pass the buffer into the send function.  ulFlags has the\r
+                       FREERTOS_ZERO_COPY bit set so the IP stack will take control of the\r
+                       buffer rather than copy data out of the buffer. */\r
+                       lReturned = FreeRTOS_sendto( xClientSocket,                             /* The socket being sent to. */\r
+                                                                               ( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */\r
+                                                                               strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */\r
+                                                                               FREERTOS_ZERO_COPY,                     /* ulFlags with the FREERTOS_ZERO_COPY bit set. */\r
+                                                                               &xDestinationAddress,                   /* Where the data is being sent. */\r
+                                                                               sizeof( xDestinationAddress ) );\r
+\r
+                       if( lReturned == 0 )\r
+                       {\r
+                               /* The send operation failed, so this task is still responsible\r
+                               for the buffer obtained from the IP stack.  To ensure the buffer\r
+                               is not lost it must either be used again, or, as in this case,\r
+                               returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().\r
+                               pucUDPPayloadBuffer can be safely re-used after this call. */\r
+                               FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );\r
+                       }\r
+                       else\r
+                       {\r
+                               /* The send was successful so the IP stack is now managing the\r
+                               buffer pointed to by pucUDPPayloadBuffer, and the IP stack will\r
+                               return the buffer once it has been sent.  pucUDPPayloadBuffer can\r
+                               be safely re-used. */\r
+                       }\r
+\r
+                       ulCount++;\r
+\r
+               } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );\r
+\r
+               FreeRTOS_closesocket( xClientSocket );\r
+\r
+               /* A short delay to prevent the messages scrolling off the screen too\r
+               quickly. */\r
+               vTaskDelay( x150ms );\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSimpleZeroCopyServerTask( void *pvParameters )\r
+{\r
+int32_t lBytes;\r
+uint8_t *pucUDPPayloadBuffer;\r
+struct freertos_sockaddr xClient, xBindAddress;\r
+uint32_t xClientLength = sizeof( xClient ), ulIPAddress;\r
+Socket_t xListeningSocket;\r
+\r
+       /* Just to prevent compiler warnings. */\r
+       ( void ) pvParameters;\r
+\r
+       /* Attempt to open the socket. */\r
+       xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
+       configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );\r
+\r
+       /* This test receives data sent from a different port on the same IP address.\r
+       Obtain the nodes IP address.  Configure the freertos_sockaddr structure with\r
+       the address being bound to.  The strange casting is to try and remove\r
+       compiler warnings on 32 bit machines.  Note that this task is only created\r
+       after the network is up, so the IP address is valid here. */\r
+       FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );\r
+       xBindAddress.sin_addr = ulIPAddress;\r
+       xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;\r
+       xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );\r
+\r
+       /* Bind the socket to the port that the client task will send to. */\r
+       FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );\r
+\r
+       for( ;; )\r
+       {\r
+               /* Receive data on the socket.  ulFlags has the zero copy bit set\r
+               (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the\r
+               received data should be passed out to this task using the second\r
+               parameter to the FreeRTOS_recvfrom() call.  When this is done the\r
+               IP stack is no longer responsible for releasing the buffer, and\r
+               the task *must* return the buffer to the stack when it is no longer\r
+               needed.  By default the block time is portMAX_DELAY. */\r
+               lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );\r
+\r
+               /* Print the received characters. */\r
+               if( lBytes > 0 )\r
+               {\r
+                       /* It is expected to receive one more byte than the string length as\r
+                       the NULL terminator is also transmitted. */\r
+                       configASSERT( lBytes == ( ( BaseType_t ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );\r
+               }\r
+\r
+               if( lBytes >= 0 )\r
+               {\r
+                       /* The buffer *must* be freed once it is no longer needed. */\r
+                       FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );\r
+               }\r
+       }\r
+}\r
+\r