MODM7AE70: Switch between DHCP and Static IP without Reboot?

Discussion to talk about software related topics only.
Post Reply
Anguel
Posts: 30
Joined: Fri Aug 23, 2019 12:39 am

MODM7AE70: Switch between DHCP and Static IP without Reboot?

Post by Anguel »

Hi,

I wonder if there is a way to switch the MODM7AE70 between DHCP and static IP mode without having to reboot the device. Is this possible at all?

Thanks in advance,

Anguel
User avatar
pbreed
Posts: 1081
Joined: Thu Apr 24, 2008 3:58 pm

Re: MODM7AE70: Switch between DHCP and Static IP without Reboot?

Post by pbreed »

From within a program, or externally via the web config interface?

Do you need to go both ways?
DHCP to static is easy...
Please note that any existing TCP connections will have to be closed.
If you initiating this change via any kind of web interface, then getting the web interface to reconnect on
the new address needs to be handled and I'm not covering that here...

I'm going to assume you want to do this programatically...
First get the interface block you want to work on:
#include <netinterface.h>

InterfaceBlock * pIf =GetInterfaceBlock(GetFirstInterface());


you will be working on the ip4 class object....

pif->ip4

This has the follwoing members:
config_chooser mode{"Mode", "DHCP", "DHCP,DHCP w Fallback,Static,Disabled","DHCP,DHCP with backup static address,Static preconfigured address,Disabled"};
config_IPADDR4 addr{"0.0.0.0", "StaticAddr", "Configured IP Address"};
config_IPADDR4 mask{"0.0.0.0", "StaticMask", "Configured IP Mask"};
config_IPADDR4 gate{"0.0.0.0", "StaticGate", "Configured IP Gateway"};
config_IPADDR4 dns1{"0.0.0.0", "StaticDNS1", "Configured IP DNS(1)"};
config_IPADDR4 dns2{"0.0.0.0", "StaticDNS2", "Configured IP DNS(2)"};

config_bool autoip{true, "AutoIPEn"};

CUR_IPADDR4 cur_addr{"ActiveAddr", "Current IPV4 address in use"};
CUR_IPADDR4 cur_mask{"ActiveMask", "Current IPV4 mask in use"};
CUR_IPADDR4 cur_gate{"ActiveGate", "Current IPV4 gateway in use"};
CUR_IPADDR4 cur_dns1{"ActiveDNS1", "Current IPV4 dns(1) in use"};
CUR_IPADDR4 cur_dns2{"ActiveDNS2", "Current IPV4 dns(2) in use"};
CUR_IPADDR4 cur_auto{"AutoIPAddr", "Current IPV4 auto address in use"};


If you don't want to change the stored IP config stuff then you probably only care about these four:
pif->ip4.cur_addr
pif->ip4.cur_mask
pif->ip4.cur_gate
pif->ip4.cur_dns1

You can just set them and it will use that as the current address...

IE
pif->ip4,cur_addr=AsciiToIp4("10.1.1.23");
pif->ip4,cur_mask=AsciiToIp4("255.255.255.0");

You probably want to turn the dhcp clinet off so it does nto change your setting back...

pif->dhcpClient.StopDHCP();



To go from static to DHCP :

pif->dhcpClient.RestartDHCP()

Now once you restart DHCP the addresses are not immediatly changed.. it taks a little while...
If there is no DHCP server on the netwrok it could take forever...

int32_t state=pif->dhcpCline.GetDHCPState();

The values of state are:
#define SDHCP_NOTSTARTED 0 ///< The System has not been initialized
#define SDHCP_DISCOVER 1 ///< The system is discovering the DHCP servers
#define SDHCP_OFFER 2 ///< The system has responded to an OFFER
#define SDHCP_ACK 3 ///< The System has Acknowledged the OFFER
#define SDHCP_INIT 4 ///< The System is reinitalzing
#define SDHCP_CMPL 5 ///< The System has obtained a valid DHCP lease
#define SDHCP_RENEW 6 ///< The System is in the procecess of renewing
#define SDHCP_REBIND 7 ///< The System has failed the Renew and is trying to Rebind
#define SDHCP_RELEASE 8 ///< The System is trying to release the Lease
#define SBOOTP_TRANSMITTING 9 ///< Trying BOOTP
#define SBOOTP_DONE 10 ///< BOOTP complete
#define SDHCP_FAILED 11 ///< DHCP attempt failed - could not obtain a DHCP lease


If you want to do these things so the changes are preserved across a reboot, or want to do them via a web page interface..
please explain EXACTLY what you want to do including how the decision of going from static to DHCP is made and we will try to help you...
Anguel
Posts: 30
Joined: Fri Aug 23, 2019 12:39 am

Re: MODM7AE70: Switch between DHCP and Static IP without Reboot?

Post by Anguel »

Thanks, that looks great, my question was if this is possible at all because the docs say somewhere that a reboot step is needed after changing IP config modes. I need to do it programatically, actually the user should be able to change it somewhere via touch display and the device should apply the new IP config without having to reboot the device.
I will come back to this topic if I have further questions when I have implemented the functionality. Thank you very much for the information!
User avatar
TomNB
Posts: 541
Joined: Tue May 10, 2016 8:22 am

Re: MODM7AE70: Switch between DHCP and Static IP without Reboot?

Post by TomNB »

Just a follow-up. You can definitely change modes without a reboot, just as explained in the previous post. Updated Change IP example based on this thread has been checked in for the next release.
Post Reply