Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT




Loading

FreeRTOS+TCP: How can I switch DHCP on/off at runtime

Posted by stefan-bat-mv on February 10, 2016

Hi there,

I am currently evaluating the FreeRTOS+TCP package. Our device/application can be configured in various ways regarding IP configuration. The following options are available (in that order):

  • if 'fixed IP' is configured and activated by the user -> use the fixed IP
  • else if 'DHCP' has been activated by the user -> try to get a DHCP address
  • else or if DHCP failed -> use LLA

Now as far as I can see in FreeRTOS I can only activate the usage of DHCP at compile time which isn't of much help here. I need a way to effectively switch this on/off at runtime or at least in a way that allows to enable/disable DHCP between power-cycles.

I found this thread in your archives: http://www.freertos.org/FreeRTOSSupportForumArchive/April2015/freertoschangingDHCPusageinruntimewhenusingFREERTOSTCPaaf06921j.html

but that does really answer my question.

Is this possible/planned for a future release?

Regards,

Stefan


FreeRTOS+TCP: How can I switch DHCP on/off at runtime

Posted by heinbali01 on February 10, 2016

hi Stefan,

Is this possible/planned for a future release?

It should already be possible with the current 160112 release. Please check http://www.freertos.org/labs for the latest download.

~~~~~

if( ipconfigDHCPUSESUSER_HOOK != 0 )
/* This is the config parameter: DHCP enabled or not. */
BaseType_t DHCPIsEnabled;

extern "C" BaseType_t xApplicationDHCPUserHook( eDHCPCallbackQuestion_t eQuestion,
	uint32_t ulIPAddress, uint32_t ulNetMask );

BaseType_t xApplicationDHCPUserHook( eDHCPCallbackQuestion_t eQuestion,
	uint32_t ulIPAddress, uint32_t ulNetMask )
{
BaseType_t xResult = ( BaseType_t ) eDHCPContinue;

	switch( eQuestion )
	{
	case eDHCPOffer:		/* Driver is about to ask for a DHCP offer. */
		/* Returning eDHCPContinue. */
		break;
	case eDHCPRequest:		/* Driver is about to request DHCP an IP address. */
		if( DHCPIsEnabled == pdFALSE )
		{
			xResult = ( BaseType_t ) eDHCPStopNoChanges;
		}
		break;
	}
	FreeRTOS_printf( ( "DHCP %s: use = %d: %s\n",
		( eQuestion == eDHCPOffer ) ? "offer" : "request",
		DHCPIsEnabled,
		xResult == ( BaseType_t ) eDHCPContinue ? "Cont" : "Stop" ) );
	return xResult;
}
endif /* ipconfigDHCPUSESUSER_HOOK */

~~~~~

In the above example the DHCP offer will always be asked for. The reason is that along with the offer other parameters are known: default gateway, network and netmask.

If DHCPIsEnabled is false, the fixed (configured) IP-address will be used.

If DHCPIsEnabled is true, DHCP will be attemped (using the configured parameters for timing). If there is no valid response, the fixed pre-configured IP-address will also be used.

Please ask if I left anything unclear.

Regards.


FreeRTOS+TCP: How can I switch DHCP on/off at runtime

Posted by stefan-bat-mv on February 10, 2016

Great! Thanks a lot for the quick answer!

I however did need to change the code a little:

~~~~ #if( ipconfigDHCPUSESUSERHOOK != 0 ) BaseTypet xApplicationDHCPUserHook( eDHCPCallbackQuestiont eQuestion, uint32t ulIPAddress, uint32t ulNetMask ) { BaseTypet xResult = ( BaseType_t ) eDHCPContinue;

switch( eQuestion )
{
case eDHCPOffer: // Driver is about to ask for a DHCP offer.
case eDHCPRequest: // Driver is about to request DHCP an IP address.
	if( s_StaticIPIsEnabled == pdTRUE )
	{
		xResult = ( BaseType_t )eDHCPUseDefaults;
	}
	else if( s_DHCPIsEnabled == pdFALSE )
	{
		xResult = ( BaseType_t )eDHCPStopNoChanges;
	}
	break;
}
FreeRTOS_printf( ( "DHCP %s: use = %d: %s\n", ( eQuestion == eDHCPOffer ) ? "offer" : "request", s_DHCPIsEnabled, xResult == ( BaseType_t ) eDHCPContinue ? "Cont" : "Stop" ) );
return xResult;

}

endif /* ipconfigDHCPUSESUSER_HOOK */

~~~~

The first time this hook gets called it is with eDHCPOffer, which in the code you suggested did result in the DHCP discover packet to be send at least once. Does this make sense?

Regards,

Stefan


FreeRTOS+TCP: How can I switch DHCP on/off at runtime

Posted by heinbali01 on February 10, 2016

The first time this hook gets called it is with eDHCPOffer, which in the code you suggested did result in the DHCP discover packet to be send at least once. Does this make sense?

That makes sense. The library will do at most two call-backs:

~~~~ eDHCPOffer: Driver is about to ask for a DHCP offer eDHCPRequest: Driver is about to request DHCP an IP address ~~~~

If you do not reply eDHCPContinue in the state eDHCPOffer, you will never see the second question eDHCPRequest.

In my applications I'd always return eDHCPContinue to eDHCPOffer because I want to know the values of the other fields (GW, DNS). Then if the user has indicated to use a fixed IP-address, I'd answer eDHCPStopNoChanges to the second question eDHCPRequest.

Within my vApplicationIPNetworkEventHook(), the code will set the final parameters to work with:

~~~~ if( ( DHCPIsEnabled == pdFALSE ) || ( DHCPFailed == pdTRUE ) ) { FreeRTOS_SetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGateway, &ulDNSAddress); } ~~~~

Regards, Hein


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS