10 years, 2 months ago.

successful ip change

Hi all, I posted a question 2 days before that how can I change the static ip on run time, I saw there was one guy who asked this question as well. Well I managed to change the ip of the ethernet module, and you can do it for the wifi as well. What you are supposed to do is to make changes in the ethernet.cpp file in the Ehternetinterface library.

replace the code in init_netif function with the following one.

static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
    if (a>0){
       // a=0;
        netif_set_addr(&netif,ipaddr, netmask,gw); // for changing ip
        }
    if (a==0){ 
a=a+1;
    tcpip_init(tcpip_init_done, NULL);
    tcpip_inited.wait();
    
    memset((void*) &netif, 0, sizeof(netif));
    netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input);  // for assigning ip for the first time
    netif_set_default(&netif);
    
    netif_set_link_callback  (&netif, netif_link_callback);
    netif_set_status_callback(&netif, netif_status_callback);
}
}

I created a global flag int a =0; and I made a simple logic that if eth.init(ip,subnet,gw) called it for the first time, we should assign ip normally, but when we call it for the second or multiple time, we should call netif_set_addr function which is to change the ip.

hope it will help to all of you

cheers zain

Minor tweak: Rather than a global flag with a vague name like a it would probably be a lot cleaner to use static bool that is local to the init_netif function. e.g.

static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
    static bool firstInit = true;
    if (!firstInit) {
       netif_set_addr(&netif,ipaddr, netmask,gw);
    } else {
      firstInit = false;
      ....
   }
}
posted by Andy A 15 Sep 2014

thanks andy, you are right. cheers

posted by zain aftab 15 Sep 2014
Be the first to answer this question.