static BaseType_t prvDisplayIPConfig( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString ) { static BaseType_t xIndex = 0; BaseType_t xReturn; uint32_t ulAddress; /* Remove compile time warnings about unused parameters, and check the write buffer is not NULL. NOTE - for simplicity, this example assumes the write buffer length is adequate, so does not check for buffer overflows. */ ( void ) pcCommandString; ( void ) xWriteBufferLen; configASSERT( pcWriteBuffer ); switch( xIndex ) { case 0 : FreeRTOS_GetAddressConfiguration( &ulAddress, NULL, NULL, NULL ); sprintf( pcWriteBuffer, "\r\nIP address " ); xReturn = pdTRUE; xIndex++; break; case 1 : FreeRTOS_GetAddressConfiguration( NULL, &ulAddress, NULL, NULL ); sprintf( pcWriteBuffer, "\r\nNet mask " ); xReturn = pdTRUE; xIndex++; break; case 2 : FreeRTOS_GetAddressConfiguration( NULL, NULL, &ulAddress, NULL ); sprintf( pcWriteBuffer, "\r\nGateway address " ); xReturn = pdTRUE; xIndex++; break; case 3 : FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulAddress ); sprintf( pcWriteBuffer, "\r\nDNS server address " ); xReturn = pdTRUE; xIndex++; break; default : ulAddress = 0; sprintf( pcWriteBuffer, "\r\n\r\n" ); xReturn = pdFALSE; xIndex = 0; break; } if( ulAddress != 0 ) { FreeRTOS_inet_ntoa( ulAddress, ( &( pcWriteBuffer[ strlen( pcWriteBuffer ) ] ) ) ); } return xReturn; }
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 BaseType_t prvPingCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString ) { char * pcParameter; BaseType_t lParameterStringLength, xReturn = pdFALSE; uint32_t ulIPAddress, ulBytesToPing; const uint32_t ulDefaultBytesToPing = 8UL; char cBuffer[ 16 ]; /* Remove compile time warnings about unused parameters, and check the write buffer is not NULL. NOTE - for simplicity, this example assumes the write buffer length is adequate, so does not check for buffer overflows. */ ( void ) pcCommandString; ( void ) xWriteBufferLen; configASSERT( pcWriteBuffer ); /* Start with an empty string. */ pcWriteBuffer[ 0 ] = 0x00; /* Obtain the number of bytes to ping. */ pcParameter = ( char * ) FreeRTOS_CLIGetParameter ( pcCommandString, /* The command string itself. */ 2, /* Return the second parameter. */ &lParameterStringLength /* Store the parameter string length. */ ); if( pcParameter == NULL ) { /* The number of bytes was not specified, so default it. */ ulBytesToPing = ulDefaultBytesToPing; } else { ulBytesToPing = atol( pcParameter ); } /* Obtain the IP address string. */ pcParameter = ( char * ) FreeRTOS_CLIGetParameter ( pcCommandString, /* The command string itself. */ 1, /* Return the first parameter. */ &lParameterStringLength /* Store the parameter string length. */ ); if( pcParameter != NULL ) { /* Terminate the host name or IP address string. */ pcParameter[ lParameterStringLength ] = 0x00; /* Attempt to obtain the IP address. If the first character is not a digit, assume the host name has been passed in. */ if( ( *pcParameter >= '0' ) && ( *pcParameter <= '9' ) ) { ulIPAddress = FreeRTOS_inet_addr( pcParameter ); } else { /* Attempt to resolve host. */ ulIPAddress = FreeRTOS_gethostbyname( pcParameter ); } /* Convert IP address, which may have come from a DNS lookup, to string. */ FreeRTOS_inet_ntoa( ulIPAddress, cBuffer ); if( ulIPAddress != 0 ) { xReturn = FreeRTOS_SendPingRequest( ulIPAddress, ( uint16_t ) ulBytesToPing, portMAX_DELAY ); } } if( xReturn == pdFALSE ) { sprintf( pcWriteBuffer, "%s", "Could not send ping request\r\n" ); } else { sprintf( pcWriteBuffer, "Ping sent to %s with identifier %d\r\n", cBuffer, ( int ) xReturn ); } return pdFALSE; }
/* Called by FreeRTOS+UDP when the network connects. */ void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent ) { uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress; int8_t cBuffer[ 16 ]; static portBASE_TYPE xTasksAlreadyCreated = pdFALSE; if( eNetworkEvent == eNetworkUp ) { /* Create the tasks that use the IP stack if they have not already been created. */ if( xTasksAlreadyCreated == pdFALSE ) { #if( mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS == 1 ) { /* Create tasks that demonstrate sending and receiving in both standard and zero copy mode. */ vStartSimpleUDPClientServerTasks( mainSIMPLE_CLIENT_SERVER_TASK_STACK_SIZE, mainSIMPLE_CLIENT_SERVER_PORT, mainSIMPLE_CLIENT_SERVER_TASK_PRIORITY ); } #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */ #if( mainCREATE_SELECT_UDP_SERVER_TASKS == 1 ) { /* Create tasks that demonstrate sending and receiving in both standard and zero copy mode. */ vStartSelectUDPServerTasks( mainSELECT_SERVER_TASK_STACK_SIZE, mainSELECT_SERVER_PORT, mainSELECT_SERVER_TASK_PRIORITY ); } #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */ #if( mainCREATE_UDP_ECHO_TASKS == 1 ) { /* Create the tasks that transmit to and receive from a standard echo server (see the web documentation for this port) in both standard and zero copy mode. */ vStartEchoClientTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY ); } #endif /* mainCREATE_UDP_ECHO_TASKS */ #if( mainCREATE_UDP_CLI_TASKS == 1 ) { /* Create the task that handles the CLI on a UDP port. The port number is set using the configUDP_CLI_PORT_NUMBER setting in FreeRTOSConfig.h. */ vStartUDPCommandInterpreterTask( mainUDP_CLI_TASK_STACK_SIZE, mainUDP_CLI_PORT_NUMBER, mainUDP_CLI_TASK_PRIORITY ); } #endif /* mainCREATE_UDP_CLI_TASKS */ xTasksAlreadyCreated = pdTRUE; } /* Print out the network configuration, which may have come from a DHCP server. */ FreeRTOS_GetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress ); vOutputString( "IP Address: " ); FreeRTOS_inet_ntoa( ulIPAddress, cBuffer ); vOutputString( ( char * ) cBuffer ); vOutputString( "\r\nSubnet Mask: " ); FreeRTOS_inet_ntoa( ulNetMask, cBuffer ); vOutputString( ( char * ) cBuffer ); vOutputString( "\r\nGateway Address: " ); FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer ); vOutputString( ( char * ) cBuffer ); vOutputString( "\r\nDNS Server Address: " ); FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer ); vOutputString( ( char * ) cBuffer ); vOutputString( "\r\n\r\n" ); } }
/* Called by FreeRTOS+UDP when the network connects. */ void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent ) { static long lTasksAlreadyCreated = pdFALSE; const unsigned long ulXCoord = 3, ulYCoord = 3, ulIPAddressOffset = 45; unsigned long ulIPAddress; char cIPAddress[ 20 ]; /* Note: If the application is started without the network cable plugged in then ipconfigUDP_TASK_PRIORITY should be set to 0 in FreeRTOSIPConfig.h to ensure the IP task is created at the idle priority. This is because the Atmel ASF GMAC driver polls the GMAC looking for a connection, and doing so will prevent any lower priority tasks from executing. In this demo the IP task is started at the idle priority, then set to configMAX_PRIORITIES - 2 in the network event hook only after a connection has been established (when the event passed into the network event hook is eNetworkUp). */ if( eNetworkEvent == eNetworkUp ) { /* Ensure tasks are only created once. */ if( lTasksAlreadyCreated == pdFALSE ) { /* Create the task that handles the CLI on a UDP port. The port number is set using the configUDP_CLI_PORT_NUMBER setting in FreeRTOSConfig.h. */ vStartUDPCommandInterpreterTask( mainUDP_CLI_TASK_STACK_SIZE, mainUDP_CLI_PORT_NUMBER, mainUDP_CLI_TASK_PRIORITY ); #if( mainINCLUDE_ECHO_CLIENT_TASKS == 1 ) { /* Create the UDP echo tasks. The UDP echo tasks require the IP address of the echo server to be defined using the configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 constants in FreeRTOSConfig.h. */ vStartEchoClientTasks( mainECHO_CLIENT_STACK_SIZE, tskIDLE_PRIORITY ); } #endif } /* Obtain the IP address, convert it to a string, then display it on the LCD. */ FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL ); FreeRTOS_inet_ntoa( ulIPAddress, cIPAddress ); ili93xx_draw_string( ulXCoord, ulYCoord, ( uint8_t * ) "IP: " ); ili93xx_draw_string( ulXCoord + ulIPAddressOffset, ulYCoord, ( uint8_t * ) cIPAddress ); /* Set the priority of the IP task up to the desired priority now it has connected. */ vTaskPrioritySet( NULL, mainCONNECTED_IP_TASK_PRIORITY ); } /* NOTE: At the time of writing the Ethernet driver does not report the cable being unplugged - so the following if() condition will never be met. It is included for possible future updates to the driver. */ if( eNetworkEvent == eNetworkDown ) { /* Ensure the Atmel GMAC drivers don't hog all the CPU time as they look for a new connection by lowering the priority of the IP task to that of the Idle task. */ vTaskPrioritySet( NULL, tskIDLE_PRIORITY ); /* Disconnected - so no IP address. */ ili93xx_draw_string( ulXCoord, ulYCoord, ( uint8_t * ) "IP: " ); } }
static void prvNTPTask( void *pvParameters ) { BaseType_t xServerIndex = 3; struct freertos_sockaddr xAddress; #if( ipconfigUSE_CALLBACKS != 0 ) F_TCP_UDP_Handler_t xHandler; #endif /* ipconfigUSE_CALLBACKS != 0 */ xStatus = EStatusLookup; #if( ipconfigSOCKET_HAS_USER_SEMAPHORE != 0 ) || ( ipconfigUSE_CALLBACKS != 0 ) { xNTPWakeupSem = xSemaphoreCreateBinary(); } #endif #if( ipconfigUSE_CALLBACKS != 0 ) { memset( &xHandler, '\0', sizeof ( xHandler ) ); xHandler.pOnUdpReceive = xOnUdpReceive; FreeRTOS_setsockopt( xUDPSocket, 0, FREERTOS_SO_UDP_RECV_HANDLER, ( void * ) &xHandler, sizeof( xHandler ) ); } #endif #if( ipconfigSOCKET_HAS_USER_SEMAPHORE != 0 ) { FreeRTOS_setsockopt( xUDPSocket, 0, FREERTOS_SO_SET_SEMAPHORE, ( void * ) &xNTPWakeupSem, sizeof( xNTPWakeupSem ) ); } #endif for( ; ; ) { switch( xStatus ) { case EStatusLookup: if( ( ulIPAddressFound == 0ul ) || ( ulIPAddressFound == ~0ul ) ) { if( ++xServerIndex == sizeof pcTimeServers / sizeof pcTimeServers[ 0 ] ) { xServerIndex = 0; } FreeRTOS_printf( ( "Looking up server '%s'\n", pcTimeServers[ xServerIndex ] ) ); FreeRTOS_gethostbyname_a( pcTimeServers[ xServerIndex ], vDNS_callback, (void *)NULL, 1200 ); } else { xStatus = EStatusAsking; } break; case EStatusAsking: { char pcBuf[16]; prvNTPPacketInit( ); xAddress.sin_addr = ulIPAddressFound; xAddress.sin_port = FreeRTOS_htons( NTP_PORT ); FreeRTOS_inet_ntoa( xAddress.sin_addr, pcBuf ); FreeRTOS_printf( ( "Sending UDP message to %s:%u\n", pcBuf, FreeRTOS_ntohs( xAddress.sin_port ) ) ); uxSendTime = xTaskGetTickCount( ); FreeRTOS_sendto( xUDPSocket, ( void * )&xNTPPacket, sizeof( xNTPPacket ), 0, &xAddress, sizeof( xAddress ) ); } break; case EStatusPause: break; case EStatusFailed: break; } #if( ipconfigUSE_CALLBACKS != 0 ) { xSemaphoreTake( xNTPWakeupSem, 5000 ); } #else { uint32_t xAddressSize; BaseType_t xReturned; xAddressSize = sizeof( xAddress ); xReturned = FreeRTOS_recvfrom( xUDPSocket, ( void * ) cRecvBuffer, sizeof( cRecvBuffer ), 0, &xAddress, &xAddressSize ); switch( xReturned ) { case 0: case -pdFREERTOS_ERRNO_EAGAIN: case -pdFREERTOS_ERRNO_EINTR: break; default: if( xReturned < sizeof( xNTPPacket ) ) { FreeRTOS_printf( ( "FreeRTOS_recvfrom: returns %ld\n", xReturned ) ); } else { prvReadTime( ( struct SNtpPacket *)cRecvBuffer ); if( xStatus != EStatusPause ) { xStatus = EStatusPause; } } break; } } #endif } }