Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot change network interface after creation #332

Open
torkleyy opened this issue Dec 20, 2023 · 5 comments · May be fixed by #398
Open

Cannot change network interface after creation #332

torkleyy opened this issue Dec 20, 2023 · 5 comments · May be fixed by #398
Labels
question Further information is requested

Comments

@torkleyy
Copy link
Contributor

Many methods of EspNetif are not exposed, such as setters and dhcp control. Without this, the ip configuration of an established connection cannot be changed.

@ivmarkov
Copy link
Collaborator

If you have concrete methods that need to be exposed (as well as justification why just re-creating the interface is not ideal), please go ahead and justify.

But in its current shape, this issue is hardly actionable.

@torkleyy
Copy link
Contributor Author

as well as justification why just re-creating the interface is not ideal

You cannot just recreate the interface. You would at least have to rotate the key, because it has to be unique. And swapping the interface does not allow setting an IP for an existing connection:

    log::info!("Starting...");
    wifi.start()?;
    log::info!("Connecting...");
    wifi.connect()?;
    log::info!("Connected");
    wifi.wait_netif_up()?;
    log::info!("Ready");

    log::info!("Changing ip");

    wifi.wifi_mut().swap_netif_sta(EspNetif::new_with_conf(&NetifConfiguration {
        key: "test".into(),
        ip_configuration: ipv4::Configuration::Client(ipv4::ClientConfiguration::Fixed(ipv4::ClientSettings {
            ip: ipv4::Ipv4Addr::new(192, 168, 178, 244),
            subnet: ipv4::Subnet {
                gateway: ipv4::Ipv4Addr::new(192, 168, 178, 1),
                mask: ipv4::Mask(24),
            },
            dns: Some(ipv4::Ipv4Addr::new(1, 1, 1, 1)),
            secondary_dns: None,
        })),
        ..NetifConfiguration::wifi_default_client()
    }).unwrap()).unwrap();

    log::info!("Changed ip");
    wifi.wait_netif_up()?;

    log::info!("Netif up again");

Above code will simply timeout.

@ivmarkov
Copy link
Collaborator

ivmarkov commented Dec 21, 2023

If you believe this is possible to do at runtime without destroying the EspNetif object, by all means demonstrate how, and I'll merge.

As for the the above code timing out, what is the root cause? You can't just say "you need to do B. because there is some bug with A, which I don't know the reason for".

@torkleyy
Copy link
Contributor Author

torkleyy commented Dec 21, 2023

As for the the above code timing out, what is the root cause? You can't just say "you need to do B. because there is some bug with A, which I don't know the reason for".

You're correct. I assumed you're not supposed to swap network interfaces during an active connection, but maybe it's also a bug. I have not investigated it further. It's hard to tell whether it can be considered a bug because esp-idf doesn't really document what esp_netif_attach is even supposed to do.

If you believe this is possible to do at runtime without destroying the EspNetif object, by all means demonstrate how, and I'll merge.

I can do that. The esp-idf docs for esp_netif_set_ip_info lead me to believe that it's possible.

@torkleyy torkleyy linked a pull request Mar 25, 2024 that will close this issue
@Vollbrecht Vollbrecht added the question Further information is requested label Jun 21, 2024
@DaneSlattery
Copy link
Contributor

DaneSlattery commented Jul 22, 2024

For anyone curious about how to get around this, we have this snippet:

static IF_COUNTER: AtomicUsize = AtomicUsize::new(1);

/// A global interface ID that prevents creating duplicate interface keys.
fn get_id() -> usize {
    IF_COUNTER.fetch_add(1, Ordering::Relaxed)
}

// and then later:
let new_netif = EspNetif::new_with_conf(&NetifConfiguration {
                            ip_configuration: ipv4::Configuration::Client(...),
                            key: format!("ETH_CL_{}", get_id()).as_str().try_into().unwrap(),
                            ..NetifConfiguration::eth_default_client()
                        })


 eth.eth_mut()
      .swap_netif(new_netif);
log::info!("Starting Eth"); 
eth.start().await; 
info!("Eth started.");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

4 participants