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

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

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: %sn",
        ( 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

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: %sn", ( 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

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

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

Apologies for ressurecting such an old thread but I have tried to use this with FreeRTOS-TCP that is included with FreeRTOS v10.1.0 and all the names seem to have changed. Could you confirm that this is the correct re-implementation: ~~~

if( ipconfigUSEDHCPHOOK != 0 )

eDHCPCallbackAnswert xApplicationDHCPHook(eDHCPCallbackPhaset eDHCPPhase, uint32t ulIPAddress) { eDHCPCallbackAnswert xResult = eDHCPContinue;
switch(eDHCPPhase)
{
    case eDHCPPhasePreRequest:
        // Driver is about to ask for a DHCP offer.
    case eDHCPPhasePreDiscover:
        // Driver is about to request DHCP an IP address.
        if(s_StaticIPIsEnabled == pdTRUE)
        {
            xResult = eDHCPUseDefaults;
        }
        else if(s_DHCPIsEnabled == pdFALSE)
        {
            xResult = eDHCPStopNoChanges;
        }
        break;
}

// Return the result.
return(xResult);
}

endif

~~~ -Andy.

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

I’m sorry about the name changing. The example that you give is OK and it will work with the latest release. The earlier implementation ( long time ago now ) was still in the Labs, undocumented. We found that the names were not clear enough and also that they were not in-tune with the rest of the software. The source code does issue an #error though in case old #define‘s are still used. Here you find an up-to-date description of the DHCP application hook.

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

Thanks! All sorted and working now! -Andy.