2 * FreeRTOS Kernel V10.2.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 * Creates two transmitting tasks and two receiving tasks. The transmitting
\r
30 * tasks send values that are received by the receiving tasks. One set of tasks
\r
31 * uses the standard API. The other set of tasks uses the zero copy API.
\r
33 * See the following web page for essential demo usage and configuration
\r
35 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
38 /* Standard includes. */
\r
42 /* FreeRTOS includes. */
\r
43 #include "FreeRTOS.h"
\r
46 /* FreeRTOS+TCP includes. */
\r
47 #include "FreeRTOS_IP.h"
\r
48 #include "FreeRTOS_Sockets.h"
\r
50 #if( configASSERT_DEFINED == 0 )
\r
51 #error This demo uses configASSERT() to trap errors. configASSERT() must be defined in FreeRTOSConfig.h https://www.freertos.org/a00110.html#configASSERT
\r
54 #define simpTINY_DELAY ( ( TickType_t ) 2 )
\r
57 * Uses a socket to send data without using the zero copy option.
\r
58 * prvSimpleServerTask() will receive the data.
\r
60 static void prvSimpleClientTask( void *pvParameters );
\r
63 * Uses a socket to receive the data sent by the prvSimpleClientTask() task.
\r
64 * Does not use the zero copy option.
\r
66 static void prvSimpleServerTask( void *pvParameters );
\r
69 * Uses a socket to send data using the zero copy option.
\r
70 * prvSimpleZeroCopyServerTask() will receive the data.
\r
72 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );
\r
75 * Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()
\r
76 * task. Uses the zero copy option.
\r
78 static void prvSimpleZeroCopyServerTask( void *pvParameters );
\r
80 /*-----------------------------------------------------------*/
\r
82 void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
\r
84 /* Create the client and server tasks that do not use the zero copy
\r
86 xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );
\r
87 xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );
\r
89 /* Create the client and server tasks that do use the zero copy interface. */
\r
90 xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );
\r
91 xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );
\r
93 /*-----------------------------------------------------------*/
\r
95 static void prvSimpleClientTask( void *pvParameters )
\r
97 Socket_t xClientSocket;
\r
98 struct freertos_sockaddr xDestinationAddress;
\r
99 uint8_t cString[ 65 ];
\r
100 BaseType_t lReturned;
\r
101 uint32_t ulCount = 0UL, ulIPAddress;
\r
102 const uint32_t ulLoopsPerSocket = 10UL;
\r
103 const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;
\r
105 /* Remove compiler warning about unused parameters. */
\r
106 ( void ) pvParameters;
\r
108 FreeRTOS_printf( ( "Starting prvSimpleClientTask\r\n" ) );
\r
110 /* It is assumed that this task is not created until the network is up,
\r
111 so the IP address can be obtained immediately. store the IP address being
\r
112 used in ulIPAddress. This is done so the socket can send to a different
\r
113 port on the same IP address. */
\r
114 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
116 /* This test sends to itself, so data sent from here is received by a server
\r
117 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
118 this nodes IP address, and the port number being sent to. The strange
\r
119 casting is to try and remove compiler warnings on 32 bit machines. */
\r
120 xDestinationAddress.sin_addr = ulIPAddress;
\r
121 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
122 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
126 /* Create the socket. */
\r
127 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
128 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
130 /* The count is used to differentiate between different messages sent to
\r
131 the server, and to break out of the do while loop below. */
\r
136 /* Create the string that is sent to the server. */
\r
137 sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );
\r
139 /* Send the string to the socket. ulFlags is set to 0, so the zero
\r
140 copy option is not selected. That means the data from cString[] is
\r
141 copied into a network buffer inside FreeRTOS_sendto(), and cString[]
\r
142 can be reused as soon as FreeRTOS_sendto() has returned. */
\r
143 lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
\r
147 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
149 FreeRTOS_closesocket( xClientSocket );
\r
151 /* A short delay to prevent the messages printed by the server task
\r
152 scrolling off the screen too quickly, and to prevent reduce the network
\r
154 vTaskDelay( x150ms );
\r
157 /*-----------------------------------------------------------*/
\r
159 static void prvSimpleServerTask( void *pvParameters )
\r
162 uint8_t cReceivedString[ 60 ];
\r
163 struct freertos_sockaddr xClient, xBindAddress;
\r
164 uint32_t xClientLength = sizeof( xClient );
\r
165 Socket_t xListeningSocket;
\r
167 /* Just to prevent compiler warnings. */
\r
168 ( void ) pvParameters;
\r
170 FreeRTOS_printf( ( "Starting prvSimpleServerTask\r\n" ) );
\r
172 /* Attempt to open the socket. */
\r
173 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
174 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
176 /* This test receives data sent from a different port on the same IP
\r
177 address. Configure the freertos_sockaddr structure with the address being
\r
178 bound to. The strange casting is to try and remove compiler warnings on 32
\r
179 bit machines. Note that this task is only created after the network is up,
\r
180 so the IP address is valid here. */
\r
181 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
182 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
184 /* Bind the socket to the port that the client task will send to. */
\r
185 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
189 /* Zero out the receive array so there is NULL at the end of the string
\r
190 when it is printed out. */
\r
191 memset( cReceivedString, 0x00, sizeof( cReceivedString ) );
\r
193 /* Receive data on the socket. ulFlags is zero, so the zero copy option
\r
194 is not set and the received data is copied into the buffer pointed to by
\r
195 cReceivedString. By default the block time is portMAX_DELAY.
\r
196 xClientLength is not actually used by FreeRTOS_recvfrom(), but is set
\r
197 appropriately in case future versions do use it. */
\r
198 lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );
\r
201 configASSERT( lBytes == ( BaseType_t ) strlen( ( const char * ) cReceivedString ) );
\r
202 FreeRTOS_printf( ( "prvSimpleServerTask() recieved %s\r\n", ( const char * ) cReceivedString ) );
\r
205 /*-----------------------------------------------------------*/
\r
207 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )
\r
209 Socket_t xClientSocket;
\r
210 uint8_t *pucUDPPayloadBuffer;
\r
211 struct freertos_sockaddr xDestinationAddress;
\r
212 BaseType_t lReturned;
\r
213 uint32_t ulCount = 0UL, ulIPAddress;
\r
214 const uint32_t ulLoopsPerSocket = 10UL;
\r
215 const char *pcStringToSend = "Server received (using zero copy): Message number ";
\r
216 const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;
\r
217 /* 15 is added to ensure the number, \r\n and terminating zero fit. */
\r
218 const size_t xStringLength = strlen( pcStringToSend ) + 15;
\r
220 /* Remove compiler warning about unused parameters. */
\r
221 ( void ) pvParameters;
\r
223 FreeRTOS_printf( ( "Starting prvSimpleZeroCopyUDPClientTask\r\n" ) );
\r
225 /* It is assumed that this task is not created until the network is up,
\r
226 so the IP address can be obtained immediately. store the IP address being
\r
227 used in ulIPAddress. This is done so the socket can send to a different
\r
228 port on the same IP address. */
\r
229 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
231 /* This test sends to itself, so data sent from here is received by a server
\r
232 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
233 this nodes IP address, and the port number being sent to. The strange
\r
234 casting is to try and remove compiler warnings on 32 bit machines. */
\r
235 xDestinationAddress.sin_addr = ulIPAddress;
\r
236 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
237 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
241 /* Create the socket. */
\r
242 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
243 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
245 /* The count is used to differentiate between different messages sent to
\r
246 the server, and to break out of the do while loop below. */
\r
251 /* This task is going to send using the zero copy interface. The
\r
252 data being sent is therefore written directly into a buffer that is
\r
253 passed into, rather than copied into, the FreeRTOS_sendto()
\r
256 First obtain a buffer of adequate length from the IP stack into which
\r
257 the string will be written. Although a max delay is used, the actual
\r
258 delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence
\r
259 the do while loop is used to ensure a buffer is obtained. */
\r
262 } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );
\r
264 /* A buffer was successfully obtained. Create the string that is
\r
265 sent to the server. First the string is filled with zeros as this will
\r
266 effectively be the null terminator when the string is received at the other
\r
267 end. Note that the string is being written directly into the buffer
\r
268 obtained from the IP stack above. */
\r
269 memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );
\r
270 sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", pcStringToSend, ulCount );
\r
272 /* Pass the buffer into the send function. ulFlags has the
\r
273 FREERTOS_ZERO_COPY bit set so the IP stack will take control of the
\r
274 buffer rather than copy data out of the buffer. */
\r
275 lReturned = FreeRTOS_sendto( xClientSocket, /* The socket being sent to. */
\r
276 ( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */
\r
277 strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */
\r
278 FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
\r
279 &xDestinationAddress, /* Where the data is being sent. */
\r
280 sizeof( xDestinationAddress ) );
\r
282 if( lReturned == 0 )
\r
284 /* The send operation failed, so this task is still responsible
\r
285 for the buffer obtained from the IP stack. To ensure the buffer
\r
286 is not lost it must either be used again, or, as in this case,
\r
287 returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().
\r
288 pucUDPPayloadBuffer can be safely re-used after this call. */
\r
289 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
293 /* The send was successful so the IP stack is now managing the
\r
294 buffer pointed to by pucUDPPayloadBuffer, and the IP stack will
\r
295 return the buffer once it has been sent. pucUDPPayloadBuffer can
\r
296 be safely re-used. */
\r
301 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
303 FreeRTOS_closesocket( xClientSocket );
\r
305 /* A short delay to prevent the messages scrolling off the screen too
\r
307 vTaskDelay( x150ms );
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvSimpleZeroCopyServerTask( void *pvParameters )
\r
315 uint8_t *pucUDPPayloadBuffer;
\r
316 struct freertos_sockaddr xClient, xBindAddress;
\r
317 uint32_t xClientLength = sizeof( xClient ), ulIPAddress;
\r
318 Socket_t xListeningSocket;
\r
320 /* Just to prevent compiler warnings. */
\r
321 ( void ) pvParameters;
\r
323 FreeRTOS_printf( ( "Starting prvSimpleZeroCopyServerTask\r\n" ) );
\r
325 /* Attempt to open the socket. */
\r
326 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
327 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
329 /* This test receives data sent from a different port on the same IP address.
\r
330 Obtain the nodes IP address. Configure the freertos_sockaddr structure with
\r
331 the address being bound to. The strange casting is to try and remove
\r
332 compiler warnings on 32 bit machines. Note that this task is only created
\r
333 after the network is up, so the IP address is valid here. */
\r
334 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
335 xBindAddress.sin_addr = ulIPAddress;
\r
336 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
337 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
339 /* Bind the socket to the port that the client task will send to. */
\r
340 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
344 /* Receive data on the socket. ulFlags has the zero copy bit set
\r
345 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the
\r
346 received data should be passed out to this task using the second
\r
347 parameter to the FreeRTOS_recvfrom() call. When this is done the
\r
348 IP stack is no longer responsible for releasing the buffer, and
\r
349 the task *must* return the buffer to the stack when it is no longer
\r
350 needed. By default the block time is portMAX_DELAY. */
\r
351 lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
\r
353 /* Print the received characters. */
\r
356 /* It is expected to receive one more byte than the string length as
\r
357 the NULL terminator is also transmitted. */
\r
358 configASSERT( lBytes == ( ( BaseType_t ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );
\r
359 FreeRTOS_printf( ( "prvSimpleZeroCopyServerTask() recieved %s\r\n", ( const char * ) pucUDPPayloadBuffer ) );
\r
364 /* The buffer *must* be freed once it is no longer needed. */
\r
365 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r