diff --git a/firmware/Cargo.lock b/firmware/Cargo.lock index c63c3e6..0bf7032 100644 --- a/firmware/Cargo.lock +++ b/firmware/Cargo.lock @@ -2052,7 +2052,6 @@ dependencies = [ "serde", "serde_json", "ssd1306", - "static_cell", "u8g2-fonts", ] diff --git a/firmware/Cargo.toml b/firmware/Cargo.toml index 869e89a..ef6a54d 100644 --- a/firmware/Cargo.toml +++ b/firmware/Cargo.toml @@ -71,5 +71,4 @@ reqwless = { version = "0.12", default-features = false, features = ["alloc", "e serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] } serde_json = { version = "1.0", default-features = false, features = ["alloc"] } ssd1306 = { version = "0.9", features = ["async"] } -static_cell = "2.1" u8g2-fonts = "0.4" diff --git a/firmware/src/wifi.rs b/firmware/src/wifi.rs index 37cca4e..cb27443 100644 --- a/firmware/src/wifi.rs +++ b/firmware/src/wifi.rs @@ -1,3 +1,4 @@ +use alloc::boxed::Box; use core::cell::Cell; use core::fmt; use embassy_executor::{task, Spawner}; @@ -16,7 +17,6 @@ use esp_wifi::wifi::{ use esp_wifi::{EspWifiInitFor, EspWifiTimerSource}; use log::{debug, info, warn}; use rand_core::RngCore; -use static_cell::StaticCell; /// Delay after Wifi disconnect or connection failure before trying to reconnect const CONNECT_RETRY_DELAY: Duration = Duration::from_millis(5000); @@ -33,18 +33,6 @@ const TX_BUFFER_SIZE: usize = 1024; /// Wifi initialization error pub use esp_wifi::InitializationError; -/// Static network stack -static STACK: StaticCell>> = StaticCell::new(); - -/// Static network stack resources (sockets, inflight dns queries) -// Needs at least one socket for DHCP, one socket for DNS, plus additional sockets for connections -static RESOURCES: StaticCell> = StaticCell::new(); - -/// Static TCP client state -static TCP_CLIENT_STATE: StaticCell< - TcpClientState, -> = StaticCell::new(); - /// Option display helper struct DisplayOption(Option); @@ -178,10 +166,18 @@ impl Wifi { Err(err) => panic!("Failed to spawn Wifi connection task: {:?}", err), } - // Initialize static network stack + // Following some resources are allocated and leaked to get a `&'static mut` reference. + // This is ok, since only one instance of `Wifi` can exist and it'll never be dropped. + + // Initialize network stack resources (sockets, inflight dns queries). Needs at least one + // socket for DHCP, one socket for DNS, plus additional sockets for connections. + let resources = Box::new(StackResources::<{ 2 + NUM_TCP_SOCKETS }>::new()); + let resources = Box::leak(resources); + + // Initialize network stack let config = Config::dhcpv4(DhcpConfig::default()); - let resources = RESOURCES.init(StackResources::new()); - let stack = STACK.init(Stack::new(device, config, resources, random_seed)); + let stack = Box::new(Stack::new(device, config, resources, random_seed)); + let stack = Box::leak(stack); // Spawn task for running network stack debug!("Wifi: Spawning network task..."); @@ -192,7 +188,8 @@ impl Wifi { } // Initialize TCP client state (reserves tx/rx buffers for TCP sockets) - let tcp_client_state = TCP_CLIENT_STATE.init(TcpClientState::new()); + let tcp_client_state = Box::new(TcpClientState::new()); + let tcp_client_state = Box::leak(tcp_client_state); info!("Wifi: Controller initialized"); Ok(Self {