2 * FreeRTOS Kernel V10.0.0
\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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Creates two transmitting tasks and two receiving tasks. The transmitting
\r
31 * tasks send values that are received by the receiving tasks. One set of tasks
\r
32 * uses the standard API. The other set of tasks uses the zero copy API.
\r
34 * See the following web page for essential demo usage and configuration
\r
36 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/examples_FreeRTOS_simulator.html
\r
39 /* Standard includes. */
\r
43 /* FreeRTOS includes. */
\r
44 #include "FreeRTOS.h"
\r
47 /* FreeRTOS+TCP includes. */
\r
48 #include "FreeRTOS_IP.h"
\r
49 #include "FreeRTOS_Sockets.h"
\r
51 #define simpTINY_DELAY ( ( TickType_t ) 2 )
\r
54 * Uses a socket to send data without using the zero copy option.
\r
55 * prvSimpleServerTask() will receive the data.
\r
57 static void prvSimpleClientTask( void *pvParameters );
\r
60 * Uses a socket to receive the data sent by the prvSimpleClientTask() task.
\r
61 * Does not use the zero copy option.
\r
63 static void prvSimpleServerTask( void *pvParameters );
\r
66 * Uses a socket to send data using the zero copy option.
\r
67 * prvSimpleZeroCopyServerTask() will receive the data.
\r
69 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );
\r
72 * Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()
\r
73 * task. Uses the zero copy option.
\r
75 static void prvSimpleZeroCopyServerTask( void *pvParameters );
\r
77 /*-----------------------------------------------------------*/
\r
79 void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
\r
81 /* Create the client and server tasks that do not use the zero copy
\r
83 xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );
\r
84 xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );
\r
86 /* Create the client and server tasks that do use the zero copy interface. */
\r
87 xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );
\r
88 xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );
\r
90 /*-----------------------------------------------------------*/
\r
92 static void prvSimpleClientTask( void *pvParameters )
\r
94 Socket_t xClientSocket;
\r
95 struct freertos_sockaddr xDestinationAddress;
\r
96 uint8_t cString[ 50 ];
\r
97 BaseType_t lReturned;
\r
98 uint32_t ulCount = 0UL, ulIPAddress;
\r
99 const uint32_t ulLoopsPerSocket = 10UL;
\r
100 const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;
\r
102 /* Remove compiler warning about unused parameters. */
\r
103 ( void ) pvParameters;
\r
105 /* It is assumed that this task is not created until the network is up,
\r
106 so the IP address can be obtained immediately. store the IP address being
\r
107 used in ulIPAddress. This is done so the socket can send to a different
\r
108 port on the same IP address. */
\r
109 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
111 /* This test sends to itself, so data sent from here is received by a server
\r
112 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
113 this nodes IP address, and the port number being sent to. The strange
\r
114 casting is to try and remove compiler warnings on 32 bit machines. */
\r
115 xDestinationAddress.sin_addr = ulIPAddress;
\r
116 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
117 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
121 /* Create the socket. */
\r
122 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
123 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
125 /* The count is used to differentiate between different messages sent to
\r
126 the server, and to break out of the do while loop below. */
\r
131 /* Create the string that is sent to the server. */
\r
132 sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );
\r
134 /* Send the string to the socket. ulFlags is set to 0, so the zero
\r
135 copy option is not selected. That means the data from cString[] is
\r
136 copied into a network buffer inside FreeRTOS_sendto(), and cString[]
\r
137 can be reused as soon as FreeRTOS_sendto() has returned. */
\r
138 lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
\r
142 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
144 FreeRTOS_closesocket( xClientSocket );
\r
146 /* A short delay to prevent the messages printed by the server task
\r
147 scrolling off the screen too quickly, and to prevent reduce the network
\r
149 vTaskDelay( x150ms );
\r
152 /*-----------------------------------------------------------*/
\r
154 static void prvSimpleServerTask( void *pvParameters )
\r
157 uint8_t cReceivedString[ 60 ];
\r
158 struct freertos_sockaddr xClient, xBindAddress;
\r
159 uint32_t xClientLength = sizeof( xClient );
\r
160 Socket_t xListeningSocket;
\r
162 /* Just to prevent compiler warnings. */
\r
163 ( void ) pvParameters;
\r
165 /* Attempt to open the socket. */
\r
166 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
167 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
169 /* This test receives data sent from a different port on the same IP
\r
170 address. Configure the freertos_sockaddr structure with the address being
\r
171 bound to. The strange casting is to try and remove compiler warnings on 32
\r
172 bit machines. Note that this task is only created after the network is up,
\r
173 so the IP address is valid here. */
\r
174 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
175 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
177 /* Bind the socket to the port that the client task will send to. */
\r
178 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
182 /* Zero out the receive array so there is NULL at the end of the string
\r
183 when it is printed out. */
\r
184 memset( cReceivedString, 0x00, sizeof( cReceivedString ) );
\r
186 /* Receive data on the socket. ulFlags is zero, so the zero copy option
\r
187 is not set and the received data is copied into the buffer pointed to by
\r
188 cReceivedString. By default the block time is portMAX_DELAY.
\r
189 xClientLength is not actually used by FreeRTOS_recvfrom(), but is set
\r
190 appropriately in case future versions do use it. */
\r
191 lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );
\r
194 configASSERT( lBytes == ( BaseType_t ) strlen( ( const char * ) cReceivedString ) );
\r
197 /*-----------------------------------------------------------*/
\r
199 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )
\r
201 Socket_t xClientSocket;
\r
202 uint8_t *pucUDPPayloadBuffer;
\r
203 struct freertos_sockaddr xDestinationAddress;
\r
204 BaseType_t lReturned;
\r
205 uint32_t ulCount = 0UL, ulIPAddress;
\r
206 const uint32_t ulLoopsPerSocket = 10UL;
\r
207 const char *pcStringToSend = "Server received (using zero copy): Message number ";
\r
208 const TickType_t x150ms = 150UL / portTICK_PERIOD_MS;
\r
209 /* 15 is added to ensure the number, \r\n and terminating zero fit. */
\r
210 const size_t xStringLength = strlen( pcStringToSend ) + 15;
\r
212 /* Remove compiler warning about unused parameters. */
\r
213 ( void ) pvParameters;
\r
215 /* It is assumed that this task is not created until the network is up,
\r
216 so the IP address can be obtained immediately. store the IP address being
\r
217 used in ulIPAddress. This is done so the socket can send to a different
\r
218 port on the same IP address. */
\r
219 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
221 /* This test sends to itself, so data sent from here is received by a server
\r
222 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
223 this nodes IP address, and the port number being sent to. The strange
\r
224 casting is to try and remove compiler warnings on 32 bit machines. */
\r
225 xDestinationAddress.sin_addr = ulIPAddress;
\r
226 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
227 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
231 /* Create the socket. */
\r
232 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
233 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
235 /* The count is used to differentiate between different messages sent to
\r
236 the server, and to break out of the do while loop below. */
\r
241 /* This task is going to send using the zero copy interface. The
\r
242 data being sent is therefore written directly into a buffer that is
\r
243 passed into, rather than copied into, the FreeRTOS_sendto()
\r
246 First obtain a buffer of adequate length from the IP stack into which
\r
247 the string will be written. Although a max delay is used, the actual
\r
248 delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence
\r
249 the do while loop is used to ensure a buffer is obtained. */
\r
252 } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );
\r
254 /* A buffer was successfully obtained. Create the string that is
\r
255 sent to the server. First the string is filled with zeros as this will
\r
256 effectively be the null terminator when the string is received at the other
\r
257 end. Note that the string is being written directly into the buffer
\r
258 obtained from the IP stack above. */
\r
259 memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );
\r
260 sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", pcStringToSend, ulCount );
\r
262 /* Pass the buffer into the send function. ulFlags has the
\r
263 FREERTOS_ZERO_COPY bit set so the IP stack will take control of the
\r
264 buffer rather than copy data out of the buffer. */
\r
265 lReturned = FreeRTOS_sendto( xClientSocket, /* The socket being sent to. */
\r
266 ( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */
\r
267 strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */
\r
268 FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
\r
269 &xDestinationAddress, /* Where the data is being sent. */
\r
270 sizeof( xDestinationAddress ) );
\r
272 if( lReturned == 0 )
\r
274 /* The send operation failed, so this task is still responsible
\r
275 for the buffer obtained from the IP stack. To ensure the buffer
\r
276 is not lost it must either be used again, or, as in this case,
\r
277 returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().
\r
278 pucUDPPayloadBuffer can be safely re-used after this call. */
\r
279 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
283 /* The send was successful so the IP stack is now managing the
\r
284 buffer pointed to by pucUDPPayloadBuffer, and the IP stack will
\r
285 return the buffer once it has been sent. pucUDPPayloadBuffer can
\r
286 be safely re-used. */
\r
291 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
293 FreeRTOS_closesocket( xClientSocket );
\r
295 /* A short delay to prevent the messages scrolling off the screen too
\r
297 vTaskDelay( x150ms );
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvSimpleZeroCopyServerTask( void *pvParameters )
\r
305 uint8_t *pucUDPPayloadBuffer;
\r
306 struct freertos_sockaddr xClient, xBindAddress;
\r
307 uint32_t xClientLength = sizeof( xClient ), ulIPAddress;
\r
308 Socket_t xListeningSocket;
\r
310 /* Just to prevent compiler warnings. */
\r
311 ( void ) pvParameters;
\r
313 /* Attempt to open the socket. */
\r
314 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
315 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
317 /* This test receives data sent from a different port on the same IP address.
\r
318 Obtain the nodes IP address. Configure the freertos_sockaddr structure with
\r
319 the address being bound to. The strange casting is to try and remove
\r
320 compiler warnings on 32 bit machines. Note that this task is only created
\r
321 after the network is up, so the IP address is valid here. */
\r
322 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
323 xBindAddress.sin_addr = ulIPAddress;
\r
324 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
325 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
327 /* Bind the socket to the port that the client task will send to. */
\r
328 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
332 /* Receive data on the socket. ulFlags has the zero copy bit set
\r
333 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the
\r
334 received data should be passed out to this task using the second
\r
335 parameter to the FreeRTOS_recvfrom() call. When this is done the
\r
336 IP stack is no longer responsible for releasing the buffer, and
\r
337 the task *must* return the buffer to the stack when it is no longer
\r
338 needed. By default the block time is portMAX_DELAY. */
\r
339 lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
\r
341 /* Print the received characters. */
\r
344 /* It is expected to receive one more byte than the string length as
\r
345 the NULL terminator is also transmitted. */
\r
346 configASSERT( lBytes == ( ( BaseType_t ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );
\r
351 /* The buffer *must* be freed once it is no longer needed. */
\r
352 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r