Пример #1
0
static void prvInitialiseDHCP( void )
{
extern void vIPFunctionsTimerCallback( xTimerHandle xTimer );

	/* Initialise the parameters that will be set by the DHCP process. */
	if( ulTransactionId == 0 )
	{
		ulTransactionId = ipconfigRAND32();
	}
	else
	{
		ulTransactionId++;
	}
	ulOfferedIPAddress = 0UL;
	ulDHCPServerAddress = 0UL;
	xDHCPTxPeriod = dhcpINITIAL_DHCP_TX_PERIOD;

	/* Create the DHCP socket if it has not already been created. */
	prvCreateDHCPSocket();

	if( xDHCPTimer == NULL )
	{
		xDHCPTimer = xTimerCreate( ( const signed char * const ) "DHCP", dhcpINITIAL_TIMER_PERIOD, pdTRUE, ( void * ) eDHCPEvent, vIPFunctionsTimerCallback );
		configASSERT( xDHCPTimer );
		xTimerStart( xDHCPTimer, portMAX_DELAY );
	}
	else
	{
		xTimerChangePeriod( xDHCPTimer, dhcpINITIAL_TIMER_PERIOD, portMAX_DELAY );
	}
}
Пример #2
0
static BaseType_t prvCreateTxData( char *cBuffer, uint32_t ulBufferLength )
{
BaseType_t lCharactersToAdd, lCharacter;
char cChar = '0';

	/* Randomise the number of characters that will be sent. */
	do
	{
		lCharactersToAdd = ipconfigRAND32() % ( ulBufferLength - 20UL );
	} while ( lCharactersToAdd == 0 );

	/* Fill the buffer. */
	for( lCharacter = 0; lCharacter < lCharactersToAdd; lCharacter++ )
	{
		cBuffer[ lCharacter ] = cChar;
		cChar++;

		if( cChar > '~' )
		{
			cChar = '0';
		}
	}

	return lCharactersToAdd;
}
Пример #3
0
static BaseType_t prvCreateTxData( char *cBuffer, uint32_t ulBufferLength )
{
BaseType_t lCharactersToAdd, lCharacter;
char cChar = '0';
const BaseType_t lMinimumLength = 60;

	/* Randomise the number of characters that will be sent in the echo
	request. */
	do
	{
		lCharactersToAdd = ipconfigRAND32() % ( ulBufferLength - 20UL );
	} while ( ( lCharactersToAdd == 0 ) || ( lCharactersToAdd < lMinimumLength ) ); /* Must be at least enough to add the unique text to the start of the string later. */

	/* Fill the buffer. */
	for( lCharacter = 0; lCharacter < lCharactersToAdd; lCharacter++ )
	{
		cBuffer[ lCharacter ] = cChar;
		cChar++;

		if( cChar > '~' )
		{
			cChar = '0';
		}
	}

	return lCharactersToAdd;
}
Пример #4
0
DWORD WINAPI prvSimpleWinsockTCPClientTask( void *pvParameters )
{
char *pcTransmittedString, *pcReceivedString;
BaseType_t lReceived, lTransmitted = 0, lStringLength, lReturned = 0, lInstance;
uint32_t ulCount = 0UL, ulMaxCount, ulIPAddress;
const uint32_t ulMaxLoopsPerSocket = 100UL;
struct sockaddr_in xConnection;
SOCKET xClientSocket;
int iReturned;
TickType_t xTimeOnShutdown;

	/* Multiple instances of this task are created.  The instance is passed in
	as the parameter. */
	lInstance = ( BaseType_t ) pvParameters;

	/* Locate the buffers for this instance of this task. */
	pcTransmittedString = &( cTxBuffers[ lInstance ][ 0 ] );
	pcReceivedString = &( cRxBuffers[ lInstance ][ 0 ] );

	/* It is assumed that this task is not created until the network is up,
	so the IP address of the server (which is the FreeRTOS+TCP side of the
	connection) can be obtained immediately.  Store the IP address being
	used in ulIPAddress. */
	FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );

	/* Set family and port for client socket. */
	memset( ( void * ) &xConnection, 0x00, sizeof( struct sockaddr_in ) );
	xConnection.sin_family = AF_INET;
	xConnection.sin_addr.s_addr = ulIPAddress;
	xConnection.sin_port = htons( tcpechoPORT_NUMBER );

	for( ;; )
	{
		/* Create the socket then connect it to the FreeRTOS+TCP server. */
		xClientSocket = socket( AF_INET, SOCK_STREAM, 0 );
		configASSERT( xClientSocket != INVALID_SOCKET );

		do
		{
			iReturned = connect( xClientSocket, (const struct sockaddr*) &xConnection, sizeof( xConnection ) );
		} while( iReturned != 0 );

		ulMaxCount = ipconfigRAND32() % ulMaxLoopsPerSocket;

		for( ulCount = 0; ulCount < ulMaxCount; ulCount++ )
		{
			/* Create a string then send it to the server. */
			lStringLength = prvCreateTxData( pcTransmittedString, tcpechoBUFFER_SIZE );
			lTransmitted = send( xClientSocket, pcTransmittedString, lStringLength, 0 );

			configASSERT( lTransmitted != SOCKET_ERROR );
			configASSERT( lTransmitted == lStringLength );

			if( lTransmitted == lStringLength )
			{
				memset( ( void * ) pcReceivedString, 0x00, tcpechoBUFFER_SIZE );
				lReceived = 0;

				/* Wait for the echoed string. */
				while( lReceived < lTransmitted )
				{
					lReturned = recv( xClientSocket, ( char * ) &( pcReceivedString[ lReceived ] ), lTransmitted - lReceived, 0 );

					if( lReturned >= 0 )
					{
						/* Data was received. */
						lReceived += lReturned;
					}
					else
					{
						/* Error was returned. */
						ulClientReceiveErrors++;
						break;
					}
				}

				/* If the socket was not closed, check the number of bytes
				received. */
				if( lReceived == lTransmitted )
				{
					/* Were the expected characters received? */
					configASSERT( memcmp( pcTransmittedString, pcReceivedString, lTransmitted ) == 0x00 );
					if( memcmp( pcTransmittedString, pcReceivedString, lReceived ) != 0x00 )
					{
						ulIncorrectDataReceived++;
						break;
					}
					else
					{
						/* Received expected string, increment the count of
						successful cycles. */
						ulClientCycles++;
					}
				}
				else
				{
					/* Socket is being closed or an error occurred.  Don't try
					using the same socket again. */
					break;
				}
			}
			else
			{
				ulClientTransmitErrors++;
				break;
			}
		}

		shutdown( xClientSocket, SD_BOTH );
		xTimeOnShutdown = xTaskGetTickCount();

		do
		{
			Sleep( tcpechoWINSOCK_SHUTDOWN_DELAY );
			lReturned = recv( xClientSocket, pcReceivedString, lTransmitted, 0 );

			if( lReturned < 0 )
			{
				break;
			}
		} while( ( xTaskGetTickCount() - xTimeOnShutdown ) < tcpechoSHUTDOWN_DELAY );

		configASSERT( closesocket( xClientSocket ) == 0 );

		Sleep( tcpechoLOOP_DELAY );
	}
}
Пример #5
0
static void prvMultipleSocketTxTask( void *pvParameters )
{
uint32_t ulTxValue = 0;
struct freertos_sockaddr xDestinationAddress;
uint32_t ulIPAddress, ulFirstDestinationPortNumber, xPortNumber;
xSocket_t xTxSocket;
uint32_t ulSendCount[ selNUMBER_OF_SOCKETS ];

	memset( ulSendCount, '\0', sizeof( ulSendCount ) );

	/* The first destination port number is passed in as the task parameter.
	Other destination port numbers used are consecutive from this. */
	ulFirstDestinationPortNumber = ( uint32_t ) pvParameters;

	/* Create the socket used to send to the sockets created by the Rx task.
	Let the IP stack select a port to bind to. */
	xTxSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
	FreeRTOS_bind( xTxSocket, NULL, sizeof( struct freertos_sockaddr ) );

	/* The Rx and Tx tasks execute at the same priority so it is possible that
	the Tx task will fill up the send queue - set a Tx block time to ensure
	flow control is managed if this happens. */
	FreeRTOS_setsockopt( xTxSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendBlockTime, sizeof( xSendBlockTime ) );

	/* It is assumed that this task is not created until the network is up,
	so the IP address can be obtained immediately.  Store the IP address being
	used in ulIPAddress.  This is done so the socket can send to a different
	port on the same IP address. */
	FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );

	/* This test sends to itself, so data sent from here is received by a server
	socket on the same IP address.  Setup the freertos_sockaddr structure with
	this nodes IP address. */
	xDestinationAddress.sin_addr = ulIPAddress;

	/* Block for a short time to ensure the task implemented by the
	prvMultipleSocketRxTask() function has finished creating the Rx sockets. */
	while( eTaskGetState( xRxTaskHandle ) != eSuspended )
	{
		vTaskDelay( xSendBlockTime );
	}
	vTaskResume( xRxTaskHandle );

	for( ;; )
	{
		/* Pseudo randomly select the destination port number from the range of
		valid destination port numbers. */
		xPortNumber = ipconfigRAND32() % selNUMBER_OF_SOCKETS;
		ulSendCount[ xPortNumber ]++;
		xDestinationAddress.sin_port = ( uint16_t ) ( ulFirstDestinationPortNumber + xPortNumber );
		xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );

		/* Send an incrementing value to the pseudo randomly selected port. */
		FreeRTOS_sendto( xTxSocket, &ulTxValue, sizeof( ulTxValue ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
		ulTxValue++;

		if( ulTxValue >= selMAX_TX_VALUE )
		{
			/* Start over. */
			ulTxValue = 0;

			/* As a sanity check that this demo is valid, ensure each socket has
			been used at least once. */
			for( xPortNumber = 0; xPortNumber < selNUMBER_OF_SOCKETS; xPortNumber++ )
			{
				if( ulSendCount[ xPortNumber ] == 0 )
				{
					ulErrorOccurred = pdTRUE;
				}

				ulSendCount[ xPortNumber ] = 0;
			}

			/* Allow the Rx task to check it has received all the values. */
			while( eTaskGetState( xRxTaskHandle ) != eSuspended )
			{
				vTaskDelay( xSendBlockTime );
			}
			vTaskResume( xRxTaskHandle );

			/* Increment to show this task is still executing. */
			ulTxCycles++;
		}

		/* Delay here because in the Windows simulator the MAC interrupt
		simulator delays, so network traffic cannot be received any faster than
		this. */
		vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY << 2 );
	}
}