lwip sitch from DHCP to static IP at runtime

Created at 2022-06-22 00:22:15

Hi,

first, I do not have experience with RT-Thread, so please, be patient :)

I can't find way to switch from DHCP dynamic IP address to static IP address and runtime. Problem is that I designing device which will be shipped with DHCP option, but I want to have option that user can (using serial terminal) change this to static IP and store this information in EEPROM, so on next boot to start with static IP.
Without RTthread, I read EEPROM at startup and decide how to start LWIP. With RT, I can't find way to do it.
For everything else I really like RT and want to switch all my projects to it, but I stuck on first one :(

Any idea how to do this?

regards
Goran

More

Follower
0
Views
85
xiangxistu
xiangxistu 6 days ago

First, you should turn on DHCP in configuration. The DHCP function will not be compiled in source code if you turn off DHCP in configuration. So code will work failed.

You should turn on DHCP in configuration. Use "dhcp_stop" and "dhcp_start" in the user code to control DHCP state.
In the "netdev", you can use "netdev_dhcp_enable" to control DHCP state.

There are user documents about "netdev", you can get some useful information form it. netdev Doc

4 Answers
xiangxistu
xiangxistu 2022-06-26

RT-Thread use lwip to regard as tcpip prototol stack. So, you also can read EEPROM to get static ip ,and startup lwip.


At the same time, RT-Thread have "netdev" to manage "netif" in the lwIP. Therefore, you can use serial terminal to change the lwip dhcp state.


"netdev_set_if" and "ifconfig" can configure static IP address in the runtime.But you need make sure the "netdev" that your are using is not under "DHCP" state.

This is origin code : ifconfig


If you want to define a terminal command by yourself, the command can store IP address into EEPROM. You can use MSH_CMD_EXPORT_ALIAS and MSH_CMD_EXPORT to exprot user command(functions) to terminal.

Best wish to you.

gpufler
gpufler 2022-06-27

Hi,

thank you for answer. Yes, I look into this direction, and I found ifconfig source. And, yes, I create test console command to try to change IP, but, I'm doing something wrong. I can't find command to turn DHCP off.
Here is my RT network config
rtconfig.PNG

and here is test code. If I turn off DHCP in configuration, code work like expected, but if I turn it on, I can't find a way to switch it off.


int test(void) {

    struct netdev *netdev = RT_NULL;
    ip_addr_t dns_addr;

    netdev = netdev_get_by_name("e0");
    if (netdev == RT_NULL) {
        rt_kprintf("bad network interface device name(e0).\n");
    }

    inet_aton("192.168.1.10", &dns_addr);
    if (netdev_set_dns_server(netdev, 0, &dns_addr) == RT_EOK) {
        rt_kprintf("set network interface device(%s) dns server #%d: %s\n", "e0", 0, "192.168.1.10");
    }

    inet_aton("192.168.1.251", &dns_addr);
    netdev_set_ipaddr(netdev, &dns_addr);

}

THX
Goran

gpufler
gpufler 6 days ago

Hi xiangxistu,

thank you for help, this done a trick, and work like charm.
Here is a test code if someone need it:


int dhcpDown(void) {

    struct netdev *netdev = RT_NULL;
    ip_addr_t dns_addr;

    netdev = netdev_get_by_name("e0");
    if (netdev == RT_NULL) {
        rt_kprintf("bad network interface device name(e0).\n");
    }

    netdev_dhcp_enabled(netdev, RT_FALSE);

    inet_aton("192.168.1.10", &dns_addr);
    if (netdev_set_dns_server(netdev, 0, &dns_addr) == RT_EOK) {
        rt_kprintf("set network interface device(%s) dns server #%d: %s\n", "e0", 0, "192.168.1.10");
    }
    inet_aton("192.168.1.251", &dns_addr);
    netdev_set_ipaddr(netdev, &dns_addr);

}

int dhcpUp(void) {
    struct netdev *netdev = RT_NULL;

    netdev = netdev_get_by_name("e0");
    if (netdev == RT_NULL) {
        rt_kprintf("bad network interface device name(e0).\n");
    }

    netdev_dhcp_enabled(netdev, RT_TRUE);
}


MSH_CMD_EXPORT(dhcpDown,  disable DHCP and set static IP);
MSH_CMD_EXPORT(dhcpUp,  enable DHCP);

Thank you again.

Ok, while we are here, I got another one question :)

I'm using CH32V307, and he does not have dedicated pins for network LED's. I try to find out in board files and low level functions to assign and enable this LEDs to have visual representation of network activity. Without RT Thread, I use WCHNET library and there is Ethernet_LED_LINKSET and Ethernet_LED_DATASET and this done a trick. This is lib external functions, but I can not find anything like this in RT.

Any idea how to do that?

THX
Goran

Write Your Answer

Log in to publish your answer.,Click here to log in.

Create
Post

Share