Can't configure fix-ip

03 Jun 2011

Hello,

The following coding is successful compiled. What i tried to do in the coding is to configure the LAN ip either DHCP or fix ip rely on setting(p30). I can't get the fix-ip, I wonder why i always get the DHCP ip. Is there any wrong with my coding?

#include "mbed.h"
#include "EthernetNetIf.h"

DigitalOut blink(LED1);
DigitalIn setting(p30);


void LAN_configure_ip(void)
  {  
  printf("\n\nSetting up ethernet...\r\n");

  if(setting)
    {
    printf ("FixIP Config\n\r");

    EthernetNetIf eth(
      IpAddr( 192, 168, 0, 128 ), // IP Address
      IpAddr( 255, 255, 255, 0 ), // Network Mask
      IpAddr( 192, 168, 0, 254 ), // Gateway
      IpAddr( 123, 456, 1, 2 )    // DNS    
      );  
    }
      
    EthernetNetIf eth;
     
    printf("Ethernet setup...\r\n");      
    EthernetErr ethErr = eth.setup();
    if(ethErr){
        printf("Error %d in setup.\n\r", ethErr);
        return;
    } 
    printf("Ethernet setup OK\r\n");
    
  IpAddr Ip = eth.getIp();
  printf("mbed IP Address is %d.%d.%d.%d\r\n", Ip[0], Ip[1], Ip[2], Ip[3]);

  }
   
int main() 
  { 
  char temp;
  
  printf("Hello World!\n");

  while(1)
    {
    if(setting != temp)
      {
      temp = setting;
      LAN_configure_ip();
      }
      
    blink = !blink;
    wait(1);
    }
  }

Thank you

03 Jun 2011

Hi Wen

The eth for your static IP allocation is declared inside an "if" block, so it is only local to that block. As soon as you come out of that block, the object should get destructed.

If you want static or DHCP allocation, you'll need to use a pointer to an EthernetNetIf and a "new" keyword for the constructor.

EthernetNetIf *eth;
EthernetErr ethErr;
if(setting) {
    eth = new EthernetNetIf(
      IpAddr( 192, 168, 0, 128 ), // IP Address
      IpAddr( 255, 255, 255, 0 ), // Network Mask
      IpAddr( 192, 168, 0, 254 ), // Gateway
      IpAddr( 123, 456, 1, 2 )    // DNS    
      );  
  } else {
      eth = new EthernetNetIf();
  }
   
printf("Ethernet setup...\r\n");      
EthernetErr ethErr = eth->setup();

Note since eth is now a pointer, you need to access it's members with -> rather than .

Regards
Daniel

06 Jun 2011

Hello Daniel,

Thanks for your reply.

/media/uploads/bc/hang.jpg

The ethernet setup only allow me to do the first time. The next ethernet setup will having problem and hanging in the eth->setup() loop not able to jump out the function.

Is there any wrong with my coding?

Bst Rgds, Wen

06 Jun 2011

Hi Wen

The simple answer is that the EthernetNetIf class is not designed to do that. You can call it once with either static or dynamic setup.

If you want to flip between DHCP and static address allocation, then lwip (which is underneath the NetServices) can potentially do that (as well as auto-IP), but it would mean code changes in a few places in the libraries.

What do you actually want to achieve with this?

Regards
Daniel

06 Jun 2011

Hello Daniel,

Thanks for your fast respond.

oic...Actually, i'm new on Mbed and I'm tried to give myself a task to learn up. First thing i would like to learn is LAN. My aim is to do a simple web browser to do all the ethernet configuration which can configure auto-IP, fix-IP, port, duplex mode and so on. For advance may be can check the IP address on the network and list out in table form show it on browser then make nice browser on it. Something look like our router browser.

Can you guide me how to do that?

Thank you, Wen

07 Jun 2011

Hi Wen

Nothing like an ambitious first project!

You'll need to understand the various layers of the network stack - this is Ethernet driver (not IP), then lwip (if you use the code already done for the mbed).

For what you are trying to achieve, it might be easier in the first instance to consider resetting the mbed after a configuration change - you can reset the mbed in software http://mbed.org/forum/mbed/topic/318/

Regards
Daniel

07 Jun 2011

Hello Daniel,

Very happy to receive your reply again.

Ya, I also few very hard to make it. I did google search the key work "autoip" as you mention. I did found the autoip.c/h library. There are few function for this library but wonder what parameter should i put it and the coding looking very complicated and i'm lose! hmmm.... :(

Quote:

Note from autoip.c

/*****************

  • USAGE:
  • define LWIP_AUTOIP 1 in your lwipopts.h
  • If you don't use tcpip.c (so, don't call, you don't call tcpip_init):
  • - First, call autoip_init().
  • - call autoip_tmr() all AUTOIP_TMR_INTERVAL msces,
  • that should be defined in autoip.h.
  • I recommend a value of 100. The value must divide 1000 with a remainder almost 0.
  • Possible values are 1000, 500, 333, 250, 200, 166, 142, 125, 111, 100 ....
  • Without DHCP:
  • - Call autoip_start() after netif_add().
  • With DHCP:
  • - define LWIP_DHCP_AUTOIP_COOP 1 in your lwipopts.h.
  • - Configure your DHCP Client.
  • /

Is there anything i can try with this library?

Quote:

void autoip_init(void);

void autoip_set_struct(struct netif *netif, struct autoip *autoip);

err_t autoip_start(struct netif *netif);

err_t autoip_stop(struct netif *netif);

void autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr);

void autoip_tmr(void);

void autoip_network_changed(struct netif *netif);

And i try to review back the sample code that i tried. I found out this EthernetTesterGood ( http://mbed.org/users/JeffM/programs/EthernetTesterGood/lgncop ) sample code don't have using setup ( eth.setup(); ). I wonder is there the possibility to make it work. Hopefully this the last chances for me to try.

Ya, i did tried hardware reset. It work's. Ya, good idea, instead of using h/w reset use software reset.

I'm now trying the code. Hopefully later can work!

Best Regards, Wen

07 Jun 2011

Tested fail... :(