mirror of
				https://github.com/Djeeberjr/fw-anwesenheit.git
				synced 2025-11-04 07:34:10 +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_executor::Spawner;
 | 
				
			||||||
use embassy_net::{Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
 | 
					use embassy_net::{Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
 | 
				
			||||||
use embassy_time::{Duration, Timer};
 | 
					use embassy_time::{Duration, Timer};
 | 
				
			||||||
use esp_radio::wifi::WifiDevice;
 | 
					use esp_radio::wifi::WifiDevice;
 | 
				
			||||||
use static_cell::make_static;
 | 
					use static_cell::StaticCell;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::webserver::WEB_TAKS_SIZE;
 | 
					use crate::webserver::WEB_TAKS_SIZE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub const NETWORK_STACK_SIZE: usize = WEB_TAKS_SIZE + 2; // + 2 for other network taks. Breaks
 | 
					pub const NETWORK_STACK_SIZE: usize = WEB_TAKS_SIZE + 2; // + 2 for other network taks. Breaks without
 | 
				
			||||||
// 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> {
 | 
					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 {
 | 
					    let config = embassy_net::Config::ipv4_static(StaticConfigV4 {
 | 
				
			||||||
        address: Ipv4Cidr::new(gw_ip_addr, 24),
 | 
					        address: Ipv4Cidr::new(GW_IP, 24),
 | 
				
			||||||
        gateway: Some(gw_ip_addr),
 | 
					        gateway: Some(GW_IP),
 | 
				
			||||||
        dns_servers: Default::default(),
 | 
					        dns_servers: Default::default(),
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let nw_stack: &'static mut StackResources<NETWORK_STACK_SIZE> =
 | 
					    static NETWORK_STACK: StaticCell<StackResources<NETWORK_STACK_SIZE>> = StaticCell::new();
 | 
				
			||||||
        make_static!(StackResources::<NETWORK_STACK_SIZE>::new());
 | 
					    let nw_stack = NETWORK_STACK.init(StackResources::new());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let (stack, runner) = embassy_net::new(wifi, config, nw_stack, seed);
 | 
					    let (stack, runner) = embassy_net::new(wifi, config, nw_stack, seed);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    spawner.must_spawn(net_task(runner));
 | 
					    spawner.must_spawn(net_task(runner));
 | 
				
			||||||
    spawner.must_spawn(run_dhcp(stack, gw_ip_addr_str));
 | 
					    spawner.must_spawn(run_dhcp(stack));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    stack
 | 
					    stack
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[embassy_executor::task]
 | 
					#[embassy_executor::task]
 | 
				
			||||||
async fn run_dhcp(stack: Stack<'static>, gw_ip_addr: &'static str) {
 | 
					async fn run_dhcp(stack: Stack<'static>) {
 | 
				
			||||||
    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");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let mut buf = [0u8; 1500];
 | 
					    let mut buf = [0u8; 1500];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut gw_buf = [Ipv4Addr::UNSPECIFIED];
 | 
					    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,
 | 
					            DEFAULT_SERVER_PORT,
 | 
				
			||||||
        )))
 | 
					        )))
 | 
				
			||||||
        .await
 | 
					        .await
 | 
				
			||||||
        .unwrap();
 | 
					        .expect("Failed to bind socket for DHCP server");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    loop {
 | 
					    loop {
 | 
				
			||||||
        _ = io::server::run(
 | 
					        _ = io::server::run(
 | 
				
			||||||
            &mut Server::<_, 64>::new_with_et(ip),
 | 
					            &mut Server::<_, 64>::new_with_et(GW_IP),
 | 
				
			||||||
            &ServerOptions::new(ip, Some(&mut gw_buf)),
 | 
					            &ServerOptions::new(GW_IP, Some(&mut gw_buf)),
 | 
				
			||||||
            &mut bound_socket,
 | 
					            &mut bound_socket,
 | 
				
			||||||
            &mut buf,
 | 
					            &mut buf,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user