esp_hal_dhcp_server/
lib.rs

1#![no_std]
2
3pub use edge_dhcp::Ipv4Addr;
4use embassy_net::{
5    udp::{BindError, PacketMetadata, UdpSocket},
6    Stack,
7};
8use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};
9use server::DhcpServer;
10use structs::DhcpLeaser;
11use structs::DhcpServerConfig;
12
13pub mod server;
14pub mod simple_leaser;
15pub mod structs;
16
17pub type CloseSignal = Signal<CriticalSectionRawMutex, ()>;
18pub static CLOSE_SIGNAL: CloseSignal = Signal::new();
19
20pub async fn run_dhcp_server(
21    stack: Stack<'static>,
22    config: DhcpServerConfig<'_>,
23    leaser: &'_ mut dyn DhcpLeaser,
24) -> Result<(), BindError> {
25    let mut rx_buffer = [0; 1024];
26    let mut tx_buffer = [0; 1024];
27    let mut rx_meta = [PacketMetadata::EMPTY; 16];
28    let mut tx_meta = [PacketMetadata::EMPTY; 16];
29    let sock = UdpSocket::new(
30        stack,
31        &mut rx_meta,
32        &mut rx_buffer,
33        &mut tx_meta,
34        &mut tx_buffer,
35    );
36
37    let mut server = DhcpServer::new(config, leaser, sock)?;
38    embassy_futures::select::select(server.run(), CLOSE_SIGNAL.wait()).await;
39    Ok(())
40}
41
42pub fn dhcp_close() {
43    CLOSE_SIGNAL.signal(());
44}