FreeRTOS SetAddressConfiguration question

I would like to change the IP address after FreeRTOS_IPInit has run.

This is what I have tried

taskENTER_CRITICAL();
FreeRTOS_SetAddressConfiguration( &xNetinfo.ip,
&xNetinfo.netmask,
&xNetinfo.gateway,
&xNetinfo.dns );
taskEXIT_CRITICAL();
FreeRTOS_NetworkDown();

After this code is run the network goes down and up again. But it uses the same IP address
as before, not the xNetinfo.ip passed to FreeRTOS_SetAddressConfiguration

What am I missing?

What am I missing?

Lots of information necessary to answer your question :wink:

Do you have ipconfigUSE_DHCP set to 1 or 0?
When are you calling this code, at initialisation time, after you have already been communicating on the network, or somewhere in between?

I have the following set:
#define ipconfigUSE_DHCP_HOOK 1
#define ipconfigDHCP_FALL_BACK_AUTO_IP 1

and ipconfigUSE_DHCP defaults to 1

The program was running and answering ping using a previously configured address.

After the call to FreeRTOS_SetAddressConfiguration and FreeRTOS_NetworkDown a call to xApplicationDHCPHook was triggered with eDHCPPhasePreDiscover and the old address. This
call returned eDHCPUseDefaults

So you want FreeRTOS+TCP to stop using DHCP and use a static address instead?

In this case I was not using DHCP. So The bahaivoir I am looking for.

  1. At startup read flash:

    • if you find a configuration use that
    • else use DHCP with ipconfigDHCP_FALL_BACK_AUTO_IP
  2. Make it possible to change ip configuration without rebooting independent of what happened at startup

(1) I have implemented using xApplicationDHCPHook

Is it a way of getting (2) to work?

What I do in most of my projects is store the IP-address that was assigned by DHCP.
Now when the device starts, it initialises the IP-stack with that last valid address.
If some day there is no valid response from DHCP, the last IP address will be used.
I don’t think that falling back to using a Link Layer is a wide-spread method.

But if you want to change the IP address for whatever reason, I think you can just call mentioned function.
Changing the MAC-address is more difficult, because the EMAC must be programmed to allow the new MAC-address.

Sorry for this limited answer, I’m typing with one finger on my phone.

Thanks for the tip. I am not interested in changing mac address, just the IP address. As I mentioned before I can not change the address used by the system by calling function FreeRTOS_SetAddressConfiguration. I set a new address then call FreeRTOS_NetworkDown(); and the network gets up using the old address.

Does FreeRTOS_SetAddressConfiguration just work in very particular circumstances.

Hello @andreas.sundstrom,

Thank you for that information. I have one more question though. Why are you trying to change the IP-address? Maybe we can find a way around it without changing the IP-address.
If that is something which we cannot do, we’ll help you find a solution to this problem.

Thanks,
Aniruddha

There is some legacy code that works this way. I.e. If no address is configured and no DHCP server is reached we get an automatically generated fallback IP. If then the user wants to change the IP address without rebooting, they can do that.

This I do not know how to achieve.

More so I just want to understand how FreeRTOS_SetAddressConfiguration works. How is it supposed to be used? In what circumstances does it work?

I see. Thanks for that explanation.

To make it clear, the FreeRTOS_SetAddressConfiguration function sets the current value of IP-address, Netmask, Gateway address etc.

When you choose to use DHCP and call FreeRTOS_NetworkDown after modifying the current IP address, DHCP overrides your setting.
So, you might want to include a callback in DHCP to terminate the DHCP process before it can override your IP-address. See the details here and here. The xApplicationDHCPHook will be called with the first parameter eDHCPPhasePreDiscover - if it returns anything except eDHCPContinue, the DHCP process will terminate and your configuration will remain as is.

However, on a DHCP managed network, I do not like the idea of setting the IP address yourself since the DHCP server might assign the same IP to other devices leading to a clash which will affect the network performance severely until one of the devices relinquishes their IP address.

Right now I am testing on a network without a DHCP server and I have a xApplicationDHCPHook that returns eDHCPUseDefaults. Which makes the network use the current settings (i.e. the fallback address FreeRTOS+TCP made up during startup). Can I make it use the new default settings that was just set with FreeRTOS_SetAddressConfiguration?

Yes, you can. Just be sure to update the defaults according to your needs.

But how?

This is what happens:

  1. Network is up with IP address 169.254.54.42
  2. FreeRTOS_SetAddressConfiguration is called with the first argument pointing to 0xa9fe402a (i.e. 169.254.64.42)
  3. FreeRTOS_NetworkDown is called
  4. The stack calls xApplicationDHCPHook with eDHCPPhasePreDiscover and 0xa9fe362a as arguments
  5. xApplicationDHCPHook returns eDHCPUseDefaults
  6. The network is up again, still with IP address 169.254.54.42

Is this a bug?

Hello @andreas.sundstrom,

Apologies, I lost track of this post and thus the delay in my response.
I’ll take a look at this and will try to come up with a solution.

Thanks,
Aniruddha

Hello @andreas.sundstrom,

I was wrong earlier. To get the new IP address set - the function FreeRTOS_SetAddressConfiguration won’t work since that sets the IP address directly which gets overridden while DHCP process going on.

To get around this problem, I suggest that you use the IP address parameter when calling FreeRTOS_IPInit for the first time. It would look something like the following.

FreeRTOS_IPInit( IPAddressEquivalentof /* 0xa9fe402a */, ... , ... , ... );

/* Use DHCP normally where you'll get assigned an IP-address (169.254.54.42). */

/* Now work on setting the default IP-address. */
/* Call FreeRTOS_NetworkDown. */
FreeRTOS_NetworkDown();

/* DHCP will start at this point. Make sure that xApplicationDHCPHook returns
 * eDHCPUseDefaults. The IP-address that was used while calling FreeRTOS_IPInit
 * will be the new IP address. */

Let me know if that works or you need some clarification. :slight_smile:

Thanks for your patience,
Aniruddha

Thanks for the information.

So, it is not possible to change the configuration manually after starting the system?
or is it ok to rerun FreeRTOS_IPInit?

I do not think this is a very important feature for the customers, so letting them reboot after changing the configuration would be an ok workaround. This was just something that worked in our legacy code, but not the most important feature.

I am a bit confused about FreeRTOS_SetAddressConfiguration and FreeRTOS_SetIPAddress.

In what circumstances can they be used?
Can the documentation of these functions be clearified somehow?

/Andreas

You should not run IPInit again. That function spawns the IP-task.

There can be a work around. Before calling FreeRTOS_NetworkDown(), you can modify the struct xDefaultAddressing (The field xDefaultAddressing.ulDefaultIPAddress) to have the IP address that you like. Something of the sorts:

xDefaultAddressing.ulDefaultIPAddress = FreeRTOS_inet_addr_quick( ucIPAddress[ 0 ], ucIPAddress[ 1 ], ucIPAddress[ 2 ], ucIPAddress[ 3 ] );

After this, calling FreeRTOS_NetworkDown and returning eDHCPUseDefaults in DHCP hook will cause the system to use the new default IP address that you just set in xDefaultAddressing.

Well, these functions change the IP-address used by the system without restarting the network. This could cause ongoing TCP connections to break and UDP packets to be dropped. You can use them to directly change the IP (but in that case, you should not call NetworkDown) - however, that would not be a ‘clean’ way of setting a new IP.

You should use the above method instead.

Let me know if that helps.

Thank’s for the information. The workaround did work for me.