Quality RTOS & Embedded Software

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




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) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists