static void vDNS_callback( const char *pcName, void *pvSearchID, uint32_t ulIPAddress ) { char pcBuf[16]; /* The DNS lookup has a result, or it has reached the time-out. */ FreeRTOS_inet_ntoa( ulIPAddress, pcBuf ); FreeRTOS_printf( ( "IP address of %s found: %s\n", pcName, pcBuf ) ); if( ulIPAddressFound == 0ul ) { ulIPAddressFound = ulIPAddress; } /* For testing: in case DNS doen't respond, still try some NTP server with a known IP-address. */ if( ulIPAddressFound == 0ul ) { ulIPAddressFound = FreeRTOS_inet_addr_quick( 184, 105, 182, 7 ); /* ulIPAddressFound = FreeRTOS_inet_addr_quick( 103, 242, 70, 4 ); */ } xStatus = EStatusAsking; vSignalTask(); }
static void prvZeroCopyEchoClientTask( void *pvParameters ) { xSocket_t xSocket; struct freertos_sockaddr xEchoServerAddress; static char cTxString[ 40 ]; int32_t lLoopCount = 0UL; volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL; uint32_t xAddressLength = sizeof( xEchoServerAddress ); int32_t lReturned; uint8_t *pucUDPPayloadBuffer; const int32_t lMaxLoopCount = 50; const char * const pcStringToSend = "Zero copy message number"; /* The buffer is large enough to hold the string, a number, and the string terminator. */ const size_t xBufferLength = strlen( pcStringToSend ) + 15; #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 { /* When the trace recorder code is included user events are generated to mark the sending and receiving of the echoed data (only in the zero copy task). */ xZeroCopySendEvent = xTraceOpenLabel( "ZeroCopyTx" ); xZeroCopyReceiveEvent = xTraceOpenLabel( "ZeroCopyRx" ); } #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS */ /* Remove compiler warning about unused parameters. */ ( void ) pvParameters; /* Delay for a little while to ensure the task is out of synch with the other echo task implemented above. */ vTaskDelay( echoLOOP_DELAY >> 1 ); /* Echo requests are sent to the echo server. The address of the echo server is configured by the constants configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */ xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT ); xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0, configECHO_SERVER_ADDR1, configECHO_SERVER_ADDR2, configECHO_SERVER_ADDR3 ); for( ;; ) { /* Create a socket. */ xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP ); configASSERT( xSocket != FREERTOS_INVALID_SOCKET ); /* Set a time out so a missing reply does not cause the task to block indefinitely. */ FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) ); /* Send a number of echo requests. */ for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ ) { /* This task is going to send using the zero copy interface. The data being sent is therefore written directly into a buffer that is passed by reference into the FreeRTOS_sendto() function. First obtain a buffer of adequate size from the IP stack. Although a max delay is used, the actual delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence the test to ensure a buffer was actually obtained. */ pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xBufferLength, portMAX_DELAY ); if( pucUDPPayloadBuffer != NULL ) { /* A buffer was successfully obtained. Create the string that is sent to the echo server. Note the string is written directly into the buffer obtained from the IP stack. */ sprintf( ( char * ) pucUDPPayloadBuffer, "%s %u\r\n", "Zero copy message number", ( unsigned int ) ulTxCount ); /* Also copy the string into a local buffer so it can be compared with the string that is later received back from the echo server. */ strcpy( cTxString, ( char * ) pucUDPPayloadBuffer ); /* Pass the buffer into the send function. ulFlags has the FREERTOS_ZERO_COPY bit set so the IP stack will take control of the buffer, rather than copy data out of the buffer. */ echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopySendEvent ); lReturned = FreeRTOS_sendto( xSocket, /* The socket being sent to. */ ( void * ) pucUDPPayloadBuffer, /* The buffer being passed into the IP stack. */ strlen( cTxString ) + 1, /* The length of the data being sent. Plus 1 to ensure the null terminator is part of the data. */ FREERTOS_ZERO_COPY, /* ulFlags with the zero copy bit is set. */ &xEchoServerAddress, /* Where the data is being sent. */ sizeof( xEchoServerAddress ) ); if( lReturned == 0 ) { /* The send operation failed, so this task is still responsible for the buffer obtained from the IP stack. To ensure the buffer is not lost it must either be used again, or, as in this case, returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer(). pucUDPPayloadBuffer can be safely re-used to receive from the socket below once the buffer has been returned to the stack. */ FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer ); } else { /* The send was successful so the IP stack is now managing the buffer pointed to by pucUDPPayloadBuffer, and the IP stack will return the buffer once it has been sent. pucUDPPayloadBuffer can be safely re-used to receive from the socket below. */ } /* Keep a count of how many echo requests have been transmitted so it can be compared to the number of echo replies received. It would be expected to loose at least one to an ARP message the first time the connection is created. */ ulTxCount++; /* Receive data on the socket. ulFlags has the zero copy bit set (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the received data should be passed out to this task using the second parameter to the FreeRTOS_recvfrom() call. When this is done the IP stack is no longer responsible for releasing the buffer, and the task *must* return the buffer to the stack when it is no longer needed. By default the receive block time is portMAX_DELAY. */ echoMARK_SEND_IN_TRACE_BUFFER( xZeroCopyReceiveEvent ); lReturned = FreeRTOS_recvfrom( xSocket, /* The socket to receive from. */ ( void * ) &pucUDPPayloadBuffer, /* pucUDPPayloadBuffer will be set to point to the buffer that already contains the received data. */ 0, /* Ignored because the zero copy interface is being used. */ FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */ &xEchoServerAddress, /* The address from which the data was sent. */ &xAddressLength ); if( lReturned > 0 ) { /* Compare the string sent to the echo server with the string received back from the echo server. */ if( strcmp( ( char * ) pucUDPPayloadBuffer, cTxString ) == 0 ) { /* The strings matched. */ ulRxCount++; } /* The buffer that contains the data passed out of the stack *must* be returned to the stack. */ FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer ); } } } /* Pause for a short while to ensure the network is not too congested. */ vTaskDelay( echoLOOP_DELAY ); /* Close this socket before looping back to create another. */ FreeRTOS_closesocket( xSocket ); } }
static void prvEchoClientTask( void *pvParameters ) { xSocket_t xSocket; struct freertos_sockaddr xEchoServerAddress; char cTxString[ 25 ], cRxString[ 25 ]; /* Make sure the stack is large enough to hold these. Turn on stack overflow checking during debug to be sure. */ int32_t lLoopCount = 0UL; const int32_t lMaxLoopCount = 50; volatile uint32_t ulRxCount = 0UL, ulTxCount = 0UL; uint32_t xAddressLength = sizeof( xEchoServerAddress ); /* Remove compiler warning about unused parameters. */ ( void ) pvParameters; /* Echo requests are sent to the echo server. The address of the echo server is configured by the constants configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */ xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT ); xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0, configECHO_SERVER_ADDR1, configECHO_SERVER_ADDR2, configECHO_SERVER_ADDR3 ); for( ;; ) { /* Create a socket. */ xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP ); configASSERT( xSocket != FREERTOS_INVALID_SOCKET ); /* Set a time out so a missing reply does not cause the task to block indefinitely. */ FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) ); /* Send a number of echo requests. */ for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ ) { /* Create the string that is sent to the echo server. */ sprintf( cTxString, "Message number %u\r\n", ( unsigned int ) ulTxCount ); /* Send the string to the socket. ulFlags is set to 0, so the zero copy interface is not used. That means the data from cTxString is copied into a network buffer inside FreeRTOS_sendto(), and cTxString can be reused as soon as FreeRTOS_sendto() has returned. 1 is added to ensure the NULL string terminator is sent as part of the message. */ FreeRTOS_sendto( xSocket, /* The socket being sent to. */ ( void * ) cTxString, /* The data being sent. */ strlen( cTxString ) + 1,/* The length of the data being sent. */ 0, /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */ &xEchoServerAddress, /* The destination address. */ sizeof( xEchoServerAddress ) ); /* Keep a count of how many echo requests have been transmitted so it can be compared to the number of echo replies received. It would be expected to loose at least one to an ARP message the first time the connection is created. */ ulTxCount++; /* Receive data echoed back to the socket. ulFlags is zero, so the zero copy option is not being used and the received data will be copied into the buffer pointed to by cRxString. xAddressLength is not actually used (at the time of writing this comment, anyway) by FreeRTOS_recvfrom(), but is set appropriately in case future versions do use it. */ memset( ( void * ) cRxString, 0x00, sizeof( cRxString ) ); FreeRTOS_recvfrom( xSocket, /* The socket being received from. */ cRxString, /* The buffer into which the received data will be written. */ sizeof( cRxString ), /* The size of the buffer provided to receive the data. */ 0, /* ulFlags with the FREERTOS_ZERO_COPY bit clear. */ &xEchoServerAddress, /* The address from where the data was sent (the source address). */ &xAddressLength ); /* Compare the transmitted string to the received string. */ if( strcmp( cRxString, cTxString ) == 0 ) { /* The echo reply was received without error. */ ulRxCount++; } }; /* Pause for a short while to ensure the network is not too congested. */ vTaskDelay( echoLOOP_DELAY ); /* Close this socket before looping back to create another. */ FreeRTOS_closesocket( xSocket ); } }
uint32_t FreeRTOS_inet_addr( const char *pcIPAddress ) { const uint8_t ucDecimalBase = 10; uint8_t ucOctet[ socketMAX_IP_ADDRESS_OCTETS ]; const char *pcPointerOnEntering; uint32_t ulReturn = 0UL, ulOctetNumber, ulValue; BaseType_t xResult = pdPASS; for( ulOctetNumber = 0; ulOctetNumber < socketMAX_IP_ADDRESS_OCTETS; ulOctetNumber++ ) { ulValue = 0; pcPointerOnEntering = pcIPAddress; while( ( *pcIPAddress >= ( uint8_t ) '0' ) && ( *pcIPAddress <= ( uint8_t ) '9' ) ) { /* Move previous read characters into the next decimal position. */ ulValue *= ucDecimalBase; /* Add the binary value of the ascii character. */ ulValue += ( *pcIPAddress - ( uint8_t ) '0' ); /* Move to next character in the string. */ pcIPAddress++; } /* Check characters were read. */ if( pcIPAddress == pcPointerOnEntering ) { xResult = pdFAIL; } /* Check the value fits in an 8-bit number. */ if( ulValue > 0xffUL ) { xResult = pdFAIL; } else { ucOctet[ ulOctetNumber ] = ( uint8_t ) ulValue; /* Check the next character is as expected. */ if( ulOctetNumber < ( socketMAX_IP_ADDRESS_OCTETS - 1 ) ) { if( *pcIPAddress != ( uint8_t ) '.' ) { xResult = pdFAIL; } else { /* Move past the dot. */ pcIPAddress++; } } } if( xResult == pdFAIL ) { /* No point going on. */ break; } } if( *pcIPAddress != ( uint8_t ) 0x00 ) { /* Expected the end of the string. */ xResult = pdFAIL; } if( ulOctetNumber != socketMAX_IP_ADDRESS_OCTETS ) { /* Didn't read enough octets. */ xResult = pdFAIL; } if( xResult == pdPASS ) { ulReturn = FreeRTOS_inet_addr_quick( ucOctet[ 0 ], ucOctet[ 1 ], ucOctet[ 2 ], ucOctet[ 3 ] ); } return ulReturn; }
static void prvEchoClientTask( void *pvParameters ) { Socket_t xSocket; struct freertos_sockaddr xEchoServerAddress; int32_t lLoopCount = 0UL; const int32_t lMaxLoopCount = 1; volatile uint32_t ulTxCount = 0UL; BaseType_t xReceivedBytes, xReturned, xInstance; BaseType_t lTransmitted, lStringLength; char *pcTransmittedString, *pcReceivedString; WinProperties_t xWinProps; TickType_t xTimeOnEntering; /* Fill in the buffer and window sizes that will be used by the socket. */ xWinProps.lTxBufSize = 6 * ipconfigTCP_MSS; xWinProps.lTxWinSize = 3; xWinProps.lRxBufSize = 6 * ipconfigTCP_MSS; xWinProps.lRxWinSize = 3; /* This task can be created a number of times. Each instance is numbered to enable each instance to use a different Rx and Tx buffer. The number is passed in as the task's parameter. */ xInstance = ( BaseType_t ) pvParameters; /* Point to the buffers to be used by this instance of this task. */ pcTransmittedString = &( cTxBuffers[ xInstance ][ 0 ] ); pcReceivedString = &( cRxBuffers[ xInstance ][ 0 ] ); /* Echo requests are sent to the echo server. The address of the echo server is configured by the constants configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 in FreeRTOSConfig.h. */ xEchoServerAddress.sin_port = FreeRTOS_htons( echoECHO_PORT ); xEchoServerAddress.sin_addr = FreeRTOS_inet_addr_quick( configECHO_SERVER_ADDR0, configECHO_SERVER_ADDR1, configECHO_SERVER_ADDR2, configECHO_SERVER_ADDR3 ); for( ;; ) { /* Create a TCP socket. */ xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP ); configASSERT( xSocket != FREERTOS_INVALID_SOCKET ); /* Set a time out so a missing reply does not cause the task to block indefinitely. */ FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) ); FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) ); /* Set the window and buffer sizes. */ FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProps, sizeof( xWinProps ) ); /* Connect to the echo server. */ if( FreeRTOS_connect( xSocket, &xEchoServerAddress, sizeof( xEchoServerAddress ) ) == 0 ) { ulConnections[ xInstance ]++; /* Send a number of echo requests. */ for( lLoopCount = 0; lLoopCount < lMaxLoopCount; lLoopCount++ ) { /* Create the string that is sent to the echo server. */ lStringLength = prvCreateTxData( pcTransmittedString, echoBUFFER_SIZES ); /* Add in some unique text at the front of the string. */ sprintf( pcTransmittedString, "TxRx message number %u", ulTxCount ); ulTxCount++; /* Send the string to the socket. */ lTransmitted = FreeRTOS_send( xSocket, /* The socket being sent to. */ ( void * ) pcTransmittedString, /* The data being sent. */ lStringLength, /* The length of the data being sent. */ 0 ); /* No flags. */ if( lTransmitted < 0 ) { /* Error? */ break; } /* Clear the buffer into which the echoed string will be placed. */ memset( ( void * ) pcReceivedString, 0x00, echoBUFFER_SIZES ); xReceivedBytes = 0; /* Receive data echoed back to the socket. */ while( xReceivedBytes < lTransmitted ) { xReturned = FreeRTOS_recv( xSocket, /* The socket being received from. */ &( pcReceivedString[ xReceivedBytes ] ),/* The buffer into which the received data will be written. */ lStringLength - xReceivedBytes, /* The size of the buffer provided to receive the data. */ 0 ); /* No flags. */ if( xReturned < 0 ) { /* Error occurred. Latch it so it can be detected below. */ xReceivedBytes = xReturned; break; } else if( xReturned == 0 ) { /* Timed out. */ break; } else { /* Keep a count of the bytes received so far. */ xReceivedBytes += xReturned; } } /* If an error occurred it will be latched in xReceivedBytes, otherwise xReceived bytes will be just that - the number of bytes received from the echo server. */ if( xReceivedBytes > 0 ) { /* Compare the transmitted string to the received string. */ configASSERT( strncmp( pcReceivedString, pcTransmittedString, lTransmitted ) == 0 ); if( strncmp( pcReceivedString, pcTransmittedString, lTransmitted ) == 0 ) { /* The echo reply was received without error. */ ulTxRxCycles[ xInstance ]++; } else { /* The received string did not match the transmitted string. */ ulTxRxFailures[ xInstance ]++; break; } } else if( xReceivedBytes < 0 ) { /* FreeRTOS_recv() returned an error. */ break; } else { /* Timed out without receiving anything? */ break; } } /* Finished using the connected socket, initiate a graceful close: FIN, FIN+ACK, ACK. */ FreeRTOS_shutdown( xSocket, FREERTOS_SHUT_RDWR ); /* Expect FreeRTOS_recv() to return an error once the shutdown is complete. */ xTimeOnEntering = xTaskGetTickCount(); do { xReturned = FreeRTOS_recv( xSocket, /* The socket being received from. */ &( pcReceivedString[ 0 ] ), /* The buffer into which the received data will be written. */ echoBUFFER_SIZES, /* The size of the buffer provided to receive the data. */ 0 ); if( xReturned < 0 ) { break; } } while( ( xTaskGetTickCount() - xTimeOnEntering ) < xReceiveTimeOut ); } /* Close this socket before looping back to create another. */ FreeRTOS_closesocket( xSocket ); /* Pause for a short while to ensure the network is not too congested. */ vTaskDelay( echoLOOP_DELAY ); } }