esp-rs / esp-hal

no_std Hardware Abstraction Layers for ESP32 microcontrollers
https://docs.esp-rs.org/esp-hal/
Apache License 2.0
740 stars 207 forks source link

Support running a DHCP server while in AP mode #1994

Closed Sycrosity closed 1 month ago

Sycrosity commented 2 months ago

Currently, to my knowledge there is no way to automatically provision IP addresses while in AP mode, with the wifi_(embassy_)_access_point examples prompting the user to set their IP address to a value in the ip range. A method of provisioning these (ideally through DHCP) would be useful in making the esp-wifi AP experience a bit more natural.

At the same time, this could also solve (although this should maybe be a seperate issue) the seemingly unexpected behaviour of setting embassy-net::Config (and the mostly equivalent esp_wifi::wifi::ipv4::ClientConfiguration) settings for wifi when in AP mode - as the respective DHCP variants of these either do not work or cause no connections to be possible when in AP mode and don't cause any compile or runtime errors.

In the meantime, what would be the best way to go around implementing a pseudo DHCP server for AP mode? Having looked into the smoltcp dhcp socket, I cannot work out how to add this into an wifi AP instance (since we do not have access to the internals of embassy_net::StackResources or smoltcp::SocketStorage).

### Tasks
MabezDev commented 2 months ago

As you mentioned, this is because there isn't a DHCP server available in smoltcp. @bjoernQ wrote a simple one in an experiment last year: https://github.com/bjoernQ/esp32c3-dhcp-experiment/blob/0a9432c4a348fce9360006c8dab99b7e4d21de69/dhcp-experiment/src/main.rs#L218 which may be helpful as a starting point. Getting something like that upstreamed into smoltcp (and I already know they want it, I've seen on matrix) would be the way to solve this.

redengin commented 2 months ago

I've found a lot of utility in the C/C++ ESP implementations. But the C/C++ implementations don't follow SOLID principles which are paramount for Rust.

To decouple responsibility, there should be a rust struct that focuses on providing an access point (not a generic thing that can be both a server or a client).

As stated above, the ESP Wifi AP struct shouldn't be responsible for DHCP. Instead, the Wifi AP struct should provide an interface to service connections.

MabezDev commented 2 months ago

As stated above, the ESP Wifi AP struct shouldn't be responsible for DHCP. Instead, the Wifi AP struct should provide an interface to service connections.

I think you misunderstood me, I'm saying the exact same thing. This should be a socket type in smoltcp, and nothing to do with esp-wifi's AP at all.

filipton commented 2 months ago

I've made easy to use dhcp server for embassy (dhcp packet parsing based on edge-dhcp crate). Tested it on esp32-s3 (with android phone). Look at example dir for implementation.

Sycrosity commented 2 months ago

As you mentioned, this is because there isn't a DHCP server available in smoltcp. @bjoernQ wrote a simple one in an experiment last year: https://github.com/bjoernQ/esp32c3-dhcp-experiment/blob/0a9432c4a348fce9360006c8dab99b7e4d21de69/dhcp-experiment/src/main.rs#L218 which may be helpful as a starting point. Getting something like that upstreamed into smoltcp (and I already know they want it, I've seen on matrix) would be the way to solve this.

Having implemented one myself following @bjoernQ's example using smoltcp wire dhcp structs (mostly for fun but also to see how it would be to implement a dhcp server into smoltcp) this does seem to not really be an issue in esp-hal, but regardless would seem to be the expected outcome when running an AP server (or at least in my eyes - although others may have different uses for APs that don't require DHCP provisioning), or at least something which could be put in an example. Should there be an issue for adding an example dhcp server, at least until smoltcp upstreams a dhcp server? Not sure on this one.

MabezDev commented 1 month ago

this does seem to not really be an issue in esp-hal

I agree.

Should there be an issue for adding an example dhcp server, at least until smoltcp upstreams a dhcp server

Imo, no, we should at the very least file an issue under smoltcp though and referencing some external projects that use smoltcp to create one would be a good idea.

Closing this for now.