esp-rs / esp-idf-svc

Type-Safe Rust Wrappers for various ESP-IDF services (WiFi, Network, Httpd, Logging, etc.)
https://docs.esp-rs.org/esp-idf-svc/
Apache License 2.0
317 stars 178 forks source link

Ethernet to WiFi bridge: where to start? #508

Open FrigoEU opened 2 hours ago

FrigoEU commented 2 hours ago

Hi,

This isn't an issue but rather a request for a nudge in the right direction. I've made a fairly large application in Rust running on the ESP32. So far it's been fairly smooth sailing. But the next feature I'd like to build is to me quite difficult to get started on.

The situation: my ESP32 will be plugged into Ethernet. When connection is successful (and it's configured as such) it should start a WiFi access point (with pre-configured ssid/password) and allow another device to connect to the WiFi and connect to the internet via the ESP32's Ethernet connection. I feel in theory this should be possible but I've found only very limited information/examples online (especially in the Rust language).

I'd greatly appreciate it if somebody could point me in the right direction to get started on this.

Simon

ivmarkov commented 2 hours ago

A quick googling reveals this C ESP IDF example which should be your starting point, and seems to do exactly what you want: https://github.com/espressif/esp-idf/tree/release/v5.2/examples/network/bridge

Informally speaking, how it operates is as follows:

Apparently, this is something new in ESP IDF 5. ESP IDF 4.4 did not support layer-2 bridging, only layer3 IP packet forwarding (which is still supported in the esp-idf-* crates btw)

How to do it in Rust?

Now the non-trivial part: Once the above laborious excercise is done, you need to create - as per the C example - an extra netif, and configure it as a bridge. The configuration currently cannot be 100% done with safe Rust calls, because the ESP IDF bridge APIs are not yet exposed in a safe manner in esp-idf-svc (as in the esp_netif_br_* structs and glue functions). Until this is available in Rust, you can just do it with unsafe calls into esp_idf_svc::sys::*.

Once you have this, I would appreciate if you publish it as I can use it as a template to expose the bridge APIs in a safe way in esp-idf-svc.

The bridge configuration stuff in the example starts from here. All above (ethernet and wifi conf) can be done with safe Rust.

Hope that helps.