mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2025-11-03 15:24:09 +00:00
improved networks stack & DHCP server
This commit is contained in:
parent
c0bf8399a3
commit
7346fb42bd
@ -1,48 +1,41 @@
|
||||
use core::{net::Ipv4Addr, str::FromStr};
|
||||
use core::net::{Ipv4Addr, SocketAddrV4};
|
||||
use edge_dhcp::{
|
||||
io::{self, DEFAULT_SERVER_PORT},
|
||||
server::{Server, ServerOptions},
|
||||
};
|
||||
use edge_nal::UdpBind;
|
||||
use edge_nal_embassy::{Udp, UdpBuffers};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::{Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use esp_radio::wifi::WifiDevice;
|
||||
use static_cell::make_static;
|
||||
use static_cell::StaticCell;
|
||||
|
||||
use crate::webserver::WEB_TAKS_SIZE;
|
||||
|
||||
pub const NETWORK_STACK_SIZE: usize = WEB_TAKS_SIZE + 2; // + 2 for other network taks. Breaks
|
||||
// without
|
||||
pub const NETWORK_STACK_SIZE: usize = WEB_TAKS_SIZE + 2; // + 2 for other network taks. Breaks without
|
||||
pub const GW_IP: Ipv4Addr = Ipv4Addr::new(192, 168, 2, 1);
|
||||
|
||||
pub fn setup_network<'a>(seed: u64, wifi: WifiDevice<'static>, spawner: Spawner) -> Stack<'a> {
|
||||
let gw_ip_addr_str = "192.168.2.1";
|
||||
let gw_ip_addr = Ipv4Addr::from_str(gw_ip_addr_str).expect("failed to parse gateway ip");
|
||||
let config = embassy_net::Config::ipv4_static(StaticConfigV4 {
|
||||
address: Ipv4Cidr::new(gw_ip_addr, 24),
|
||||
gateway: Some(gw_ip_addr),
|
||||
address: Ipv4Cidr::new(GW_IP, 24),
|
||||
gateway: Some(GW_IP),
|
||||
dns_servers: Default::default(),
|
||||
});
|
||||
|
||||
let nw_stack: &'static mut StackResources<NETWORK_STACK_SIZE> =
|
||||
make_static!(StackResources::<NETWORK_STACK_SIZE>::new());
|
||||
static NETWORK_STACK: StaticCell<StackResources<NETWORK_STACK_SIZE>> = StaticCell::new();
|
||||
let nw_stack = NETWORK_STACK.init(StackResources::new());
|
||||
|
||||
let (stack, runner) = embassy_net::new(wifi, config, nw_stack, seed);
|
||||
|
||||
spawner.must_spawn(net_task(runner));
|
||||
spawner.must_spawn(run_dhcp(stack, gw_ip_addr_str));
|
||||
spawner.must_spawn(run_dhcp(stack));
|
||||
|
||||
stack
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn run_dhcp(stack: Stack<'static>, gw_ip_addr: &'static str) {
|
||||
use core::net::{Ipv4Addr, SocketAddrV4};
|
||||
|
||||
use edge_dhcp::{
|
||||
io::{self, DEFAULT_SERVER_PORT},
|
||||
server::{Server, ServerOptions},
|
||||
};
|
||||
use edge_nal::UdpBind;
|
||||
use edge_nal_embassy::{Udp, UdpBuffers};
|
||||
|
||||
let ip = Ipv4Addr::from_str(gw_ip_addr).expect("dhcp task failed to parse gw ip");
|
||||
|
||||
async fn run_dhcp(stack: Stack<'static>) {
|
||||
let mut buf = [0u8; 1500];
|
||||
|
||||
let mut gw_buf = [Ipv4Addr::UNSPECIFIED];
|
||||
@ -55,12 +48,12 @@ async fn run_dhcp(stack: Stack<'static>, gw_ip_addr: &'static str) {
|
||||
DEFAULT_SERVER_PORT,
|
||||
)))
|
||||
.await
|
||||
.unwrap();
|
||||
.expect("Failed to bind socket for DHCP server");
|
||||
|
||||
loop {
|
||||
_ = io::server::run(
|
||||
&mut Server::<_, 64>::new_with_et(ip),
|
||||
&ServerOptions::new(ip, Some(&mut gw_buf)),
|
||||
&mut Server::<_, 64>::new_with_et(GW_IP),
|
||||
&ServerOptions::new(GW_IP, Some(&mut gw_buf)),
|
||||
&mut bound_socket,
|
||||
&mut buf,
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user